ptrace.c 43 KB

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