ptrace.c 44 KB

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