ptrace.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Kernel support for the ptrace() and syscall tracing interfaces.
  4. *
  5. * Copyright (C) 1999-2005 Hewlett-Packard Co
  6. * David Mosberger-Tang <[email protected]>
  7. * Copyright (C) 2006 Intel Co
  8. * 2006-08-12 - IA64 Native Utrace implementation support added by
  9. * Anil S Keshavamurthy <[email protected]>
  10. *
  11. * Derived from the x86 and Alpha versions.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/mm.h>
  18. #include <linux/errno.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/user.h>
  21. #include <linux/security.h>
  22. #include <linux/audit.h>
  23. #include <linux/signal.h>
  24. #include <linux/regset.h>
  25. #include <linux/elf.h>
  26. #include <linux/resume_user_mode.h>
  27. #include <asm/processor.h>
  28. #include <asm/ptrace_offsets.h>
  29. #include <asm/rse.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/unwind.h>
  32. #include "entry.h"
  33. /*
  34. * Bits in the PSR that we allow ptrace() to change:
  35. * be, up, ac, mfl, mfh (the user mask; five bits total)
  36. * db (debug breakpoint fault; one bit)
  37. * id (instruction debug fault disable; one bit)
  38. * dd (data debug fault disable; one bit)
  39. * ri (restart instruction; two bits)
  40. * is (instruction set; one bit)
  41. */
  42. #define IPSR_MASK (IA64_PSR_UM | IA64_PSR_DB | IA64_PSR_IS \
  43. | IA64_PSR_ID | IA64_PSR_DD | IA64_PSR_RI)
  44. #define MASK(nbits) ((1UL << (nbits)) - 1) /* mask with NBITS bits set */
  45. #define PFM_MASK MASK(38)
  46. #define PTRACE_DEBUG 0
  47. #if PTRACE_DEBUG
  48. # define dprintk(format...) printk(format)
  49. # define inline
  50. #else
  51. # define dprintk(format...)
  52. #endif
  53. /* Return TRUE if PT was created due to kernel-entry via a system-call. */
  54. static inline int
  55. in_syscall (struct pt_regs *pt)
  56. {
  57. return (long) pt->cr_ifs >= 0;
  58. }
  59. /*
  60. * Collect the NaT bits for r1-r31 from scratch_unat and return a NaT
  61. * bitset where bit i is set iff the NaT bit of register i is set.
  62. */
  63. unsigned long
  64. ia64_get_scratch_nat_bits (struct pt_regs *pt, unsigned long scratch_unat)
  65. {
  66. # define GET_BITS(first, last, unat) \
  67. ({ \
  68. unsigned long bit = ia64_unat_pos(&pt->r##first); \
  69. unsigned long nbits = (last - first + 1); \
  70. unsigned long mask = MASK(nbits) << first; \
  71. unsigned long dist; \
  72. if (bit < first) \
  73. dist = 64 + bit - first; \
  74. else \
  75. dist = bit - first; \
  76. ia64_rotr(unat, dist) & mask; \
  77. })
  78. unsigned long val;
  79. /*
  80. * Registers that are stored consecutively in struct pt_regs
  81. * can be handled in parallel. If the register order in
  82. * struct_pt_regs changes, this code MUST be updated.
  83. */
  84. val = GET_BITS( 1, 1, scratch_unat);
  85. val |= GET_BITS( 2, 3, scratch_unat);
  86. val |= GET_BITS(12, 13, scratch_unat);
  87. val |= GET_BITS(14, 14, scratch_unat);
  88. val |= GET_BITS(15, 15, scratch_unat);
  89. val |= GET_BITS( 8, 11, scratch_unat);
  90. val |= GET_BITS(16, 31, scratch_unat);
  91. return val;
  92. # undef GET_BITS
  93. }
  94. /*
  95. * Set the NaT bits for the scratch registers according to NAT and
  96. * return the resulting unat (assuming the scratch registers are
  97. * stored in PT).
  98. */
  99. unsigned long
  100. ia64_put_scratch_nat_bits (struct pt_regs *pt, unsigned long nat)
  101. {
  102. # define PUT_BITS(first, last, nat) \
  103. ({ \
  104. unsigned long bit = ia64_unat_pos(&pt->r##first); \
  105. unsigned long nbits = (last - first + 1); \
  106. unsigned long mask = MASK(nbits) << first; \
  107. long dist; \
  108. if (bit < first) \
  109. dist = 64 + bit - first; \
  110. else \
  111. dist = bit - first; \
  112. ia64_rotl(nat & mask, dist); \
  113. })
  114. unsigned long scratch_unat;
  115. /*
  116. * Registers that are stored consecutively in struct pt_regs
  117. * can be handled in parallel. If the register order in
  118. * struct_pt_regs changes, this code MUST be updated.
  119. */
  120. scratch_unat = PUT_BITS( 1, 1, nat);
  121. scratch_unat |= PUT_BITS( 2, 3, nat);
  122. scratch_unat |= PUT_BITS(12, 13, nat);
  123. scratch_unat |= PUT_BITS(14, 14, nat);
  124. scratch_unat |= PUT_BITS(15, 15, nat);
  125. scratch_unat |= PUT_BITS( 8, 11, nat);
  126. scratch_unat |= PUT_BITS(16, 31, nat);
  127. return scratch_unat;
  128. # undef PUT_BITS
  129. }
  130. #define IA64_MLX_TEMPLATE 0x2
  131. #define IA64_MOVL_OPCODE 6
  132. void
  133. ia64_increment_ip (struct pt_regs *regs)
  134. {
  135. unsigned long w0, ri = ia64_psr(regs)->ri + 1;
  136. if (ri > 2) {
  137. ri = 0;
  138. regs->cr_iip += 16;
  139. } else if (ri == 2) {
  140. get_user(w0, (char __user *) regs->cr_iip + 0);
  141. if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
  142. /*
  143. * rfi'ing to slot 2 of an MLX bundle causes
  144. * an illegal operation fault. We don't want
  145. * that to happen...
  146. */
  147. ri = 0;
  148. regs->cr_iip += 16;
  149. }
  150. }
  151. ia64_psr(regs)->ri = ri;
  152. }
  153. void
  154. ia64_decrement_ip (struct pt_regs *regs)
  155. {
  156. unsigned long w0, ri = ia64_psr(regs)->ri - 1;
  157. if (ia64_psr(regs)->ri == 0) {
  158. regs->cr_iip -= 16;
  159. ri = 2;
  160. get_user(w0, (char __user *) regs->cr_iip + 0);
  161. if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
  162. /*
  163. * rfi'ing to slot 2 of an MLX bundle causes
  164. * an illegal operation fault. We don't want
  165. * that to happen...
  166. */
  167. ri = 1;
  168. }
  169. }
  170. ia64_psr(regs)->ri = ri;
  171. }
  172. /*
  173. * This routine is used to read an rnat bits that are stored on the
  174. * kernel backing store. Since, in general, the alignment of the user
  175. * and kernel are different, this is not completely trivial. In
  176. * essence, we need to construct the user RNAT based on up to two
  177. * kernel RNAT values and/or the RNAT value saved in the child's
  178. * pt_regs.
  179. *
  180. * user rbs
  181. *
  182. * +--------+ <-- lowest address
  183. * | slot62 |
  184. * +--------+
  185. * | rnat | 0x....1f8
  186. * +--------+
  187. * | slot00 | \
  188. * +--------+ |
  189. * | slot01 | > child_regs->ar_rnat
  190. * +--------+ |
  191. * | slot02 | / kernel rbs
  192. * +--------+ +--------+
  193. * <- child_regs->ar_bspstore | slot61 | <-- krbs
  194. * +- - - - + +--------+
  195. * | slot62 |
  196. * +- - - - + +--------+
  197. * | rnat |
  198. * +- - - - + +--------+
  199. * vrnat | slot00 |
  200. * +- - - - + +--------+
  201. * = =
  202. * +--------+
  203. * | slot00 | \
  204. * +--------+ |
  205. * | slot01 | > child_stack->ar_rnat
  206. * +--------+ |
  207. * | slot02 | /
  208. * +--------+
  209. * <--- child_stack->ar_bspstore
  210. *
  211. * The way to think of this code is as follows: bit 0 in the user rnat
  212. * corresponds to some bit N (0 <= N <= 62) in one of the kernel rnat
  213. * value. The kernel rnat value holding this bit is stored in
  214. * variable rnat0. rnat1 is loaded with the kernel rnat value that
  215. * form the upper bits of the user rnat value.
  216. *
  217. * Boundary cases:
  218. *
  219. * o when reading the rnat "below" the first rnat slot on the kernel
  220. * backing store, rnat0/rnat1 are set to 0 and the low order bits are
  221. * merged in from pt->ar_rnat.
  222. *
  223. * o when reading the rnat "above" the last rnat slot on the kernel
  224. * backing store, rnat0/rnat1 gets its value from sw->ar_rnat.
  225. */
  226. static unsigned long
  227. get_rnat (struct task_struct *task, struct switch_stack *sw,
  228. unsigned long *krbs, unsigned long *urnat_addr,
  229. unsigned long *urbs_end)
  230. {
  231. unsigned long rnat0 = 0, rnat1 = 0, urnat = 0, *slot0_kaddr;
  232. unsigned long umask = 0, mask, m;
  233. unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
  234. long num_regs, nbits;
  235. struct pt_regs *pt;
  236. pt = task_pt_regs(task);
  237. kbsp = (unsigned long *) sw->ar_bspstore;
  238. ubspstore = (unsigned long *) pt->ar_bspstore;
  239. if (urbs_end < urnat_addr)
  240. nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);
  241. else
  242. nbits = 63;
  243. mask = MASK(nbits);
  244. /*
  245. * First, figure out which bit number slot 0 in user-land maps
  246. * to in the kernel rnat. Do this by figuring out how many
  247. * register slots we're beyond the user's backingstore and
  248. * then computing the equivalent address in kernel space.
  249. */
  250. num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
  251. slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
  252. shift = ia64_rse_slot_num(slot0_kaddr);
  253. rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
  254. rnat0_kaddr = rnat1_kaddr - 64;
  255. if (ubspstore + 63 > urnat_addr) {
  256. /* some bits need to be merged in from pt->ar_rnat */
  257. umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
  258. urnat = (pt->ar_rnat & umask);
  259. mask &= ~umask;
  260. if (!mask)
  261. return urnat;
  262. }
  263. m = mask << shift;
  264. if (rnat0_kaddr >= kbsp)
  265. rnat0 = sw->ar_rnat;
  266. else if (rnat0_kaddr > krbs)
  267. rnat0 = *rnat0_kaddr;
  268. urnat |= (rnat0 & m) >> shift;
  269. m = mask >> (63 - shift);
  270. if (rnat1_kaddr >= kbsp)
  271. rnat1 = sw->ar_rnat;
  272. else if (rnat1_kaddr > krbs)
  273. rnat1 = *rnat1_kaddr;
  274. urnat |= (rnat1 & m) << (63 - shift);
  275. return urnat;
  276. }
  277. /*
  278. * The reverse of get_rnat.
  279. */
  280. static void
  281. put_rnat (struct task_struct *task, struct switch_stack *sw,
  282. unsigned long *krbs, unsigned long *urnat_addr, unsigned long urnat,
  283. unsigned long *urbs_end)
  284. {
  285. unsigned long rnat0 = 0, rnat1 = 0, *slot0_kaddr, umask = 0, mask, m;
  286. unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
  287. long num_regs, nbits;
  288. struct pt_regs *pt;
  289. unsigned long cfm, *urbs_kargs;
  290. pt = task_pt_regs(task);
  291. kbsp = (unsigned long *) sw->ar_bspstore;
  292. ubspstore = (unsigned long *) pt->ar_bspstore;
  293. urbs_kargs = urbs_end;
  294. if (in_syscall(pt)) {
  295. /*
  296. * If entered via syscall, don't allow user to set rnat bits
  297. * for syscall args.
  298. */
  299. cfm = pt->cr_ifs;
  300. urbs_kargs = ia64_rse_skip_regs(urbs_end, -(cfm & 0x7f));
  301. }
  302. if (urbs_kargs >= urnat_addr)
  303. nbits = 63;
  304. else {
  305. if ((urnat_addr - 63) >= urbs_kargs)
  306. return;
  307. nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);
  308. }
  309. mask = MASK(nbits);
  310. /*
  311. * First, figure out which bit number slot 0 in user-land maps
  312. * to in the kernel rnat. Do this by figuring out how many
  313. * register slots we're beyond the user's backingstore and
  314. * then computing the equivalent address in kernel space.
  315. */
  316. num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
  317. slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
  318. shift = ia64_rse_slot_num(slot0_kaddr);
  319. rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
  320. rnat0_kaddr = rnat1_kaddr - 64;
  321. if (ubspstore + 63 > urnat_addr) {
  322. /* some bits need to be place in pt->ar_rnat: */
  323. umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
  324. pt->ar_rnat = (pt->ar_rnat & ~umask) | (urnat & umask);
  325. mask &= ~umask;
  326. if (!mask)
  327. return;
  328. }
  329. /*
  330. * Note: Section 11.1 of the EAS guarantees that bit 63 of an
  331. * rnat slot is ignored. so we don't have to clear it here.
  332. */
  333. rnat0 = (urnat << shift);
  334. m = mask << shift;
  335. if (rnat0_kaddr >= kbsp)
  336. sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat0 & m);
  337. else if (rnat0_kaddr > krbs)
  338. *rnat0_kaddr = ((*rnat0_kaddr & ~m) | (rnat0 & m));
  339. rnat1 = (urnat >> (63 - shift));
  340. m = mask >> (63 - shift);
  341. if (rnat1_kaddr >= kbsp)
  342. sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat1 & m);
  343. else if (rnat1_kaddr > krbs)
  344. *rnat1_kaddr = ((*rnat1_kaddr & ~m) | (rnat1 & m));
  345. }
  346. static inline int
  347. on_kernel_rbs (unsigned long addr, unsigned long bspstore,
  348. unsigned long urbs_end)
  349. {
  350. unsigned long *rnat_addr = ia64_rse_rnat_addr((unsigned long *)
  351. urbs_end);
  352. return (addr >= bspstore && addr <= (unsigned long) rnat_addr);
  353. }
  354. /*
  355. * Read a word from the user-level backing store of task CHILD. ADDR
  356. * is the user-level address to read the word from, VAL a pointer to
  357. * the return value, and USER_BSP gives the end of the user-level
  358. * backing store (i.e., it's the address that would be in ar.bsp after
  359. * the user executed a "cover" instruction).
  360. *
  361. * This routine takes care of accessing the kernel register backing
  362. * store for those registers that got spilled there. It also takes
  363. * care of calculating the appropriate RNaT collection words.
  364. */
  365. long
  366. ia64_peek (struct task_struct *child, struct switch_stack *child_stack,
  367. unsigned long user_rbs_end, unsigned long addr, long *val)
  368. {
  369. unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;
  370. struct pt_regs *child_regs;
  371. size_t copied;
  372. long ret;
  373. urbs_end = (long *) user_rbs_end;
  374. laddr = (unsigned long *) addr;
  375. child_regs = task_pt_regs(child);
  376. bspstore = (unsigned long *) child_regs->ar_bspstore;
  377. krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
  378. if (on_kernel_rbs(addr, (unsigned long) bspstore,
  379. (unsigned long) urbs_end))
  380. {
  381. /*
  382. * Attempt to read the RBS in an area that's actually
  383. * on the kernel RBS => read the corresponding bits in
  384. * the kernel RBS.
  385. */
  386. rnat_addr = ia64_rse_rnat_addr(laddr);
  387. ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);
  388. if (laddr == rnat_addr) {
  389. /* return NaT collection word itself */
  390. *val = ret;
  391. return 0;
  392. }
  393. if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {
  394. /*
  395. * It is implementation dependent whether the
  396. * data portion of a NaT value gets saved on a
  397. * st8.spill or RSE spill (e.g., see EAS 2.6,
  398. * 4.4.4.6 Register Spill and Fill). To get
  399. * consistent behavior across all possible
  400. * IA-64 implementations, we return zero in
  401. * this case.
  402. */
  403. *val = 0;
  404. return 0;
  405. }
  406. if (laddr < urbs_end) {
  407. /*
  408. * The desired word is on the kernel RBS and
  409. * is not a NaT.
  410. */
  411. regnum = ia64_rse_num_regs(bspstore, laddr);
  412. *val = *ia64_rse_skip_regs(krbs, regnum);
  413. return 0;
  414. }
  415. }
  416. copied = access_process_vm(child, addr, &ret, sizeof(ret), FOLL_FORCE);
  417. if (copied != sizeof(ret))
  418. return -EIO;
  419. *val = ret;
  420. return 0;
  421. }
  422. long
  423. ia64_poke (struct task_struct *child, struct switch_stack *child_stack,
  424. unsigned long user_rbs_end, unsigned long addr, long val)
  425. {
  426. unsigned long *bspstore, *krbs, regnum, *laddr;
  427. unsigned long *urbs_end = (long *) user_rbs_end;
  428. struct pt_regs *child_regs;
  429. laddr = (unsigned long *) addr;
  430. child_regs = task_pt_regs(child);
  431. bspstore = (unsigned long *) child_regs->ar_bspstore;
  432. krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
  433. if (on_kernel_rbs(addr, (unsigned long) bspstore,
  434. (unsigned long) urbs_end))
  435. {
  436. /*
  437. * Attempt to write the RBS in an area that's actually
  438. * on the kernel RBS => write the corresponding bits
  439. * in the kernel RBS.
  440. */
  441. if (ia64_rse_is_rnat_slot(laddr))
  442. put_rnat(child, child_stack, krbs, laddr, val,
  443. urbs_end);
  444. else {
  445. if (laddr < urbs_end) {
  446. regnum = ia64_rse_num_regs(bspstore, laddr);
  447. *ia64_rse_skip_regs(krbs, regnum) = val;
  448. }
  449. }
  450. } else if (access_process_vm(child, addr, &val, sizeof(val),
  451. FOLL_FORCE | FOLL_WRITE)
  452. != sizeof(val))
  453. return -EIO;
  454. return 0;
  455. }
  456. /*
  457. * Calculate the address of the end of the user-level register backing
  458. * store. This is the address that would have been stored in ar.bsp
  459. * if the user had executed a "cover" instruction right before
  460. * entering the kernel. If CFMP is not NULL, it is used to return the
  461. * "current frame mask" that was active at the time the kernel was
  462. * entered.
  463. */
  464. unsigned long
  465. ia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt,
  466. unsigned long *cfmp)
  467. {
  468. unsigned long *krbs, *bspstore, cfm = pt->cr_ifs;
  469. long ndirty;
  470. krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
  471. bspstore = (unsigned long *) pt->ar_bspstore;
  472. ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
  473. if (in_syscall(pt))
  474. ndirty += (cfm & 0x7f);
  475. else
  476. cfm &= ~(1UL << 63); /* clear valid bit */
  477. if (cfmp)
  478. *cfmp = cfm;
  479. return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);
  480. }
  481. /*
  482. * Synchronize (i.e, write) the RSE backing store living in kernel
  483. * space to the VM of the CHILD task. SW and PT are the pointers to
  484. * the switch_stack and pt_regs structures, respectively.
  485. * USER_RBS_END is the user-level address at which the backing store
  486. * ends.
  487. */
  488. long
  489. ia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,
  490. unsigned long user_rbs_start, unsigned long user_rbs_end)
  491. {
  492. unsigned long addr, val;
  493. long ret;
  494. /* now copy word for word from kernel rbs to user rbs: */
  495. for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
  496. ret = ia64_peek(child, sw, user_rbs_end, addr, &val);
  497. if (ret < 0)
  498. return ret;
  499. if (access_process_vm(child, addr, &val, sizeof(val),
  500. FOLL_FORCE | FOLL_WRITE)
  501. != sizeof(val))
  502. return -EIO;
  503. }
  504. return 0;
  505. }
  506. static long
  507. ia64_sync_kernel_rbs (struct task_struct *child, struct switch_stack *sw,
  508. unsigned long user_rbs_start, unsigned long user_rbs_end)
  509. {
  510. unsigned long addr, val;
  511. long ret;
  512. /* now copy word for word from user rbs to kernel rbs: */
  513. for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
  514. if (access_process_vm(child, addr, &val, sizeof(val),
  515. FOLL_FORCE)
  516. != sizeof(val))
  517. return -EIO;
  518. ret = ia64_poke(child, sw, user_rbs_end, addr, val);
  519. if (ret < 0)
  520. return ret;
  521. }
  522. return 0;
  523. }
  524. typedef long (*syncfunc_t)(struct task_struct *, struct switch_stack *,
  525. unsigned long, unsigned long);
  526. static void do_sync_rbs(struct unw_frame_info *info, void *arg)
  527. {
  528. struct pt_regs *pt;
  529. unsigned long urbs_end;
  530. syncfunc_t fn = arg;
  531. if (unw_unwind_to_user(info) < 0)
  532. return;
  533. pt = task_pt_regs(info->task);
  534. urbs_end = ia64_get_user_rbs_end(info->task, pt, NULL);
  535. fn(info->task, info->sw, pt->ar_bspstore, urbs_end);
  536. }
  537. /*
  538. * when a thread is stopped (ptraced), debugger might change thread's user
  539. * stack (change memory directly), and we must avoid the RSE stored in kernel
  540. * to override user stack (user space's RSE is newer than kernel's in the
  541. * case). To workaround the issue, we copy kernel RSE to user RSE before the
  542. * task is stopped, so user RSE has updated data. we then copy user RSE to
  543. * kernel after the task is resummed from traced stop and kernel will use the
  544. * newer RSE to return to user. TIF_RESTORE_RSE is the flag to indicate we need
  545. * synchronize user RSE to kernel.
  546. */
  547. void ia64_ptrace_stop(void)
  548. {
  549. if (test_and_set_tsk_thread_flag(current, TIF_RESTORE_RSE))
  550. return;
  551. set_notify_resume(current);
  552. unw_init_running(do_sync_rbs, ia64_sync_user_rbs);
  553. }
  554. /*
  555. * This is called to read back the register backing store.
  556. */
  557. void ia64_sync_krbs(void)
  558. {
  559. clear_tsk_thread_flag(current, TIF_RESTORE_RSE);
  560. unw_init_running(do_sync_rbs, ia64_sync_kernel_rbs);
  561. }
  562. /*
  563. * Write f32-f127 back to task->thread.fph if it has been modified.
  564. */
  565. inline void
  566. ia64_flush_fph (struct task_struct *task)
  567. {
  568. struct ia64_psr *psr = ia64_psr(task_pt_regs(task));
  569. /*
  570. * Prevent migrating this task while
  571. * we're fiddling with the FPU state
  572. */
  573. preempt_disable();
  574. if (ia64_is_local_fpu_owner(task) && psr->mfh) {
  575. psr->mfh = 0;
  576. task->thread.flags |= IA64_THREAD_FPH_VALID;
  577. ia64_save_fpu(&task->thread.fph[0]);
  578. }
  579. preempt_enable();
  580. }
  581. /*
  582. * Sync the fph state of the task so that it can be manipulated
  583. * through thread.fph. If necessary, f32-f127 are written back to
  584. * thread.fph or, if the fph state hasn't been used before, thread.fph
  585. * is cleared to zeroes. Also, access to f32-f127 is disabled to
  586. * ensure that the task picks up the state from thread.fph when it
  587. * executes again.
  588. */
  589. void
  590. ia64_sync_fph (struct task_struct *task)
  591. {
  592. struct ia64_psr *psr = ia64_psr(task_pt_regs(task));
  593. ia64_flush_fph(task);
  594. if (!(task->thread.flags & IA64_THREAD_FPH_VALID)) {
  595. task->thread.flags |= IA64_THREAD_FPH_VALID;
  596. memset(&task->thread.fph, 0, sizeof(task->thread.fph));
  597. }
  598. ia64_drop_fpu(task);
  599. psr->dfh = 1;
  600. }
  601. /*
  602. * Change the machine-state of CHILD such that it will return via the normal
  603. * kernel exit-path, rather than the syscall-exit path.
  604. */
  605. static void
  606. convert_to_non_syscall (struct task_struct *child, struct pt_regs *pt,
  607. unsigned long cfm)
  608. {
  609. struct unw_frame_info info, prev_info;
  610. unsigned long ip, sp, pr;
  611. unw_init_from_blocked_task(&info, child);
  612. while (1) {
  613. prev_info = info;
  614. if (unw_unwind(&info) < 0)
  615. return;
  616. unw_get_sp(&info, &sp);
  617. if ((long)((unsigned long)child + IA64_STK_OFFSET - sp)
  618. < IA64_PT_REGS_SIZE) {
  619. dprintk("ptrace.%s: ran off the top of the kernel "
  620. "stack\n", __func__);
  621. return;
  622. }
  623. if (unw_get_pr (&prev_info, &pr) < 0) {
  624. unw_get_rp(&prev_info, &ip);
  625. dprintk("ptrace.%s: failed to read "
  626. "predicate register (ip=0x%lx)\n",
  627. __func__, ip);
  628. return;
  629. }
  630. if (unw_is_intr_frame(&info)
  631. && (pr & (1UL << PRED_USER_STACK)))
  632. break;
  633. }
  634. /*
  635. * Note: at the time of this call, the target task is blocked
  636. * in notify_resume_user() and by clearling PRED_LEAVE_SYSCALL
  637. * (aka, "pLvSys") we redirect execution from
  638. * .work_pending_syscall_end to .work_processed_kernel.
  639. */
  640. unw_get_pr(&prev_info, &pr);
  641. pr &= ~((1UL << PRED_SYSCALL) | (1UL << PRED_LEAVE_SYSCALL));
  642. pr |= (1UL << PRED_NON_SYSCALL);
  643. unw_set_pr(&prev_info, pr);
  644. pt->cr_ifs = (1UL << 63) | cfm;
  645. /*
  646. * Clear the memory that is NOT written on syscall-entry to
  647. * ensure we do not leak kernel-state to user when execution
  648. * resumes.
  649. */
  650. pt->r2 = 0;
  651. pt->r3 = 0;
  652. pt->r14 = 0;
  653. memset(&pt->r16, 0, 16*8); /* clear r16-r31 */
  654. memset(&pt->f6, 0, 6*16); /* clear f6-f11 */
  655. pt->b7 = 0;
  656. pt->ar_ccv = 0;
  657. pt->ar_csd = 0;
  658. pt->ar_ssd = 0;
  659. }
  660. static int
  661. access_nat_bits (struct task_struct *child, struct pt_regs *pt,
  662. struct unw_frame_info *info,
  663. unsigned long *data, int write_access)
  664. {
  665. unsigned long regnum, nat_bits, scratch_unat, dummy = 0;
  666. char nat = 0;
  667. if (write_access) {
  668. nat_bits = *data;
  669. scratch_unat = ia64_put_scratch_nat_bits(pt, nat_bits);
  670. if (unw_set_ar(info, UNW_AR_UNAT, scratch_unat) < 0) {
  671. dprintk("ptrace: failed to set ar.unat\n");
  672. return -1;
  673. }
  674. for (regnum = 4; regnum <= 7; ++regnum) {
  675. unw_get_gr(info, regnum, &dummy, &nat);
  676. unw_set_gr(info, regnum, dummy,
  677. (nat_bits >> regnum) & 1);
  678. }
  679. } else {
  680. if (unw_get_ar(info, UNW_AR_UNAT, &scratch_unat) < 0) {
  681. dprintk("ptrace: failed to read ar.unat\n");
  682. return -1;
  683. }
  684. nat_bits = ia64_get_scratch_nat_bits(pt, scratch_unat);
  685. for (regnum = 4; regnum <= 7; ++regnum) {
  686. unw_get_gr(info, regnum, &dummy, &nat);
  687. nat_bits |= (nat != 0) << regnum;
  688. }
  689. *data = nat_bits;
  690. }
  691. return 0;
  692. }
  693. static int
  694. access_elf_reg(struct task_struct *target, struct unw_frame_info *info,
  695. unsigned long addr, unsigned long *data, int write_access);
  696. static long
  697. ptrace_getregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
  698. {
  699. unsigned long psr, ec, lc, rnat, bsp, cfm, nat_bits, val;
  700. struct unw_frame_info info;
  701. struct ia64_fpreg fpval;
  702. struct switch_stack *sw;
  703. struct pt_regs *pt;
  704. long ret, retval = 0;
  705. char nat = 0;
  706. int i;
  707. if (!access_ok(ppr, sizeof(struct pt_all_user_regs)))
  708. return -EIO;
  709. pt = task_pt_regs(child);
  710. sw = (struct switch_stack *) (child->thread.ksp + 16);
  711. unw_init_from_blocked_task(&info, child);
  712. if (unw_unwind_to_user(&info) < 0) {
  713. return -EIO;
  714. }
  715. if (((unsigned long) ppr & 0x7) != 0) {
  716. dprintk("ptrace:unaligned register address %p\n", ppr);
  717. return -EIO;
  718. }
  719. if (access_elf_reg(child, &info, ELF_CR_IPSR_OFFSET, &psr, 0) < 0 ||
  720. access_elf_reg(child, &info, ELF_AR_EC_OFFSET, &ec, 0) < 0 ||
  721. access_elf_reg(child, &info, ELF_AR_LC_OFFSET, &lc, 0) < 0 ||
  722. access_elf_reg(child, &info, ELF_AR_RNAT_OFFSET, &rnat, 0) < 0 ||
  723. access_elf_reg(child, &info, ELF_AR_BSP_OFFSET, &bsp, 0) < 0 ||
  724. access_elf_reg(child, &info, ELF_CFM_OFFSET, &cfm, 0) < 0 ||
  725. access_elf_reg(child, &info, ELF_NAT_OFFSET, &nat_bits, 0) < 0)
  726. return -EIO;
  727. /* control regs */
  728. retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
  729. retval |= __put_user(psr, &ppr->cr_ipsr);
  730. /* app regs */
  731. retval |= __put_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
  732. retval |= __put_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
  733. retval |= __put_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
  734. retval |= __put_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
  735. retval |= __put_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
  736. retval |= __put_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
  737. retval |= __put_user(ec, &ppr->ar[PT_AUR_EC]);
  738. retval |= __put_user(lc, &ppr->ar[PT_AUR_LC]);
  739. retval |= __put_user(rnat, &ppr->ar[PT_AUR_RNAT]);
  740. retval |= __put_user(bsp, &ppr->ar[PT_AUR_BSP]);
  741. retval |= __put_user(cfm, &ppr->cfm);
  742. /* gr1-gr3 */
  743. retval |= __copy_to_user(&ppr->gr[1], &pt->r1, sizeof(long));
  744. retval |= __copy_to_user(&ppr->gr[2], &pt->r2, sizeof(long) *2);
  745. /* gr4-gr7 */
  746. for (i = 4; i < 8; i++) {
  747. if (unw_access_gr(&info, i, &val, &nat, 0) < 0)
  748. return -EIO;
  749. retval |= __put_user(val, &ppr->gr[i]);
  750. }
  751. /* gr8-gr11 */
  752. retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
  753. /* gr12-gr15 */
  754. retval |= __copy_to_user(&ppr->gr[12], &pt->r12, sizeof(long) * 2);
  755. retval |= __copy_to_user(&ppr->gr[14], &pt->r14, sizeof(long));
  756. retval |= __copy_to_user(&ppr->gr[15], &pt->r15, sizeof(long));
  757. /* gr16-gr31 */
  758. retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
  759. /* b0 */
  760. retval |= __put_user(pt->b0, &ppr->br[0]);
  761. /* b1-b5 */
  762. for (i = 1; i < 6; i++) {
  763. if (unw_access_br(&info, i, &val, 0) < 0)
  764. return -EIO;
  765. __put_user(val, &ppr->br[i]);
  766. }
  767. /* b6-b7 */
  768. retval |= __put_user(pt->b6, &ppr->br[6]);
  769. retval |= __put_user(pt->b7, &ppr->br[7]);
  770. /* fr2-fr5 */
  771. for (i = 2; i < 6; i++) {
  772. if (unw_get_fr(&info, i, &fpval) < 0)
  773. return -EIO;
  774. retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
  775. }
  776. /* fr6-fr11 */
  777. retval |= __copy_to_user(&ppr->fr[6], &pt->f6,
  778. sizeof(struct ia64_fpreg) * 6);
  779. /* fp scratch regs(12-15) */
  780. retval |= __copy_to_user(&ppr->fr[12], &sw->f12,
  781. sizeof(struct ia64_fpreg) * 4);
  782. /* fr16-fr31 */
  783. for (i = 16; i < 32; i++) {
  784. if (unw_get_fr(&info, i, &fpval) < 0)
  785. return -EIO;
  786. retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
  787. }
  788. /* fph */
  789. ia64_flush_fph(child);
  790. retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph,
  791. sizeof(ppr->fr[32]) * 96);
  792. /* preds */
  793. retval |= __put_user(pt->pr, &ppr->pr);
  794. /* nat bits */
  795. retval |= __put_user(nat_bits, &ppr->nat);
  796. ret = retval ? -EIO : 0;
  797. return ret;
  798. }
  799. static long
  800. ptrace_setregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
  801. {
  802. unsigned long psr, rsc, ec, lc, rnat, bsp, cfm, nat_bits, val = 0;
  803. struct unw_frame_info info;
  804. struct switch_stack *sw;
  805. struct ia64_fpreg fpval;
  806. struct pt_regs *pt;
  807. long retval = 0;
  808. int i;
  809. memset(&fpval, 0, sizeof(fpval));
  810. if (!access_ok(ppr, sizeof(struct pt_all_user_regs)))
  811. return -EIO;
  812. pt = task_pt_regs(child);
  813. sw = (struct switch_stack *) (child->thread.ksp + 16);
  814. unw_init_from_blocked_task(&info, child);
  815. if (unw_unwind_to_user(&info) < 0) {
  816. return -EIO;
  817. }
  818. if (((unsigned long) ppr & 0x7) != 0) {
  819. dprintk("ptrace:unaligned register address %p\n", ppr);
  820. return -EIO;
  821. }
  822. /* control regs */
  823. retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
  824. retval |= __get_user(psr, &ppr->cr_ipsr);
  825. /* app regs */
  826. retval |= __get_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
  827. retval |= __get_user(rsc, &ppr->ar[PT_AUR_RSC]);
  828. retval |= __get_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
  829. retval |= __get_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
  830. retval |= __get_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
  831. retval |= __get_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
  832. retval |= __get_user(ec, &ppr->ar[PT_AUR_EC]);
  833. retval |= __get_user(lc, &ppr->ar[PT_AUR_LC]);
  834. retval |= __get_user(rnat, &ppr->ar[PT_AUR_RNAT]);
  835. retval |= __get_user(bsp, &ppr->ar[PT_AUR_BSP]);
  836. retval |= __get_user(cfm, &ppr->cfm);
  837. /* gr1-gr3 */
  838. retval |= __copy_from_user(&pt->r1, &ppr->gr[1], sizeof(long));
  839. retval |= __copy_from_user(&pt->r2, &ppr->gr[2], sizeof(long) * 2);
  840. /* gr4-gr7 */
  841. for (i = 4; i < 8; i++) {
  842. retval |= __get_user(val, &ppr->gr[i]);
  843. /* NaT bit will be set via PT_NAT_BITS: */
  844. if (unw_set_gr(&info, i, val, 0) < 0)
  845. return -EIO;
  846. }
  847. /* gr8-gr11 */
  848. retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
  849. /* gr12-gr15 */
  850. retval |= __copy_from_user(&pt->r12, &ppr->gr[12], sizeof(long) * 2);
  851. retval |= __copy_from_user(&pt->r14, &ppr->gr[14], sizeof(long));
  852. retval |= __copy_from_user(&pt->r15, &ppr->gr[15], sizeof(long));
  853. /* gr16-gr31 */
  854. retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
  855. /* b0 */
  856. retval |= __get_user(pt->b0, &ppr->br[0]);
  857. /* b1-b5 */
  858. for (i = 1; i < 6; i++) {
  859. retval |= __get_user(val, &ppr->br[i]);
  860. unw_set_br(&info, i, val);
  861. }
  862. /* b6-b7 */
  863. retval |= __get_user(pt->b6, &ppr->br[6]);
  864. retval |= __get_user(pt->b7, &ppr->br[7]);
  865. /* fr2-fr5 */
  866. for (i = 2; i < 6; i++) {
  867. retval |= __copy_from_user(&fpval, &ppr->fr[i], sizeof(fpval));
  868. if (unw_set_fr(&info, i, fpval) < 0)
  869. return -EIO;
  870. }
  871. /* fr6-fr11 */
  872. retval |= __copy_from_user(&pt->f6, &ppr->fr[6],
  873. sizeof(ppr->fr[6]) * 6);
  874. /* fp scratch regs(12-15) */
  875. retval |= __copy_from_user(&sw->f12, &ppr->fr[12],
  876. sizeof(ppr->fr[12]) * 4);
  877. /* fr16-fr31 */
  878. for (i = 16; i < 32; i++) {
  879. retval |= __copy_from_user(&fpval, &ppr->fr[i],
  880. sizeof(fpval));
  881. if (unw_set_fr(&info, i, fpval) < 0)
  882. return -EIO;
  883. }
  884. /* fph */
  885. ia64_sync_fph(child);
  886. retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32],
  887. sizeof(ppr->fr[32]) * 96);
  888. /* preds */
  889. retval |= __get_user(pt->pr, &ppr->pr);
  890. /* nat bits */
  891. retval |= __get_user(nat_bits, &ppr->nat);
  892. retval |= access_elf_reg(child, &info, ELF_CR_IPSR_OFFSET, &psr, 1);
  893. retval |= access_elf_reg(child, &info, ELF_AR_RSC_OFFSET, &rsc, 1);
  894. retval |= access_elf_reg(child, &info, ELF_AR_EC_OFFSET, &ec, 1);
  895. retval |= access_elf_reg(child, &info, ELF_AR_LC_OFFSET, &lc, 1);
  896. retval |= access_elf_reg(child, &info, ELF_AR_RNAT_OFFSET, &rnat, 1);
  897. retval |= access_elf_reg(child, &info, ELF_AR_BSP_OFFSET, &bsp, 1);
  898. retval |= access_elf_reg(child, &info, ELF_CFM_OFFSET, &cfm, 1);
  899. retval |= access_elf_reg(child, &info, ELF_NAT_OFFSET, &nat_bits, 1);
  900. return retval ? -EIO : 0;
  901. }
  902. void
  903. user_enable_single_step (struct task_struct *child)
  904. {
  905. struct ia64_psr *child_psr = ia64_psr(task_pt_regs(child));
  906. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  907. child_psr->ss = 1;
  908. }
  909. void
  910. user_enable_block_step (struct task_struct *child)
  911. {
  912. struct ia64_psr *child_psr = ia64_psr(task_pt_regs(child));
  913. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  914. child_psr->tb = 1;
  915. }
  916. void
  917. user_disable_single_step (struct task_struct *child)
  918. {
  919. struct ia64_psr *child_psr = ia64_psr(task_pt_regs(child));
  920. /* make sure the single step/taken-branch trap bits are not set: */
  921. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  922. child_psr->ss = 0;
  923. child_psr->tb = 0;
  924. }
  925. /*
  926. * Called by kernel/ptrace.c when detaching..
  927. *
  928. * Make sure the single step bit is not set.
  929. */
  930. void
  931. ptrace_disable (struct task_struct *child)
  932. {
  933. user_disable_single_step(child);
  934. }
  935. static int
  936. access_uarea (struct task_struct *child, unsigned long addr,
  937. unsigned long *data, int write_access);
  938. long
  939. arch_ptrace (struct task_struct *child, long request,
  940. unsigned long addr, unsigned long data)
  941. {
  942. switch (request) {
  943. case PTRACE_PEEKTEXT:
  944. case PTRACE_PEEKDATA:
  945. /* read word at location addr */
  946. if (ptrace_access_vm(child, addr, &data, sizeof(data),
  947. FOLL_FORCE)
  948. != sizeof(data))
  949. return -EIO;
  950. /* ensure return value is not mistaken for error code */
  951. force_successful_syscall_return();
  952. return data;
  953. /* PTRACE_POKETEXT and PTRACE_POKEDATA is handled
  954. * by the generic ptrace_request().
  955. */
  956. case PTRACE_PEEKUSR:
  957. /* read the word at addr in the USER area */
  958. if (access_uarea(child, addr, &data, 0) < 0)
  959. return -EIO;
  960. /* ensure return value is not mistaken for error code */
  961. force_successful_syscall_return();
  962. return data;
  963. case PTRACE_POKEUSR:
  964. /* write the word at addr in the USER area */
  965. if (access_uarea(child, addr, &data, 1) < 0)
  966. return -EIO;
  967. return 0;
  968. case PTRACE_OLD_GETSIGINFO:
  969. /* for backwards-compatibility */
  970. return ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
  971. case PTRACE_OLD_SETSIGINFO:
  972. /* for backwards-compatibility */
  973. return ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
  974. case PTRACE_GETREGS:
  975. return ptrace_getregs(child,
  976. (struct pt_all_user_regs __user *) data);
  977. case PTRACE_SETREGS:
  978. return ptrace_setregs(child,
  979. (struct pt_all_user_regs __user *) data);
  980. default:
  981. return ptrace_request(child, request, addr, data);
  982. }
  983. }
  984. /* "asmlinkage" so the input arguments are preserved... */
  985. asmlinkage long
  986. syscall_trace_enter (long arg0, long arg1, long arg2, long arg3,
  987. long arg4, long arg5, long arg6, long arg7,
  988. struct pt_regs regs)
  989. {
  990. if (test_thread_flag(TIF_SYSCALL_TRACE))
  991. if (ptrace_report_syscall_entry(&regs))
  992. return -ENOSYS;
  993. /* copy user rbs to kernel rbs */
  994. if (test_thread_flag(TIF_RESTORE_RSE))
  995. ia64_sync_krbs();
  996. audit_syscall_entry(regs.r15, arg0, arg1, arg2, arg3);
  997. return 0;
  998. }
  999. /* "asmlinkage" so the input arguments are preserved... */
  1000. asmlinkage void
  1001. syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
  1002. long arg4, long arg5, long arg6, long arg7,
  1003. struct pt_regs regs)
  1004. {
  1005. int step;
  1006. audit_syscall_exit(&regs);
  1007. step = test_thread_flag(TIF_SINGLESTEP);
  1008. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1009. ptrace_report_syscall_exit(&regs, step);
  1010. /* copy user rbs to kernel rbs */
  1011. if (test_thread_flag(TIF_RESTORE_RSE))
  1012. ia64_sync_krbs();
  1013. }
  1014. /* Utrace implementation starts here */
  1015. struct regset_get {
  1016. void *kbuf;
  1017. void __user *ubuf;
  1018. };
  1019. struct regset_set {
  1020. const void *kbuf;
  1021. const void __user *ubuf;
  1022. };
  1023. struct regset_getset {
  1024. struct task_struct *target;
  1025. const struct user_regset *regset;
  1026. union {
  1027. struct regset_get get;
  1028. struct regset_set set;
  1029. } u;
  1030. unsigned int pos;
  1031. unsigned int count;
  1032. int ret;
  1033. };
  1034. static const ptrdiff_t pt_offsets[32] =
  1035. {
  1036. #define R(n) offsetof(struct pt_regs, r##n)
  1037. [0] = -1, R(1), R(2), R(3),
  1038. [4] = -1, [5] = -1, [6] = -1, [7] = -1,
  1039. R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
  1040. R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
  1041. R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
  1042. #undef R
  1043. };
  1044. static int
  1045. access_elf_gpreg(struct task_struct *target, struct unw_frame_info *info,
  1046. unsigned long addr, unsigned long *data, int write_access)
  1047. {
  1048. struct pt_regs *pt = task_pt_regs(target);
  1049. unsigned reg = addr / sizeof(unsigned long);
  1050. ptrdiff_t d = pt_offsets[reg];
  1051. if (d >= 0) {
  1052. unsigned long *ptr = (void *)pt + d;
  1053. if (write_access)
  1054. *ptr = *data;
  1055. else
  1056. *data = *ptr;
  1057. return 0;
  1058. } else {
  1059. char nat = 0;
  1060. if (write_access) {
  1061. /* read NaT bit first: */
  1062. unsigned long dummy;
  1063. int ret = unw_get_gr(info, reg, &dummy, &nat);
  1064. if (ret < 0)
  1065. return ret;
  1066. }
  1067. return unw_access_gr(info, reg, data, &nat, write_access);
  1068. }
  1069. }
  1070. static int
  1071. access_elf_breg(struct task_struct *target, struct unw_frame_info *info,
  1072. unsigned long addr, unsigned long *data, int write_access)
  1073. {
  1074. struct pt_regs *pt;
  1075. unsigned long *ptr = NULL;
  1076. pt = task_pt_regs(target);
  1077. switch (addr) {
  1078. case ELF_BR_OFFSET(0):
  1079. ptr = &pt->b0;
  1080. break;
  1081. case ELF_BR_OFFSET(1) ... ELF_BR_OFFSET(5):
  1082. return unw_access_br(info, (addr - ELF_BR_OFFSET(0))/8,
  1083. data, write_access);
  1084. case ELF_BR_OFFSET(6):
  1085. ptr = &pt->b6;
  1086. break;
  1087. case ELF_BR_OFFSET(7):
  1088. ptr = &pt->b7;
  1089. }
  1090. if (write_access)
  1091. *ptr = *data;
  1092. else
  1093. *data = *ptr;
  1094. return 0;
  1095. }
  1096. static int
  1097. access_elf_areg(struct task_struct *target, struct unw_frame_info *info,
  1098. unsigned long addr, unsigned long *data, int write_access)
  1099. {
  1100. struct pt_regs *pt;
  1101. unsigned long cfm, urbs_end;
  1102. unsigned long *ptr = NULL;
  1103. pt = task_pt_regs(target);
  1104. if (addr >= ELF_AR_RSC_OFFSET && addr <= ELF_AR_SSD_OFFSET) {
  1105. switch (addr) {
  1106. case ELF_AR_RSC_OFFSET:
  1107. /* force PL3 */
  1108. if (write_access)
  1109. pt->ar_rsc = *data | (3 << 2);
  1110. else
  1111. *data = pt->ar_rsc;
  1112. return 0;
  1113. case ELF_AR_BSP_OFFSET:
  1114. /*
  1115. * By convention, we use PT_AR_BSP to refer to
  1116. * the end of the user-level backing store.
  1117. * Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof)
  1118. * to get the real value of ar.bsp at the time
  1119. * the kernel was entered.
  1120. *
  1121. * Furthermore, when changing the contents of
  1122. * PT_AR_BSP (or PT_CFM) while the task is
  1123. * blocked in a system call, convert the state
  1124. * so that the non-system-call exit
  1125. * path is used. This ensures that the proper
  1126. * state will be picked up when resuming
  1127. * execution. However, it *also* means that
  1128. * once we write PT_AR_BSP/PT_CFM, it won't be
  1129. * possible to modify the syscall arguments of
  1130. * the pending system call any longer. This
  1131. * shouldn't be an issue because modifying
  1132. * PT_AR_BSP/PT_CFM generally implies that
  1133. * we're either abandoning the pending system
  1134. * call or that we defer it's re-execution
  1135. * (e.g., due to GDB doing an inferior
  1136. * function call).
  1137. */
  1138. urbs_end = ia64_get_user_rbs_end(target, pt, &cfm);
  1139. if (write_access) {
  1140. if (*data != urbs_end) {
  1141. if (in_syscall(pt))
  1142. convert_to_non_syscall(target,
  1143. pt,
  1144. cfm);
  1145. /*
  1146. * Simulate user-level write
  1147. * of ar.bsp:
  1148. */
  1149. pt->loadrs = 0;
  1150. pt->ar_bspstore = *data;
  1151. }
  1152. } else
  1153. *data = urbs_end;
  1154. return 0;
  1155. case ELF_AR_BSPSTORE_OFFSET:
  1156. ptr = &pt->ar_bspstore;
  1157. break;
  1158. case ELF_AR_RNAT_OFFSET:
  1159. ptr = &pt->ar_rnat;
  1160. break;
  1161. case ELF_AR_CCV_OFFSET:
  1162. ptr = &pt->ar_ccv;
  1163. break;
  1164. case ELF_AR_UNAT_OFFSET:
  1165. ptr = &pt->ar_unat;
  1166. break;
  1167. case ELF_AR_FPSR_OFFSET:
  1168. ptr = &pt->ar_fpsr;
  1169. break;
  1170. case ELF_AR_PFS_OFFSET:
  1171. ptr = &pt->ar_pfs;
  1172. break;
  1173. case ELF_AR_LC_OFFSET:
  1174. return unw_access_ar(info, UNW_AR_LC, data,
  1175. write_access);
  1176. case ELF_AR_EC_OFFSET:
  1177. return unw_access_ar(info, UNW_AR_EC, data,
  1178. write_access);
  1179. case ELF_AR_CSD_OFFSET:
  1180. ptr = &pt->ar_csd;
  1181. break;
  1182. case ELF_AR_SSD_OFFSET:
  1183. ptr = &pt->ar_ssd;
  1184. }
  1185. } else if (addr >= ELF_CR_IIP_OFFSET && addr <= ELF_CR_IPSR_OFFSET) {
  1186. switch (addr) {
  1187. case ELF_CR_IIP_OFFSET:
  1188. ptr = &pt->cr_iip;
  1189. break;
  1190. case ELF_CFM_OFFSET:
  1191. urbs_end = ia64_get_user_rbs_end(target, pt, &cfm);
  1192. if (write_access) {
  1193. if (((cfm ^ *data) & PFM_MASK) != 0) {
  1194. if (in_syscall(pt))
  1195. convert_to_non_syscall(target,
  1196. pt,
  1197. cfm);
  1198. pt->cr_ifs = ((pt->cr_ifs & ~PFM_MASK)
  1199. | (*data & PFM_MASK));
  1200. }
  1201. } else
  1202. *data = cfm;
  1203. return 0;
  1204. case ELF_CR_IPSR_OFFSET:
  1205. if (write_access) {
  1206. unsigned long tmp = *data;
  1207. /* psr.ri==3 is a reserved value: SDM 2:25 */
  1208. if ((tmp & IA64_PSR_RI) == IA64_PSR_RI)
  1209. tmp &= ~IA64_PSR_RI;
  1210. pt->cr_ipsr = ((tmp & IPSR_MASK)
  1211. | (pt->cr_ipsr & ~IPSR_MASK));
  1212. } else
  1213. *data = (pt->cr_ipsr & IPSR_MASK);
  1214. return 0;
  1215. }
  1216. } else if (addr == ELF_NAT_OFFSET)
  1217. return access_nat_bits(target, pt, info,
  1218. data, write_access);
  1219. else if (addr == ELF_PR_OFFSET)
  1220. ptr = &pt->pr;
  1221. else
  1222. return -1;
  1223. if (write_access)
  1224. *ptr = *data;
  1225. else
  1226. *data = *ptr;
  1227. return 0;
  1228. }
  1229. static int
  1230. access_elf_reg(struct task_struct *target, struct unw_frame_info *info,
  1231. unsigned long addr, unsigned long *data, int write_access)
  1232. {
  1233. if (addr >= ELF_GR_OFFSET(1) && addr <= ELF_GR_OFFSET(31))
  1234. return access_elf_gpreg(target, info, addr, data, write_access);
  1235. else if (addr >= ELF_BR_OFFSET(0) && addr <= ELF_BR_OFFSET(7))
  1236. return access_elf_breg(target, info, addr, data, write_access);
  1237. else
  1238. return access_elf_areg(target, info, addr, data, write_access);
  1239. }
  1240. struct regset_membuf {
  1241. struct membuf to;
  1242. int ret;
  1243. };
  1244. static void do_gpregs_get(struct unw_frame_info *info, void *arg)
  1245. {
  1246. struct regset_membuf *dst = arg;
  1247. struct membuf to = dst->to;
  1248. unsigned int n;
  1249. elf_greg_t reg;
  1250. if (unw_unwind_to_user(info) < 0)
  1251. return;
  1252. /*
  1253. * coredump format:
  1254. * r0-r31
  1255. * NaT bits (for r0-r31; bit N == 1 iff rN is a NaT)
  1256. * predicate registers (p0-p63)
  1257. * b0-b7
  1258. * ip cfm user-mask
  1259. * ar.rsc ar.bsp ar.bspstore ar.rnat
  1260. * ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec
  1261. */
  1262. /* Skip r0 */
  1263. membuf_zero(&to, 8);
  1264. for (n = 8; to.left && n < ELF_AR_END_OFFSET; n += 8) {
  1265. if (access_elf_reg(info->task, info, n, &reg, 0) < 0) {
  1266. dst->ret = -EIO;
  1267. return;
  1268. }
  1269. membuf_store(&to, reg);
  1270. }
  1271. }
  1272. static void do_gpregs_set(struct unw_frame_info *info, void *arg)
  1273. {
  1274. struct regset_getset *dst = arg;
  1275. if (unw_unwind_to_user(info) < 0)
  1276. return;
  1277. if (!dst->count)
  1278. return;
  1279. /* Skip r0 */
  1280. if (dst->pos < ELF_GR_OFFSET(1)) {
  1281. dst->ret = user_regset_copyin_ignore(&dst->pos, &dst->count,
  1282. &dst->u.set.kbuf,
  1283. &dst->u.set.ubuf,
  1284. 0, ELF_GR_OFFSET(1));
  1285. if (dst->ret)
  1286. return;
  1287. }
  1288. while (dst->count && dst->pos < ELF_AR_END_OFFSET) {
  1289. unsigned int n, from, to;
  1290. elf_greg_t tmp[16];
  1291. from = dst->pos;
  1292. to = from + sizeof(tmp);
  1293. if (to > ELF_AR_END_OFFSET)
  1294. to = ELF_AR_END_OFFSET;
  1295. /* get up to 16 values */
  1296. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1297. &dst->u.set.kbuf, &dst->u.set.ubuf, tmp,
  1298. from, to);
  1299. if (dst->ret)
  1300. return;
  1301. /* now copy them into registers */
  1302. for (n = 0; from < dst->pos; from += sizeof(elf_greg_t), n++)
  1303. if (access_elf_reg(dst->target, info, from,
  1304. &tmp[n], 1) < 0) {
  1305. dst->ret = -EIO;
  1306. return;
  1307. }
  1308. }
  1309. }
  1310. #define ELF_FP_OFFSET(i) (i * sizeof(elf_fpreg_t))
  1311. static void do_fpregs_get(struct unw_frame_info *info, void *arg)
  1312. {
  1313. struct task_struct *task = info->task;
  1314. struct regset_membuf *dst = arg;
  1315. struct membuf to = dst->to;
  1316. elf_fpreg_t reg;
  1317. unsigned int n;
  1318. if (unw_unwind_to_user(info) < 0)
  1319. return;
  1320. /* Skip pos 0 and 1 */
  1321. membuf_zero(&to, 2 * sizeof(elf_fpreg_t));
  1322. /* fr2-fr31 */
  1323. for (n = 2; to.left && n < 32; n++) {
  1324. if (unw_get_fr(info, n, &reg)) {
  1325. dst->ret = -EIO;
  1326. return;
  1327. }
  1328. membuf_write(&to, &reg, sizeof(reg));
  1329. }
  1330. /* fph */
  1331. if (!to.left)
  1332. return;
  1333. ia64_flush_fph(task);
  1334. if (task->thread.flags & IA64_THREAD_FPH_VALID)
  1335. membuf_write(&to, &task->thread.fph, 96 * sizeof(reg));
  1336. else
  1337. membuf_zero(&to, 96 * sizeof(reg));
  1338. }
  1339. static void do_fpregs_set(struct unw_frame_info *info, void *arg)
  1340. {
  1341. struct regset_getset *dst = arg;
  1342. elf_fpreg_t fpreg, tmp[30];
  1343. int index, start, end;
  1344. if (unw_unwind_to_user(info) < 0)
  1345. return;
  1346. /* Skip pos 0 and 1 */
  1347. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(2)) {
  1348. dst->ret = user_regset_copyin_ignore(&dst->pos, &dst->count,
  1349. &dst->u.set.kbuf,
  1350. &dst->u.set.ubuf,
  1351. 0, ELF_FP_OFFSET(2));
  1352. if (dst->count == 0 || dst->ret)
  1353. return;
  1354. }
  1355. /* fr2-fr31 */
  1356. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(32)) {
  1357. start = dst->pos;
  1358. end = min(((unsigned int)ELF_FP_OFFSET(32)),
  1359. dst->pos + dst->count);
  1360. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1361. &dst->u.set.kbuf, &dst->u.set.ubuf, tmp,
  1362. ELF_FP_OFFSET(2), ELF_FP_OFFSET(32));
  1363. if (dst->ret)
  1364. return;
  1365. if (start & 0xF) { /* only write high part */
  1366. if (unw_get_fr(info, start / sizeof(elf_fpreg_t),
  1367. &fpreg)) {
  1368. dst->ret = -EIO;
  1369. return;
  1370. }
  1371. tmp[start / sizeof(elf_fpreg_t) - 2].u.bits[0]
  1372. = fpreg.u.bits[0];
  1373. start &= ~0xFUL;
  1374. }
  1375. if (end & 0xF) { /* only write low part */
  1376. if (unw_get_fr(info, end / sizeof(elf_fpreg_t),
  1377. &fpreg)) {
  1378. dst->ret = -EIO;
  1379. return;
  1380. }
  1381. tmp[end / sizeof(elf_fpreg_t) - 2].u.bits[1]
  1382. = fpreg.u.bits[1];
  1383. end = (end + 0xF) & ~0xFUL;
  1384. }
  1385. for ( ; start < end ; start += sizeof(elf_fpreg_t)) {
  1386. index = start / sizeof(elf_fpreg_t);
  1387. if (unw_set_fr(info, index, tmp[index - 2])) {
  1388. dst->ret = -EIO;
  1389. return;
  1390. }
  1391. }
  1392. if (dst->ret || dst->count == 0)
  1393. return;
  1394. }
  1395. /* fph */
  1396. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(128)) {
  1397. ia64_sync_fph(dst->target);
  1398. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1399. &dst->u.set.kbuf,
  1400. &dst->u.set.ubuf,
  1401. &dst->target->thread.fph,
  1402. ELF_FP_OFFSET(32), -1);
  1403. }
  1404. }
  1405. static void
  1406. unwind_and_call(void (*call)(struct unw_frame_info *, void *),
  1407. struct task_struct *target, void *data)
  1408. {
  1409. if (target == current)
  1410. unw_init_running(call, data);
  1411. else {
  1412. struct unw_frame_info info;
  1413. memset(&info, 0, sizeof(info));
  1414. unw_init_from_blocked_task(&info, target);
  1415. (*call)(&info, data);
  1416. }
  1417. }
  1418. static int
  1419. do_regset_call(void (*call)(struct unw_frame_info *, void *),
  1420. struct task_struct *target,
  1421. const struct user_regset *regset,
  1422. unsigned int pos, unsigned int count,
  1423. const void *kbuf, const void __user *ubuf)
  1424. {
  1425. struct regset_getset info = { .target = target, .regset = regset,
  1426. .pos = pos, .count = count,
  1427. .u.set = { .kbuf = kbuf, .ubuf = ubuf },
  1428. .ret = 0 };
  1429. unwind_and_call(call, target, &info);
  1430. return info.ret;
  1431. }
  1432. static int
  1433. gpregs_get(struct task_struct *target,
  1434. const struct user_regset *regset,
  1435. struct membuf to)
  1436. {
  1437. struct regset_membuf info = {.to = to};
  1438. unwind_and_call(do_gpregs_get, target, &info);
  1439. return info.ret;
  1440. }
  1441. static int gpregs_set(struct task_struct *target,
  1442. const struct user_regset *regset,
  1443. unsigned int pos, unsigned int count,
  1444. const void *kbuf, const void __user *ubuf)
  1445. {
  1446. return do_regset_call(do_gpregs_set, target, regset, pos, count,
  1447. kbuf, ubuf);
  1448. }
  1449. static void do_gpregs_writeback(struct unw_frame_info *info, void *arg)
  1450. {
  1451. do_sync_rbs(info, ia64_sync_user_rbs);
  1452. }
  1453. /*
  1454. * This is called to write back the register backing store.
  1455. * ptrace does this before it stops, so that a tracer reading the user
  1456. * memory after the thread stops will get the current register data.
  1457. */
  1458. static int
  1459. gpregs_writeback(struct task_struct *target,
  1460. const struct user_regset *regset,
  1461. int now)
  1462. {
  1463. if (test_and_set_tsk_thread_flag(target, TIF_RESTORE_RSE))
  1464. return 0;
  1465. set_notify_resume(target);
  1466. return do_regset_call(do_gpregs_writeback, target, regset, 0, 0,
  1467. NULL, NULL);
  1468. }
  1469. static int
  1470. fpregs_active(struct task_struct *target, const struct user_regset *regset)
  1471. {
  1472. return (target->thread.flags & IA64_THREAD_FPH_VALID) ? 128 : 32;
  1473. }
  1474. static int fpregs_get(struct task_struct *target,
  1475. const struct user_regset *regset,
  1476. struct membuf to)
  1477. {
  1478. struct regset_membuf info = {.to = to};
  1479. unwind_and_call(do_fpregs_get, target, &info);
  1480. return info.ret;
  1481. }
  1482. static int fpregs_set(struct task_struct *target,
  1483. const struct user_regset *regset,
  1484. unsigned int pos, unsigned int count,
  1485. const void *kbuf, const void __user *ubuf)
  1486. {
  1487. return do_regset_call(do_fpregs_set, target, regset, pos, count,
  1488. kbuf, ubuf);
  1489. }
  1490. static int
  1491. access_uarea(struct task_struct *child, unsigned long addr,
  1492. unsigned long *data, int write_access)
  1493. {
  1494. unsigned int pos = -1; /* an invalid value */
  1495. unsigned long *ptr, regnum;
  1496. if ((addr & 0x7) != 0) {
  1497. dprintk("ptrace: unaligned register address 0x%lx\n", addr);
  1498. return -1;
  1499. }
  1500. if ((addr >= PT_NAT_BITS + 8 && addr < PT_F2) ||
  1501. (addr >= PT_R7 + 8 && addr < PT_B1) ||
  1502. (addr >= PT_AR_LC + 8 && addr < PT_CR_IPSR) ||
  1503. (addr >= PT_AR_SSD + 8 && addr < PT_DBR)) {
  1504. dprintk("ptrace: rejecting access to register "
  1505. "address 0x%lx\n", addr);
  1506. return -1;
  1507. }
  1508. switch (addr) {
  1509. case PT_F32 ... (PT_F127 + 15):
  1510. pos = addr - PT_F32 + ELF_FP_OFFSET(32);
  1511. break;
  1512. case PT_F2 ... (PT_F5 + 15):
  1513. pos = addr - PT_F2 + ELF_FP_OFFSET(2);
  1514. break;
  1515. case PT_F10 ... (PT_F31 + 15):
  1516. pos = addr - PT_F10 + ELF_FP_OFFSET(10);
  1517. break;
  1518. case PT_F6 ... (PT_F9 + 15):
  1519. pos = addr - PT_F6 + ELF_FP_OFFSET(6);
  1520. break;
  1521. }
  1522. if (pos != -1) {
  1523. unsigned reg = pos / sizeof(elf_fpreg_t);
  1524. int which_half = (pos / sizeof(unsigned long)) & 1;
  1525. if (reg < 32) { /* fr2-fr31 */
  1526. struct unw_frame_info info;
  1527. elf_fpreg_t fpreg;
  1528. memset(&info, 0, sizeof(info));
  1529. unw_init_from_blocked_task(&info, child);
  1530. if (unw_unwind_to_user(&info) < 0)
  1531. return 0;
  1532. if (unw_get_fr(&info, reg, &fpreg))
  1533. return -1;
  1534. if (write_access) {
  1535. fpreg.u.bits[which_half] = *data;
  1536. if (unw_set_fr(&info, reg, fpreg))
  1537. return -1;
  1538. } else {
  1539. *data = fpreg.u.bits[which_half];
  1540. }
  1541. } else { /* fph */
  1542. elf_fpreg_t *p = &child->thread.fph[reg - 32];
  1543. unsigned long *bits = &p->u.bits[which_half];
  1544. ia64_sync_fph(child);
  1545. if (write_access)
  1546. *bits = *data;
  1547. else if (child->thread.flags & IA64_THREAD_FPH_VALID)
  1548. *data = *bits;
  1549. else
  1550. *data = 0;
  1551. }
  1552. return 0;
  1553. }
  1554. switch (addr) {
  1555. case PT_NAT_BITS:
  1556. pos = ELF_NAT_OFFSET;
  1557. break;
  1558. case PT_R4 ... PT_R7:
  1559. pos = addr - PT_R4 + ELF_GR_OFFSET(4);
  1560. break;
  1561. case PT_B1 ... PT_B5:
  1562. pos = addr - PT_B1 + ELF_BR_OFFSET(1);
  1563. break;
  1564. case PT_AR_EC:
  1565. pos = ELF_AR_EC_OFFSET;
  1566. break;
  1567. case PT_AR_LC:
  1568. pos = ELF_AR_LC_OFFSET;
  1569. break;
  1570. case PT_CR_IPSR:
  1571. pos = ELF_CR_IPSR_OFFSET;
  1572. break;
  1573. case PT_CR_IIP:
  1574. pos = ELF_CR_IIP_OFFSET;
  1575. break;
  1576. case PT_CFM:
  1577. pos = ELF_CFM_OFFSET;
  1578. break;
  1579. case PT_AR_UNAT:
  1580. pos = ELF_AR_UNAT_OFFSET;
  1581. break;
  1582. case PT_AR_PFS:
  1583. pos = ELF_AR_PFS_OFFSET;
  1584. break;
  1585. case PT_AR_RSC:
  1586. pos = ELF_AR_RSC_OFFSET;
  1587. break;
  1588. case PT_AR_RNAT:
  1589. pos = ELF_AR_RNAT_OFFSET;
  1590. break;
  1591. case PT_AR_BSPSTORE:
  1592. pos = ELF_AR_BSPSTORE_OFFSET;
  1593. break;
  1594. case PT_PR:
  1595. pos = ELF_PR_OFFSET;
  1596. break;
  1597. case PT_B6:
  1598. pos = ELF_BR_OFFSET(6);
  1599. break;
  1600. case PT_AR_BSP:
  1601. pos = ELF_AR_BSP_OFFSET;
  1602. break;
  1603. case PT_R1 ... PT_R3:
  1604. pos = addr - PT_R1 + ELF_GR_OFFSET(1);
  1605. break;
  1606. case PT_R12 ... PT_R15:
  1607. pos = addr - PT_R12 + ELF_GR_OFFSET(12);
  1608. break;
  1609. case PT_R8 ... PT_R11:
  1610. pos = addr - PT_R8 + ELF_GR_OFFSET(8);
  1611. break;
  1612. case PT_R16 ... PT_R31:
  1613. pos = addr - PT_R16 + ELF_GR_OFFSET(16);
  1614. break;
  1615. case PT_AR_CCV:
  1616. pos = ELF_AR_CCV_OFFSET;
  1617. break;
  1618. case PT_AR_FPSR:
  1619. pos = ELF_AR_FPSR_OFFSET;
  1620. break;
  1621. case PT_B0:
  1622. pos = ELF_BR_OFFSET(0);
  1623. break;
  1624. case PT_B7:
  1625. pos = ELF_BR_OFFSET(7);
  1626. break;
  1627. case PT_AR_CSD:
  1628. pos = ELF_AR_CSD_OFFSET;
  1629. break;
  1630. case PT_AR_SSD:
  1631. pos = ELF_AR_SSD_OFFSET;
  1632. break;
  1633. }
  1634. if (pos != -1) {
  1635. struct unw_frame_info info;
  1636. memset(&info, 0, sizeof(info));
  1637. unw_init_from_blocked_task(&info, child);
  1638. if (unw_unwind_to_user(&info) < 0)
  1639. return 0;
  1640. return access_elf_reg(child, &info, pos, data, write_access);
  1641. }
  1642. /* access debug registers */
  1643. if (addr >= PT_IBR) {
  1644. regnum = (addr - PT_IBR) >> 3;
  1645. ptr = &child->thread.ibr[0];
  1646. } else {
  1647. regnum = (addr - PT_DBR) >> 3;
  1648. ptr = &child->thread.dbr[0];
  1649. }
  1650. if (regnum >= 8) {
  1651. dprintk("ptrace: rejecting access to register "
  1652. "address 0x%lx\n", addr);
  1653. return -1;
  1654. }
  1655. if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
  1656. child->thread.flags |= IA64_THREAD_DBG_VALID;
  1657. memset(child->thread.dbr, 0,
  1658. sizeof(child->thread.dbr));
  1659. memset(child->thread.ibr, 0,
  1660. sizeof(child->thread.ibr));
  1661. }
  1662. ptr += regnum;
  1663. if ((regnum & 1) && write_access) {
  1664. /* don't let the user set kernel-level breakpoints: */
  1665. *ptr = *data & ~(7UL << 56);
  1666. return 0;
  1667. }
  1668. if (write_access)
  1669. *ptr = *data;
  1670. else
  1671. *data = *ptr;
  1672. return 0;
  1673. }
  1674. static const struct user_regset native_regsets[] = {
  1675. {
  1676. .core_note_type = NT_PRSTATUS,
  1677. .n = ELF_NGREG,
  1678. .size = sizeof(elf_greg_t), .align = sizeof(elf_greg_t),
  1679. .regset_get = gpregs_get, .set = gpregs_set,
  1680. .writeback = gpregs_writeback
  1681. },
  1682. {
  1683. .core_note_type = NT_PRFPREG,
  1684. .n = ELF_NFPREG,
  1685. .size = sizeof(elf_fpreg_t), .align = sizeof(elf_fpreg_t),
  1686. .regset_get = fpregs_get, .set = fpregs_set, .active = fpregs_active
  1687. },
  1688. };
  1689. static const struct user_regset_view user_ia64_view = {
  1690. .name = "ia64",
  1691. .e_machine = EM_IA_64,
  1692. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  1693. };
  1694. const struct user_regset_view *task_user_regset_view(struct task_struct *tsk)
  1695. {
  1696. return &user_ia64_view;
  1697. }
  1698. struct syscall_get_args {
  1699. unsigned int i;
  1700. unsigned int n;
  1701. unsigned long *args;
  1702. struct pt_regs *regs;
  1703. };
  1704. static void syscall_get_args_cb(struct unw_frame_info *info, void *data)
  1705. {
  1706. struct syscall_get_args *args = data;
  1707. struct pt_regs *pt = args->regs;
  1708. unsigned long *krbs, cfm, ndirty, nlocals, nouts;
  1709. int i, count;
  1710. if (unw_unwind_to_user(info) < 0)
  1711. return;
  1712. /*
  1713. * We get here via a few paths:
  1714. * - break instruction: cfm is shared with caller.
  1715. * syscall args are in out= regs, locals are non-empty.
  1716. * - epsinstruction: cfm is set by br.call
  1717. * locals don't exist.
  1718. *
  1719. * For both cases arguments are reachable in cfm.sof - cfm.sol.
  1720. * CFM: [ ... | sor: 17..14 | sol : 13..7 | sof : 6..0 ]
  1721. */
  1722. cfm = pt->cr_ifs;
  1723. nlocals = (cfm >> 7) & 0x7f; /* aka sol */
  1724. nouts = (cfm & 0x7f) - nlocals; /* aka sof - sol */
  1725. krbs = (unsigned long *)info->task + IA64_RBS_OFFSET/8;
  1726. ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
  1727. count = 0;
  1728. if (in_syscall(pt))
  1729. count = min_t(int, args->n, nouts);
  1730. /* Iterate over outs. */
  1731. for (i = 0; i < count; i++) {
  1732. int j = ndirty + nlocals + i + args->i;
  1733. args->args[i] = *ia64_rse_skip_regs(krbs, j);
  1734. }
  1735. while (i < args->n) {
  1736. args->args[i] = 0;
  1737. i++;
  1738. }
  1739. }
  1740. void syscall_get_arguments(struct task_struct *task,
  1741. struct pt_regs *regs, unsigned long *args)
  1742. {
  1743. struct syscall_get_args data = {
  1744. .i = 0,
  1745. .n = 6,
  1746. .args = args,
  1747. .regs = regs,
  1748. };
  1749. if (task == current)
  1750. unw_init_running(syscall_get_args_cb, &data);
  1751. else {
  1752. struct unw_frame_info ufi;
  1753. memset(&ufi, 0, sizeof(ufi));
  1754. unw_init_from_blocked_task(&ufi, task);
  1755. syscall_get_args_cb(&ufi, &data);
  1756. }
  1757. }