ptrace.c 43 KB

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