ptrace.c 25 KB

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