ptrace.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * arch/s390/kernel/ptrace.c
  3. *
  4. * S390 version
  5. * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. *
  9. * Based on PowerPC version
  10. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  11. *
  12. * Derived from "arch/m68k/kernel/ptrace.c"
  13. * Copyright (C) 1994 by Hamish Macdonald
  14. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  15. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  16. *
  17. * Modified by Cort Dougan (cort@cs.nmt.edu)
  18. *
  19. *
  20. * This file is subject to the terms and conditions of the GNU General
  21. * Public License. See the file README.legal in the main directory of
  22. * this archive for more details.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/mm.h>
  27. #include <linux/smp.h>
  28. #include <linux/errno.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/user.h>
  31. #include <linux/security.h>
  32. #include <linux/audit.h>
  33. #include <linux/signal.h>
  34. #include <linux/elf.h>
  35. #include <linux/regset.h>
  36. #include <linux/tracehook.h>
  37. #include <linux/seccomp.h>
  38. #include <trace/syscall.h>
  39. #include <asm/compat.h>
  40. #include <asm/segment.h>
  41. #include <asm/page.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/system.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/unistd.h>
  47. #include "entry.h"
  48. #ifdef CONFIG_COMPAT
  49. #include "compat_ptrace.h"
  50. #endif
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/syscalls.h>
  53. enum s390_regset {
  54. REGSET_GENERAL,
  55. REGSET_FP,
  56. };
  57. static void
  58. FixPerRegisters(struct task_struct *task)
  59. {
  60. struct pt_regs *regs;
  61. per_struct *per_info;
  62. regs = task_pt_regs(task);
  63. per_info = (per_struct *) &task->thread.per_info;
  64. per_info->control_regs.bits.em_instruction_fetch =
  65. per_info->single_step | per_info->instruction_fetch;
  66. if (per_info->single_step) {
  67. per_info->control_regs.bits.starting_addr = 0;
  68. #ifdef CONFIG_COMPAT
  69. if (is_compat_task())
  70. per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
  71. else
  72. #endif
  73. per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
  74. } else {
  75. per_info->control_regs.bits.starting_addr =
  76. per_info->starting_addr;
  77. per_info->control_regs.bits.ending_addr =
  78. per_info->ending_addr;
  79. }
  80. /*
  81. * if any of the control reg tracing bits are on
  82. * we switch on per in the psw
  83. */
  84. if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
  85. regs->psw.mask |= PSW_MASK_PER;
  86. else
  87. regs->psw.mask &= ~PSW_MASK_PER;
  88. if (per_info->control_regs.bits.em_storage_alteration)
  89. per_info->control_regs.bits.storage_alt_space_ctl = 1;
  90. else
  91. per_info->control_regs.bits.storage_alt_space_ctl = 0;
  92. }
  93. void user_enable_single_step(struct task_struct *task)
  94. {
  95. task->thread.per_info.single_step = 1;
  96. FixPerRegisters(task);
  97. }
  98. void user_disable_single_step(struct task_struct *task)
  99. {
  100. task->thread.per_info.single_step = 0;
  101. FixPerRegisters(task);
  102. }
  103. /*
  104. * Called by kernel/ptrace.c when detaching..
  105. *
  106. * Make sure single step bits etc are not set.
  107. */
  108. void
  109. ptrace_disable(struct task_struct *child)
  110. {
  111. /* make sure the single step bit is not set. */
  112. user_disable_single_step(child);
  113. }
  114. #ifndef CONFIG_64BIT
  115. # define __ADDR_MASK 3
  116. #else
  117. # define __ADDR_MASK 7
  118. #endif
  119. /*
  120. * Read the word at offset addr from the user area of a process. The
  121. * trouble here is that the information is littered over different
  122. * locations. The process registers are found on the kernel stack,
  123. * the floating point stuff and the trace settings are stored in
  124. * the task structure. In addition the different structures in
  125. * struct user contain pad bytes that should be read as zeroes.
  126. * Lovely...
  127. */
  128. static unsigned long __peek_user(struct task_struct *child, addr_t addr)
  129. {
  130. struct user *dummy = NULL;
  131. addr_t offset, tmp;
  132. if (addr < (addr_t) &dummy->regs.acrs) {
  133. /*
  134. * psw and gprs are stored on the stack
  135. */
  136. tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
  137. if (addr == (addr_t) &dummy->regs.psw.mask)
  138. /* Remove per bit from user psw. */
  139. tmp &= ~PSW_MASK_PER;
  140. } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
  141. /*
  142. * access registers are stored in the thread structure
  143. */
  144. offset = addr - (addr_t) &dummy->regs.acrs;
  145. #ifdef CONFIG_64BIT
  146. /*
  147. * Very special case: old & broken 64 bit gdb reading
  148. * from acrs[15]. Result is a 64 bit value. Read the
  149. * 32 bit acrs[15] value and shift it by 32. Sick...
  150. */
  151. if (addr == (addr_t) &dummy->regs.acrs[15])
  152. tmp = ((unsigned long) child->thread.acrs[15]) << 32;
  153. else
  154. #endif
  155. tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
  156. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  157. /*
  158. * orig_gpr2 is stored on the kernel stack
  159. */
  160. tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
  161. } else if (addr < (addr_t) &dummy->regs.fp_regs) {
  162. /*
  163. * prevent reads of padding hole between
  164. * orig_gpr2 and fp_regs on s390.
  165. */
  166. tmp = 0;
  167. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  168. /*
  169. * floating point regs. are stored in the thread structure
  170. */
  171. offset = addr - (addr_t) &dummy->regs.fp_regs;
  172. tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
  173. if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
  174. tmp &= (unsigned long) FPC_VALID_MASK
  175. << (BITS_PER_LONG - 32);
  176. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  177. /*
  178. * per_info is found in the thread structure
  179. */
  180. offset = addr - (addr_t) &dummy->regs.per_info;
  181. tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
  182. } else
  183. tmp = 0;
  184. return tmp;
  185. }
  186. static int
  187. peek_user(struct task_struct *child, addr_t addr, addr_t data)
  188. {
  189. addr_t tmp, mask;
  190. /*
  191. * Stupid gdb peeks/pokes the access registers in 64 bit with
  192. * an alignment of 4. Programmers from hell...
  193. */
  194. mask = __ADDR_MASK;
  195. #ifdef CONFIG_64BIT
  196. if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
  197. addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
  198. mask = 3;
  199. #endif
  200. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  201. return -EIO;
  202. tmp = __peek_user(child, addr);
  203. return put_user(tmp, (addr_t __user *) data);
  204. }
  205. /*
  206. * Write a word to the user area of a process at location addr. This
  207. * operation does have an additional problem compared to peek_user.
  208. * Stores to the program status word and on the floating point
  209. * control register needs to get checked for validity.
  210. */
  211. static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
  212. {
  213. struct user *dummy = NULL;
  214. addr_t offset;
  215. if (addr < (addr_t) &dummy->regs.acrs) {
  216. /*
  217. * psw and gprs are stored on the stack
  218. */
  219. if (addr == (addr_t) &dummy->regs.psw.mask &&
  220. #ifdef CONFIG_COMPAT
  221. data != PSW_MASK_MERGE(psw_user32_bits, data) &&
  222. #endif
  223. data != PSW_MASK_MERGE(psw_user_bits, data))
  224. /* Invalid psw mask. */
  225. return -EINVAL;
  226. #ifndef CONFIG_64BIT
  227. if (addr == (addr_t) &dummy->regs.psw.addr)
  228. /* I'd like to reject addresses without the
  229. high order bit but older gdb's rely on it */
  230. data |= PSW_ADDR_AMODE;
  231. #endif
  232. *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
  233. } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
  234. /*
  235. * access registers are stored in the thread structure
  236. */
  237. offset = addr - (addr_t) &dummy->regs.acrs;
  238. #ifdef CONFIG_64BIT
  239. /*
  240. * Very special case: old & broken 64 bit gdb writing
  241. * to acrs[15] with a 64 bit value. Ignore the lower
  242. * half of the value and write the upper 32 bit to
  243. * acrs[15]. Sick...
  244. */
  245. if (addr == (addr_t) &dummy->regs.acrs[15])
  246. child->thread.acrs[15] = (unsigned int) (data >> 32);
  247. else
  248. #endif
  249. *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
  250. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  251. /*
  252. * orig_gpr2 is stored on the kernel stack
  253. */
  254. task_pt_regs(child)->orig_gpr2 = data;
  255. } else if (addr < (addr_t) &dummy->regs.fp_regs) {
  256. /*
  257. * prevent writes of padding hole between
  258. * orig_gpr2 and fp_regs on s390.
  259. */
  260. return 0;
  261. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  262. /*
  263. * floating point regs. are stored in the thread structure
  264. */
  265. if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
  266. (data & ~((unsigned long) FPC_VALID_MASK
  267. << (BITS_PER_LONG - 32))) != 0)
  268. return -EINVAL;
  269. offset = addr - (addr_t) &dummy->regs.fp_regs;
  270. *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
  271. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  272. /*
  273. * per_info is found in the thread structure
  274. */
  275. offset = addr - (addr_t) &dummy->regs.per_info;
  276. *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
  277. }
  278. FixPerRegisters(child);
  279. return 0;
  280. }
  281. static int
  282. poke_user(struct task_struct *child, addr_t addr, addr_t data)
  283. {
  284. addr_t mask;
  285. /*
  286. * Stupid gdb peeks/pokes the access registers in 64 bit with
  287. * an alignment of 4. Programmers from hell indeed...
  288. */
  289. mask = __ADDR_MASK;
  290. #ifdef CONFIG_64BIT
  291. if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
  292. addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
  293. mask = 3;
  294. #endif
  295. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  296. return -EIO;
  297. return __poke_user(child, addr, data);
  298. }
  299. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  300. {
  301. ptrace_area parea;
  302. int copied, ret;
  303. switch (request) {
  304. case PTRACE_PEEKUSR:
  305. /* read the word at location addr in the USER area. */
  306. return peek_user(child, addr, data);
  307. case PTRACE_POKEUSR:
  308. /* write the word at location addr in the USER area */
  309. return poke_user(child, addr, data);
  310. case PTRACE_PEEKUSR_AREA:
  311. case PTRACE_POKEUSR_AREA:
  312. if (copy_from_user(&parea, (void __force __user *) addr,
  313. sizeof(parea)))
  314. return -EFAULT;
  315. addr = parea.kernel_addr;
  316. data = parea.process_addr;
  317. copied = 0;
  318. while (copied < parea.len) {
  319. if (request == PTRACE_PEEKUSR_AREA)
  320. ret = peek_user(child, addr, data);
  321. else {
  322. addr_t utmp;
  323. if (get_user(utmp,
  324. (addr_t __force __user *) data))
  325. return -EFAULT;
  326. ret = poke_user(child, addr, utmp);
  327. }
  328. if (ret)
  329. return ret;
  330. addr += sizeof(unsigned long);
  331. data += sizeof(unsigned long);
  332. copied += sizeof(unsigned long);
  333. }
  334. return 0;
  335. default:
  336. /* Removing high order bit from addr (only for 31 bit). */
  337. addr &= PSW_ADDR_INSN;
  338. return ptrace_request(child, request, addr, data);
  339. }
  340. }
  341. #ifdef CONFIG_COMPAT
  342. /*
  343. * Now the fun part starts... a 31 bit program running in the
  344. * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
  345. * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
  346. * to handle, the difference to the 64 bit versions of the requests
  347. * is that the access is done in multiples of 4 byte instead of
  348. * 8 bytes (sizeof(unsigned long) on 31/64 bit).
  349. * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
  350. * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
  351. * is a 31 bit program too, the content of struct user can be
  352. * emulated. A 31 bit program peeking into the struct user of
  353. * a 64 bit program is a no-no.
  354. */
  355. /*
  356. * Same as peek_user but for a 31 bit program.
  357. */
  358. static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
  359. {
  360. struct user32 *dummy32 = NULL;
  361. per_struct32 *dummy_per32 = NULL;
  362. addr_t offset;
  363. __u32 tmp;
  364. if (addr < (addr_t) &dummy32->regs.acrs) {
  365. /*
  366. * psw and gprs are stored on the stack
  367. */
  368. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  369. /* Fake a 31 bit psw mask. */
  370. tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
  371. tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
  372. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  373. /* Fake a 31 bit psw address. */
  374. tmp = (__u32) task_pt_regs(child)->psw.addr |
  375. PSW32_ADDR_AMODE31;
  376. } else {
  377. /* gpr 0-15 */
  378. tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
  379. addr*2 + 4);
  380. }
  381. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  382. /*
  383. * access registers are stored in the thread structure
  384. */
  385. offset = addr - (addr_t) &dummy32->regs.acrs;
  386. tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
  387. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  388. /*
  389. * orig_gpr2 is stored on the kernel stack
  390. */
  391. tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
  392. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  393. /*
  394. * prevent reads of padding hole between
  395. * orig_gpr2 and fp_regs on s390.
  396. */
  397. tmp = 0;
  398. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  399. /*
  400. * floating point regs. are stored in the thread structure
  401. */
  402. offset = addr - (addr_t) &dummy32->regs.fp_regs;
  403. tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
  404. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  405. /*
  406. * per_info is found in the thread structure
  407. */
  408. offset = addr - (addr_t) &dummy32->regs.per_info;
  409. /* This is magic. See per_struct and per_struct32. */
  410. if ((offset >= (addr_t) &dummy_per32->control_regs &&
  411. offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
  412. (offset >= (addr_t) &dummy_per32->starting_addr &&
  413. offset <= (addr_t) &dummy_per32->ending_addr) ||
  414. offset == (addr_t) &dummy_per32->lowcore.words.address)
  415. offset = offset*2 + 4;
  416. else
  417. offset = offset*2;
  418. tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
  419. } else
  420. tmp = 0;
  421. return tmp;
  422. }
  423. static int peek_user_compat(struct task_struct *child,
  424. addr_t addr, addr_t data)
  425. {
  426. __u32 tmp;
  427. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
  428. return -EIO;
  429. tmp = __peek_user_compat(child, addr);
  430. return put_user(tmp, (__u32 __user *) data);
  431. }
  432. /*
  433. * Same as poke_user but for a 31 bit program.
  434. */
  435. static int __poke_user_compat(struct task_struct *child,
  436. addr_t addr, addr_t data)
  437. {
  438. struct user32 *dummy32 = NULL;
  439. per_struct32 *dummy_per32 = NULL;
  440. __u32 tmp = (__u32) data;
  441. addr_t offset;
  442. if (addr < (addr_t) &dummy32->regs.acrs) {
  443. /*
  444. * psw, gprs, acrs and orig_gpr2 are stored on the stack
  445. */
  446. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  447. /* Build a 64 bit psw mask from 31 bit mask. */
  448. if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
  449. /* Invalid psw mask. */
  450. return -EINVAL;
  451. task_pt_regs(child)->psw.mask =
  452. PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
  453. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  454. /* Build a 64 bit psw address from 31 bit address. */
  455. task_pt_regs(child)->psw.addr =
  456. (__u64) tmp & PSW32_ADDR_INSN;
  457. } else {
  458. /* gpr 0-15 */
  459. *(__u32*)((addr_t) &task_pt_regs(child)->psw
  460. + addr*2 + 4) = tmp;
  461. }
  462. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  463. /*
  464. * access registers are stored in the thread structure
  465. */
  466. offset = addr - (addr_t) &dummy32->regs.acrs;
  467. *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
  468. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  469. /*
  470. * orig_gpr2 is stored on the kernel stack
  471. */
  472. *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
  473. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  474. /*
  475. * prevent writess of padding hole between
  476. * orig_gpr2 and fp_regs on s390.
  477. */
  478. return 0;
  479. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  480. /*
  481. * floating point regs. are stored in the thread structure
  482. */
  483. if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
  484. (tmp & ~FPC_VALID_MASK) != 0)
  485. /* Invalid floating point control. */
  486. return -EINVAL;
  487. offset = addr - (addr_t) &dummy32->regs.fp_regs;
  488. *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
  489. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  490. /*
  491. * per_info is found in the thread structure.
  492. */
  493. offset = addr - (addr_t) &dummy32->regs.per_info;
  494. /*
  495. * This is magic. See per_struct and per_struct32.
  496. * By incident the offsets in per_struct are exactly
  497. * twice the offsets in per_struct32 for all fields.
  498. * The 8 byte fields need special handling though,
  499. * because the second half (bytes 4-7) is needed and
  500. * not the first half.
  501. */
  502. if ((offset >= (addr_t) &dummy_per32->control_regs &&
  503. offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
  504. (offset >= (addr_t) &dummy_per32->starting_addr &&
  505. offset <= (addr_t) &dummy_per32->ending_addr) ||
  506. offset == (addr_t) &dummy_per32->lowcore.words.address)
  507. offset = offset*2 + 4;
  508. else
  509. offset = offset*2;
  510. *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
  511. }
  512. FixPerRegisters(child);
  513. return 0;
  514. }
  515. static int poke_user_compat(struct task_struct *child,
  516. addr_t addr, addr_t data)
  517. {
  518. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
  519. return -EIO;
  520. return __poke_user_compat(child, addr, data);
  521. }
  522. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  523. compat_ulong_t caddr, compat_ulong_t cdata)
  524. {
  525. unsigned long addr = caddr;
  526. unsigned long data = cdata;
  527. ptrace_area_emu31 parea;
  528. int copied, ret;
  529. switch (request) {
  530. case PTRACE_PEEKUSR:
  531. /* read the word at location addr in the USER area. */
  532. return peek_user_compat(child, addr, data);
  533. case PTRACE_POKEUSR:
  534. /* write the word at location addr in the USER area */
  535. return poke_user_compat(child, addr, data);
  536. case PTRACE_PEEKUSR_AREA:
  537. case PTRACE_POKEUSR_AREA:
  538. if (copy_from_user(&parea, (void __force __user *) addr,
  539. sizeof(parea)))
  540. return -EFAULT;
  541. addr = parea.kernel_addr;
  542. data = parea.process_addr;
  543. copied = 0;
  544. while (copied < parea.len) {
  545. if (request == PTRACE_PEEKUSR_AREA)
  546. ret = peek_user_compat(child, addr, data);
  547. else {
  548. __u32 utmp;
  549. if (get_user(utmp,
  550. (__u32 __force __user *) data))
  551. return -EFAULT;
  552. ret = poke_user_compat(child, addr, utmp);
  553. }
  554. if (ret)
  555. return ret;
  556. addr += sizeof(unsigned int);
  557. data += sizeof(unsigned int);
  558. copied += sizeof(unsigned int);
  559. }
  560. return 0;
  561. }
  562. return compat_ptrace_request(child, request, addr, data);
  563. }
  564. #endif
  565. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  566. {
  567. long ret;
  568. /* Do the secure computing check first. */
  569. secure_computing(regs->gprs[2]);
  570. /*
  571. * The sysc_tracesys code in entry.S stored the system
  572. * call number to gprs[2].
  573. */
  574. ret = regs->gprs[2];
  575. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  576. (tracehook_report_syscall_entry(regs) ||
  577. regs->gprs[2] >= NR_syscalls)) {
  578. /*
  579. * Tracing decided this syscall should not happen or the
  580. * debugger stored an invalid system call number. Skip
  581. * the system call and the system call restart handling.
  582. */
  583. regs->svcnr = 0;
  584. ret = -1;
  585. }
  586. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  587. trace_sys_enter(regs, regs->gprs[2]);
  588. if (unlikely(current->audit_context))
  589. audit_syscall_entry(is_compat_task() ?
  590. AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
  591. regs->gprs[2], regs->orig_gpr2,
  592. regs->gprs[3], regs->gprs[4],
  593. regs->gprs[5]);
  594. return ret;
  595. }
  596. asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
  597. {
  598. if (unlikely(current->audit_context))
  599. audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
  600. regs->gprs[2]);
  601. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  602. trace_sys_exit(regs, regs->gprs[2]);
  603. if (test_thread_flag(TIF_SYSCALL_TRACE))
  604. tracehook_report_syscall_exit(regs, 0);
  605. }
  606. /*
  607. * user_regset definitions.
  608. */
  609. static int s390_regs_get(struct task_struct *target,
  610. const struct user_regset *regset,
  611. unsigned int pos, unsigned int count,
  612. void *kbuf, void __user *ubuf)
  613. {
  614. if (target == current)
  615. save_access_regs(target->thread.acrs);
  616. if (kbuf) {
  617. unsigned long *k = kbuf;
  618. while (count > 0) {
  619. *k++ = __peek_user(target, pos);
  620. count -= sizeof(*k);
  621. pos += sizeof(*k);
  622. }
  623. } else {
  624. unsigned long __user *u = ubuf;
  625. while (count > 0) {
  626. if (__put_user(__peek_user(target, pos), u++))
  627. return -EFAULT;
  628. count -= sizeof(*u);
  629. pos += sizeof(*u);
  630. }
  631. }
  632. return 0;
  633. }
  634. static int s390_regs_set(struct task_struct *target,
  635. const struct user_regset *regset,
  636. unsigned int pos, unsigned int count,
  637. const void *kbuf, const void __user *ubuf)
  638. {
  639. int rc = 0;
  640. if (target == current)
  641. save_access_regs(target->thread.acrs);
  642. if (kbuf) {
  643. const unsigned long *k = kbuf;
  644. while (count > 0 && !rc) {
  645. rc = __poke_user(target, pos, *k++);
  646. count -= sizeof(*k);
  647. pos += sizeof(*k);
  648. }
  649. } else {
  650. const unsigned long __user *u = ubuf;
  651. while (count > 0 && !rc) {
  652. unsigned long word;
  653. rc = __get_user(word, u++);
  654. if (rc)
  655. break;
  656. rc = __poke_user(target, pos, word);
  657. count -= sizeof(*u);
  658. pos += sizeof(*u);
  659. }
  660. }
  661. if (rc == 0 && target == current)
  662. restore_access_regs(target->thread.acrs);
  663. return rc;
  664. }
  665. static int s390_fpregs_get(struct task_struct *target,
  666. const struct user_regset *regset, unsigned int pos,
  667. unsigned int count, void *kbuf, void __user *ubuf)
  668. {
  669. if (target == current)
  670. save_fp_regs(&target->thread.fp_regs);
  671. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  672. &target->thread.fp_regs, 0, -1);
  673. }
  674. static int s390_fpregs_set(struct task_struct *target,
  675. const struct user_regset *regset, unsigned int pos,
  676. unsigned int count, const void *kbuf,
  677. const void __user *ubuf)
  678. {
  679. int rc = 0;
  680. if (target == current)
  681. save_fp_regs(&target->thread.fp_regs);
  682. /* If setting FPC, must validate it first. */
  683. if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
  684. u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
  685. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
  686. 0, offsetof(s390_fp_regs, fprs));
  687. if (rc)
  688. return rc;
  689. if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
  690. return -EINVAL;
  691. target->thread.fp_regs.fpc = fpc[0];
  692. }
  693. if (rc == 0 && count > 0)
  694. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  695. target->thread.fp_regs.fprs,
  696. offsetof(s390_fp_regs, fprs), -1);
  697. if (rc == 0 && target == current)
  698. restore_fp_regs(&target->thread.fp_regs);
  699. return rc;
  700. }
  701. static const struct user_regset s390_regsets[] = {
  702. [REGSET_GENERAL] = {
  703. .core_note_type = NT_PRSTATUS,
  704. .n = sizeof(s390_regs) / sizeof(long),
  705. .size = sizeof(long),
  706. .align = sizeof(long),
  707. .get = s390_regs_get,
  708. .set = s390_regs_set,
  709. },
  710. [REGSET_FP] = {
  711. .core_note_type = NT_PRFPREG,
  712. .n = sizeof(s390_fp_regs) / sizeof(long),
  713. .size = sizeof(long),
  714. .align = sizeof(long),
  715. .get = s390_fpregs_get,
  716. .set = s390_fpregs_set,
  717. },
  718. };
  719. static const struct user_regset_view user_s390_view = {
  720. .name = UTS_MACHINE,
  721. .e_machine = EM_S390,
  722. .regsets = s390_regsets,
  723. .n = ARRAY_SIZE(s390_regsets)
  724. };
  725. #ifdef CONFIG_COMPAT
  726. static int s390_compat_regs_get(struct task_struct *target,
  727. const struct user_regset *regset,
  728. unsigned int pos, unsigned int count,
  729. void *kbuf, void __user *ubuf)
  730. {
  731. if (target == current)
  732. save_access_regs(target->thread.acrs);
  733. if (kbuf) {
  734. compat_ulong_t *k = kbuf;
  735. while (count > 0) {
  736. *k++ = __peek_user_compat(target, pos);
  737. count -= sizeof(*k);
  738. pos += sizeof(*k);
  739. }
  740. } else {
  741. compat_ulong_t __user *u = ubuf;
  742. while (count > 0) {
  743. if (__put_user(__peek_user_compat(target, pos), u++))
  744. return -EFAULT;
  745. count -= sizeof(*u);
  746. pos += sizeof(*u);
  747. }
  748. }
  749. return 0;
  750. }
  751. static int s390_compat_regs_set(struct task_struct *target,
  752. const struct user_regset *regset,
  753. unsigned int pos, unsigned int count,
  754. const void *kbuf, const void __user *ubuf)
  755. {
  756. int rc = 0;
  757. if (target == current)
  758. save_access_regs(target->thread.acrs);
  759. if (kbuf) {
  760. const compat_ulong_t *k = kbuf;
  761. while (count > 0 && !rc) {
  762. rc = __poke_user_compat(target, pos, *k++);
  763. count -= sizeof(*k);
  764. pos += sizeof(*k);
  765. }
  766. } else {
  767. const compat_ulong_t __user *u = ubuf;
  768. while (count > 0 && !rc) {
  769. compat_ulong_t word;
  770. rc = __get_user(word, u++);
  771. if (rc)
  772. break;
  773. rc = __poke_user_compat(target, pos, word);
  774. count -= sizeof(*u);
  775. pos += sizeof(*u);
  776. }
  777. }
  778. if (rc == 0 && target == current)
  779. restore_access_regs(target->thread.acrs);
  780. return rc;
  781. }
  782. static const struct user_regset s390_compat_regsets[] = {
  783. [REGSET_GENERAL] = {
  784. .core_note_type = NT_PRSTATUS,
  785. .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
  786. .size = sizeof(compat_long_t),
  787. .align = sizeof(compat_long_t),
  788. .get = s390_compat_regs_get,
  789. .set = s390_compat_regs_set,
  790. },
  791. [REGSET_FP] = {
  792. .core_note_type = NT_PRFPREG,
  793. .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
  794. .size = sizeof(compat_long_t),
  795. .align = sizeof(compat_long_t),
  796. .get = s390_fpregs_get,
  797. .set = s390_fpregs_set,
  798. },
  799. };
  800. static const struct user_regset_view user_s390_compat_view = {
  801. .name = "s390",
  802. .e_machine = EM_S390,
  803. .regsets = s390_compat_regsets,
  804. .n = ARRAY_SIZE(s390_compat_regsets)
  805. };
  806. #endif
  807. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  808. {
  809. #ifdef CONFIG_COMPAT
  810. if (test_tsk_thread_flag(task, TIF_31BIT))
  811. return &user_s390_compat_view;
  812. #endif
  813. return &user_s390_view;
  814. }