ptrace.c 58 KB

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