ptrace.c 26 KB

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