ptrace.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. /*
  2. * Kernel support for the ptrace() and syscall tracing interfaces.
  3. *
  4. * Copyright (C) 1999-2005 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. *
  7. * Derived from the x86 and Alpha versions.
  8. */
  9. #include <linux/config.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/mm.h>
  14. #include <linux/errno.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/user.h>
  18. #include <linux/security.h>
  19. #include <linux/audit.h>
  20. #include <linux/signal.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/processor.h>
  23. #include <asm/ptrace_offsets.h>
  24. #include <asm/rse.h>
  25. #include <asm/system.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/unwind.h>
  28. #ifdef CONFIG_PERFMON
  29. #include <asm/perfmon.h>
  30. #endif
  31. #include "entry.h"
  32. /*
  33. * Bits in the PSR that we allow ptrace() to change:
  34. * be, up, ac, mfl, mfh (the user mask; five bits total)
  35. * db (debug breakpoint fault; one bit)
  36. * id (instruction debug fault disable; one bit)
  37. * dd (data debug fault disable; one bit)
  38. * ri (restart instruction; two bits)
  39. * is (instruction set; one bit)
  40. */
  41. #define IPSR_MASK (IA64_PSR_UM | IA64_PSR_DB | IA64_PSR_IS \
  42. | IA64_PSR_ID | IA64_PSR_DD | IA64_PSR_RI)
  43. #define MASK(nbits) ((1UL << (nbits)) - 1) /* mask with NBITS bits set */
  44. #define PFM_MASK MASK(38)
  45. #define PTRACE_DEBUG 0
  46. #if PTRACE_DEBUG
  47. # define dprintk(format...) printk(format)
  48. # define inline
  49. #else
  50. # define dprintk(format...)
  51. #endif
  52. /* Return TRUE if PT was created due to kernel-entry via a system-call. */
  53. static inline int
  54. in_syscall (struct pt_regs *pt)
  55. {
  56. return (long) pt->cr_ifs >= 0;
  57. }
  58. /*
  59. * Collect the NaT bits for r1-r31 from scratch_unat and return a NaT
  60. * bitset where bit i is set iff the NaT bit of register i is set.
  61. */
  62. unsigned long
  63. ia64_get_scratch_nat_bits (struct pt_regs *pt, unsigned long scratch_unat)
  64. {
  65. # define GET_BITS(first, last, unat) \
  66. ({ \
  67. unsigned long bit = ia64_unat_pos(&pt->r##first); \
  68. unsigned long nbits = (last - first + 1); \
  69. unsigned long mask = MASK(nbits) << first; \
  70. unsigned long dist; \
  71. if (bit < first) \
  72. dist = 64 + bit - first; \
  73. else \
  74. dist = bit - first; \
  75. ia64_rotr(unat, dist) & mask; \
  76. })
  77. unsigned long val;
  78. /*
  79. * Registers that are stored consecutively in struct pt_regs
  80. * can be handled in parallel. If the register order in
  81. * struct_pt_regs changes, this code MUST be updated.
  82. */
  83. val = GET_BITS( 1, 1, scratch_unat);
  84. val |= GET_BITS( 2, 3, scratch_unat);
  85. val |= GET_BITS(12, 13, scratch_unat);
  86. val |= GET_BITS(14, 14, scratch_unat);
  87. val |= GET_BITS(15, 15, scratch_unat);
  88. val |= GET_BITS( 8, 11, scratch_unat);
  89. val |= GET_BITS(16, 31, scratch_unat);
  90. return val;
  91. # undef GET_BITS
  92. }
  93. /*
  94. * Set the NaT bits for the scratch registers according to NAT and
  95. * return the resulting unat (assuming the scratch registers are
  96. * stored in PT).
  97. */
  98. unsigned long
  99. ia64_put_scratch_nat_bits (struct pt_regs *pt, unsigned long nat)
  100. {
  101. # define PUT_BITS(first, last, nat) \
  102. ({ \
  103. unsigned long bit = ia64_unat_pos(&pt->r##first); \
  104. unsigned long nbits = (last - first + 1); \
  105. unsigned long mask = MASK(nbits) << first; \
  106. long dist; \
  107. if (bit < first) \
  108. dist = 64 + bit - first; \
  109. else \
  110. dist = bit - first; \
  111. ia64_rotl(nat & mask, dist); \
  112. })
  113. unsigned long scratch_unat;
  114. /*
  115. * Registers that are stored consecutively in struct pt_regs
  116. * can be handled in parallel. If the register order in
  117. * struct_pt_regs changes, this code MUST be updated.
  118. */
  119. scratch_unat = PUT_BITS( 1, 1, nat);
  120. scratch_unat |= PUT_BITS( 2, 3, nat);
  121. scratch_unat |= PUT_BITS(12, 13, nat);
  122. scratch_unat |= PUT_BITS(14, 14, nat);
  123. scratch_unat |= PUT_BITS(15, 15, nat);
  124. scratch_unat |= PUT_BITS( 8, 11, nat);
  125. scratch_unat |= PUT_BITS(16, 31, nat);
  126. return scratch_unat;
  127. # undef PUT_BITS
  128. }
  129. #define IA64_MLX_TEMPLATE 0x2
  130. #define IA64_MOVL_OPCODE 6
  131. void
  132. ia64_increment_ip (struct pt_regs *regs)
  133. {
  134. unsigned long w0, ri = ia64_psr(regs)->ri + 1;
  135. if (ri > 2) {
  136. ri = 0;
  137. regs->cr_iip += 16;
  138. } else if (ri == 2) {
  139. get_user(w0, (char __user *) regs->cr_iip + 0);
  140. if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
  141. /*
  142. * rfi'ing to slot 2 of an MLX bundle causes
  143. * an illegal operation fault. We don't want
  144. * that to happen...
  145. */
  146. ri = 0;
  147. regs->cr_iip += 16;
  148. }
  149. }
  150. ia64_psr(regs)->ri = ri;
  151. }
  152. void
  153. ia64_decrement_ip (struct pt_regs *regs)
  154. {
  155. unsigned long w0, ri = ia64_psr(regs)->ri - 1;
  156. if (ia64_psr(regs)->ri == 0) {
  157. regs->cr_iip -= 16;
  158. ri = 2;
  159. get_user(w0, (char __user *) regs->cr_iip + 0);
  160. if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
  161. /*
  162. * rfi'ing to slot 2 of an MLX bundle causes
  163. * an illegal operation fault. We don't want
  164. * that to happen...
  165. */
  166. ri = 1;
  167. }
  168. }
  169. ia64_psr(regs)->ri = ri;
  170. }
  171. /*
  172. * This routine is used to read an rnat bits that are stored on the
  173. * kernel backing store. Since, in general, the alignment of the user
  174. * and kernel are different, this is not completely trivial. In
  175. * essence, we need to construct the user RNAT based on up to two
  176. * kernel RNAT values and/or the RNAT value saved in the child's
  177. * pt_regs.
  178. *
  179. * user rbs
  180. *
  181. * +--------+ <-- lowest address
  182. * | slot62 |
  183. * +--------+
  184. * | rnat | 0x....1f8
  185. * +--------+
  186. * | slot00 | \
  187. * +--------+ |
  188. * | slot01 | > child_regs->ar_rnat
  189. * +--------+ |
  190. * | slot02 | / kernel rbs
  191. * +--------+ +--------+
  192. * <- child_regs->ar_bspstore | slot61 | <-- krbs
  193. * +- - - - + +--------+
  194. * | slot62 |
  195. * +- - - - + +--------+
  196. * | rnat |
  197. * +- - - - + +--------+
  198. * vrnat | slot00 |
  199. * +- - - - + +--------+
  200. * = =
  201. * +--------+
  202. * | slot00 | \
  203. * +--------+ |
  204. * | slot01 | > child_stack->ar_rnat
  205. * +--------+ |
  206. * | slot02 | /
  207. * +--------+
  208. * <--- child_stack->ar_bspstore
  209. *
  210. * The way to think of this code is as follows: bit 0 in the user rnat
  211. * corresponds to some bit N (0 <= N <= 62) in one of the kernel rnat
  212. * value. The kernel rnat value holding this bit is stored in
  213. * variable rnat0. rnat1 is loaded with the kernel rnat value that
  214. * form the upper bits of the user rnat value.
  215. *
  216. * Boundary cases:
  217. *
  218. * o when reading the rnat "below" the first rnat slot on the kernel
  219. * backing store, rnat0/rnat1 are set to 0 and the low order bits are
  220. * merged in from pt->ar_rnat.
  221. *
  222. * o when reading the rnat "above" the last rnat slot on the kernel
  223. * backing store, rnat0/rnat1 gets its value from sw->ar_rnat.
  224. */
  225. static unsigned long
  226. get_rnat (struct task_struct *task, struct switch_stack *sw,
  227. unsigned long *krbs, unsigned long *urnat_addr,
  228. unsigned long *urbs_end)
  229. {
  230. unsigned long rnat0 = 0, rnat1 = 0, urnat = 0, *slot0_kaddr;
  231. unsigned long umask = 0, mask, m;
  232. unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
  233. long num_regs, nbits;
  234. struct pt_regs *pt;
  235. pt = task_pt_regs(task);
  236. kbsp = (unsigned long *) sw->ar_bspstore;
  237. ubspstore = (unsigned long *) pt->ar_bspstore;
  238. if (urbs_end < urnat_addr)
  239. nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);
  240. else
  241. nbits = 63;
  242. mask = MASK(nbits);
  243. /*
  244. * First, figure out which bit number slot 0 in user-land maps
  245. * to in the kernel rnat. Do this by figuring out how many
  246. * register slots we're beyond the user's backingstore and
  247. * then computing the equivalent address in kernel space.
  248. */
  249. num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
  250. slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
  251. shift = ia64_rse_slot_num(slot0_kaddr);
  252. rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
  253. rnat0_kaddr = rnat1_kaddr - 64;
  254. if (ubspstore + 63 > urnat_addr) {
  255. /* some bits need to be merged in from pt->ar_rnat */
  256. umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
  257. urnat = (pt->ar_rnat & umask);
  258. mask &= ~umask;
  259. if (!mask)
  260. return urnat;
  261. }
  262. m = mask << shift;
  263. if (rnat0_kaddr >= kbsp)
  264. rnat0 = sw->ar_rnat;
  265. else if (rnat0_kaddr > krbs)
  266. rnat0 = *rnat0_kaddr;
  267. urnat |= (rnat0 & m) >> shift;
  268. m = mask >> (63 - shift);
  269. if (rnat1_kaddr >= kbsp)
  270. rnat1 = sw->ar_rnat;
  271. else if (rnat1_kaddr > krbs)
  272. rnat1 = *rnat1_kaddr;
  273. urnat |= (rnat1 & m) << (63 - shift);
  274. return urnat;
  275. }
  276. /*
  277. * The reverse of get_rnat.
  278. */
  279. static void
  280. put_rnat (struct task_struct *task, struct switch_stack *sw,
  281. unsigned long *krbs, unsigned long *urnat_addr, unsigned long urnat,
  282. unsigned long *urbs_end)
  283. {
  284. unsigned long rnat0 = 0, rnat1 = 0, *slot0_kaddr, umask = 0, mask, m;
  285. unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
  286. long num_regs, nbits;
  287. struct pt_regs *pt;
  288. unsigned long cfm, *urbs_kargs;
  289. pt = task_pt_regs(task);
  290. kbsp = (unsigned long *) sw->ar_bspstore;
  291. ubspstore = (unsigned long *) pt->ar_bspstore;
  292. urbs_kargs = urbs_end;
  293. if (in_syscall(pt)) {
  294. /*
  295. * If entered via syscall, don't allow user to set rnat bits
  296. * for syscall args.
  297. */
  298. cfm = pt->cr_ifs;
  299. urbs_kargs = ia64_rse_skip_regs(urbs_end, -(cfm & 0x7f));
  300. }
  301. if (urbs_kargs >= urnat_addr)
  302. nbits = 63;
  303. else {
  304. if ((urnat_addr - 63) >= urbs_kargs)
  305. return;
  306. nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);
  307. }
  308. mask = MASK(nbits);
  309. /*
  310. * First, figure out which bit number slot 0 in user-land maps
  311. * to in the kernel rnat. Do this by figuring out how many
  312. * register slots we're beyond the user's backingstore and
  313. * then computing the equivalent address in kernel space.
  314. */
  315. num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
  316. slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
  317. shift = ia64_rse_slot_num(slot0_kaddr);
  318. rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
  319. rnat0_kaddr = rnat1_kaddr - 64;
  320. if (ubspstore + 63 > urnat_addr) {
  321. /* some bits need to be place in pt->ar_rnat: */
  322. umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
  323. pt->ar_rnat = (pt->ar_rnat & ~umask) | (urnat & umask);
  324. mask &= ~umask;
  325. if (!mask)
  326. return;
  327. }
  328. /*
  329. * Note: Section 11.1 of the EAS guarantees that bit 63 of an
  330. * rnat slot is ignored. so we don't have to clear it here.
  331. */
  332. rnat0 = (urnat << shift);
  333. m = mask << shift;
  334. if (rnat0_kaddr >= kbsp)
  335. sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat0 & m);
  336. else if (rnat0_kaddr > krbs)
  337. *rnat0_kaddr = ((*rnat0_kaddr & ~m) | (rnat0 & m));
  338. rnat1 = (urnat >> (63 - shift));
  339. m = mask >> (63 - shift);
  340. if (rnat1_kaddr >= kbsp)
  341. sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat1 & m);
  342. else if (rnat1_kaddr > krbs)
  343. *rnat1_kaddr = ((*rnat1_kaddr & ~m) | (rnat1 & m));
  344. }
  345. static inline int
  346. on_kernel_rbs (unsigned long addr, unsigned long bspstore,
  347. unsigned long urbs_end)
  348. {
  349. unsigned long *rnat_addr = ia64_rse_rnat_addr((unsigned long *)
  350. urbs_end);
  351. return (addr >= bspstore && addr <= (unsigned long) rnat_addr);
  352. }
  353. /*
  354. * Read a word from the user-level backing store of task CHILD. ADDR
  355. * is the user-level address to read the word from, VAL a pointer to
  356. * the return value, and USER_BSP gives the end of the user-level
  357. * backing store (i.e., it's the address that would be in ar.bsp after
  358. * the user executed a "cover" instruction).
  359. *
  360. * This routine takes care of accessing the kernel register backing
  361. * store for those registers that got spilled there. It also takes
  362. * care of calculating the appropriate RNaT collection words.
  363. */
  364. long
  365. ia64_peek (struct task_struct *child, struct switch_stack *child_stack,
  366. unsigned long user_rbs_end, unsigned long addr, long *val)
  367. {
  368. unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;
  369. struct pt_regs *child_regs;
  370. size_t copied;
  371. long ret;
  372. urbs_end = (long *) user_rbs_end;
  373. laddr = (unsigned long *) addr;
  374. child_regs = task_pt_regs(child);
  375. bspstore = (unsigned long *) child_regs->ar_bspstore;
  376. krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
  377. if (on_kernel_rbs(addr, (unsigned long) bspstore,
  378. (unsigned long) urbs_end))
  379. {
  380. /*
  381. * Attempt to read the RBS in an area that's actually
  382. * on the kernel RBS => read the corresponding bits in
  383. * the kernel RBS.
  384. */
  385. rnat_addr = ia64_rse_rnat_addr(laddr);
  386. ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);
  387. if (laddr == rnat_addr) {
  388. /* return NaT collection word itself */
  389. *val = ret;
  390. return 0;
  391. }
  392. if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {
  393. /*
  394. * It is implementation dependent whether the
  395. * data portion of a NaT value gets saved on a
  396. * st8.spill or RSE spill (e.g., see EAS 2.6,
  397. * 4.4.4.6 Register Spill and Fill). To get
  398. * consistent behavior across all possible
  399. * IA-64 implementations, we return zero in
  400. * this case.
  401. */
  402. *val = 0;
  403. return 0;
  404. }
  405. if (laddr < urbs_end) {
  406. /*
  407. * The desired word is on the kernel RBS and
  408. * is not a NaT.
  409. */
  410. regnum = ia64_rse_num_regs(bspstore, laddr);
  411. *val = *ia64_rse_skip_regs(krbs, regnum);
  412. return 0;
  413. }
  414. }
  415. copied = access_process_vm(child, addr, &ret, sizeof(ret), 0);
  416. if (copied != sizeof(ret))
  417. return -EIO;
  418. *val = ret;
  419. return 0;
  420. }
  421. long
  422. ia64_poke (struct task_struct *child, struct switch_stack *child_stack,
  423. unsigned long user_rbs_end, unsigned long addr, long val)
  424. {
  425. unsigned long *bspstore, *krbs, regnum, *laddr;
  426. unsigned long *urbs_end = (long *) user_rbs_end;
  427. struct pt_regs *child_regs;
  428. laddr = (unsigned long *) addr;
  429. child_regs = task_pt_regs(child);
  430. bspstore = (unsigned long *) child_regs->ar_bspstore;
  431. krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
  432. if (on_kernel_rbs(addr, (unsigned long) bspstore,
  433. (unsigned long) urbs_end))
  434. {
  435. /*
  436. * Attempt to write the RBS in an area that's actually
  437. * on the kernel RBS => write the corresponding bits
  438. * in the kernel RBS.
  439. */
  440. if (ia64_rse_is_rnat_slot(laddr))
  441. put_rnat(child, child_stack, krbs, laddr, val,
  442. urbs_end);
  443. else {
  444. if (laddr < urbs_end) {
  445. regnum = ia64_rse_num_regs(bspstore, laddr);
  446. *ia64_rse_skip_regs(krbs, regnum) = val;
  447. }
  448. }
  449. } else if (access_process_vm(child, addr, &val, sizeof(val), 1)
  450. != sizeof(val))
  451. return -EIO;
  452. return 0;
  453. }
  454. /*
  455. * Calculate the address of the end of the user-level register backing
  456. * store. This is the address that would have been stored in ar.bsp
  457. * if the user had executed a "cover" instruction right before
  458. * entering the kernel. If CFMP is not NULL, it is used to return the
  459. * "current frame mask" that was active at the time the kernel was
  460. * entered.
  461. */
  462. unsigned long
  463. ia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt,
  464. unsigned long *cfmp)
  465. {
  466. unsigned long *krbs, *bspstore, cfm = pt->cr_ifs;
  467. long ndirty;
  468. krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
  469. bspstore = (unsigned long *) pt->ar_bspstore;
  470. ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
  471. if (in_syscall(pt))
  472. ndirty += (cfm & 0x7f);
  473. else
  474. cfm &= ~(1UL << 63); /* clear valid bit */
  475. if (cfmp)
  476. *cfmp = cfm;
  477. return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);
  478. }
  479. /*
  480. * Synchronize (i.e, write) the RSE backing store living in kernel
  481. * space to the VM of the CHILD task. SW and PT are the pointers to
  482. * the switch_stack and pt_regs structures, respectively.
  483. * USER_RBS_END is the user-level address at which the backing store
  484. * ends.
  485. */
  486. long
  487. ia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,
  488. unsigned long user_rbs_start, unsigned long user_rbs_end)
  489. {
  490. unsigned long addr, val;
  491. long ret;
  492. /* now copy word for word from kernel rbs to user rbs: */
  493. for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
  494. ret = ia64_peek(child, sw, user_rbs_end, addr, &val);
  495. if (ret < 0)
  496. return ret;
  497. if (access_process_vm(child, addr, &val, sizeof(val), 1)
  498. != sizeof(val))
  499. return -EIO;
  500. }
  501. return 0;
  502. }
  503. static inline int
  504. thread_matches (struct task_struct *thread, unsigned long addr)
  505. {
  506. unsigned long thread_rbs_end;
  507. struct pt_regs *thread_regs;
  508. if (ptrace_check_attach(thread, 0) < 0)
  509. /*
  510. * If the thread is not in an attachable state, we'll
  511. * ignore it. The net effect is that if ADDR happens
  512. * to overlap with the portion of the thread's
  513. * register backing store that is currently residing
  514. * on the thread's kernel stack, then ptrace() may end
  515. * up accessing a stale value. But if the thread
  516. * isn't stopped, that's a problem anyhow, so we're
  517. * doing as well as we can...
  518. */
  519. return 0;
  520. thread_regs = task_pt_regs(thread);
  521. thread_rbs_end = ia64_get_user_rbs_end(thread, thread_regs, NULL);
  522. if (!on_kernel_rbs(addr, thread_regs->ar_bspstore, thread_rbs_end))
  523. return 0;
  524. return 1; /* looks like we've got a winner */
  525. }
  526. /*
  527. * GDB apparently wants to be able to read the register-backing store
  528. * of any thread when attached to a given process. If we are peeking
  529. * or poking an address that happens to reside in the kernel-backing
  530. * store of another thread, we need to attach to that thread, because
  531. * otherwise we end up accessing stale data.
  532. *
  533. * task_list_lock must be read-locked before calling this routine!
  534. */
  535. static struct task_struct *
  536. find_thread_for_addr (struct task_struct *child, unsigned long addr)
  537. {
  538. struct task_struct *p;
  539. struct mm_struct *mm;
  540. struct list_head *this, *next;
  541. int mm_users;
  542. if (!(mm = get_task_mm(child)))
  543. return child;
  544. /* -1 because of our get_task_mm(): */
  545. mm_users = atomic_read(&mm->mm_users) - 1;
  546. if (mm_users <= 1)
  547. goto out; /* not multi-threaded */
  548. /*
  549. * Traverse the current process' children list. Every task that
  550. * one attaches to becomes a child. And it is only attached children
  551. * of the debugger that are of interest (ptrace_check_attach checks
  552. * for this).
  553. */
  554. list_for_each_safe(this, next, &current->children) {
  555. p = list_entry(this, struct task_struct, sibling);
  556. if (p->mm != mm)
  557. continue;
  558. if (thread_matches(p, addr)) {
  559. child = p;
  560. goto out;
  561. }
  562. }
  563. out:
  564. mmput(mm);
  565. return child;
  566. }
  567. /*
  568. * Write f32-f127 back to task->thread.fph if it has been modified.
  569. */
  570. inline void
  571. ia64_flush_fph (struct task_struct *task)
  572. {
  573. struct ia64_psr *psr = ia64_psr(task_pt_regs(task));
  574. /*
  575. * Prevent migrating this task while
  576. * we're fiddling with the FPU state
  577. */
  578. preempt_disable();
  579. if (ia64_is_local_fpu_owner(task) && psr->mfh) {
  580. psr->mfh = 0;
  581. task->thread.flags |= IA64_THREAD_FPH_VALID;
  582. ia64_save_fpu(&task->thread.fph[0]);
  583. }
  584. preempt_enable();
  585. }
  586. /*
  587. * Sync the fph state of the task so that it can be manipulated
  588. * through thread.fph. If necessary, f32-f127 are written back to
  589. * thread.fph or, if the fph state hasn't been used before, thread.fph
  590. * is cleared to zeroes. Also, access to f32-f127 is disabled to
  591. * ensure that the task picks up the state from thread.fph when it
  592. * executes again.
  593. */
  594. void
  595. ia64_sync_fph (struct task_struct *task)
  596. {
  597. struct ia64_psr *psr = ia64_psr(task_pt_regs(task));
  598. ia64_flush_fph(task);
  599. if (!(task->thread.flags & IA64_THREAD_FPH_VALID)) {
  600. task->thread.flags |= IA64_THREAD_FPH_VALID;
  601. memset(&task->thread.fph, 0, sizeof(task->thread.fph));
  602. }
  603. ia64_drop_fpu(task);
  604. psr->dfh = 1;
  605. }
  606. static int
  607. access_fr (struct unw_frame_info *info, int regnum, int hi,
  608. unsigned long *data, int write_access)
  609. {
  610. struct ia64_fpreg fpval;
  611. int ret;
  612. ret = unw_get_fr(info, regnum, &fpval);
  613. if (ret < 0)
  614. return ret;
  615. if (write_access) {
  616. fpval.u.bits[hi] = *data;
  617. ret = unw_set_fr(info, regnum, fpval);
  618. } else
  619. *data = fpval.u.bits[hi];
  620. return ret;
  621. }
  622. /*
  623. * Change the machine-state of CHILD such that it will return via the normal
  624. * kernel exit-path, rather than the syscall-exit path.
  625. */
  626. static void
  627. convert_to_non_syscall (struct task_struct *child, struct pt_regs *pt,
  628. unsigned long cfm)
  629. {
  630. struct unw_frame_info info, prev_info;
  631. unsigned long ip, sp, pr;
  632. unw_init_from_blocked_task(&info, child);
  633. while (1) {
  634. prev_info = info;
  635. if (unw_unwind(&info) < 0)
  636. return;
  637. unw_get_sp(&info, &sp);
  638. if ((long)((unsigned long)child + IA64_STK_OFFSET - sp)
  639. < IA64_PT_REGS_SIZE) {
  640. dprintk("ptrace.%s: ran off the top of the kernel "
  641. "stack\n", __FUNCTION__);
  642. return;
  643. }
  644. if (unw_get_pr (&prev_info, &pr) < 0) {
  645. unw_get_rp(&prev_info, &ip);
  646. dprintk("ptrace.%s: failed to read "
  647. "predicate register (ip=0x%lx)\n",
  648. __FUNCTION__, ip);
  649. return;
  650. }
  651. if (unw_is_intr_frame(&info)
  652. && (pr & (1UL << PRED_USER_STACK)))
  653. break;
  654. }
  655. /*
  656. * Note: at the time of this call, the target task is blocked
  657. * in notify_resume_user() and by clearling PRED_LEAVE_SYSCALL
  658. * (aka, "pLvSys") we redirect execution from
  659. * .work_pending_syscall_end to .work_processed_kernel.
  660. */
  661. unw_get_pr(&prev_info, &pr);
  662. pr &= ~((1UL << PRED_SYSCALL) | (1UL << PRED_LEAVE_SYSCALL));
  663. pr |= (1UL << PRED_NON_SYSCALL);
  664. unw_set_pr(&prev_info, pr);
  665. pt->cr_ifs = (1UL << 63) | cfm;
  666. /*
  667. * Clear the memory that is NOT written on syscall-entry to
  668. * ensure we do not leak kernel-state to user when execution
  669. * resumes.
  670. */
  671. pt->r2 = 0;
  672. pt->r3 = 0;
  673. pt->r14 = 0;
  674. memset(&pt->r16, 0, 16*8); /* clear r16-r31 */
  675. memset(&pt->f6, 0, 6*16); /* clear f6-f11 */
  676. pt->b7 = 0;
  677. pt->ar_ccv = 0;
  678. pt->ar_csd = 0;
  679. pt->ar_ssd = 0;
  680. }
  681. static int
  682. access_nat_bits (struct task_struct *child, struct pt_regs *pt,
  683. struct unw_frame_info *info,
  684. unsigned long *data, int write_access)
  685. {
  686. unsigned long regnum, nat_bits, scratch_unat, dummy = 0;
  687. char nat = 0;
  688. if (write_access) {
  689. nat_bits = *data;
  690. scratch_unat = ia64_put_scratch_nat_bits(pt, nat_bits);
  691. if (unw_set_ar(info, UNW_AR_UNAT, scratch_unat) < 0) {
  692. dprintk("ptrace: failed to set ar.unat\n");
  693. return -1;
  694. }
  695. for (regnum = 4; regnum <= 7; ++regnum) {
  696. unw_get_gr(info, regnum, &dummy, &nat);
  697. unw_set_gr(info, regnum, dummy,
  698. (nat_bits >> regnum) & 1);
  699. }
  700. } else {
  701. if (unw_get_ar(info, UNW_AR_UNAT, &scratch_unat) < 0) {
  702. dprintk("ptrace: failed to read ar.unat\n");
  703. return -1;
  704. }
  705. nat_bits = ia64_get_scratch_nat_bits(pt, scratch_unat);
  706. for (regnum = 4; regnum <= 7; ++regnum) {
  707. unw_get_gr(info, regnum, &dummy, &nat);
  708. nat_bits |= (nat != 0) << regnum;
  709. }
  710. *data = nat_bits;
  711. }
  712. return 0;
  713. }
  714. static int
  715. access_uarea (struct task_struct *child, unsigned long addr,
  716. unsigned long *data, int write_access)
  717. {
  718. unsigned long *ptr, regnum, urbs_end, rnat_addr, cfm;
  719. struct switch_stack *sw;
  720. struct pt_regs *pt;
  721. # define pt_reg_addr(pt, reg) ((void *) \
  722. ((unsigned long) (pt) \
  723. + offsetof(struct pt_regs, reg)))
  724. pt = task_pt_regs(child);
  725. sw = (struct switch_stack *) (child->thread.ksp + 16);
  726. if ((addr & 0x7) != 0) {
  727. dprintk("ptrace: unaligned register address 0x%lx\n", addr);
  728. return -1;
  729. }
  730. if (addr < PT_F127 + 16) {
  731. /* accessing fph */
  732. if (write_access)
  733. ia64_sync_fph(child);
  734. else
  735. ia64_flush_fph(child);
  736. ptr = (unsigned long *)
  737. ((unsigned long) &child->thread.fph + addr);
  738. } else if ((addr >= PT_F10) && (addr < PT_F11 + 16)) {
  739. /* scratch registers untouched by kernel (saved in pt_regs) */
  740. ptr = pt_reg_addr(pt, f10) + (addr - PT_F10);
  741. } else if (addr >= PT_F12 && addr < PT_F15 + 16) {
  742. /*
  743. * Scratch registers untouched by kernel (saved in
  744. * switch_stack).
  745. */
  746. ptr = (unsigned long *) ((long) sw
  747. + (addr - PT_NAT_BITS - 32));
  748. } else if (addr < PT_AR_LC + 8) {
  749. /* preserved state: */
  750. struct unw_frame_info info;
  751. char nat = 0;
  752. int ret;
  753. unw_init_from_blocked_task(&info, child);
  754. if (unw_unwind_to_user(&info) < 0)
  755. return -1;
  756. switch (addr) {
  757. case PT_NAT_BITS:
  758. return access_nat_bits(child, pt, &info,
  759. data, write_access);
  760. case PT_R4: case PT_R5: case PT_R6: case PT_R7:
  761. if (write_access) {
  762. /* read NaT bit first: */
  763. unsigned long dummy;
  764. ret = unw_get_gr(&info, (addr - PT_R4)/8 + 4,
  765. &dummy, &nat);
  766. if (ret < 0)
  767. return ret;
  768. }
  769. return unw_access_gr(&info, (addr - PT_R4)/8 + 4, data,
  770. &nat, write_access);
  771. case PT_B1: case PT_B2: case PT_B3:
  772. case PT_B4: case PT_B5:
  773. return unw_access_br(&info, (addr - PT_B1)/8 + 1, data,
  774. write_access);
  775. case PT_AR_EC:
  776. return unw_access_ar(&info, UNW_AR_EC, data,
  777. write_access);
  778. case PT_AR_LC:
  779. return unw_access_ar(&info, UNW_AR_LC, data,
  780. write_access);
  781. default:
  782. if (addr >= PT_F2 && addr < PT_F5 + 16)
  783. return access_fr(&info, (addr - PT_F2)/16 + 2,
  784. (addr & 8) != 0, data,
  785. write_access);
  786. else if (addr >= PT_F16 && addr < PT_F31 + 16)
  787. return access_fr(&info,
  788. (addr - PT_F16)/16 + 16,
  789. (addr & 8) != 0,
  790. data, write_access);
  791. else {
  792. dprintk("ptrace: rejecting access to register "
  793. "address 0x%lx\n", addr);
  794. return -1;
  795. }
  796. }
  797. } else if (addr < PT_F9+16) {
  798. /* scratch state */
  799. switch (addr) {
  800. case PT_AR_BSP:
  801. /*
  802. * By convention, we use PT_AR_BSP to refer to
  803. * the end of the user-level backing store.
  804. * Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof)
  805. * to get the real value of ar.bsp at the time
  806. * the kernel was entered.
  807. *
  808. * Furthermore, when changing the contents of
  809. * PT_AR_BSP (or PT_CFM) we MUST copy any
  810. * users-level stacked registers that are
  811. * stored on the kernel stack back to
  812. * user-space because otherwise, we might end
  813. * up clobbering kernel stacked registers.
  814. * Also, if this happens while the task is
  815. * blocked in a system call, which convert the
  816. * state such that the non-system-call exit
  817. * path is used. This ensures that the proper
  818. * state will be picked up when resuming
  819. * execution. However, it *also* means that
  820. * once we write PT_AR_BSP/PT_CFM, it won't be
  821. * possible to modify the syscall arguments of
  822. * the pending system call any longer. This
  823. * shouldn't be an issue because modifying
  824. * PT_AR_BSP/PT_CFM generally implies that
  825. * we're either abandoning the pending system
  826. * call or that we defer it's re-execution
  827. * (e.g., due to GDB doing an inferior
  828. * function call).
  829. */
  830. urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
  831. if (write_access) {
  832. if (*data != urbs_end) {
  833. if (ia64_sync_user_rbs(child, sw,
  834. pt->ar_bspstore,
  835. urbs_end) < 0)
  836. return -1;
  837. if (in_syscall(pt))
  838. convert_to_non_syscall(child,
  839. pt,
  840. cfm);
  841. /*
  842. * Simulate user-level write
  843. * of ar.bsp:
  844. */
  845. pt->loadrs = 0;
  846. pt->ar_bspstore = *data;
  847. }
  848. } else
  849. *data = urbs_end;
  850. return 0;
  851. case PT_CFM:
  852. urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
  853. if (write_access) {
  854. if (((cfm ^ *data) & PFM_MASK) != 0) {
  855. if (ia64_sync_user_rbs(child, sw,
  856. pt->ar_bspstore,
  857. urbs_end) < 0)
  858. return -1;
  859. if (in_syscall(pt))
  860. convert_to_non_syscall(child,
  861. pt,
  862. cfm);
  863. pt->cr_ifs = ((pt->cr_ifs & ~PFM_MASK)
  864. | (*data & PFM_MASK));
  865. }
  866. } else
  867. *data = cfm;
  868. return 0;
  869. case PT_CR_IPSR:
  870. if (write_access)
  871. pt->cr_ipsr = ((*data & IPSR_MASK)
  872. | (pt->cr_ipsr & ~IPSR_MASK));
  873. else
  874. *data = (pt->cr_ipsr & IPSR_MASK);
  875. return 0;
  876. case PT_AR_RSC:
  877. if (write_access)
  878. pt->ar_rsc = *data | (3 << 2); /* force PL3 */
  879. else
  880. *data = pt->ar_rsc;
  881. return 0;
  882. case PT_AR_RNAT:
  883. urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
  884. rnat_addr = (long) ia64_rse_rnat_addr((long *)
  885. urbs_end);
  886. if (write_access)
  887. return ia64_poke(child, sw, urbs_end,
  888. rnat_addr, *data);
  889. else
  890. return ia64_peek(child, sw, urbs_end,
  891. rnat_addr, data);
  892. case PT_R1:
  893. ptr = pt_reg_addr(pt, r1);
  894. break;
  895. case PT_R2: case PT_R3:
  896. ptr = pt_reg_addr(pt, r2) + (addr - PT_R2);
  897. break;
  898. case PT_R8: case PT_R9: case PT_R10: case PT_R11:
  899. ptr = pt_reg_addr(pt, r8) + (addr - PT_R8);
  900. break;
  901. case PT_R12: case PT_R13:
  902. ptr = pt_reg_addr(pt, r12) + (addr - PT_R12);
  903. break;
  904. case PT_R14:
  905. ptr = pt_reg_addr(pt, r14);
  906. break;
  907. case PT_R15:
  908. ptr = pt_reg_addr(pt, r15);
  909. break;
  910. case PT_R16: case PT_R17: case PT_R18: case PT_R19:
  911. case PT_R20: case PT_R21: case PT_R22: case PT_R23:
  912. case PT_R24: case PT_R25: case PT_R26: case PT_R27:
  913. case PT_R28: case PT_R29: case PT_R30: case PT_R31:
  914. ptr = pt_reg_addr(pt, r16) + (addr - PT_R16);
  915. break;
  916. case PT_B0:
  917. ptr = pt_reg_addr(pt, b0);
  918. break;
  919. case PT_B6:
  920. ptr = pt_reg_addr(pt, b6);
  921. break;
  922. case PT_B7:
  923. ptr = pt_reg_addr(pt, b7);
  924. break;
  925. case PT_F6: case PT_F6+8: case PT_F7: case PT_F7+8:
  926. case PT_F8: case PT_F8+8: case PT_F9: case PT_F9+8:
  927. ptr = pt_reg_addr(pt, f6) + (addr - PT_F6);
  928. break;
  929. case PT_AR_BSPSTORE:
  930. ptr = pt_reg_addr(pt, ar_bspstore);
  931. break;
  932. case PT_AR_UNAT:
  933. ptr = pt_reg_addr(pt, ar_unat);
  934. break;
  935. case PT_AR_PFS:
  936. ptr = pt_reg_addr(pt, ar_pfs);
  937. break;
  938. case PT_AR_CCV:
  939. ptr = pt_reg_addr(pt, ar_ccv);
  940. break;
  941. case PT_AR_FPSR:
  942. ptr = pt_reg_addr(pt, ar_fpsr);
  943. break;
  944. case PT_CR_IIP:
  945. ptr = pt_reg_addr(pt, cr_iip);
  946. break;
  947. case PT_PR:
  948. ptr = pt_reg_addr(pt, pr);
  949. break;
  950. /* scratch register */
  951. default:
  952. /* disallow accessing anything else... */
  953. dprintk("ptrace: rejecting access to register "
  954. "address 0x%lx\n", addr);
  955. return -1;
  956. }
  957. } else if (addr <= PT_AR_SSD) {
  958. ptr = pt_reg_addr(pt, ar_csd) + (addr - PT_AR_CSD);
  959. } else {
  960. /* access debug registers */
  961. if (addr >= PT_IBR) {
  962. regnum = (addr - PT_IBR) >> 3;
  963. ptr = &child->thread.ibr[0];
  964. } else {
  965. regnum = (addr - PT_DBR) >> 3;
  966. ptr = &child->thread.dbr[0];
  967. }
  968. if (regnum >= 8) {
  969. dprintk("ptrace: rejecting access to register "
  970. "address 0x%lx\n", addr);
  971. return -1;
  972. }
  973. #ifdef CONFIG_PERFMON
  974. /*
  975. * Check if debug registers are used by perfmon. This
  976. * test must be done once we know that we can do the
  977. * operation, i.e. the arguments are all valid, but
  978. * before we start modifying the state.
  979. *
  980. * Perfmon needs to keep a count of how many processes
  981. * are trying to modify the debug registers for system
  982. * wide monitoring sessions.
  983. *
  984. * We also include read access here, because they may
  985. * cause the PMU-installed debug register state
  986. * (dbr[], ibr[]) to be reset. The two arrays are also
  987. * used by perfmon, but we do not use
  988. * IA64_THREAD_DBG_VALID. The registers are restored
  989. * by the PMU context switch code.
  990. */
  991. if (pfm_use_debug_registers(child)) return -1;
  992. #endif
  993. if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
  994. child->thread.flags |= IA64_THREAD_DBG_VALID;
  995. memset(child->thread.dbr, 0,
  996. sizeof(child->thread.dbr));
  997. memset(child->thread.ibr, 0,
  998. sizeof(child->thread.ibr));
  999. }
  1000. ptr += regnum;
  1001. if ((regnum & 1) && write_access) {
  1002. /* don't let the user set kernel-level breakpoints: */
  1003. *ptr = *data & ~(7UL << 56);
  1004. return 0;
  1005. }
  1006. }
  1007. if (write_access)
  1008. *ptr = *data;
  1009. else
  1010. *data = *ptr;
  1011. return 0;
  1012. }
  1013. static long
  1014. ptrace_getregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
  1015. {
  1016. unsigned long psr, ec, lc, rnat, bsp, cfm, nat_bits, val;
  1017. struct unw_frame_info info;
  1018. struct ia64_fpreg fpval;
  1019. struct switch_stack *sw;
  1020. struct pt_regs *pt;
  1021. long ret, retval = 0;
  1022. char nat = 0;
  1023. int i;
  1024. if (!access_ok(VERIFY_WRITE, ppr, sizeof(struct pt_all_user_regs)))
  1025. return -EIO;
  1026. pt = task_pt_regs(child);
  1027. sw = (struct switch_stack *) (child->thread.ksp + 16);
  1028. unw_init_from_blocked_task(&info, child);
  1029. if (unw_unwind_to_user(&info) < 0) {
  1030. return -EIO;
  1031. }
  1032. if (((unsigned long) ppr & 0x7) != 0) {
  1033. dprintk("ptrace:unaligned register address %p\n", ppr);
  1034. return -EIO;
  1035. }
  1036. if (access_uarea(child, PT_CR_IPSR, &psr, 0) < 0
  1037. || access_uarea(child, PT_AR_EC, &ec, 0) < 0
  1038. || access_uarea(child, PT_AR_LC, &lc, 0) < 0
  1039. || access_uarea(child, PT_AR_RNAT, &rnat, 0) < 0
  1040. || access_uarea(child, PT_AR_BSP, &bsp, 0) < 0
  1041. || access_uarea(child, PT_CFM, &cfm, 0)
  1042. || access_uarea(child, PT_NAT_BITS, &nat_bits, 0))
  1043. return -EIO;
  1044. /* control regs */
  1045. retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
  1046. retval |= __put_user(psr, &ppr->cr_ipsr);
  1047. /* app regs */
  1048. retval |= __put_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
  1049. retval |= __put_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
  1050. retval |= __put_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
  1051. retval |= __put_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
  1052. retval |= __put_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
  1053. retval |= __put_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
  1054. retval |= __put_user(ec, &ppr->ar[PT_AUR_EC]);
  1055. retval |= __put_user(lc, &ppr->ar[PT_AUR_LC]);
  1056. retval |= __put_user(rnat, &ppr->ar[PT_AUR_RNAT]);
  1057. retval |= __put_user(bsp, &ppr->ar[PT_AUR_BSP]);
  1058. retval |= __put_user(cfm, &ppr->cfm);
  1059. /* gr1-gr3 */
  1060. retval |= __copy_to_user(&ppr->gr[1], &pt->r1, sizeof(long));
  1061. retval |= __copy_to_user(&ppr->gr[2], &pt->r2, sizeof(long) *2);
  1062. /* gr4-gr7 */
  1063. for (i = 4; i < 8; i++) {
  1064. if (unw_access_gr(&info, i, &val, &nat, 0) < 0)
  1065. return -EIO;
  1066. retval |= __put_user(val, &ppr->gr[i]);
  1067. }
  1068. /* gr8-gr11 */
  1069. retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
  1070. /* gr12-gr15 */
  1071. retval |= __copy_to_user(&ppr->gr[12], &pt->r12, sizeof(long) * 2);
  1072. retval |= __copy_to_user(&ppr->gr[14], &pt->r14, sizeof(long));
  1073. retval |= __copy_to_user(&ppr->gr[15], &pt->r15, sizeof(long));
  1074. /* gr16-gr31 */
  1075. retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
  1076. /* b0 */
  1077. retval |= __put_user(pt->b0, &ppr->br[0]);
  1078. /* b1-b5 */
  1079. for (i = 1; i < 6; i++) {
  1080. if (unw_access_br(&info, i, &val, 0) < 0)
  1081. return -EIO;
  1082. __put_user(val, &ppr->br[i]);
  1083. }
  1084. /* b6-b7 */
  1085. retval |= __put_user(pt->b6, &ppr->br[6]);
  1086. retval |= __put_user(pt->b7, &ppr->br[7]);
  1087. /* fr2-fr5 */
  1088. for (i = 2; i < 6; i++) {
  1089. if (unw_get_fr(&info, i, &fpval) < 0)
  1090. return -EIO;
  1091. retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
  1092. }
  1093. /* fr6-fr11 */
  1094. retval |= __copy_to_user(&ppr->fr[6], &pt->f6,
  1095. sizeof(struct ia64_fpreg) * 6);
  1096. /* fp scratch regs(12-15) */
  1097. retval |= __copy_to_user(&ppr->fr[12], &sw->f12,
  1098. sizeof(struct ia64_fpreg) * 4);
  1099. /* fr16-fr31 */
  1100. for (i = 16; i < 32; i++) {
  1101. if (unw_get_fr(&info, i, &fpval) < 0)
  1102. return -EIO;
  1103. retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
  1104. }
  1105. /* fph */
  1106. ia64_flush_fph(child);
  1107. retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph,
  1108. sizeof(ppr->fr[32]) * 96);
  1109. /* preds */
  1110. retval |= __put_user(pt->pr, &ppr->pr);
  1111. /* nat bits */
  1112. retval |= __put_user(nat_bits, &ppr->nat);
  1113. ret = retval ? -EIO : 0;
  1114. return ret;
  1115. }
  1116. static long
  1117. ptrace_setregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
  1118. {
  1119. unsigned long psr, rsc, ec, lc, rnat, bsp, cfm, nat_bits, val = 0;
  1120. struct unw_frame_info info;
  1121. struct switch_stack *sw;
  1122. struct ia64_fpreg fpval;
  1123. struct pt_regs *pt;
  1124. long ret, retval = 0;
  1125. int i;
  1126. memset(&fpval, 0, sizeof(fpval));
  1127. if (!access_ok(VERIFY_READ, ppr, sizeof(struct pt_all_user_regs)))
  1128. return -EIO;
  1129. pt = task_pt_regs(child);
  1130. sw = (struct switch_stack *) (child->thread.ksp + 16);
  1131. unw_init_from_blocked_task(&info, child);
  1132. if (unw_unwind_to_user(&info) < 0) {
  1133. return -EIO;
  1134. }
  1135. if (((unsigned long) ppr & 0x7) != 0) {
  1136. dprintk("ptrace:unaligned register address %p\n", ppr);
  1137. return -EIO;
  1138. }
  1139. /* control regs */
  1140. retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
  1141. retval |= __get_user(psr, &ppr->cr_ipsr);
  1142. /* app regs */
  1143. retval |= __get_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
  1144. retval |= __get_user(rsc, &ppr->ar[PT_AUR_RSC]);
  1145. retval |= __get_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
  1146. retval |= __get_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
  1147. retval |= __get_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
  1148. retval |= __get_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
  1149. retval |= __get_user(ec, &ppr->ar[PT_AUR_EC]);
  1150. retval |= __get_user(lc, &ppr->ar[PT_AUR_LC]);
  1151. retval |= __get_user(rnat, &ppr->ar[PT_AUR_RNAT]);
  1152. retval |= __get_user(bsp, &ppr->ar[PT_AUR_BSP]);
  1153. retval |= __get_user(cfm, &ppr->cfm);
  1154. /* gr1-gr3 */
  1155. retval |= __copy_from_user(&pt->r1, &ppr->gr[1], sizeof(long));
  1156. retval |= __copy_from_user(&pt->r2, &ppr->gr[2], sizeof(long) * 2);
  1157. /* gr4-gr7 */
  1158. for (i = 4; i < 8; i++) {
  1159. retval |= __get_user(val, &ppr->gr[i]);
  1160. /* NaT bit will be set via PT_NAT_BITS: */
  1161. if (unw_set_gr(&info, i, val, 0) < 0)
  1162. return -EIO;
  1163. }
  1164. /* gr8-gr11 */
  1165. retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
  1166. /* gr12-gr15 */
  1167. retval |= __copy_from_user(&pt->r12, &ppr->gr[12], sizeof(long) * 2);
  1168. retval |= __copy_from_user(&pt->r14, &ppr->gr[14], sizeof(long));
  1169. retval |= __copy_from_user(&pt->r15, &ppr->gr[15], sizeof(long));
  1170. /* gr16-gr31 */
  1171. retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
  1172. /* b0 */
  1173. retval |= __get_user(pt->b0, &ppr->br[0]);
  1174. /* b1-b5 */
  1175. for (i = 1; i < 6; i++) {
  1176. retval |= __get_user(val, &ppr->br[i]);
  1177. unw_set_br(&info, i, val);
  1178. }
  1179. /* b6-b7 */
  1180. retval |= __get_user(pt->b6, &ppr->br[6]);
  1181. retval |= __get_user(pt->b7, &ppr->br[7]);
  1182. /* fr2-fr5 */
  1183. for (i = 2; i < 6; i++) {
  1184. retval |= __copy_from_user(&fpval, &ppr->fr[i], sizeof(fpval));
  1185. if (unw_set_fr(&info, i, fpval) < 0)
  1186. return -EIO;
  1187. }
  1188. /* fr6-fr11 */
  1189. retval |= __copy_from_user(&pt->f6, &ppr->fr[6],
  1190. sizeof(ppr->fr[6]) * 6);
  1191. /* fp scratch regs(12-15) */
  1192. retval |= __copy_from_user(&sw->f12, &ppr->fr[12],
  1193. sizeof(ppr->fr[12]) * 4);
  1194. /* fr16-fr31 */
  1195. for (i = 16; i < 32; i++) {
  1196. retval |= __copy_from_user(&fpval, &ppr->fr[i],
  1197. sizeof(fpval));
  1198. if (unw_set_fr(&info, i, fpval) < 0)
  1199. return -EIO;
  1200. }
  1201. /* fph */
  1202. ia64_sync_fph(child);
  1203. retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32],
  1204. sizeof(ppr->fr[32]) * 96);
  1205. /* preds */
  1206. retval |= __get_user(pt->pr, &ppr->pr);
  1207. /* nat bits */
  1208. retval |= __get_user(nat_bits, &ppr->nat);
  1209. retval |= access_uarea(child, PT_CR_IPSR, &psr, 1);
  1210. retval |= access_uarea(child, PT_AR_RSC, &rsc, 1);
  1211. retval |= access_uarea(child, PT_AR_EC, &ec, 1);
  1212. retval |= access_uarea(child, PT_AR_LC, &lc, 1);
  1213. retval |= access_uarea(child, PT_AR_RNAT, &rnat, 1);
  1214. retval |= access_uarea(child, PT_AR_BSP, &bsp, 1);
  1215. retval |= access_uarea(child, PT_CFM, &cfm, 1);
  1216. retval |= access_uarea(child, PT_NAT_BITS, &nat_bits, 1);
  1217. ret = retval ? -EIO : 0;
  1218. return ret;
  1219. }
  1220. /*
  1221. * Called by kernel/ptrace.c when detaching..
  1222. *
  1223. * Make sure the single step bit is not set.
  1224. */
  1225. void
  1226. ptrace_disable (struct task_struct *child)
  1227. {
  1228. struct ia64_psr *child_psr = ia64_psr(task_pt_regs(child));
  1229. /* make sure the single step/taken-branch trap bits are not set: */
  1230. child_psr->ss = 0;
  1231. child_psr->tb = 0;
  1232. }
  1233. asmlinkage long
  1234. sys_ptrace (long request, pid_t pid, unsigned long addr, unsigned long data)
  1235. {
  1236. struct pt_regs *pt;
  1237. unsigned long urbs_end, peek_or_poke;
  1238. struct task_struct *child;
  1239. struct switch_stack *sw;
  1240. long ret;
  1241. lock_kernel();
  1242. ret = -EPERM;
  1243. if (request == PTRACE_TRACEME) {
  1244. ret = ptrace_traceme();
  1245. goto out;
  1246. }
  1247. peek_or_poke = (request == PTRACE_PEEKTEXT
  1248. || request == PTRACE_PEEKDATA
  1249. || request == PTRACE_POKETEXT
  1250. || request == PTRACE_POKEDATA);
  1251. ret = -ESRCH;
  1252. read_lock(&tasklist_lock);
  1253. {
  1254. child = find_task_by_pid(pid);
  1255. if (child) {
  1256. if (peek_or_poke)
  1257. child = find_thread_for_addr(child, addr);
  1258. get_task_struct(child);
  1259. }
  1260. }
  1261. read_unlock(&tasklist_lock);
  1262. if (!child)
  1263. goto out;
  1264. ret = -EPERM;
  1265. if (pid == 1) /* no messing around with init! */
  1266. goto out_tsk;
  1267. if (request == PTRACE_ATTACH) {
  1268. ret = ptrace_attach(child);
  1269. goto out_tsk;
  1270. }
  1271. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  1272. if (ret < 0)
  1273. goto out_tsk;
  1274. pt = task_pt_regs(child);
  1275. sw = (struct switch_stack *) (child->thread.ksp + 16);
  1276. switch (request) {
  1277. case PTRACE_PEEKTEXT:
  1278. case PTRACE_PEEKDATA:
  1279. /* read word at location addr */
  1280. urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
  1281. ret = ia64_peek(child, sw, urbs_end, addr, &data);
  1282. if (ret == 0) {
  1283. ret = data;
  1284. /* ensure "ret" is not mistaken as an error code: */
  1285. force_successful_syscall_return();
  1286. }
  1287. goto out_tsk;
  1288. case PTRACE_POKETEXT:
  1289. case PTRACE_POKEDATA:
  1290. /* write the word at location addr */
  1291. urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
  1292. ret = ia64_poke(child, sw, urbs_end, addr, data);
  1293. goto out_tsk;
  1294. case PTRACE_PEEKUSR:
  1295. /* read the word at addr in the USER area */
  1296. if (access_uarea(child, addr, &data, 0) < 0) {
  1297. ret = -EIO;
  1298. goto out_tsk;
  1299. }
  1300. ret = data;
  1301. /* ensure "ret" is not mistaken as an error code */
  1302. force_successful_syscall_return();
  1303. goto out_tsk;
  1304. case PTRACE_POKEUSR:
  1305. /* write the word at addr in the USER area */
  1306. if (access_uarea(child, addr, &data, 1) < 0) {
  1307. ret = -EIO;
  1308. goto out_tsk;
  1309. }
  1310. ret = 0;
  1311. goto out_tsk;
  1312. case PTRACE_OLD_GETSIGINFO:
  1313. /* for backwards-compatibility */
  1314. ret = ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
  1315. goto out_tsk;
  1316. case PTRACE_OLD_SETSIGINFO:
  1317. /* for backwards-compatibility */
  1318. ret = ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
  1319. goto out_tsk;
  1320. case PTRACE_SYSCALL:
  1321. /* continue and stop at next (return from) syscall */
  1322. case PTRACE_CONT:
  1323. /* restart after signal. */
  1324. ret = -EIO;
  1325. if (!valid_signal(data))
  1326. goto out_tsk;
  1327. if (request == PTRACE_SYSCALL)
  1328. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  1329. else
  1330. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  1331. child->exit_code = data;
  1332. /*
  1333. * Make sure the single step/taken-branch trap bits
  1334. * are not set:
  1335. */
  1336. ia64_psr(pt)->ss = 0;
  1337. ia64_psr(pt)->tb = 0;
  1338. wake_up_process(child);
  1339. ret = 0;
  1340. goto out_tsk;
  1341. case PTRACE_KILL:
  1342. /*
  1343. * Make the child exit. Best I can do is send it a
  1344. * sigkill. Perhaps it should be put in the status
  1345. * that it wants to exit.
  1346. */
  1347. if (child->exit_state == EXIT_ZOMBIE)
  1348. /* already dead */
  1349. goto out_tsk;
  1350. child->exit_code = SIGKILL;
  1351. ptrace_disable(child);
  1352. wake_up_process(child);
  1353. ret = 0;
  1354. goto out_tsk;
  1355. case PTRACE_SINGLESTEP:
  1356. /* let child execute for one instruction */
  1357. case PTRACE_SINGLEBLOCK:
  1358. ret = -EIO;
  1359. if (!valid_signal(data))
  1360. goto out_tsk;
  1361. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  1362. if (request == PTRACE_SINGLESTEP) {
  1363. ia64_psr(pt)->ss = 1;
  1364. } else {
  1365. ia64_psr(pt)->tb = 1;
  1366. }
  1367. child->exit_code = data;
  1368. /* give it a chance to run. */
  1369. wake_up_process(child);
  1370. ret = 0;
  1371. goto out_tsk;
  1372. case PTRACE_DETACH:
  1373. /* detach a process that was attached. */
  1374. ret = ptrace_detach(child, data);
  1375. goto out_tsk;
  1376. case PTRACE_GETREGS:
  1377. ret = ptrace_getregs(child,
  1378. (struct pt_all_user_regs __user *) data);
  1379. goto out_tsk;
  1380. case PTRACE_SETREGS:
  1381. ret = ptrace_setregs(child,
  1382. (struct pt_all_user_regs __user *) data);
  1383. goto out_tsk;
  1384. default:
  1385. ret = ptrace_request(child, request, addr, data);
  1386. goto out_tsk;
  1387. }
  1388. out_tsk:
  1389. put_task_struct(child);
  1390. out:
  1391. unlock_kernel();
  1392. return ret;
  1393. }
  1394. void
  1395. syscall_trace (void)
  1396. {
  1397. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  1398. return;
  1399. if (!(current->ptrace & PT_PTRACED))
  1400. return;
  1401. /*
  1402. * The 0x80 provides a way for the tracing parent to
  1403. * distinguish between a syscall stop and SIGTRAP delivery.
  1404. */
  1405. ptrace_notify(SIGTRAP
  1406. | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  1407. /*
  1408. * This isn't the same as continuing with a signal, but it
  1409. * will do for normal use. strace only continues with a
  1410. * signal if the stopping signal is not SIGTRAP. -brl
  1411. */
  1412. if (current->exit_code) {
  1413. send_sig(current->exit_code, current, 1);
  1414. current->exit_code = 0;
  1415. }
  1416. }
  1417. /* "asmlinkage" so the input arguments are preserved... */
  1418. asmlinkage void
  1419. syscall_trace_enter (long arg0, long arg1, long arg2, long arg3,
  1420. long arg4, long arg5, long arg6, long arg7,
  1421. struct pt_regs regs)
  1422. {
  1423. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1424. && (current->ptrace & PT_PTRACED))
  1425. syscall_trace();
  1426. if (unlikely(current->audit_context)) {
  1427. long syscall;
  1428. int arch;
  1429. if (IS_IA32_PROCESS(&regs)) {
  1430. syscall = regs.r1;
  1431. arch = AUDIT_ARCH_I386;
  1432. } else {
  1433. syscall = regs.r15;
  1434. arch = AUDIT_ARCH_IA64;
  1435. }
  1436. audit_syscall_entry(current, arch, syscall, arg0, arg1, arg2, arg3);
  1437. }
  1438. }
  1439. /* "asmlinkage" so the input arguments are preserved... */
  1440. asmlinkage void
  1441. syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
  1442. long arg4, long arg5, long arg6, long arg7,
  1443. struct pt_regs regs)
  1444. {
  1445. if (unlikely(current->audit_context))
  1446. audit_syscall_exit(current, AUDITSC_RESULT(regs.r10), regs.r8);
  1447. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1448. && (current->ptrace & PT_PTRACED))
  1449. syscall_trace();
  1450. }