ptrace.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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 = ia64_task_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 = ia64_task_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 = ia64_task_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 = ia64_task_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 = ia64_task_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 *g, *p;
  539. struct mm_struct *mm;
  540. int mm_users;
  541. if (!(mm = get_task_mm(child)))
  542. return child;
  543. /* -1 because of our get_task_mm(): */
  544. mm_users = atomic_read(&mm->mm_users) - 1;
  545. if (mm_users <= 1)
  546. goto out; /* not multi-threaded */
  547. /*
  548. * First, traverse the child's thread-list. Good for scalability with
  549. * NPTL-threads.
  550. */
  551. p = child;
  552. do {
  553. if (thread_matches(p, addr)) {
  554. child = p;
  555. goto out;
  556. }
  557. if (mm_users-- <= 1)
  558. goto out;
  559. } while ((p = next_thread(p)) != child);
  560. do_each_thread(g, p) {
  561. if (child->mm != mm)
  562. continue;
  563. if (thread_matches(p, addr)) {
  564. child = p;
  565. goto out;
  566. }
  567. } while_each_thread(g, p);
  568. out:
  569. mmput(mm);
  570. return child;
  571. }
  572. /*
  573. * Write f32-f127 back to task->thread.fph if it has been modified.
  574. */
  575. inline void
  576. ia64_flush_fph (struct task_struct *task)
  577. {
  578. struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
  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. }
  585. /*
  586. * Sync the fph state of the task so that it can be manipulated
  587. * through thread.fph. If necessary, f32-f127 are written back to
  588. * thread.fph or, if the fph state hasn't been used before, thread.fph
  589. * is cleared to zeroes. Also, access to f32-f127 is disabled to
  590. * ensure that the task picks up the state from thread.fph when it
  591. * executes again.
  592. */
  593. void
  594. ia64_sync_fph (struct task_struct *task)
  595. {
  596. struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
  597. ia64_flush_fph(task);
  598. if (!(task->thread.flags & IA64_THREAD_FPH_VALID)) {
  599. task->thread.flags |= IA64_THREAD_FPH_VALID;
  600. memset(&task->thread.fph, 0, sizeof(task->thread.fph));
  601. }
  602. ia64_drop_fpu(task);
  603. psr->dfh = 1;
  604. }
  605. static int
  606. access_fr (struct unw_frame_info *info, int regnum, int hi,
  607. unsigned long *data, int write_access)
  608. {
  609. struct ia64_fpreg fpval;
  610. int ret;
  611. ret = unw_get_fr(info, regnum, &fpval);
  612. if (ret < 0)
  613. return ret;
  614. if (write_access) {
  615. fpval.u.bits[hi] = *data;
  616. ret = unw_set_fr(info, regnum, fpval);
  617. } else
  618. *data = fpval.u.bits[hi];
  619. return ret;
  620. }
  621. /*
  622. * Change the machine-state of CHILD such that it will return via the normal
  623. * kernel exit-path, rather than the syscall-exit path.
  624. */
  625. static void
  626. convert_to_non_syscall (struct task_struct *child, struct pt_regs *pt,
  627. unsigned long cfm)
  628. {
  629. struct unw_frame_info info, prev_info;
  630. unsigned long ip, pr;
  631. unw_init_from_blocked_task(&info, child);
  632. while (1) {
  633. prev_info = info;
  634. if (unw_unwind(&info) < 0)
  635. return;
  636. if (unw_get_rp(&info, &ip) < 0)
  637. return;
  638. if (ip < FIXADDR_USER_END)
  639. break;
  640. }
  641. unw_get_pr(&prev_info, &pr);
  642. pr &= ~(1UL << PRED_SYSCALL);
  643. pr |= (1UL << PRED_NON_SYSCALL);
  644. unw_set_pr(&prev_info, pr);
  645. pt->cr_ifs = (1UL << 63) | cfm;
  646. }
  647. static int
  648. access_nat_bits (struct task_struct *child, struct pt_regs *pt,
  649. struct unw_frame_info *info,
  650. unsigned long *data, int write_access)
  651. {
  652. unsigned long regnum, nat_bits, scratch_unat, dummy = 0;
  653. char nat = 0;
  654. if (write_access) {
  655. nat_bits = *data;
  656. scratch_unat = ia64_put_scratch_nat_bits(pt, nat_bits);
  657. if (unw_set_ar(info, UNW_AR_UNAT, scratch_unat) < 0) {
  658. dprintk("ptrace: failed to set ar.unat\n");
  659. return -1;
  660. }
  661. for (regnum = 4; regnum <= 7; ++regnum) {
  662. unw_get_gr(info, regnum, &dummy, &nat);
  663. unw_set_gr(info, regnum, dummy,
  664. (nat_bits >> regnum) & 1);
  665. }
  666. } else {
  667. if (unw_get_ar(info, UNW_AR_UNAT, &scratch_unat) < 0) {
  668. dprintk("ptrace: failed to read ar.unat\n");
  669. return -1;
  670. }
  671. nat_bits = ia64_get_scratch_nat_bits(pt, scratch_unat);
  672. for (regnum = 4; regnum <= 7; ++regnum) {
  673. unw_get_gr(info, regnum, &dummy, &nat);
  674. nat_bits |= (nat != 0) << regnum;
  675. }
  676. *data = nat_bits;
  677. }
  678. return 0;
  679. }
  680. static int
  681. access_uarea (struct task_struct *child, unsigned long addr,
  682. unsigned long *data, int write_access)
  683. {
  684. unsigned long *ptr, regnum, urbs_end, rnat_addr, cfm;
  685. struct switch_stack *sw;
  686. struct pt_regs *pt;
  687. # define pt_reg_addr(pt, reg) ((void *) \
  688. ((unsigned long) (pt) \
  689. + offsetof(struct pt_regs, reg)))
  690. pt = ia64_task_regs(child);
  691. sw = (struct switch_stack *) (child->thread.ksp + 16);
  692. if ((addr & 0x7) != 0) {
  693. dprintk("ptrace: unaligned register address 0x%lx\n", addr);
  694. return -1;
  695. }
  696. if (addr < PT_F127 + 16) {
  697. /* accessing fph */
  698. if (write_access)
  699. ia64_sync_fph(child);
  700. else
  701. ia64_flush_fph(child);
  702. ptr = (unsigned long *)
  703. ((unsigned long) &child->thread.fph + addr);
  704. } else if ((addr >= PT_F10) && (addr < PT_F11 + 16)) {
  705. /* scratch registers untouched by kernel (saved in pt_regs) */
  706. ptr = pt_reg_addr(pt, f10) + (addr - PT_F10);
  707. } else if (addr >= PT_F12 && addr < PT_F15 + 16) {
  708. /*
  709. * Scratch registers untouched by kernel (saved in
  710. * switch_stack).
  711. */
  712. ptr = (unsigned long *) ((long) sw
  713. + (addr - PT_NAT_BITS - 32));
  714. } else if (addr < PT_AR_LC + 8) {
  715. /* preserved state: */
  716. struct unw_frame_info info;
  717. char nat = 0;
  718. int ret;
  719. unw_init_from_blocked_task(&info, child);
  720. if (unw_unwind_to_user(&info) < 0)
  721. return -1;
  722. switch (addr) {
  723. case PT_NAT_BITS:
  724. return access_nat_bits(child, pt, &info,
  725. data, write_access);
  726. case PT_R4: case PT_R5: case PT_R6: case PT_R7:
  727. if (write_access) {
  728. /* read NaT bit first: */
  729. unsigned long dummy;
  730. ret = unw_get_gr(&info, (addr - PT_R4)/8 + 4,
  731. &dummy, &nat);
  732. if (ret < 0)
  733. return ret;
  734. }
  735. return unw_access_gr(&info, (addr - PT_R4)/8 + 4, data,
  736. &nat, write_access);
  737. case PT_B1: case PT_B2: case PT_B3:
  738. case PT_B4: case PT_B5:
  739. return unw_access_br(&info, (addr - PT_B1)/8 + 1, data,
  740. write_access);
  741. case PT_AR_EC:
  742. return unw_access_ar(&info, UNW_AR_EC, data,
  743. write_access);
  744. case PT_AR_LC:
  745. return unw_access_ar(&info, UNW_AR_LC, data,
  746. write_access);
  747. default:
  748. if (addr >= PT_F2 && addr < PT_F5 + 16)
  749. return access_fr(&info, (addr - PT_F2)/16 + 2,
  750. (addr & 8) != 0, data,
  751. write_access);
  752. else if (addr >= PT_F16 && addr < PT_F31 + 16)
  753. return access_fr(&info,
  754. (addr - PT_F16)/16 + 16,
  755. (addr & 8) != 0,
  756. data, write_access);
  757. else {
  758. dprintk("ptrace: rejecting access to register "
  759. "address 0x%lx\n", addr);
  760. return -1;
  761. }
  762. }
  763. } else if (addr < PT_F9+16) {
  764. /* scratch state */
  765. switch (addr) {
  766. case PT_AR_BSP:
  767. /*
  768. * By convention, we use PT_AR_BSP to refer to
  769. * the end of the user-level backing store.
  770. * Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof)
  771. * to get the real value of ar.bsp at the time
  772. * the kernel was entered.
  773. *
  774. * Furthermore, when changing the contents of
  775. * PT_AR_BSP (or PT_CFM) we MUST copy any
  776. * users-level stacked registers that are
  777. * stored on the kernel stack back to
  778. * user-space because otherwise, we might end
  779. * up clobbering kernel stacked registers.
  780. * Also, if this happens while the task is
  781. * blocked in a system call, which convert the
  782. * state such that the non-system-call exit
  783. * path is used. This ensures that the proper
  784. * state will be picked up when resuming
  785. * execution. However, it *also* means that
  786. * once we write PT_AR_BSP/PT_CFM, it won't be
  787. * possible to modify the syscall arguments of
  788. * the pending system call any longer. This
  789. * shouldn't be an issue because modifying
  790. * PT_AR_BSP/PT_CFM generally implies that
  791. * we're either abandoning the pending system
  792. * call or that we defer it's re-execution
  793. * (e.g., due to GDB doing an inferior
  794. * function call).
  795. */
  796. urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
  797. if (write_access) {
  798. if (*data != urbs_end) {
  799. if (ia64_sync_user_rbs(child, sw,
  800. pt->ar_bspstore,
  801. urbs_end) < 0)
  802. return -1;
  803. if (in_syscall(pt))
  804. convert_to_non_syscall(child,
  805. pt,
  806. cfm);
  807. /*
  808. * Simulate user-level write
  809. * of ar.bsp:
  810. */
  811. pt->loadrs = 0;
  812. pt->ar_bspstore = *data;
  813. }
  814. } else
  815. *data = urbs_end;
  816. return 0;
  817. case PT_CFM:
  818. urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
  819. if (write_access) {
  820. if (((cfm ^ *data) & PFM_MASK) != 0) {
  821. if (ia64_sync_user_rbs(child, sw,
  822. pt->ar_bspstore,
  823. urbs_end) < 0)
  824. return -1;
  825. if (in_syscall(pt))
  826. convert_to_non_syscall(child,
  827. pt,
  828. cfm);
  829. pt->cr_ifs = ((pt->cr_ifs & ~PFM_MASK)
  830. | (*data & PFM_MASK));
  831. }
  832. } else
  833. *data = cfm;
  834. return 0;
  835. case PT_CR_IPSR:
  836. if (write_access)
  837. pt->cr_ipsr = ((*data & IPSR_MASK)
  838. | (pt->cr_ipsr & ~IPSR_MASK));
  839. else
  840. *data = (pt->cr_ipsr & IPSR_MASK);
  841. return 0;
  842. case PT_AR_RNAT:
  843. urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
  844. rnat_addr = (long) ia64_rse_rnat_addr((long *)
  845. urbs_end);
  846. if (write_access)
  847. return ia64_poke(child, sw, urbs_end,
  848. rnat_addr, *data);
  849. else
  850. return ia64_peek(child, sw, urbs_end,
  851. rnat_addr, data);
  852. case PT_R1:
  853. ptr = pt_reg_addr(pt, r1);
  854. break;
  855. case PT_R2: case PT_R3:
  856. ptr = pt_reg_addr(pt, r2) + (addr - PT_R2);
  857. break;
  858. case PT_R8: case PT_R9: case PT_R10: case PT_R11:
  859. ptr = pt_reg_addr(pt, r8) + (addr - PT_R8);
  860. break;
  861. case PT_R12: case PT_R13:
  862. ptr = pt_reg_addr(pt, r12) + (addr - PT_R12);
  863. break;
  864. case PT_R14:
  865. ptr = pt_reg_addr(pt, r14);
  866. break;
  867. case PT_R15:
  868. ptr = pt_reg_addr(pt, r15);
  869. break;
  870. case PT_R16: case PT_R17: case PT_R18: case PT_R19:
  871. case PT_R20: case PT_R21: case PT_R22: case PT_R23:
  872. case PT_R24: case PT_R25: case PT_R26: case PT_R27:
  873. case PT_R28: case PT_R29: case PT_R30: case PT_R31:
  874. ptr = pt_reg_addr(pt, r16) + (addr - PT_R16);
  875. break;
  876. case PT_B0:
  877. ptr = pt_reg_addr(pt, b0);
  878. break;
  879. case PT_B6:
  880. ptr = pt_reg_addr(pt, b6);
  881. break;
  882. case PT_B7:
  883. ptr = pt_reg_addr(pt, b7);
  884. break;
  885. case PT_F6: case PT_F6+8: case PT_F7: case PT_F7+8:
  886. case PT_F8: case PT_F8+8: case PT_F9: case PT_F9+8:
  887. ptr = pt_reg_addr(pt, f6) + (addr - PT_F6);
  888. break;
  889. case PT_AR_BSPSTORE:
  890. ptr = pt_reg_addr(pt, ar_bspstore);
  891. break;
  892. case PT_AR_RSC:
  893. ptr = pt_reg_addr(pt, ar_rsc);
  894. break;
  895. case PT_AR_UNAT:
  896. ptr = pt_reg_addr(pt, ar_unat);
  897. break;
  898. case PT_AR_PFS:
  899. ptr = pt_reg_addr(pt, ar_pfs);
  900. break;
  901. case PT_AR_CCV:
  902. ptr = pt_reg_addr(pt, ar_ccv);
  903. break;
  904. case PT_AR_FPSR:
  905. ptr = pt_reg_addr(pt, ar_fpsr);
  906. break;
  907. case PT_CR_IIP:
  908. ptr = pt_reg_addr(pt, cr_iip);
  909. break;
  910. case PT_PR:
  911. ptr = pt_reg_addr(pt, pr);
  912. break;
  913. /* scratch register */
  914. default:
  915. /* disallow accessing anything else... */
  916. dprintk("ptrace: rejecting access to register "
  917. "address 0x%lx\n", addr);
  918. return -1;
  919. }
  920. } else if (addr <= PT_AR_SSD) {
  921. ptr = pt_reg_addr(pt, ar_csd) + (addr - PT_AR_CSD);
  922. } else {
  923. /* access debug registers */
  924. if (addr >= PT_IBR) {
  925. regnum = (addr - PT_IBR) >> 3;
  926. ptr = &child->thread.ibr[0];
  927. } else {
  928. regnum = (addr - PT_DBR) >> 3;
  929. ptr = &child->thread.dbr[0];
  930. }
  931. if (regnum >= 8) {
  932. dprintk("ptrace: rejecting access to register "
  933. "address 0x%lx\n", addr);
  934. return -1;
  935. }
  936. #ifdef CONFIG_PERFMON
  937. /*
  938. * Check if debug registers are used by perfmon. This
  939. * test must be done once we know that we can do the
  940. * operation, i.e. the arguments are all valid, but
  941. * before we start modifying the state.
  942. *
  943. * Perfmon needs to keep a count of how many processes
  944. * are trying to modify the debug registers for system
  945. * wide monitoring sessions.
  946. *
  947. * We also include read access here, because they may
  948. * cause the PMU-installed debug register state
  949. * (dbr[], ibr[]) to be reset. The two arrays are also
  950. * used by perfmon, but we do not use
  951. * IA64_THREAD_DBG_VALID. The registers are restored
  952. * by the PMU context switch code.
  953. */
  954. if (pfm_use_debug_registers(child)) return -1;
  955. #endif
  956. if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
  957. child->thread.flags |= IA64_THREAD_DBG_VALID;
  958. memset(child->thread.dbr, 0,
  959. sizeof(child->thread.dbr));
  960. memset(child->thread.ibr, 0,
  961. sizeof(child->thread.ibr));
  962. }
  963. ptr += regnum;
  964. if ((regnum & 1) && write_access) {
  965. /* don't let the user set kernel-level breakpoints: */
  966. *ptr = *data & ~(7UL << 56);
  967. return 0;
  968. }
  969. }
  970. if (write_access)
  971. *ptr = *data;
  972. else
  973. *data = *ptr;
  974. return 0;
  975. }
  976. static long
  977. ptrace_getregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
  978. {
  979. unsigned long psr, ec, lc, rnat, bsp, cfm, nat_bits, val;
  980. struct unw_frame_info info;
  981. struct ia64_fpreg fpval;
  982. struct switch_stack *sw;
  983. struct pt_regs *pt;
  984. long ret, retval = 0;
  985. char nat = 0;
  986. int i;
  987. if (!access_ok(VERIFY_WRITE, ppr, sizeof(struct pt_all_user_regs)))
  988. return -EIO;
  989. pt = ia64_task_regs(child);
  990. sw = (struct switch_stack *) (child->thread.ksp + 16);
  991. unw_init_from_blocked_task(&info, child);
  992. if (unw_unwind_to_user(&info) < 0) {
  993. return -EIO;
  994. }
  995. if (((unsigned long) ppr & 0x7) != 0) {
  996. dprintk("ptrace:unaligned register address %p\n", ppr);
  997. return -EIO;
  998. }
  999. if (access_uarea(child, PT_CR_IPSR, &psr, 0) < 0
  1000. || access_uarea(child, PT_AR_EC, &ec, 0) < 0
  1001. || access_uarea(child, PT_AR_LC, &lc, 0) < 0
  1002. || access_uarea(child, PT_AR_RNAT, &rnat, 0) < 0
  1003. || access_uarea(child, PT_AR_BSP, &bsp, 0) < 0
  1004. || access_uarea(child, PT_CFM, &cfm, 0)
  1005. || access_uarea(child, PT_NAT_BITS, &nat_bits, 0))
  1006. return -EIO;
  1007. /* control regs */
  1008. retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
  1009. retval |= __put_user(psr, &ppr->cr_ipsr);
  1010. /* app regs */
  1011. retval |= __put_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
  1012. retval |= __put_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
  1013. retval |= __put_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
  1014. retval |= __put_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
  1015. retval |= __put_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
  1016. retval |= __put_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
  1017. retval |= __put_user(ec, &ppr->ar[PT_AUR_EC]);
  1018. retval |= __put_user(lc, &ppr->ar[PT_AUR_LC]);
  1019. retval |= __put_user(rnat, &ppr->ar[PT_AUR_RNAT]);
  1020. retval |= __put_user(bsp, &ppr->ar[PT_AUR_BSP]);
  1021. retval |= __put_user(cfm, &ppr->cfm);
  1022. /* gr1-gr3 */
  1023. retval |= __copy_to_user(&ppr->gr[1], &pt->r1, sizeof(long));
  1024. retval |= __copy_to_user(&ppr->gr[2], &pt->r2, sizeof(long) *2);
  1025. /* gr4-gr7 */
  1026. for (i = 4; i < 8; i++) {
  1027. if (unw_access_gr(&info, i, &val, &nat, 0) < 0)
  1028. return -EIO;
  1029. retval |= __put_user(val, &ppr->gr[i]);
  1030. }
  1031. /* gr8-gr11 */
  1032. retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
  1033. /* gr12-gr15 */
  1034. retval |= __copy_to_user(&ppr->gr[12], &pt->r12, sizeof(long) * 2);
  1035. retval |= __copy_to_user(&ppr->gr[14], &pt->r14, sizeof(long));
  1036. retval |= __copy_to_user(&ppr->gr[15], &pt->r15, sizeof(long));
  1037. /* gr16-gr31 */
  1038. retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
  1039. /* b0 */
  1040. retval |= __put_user(pt->b0, &ppr->br[0]);
  1041. /* b1-b5 */
  1042. for (i = 1; i < 6; i++) {
  1043. if (unw_access_br(&info, i, &val, 0) < 0)
  1044. return -EIO;
  1045. __put_user(val, &ppr->br[i]);
  1046. }
  1047. /* b6-b7 */
  1048. retval |= __put_user(pt->b6, &ppr->br[6]);
  1049. retval |= __put_user(pt->b7, &ppr->br[7]);
  1050. /* fr2-fr5 */
  1051. for (i = 2; i < 6; i++) {
  1052. if (unw_get_fr(&info, i, &fpval) < 0)
  1053. return -EIO;
  1054. retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
  1055. }
  1056. /* fr6-fr11 */
  1057. retval |= __copy_to_user(&ppr->fr[6], &pt->f6,
  1058. sizeof(struct ia64_fpreg) * 6);
  1059. /* fp scratch regs(12-15) */
  1060. retval |= __copy_to_user(&ppr->fr[12], &sw->f12,
  1061. sizeof(struct ia64_fpreg) * 4);
  1062. /* fr16-fr31 */
  1063. for (i = 16; i < 32; i++) {
  1064. if (unw_get_fr(&info, i, &fpval) < 0)
  1065. return -EIO;
  1066. retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
  1067. }
  1068. /* fph */
  1069. ia64_flush_fph(child);
  1070. retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph,
  1071. sizeof(ppr->fr[32]) * 96);
  1072. /* preds */
  1073. retval |= __put_user(pt->pr, &ppr->pr);
  1074. /* nat bits */
  1075. retval |= __put_user(nat_bits, &ppr->nat);
  1076. ret = retval ? -EIO : 0;
  1077. return ret;
  1078. }
  1079. static long
  1080. ptrace_setregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
  1081. {
  1082. unsigned long psr, ec, lc, rnat, bsp, cfm, nat_bits, val = 0;
  1083. struct unw_frame_info info;
  1084. struct switch_stack *sw;
  1085. struct ia64_fpreg fpval;
  1086. struct pt_regs *pt;
  1087. long ret, retval = 0;
  1088. int i;
  1089. memset(&fpval, 0, sizeof(fpval));
  1090. if (!access_ok(VERIFY_READ, ppr, sizeof(struct pt_all_user_regs)))
  1091. return -EIO;
  1092. pt = ia64_task_regs(child);
  1093. sw = (struct switch_stack *) (child->thread.ksp + 16);
  1094. unw_init_from_blocked_task(&info, child);
  1095. if (unw_unwind_to_user(&info) < 0) {
  1096. return -EIO;
  1097. }
  1098. if (((unsigned long) ppr & 0x7) != 0) {
  1099. dprintk("ptrace:unaligned register address %p\n", ppr);
  1100. return -EIO;
  1101. }
  1102. /* control regs */
  1103. retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
  1104. retval |= __get_user(psr, &ppr->cr_ipsr);
  1105. /* app regs */
  1106. retval |= __get_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
  1107. retval |= __get_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
  1108. retval |= __get_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
  1109. retval |= __get_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
  1110. retval |= __get_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
  1111. retval |= __get_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
  1112. retval |= __get_user(ec, &ppr->ar[PT_AUR_EC]);
  1113. retval |= __get_user(lc, &ppr->ar[PT_AUR_LC]);
  1114. retval |= __get_user(rnat, &ppr->ar[PT_AUR_RNAT]);
  1115. retval |= __get_user(bsp, &ppr->ar[PT_AUR_BSP]);
  1116. retval |= __get_user(cfm, &ppr->cfm);
  1117. /* gr1-gr3 */
  1118. retval |= __copy_from_user(&pt->r1, &ppr->gr[1], sizeof(long));
  1119. retval |= __copy_from_user(&pt->r2, &ppr->gr[2], sizeof(long) * 2);
  1120. /* gr4-gr7 */
  1121. for (i = 4; i < 8; i++) {
  1122. retval |= __get_user(val, &ppr->gr[i]);
  1123. /* NaT bit will be set via PT_NAT_BITS: */
  1124. if (unw_set_gr(&info, i, val, 0) < 0)
  1125. return -EIO;
  1126. }
  1127. /* gr8-gr11 */
  1128. retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
  1129. /* gr12-gr15 */
  1130. retval |= __copy_from_user(&pt->r12, &ppr->gr[12], sizeof(long) * 2);
  1131. retval |= __copy_from_user(&pt->r14, &ppr->gr[14], sizeof(long));
  1132. retval |= __copy_from_user(&pt->r15, &ppr->gr[15], sizeof(long));
  1133. /* gr16-gr31 */
  1134. retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
  1135. /* b0 */
  1136. retval |= __get_user(pt->b0, &ppr->br[0]);
  1137. /* b1-b5 */
  1138. for (i = 1; i < 6; i++) {
  1139. retval |= __get_user(val, &ppr->br[i]);
  1140. unw_set_br(&info, i, val);
  1141. }
  1142. /* b6-b7 */
  1143. retval |= __get_user(pt->b6, &ppr->br[6]);
  1144. retval |= __get_user(pt->b7, &ppr->br[7]);
  1145. /* fr2-fr5 */
  1146. for (i = 2; i < 6; i++) {
  1147. retval |= __copy_from_user(&fpval, &ppr->fr[i], sizeof(fpval));
  1148. if (unw_set_fr(&info, i, fpval) < 0)
  1149. return -EIO;
  1150. }
  1151. /* fr6-fr11 */
  1152. retval |= __copy_from_user(&pt->f6, &ppr->fr[6],
  1153. sizeof(ppr->fr[6]) * 6);
  1154. /* fp scratch regs(12-15) */
  1155. retval |= __copy_from_user(&sw->f12, &ppr->fr[12],
  1156. sizeof(ppr->fr[12]) * 4);
  1157. /* fr16-fr31 */
  1158. for (i = 16; i < 32; i++) {
  1159. retval |= __copy_from_user(&fpval, &ppr->fr[i],
  1160. sizeof(fpval));
  1161. if (unw_set_fr(&info, i, fpval) < 0)
  1162. return -EIO;
  1163. }
  1164. /* fph */
  1165. ia64_sync_fph(child);
  1166. retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32],
  1167. sizeof(ppr->fr[32]) * 96);
  1168. /* preds */
  1169. retval |= __get_user(pt->pr, &ppr->pr);
  1170. /* nat bits */
  1171. retval |= __get_user(nat_bits, &ppr->nat);
  1172. retval |= access_uarea(child, PT_CR_IPSR, &psr, 1);
  1173. retval |= access_uarea(child, PT_AR_EC, &ec, 1);
  1174. retval |= access_uarea(child, PT_AR_LC, &lc, 1);
  1175. retval |= access_uarea(child, PT_AR_RNAT, &rnat, 1);
  1176. retval |= access_uarea(child, PT_AR_BSP, &bsp, 1);
  1177. retval |= access_uarea(child, PT_CFM, &cfm, 1);
  1178. retval |= access_uarea(child, PT_NAT_BITS, &nat_bits, 1);
  1179. ret = retval ? -EIO : 0;
  1180. return ret;
  1181. }
  1182. /*
  1183. * Called by kernel/ptrace.c when detaching..
  1184. *
  1185. * Make sure the single step bit is not set.
  1186. */
  1187. void
  1188. ptrace_disable (struct task_struct *child)
  1189. {
  1190. struct ia64_psr *child_psr = ia64_psr(ia64_task_regs(child));
  1191. /* make sure the single step/taken-branch trap bits are not set: */
  1192. child_psr->ss = 0;
  1193. child_psr->tb = 0;
  1194. }
  1195. asmlinkage long
  1196. sys_ptrace (long request, pid_t pid, unsigned long addr, unsigned long data)
  1197. {
  1198. struct pt_regs *pt;
  1199. unsigned long urbs_end, peek_or_poke;
  1200. struct task_struct *child;
  1201. struct switch_stack *sw;
  1202. long ret;
  1203. lock_kernel();
  1204. ret = -EPERM;
  1205. if (request == PTRACE_TRACEME) {
  1206. /* are we already being traced? */
  1207. if (current->ptrace & PT_PTRACED)
  1208. goto out;
  1209. ret = security_ptrace(current->parent, current);
  1210. if (ret)
  1211. goto out;
  1212. current->ptrace |= PT_PTRACED;
  1213. ret = 0;
  1214. goto out;
  1215. }
  1216. peek_or_poke = (request == PTRACE_PEEKTEXT
  1217. || request == PTRACE_PEEKDATA
  1218. || request == PTRACE_POKETEXT
  1219. || request == PTRACE_POKEDATA);
  1220. ret = -ESRCH;
  1221. read_lock(&tasklist_lock);
  1222. {
  1223. child = find_task_by_pid(pid);
  1224. if (child) {
  1225. if (peek_or_poke)
  1226. child = find_thread_for_addr(child, addr);
  1227. get_task_struct(child);
  1228. }
  1229. }
  1230. read_unlock(&tasklist_lock);
  1231. if (!child)
  1232. goto out;
  1233. ret = -EPERM;
  1234. if (pid == 1) /* no messing around with init! */
  1235. goto out_tsk;
  1236. if (request == PTRACE_ATTACH) {
  1237. ret = ptrace_attach(child);
  1238. goto out_tsk;
  1239. }
  1240. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  1241. if (ret < 0)
  1242. goto out_tsk;
  1243. pt = ia64_task_regs(child);
  1244. sw = (struct switch_stack *) (child->thread.ksp + 16);
  1245. switch (request) {
  1246. case PTRACE_PEEKTEXT:
  1247. case PTRACE_PEEKDATA:
  1248. /* read word at location addr */
  1249. urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
  1250. ret = ia64_peek(child, sw, urbs_end, addr, &data);
  1251. if (ret == 0) {
  1252. ret = data;
  1253. /* ensure "ret" is not mistaken as an error code: */
  1254. force_successful_syscall_return();
  1255. }
  1256. goto out_tsk;
  1257. case PTRACE_POKETEXT:
  1258. case PTRACE_POKEDATA:
  1259. /* write the word at location addr */
  1260. urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
  1261. ret = ia64_poke(child, sw, urbs_end, addr, data);
  1262. goto out_tsk;
  1263. case PTRACE_PEEKUSR:
  1264. /* read the word at addr in the USER area */
  1265. if (access_uarea(child, addr, &data, 0) < 0) {
  1266. ret = -EIO;
  1267. goto out_tsk;
  1268. }
  1269. ret = data;
  1270. /* ensure "ret" is not mistaken as an error code */
  1271. force_successful_syscall_return();
  1272. goto out_tsk;
  1273. case PTRACE_POKEUSR:
  1274. /* write the word at addr in the USER area */
  1275. if (access_uarea(child, addr, &data, 1) < 0) {
  1276. ret = -EIO;
  1277. goto out_tsk;
  1278. }
  1279. ret = 0;
  1280. goto out_tsk;
  1281. case PTRACE_OLD_GETSIGINFO:
  1282. /* for backwards-compatibility */
  1283. ret = ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
  1284. goto out_tsk;
  1285. case PTRACE_OLD_SETSIGINFO:
  1286. /* for backwards-compatibility */
  1287. ret = ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
  1288. goto out_tsk;
  1289. case PTRACE_SYSCALL:
  1290. /* continue and stop at next (return from) syscall */
  1291. case PTRACE_CONT:
  1292. /* restart after signal. */
  1293. ret = -EIO;
  1294. if (!valid_signal(data))
  1295. goto out_tsk;
  1296. if (request == PTRACE_SYSCALL)
  1297. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  1298. else
  1299. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  1300. child->exit_code = data;
  1301. /*
  1302. * Make sure the single step/taken-branch trap bits
  1303. * are not set:
  1304. */
  1305. ia64_psr(pt)->ss = 0;
  1306. ia64_psr(pt)->tb = 0;
  1307. wake_up_process(child);
  1308. ret = 0;
  1309. goto out_tsk;
  1310. case PTRACE_KILL:
  1311. /*
  1312. * Make the child exit. Best I can do is send it a
  1313. * sigkill. Perhaps it should be put in the status
  1314. * that it wants to exit.
  1315. */
  1316. if (child->exit_state == EXIT_ZOMBIE)
  1317. /* already dead */
  1318. goto out_tsk;
  1319. child->exit_code = SIGKILL;
  1320. ptrace_disable(child);
  1321. wake_up_process(child);
  1322. ret = 0;
  1323. goto out_tsk;
  1324. case PTRACE_SINGLESTEP:
  1325. /* let child execute for one instruction */
  1326. case PTRACE_SINGLEBLOCK:
  1327. ret = -EIO;
  1328. if (!valid_signal(data))
  1329. goto out_tsk;
  1330. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  1331. if (request == PTRACE_SINGLESTEP) {
  1332. ia64_psr(pt)->ss = 1;
  1333. } else {
  1334. ia64_psr(pt)->tb = 1;
  1335. }
  1336. child->exit_code = data;
  1337. /* give it a chance to run. */
  1338. wake_up_process(child);
  1339. ret = 0;
  1340. goto out_tsk;
  1341. case PTRACE_DETACH:
  1342. /* detach a process that was attached. */
  1343. ret = ptrace_detach(child, data);
  1344. goto out_tsk;
  1345. case PTRACE_GETREGS:
  1346. ret = ptrace_getregs(child,
  1347. (struct pt_all_user_regs __user *) data);
  1348. goto out_tsk;
  1349. case PTRACE_SETREGS:
  1350. ret = ptrace_setregs(child,
  1351. (struct pt_all_user_regs __user *) data);
  1352. goto out_tsk;
  1353. default:
  1354. ret = ptrace_request(child, request, addr, data);
  1355. goto out_tsk;
  1356. }
  1357. out_tsk:
  1358. put_task_struct(child);
  1359. out:
  1360. unlock_kernel();
  1361. return ret;
  1362. }
  1363. void
  1364. syscall_trace (void)
  1365. {
  1366. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  1367. return;
  1368. if (!(current->ptrace & PT_PTRACED))
  1369. return;
  1370. /*
  1371. * The 0x80 provides a way for the tracing parent to
  1372. * distinguish between a syscall stop and SIGTRAP delivery.
  1373. */
  1374. ptrace_notify(SIGTRAP
  1375. | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  1376. /*
  1377. * This isn't the same as continuing with a signal, but it
  1378. * will do for normal use. strace only continues with a
  1379. * signal if the stopping signal is not SIGTRAP. -brl
  1380. */
  1381. if (current->exit_code) {
  1382. send_sig(current->exit_code, current, 1);
  1383. current->exit_code = 0;
  1384. }
  1385. }
  1386. /* "asmlinkage" so the input arguments are preserved... */
  1387. asmlinkage void
  1388. syscall_trace_enter (long arg0, long arg1, long arg2, long arg3,
  1389. long arg4, long arg5, long arg6, long arg7,
  1390. struct pt_regs regs)
  1391. {
  1392. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1393. && (current->ptrace & PT_PTRACED))
  1394. syscall_trace();
  1395. if (unlikely(current->audit_context)) {
  1396. long syscall;
  1397. int arch;
  1398. if (IS_IA32_PROCESS(&regs)) {
  1399. syscall = regs.r1;
  1400. arch = AUDIT_ARCH_I386;
  1401. } else {
  1402. syscall = regs.r15;
  1403. arch = AUDIT_ARCH_IA64;
  1404. }
  1405. audit_syscall_entry(current, arch, syscall, arg0, arg1, arg2, arg3);
  1406. }
  1407. }
  1408. /* "asmlinkage" so the input arguments are preserved... */
  1409. asmlinkage void
  1410. syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
  1411. long arg4, long arg5, long arg6, long arg7,
  1412. struct pt_regs regs)
  1413. {
  1414. if (unlikely(current->audit_context))
  1415. audit_syscall_exit(current, AUDITSC_RESULT(regs.r10), regs.r8);
  1416. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1417. && (current->ptrace & PT_PTRACED))
  1418. syscall_trace();
  1419. }