ptrace.c 24 KB

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