ptrace.c 43 KB

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