ptrace.c 57 KB

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