ptrace.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. * Ptrace user space interface.
  3. *
  4. * Copyright IBM Corp. 1999, 2010
  5. * Author(s): Denis Joseph Barrow
  6. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/user.h>
  15. #include <linux/security.h>
  16. #include <linux/audit.h>
  17. #include <linux/signal.h>
  18. #include <linux/elf.h>
  19. #include <linux/regset.h>
  20. #include <linux/tracehook.h>
  21. #include <linux/seccomp.h>
  22. #include <linux/compat.h>
  23. #include <trace/syscall.h>
  24. #include <asm/segment.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/unistd.h>
  30. #include <asm/switch_to.h>
  31. #include "entry.h"
  32. #ifdef CONFIG_COMPAT
  33. #include "compat_ptrace.h"
  34. #endif
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/syscalls.h>
  37. void update_cr_regs(struct task_struct *task)
  38. {
  39. struct pt_regs *regs = task_pt_regs(task);
  40. struct thread_struct *thread = &task->thread;
  41. struct per_regs old, new;
  42. /* Take care of the enable/disable of transactional execution. */
  43. if (MACHINE_HAS_TE || MACHINE_HAS_VX) {
  44. unsigned long cr, cr_new;
  45. __ctl_store(cr, 0, 0);
  46. cr_new = cr;
  47. if (MACHINE_HAS_TE) {
  48. /* Set or clear transaction execution TXC bit 8. */
  49. cr_new |= (1UL << 55);
  50. if (task->thread.per_flags & PER_FLAG_NO_TE)
  51. cr_new &= ~(1UL << 55);
  52. }
  53. if (MACHINE_HAS_VX) {
  54. /* Enable/disable of vector extension */
  55. cr_new &= ~(1UL << 17);
  56. if (task->thread.vxrs)
  57. cr_new |= (1UL << 17);
  58. }
  59. if (cr_new != cr)
  60. __ctl_load(cr_new, 0, 0);
  61. if (MACHINE_HAS_TE) {
  62. /* Set/clear transaction execution TDC bits 62/63. */
  63. __ctl_store(cr, 2, 2);
  64. cr_new = cr & ~3UL;
  65. if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
  66. if (task->thread.per_flags &
  67. PER_FLAG_TE_ABORT_RAND_TEND)
  68. cr_new |= 1UL;
  69. else
  70. cr_new |= 2UL;
  71. }
  72. if (cr_new != cr)
  73. __ctl_load(cr_new, 2, 2);
  74. }
  75. }
  76. /* Copy user specified PER registers */
  77. new.control = thread->per_user.control;
  78. new.start = thread->per_user.start;
  79. new.end = thread->per_user.end;
  80. /* merge TIF_SINGLE_STEP into user specified PER registers. */
  81. if (test_tsk_thread_flag(task, TIF_SINGLE_STEP) ||
  82. test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP)) {
  83. if (test_tsk_thread_flag(task, TIF_BLOCK_STEP))
  84. new.control |= PER_EVENT_BRANCH;
  85. else
  86. new.control |= PER_EVENT_IFETCH;
  87. new.control |= PER_CONTROL_SUSPENSION;
  88. new.control |= PER_EVENT_TRANSACTION_END;
  89. if (test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP))
  90. new.control |= PER_EVENT_IFETCH;
  91. new.start = 0;
  92. new.end = PSW_ADDR_INSN;
  93. }
  94. /* Take care of the PER enablement bit in the PSW. */
  95. if (!(new.control & PER_EVENT_MASK)) {
  96. regs->psw.mask &= ~PSW_MASK_PER;
  97. return;
  98. }
  99. regs->psw.mask |= PSW_MASK_PER;
  100. __ctl_store(old, 9, 11);
  101. if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
  102. __ctl_load(new, 9, 11);
  103. }
  104. void user_enable_single_step(struct task_struct *task)
  105. {
  106. clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
  107. set_tsk_thread_flag(task, TIF_SINGLE_STEP);
  108. }
  109. void user_disable_single_step(struct task_struct *task)
  110. {
  111. clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
  112. clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
  113. }
  114. void user_enable_block_step(struct task_struct *task)
  115. {
  116. set_tsk_thread_flag(task, TIF_SINGLE_STEP);
  117. set_tsk_thread_flag(task, TIF_BLOCK_STEP);
  118. }
  119. /*
  120. * Called by kernel/ptrace.c when detaching..
  121. *
  122. * Clear all debugging related fields.
  123. */
  124. void ptrace_disable(struct task_struct *task)
  125. {
  126. memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
  127. memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
  128. clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
  129. clear_pt_regs_flag(task_pt_regs(task), PIF_PER_TRAP);
  130. task->thread.per_flags = 0;
  131. }
  132. #define __ADDR_MASK 7
  133. static inline unsigned long __peek_user_per(struct task_struct *child,
  134. addr_t addr)
  135. {
  136. struct per_struct_kernel *dummy = NULL;
  137. if (addr == (addr_t) &dummy->cr9)
  138. /* Control bits of the active per set. */
  139. return test_thread_flag(TIF_SINGLE_STEP) ?
  140. PER_EVENT_IFETCH : child->thread.per_user.control;
  141. else if (addr == (addr_t) &dummy->cr10)
  142. /* Start address of the active per set. */
  143. return test_thread_flag(TIF_SINGLE_STEP) ?
  144. 0 : child->thread.per_user.start;
  145. else if (addr == (addr_t) &dummy->cr11)
  146. /* End address of the active per set. */
  147. return test_thread_flag(TIF_SINGLE_STEP) ?
  148. PSW_ADDR_INSN : child->thread.per_user.end;
  149. else if (addr == (addr_t) &dummy->bits)
  150. /* Single-step bit. */
  151. return test_thread_flag(TIF_SINGLE_STEP) ?
  152. (1UL << (BITS_PER_LONG - 1)) : 0;
  153. else if (addr == (addr_t) &dummy->starting_addr)
  154. /* Start address of the user specified per set. */
  155. return child->thread.per_user.start;
  156. else if (addr == (addr_t) &dummy->ending_addr)
  157. /* End address of the user specified per set. */
  158. return child->thread.per_user.end;
  159. else if (addr == (addr_t) &dummy->perc_atmid)
  160. /* PER code, ATMID and AI of the last PER trap */
  161. return (unsigned long)
  162. child->thread.per_event.cause << (BITS_PER_LONG - 16);
  163. else if (addr == (addr_t) &dummy->address)
  164. /* Address of the last PER trap */
  165. return child->thread.per_event.address;
  166. else if (addr == (addr_t) &dummy->access_id)
  167. /* Access id of the last PER trap */
  168. return (unsigned long)
  169. child->thread.per_event.paid << (BITS_PER_LONG - 8);
  170. return 0;
  171. }
  172. /*
  173. * Read the word at offset addr from the user area of a process. The
  174. * trouble here is that the information is littered over different
  175. * locations. The process registers are found on the kernel stack,
  176. * the floating point stuff and the trace settings are stored in
  177. * the task structure. In addition the different structures in
  178. * struct user contain pad bytes that should be read as zeroes.
  179. * Lovely...
  180. */
  181. static unsigned long __peek_user(struct task_struct *child, addr_t addr)
  182. {
  183. struct user *dummy = NULL;
  184. addr_t offset, tmp;
  185. if (addr < (addr_t) &dummy->regs.acrs) {
  186. /*
  187. * psw and gprs are stored on the stack
  188. */
  189. tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
  190. if (addr == (addr_t) &dummy->regs.psw.mask) {
  191. /* Return a clean psw mask. */
  192. tmp &= PSW_MASK_USER | PSW_MASK_RI;
  193. tmp |= PSW_USER_BITS;
  194. }
  195. } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
  196. /*
  197. * access registers are stored in the thread structure
  198. */
  199. offset = addr - (addr_t) &dummy->regs.acrs;
  200. /*
  201. * Very special case: old & broken 64 bit gdb reading
  202. * from acrs[15]. Result is a 64 bit value. Read the
  203. * 32 bit acrs[15] value and shift it by 32. Sick...
  204. */
  205. if (addr == (addr_t) &dummy->regs.acrs[15])
  206. tmp = ((unsigned long) child->thread.acrs[15]) << 32;
  207. else
  208. tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
  209. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  210. /*
  211. * orig_gpr2 is stored on the kernel stack
  212. */
  213. tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
  214. } else if (addr < (addr_t) &dummy->regs.fp_regs) {
  215. /*
  216. * prevent reads of padding hole between
  217. * orig_gpr2 and fp_regs on s390.
  218. */
  219. tmp = 0;
  220. } else if (addr == (addr_t) &dummy->regs.fp_regs.fpc) {
  221. /*
  222. * floating point control reg. is in the thread structure
  223. */
  224. tmp = child->thread.fp_regs.fpc;
  225. tmp <<= BITS_PER_LONG - 32;
  226. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  227. /*
  228. * floating point regs. are either in child->thread.fp_regs
  229. * or the child->thread.vxrs array
  230. */
  231. offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
  232. if (child->thread.vxrs)
  233. tmp = *(addr_t *)
  234. ((addr_t) child->thread.vxrs + 2*offset);
  235. else
  236. tmp = *(addr_t *)
  237. ((addr_t) &child->thread.fp_regs.fprs + offset);
  238. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  239. /*
  240. * Handle access to the per_info structure.
  241. */
  242. addr -= (addr_t) &dummy->regs.per_info;
  243. tmp = __peek_user_per(child, addr);
  244. } else
  245. tmp = 0;
  246. return tmp;
  247. }
  248. static int
  249. peek_user(struct task_struct *child, addr_t addr, addr_t data)
  250. {
  251. addr_t tmp, mask;
  252. /*
  253. * Stupid gdb peeks/pokes the access registers in 64 bit with
  254. * an alignment of 4. Programmers from hell...
  255. */
  256. mask = __ADDR_MASK;
  257. if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
  258. addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
  259. mask = 3;
  260. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  261. return -EIO;
  262. tmp = __peek_user(child, addr);
  263. return put_user(tmp, (addr_t __user *) data);
  264. }
  265. static inline void __poke_user_per(struct task_struct *child,
  266. addr_t addr, addr_t data)
  267. {
  268. struct per_struct_kernel *dummy = NULL;
  269. /*
  270. * There are only three fields in the per_info struct that the
  271. * debugger user can write to.
  272. * 1) cr9: the debugger wants to set a new PER event mask
  273. * 2) starting_addr: the debugger wants to set a new starting
  274. * address to use with the PER event mask.
  275. * 3) ending_addr: the debugger wants to set a new ending
  276. * address to use with the PER event mask.
  277. * The user specified PER event mask and the start and end
  278. * addresses are used only if single stepping is not in effect.
  279. * Writes to any other field in per_info are ignored.
  280. */
  281. if (addr == (addr_t) &dummy->cr9)
  282. /* PER event mask of the user specified per set. */
  283. child->thread.per_user.control =
  284. data & (PER_EVENT_MASK | PER_CONTROL_MASK);
  285. else if (addr == (addr_t) &dummy->starting_addr)
  286. /* Starting address of the user specified per set. */
  287. child->thread.per_user.start = data;
  288. else if (addr == (addr_t) &dummy->ending_addr)
  289. /* Ending address of the user specified per set. */
  290. child->thread.per_user.end = data;
  291. }
  292. /*
  293. * Write a word to the user area of a process at location addr. This
  294. * operation does have an additional problem compared to peek_user.
  295. * Stores to the program status word and on the floating point
  296. * control register needs to get checked for validity.
  297. */
  298. static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
  299. {
  300. struct user *dummy = NULL;
  301. addr_t offset;
  302. if (addr < (addr_t) &dummy->regs.acrs) {
  303. /*
  304. * psw and gprs are stored on the stack
  305. */
  306. if (addr == (addr_t) &dummy->regs.psw.mask) {
  307. unsigned long mask = PSW_MASK_USER;
  308. mask |= is_ri_task(child) ? PSW_MASK_RI : 0;
  309. if ((data ^ PSW_USER_BITS) & ~mask)
  310. /* Invalid psw mask. */
  311. return -EINVAL;
  312. if ((data & PSW_MASK_ASC) == PSW_ASC_HOME)
  313. /* Invalid address-space-control bits */
  314. return -EINVAL;
  315. if ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))
  316. /* Invalid addressing mode bits */
  317. return -EINVAL;
  318. }
  319. *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
  320. } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
  321. /*
  322. * access registers are stored in the thread structure
  323. */
  324. offset = addr - (addr_t) &dummy->regs.acrs;
  325. /*
  326. * Very special case: old & broken 64 bit gdb writing
  327. * to acrs[15] with a 64 bit value. Ignore the lower
  328. * half of the value and write the upper 32 bit to
  329. * acrs[15]. Sick...
  330. */
  331. if (addr == (addr_t) &dummy->regs.acrs[15])
  332. child->thread.acrs[15] = (unsigned int) (data >> 32);
  333. else
  334. *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
  335. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  336. /*
  337. * orig_gpr2 is stored on the kernel stack
  338. */
  339. task_pt_regs(child)->orig_gpr2 = data;
  340. } else if (addr < (addr_t) &dummy->regs.fp_regs) {
  341. /*
  342. * prevent writes of padding hole between
  343. * orig_gpr2 and fp_regs on s390.
  344. */
  345. return 0;
  346. } else if (addr == (addr_t) &dummy->regs.fp_regs.fpc) {
  347. /*
  348. * floating point control reg. is in the thread structure
  349. */
  350. if ((unsigned int) data != 0 ||
  351. test_fp_ctl(data >> (BITS_PER_LONG - 32)))
  352. return -EINVAL;
  353. child->thread.fp_regs.fpc = data >> (BITS_PER_LONG - 32);
  354. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  355. /*
  356. * floating point regs. are either in child->thread.fp_regs
  357. * or the child->thread.vxrs array
  358. */
  359. offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
  360. if (child->thread.vxrs)
  361. *(addr_t *)((addr_t)
  362. child->thread.vxrs + 2*offset) = data;
  363. else
  364. *(addr_t *)((addr_t)
  365. &child->thread.fp_regs.fprs + offset) = data;
  366. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  367. /*
  368. * Handle access to the per_info structure.
  369. */
  370. addr -= (addr_t) &dummy->regs.per_info;
  371. __poke_user_per(child, addr, data);
  372. }
  373. return 0;
  374. }
  375. static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
  376. {
  377. addr_t mask;
  378. /*
  379. * Stupid gdb peeks/pokes the access registers in 64 bit with
  380. * an alignment of 4. Programmers from hell indeed...
  381. */
  382. mask = __ADDR_MASK;
  383. if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
  384. addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
  385. mask = 3;
  386. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  387. return -EIO;
  388. return __poke_user(child, addr, data);
  389. }
  390. long arch_ptrace(struct task_struct *child, long request,
  391. unsigned long addr, unsigned long data)
  392. {
  393. ptrace_area parea;
  394. int copied, ret;
  395. switch (request) {
  396. case PTRACE_PEEKUSR:
  397. /* read the word at location addr in the USER area. */
  398. return peek_user(child, addr, data);
  399. case PTRACE_POKEUSR:
  400. /* write the word at location addr in the USER area */
  401. return poke_user(child, addr, data);
  402. case PTRACE_PEEKUSR_AREA:
  403. case PTRACE_POKEUSR_AREA:
  404. if (copy_from_user(&parea, (void __force __user *) addr,
  405. sizeof(parea)))
  406. return -EFAULT;
  407. addr = parea.kernel_addr;
  408. data = parea.process_addr;
  409. copied = 0;
  410. while (copied < parea.len) {
  411. if (request == PTRACE_PEEKUSR_AREA)
  412. ret = peek_user(child, addr, data);
  413. else {
  414. addr_t utmp;
  415. if (get_user(utmp,
  416. (addr_t __force __user *) data))
  417. return -EFAULT;
  418. ret = poke_user(child, addr, utmp);
  419. }
  420. if (ret)
  421. return ret;
  422. addr += sizeof(unsigned long);
  423. data += sizeof(unsigned long);
  424. copied += sizeof(unsigned long);
  425. }
  426. return 0;
  427. case PTRACE_GET_LAST_BREAK:
  428. put_user(task_thread_info(child)->last_break,
  429. (unsigned long __user *) data);
  430. return 0;
  431. case PTRACE_ENABLE_TE:
  432. if (!MACHINE_HAS_TE)
  433. return -EIO;
  434. child->thread.per_flags &= ~PER_FLAG_NO_TE;
  435. return 0;
  436. case PTRACE_DISABLE_TE:
  437. if (!MACHINE_HAS_TE)
  438. return -EIO;
  439. child->thread.per_flags |= PER_FLAG_NO_TE;
  440. child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
  441. return 0;
  442. case PTRACE_TE_ABORT_RAND:
  443. if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
  444. return -EIO;
  445. switch (data) {
  446. case 0UL:
  447. child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
  448. break;
  449. case 1UL:
  450. child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
  451. child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
  452. break;
  453. case 2UL:
  454. child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
  455. child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
  456. break;
  457. default:
  458. return -EINVAL;
  459. }
  460. return 0;
  461. default:
  462. /* Removing high order bit from addr (only for 31 bit). */
  463. addr &= PSW_ADDR_INSN;
  464. return ptrace_request(child, request, addr, data);
  465. }
  466. }
  467. #ifdef CONFIG_COMPAT
  468. /*
  469. * Now the fun part starts... a 31 bit program running in the
  470. * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
  471. * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
  472. * to handle, the difference to the 64 bit versions of the requests
  473. * is that the access is done in multiples of 4 byte instead of
  474. * 8 bytes (sizeof(unsigned long) on 31/64 bit).
  475. * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
  476. * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
  477. * is a 31 bit program too, the content of struct user can be
  478. * emulated. A 31 bit program peeking into the struct user of
  479. * a 64 bit program is a no-no.
  480. */
  481. /*
  482. * Same as peek_user_per but for a 31 bit program.
  483. */
  484. static inline __u32 __peek_user_per_compat(struct task_struct *child,
  485. addr_t addr)
  486. {
  487. struct compat_per_struct_kernel *dummy32 = NULL;
  488. if (addr == (addr_t) &dummy32->cr9)
  489. /* Control bits of the active per set. */
  490. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  491. PER_EVENT_IFETCH : child->thread.per_user.control;
  492. else if (addr == (addr_t) &dummy32->cr10)
  493. /* Start address of the active per set. */
  494. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  495. 0 : child->thread.per_user.start;
  496. else if (addr == (addr_t) &dummy32->cr11)
  497. /* End address of the active per set. */
  498. return test_thread_flag(TIF_SINGLE_STEP) ?
  499. PSW32_ADDR_INSN : child->thread.per_user.end;
  500. else if (addr == (addr_t) &dummy32->bits)
  501. /* Single-step bit. */
  502. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  503. 0x80000000 : 0;
  504. else if (addr == (addr_t) &dummy32->starting_addr)
  505. /* Start address of the user specified per set. */
  506. return (__u32) child->thread.per_user.start;
  507. else if (addr == (addr_t) &dummy32->ending_addr)
  508. /* End address of the user specified per set. */
  509. return (__u32) child->thread.per_user.end;
  510. else if (addr == (addr_t) &dummy32->perc_atmid)
  511. /* PER code, ATMID and AI of the last PER trap */
  512. return (__u32) child->thread.per_event.cause << 16;
  513. else if (addr == (addr_t) &dummy32->address)
  514. /* Address of the last PER trap */
  515. return (__u32) child->thread.per_event.address;
  516. else if (addr == (addr_t) &dummy32->access_id)
  517. /* Access id of the last PER trap */
  518. return (__u32) child->thread.per_event.paid << 24;
  519. return 0;
  520. }
  521. /*
  522. * Same as peek_user but for a 31 bit program.
  523. */
  524. static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
  525. {
  526. struct compat_user *dummy32 = NULL;
  527. addr_t offset;
  528. __u32 tmp;
  529. if (addr < (addr_t) &dummy32->regs.acrs) {
  530. struct pt_regs *regs = task_pt_regs(child);
  531. /*
  532. * psw and gprs are stored on the stack
  533. */
  534. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  535. /* Fake a 31 bit psw mask. */
  536. tmp = (__u32)(regs->psw.mask >> 32);
  537. tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
  538. tmp |= PSW32_USER_BITS;
  539. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  540. /* Fake a 31 bit psw address. */
  541. tmp = (__u32) regs->psw.addr |
  542. (__u32)(regs->psw.mask & PSW_MASK_BA);
  543. } else {
  544. /* gpr 0-15 */
  545. tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
  546. }
  547. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  548. /*
  549. * access registers are stored in the thread structure
  550. */
  551. offset = addr - (addr_t) &dummy32->regs.acrs;
  552. tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
  553. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  554. /*
  555. * orig_gpr2 is stored on the kernel stack
  556. */
  557. tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
  558. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  559. /*
  560. * prevent reads of padding hole between
  561. * orig_gpr2 and fp_regs on s390.
  562. */
  563. tmp = 0;
  564. } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
  565. /*
  566. * floating point control reg. is in the thread structure
  567. */
  568. tmp = child->thread.fp_regs.fpc;
  569. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  570. /*
  571. * floating point regs. are either in child->thread.fp_regs
  572. * or the child->thread.vxrs array
  573. */
  574. offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
  575. if (child->thread.vxrs)
  576. tmp = *(__u32 *)
  577. ((addr_t) child->thread.vxrs + 2*offset);
  578. else
  579. tmp = *(__u32 *)
  580. ((addr_t) &child->thread.fp_regs.fprs + offset);
  581. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  582. /*
  583. * Handle access to the per_info structure.
  584. */
  585. addr -= (addr_t) &dummy32->regs.per_info;
  586. tmp = __peek_user_per_compat(child, addr);
  587. } else
  588. tmp = 0;
  589. return tmp;
  590. }
  591. static int peek_user_compat(struct task_struct *child,
  592. addr_t addr, addr_t data)
  593. {
  594. __u32 tmp;
  595. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
  596. return -EIO;
  597. tmp = __peek_user_compat(child, addr);
  598. return put_user(tmp, (__u32 __user *) data);
  599. }
  600. /*
  601. * Same as poke_user_per but for a 31 bit program.
  602. */
  603. static inline void __poke_user_per_compat(struct task_struct *child,
  604. addr_t addr, __u32 data)
  605. {
  606. struct compat_per_struct_kernel *dummy32 = NULL;
  607. if (addr == (addr_t) &dummy32->cr9)
  608. /* PER event mask of the user specified per set. */
  609. child->thread.per_user.control =
  610. data & (PER_EVENT_MASK | PER_CONTROL_MASK);
  611. else if (addr == (addr_t) &dummy32->starting_addr)
  612. /* Starting address of the user specified per set. */
  613. child->thread.per_user.start = data;
  614. else if (addr == (addr_t) &dummy32->ending_addr)
  615. /* Ending address of the user specified per set. */
  616. child->thread.per_user.end = data;
  617. }
  618. /*
  619. * Same as poke_user but for a 31 bit program.
  620. */
  621. static int __poke_user_compat(struct task_struct *child,
  622. addr_t addr, addr_t data)
  623. {
  624. struct compat_user *dummy32 = NULL;
  625. __u32 tmp = (__u32) data;
  626. addr_t offset;
  627. if (addr < (addr_t) &dummy32->regs.acrs) {
  628. struct pt_regs *regs = task_pt_regs(child);
  629. /*
  630. * psw, gprs, acrs and orig_gpr2 are stored on the stack
  631. */
  632. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  633. __u32 mask = PSW32_MASK_USER;
  634. mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
  635. /* Build a 64 bit psw mask from 31 bit mask. */
  636. if ((tmp ^ PSW32_USER_BITS) & ~mask)
  637. /* Invalid psw mask. */
  638. return -EINVAL;
  639. if ((data & PSW32_MASK_ASC) == PSW32_ASC_HOME)
  640. /* Invalid address-space-control bits */
  641. return -EINVAL;
  642. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
  643. (regs->psw.mask & PSW_MASK_BA) |
  644. (__u64)(tmp & mask) << 32;
  645. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  646. /* Build a 64 bit psw address from 31 bit address. */
  647. regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
  648. /* Transfer 31 bit amode bit to psw mask. */
  649. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
  650. (__u64)(tmp & PSW32_ADDR_AMODE);
  651. } else {
  652. /* gpr 0-15 */
  653. *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
  654. }
  655. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  656. /*
  657. * access registers are stored in the thread structure
  658. */
  659. offset = addr - (addr_t) &dummy32->regs.acrs;
  660. *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
  661. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  662. /*
  663. * orig_gpr2 is stored on the kernel stack
  664. */
  665. *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
  666. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  667. /*
  668. * prevent writess of padding hole between
  669. * orig_gpr2 and fp_regs on s390.
  670. */
  671. return 0;
  672. } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
  673. /*
  674. * floating point control reg. is in the thread structure
  675. */
  676. if (test_fp_ctl(tmp))
  677. return -EINVAL;
  678. child->thread.fp_regs.fpc = data;
  679. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  680. /*
  681. * floating point regs. are either in child->thread.fp_regs
  682. * or the child->thread.vxrs array
  683. */
  684. offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
  685. if (child->thread.vxrs)
  686. *(__u32 *)((addr_t)
  687. child->thread.vxrs + 2*offset) = tmp;
  688. else
  689. *(__u32 *)((addr_t)
  690. &child->thread.fp_regs.fprs + offset) = tmp;
  691. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  692. /*
  693. * Handle access to the per_info structure.
  694. */
  695. addr -= (addr_t) &dummy32->regs.per_info;
  696. __poke_user_per_compat(child, addr, data);
  697. }
  698. return 0;
  699. }
  700. static int poke_user_compat(struct task_struct *child,
  701. addr_t addr, addr_t data)
  702. {
  703. if (!is_compat_task() || (addr & 3) ||
  704. addr > sizeof(struct compat_user) - 3)
  705. return -EIO;
  706. return __poke_user_compat(child, addr, data);
  707. }
  708. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  709. compat_ulong_t caddr, compat_ulong_t cdata)
  710. {
  711. unsigned long addr = caddr;
  712. unsigned long data = cdata;
  713. compat_ptrace_area parea;
  714. int copied, ret;
  715. switch (request) {
  716. case PTRACE_PEEKUSR:
  717. /* read the word at location addr in the USER area. */
  718. return peek_user_compat(child, addr, data);
  719. case PTRACE_POKEUSR:
  720. /* write the word at location addr in the USER area */
  721. return poke_user_compat(child, addr, data);
  722. case PTRACE_PEEKUSR_AREA:
  723. case PTRACE_POKEUSR_AREA:
  724. if (copy_from_user(&parea, (void __force __user *) addr,
  725. sizeof(parea)))
  726. return -EFAULT;
  727. addr = parea.kernel_addr;
  728. data = parea.process_addr;
  729. copied = 0;
  730. while (copied < parea.len) {
  731. if (request == PTRACE_PEEKUSR_AREA)
  732. ret = peek_user_compat(child, addr, data);
  733. else {
  734. __u32 utmp;
  735. if (get_user(utmp,
  736. (__u32 __force __user *) data))
  737. return -EFAULT;
  738. ret = poke_user_compat(child, addr, utmp);
  739. }
  740. if (ret)
  741. return ret;
  742. addr += sizeof(unsigned int);
  743. data += sizeof(unsigned int);
  744. copied += sizeof(unsigned int);
  745. }
  746. return 0;
  747. case PTRACE_GET_LAST_BREAK:
  748. put_user(task_thread_info(child)->last_break,
  749. (unsigned int __user *) data);
  750. return 0;
  751. }
  752. return compat_ptrace_request(child, request, addr, data);
  753. }
  754. #endif
  755. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  756. {
  757. long ret = 0;
  758. /* Do the secure computing check first. */
  759. if (secure_computing()) {
  760. /* seccomp failures shouldn't expose any additional code. */
  761. ret = -1;
  762. goto out;
  763. }
  764. /*
  765. * The sysc_tracesys code in entry.S stored the system
  766. * call number to gprs[2].
  767. */
  768. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  769. (tracehook_report_syscall_entry(regs) ||
  770. regs->gprs[2] >= NR_syscalls)) {
  771. /*
  772. * Tracing decided this syscall should not happen or the
  773. * debugger stored an invalid system call number. Skip
  774. * the system call and the system call restart handling.
  775. */
  776. clear_pt_regs_flag(regs, PIF_SYSCALL);
  777. ret = -1;
  778. }
  779. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  780. trace_sys_enter(regs, regs->gprs[2]);
  781. audit_syscall_entry(regs->gprs[2], regs->orig_gpr2,
  782. regs->gprs[3], regs->gprs[4],
  783. regs->gprs[5]);
  784. out:
  785. return ret ?: regs->gprs[2];
  786. }
  787. asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
  788. {
  789. audit_syscall_exit(regs);
  790. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  791. trace_sys_exit(regs, regs->gprs[2]);
  792. if (test_thread_flag(TIF_SYSCALL_TRACE))
  793. tracehook_report_syscall_exit(regs, 0);
  794. }
  795. /*
  796. * user_regset definitions.
  797. */
  798. static int s390_regs_get(struct task_struct *target,
  799. const struct user_regset *regset,
  800. unsigned int pos, unsigned int count,
  801. void *kbuf, void __user *ubuf)
  802. {
  803. if (target == current)
  804. save_access_regs(target->thread.acrs);
  805. if (kbuf) {
  806. unsigned long *k = kbuf;
  807. while (count > 0) {
  808. *k++ = __peek_user(target, pos);
  809. count -= sizeof(*k);
  810. pos += sizeof(*k);
  811. }
  812. } else {
  813. unsigned long __user *u = ubuf;
  814. while (count > 0) {
  815. if (__put_user(__peek_user(target, pos), u++))
  816. return -EFAULT;
  817. count -= sizeof(*u);
  818. pos += sizeof(*u);
  819. }
  820. }
  821. return 0;
  822. }
  823. static int s390_regs_set(struct task_struct *target,
  824. const struct user_regset *regset,
  825. unsigned int pos, unsigned int count,
  826. const void *kbuf, const void __user *ubuf)
  827. {
  828. int rc = 0;
  829. if (target == current)
  830. save_access_regs(target->thread.acrs);
  831. if (kbuf) {
  832. const unsigned long *k = kbuf;
  833. while (count > 0 && !rc) {
  834. rc = __poke_user(target, pos, *k++);
  835. count -= sizeof(*k);
  836. pos += sizeof(*k);
  837. }
  838. } else {
  839. const unsigned long __user *u = ubuf;
  840. while (count > 0 && !rc) {
  841. unsigned long word;
  842. rc = __get_user(word, u++);
  843. if (rc)
  844. break;
  845. rc = __poke_user(target, pos, word);
  846. count -= sizeof(*u);
  847. pos += sizeof(*u);
  848. }
  849. }
  850. if (rc == 0 && target == current)
  851. restore_access_regs(target->thread.acrs);
  852. return rc;
  853. }
  854. static int s390_fpregs_get(struct task_struct *target,
  855. const struct user_regset *regset, unsigned int pos,
  856. unsigned int count, void *kbuf, void __user *ubuf)
  857. {
  858. if (target == current) {
  859. save_fp_ctl(&target->thread.fp_regs.fpc);
  860. save_fp_regs(target->thread.fp_regs.fprs);
  861. } else if (target->thread.vxrs) {
  862. int i;
  863. for (i = 0; i < __NUM_VXRS_LOW; i++)
  864. target->thread.fp_regs.fprs[i] =
  865. *(freg_t *)(target->thread.vxrs + i);
  866. }
  867. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  868. &target->thread.fp_regs, 0, -1);
  869. }
  870. static int s390_fpregs_set(struct task_struct *target,
  871. const struct user_regset *regset, unsigned int pos,
  872. unsigned int count, const void *kbuf,
  873. const void __user *ubuf)
  874. {
  875. int rc = 0;
  876. if (target == current) {
  877. save_fp_ctl(&target->thread.fp_regs.fpc);
  878. save_fp_regs(target->thread.fp_regs.fprs);
  879. }
  880. /* If setting FPC, must validate it first. */
  881. if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
  882. u32 ufpc[2] = { target->thread.fp_regs.fpc, 0 };
  883. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
  884. 0, offsetof(s390_fp_regs, fprs));
  885. if (rc)
  886. return rc;
  887. if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
  888. return -EINVAL;
  889. target->thread.fp_regs.fpc = ufpc[0];
  890. }
  891. if (rc == 0 && count > 0)
  892. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  893. target->thread.fp_regs.fprs,
  894. offsetof(s390_fp_regs, fprs), -1);
  895. if (rc == 0) {
  896. if (target == current) {
  897. restore_fp_ctl(&target->thread.fp_regs.fpc);
  898. restore_fp_regs(target->thread.fp_regs.fprs);
  899. } else if (target->thread.vxrs) {
  900. int i;
  901. for (i = 0; i < __NUM_VXRS_LOW; i++)
  902. *(freg_t *)(target->thread.vxrs + i) =
  903. target->thread.fp_regs.fprs[i];
  904. }
  905. }
  906. return rc;
  907. }
  908. static int s390_last_break_get(struct task_struct *target,
  909. const struct user_regset *regset,
  910. unsigned int pos, unsigned int count,
  911. void *kbuf, void __user *ubuf)
  912. {
  913. if (count > 0) {
  914. if (kbuf) {
  915. unsigned long *k = kbuf;
  916. *k = task_thread_info(target)->last_break;
  917. } else {
  918. unsigned long __user *u = ubuf;
  919. if (__put_user(task_thread_info(target)->last_break, u))
  920. return -EFAULT;
  921. }
  922. }
  923. return 0;
  924. }
  925. static int s390_last_break_set(struct task_struct *target,
  926. const struct user_regset *regset,
  927. unsigned int pos, unsigned int count,
  928. const void *kbuf, const void __user *ubuf)
  929. {
  930. return 0;
  931. }
  932. static int s390_tdb_get(struct task_struct *target,
  933. const struct user_regset *regset,
  934. unsigned int pos, unsigned int count,
  935. void *kbuf, void __user *ubuf)
  936. {
  937. struct pt_regs *regs = task_pt_regs(target);
  938. unsigned char *data;
  939. if (!(regs->int_code & 0x200))
  940. return -ENODATA;
  941. data = target->thread.trap_tdb;
  942. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
  943. }
  944. static int s390_tdb_set(struct task_struct *target,
  945. const struct user_regset *regset,
  946. unsigned int pos, unsigned int count,
  947. const void *kbuf, const void __user *ubuf)
  948. {
  949. return 0;
  950. }
  951. static int s390_vxrs_low_get(struct task_struct *target,
  952. const struct user_regset *regset,
  953. unsigned int pos, unsigned int count,
  954. void *kbuf, void __user *ubuf)
  955. {
  956. __u64 vxrs[__NUM_VXRS_LOW];
  957. int i;
  958. if (!MACHINE_HAS_VX)
  959. return -ENODEV;
  960. if (target->thread.vxrs) {
  961. if (target == current)
  962. save_vx_regs(target->thread.vxrs);
  963. for (i = 0; i < __NUM_VXRS_LOW; i++)
  964. vxrs[i] = *((__u64 *)(target->thread.vxrs + i) + 1);
  965. } else
  966. memset(vxrs, 0, sizeof(vxrs));
  967. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  968. }
  969. static int s390_vxrs_low_set(struct task_struct *target,
  970. const struct user_regset *regset,
  971. unsigned int pos, unsigned int count,
  972. const void *kbuf, const void __user *ubuf)
  973. {
  974. __u64 vxrs[__NUM_VXRS_LOW];
  975. int i, rc;
  976. if (!MACHINE_HAS_VX)
  977. return -ENODEV;
  978. if (!target->thread.vxrs) {
  979. rc = alloc_vector_registers(target);
  980. if (rc)
  981. return rc;
  982. } else if (target == current)
  983. save_vx_regs(target->thread.vxrs);
  984. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  985. if (rc == 0) {
  986. for (i = 0; i < __NUM_VXRS_LOW; i++)
  987. *((__u64 *)(target->thread.vxrs + i) + 1) = vxrs[i];
  988. if (target == current)
  989. restore_vx_regs(target->thread.vxrs);
  990. }
  991. return rc;
  992. }
  993. static int s390_vxrs_high_get(struct task_struct *target,
  994. const struct user_regset *regset,
  995. unsigned int pos, unsigned int count,
  996. void *kbuf, void __user *ubuf)
  997. {
  998. __vector128 vxrs[__NUM_VXRS_HIGH];
  999. if (!MACHINE_HAS_VX)
  1000. return -ENODEV;
  1001. if (target->thread.vxrs) {
  1002. if (target == current)
  1003. save_vx_regs(target->thread.vxrs);
  1004. memcpy(vxrs, target->thread.vxrs + __NUM_VXRS_LOW,
  1005. sizeof(vxrs));
  1006. } else
  1007. memset(vxrs, 0, sizeof(vxrs));
  1008. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  1009. }
  1010. static int s390_vxrs_high_set(struct task_struct *target,
  1011. const struct user_regset *regset,
  1012. unsigned int pos, unsigned int count,
  1013. const void *kbuf, const void __user *ubuf)
  1014. {
  1015. int rc;
  1016. if (!MACHINE_HAS_VX)
  1017. return -ENODEV;
  1018. if (!target->thread.vxrs) {
  1019. rc = alloc_vector_registers(target);
  1020. if (rc)
  1021. return rc;
  1022. } else if (target == current)
  1023. save_vx_regs(target->thread.vxrs);
  1024. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1025. target->thread.vxrs + __NUM_VXRS_LOW, 0, -1);
  1026. if (rc == 0 && target == current)
  1027. restore_vx_regs(target->thread.vxrs);
  1028. return rc;
  1029. }
  1030. static int s390_system_call_get(struct task_struct *target,
  1031. const struct user_regset *regset,
  1032. unsigned int pos, unsigned int count,
  1033. void *kbuf, void __user *ubuf)
  1034. {
  1035. unsigned int *data = &task_thread_info(target)->system_call;
  1036. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1037. data, 0, sizeof(unsigned int));
  1038. }
  1039. static int s390_system_call_set(struct task_struct *target,
  1040. const struct user_regset *regset,
  1041. unsigned int pos, unsigned int count,
  1042. const void *kbuf, const void __user *ubuf)
  1043. {
  1044. unsigned int *data = &task_thread_info(target)->system_call;
  1045. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1046. data, 0, sizeof(unsigned int));
  1047. }
  1048. static const struct user_regset s390_regsets[] = {
  1049. {
  1050. .core_note_type = NT_PRSTATUS,
  1051. .n = sizeof(s390_regs) / sizeof(long),
  1052. .size = sizeof(long),
  1053. .align = sizeof(long),
  1054. .get = s390_regs_get,
  1055. .set = s390_regs_set,
  1056. },
  1057. {
  1058. .core_note_type = NT_PRFPREG,
  1059. .n = sizeof(s390_fp_regs) / sizeof(long),
  1060. .size = sizeof(long),
  1061. .align = sizeof(long),
  1062. .get = s390_fpregs_get,
  1063. .set = s390_fpregs_set,
  1064. },
  1065. {
  1066. .core_note_type = NT_S390_SYSTEM_CALL,
  1067. .n = 1,
  1068. .size = sizeof(unsigned int),
  1069. .align = sizeof(unsigned int),
  1070. .get = s390_system_call_get,
  1071. .set = s390_system_call_set,
  1072. },
  1073. {
  1074. .core_note_type = NT_S390_LAST_BREAK,
  1075. .n = 1,
  1076. .size = sizeof(long),
  1077. .align = sizeof(long),
  1078. .get = s390_last_break_get,
  1079. .set = s390_last_break_set,
  1080. },
  1081. {
  1082. .core_note_type = NT_S390_TDB,
  1083. .n = 1,
  1084. .size = 256,
  1085. .align = 1,
  1086. .get = s390_tdb_get,
  1087. .set = s390_tdb_set,
  1088. },
  1089. {
  1090. .core_note_type = NT_S390_VXRS_LOW,
  1091. .n = __NUM_VXRS_LOW,
  1092. .size = sizeof(__u64),
  1093. .align = sizeof(__u64),
  1094. .get = s390_vxrs_low_get,
  1095. .set = s390_vxrs_low_set,
  1096. },
  1097. {
  1098. .core_note_type = NT_S390_VXRS_HIGH,
  1099. .n = __NUM_VXRS_HIGH,
  1100. .size = sizeof(__vector128),
  1101. .align = sizeof(__vector128),
  1102. .get = s390_vxrs_high_get,
  1103. .set = s390_vxrs_high_set,
  1104. },
  1105. };
  1106. static const struct user_regset_view user_s390_view = {
  1107. .name = UTS_MACHINE,
  1108. .e_machine = EM_S390,
  1109. .regsets = s390_regsets,
  1110. .n = ARRAY_SIZE(s390_regsets)
  1111. };
  1112. #ifdef CONFIG_COMPAT
  1113. static int s390_compat_regs_get(struct task_struct *target,
  1114. const struct user_regset *regset,
  1115. unsigned int pos, unsigned int count,
  1116. void *kbuf, void __user *ubuf)
  1117. {
  1118. if (target == current)
  1119. save_access_regs(target->thread.acrs);
  1120. if (kbuf) {
  1121. compat_ulong_t *k = kbuf;
  1122. while (count > 0) {
  1123. *k++ = __peek_user_compat(target, pos);
  1124. count -= sizeof(*k);
  1125. pos += sizeof(*k);
  1126. }
  1127. } else {
  1128. compat_ulong_t __user *u = ubuf;
  1129. while (count > 0) {
  1130. if (__put_user(__peek_user_compat(target, pos), u++))
  1131. return -EFAULT;
  1132. count -= sizeof(*u);
  1133. pos += sizeof(*u);
  1134. }
  1135. }
  1136. return 0;
  1137. }
  1138. static int s390_compat_regs_set(struct task_struct *target,
  1139. const struct user_regset *regset,
  1140. unsigned int pos, unsigned int count,
  1141. const void *kbuf, const void __user *ubuf)
  1142. {
  1143. int rc = 0;
  1144. if (target == current)
  1145. save_access_regs(target->thread.acrs);
  1146. if (kbuf) {
  1147. const compat_ulong_t *k = kbuf;
  1148. while (count > 0 && !rc) {
  1149. rc = __poke_user_compat(target, pos, *k++);
  1150. count -= sizeof(*k);
  1151. pos += sizeof(*k);
  1152. }
  1153. } else {
  1154. const compat_ulong_t __user *u = ubuf;
  1155. while (count > 0 && !rc) {
  1156. compat_ulong_t word;
  1157. rc = __get_user(word, u++);
  1158. if (rc)
  1159. break;
  1160. rc = __poke_user_compat(target, pos, word);
  1161. count -= sizeof(*u);
  1162. pos += sizeof(*u);
  1163. }
  1164. }
  1165. if (rc == 0 && target == current)
  1166. restore_access_regs(target->thread.acrs);
  1167. return rc;
  1168. }
  1169. static int s390_compat_regs_high_get(struct task_struct *target,
  1170. const struct user_regset *regset,
  1171. unsigned int pos, unsigned int count,
  1172. void *kbuf, void __user *ubuf)
  1173. {
  1174. compat_ulong_t *gprs_high;
  1175. gprs_high = (compat_ulong_t *)
  1176. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  1177. if (kbuf) {
  1178. compat_ulong_t *k = kbuf;
  1179. while (count > 0) {
  1180. *k++ = *gprs_high;
  1181. gprs_high += 2;
  1182. count -= sizeof(*k);
  1183. }
  1184. } else {
  1185. compat_ulong_t __user *u = ubuf;
  1186. while (count > 0) {
  1187. if (__put_user(*gprs_high, u++))
  1188. return -EFAULT;
  1189. gprs_high += 2;
  1190. count -= sizeof(*u);
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. static int s390_compat_regs_high_set(struct task_struct *target,
  1196. const struct user_regset *regset,
  1197. unsigned int pos, unsigned int count,
  1198. const void *kbuf, const void __user *ubuf)
  1199. {
  1200. compat_ulong_t *gprs_high;
  1201. int rc = 0;
  1202. gprs_high = (compat_ulong_t *)
  1203. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  1204. if (kbuf) {
  1205. const compat_ulong_t *k = kbuf;
  1206. while (count > 0) {
  1207. *gprs_high = *k++;
  1208. *gprs_high += 2;
  1209. count -= sizeof(*k);
  1210. }
  1211. } else {
  1212. const compat_ulong_t __user *u = ubuf;
  1213. while (count > 0 && !rc) {
  1214. unsigned long word;
  1215. rc = __get_user(word, u++);
  1216. if (rc)
  1217. break;
  1218. *gprs_high = word;
  1219. *gprs_high += 2;
  1220. count -= sizeof(*u);
  1221. }
  1222. }
  1223. return rc;
  1224. }
  1225. static int s390_compat_last_break_get(struct task_struct *target,
  1226. const struct user_regset *regset,
  1227. unsigned int pos, unsigned int count,
  1228. void *kbuf, void __user *ubuf)
  1229. {
  1230. compat_ulong_t last_break;
  1231. if (count > 0) {
  1232. last_break = task_thread_info(target)->last_break;
  1233. if (kbuf) {
  1234. unsigned long *k = kbuf;
  1235. *k = last_break;
  1236. } else {
  1237. unsigned long __user *u = ubuf;
  1238. if (__put_user(last_break, u))
  1239. return -EFAULT;
  1240. }
  1241. }
  1242. return 0;
  1243. }
  1244. static int s390_compat_last_break_set(struct task_struct *target,
  1245. const struct user_regset *regset,
  1246. unsigned int pos, unsigned int count,
  1247. const void *kbuf, const void __user *ubuf)
  1248. {
  1249. return 0;
  1250. }
  1251. static const struct user_regset s390_compat_regsets[] = {
  1252. {
  1253. .core_note_type = NT_PRSTATUS,
  1254. .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
  1255. .size = sizeof(compat_long_t),
  1256. .align = sizeof(compat_long_t),
  1257. .get = s390_compat_regs_get,
  1258. .set = s390_compat_regs_set,
  1259. },
  1260. {
  1261. .core_note_type = NT_PRFPREG,
  1262. .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
  1263. .size = sizeof(compat_long_t),
  1264. .align = sizeof(compat_long_t),
  1265. .get = s390_fpregs_get,
  1266. .set = s390_fpregs_set,
  1267. },
  1268. {
  1269. .core_note_type = NT_S390_SYSTEM_CALL,
  1270. .n = 1,
  1271. .size = sizeof(compat_uint_t),
  1272. .align = sizeof(compat_uint_t),
  1273. .get = s390_system_call_get,
  1274. .set = s390_system_call_set,
  1275. },
  1276. {
  1277. .core_note_type = NT_S390_LAST_BREAK,
  1278. .n = 1,
  1279. .size = sizeof(long),
  1280. .align = sizeof(long),
  1281. .get = s390_compat_last_break_get,
  1282. .set = s390_compat_last_break_set,
  1283. },
  1284. {
  1285. .core_note_type = NT_S390_TDB,
  1286. .n = 1,
  1287. .size = 256,
  1288. .align = 1,
  1289. .get = s390_tdb_get,
  1290. .set = s390_tdb_set,
  1291. },
  1292. {
  1293. .core_note_type = NT_S390_VXRS_LOW,
  1294. .n = __NUM_VXRS_LOW,
  1295. .size = sizeof(__u64),
  1296. .align = sizeof(__u64),
  1297. .get = s390_vxrs_low_get,
  1298. .set = s390_vxrs_low_set,
  1299. },
  1300. {
  1301. .core_note_type = NT_S390_VXRS_HIGH,
  1302. .n = __NUM_VXRS_HIGH,
  1303. .size = sizeof(__vector128),
  1304. .align = sizeof(__vector128),
  1305. .get = s390_vxrs_high_get,
  1306. .set = s390_vxrs_high_set,
  1307. },
  1308. {
  1309. .core_note_type = NT_S390_HIGH_GPRS,
  1310. .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
  1311. .size = sizeof(compat_long_t),
  1312. .align = sizeof(compat_long_t),
  1313. .get = s390_compat_regs_high_get,
  1314. .set = s390_compat_regs_high_set,
  1315. },
  1316. };
  1317. static const struct user_regset_view user_s390_compat_view = {
  1318. .name = "s390",
  1319. .e_machine = EM_S390,
  1320. .regsets = s390_compat_regsets,
  1321. .n = ARRAY_SIZE(s390_compat_regsets)
  1322. };
  1323. #endif
  1324. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1325. {
  1326. #ifdef CONFIG_COMPAT
  1327. if (test_tsk_thread_flag(task, TIF_31BIT))
  1328. return &user_s390_compat_view;
  1329. #endif
  1330. return &user_s390_view;
  1331. }
  1332. static const char *gpr_names[NUM_GPRS] = {
  1333. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  1334. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  1335. };
  1336. unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
  1337. {
  1338. if (offset >= NUM_GPRS)
  1339. return 0;
  1340. return regs->gprs[offset];
  1341. }
  1342. int regs_query_register_offset(const char *name)
  1343. {
  1344. unsigned long offset;
  1345. if (!name || *name != 'r')
  1346. return -EINVAL;
  1347. if (kstrtoul(name + 1, 10, &offset))
  1348. return -EINVAL;
  1349. if (offset >= NUM_GPRS)
  1350. return -EINVAL;
  1351. return offset;
  1352. }
  1353. const char *regs_query_register_name(unsigned int offset)
  1354. {
  1355. if (offset >= NUM_GPRS)
  1356. return NULL;
  1357. return gpr_names[offset];
  1358. }
  1359. static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  1360. {
  1361. unsigned long ksp = kernel_stack_pointer(regs);
  1362. return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
  1363. }
  1364. /**
  1365. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  1366. * @regs:pt_regs which contains kernel stack pointer.
  1367. * @n:stack entry number.
  1368. *
  1369. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  1370. * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
  1371. * this returns 0.
  1372. */
  1373. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  1374. {
  1375. unsigned long addr;
  1376. addr = kernel_stack_pointer(regs) + n * sizeof(long);
  1377. if (!regs_within_kernel_stack(regs, addr))
  1378. return 0;
  1379. return *(unsigned long *)addr;
  1380. }