ptrace.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  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, long addr, 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. if (unlikely(current->audit_context)) {
  1065. long syscall;
  1066. int arch;
  1067. syscall = regs.r15;
  1068. arch = AUDIT_ARCH_IA64;
  1069. audit_syscall_entry(arch, syscall, arg0, arg1, arg2, arg3);
  1070. }
  1071. return 0;
  1072. }
  1073. /* "asmlinkage" so the input arguments are preserved... */
  1074. asmlinkage void
  1075. syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
  1076. long arg4, long arg5, long arg6, long arg7,
  1077. struct pt_regs regs)
  1078. {
  1079. int step;
  1080. if (unlikely(current->audit_context)) {
  1081. int success = AUDITSC_RESULT(regs.r10);
  1082. long result = regs.r8;
  1083. if (success != AUDITSC_SUCCESS)
  1084. result = -result;
  1085. audit_syscall_exit(success, result);
  1086. }
  1087. step = test_thread_flag(TIF_SINGLESTEP);
  1088. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1089. tracehook_report_syscall_exit(&regs, step);
  1090. /* copy user rbs to kernel rbs */
  1091. if (test_thread_flag(TIF_RESTORE_RSE))
  1092. ia64_sync_krbs();
  1093. }
  1094. /* Utrace implementation starts here */
  1095. struct regset_get {
  1096. void *kbuf;
  1097. void __user *ubuf;
  1098. };
  1099. struct regset_set {
  1100. const void *kbuf;
  1101. const void __user *ubuf;
  1102. };
  1103. struct regset_getset {
  1104. struct task_struct *target;
  1105. const struct user_regset *regset;
  1106. union {
  1107. struct regset_get get;
  1108. struct regset_set set;
  1109. } u;
  1110. unsigned int pos;
  1111. unsigned int count;
  1112. int ret;
  1113. };
  1114. static int
  1115. access_elf_gpreg(struct task_struct *target, struct unw_frame_info *info,
  1116. unsigned long addr, unsigned long *data, int write_access)
  1117. {
  1118. struct pt_regs *pt;
  1119. unsigned long *ptr = NULL;
  1120. int ret;
  1121. char nat = 0;
  1122. pt = task_pt_regs(target);
  1123. switch (addr) {
  1124. case ELF_GR_OFFSET(1):
  1125. ptr = &pt->r1;
  1126. break;
  1127. case ELF_GR_OFFSET(2):
  1128. case ELF_GR_OFFSET(3):
  1129. ptr = (void *)&pt->r2 + (addr - ELF_GR_OFFSET(2));
  1130. break;
  1131. case ELF_GR_OFFSET(4) ... ELF_GR_OFFSET(7):
  1132. if (write_access) {
  1133. /* read NaT bit first: */
  1134. unsigned long dummy;
  1135. ret = unw_get_gr(info, addr/8, &dummy, &nat);
  1136. if (ret < 0)
  1137. return ret;
  1138. }
  1139. return unw_access_gr(info, addr/8, data, &nat, write_access);
  1140. case ELF_GR_OFFSET(8) ... ELF_GR_OFFSET(11):
  1141. ptr = (void *)&pt->r8 + addr - ELF_GR_OFFSET(8);
  1142. break;
  1143. case ELF_GR_OFFSET(12):
  1144. case ELF_GR_OFFSET(13):
  1145. ptr = (void *)&pt->r12 + addr - ELF_GR_OFFSET(12);
  1146. break;
  1147. case ELF_GR_OFFSET(14):
  1148. ptr = &pt->r14;
  1149. break;
  1150. case ELF_GR_OFFSET(15):
  1151. ptr = &pt->r15;
  1152. }
  1153. if (write_access)
  1154. *ptr = *data;
  1155. else
  1156. *data = *ptr;
  1157. return 0;
  1158. }
  1159. static int
  1160. access_elf_breg(struct task_struct *target, struct unw_frame_info *info,
  1161. unsigned long addr, unsigned long *data, int write_access)
  1162. {
  1163. struct pt_regs *pt;
  1164. unsigned long *ptr = NULL;
  1165. pt = task_pt_regs(target);
  1166. switch (addr) {
  1167. case ELF_BR_OFFSET(0):
  1168. ptr = &pt->b0;
  1169. break;
  1170. case ELF_BR_OFFSET(1) ... ELF_BR_OFFSET(5):
  1171. return unw_access_br(info, (addr - ELF_BR_OFFSET(0))/8,
  1172. data, write_access);
  1173. case ELF_BR_OFFSET(6):
  1174. ptr = &pt->b6;
  1175. break;
  1176. case ELF_BR_OFFSET(7):
  1177. ptr = &pt->b7;
  1178. }
  1179. if (write_access)
  1180. *ptr = *data;
  1181. else
  1182. *data = *ptr;
  1183. return 0;
  1184. }
  1185. static int
  1186. access_elf_areg(struct task_struct *target, struct unw_frame_info *info,
  1187. unsigned long addr, unsigned long *data, int write_access)
  1188. {
  1189. struct pt_regs *pt;
  1190. unsigned long cfm, urbs_end;
  1191. unsigned long *ptr = NULL;
  1192. pt = task_pt_regs(target);
  1193. if (addr >= ELF_AR_RSC_OFFSET && addr <= ELF_AR_SSD_OFFSET) {
  1194. switch (addr) {
  1195. case ELF_AR_RSC_OFFSET:
  1196. /* force PL3 */
  1197. if (write_access)
  1198. pt->ar_rsc = *data | (3 << 2);
  1199. else
  1200. *data = pt->ar_rsc;
  1201. return 0;
  1202. case ELF_AR_BSP_OFFSET:
  1203. /*
  1204. * By convention, we use PT_AR_BSP to refer to
  1205. * the end of the user-level backing store.
  1206. * Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof)
  1207. * to get the real value of ar.bsp at the time
  1208. * the kernel was entered.
  1209. *
  1210. * Furthermore, when changing the contents of
  1211. * PT_AR_BSP (or PT_CFM) while the task is
  1212. * blocked in a system call, convert the state
  1213. * so that the non-system-call exit
  1214. * path is used. This ensures that the proper
  1215. * state will be picked up when resuming
  1216. * execution. However, it *also* means that
  1217. * once we write PT_AR_BSP/PT_CFM, it won't be
  1218. * possible to modify the syscall arguments of
  1219. * the pending system call any longer. This
  1220. * shouldn't be an issue because modifying
  1221. * PT_AR_BSP/PT_CFM generally implies that
  1222. * we're either abandoning the pending system
  1223. * call or that we defer it's re-execution
  1224. * (e.g., due to GDB doing an inferior
  1225. * function call).
  1226. */
  1227. urbs_end = ia64_get_user_rbs_end(target, pt, &cfm);
  1228. if (write_access) {
  1229. if (*data != urbs_end) {
  1230. if (in_syscall(pt))
  1231. convert_to_non_syscall(target,
  1232. pt,
  1233. cfm);
  1234. /*
  1235. * Simulate user-level write
  1236. * of ar.bsp:
  1237. */
  1238. pt->loadrs = 0;
  1239. pt->ar_bspstore = *data;
  1240. }
  1241. } else
  1242. *data = urbs_end;
  1243. return 0;
  1244. case ELF_AR_BSPSTORE_OFFSET:
  1245. ptr = &pt->ar_bspstore;
  1246. break;
  1247. case ELF_AR_RNAT_OFFSET:
  1248. ptr = &pt->ar_rnat;
  1249. break;
  1250. case ELF_AR_CCV_OFFSET:
  1251. ptr = &pt->ar_ccv;
  1252. break;
  1253. case ELF_AR_UNAT_OFFSET:
  1254. ptr = &pt->ar_unat;
  1255. break;
  1256. case ELF_AR_FPSR_OFFSET:
  1257. ptr = &pt->ar_fpsr;
  1258. break;
  1259. case ELF_AR_PFS_OFFSET:
  1260. ptr = &pt->ar_pfs;
  1261. break;
  1262. case ELF_AR_LC_OFFSET:
  1263. return unw_access_ar(info, UNW_AR_LC, data,
  1264. write_access);
  1265. case ELF_AR_EC_OFFSET:
  1266. return unw_access_ar(info, UNW_AR_EC, data,
  1267. write_access);
  1268. case ELF_AR_CSD_OFFSET:
  1269. ptr = &pt->ar_csd;
  1270. break;
  1271. case ELF_AR_SSD_OFFSET:
  1272. ptr = &pt->ar_ssd;
  1273. }
  1274. } else if (addr >= ELF_CR_IIP_OFFSET && addr <= ELF_CR_IPSR_OFFSET) {
  1275. switch (addr) {
  1276. case ELF_CR_IIP_OFFSET:
  1277. ptr = &pt->cr_iip;
  1278. break;
  1279. case ELF_CFM_OFFSET:
  1280. urbs_end = ia64_get_user_rbs_end(target, pt, &cfm);
  1281. if (write_access) {
  1282. if (((cfm ^ *data) & PFM_MASK) != 0) {
  1283. if (in_syscall(pt))
  1284. convert_to_non_syscall(target,
  1285. pt,
  1286. cfm);
  1287. pt->cr_ifs = ((pt->cr_ifs & ~PFM_MASK)
  1288. | (*data & PFM_MASK));
  1289. }
  1290. } else
  1291. *data = cfm;
  1292. return 0;
  1293. case ELF_CR_IPSR_OFFSET:
  1294. if (write_access) {
  1295. unsigned long tmp = *data;
  1296. /* psr.ri==3 is a reserved value: SDM 2:25 */
  1297. if ((tmp & IA64_PSR_RI) == IA64_PSR_RI)
  1298. tmp &= ~IA64_PSR_RI;
  1299. pt->cr_ipsr = ((tmp & IPSR_MASK)
  1300. | (pt->cr_ipsr & ~IPSR_MASK));
  1301. } else
  1302. *data = (pt->cr_ipsr & IPSR_MASK);
  1303. return 0;
  1304. }
  1305. } else if (addr == ELF_NAT_OFFSET)
  1306. return access_nat_bits(target, pt, info,
  1307. data, write_access);
  1308. else if (addr == ELF_PR_OFFSET)
  1309. ptr = &pt->pr;
  1310. else
  1311. return -1;
  1312. if (write_access)
  1313. *ptr = *data;
  1314. else
  1315. *data = *ptr;
  1316. return 0;
  1317. }
  1318. static int
  1319. access_elf_reg(struct task_struct *target, struct unw_frame_info *info,
  1320. unsigned long addr, unsigned long *data, int write_access)
  1321. {
  1322. if (addr >= ELF_GR_OFFSET(1) && addr <= ELF_GR_OFFSET(15))
  1323. return access_elf_gpreg(target, info, addr, data, write_access);
  1324. else if (addr >= ELF_BR_OFFSET(0) && addr <= ELF_BR_OFFSET(7))
  1325. return access_elf_breg(target, info, addr, data, write_access);
  1326. else
  1327. return access_elf_areg(target, info, addr, data, write_access);
  1328. }
  1329. void do_gpregs_get(struct unw_frame_info *info, void *arg)
  1330. {
  1331. struct pt_regs *pt;
  1332. struct regset_getset *dst = arg;
  1333. elf_greg_t tmp[16];
  1334. unsigned int i, index, min_copy;
  1335. if (unw_unwind_to_user(info) < 0)
  1336. return;
  1337. /*
  1338. * coredump format:
  1339. * r0-r31
  1340. * NaT bits (for r0-r31; bit N == 1 iff rN is a NaT)
  1341. * predicate registers (p0-p63)
  1342. * b0-b7
  1343. * ip cfm user-mask
  1344. * ar.rsc ar.bsp ar.bspstore ar.rnat
  1345. * ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec
  1346. */
  1347. /* Skip r0 */
  1348. if (dst->count > 0 && dst->pos < ELF_GR_OFFSET(1)) {
  1349. dst->ret = user_regset_copyout_zero(&dst->pos, &dst->count,
  1350. &dst->u.get.kbuf,
  1351. &dst->u.get.ubuf,
  1352. 0, ELF_GR_OFFSET(1));
  1353. if (dst->ret || dst->count == 0)
  1354. return;
  1355. }
  1356. /* gr1 - gr15 */
  1357. if (dst->count > 0 && dst->pos < ELF_GR_OFFSET(16)) {
  1358. index = (dst->pos - ELF_GR_OFFSET(1)) / sizeof(elf_greg_t);
  1359. min_copy = ELF_GR_OFFSET(16) > (dst->pos + dst->count) ?
  1360. (dst->pos + dst->count) : ELF_GR_OFFSET(16);
  1361. for (i = dst->pos; i < min_copy; i += sizeof(elf_greg_t),
  1362. index++)
  1363. if (access_elf_reg(dst->target, info, i,
  1364. &tmp[index], 0) < 0) {
  1365. dst->ret = -EIO;
  1366. return;
  1367. }
  1368. dst->ret = user_regset_copyout(&dst->pos, &dst->count,
  1369. &dst->u.get.kbuf, &dst->u.get.ubuf, tmp,
  1370. ELF_GR_OFFSET(1), ELF_GR_OFFSET(16));
  1371. if (dst->ret || dst->count == 0)
  1372. return;
  1373. }
  1374. /* r16-r31 */
  1375. if (dst->count > 0 && dst->pos < ELF_NAT_OFFSET) {
  1376. pt = task_pt_regs(dst->target);
  1377. dst->ret = user_regset_copyout(&dst->pos, &dst->count,
  1378. &dst->u.get.kbuf, &dst->u.get.ubuf, &pt->r16,
  1379. ELF_GR_OFFSET(16), ELF_NAT_OFFSET);
  1380. if (dst->ret || dst->count == 0)
  1381. return;
  1382. }
  1383. /* nat, pr, b0 - b7 */
  1384. if (dst->count > 0 && dst->pos < ELF_CR_IIP_OFFSET) {
  1385. index = (dst->pos - ELF_NAT_OFFSET) / sizeof(elf_greg_t);
  1386. min_copy = ELF_CR_IIP_OFFSET > (dst->pos + dst->count) ?
  1387. (dst->pos + dst->count) : ELF_CR_IIP_OFFSET;
  1388. for (i = dst->pos; i < min_copy; i += sizeof(elf_greg_t),
  1389. index++)
  1390. if (access_elf_reg(dst->target, info, i,
  1391. &tmp[index], 0) < 0) {
  1392. dst->ret = -EIO;
  1393. return;
  1394. }
  1395. dst->ret = user_regset_copyout(&dst->pos, &dst->count,
  1396. &dst->u.get.kbuf, &dst->u.get.ubuf, tmp,
  1397. ELF_NAT_OFFSET, ELF_CR_IIP_OFFSET);
  1398. if (dst->ret || dst->count == 0)
  1399. return;
  1400. }
  1401. /* ip cfm psr ar.rsc ar.bsp ar.bspstore ar.rnat
  1402. * ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec ar.csd ar.ssd
  1403. */
  1404. if (dst->count > 0 && dst->pos < (ELF_AR_END_OFFSET)) {
  1405. index = (dst->pos - ELF_CR_IIP_OFFSET) / sizeof(elf_greg_t);
  1406. min_copy = ELF_AR_END_OFFSET > (dst->pos + dst->count) ?
  1407. (dst->pos + dst->count) : ELF_AR_END_OFFSET;
  1408. for (i = dst->pos; i < min_copy; i += sizeof(elf_greg_t),
  1409. index++)
  1410. if (access_elf_reg(dst->target, info, i,
  1411. &tmp[index], 0) < 0) {
  1412. dst->ret = -EIO;
  1413. return;
  1414. }
  1415. dst->ret = user_regset_copyout(&dst->pos, &dst->count,
  1416. &dst->u.get.kbuf, &dst->u.get.ubuf, tmp,
  1417. ELF_CR_IIP_OFFSET, ELF_AR_END_OFFSET);
  1418. }
  1419. }
  1420. void do_gpregs_set(struct unw_frame_info *info, void *arg)
  1421. {
  1422. struct pt_regs *pt;
  1423. struct regset_getset *dst = arg;
  1424. elf_greg_t tmp[16];
  1425. unsigned int i, index;
  1426. if (unw_unwind_to_user(info) < 0)
  1427. return;
  1428. /* Skip r0 */
  1429. if (dst->count > 0 && dst->pos < ELF_GR_OFFSET(1)) {
  1430. dst->ret = user_regset_copyin_ignore(&dst->pos, &dst->count,
  1431. &dst->u.set.kbuf,
  1432. &dst->u.set.ubuf,
  1433. 0, ELF_GR_OFFSET(1));
  1434. if (dst->ret || dst->count == 0)
  1435. return;
  1436. }
  1437. /* gr1-gr15 */
  1438. if (dst->count > 0 && dst->pos < ELF_GR_OFFSET(16)) {
  1439. i = dst->pos;
  1440. index = (dst->pos - ELF_GR_OFFSET(1)) / sizeof(elf_greg_t);
  1441. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1442. &dst->u.set.kbuf, &dst->u.set.ubuf, tmp,
  1443. ELF_GR_OFFSET(1), ELF_GR_OFFSET(16));
  1444. if (dst->ret)
  1445. return;
  1446. for ( ; i < dst->pos; i += sizeof(elf_greg_t), index++)
  1447. if (access_elf_reg(dst->target, info, i,
  1448. &tmp[index], 1) < 0) {
  1449. dst->ret = -EIO;
  1450. return;
  1451. }
  1452. if (dst->count == 0)
  1453. return;
  1454. }
  1455. /* gr16-gr31 */
  1456. if (dst->count > 0 && dst->pos < ELF_NAT_OFFSET) {
  1457. pt = task_pt_regs(dst->target);
  1458. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1459. &dst->u.set.kbuf, &dst->u.set.ubuf, &pt->r16,
  1460. ELF_GR_OFFSET(16), ELF_NAT_OFFSET);
  1461. if (dst->ret || dst->count == 0)
  1462. return;
  1463. }
  1464. /* nat, pr, b0 - b7 */
  1465. if (dst->count > 0 && dst->pos < ELF_CR_IIP_OFFSET) {
  1466. i = dst->pos;
  1467. index = (dst->pos - ELF_NAT_OFFSET) / sizeof(elf_greg_t);
  1468. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1469. &dst->u.set.kbuf, &dst->u.set.ubuf, tmp,
  1470. ELF_NAT_OFFSET, ELF_CR_IIP_OFFSET);
  1471. if (dst->ret)
  1472. return;
  1473. for (; i < dst->pos; i += sizeof(elf_greg_t), index++)
  1474. if (access_elf_reg(dst->target, info, i,
  1475. &tmp[index], 1) < 0) {
  1476. dst->ret = -EIO;
  1477. return;
  1478. }
  1479. if (dst->count == 0)
  1480. return;
  1481. }
  1482. /* ip cfm psr ar.rsc ar.bsp ar.bspstore ar.rnat
  1483. * ar.ccv ar.unat ar.fpsr ar.pfs ar.lc ar.ec ar.csd ar.ssd
  1484. */
  1485. if (dst->count > 0 && dst->pos < (ELF_AR_END_OFFSET)) {
  1486. i = dst->pos;
  1487. index = (dst->pos - ELF_CR_IIP_OFFSET) / sizeof(elf_greg_t);
  1488. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1489. &dst->u.set.kbuf, &dst->u.set.ubuf, tmp,
  1490. ELF_CR_IIP_OFFSET, ELF_AR_END_OFFSET);
  1491. if (dst->ret)
  1492. return;
  1493. for ( ; i < dst->pos; i += sizeof(elf_greg_t), index++)
  1494. if (access_elf_reg(dst->target, info, i,
  1495. &tmp[index], 1) < 0) {
  1496. dst->ret = -EIO;
  1497. return;
  1498. }
  1499. }
  1500. }
  1501. #define ELF_FP_OFFSET(i) (i * sizeof(elf_fpreg_t))
  1502. void do_fpregs_get(struct unw_frame_info *info, void *arg)
  1503. {
  1504. struct regset_getset *dst = arg;
  1505. struct task_struct *task = dst->target;
  1506. elf_fpreg_t tmp[30];
  1507. int index, min_copy, i;
  1508. if (unw_unwind_to_user(info) < 0)
  1509. return;
  1510. /* Skip pos 0 and 1 */
  1511. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(2)) {
  1512. dst->ret = user_regset_copyout_zero(&dst->pos, &dst->count,
  1513. &dst->u.get.kbuf,
  1514. &dst->u.get.ubuf,
  1515. 0, ELF_FP_OFFSET(2));
  1516. if (dst->count == 0 || dst->ret)
  1517. return;
  1518. }
  1519. /* fr2-fr31 */
  1520. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(32)) {
  1521. index = (dst->pos - ELF_FP_OFFSET(2)) / sizeof(elf_fpreg_t);
  1522. min_copy = min(((unsigned int)ELF_FP_OFFSET(32)),
  1523. dst->pos + dst->count);
  1524. for (i = dst->pos; i < min_copy; i += sizeof(elf_fpreg_t),
  1525. index++)
  1526. if (unw_get_fr(info, i / sizeof(elf_fpreg_t),
  1527. &tmp[index])) {
  1528. dst->ret = -EIO;
  1529. return;
  1530. }
  1531. dst->ret = user_regset_copyout(&dst->pos, &dst->count,
  1532. &dst->u.get.kbuf, &dst->u.get.ubuf, tmp,
  1533. ELF_FP_OFFSET(2), ELF_FP_OFFSET(32));
  1534. if (dst->count == 0 || dst->ret)
  1535. return;
  1536. }
  1537. /* fph */
  1538. if (dst->count > 0) {
  1539. ia64_flush_fph(dst->target);
  1540. if (task->thread.flags & IA64_THREAD_FPH_VALID)
  1541. dst->ret = user_regset_copyout(
  1542. &dst->pos, &dst->count,
  1543. &dst->u.get.kbuf, &dst->u.get.ubuf,
  1544. &dst->target->thread.fph,
  1545. ELF_FP_OFFSET(32), -1);
  1546. else
  1547. /* Zero fill instead. */
  1548. dst->ret = user_regset_copyout_zero(
  1549. &dst->pos, &dst->count,
  1550. &dst->u.get.kbuf, &dst->u.get.ubuf,
  1551. ELF_FP_OFFSET(32), -1);
  1552. }
  1553. }
  1554. void do_fpregs_set(struct unw_frame_info *info, void *arg)
  1555. {
  1556. struct regset_getset *dst = arg;
  1557. elf_fpreg_t fpreg, tmp[30];
  1558. int index, start, end;
  1559. if (unw_unwind_to_user(info) < 0)
  1560. return;
  1561. /* Skip pos 0 and 1 */
  1562. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(2)) {
  1563. dst->ret = user_regset_copyin_ignore(&dst->pos, &dst->count,
  1564. &dst->u.set.kbuf,
  1565. &dst->u.set.ubuf,
  1566. 0, ELF_FP_OFFSET(2));
  1567. if (dst->count == 0 || dst->ret)
  1568. return;
  1569. }
  1570. /* fr2-fr31 */
  1571. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(32)) {
  1572. start = dst->pos;
  1573. end = min(((unsigned int)ELF_FP_OFFSET(32)),
  1574. dst->pos + dst->count);
  1575. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1576. &dst->u.set.kbuf, &dst->u.set.ubuf, tmp,
  1577. ELF_FP_OFFSET(2), ELF_FP_OFFSET(32));
  1578. if (dst->ret)
  1579. return;
  1580. if (start & 0xF) { /* only write high part */
  1581. if (unw_get_fr(info, start / sizeof(elf_fpreg_t),
  1582. &fpreg)) {
  1583. dst->ret = -EIO;
  1584. return;
  1585. }
  1586. tmp[start / sizeof(elf_fpreg_t) - 2].u.bits[0]
  1587. = fpreg.u.bits[0];
  1588. start &= ~0xFUL;
  1589. }
  1590. if (end & 0xF) { /* only write low part */
  1591. if (unw_get_fr(info, end / sizeof(elf_fpreg_t),
  1592. &fpreg)) {
  1593. dst->ret = -EIO;
  1594. return;
  1595. }
  1596. tmp[end / sizeof(elf_fpreg_t) - 2].u.bits[1]
  1597. = fpreg.u.bits[1];
  1598. end = (end + 0xF) & ~0xFUL;
  1599. }
  1600. for ( ; start < end ; start += sizeof(elf_fpreg_t)) {
  1601. index = start / sizeof(elf_fpreg_t);
  1602. if (unw_set_fr(info, index, tmp[index - 2])) {
  1603. dst->ret = -EIO;
  1604. return;
  1605. }
  1606. }
  1607. if (dst->ret || dst->count == 0)
  1608. return;
  1609. }
  1610. /* fph */
  1611. if (dst->count > 0 && dst->pos < ELF_FP_OFFSET(128)) {
  1612. ia64_sync_fph(dst->target);
  1613. dst->ret = user_regset_copyin(&dst->pos, &dst->count,
  1614. &dst->u.set.kbuf,
  1615. &dst->u.set.ubuf,
  1616. &dst->target->thread.fph,
  1617. ELF_FP_OFFSET(32), -1);
  1618. }
  1619. }
  1620. static int
  1621. do_regset_call(void (*call)(struct unw_frame_info *, void *),
  1622. struct task_struct *target,
  1623. const struct user_regset *regset,
  1624. unsigned int pos, unsigned int count,
  1625. const void *kbuf, const void __user *ubuf)
  1626. {
  1627. struct regset_getset info = { .target = target, .regset = regset,
  1628. .pos = pos, .count = count,
  1629. .u.set = { .kbuf = kbuf, .ubuf = ubuf },
  1630. .ret = 0 };
  1631. if (target == current)
  1632. unw_init_running(call, &info);
  1633. else {
  1634. struct unw_frame_info ufi;
  1635. memset(&ufi, 0, sizeof(ufi));
  1636. unw_init_from_blocked_task(&ufi, target);
  1637. (*call)(&ufi, &info);
  1638. }
  1639. return info.ret;
  1640. }
  1641. static int
  1642. gpregs_get(struct task_struct *target,
  1643. const struct user_regset *regset,
  1644. unsigned int pos, unsigned int count,
  1645. void *kbuf, void __user *ubuf)
  1646. {
  1647. return do_regset_call(do_gpregs_get, target, regset, pos, count,
  1648. kbuf, ubuf);
  1649. }
  1650. static int gpregs_set(struct task_struct *target,
  1651. const struct user_regset *regset,
  1652. unsigned int pos, unsigned int count,
  1653. const void *kbuf, const void __user *ubuf)
  1654. {
  1655. return do_regset_call(do_gpregs_set, target, regset, pos, count,
  1656. kbuf, ubuf);
  1657. }
  1658. static void do_gpregs_writeback(struct unw_frame_info *info, void *arg)
  1659. {
  1660. do_sync_rbs(info, ia64_sync_user_rbs);
  1661. }
  1662. /*
  1663. * This is called to write back the register backing store.
  1664. * ptrace does this before it stops, so that a tracer reading the user
  1665. * memory after the thread stops will get the current register data.
  1666. */
  1667. static int
  1668. gpregs_writeback(struct task_struct *target,
  1669. const struct user_regset *regset,
  1670. int now)
  1671. {
  1672. if (test_and_set_tsk_thread_flag(target, TIF_RESTORE_RSE))
  1673. return 0;
  1674. set_notify_resume(target);
  1675. return do_regset_call(do_gpregs_writeback, target, regset, 0, 0,
  1676. NULL, NULL);
  1677. }
  1678. static int
  1679. fpregs_active(struct task_struct *target, const struct user_regset *regset)
  1680. {
  1681. return (target->thread.flags & IA64_THREAD_FPH_VALID) ? 128 : 32;
  1682. }
  1683. static int fpregs_get(struct task_struct *target,
  1684. const struct user_regset *regset,
  1685. unsigned int pos, unsigned int count,
  1686. void *kbuf, void __user *ubuf)
  1687. {
  1688. return do_regset_call(do_fpregs_get, target, regset, pos, count,
  1689. kbuf, ubuf);
  1690. }
  1691. static int fpregs_set(struct task_struct *target,
  1692. const struct user_regset *regset,
  1693. unsigned int pos, unsigned int count,
  1694. const void *kbuf, const void __user *ubuf)
  1695. {
  1696. return do_regset_call(do_fpregs_set, target, regset, pos, count,
  1697. kbuf, ubuf);
  1698. }
  1699. static int
  1700. access_uarea(struct task_struct *child, unsigned long addr,
  1701. unsigned long *data, int write_access)
  1702. {
  1703. unsigned int pos = -1; /* an invalid value */
  1704. int ret;
  1705. unsigned long *ptr, regnum;
  1706. if ((addr & 0x7) != 0) {
  1707. dprintk("ptrace: unaligned register address 0x%lx\n", addr);
  1708. return -1;
  1709. }
  1710. if ((addr >= PT_NAT_BITS + 8 && addr < PT_F2) ||
  1711. (addr >= PT_R7 + 8 && addr < PT_B1) ||
  1712. (addr >= PT_AR_LC + 8 && addr < PT_CR_IPSR) ||
  1713. (addr >= PT_AR_SSD + 8 && addr < PT_DBR)) {
  1714. dprintk("ptrace: rejecting access to register "
  1715. "address 0x%lx\n", addr);
  1716. return -1;
  1717. }
  1718. switch (addr) {
  1719. case PT_F32 ... (PT_F127 + 15):
  1720. pos = addr - PT_F32 + ELF_FP_OFFSET(32);
  1721. break;
  1722. case PT_F2 ... (PT_F5 + 15):
  1723. pos = addr - PT_F2 + ELF_FP_OFFSET(2);
  1724. break;
  1725. case PT_F10 ... (PT_F31 + 15):
  1726. pos = addr - PT_F10 + ELF_FP_OFFSET(10);
  1727. break;
  1728. case PT_F6 ... (PT_F9 + 15):
  1729. pos = addr - PT_F6 + ELF_FP_OFFSET(6);
  1730. break;
  1731. }
  1732. if (pos != -1) {
  1733. if (write_access)
  1734. ret = fpregs_set(child, NULL, pos,
  1735. sizeof(unsigned long), data, NULL);
  1736. else
  1737. ret = fpregs_get(child, NULL, pos,
  1738. sizeof(unsigned long), data, NULL);
  1739. if (ret != 0)
  1740. return -1;
  1741. return 0;
  1742. }
  1743. switch (addr) {
  1744. case PT_NAT_BITS:
  1745. pos = ELF_NAT_OFFSET;
  1746. break;
  1747. case PT_R4 ... PT_R7:
  1748. pos = addr - PT_R4 + ELF_GR_OFFSET(4);
  1749. break;
  1750. case PT_B1 ... PT_B5:
  1751. pos = addr - PT_B1 + ELF_BR_OFFSET(1);
  1752. break;
  1753. case PT_AR_EC:
  1754. pos = ELF_AR_EC_OFFSET;
  1755. break;
  1756. case PT_AR_LC:
  1757. pos = ELF_AR_LC_OFFSET;
  1758. break;
  1759. case PT_CR_IPSR:
  1760. pos = ELF_CR_IPSR_OFFSET;
  1761. break;
  1762. case PT_CR_IIP:
  1763. pos = ELF_CR_IIP_OFFSET;
  1764. break;
  1765. case PT_CFM:
  1766. pos = ELF_CFM_OFFSET;
  1767. break;
  1768. case PT_AR_UNAT:
  1769. pos = ELF_AR_UNAT_OFFSET;
  1770. break;
  1771. case PT_AR_PFS:
  1772. pos = ELF_AR_PFS_OFFSET;
  1773. break;
  1774. case PT_AR_RSC:
  1775. pos = ELF_AR_RSC_OFFSET;
  1776. break;
  1777. case PT_AR_RNAT:
  1778. pos = ELF_AR_RNAT_OFFSET;
  1779. break;
  1780. case PT_AR_BSPSTORE:
  1781. pos = ELF_AR_BSPSTORE_OFFSET;
  1782. break;
  1783. case PT_PR:
  1784. pos = ELF_PR_OFFSET;
  1785. break;
  1786. case PT_B6:
  1787. pos = ELF_BR_OFFSET(6);
  1788. break;
  1789. case PT_AR_BSP:
  1790. pos = ELF_AR_BSP_OFFSET;
  1791. break;
  1792. case PT_R1 ... PT_R3:
  1793. pos = addr - PT_R1 + ELF_GR_OFFSET(1);
  1794. break;
  1795. case PT_R12 ... PT_R15:
  1796. pos = addr - PT_R12 + ELF_GR_OFFSET(12);
  1797. break;
  1798. case PT_R8 ... PT_R11:
  1799. pos = addr - PT_R8 + ELF_GR_OFFSET(8);
  1800. break;
  1801. case PT_R16 ... PT_R31:
  1802. pos = addr - PT_R16 + ELF_GR_OFFSET(16);
  1803. break;
  1804. case PT_AR_CCV:
  1805. pos = ELF_AR_CCV_OFFSET;
  1806. break;
  1807. case PT_AR_FPSR:
  1808. pos = ELF_AR_FPSR_OFFSET;
  1809. break;
  1810. case PT_B0:
  1811. pos = ELF_BR_OFFSET(0);
  1812. break;
  1813. case PT_B7:
  1814. pos = ELF_BR_OFFSET(7);
  1815. break;
  1816. case PT_AR_CSD:
  1817. pos = ELF_AR_CSD_OFFSET;
  1818. break;
  1819. case PT_AR_SSD:
  1820. pos = ELF_AR_SSD_OFFSET;
  1821. break;
  1822. }
  1823. if (pos != -1) {
  1824. if (write_access)
  1825. ret = gpregs_set(child, NULL, pos,
  1826. sizeof(unsigned long), data, NULL);
  1827. else
  1828. ret = gpregs_get(child, NULL, pos,
  1829. sizeof(unsigned long), data, NULL);
  1830. if (ret != 0)
  1831. return -1;
  1832. return 0;
  1833. }
  1834. /* access debug registers */
  1835. if (addr >= PT_IBR) {
  1836. regnum = (addr - PT_IBR) >> 3;
  1837. ptr = &child->thread.ibr[0];
  1838. } else {
  1839. regnum = (addr - PT_DBR) >> 3;
  1840. ptr = &child->thread.dbr[0];
  1841. }
  1842. if (regnum >= 8) {
  1843. dprintk("ptrace: rejecting access to register "
  1844. "address 0x%lx\n", addr);
  1845. return -1;
  1846. }
  1847. #ifdef CONFIG_PERFMON
  1848. /*
  1849. * Check if debug registers are used by perfmon. This
  1850. * test must be done once we know that we can do the
  1851. * operation, i.e. the arguments are all valid, but
  1852. * before we start modifying the state.
  1853. *
  1854. * Perfmon needs to keep a count of how many processes
  1855. * are trying to modify the debug registers for system
  1856. * wide monitoring sessions.
  1857. *
  1858. * We also include read access here, because they may
  1859. * cause the PMU-installed debug register state
  1860. * (dbr[], ibr[]) to be reset. The two arrays are also
  1861. * used by perfmon, but we do not use
  1862. * IA64_THREAD_DBG_VALID. The registers are restored
  1863. * by the PMU context switch code.
  1864. */
  1865. if (pfm_use_debug_registers(child))
  1866. return -1;
  1867. #endif
  1868. if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
  1869. child->thread.flags |= IA64_THREAD_DBG_VALID;
  1870. memset(child->thread.dbr, 0,
  1871. sizeof(child->thread.dbr));
  1872. memset(child->thread.ibr, 0,
  1873. sizeof(child->thread.ibr));
  1874. }
  1875. ptr += regnum;
  1876. if ((regnum & 1) && write_access) {
  1877. /* don't let the user set kernel-level breakpoints: */
  1878. *ptr = *data & ~(7UL << 56);
  1879. return 0;
  1880. }
  1881. if (write_access)
  1882. *ptr = *data;
  1883. else
  1884. *data = *ptr;
  1885. return 0;
  1886. }
  1887. static const struct user_regset native_regsets[] = {
  1888. {
  1889. .core_note_type = NT_PRSTATUS,
  1890. .n = ELF_NGREG,
  1891. .size = sizeof(elf_greg_t), .align = sizeof(elf_greg_t),
  1892. .get = gpregs_get, .set = gpregs_set,
  1893. .writeback = gpregs_writeback
  1894. },
  1895. {
  1896. .core_note_type = NT_PRFPREG,
  1897. .n = ELF_NFPREG,
  1898. .size = sizeof(elf_fpreg_t), .align = sizeof(elf_fpreg_t),
  1899. .get = fpregs_get, .set = fpregs_set, .active = fpregs_active
  1900. },
  1901. };
  1902. static const struct user_regset_view user_ia64_view = {
  1903. .name = "ia64",
  1904. .e_machine = EM_IA_64,
  1905. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  1906. };
  1907. const struct user_regset_view *task_user_regset_view(struct task_struct *tsk)
  1908. {
  1909. return &user_ia64_view;
  1910. }
  1911. struct syscall_get_set_args {
  1912. unsigned int i;
  1913. unsigned int n;
  1914. unsigned long *args;
  1915. struct pt_regs *regs;
  1916. int rw;
  1917. };
  1918. static void syscall_get_set_args_cb(struct unw_frame_info *info, void *data)
  1919. {
  1920. struct syscall_get_set_args *args = data;
  1921. struct pt_regs *pt = args->regs;
  1922. unsigned long *krbs, cfm, ndirty;
  1923. int i, count;
  1924. if (unw_unwind_to_user(info) < 0)
  1925. return;
  1926. cfm = pt->cr_ifs;
  1927. krbs = (unsigned long *)info->task + IA64_RBS_OFFSET/8;
  1928. ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
  1929. count = 0;
  1930. if (in_syscall(pt))
  1931. count = min_t(int, args->n, cfm & 0x7f);
  1932. for (i = 0; i < count; i++) {
  1933. if (args->rw)
  1934. *ia64_rse_skip_regs(krbs, ndirty + i + args->i) =
  1935. args->args[i];
  1936. else
  1937. args->args[i] = *ia64_rse_skip_regs(krbs,
  1938. ndirty + i + args->i);
  1939. }
  1940. if (!args->rw) {
  1941. while (i < args->n) {
  1942. args->args[i] = 0;
  1943. i++;
  1944. }
  1945. }
  1946. }
  1947. void ia64_syscall_get_set_arguments(struct task_struct *task,
  1948. struct pt_regs *regs, unsigned int i, unsigned int n,
  1949. unsigned long *args, int rw)
  1950. {
  1951. struct syscall_get_set_args data = {
  1952. .i = i,
  1953. .n = n,
  1954. .args = args,
  1955. .regs = regs,
  1956. .rw = rw,
  1957. };
  1958. if (task == current)
  1959. unw_init_running(syscall_get_set_args_cb, &data);
  1960. else {
  1961. struct unw_frame_info ufi;
  1962. memset(&ufi, 0, sizeof(ufi));
  1963. unw_init_from_blocked_task(&ufi, task);
  1964. syscall_get_set_args_cb(&ufi, &data);
  1965. }
  1966. }