ptrace.c 58 KB

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