ptrace.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 <asm/segment.h>
  36. #include <asm/page.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/pgalloc.h>
  39. #include <asm/system.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/unistd.h>
  42. #include "entry.h"
  43. #ifdef CONFIG_COMPAT
  44. #include "compat_ptrace.h"
  45. #endif
  46. static void
  47. FixPerRegisters(struct task_struct *task)
  48. {
  49. struct pt_regs *regs;
  50. per_struct *per_info;
  51. regs = task_pt_regs(task);
  52. per_info = (per_struct *) &task->thread.per_info;
  53. per_info->control_regs.bits.em_instruction_fetch =
  54. per_info->single_step | per_info->instruction_fetch;
  55. if (per_info->single_step) {
  56. per_info->control_regs.bits.starting_addr = 0;
  57. #ifdef CONFIG_COMPAT
  58. if (test_thread_flag(TIF_31BIT))
  59. per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
  60. else
  61. #endif
  62. per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
  63. } else {
  64. per_info->control_regs.bits.starting_addr =
  65. per_info->starting_addr;
  66. per_info->control_regs.bits.ending_addr =
  67. per_info->ending_addr;
  68. }
  69. /*
  70. * if any of the control reg tracing bits are on
  71. * we switch on per in the psw
  72. */
  73. if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
  74. regs->psw.mask |= PSW_MASK_PER;
  75. else
  76. regs->psw.mask &= ~PSW_MASK_PER;
  77. if (per_info->control_regs.bits.em_storage_alteration)
  78. per_info->control_regs.bits.storage_alt_space_ctl = 1;
  79. else
  80. per_info->control_regs.bits.storage_alt_space_ctl = 0;
  81. }
  82. void user_enable_single_step(struct task_struct *task)
  83. {
  84. task->thread.per_info.single_step = 1;
  85. FixPerRegisters(task);
  86. }
  87. void user_disable_single_step(struct task_struct *task)
  88. {
  89. task->thread.per_info.single_step = 0;
  90. FixPerRegisters(task);
  91. }
  92. /*
  93. * Called by kernel/ptrace.c when detaching..
  94. *
  95. * Make sure single step bits etc are not set.
  96. */
  97. void
  98. ptrace_disable(struct task_struct *child)
  99. {
  100. /* make sure the single step bit is not set. */
  101. user_disable_single_step(child);
  102. }
  103. #ifndef CONFIG_64BIT
  104. # define __ADDR_MASK 3
  105. #else
  106. # define __ADDR_MASK 7
  107. #endif
  108. /*
  109. * Read the word at offset addr from the user area of a process. The
  110. * trouble here is that the information is littered over different
  111. * locations. The process registers are found on the kernel stack,
  112. * the floating point stuff and the trace settings are stored in
  113. * the task structure. In addition the different structures in
  114. * struct user contain pad bytes that should be read as zeroes.
  115. * Lovely...
  116. */
  117. static int
  118. peek_user(struct task_struct *child, addr_t addr, addr_t data)
  119. {
  120. struct user *dummy = NULL;
  121. addr_t offset, tmp, mask;
  122. /*
  123. * Stupid gdb peeks/pokes the access registers in 64 bit with
  124. * an alignment of 4. Programmers from hell...
  125. */
  126. mask = __ADDR_MASK;
  127. #ifdef CONFIG_64BIT
  128. if (addr >= (addr_t) &dummy->regs.acrs &&
  129. addr < (addr_t) &dummy->regs.orig_gpr2)
  130. mask = 3;
  131. #endif
  132. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  133. return -EIO;
  134. if (addr < (addr_t) &dummy->regs.acrs) {
  135. /*
  136. * psw and gprs are stored on the stack
  137. */
  138. tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
  139. if (addr == (addr_t) &dummy->regs.psw.mask)
  140. /* Remove per bit from user psw. */
  141. tmp &= ~PSW_MASK_PER;
  142. } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
  143. /*
  144. * access registers are stored in the thread structure
  145. */
  146. offset = addr - (addr_t) &dummy->regs.acrs;
  147. #ifdef CONFIG_64BIT
  148. /*
  149. * Very special case: old & broken 64 bit gdb reading
  150. * from acrs[15]. Result is a 64 bit value. Read the
  151. * 32 bit acrs[15] value and shift it by 32. Sick...
  152. */
  153. if (addr == (addr_t) &dummy->regs.acrs[15])
  154. tmp = ((unsigned long) child->thread.acrs[15]) << 32;
  155. else
  156. #endif
  157. tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
  158. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  159. /*
  160. * orig_gpr2 is stored on the kernel stack
  161. */
  162. tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
  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 put_user(tmp, (addr_t __user *) data);
  181. }
  182. /*
  183. * Write a word to the user area of a process at location addr. This
  184. * operation does have an additional problem compared to peek_user.
  185. * Stores to the program status word and on the floating point
  186. * control register needs to get checked for validity.
  187. */
  188. static int
  189. poke_user(struct task_struct *child, addr_t addr, addr_t data)
  190. {
  191. struct user *dummy = NULL;
  192. addr_t offset, mask;
  193. /*
  194. * Stupid gdb peeks/pokes the access registers in 64 bit with
  195. * an alignment of 4. Programmers from hell indeed...
  196. */
  197. mask = __ADDR_MASK;
  198. #ifdef CONFIG_64BIT
  199. if (addr >= (addr_t) &dummy->regs.acrs &&
  200. addr < (addr_t) &dummy->regs.orig_gpr2)
  201. mask = 3;
  202. #endif
  203. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  204. return -EIO;
  205. if (addr < (addr_t) &dummy->regs.acrs) {
  206. /*
  207. * psw and gprs are stored on the stack
  208. */
  209. if (addr == (addr_t) &dummy->regs.psw.mask &&
  210. #ifdef CONFIG_COMPAT
  211. data != PSW_MASK_MERGE(psw_user32_bits, data) &&
  212. #endif
  213. data != PSW_MASK_MERGE(psw_user_bits, data))
  214. /* Invalid psw mask. */
  215. return -EINVAL;
  216. #ifndef CONFIG_64BIT
  217. if (addr == (addr_t) &dummy->regs.psw.addr)
  218. /* I'd like to reject addresses without the
  219. high order bit but older gdb's rely on it */
  220. data |= PSW_ADDR_AMODE;
  221. #endif
  222. *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
  223. } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
  224. /*
  225. * access registers are stored in the thread structure
  226. */
  227. offset = addr - (addr_t) &dummy->regs.acrs;
  228. #ifdef CONFIG_64BIT
  229. /*
  230. * Very special case: old & broken 64 bit gdb writing
  231. * to acrs[15] with a 64 bit value. Ignore the lower
  232. * half of the value and write the upper 32 bit to
  233. * acrs[15]. Sick...
  234. */
  235. if (addr == (addr_t) &dummy->regs.acrs[15])
  236. child->thread.acrs[15] = (unsigned int) (data >> 32);
  237. else
  238. #endif
  239. *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
  240. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  241. /*
  242. * orig_gpr2 is stored on the kernel stack
  243. */
  244. task_pt_regs(child)->orig_gpr2 = data;
  245. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  246. /*
  247. * floating point regs. are stored in the thread structure
  248. */
  249. if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
  250. (data & ~((unsigned long) FPC_VALID_MASK
  251. << (BITS_PER_LONG - 32))) != 0)
  252. return -EINVAL;
  253. offset = addr - (addr_t) &dummy->regs.fp_regs;
  254. *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
  255. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  256. /*
  257. * per_info is found in the thread structure
  258. */
  259. offset = addr - (addr_t) &dummy->regs.per_info;
  260. *(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
  261. }
  262. FixPerRegisters(child);
  263. return 0;
  264. }
  265. static int
  266. do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
  267. {
  268. ptrace_area parea;
  269. int copied, ret;
  270. switch (request) {
  271. case PTRACE_PEEKTEXT:
  272. case PTRACE_PEEKDATA:
  273. /* Remove high order bit from address (only for 31 bit). */
  274. addr &= PSW_ADDR_INSN;
  275. /* read word at location addr. */
  276. return generic_ptrace_peekdata(child, addr, data);
  277. case PTRACE_PEEKUSR:
  278. /* read the word at location addr in the USER area. */
  279. return peek_user(child, addr, data);
  280. case PTRACE_POKETEXT:
  281. case PTRACE_POKEDATA:
  282. /* Remove high order bit from address (only for 31 bit). */
  283. addr &= PSW_ADDR_INSN;
  284. /* write the word at location addr. */
  285. return generic_ptrace_pokedata(child, addr, data);
  286. case PTRACE_POKEUSR:
  287. /* write the word at location addr in the USER area */
  288. return poke_user(child, addr, data);
  289. case PTRACE_PEEKUSR_AREA:
  290. case PTRACE_POKEUSR_AREA:
  291. if (copy_from_user(&parea, (void __force __user *) addr,
  292. sizeof(parea)))
  293. return -EFAULT;
  294. addr = parea.kernel_addr;
  295. data = parea.process_addr;
  296. copied = 0;
  297. while (copied < parea.len) {
  298. if (request == PTRACE_PEEKUSR_AREA)
  299. ret = peek_user(child, addr, data);
  300. else {
  301. addr_t utmp;
  302. if (get_user(utmp,
  303. (addr_t __force __user *) data))
  304. return -EFAULT;
  305. ret = poke_user(child, addr, utmp);
  306. }
  307. if (ret)
  308. return ret;
  309. addr += sizeof(unsigned long);
  310. data += sizeof(unsigned long);
  311. copied += sizeof(unsigned long);
  312. }
  313. return 0;
  314. }
  315. return ptrace_request(child, request, addr, data);
  316. }
  317. #ifdef CONFIG_COMPAT
  318. /*
  319. * Now the fun part starts... a 31 bit program running in the
  320. * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
  321. * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
  322. * to handle, the difference to the 64 bit versions of the requests
  323. * is that the access is done in multiples of 4 byte instead of
  324. * 8 bytes (sizeof(unsigned long) on 31/64 bit).
  325. * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
  326. * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
  327. * is a 31 bit program too, the content of struct user can be
  328. * emulated. A 31 bit program peeking into the struct user of
  329. * a 64 bit program is a no-no.
  330. */
  331. /*
  332. * Same as peek_user but for a 31 bit program.
  333. */
  334. static int
  335. peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
  336. {
  337. struct user32 *dummy32 = NULL;
  338. per_struct32 *dummy_per32 = NULL;
  339. addr_t offset;
  340. __u32 tmp;
  341. if (!test_thread_flag(TIF_31BIT) ||
  342. (addr & 3) || addr > sizeof(struct user) - 3)
  343. return -EIO;
  344. if (addr < (addr_t) &dummy32->regs.acrs) {
  345. /*
  346. * psw and gprs are stored on the stack
  347. */
  348. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  349. /* Fake a 31 bit psw mask. */
  350. tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
  351. tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
  352. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  353. /* Fake a 31 bit psw address. */
  354. tmp = (__u32) task_pt_regs(child)->psw.addr |
  355. PSW32_ADDR_AMODE31;
  356. } else {
  357. /* gpr 0-15 */
  358. tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
  359. addr*2 + 4);
  360. }
  361. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  362. /*
  363. * access registers are stored in the thread structure
  364. */
  365. offset = addr - (addr_t) &dummy32->regs.acrs;
  366. tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
  367. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  368. /*
  369. * orig_gpr2 is stored on the kernel stack
  370. */
  371. tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
  372. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  373. /*
  374. * floating point regs. are stored in the thread structure
  375. */
  376. offset = addr - (addr_t) &dummy32->regs.fp_regs;
  377. tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
  378. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  379. /*
  380. * per_info is found in the thread structure
  381. */
  382. offset = addr - (addr_t) &dummy32->regs.per_info;
  383. /* This is magic. See per_struct and per_struct32. */
  384. if ((offset >= (addr_t) &dummy_per32->control_regs &&
  385. offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
  386. (offset >= (addr_t) &dummy_per32->starting_addr &&
  387. offset <= (addr_t) &dummy_per32->ending_addr) ||
  388. offset == (addr_t) &dummy_per32->lowcore.words.address)
  389. offset = offset*2 + 4;
  390. else
  391. offset = offset*2;
  392. tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
  393. } else
  394. tmp = 0;
  395. return put_user(tmp, (__u32 __user *) data);
  396. }
  397. /*
  398. * Same as poke_user but for a 31 bit program.
  399. */
  400. static int
  401. poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
  402. {
  403. struct user32 *dummy32 = NULL;
  404. per_struct32 *dummy_per32 = NULL;
  405. addr_t offset;
  406. __u32 tmp;
  407. if (!test_thread_flag(TIF_31BIT) ||
  408. (addr & 3) || addr > sizeof(struct user32) - 3)
  409. return -EIO;
  410. tmp = (__u32) data;
  411. if (addr < (addr_t) &dummy32->regs.acrs) {
  412. /*
  413. * psw, gprs, acrs and orig_gpr2 are stored on the stack
  414. */
  415. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  416. /* Build a 64 bit psw mask from 31 bit mask. */
  417. if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
  418. /* Invalid psw mask. */
  419. return -EINVAL;
  420. task_pt_regs(child)->psw.mask =
  421. PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
  422. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  423. /* Build a 64 bit psw address from 31 bit address. */
  424. task_pt_regs(child)->psw.addr =
  425. (__u64) tmp & PSW32_ADDR_INSN;
  426. } else {
  427. /* gpr 0-15 */
  428. *(__u32*)((addr_t) &task_pt_regs(child)->psw
  429. + addr*2 + 4) = tmp;
  430. }
  431. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  432. /*
  433. * access registers are stored in the thread structure
  434. */
  435. offset = addr - (addr_t) &dummy32->regs.acrs;
  436. *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
  437. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  438. /*
  439. * orig_gpr2 is stored on the kernel stack
  440. */
  441. *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
  442. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  443. /*
  444. * floating point regs. are stored in the thread structure
  445. */
  446. if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
  447. (tmp & ~FPC_VALID_MASK) != 0)
  448. /* Invalid floating point control. */
  449. return -EINVAL;
  450. offset = addr - (addr_t) &dummy32->regs.fp_regs;
  451. *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
  452. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  453. /*
  454. * per_info is found in the thread structure.
  455. */
  456. offset = addr - (addr_t) &dummy32->regs.per_info;
  457. /*
  458. * This is magic. See per_struct and per_struct32.
  459. * By incident the offsets in per_struct are exactly
  460. * twice the offsets in per_struct32 for all fields.
  461. * The 8 byte fields need special handling though,
  462. * because the second half (bytes 4-7) is needed and
  463. * not the first half.
  464. */
  465. if ((offset >= (addr_t) &dummy_per32->control_regs &&
  466. offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
  467. (offset >= (addr_t) &dummy_per32->starting_addr &&
  468. offset <= (addr_t) &dummy_per32->ending_addr) ||
  469. offset == (addr_t) &dummy_per32->lowcore.words.address)
  470. offset = offset*2 + 4;
  471. else
  472. offset = offset*2;
  473. *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
  474. }
  475. FixPerRegisters(child);
  476. return 0;
  477. }
  478. static int
  479. do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
  480. {
  481. unsigned int tmp; /* 4 bytes !! */
  482. ptrace_area_emu31 parea;
  483. int copied, ret;
  484. switch (request) {
  485. case PTRACE_PEEKTEXT:
  486. case PTRACE_PEEKDATA:
  487. /* read word at location addr. */
  488. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  489. if (copied != sizeof(tmp))
  490. return -EIO;
  491. return put_user(tmp, (unsigned int __force __user *) data);
  492. case PTRACE_PEEKUSR:
  493. /* read the word at location addr in the USER area. */
  494. return peek_user_emu31(child, addr, data);
  495. case PTRACE_POKETEXT:
  496. case PTRACE_POKEDATA:
  497. /* write the word at location addr. */
  498. tmp = data;
  499. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
  500. if (copied != sizeof(tmp))
  501. return -EIO;
  502. return 0;
  503. case PTRACE_POKEUSR:
  504. /* write the word at location addr in the USER area */
  505. return poke_user_emu31(child, addr, data);
  506. case PTRACE_PEEKUSR_AREA:
  507. case PTRACE_POKEUSR_AREA:
  508. if (copy_from_user(&parea, (void __force __user *) addr,
  509. sizeof(parea)))
  510. return -EFAULT;
  511. addr = parea.kernel_addr;
  512. data = parea.process_addr;
  513. copied = 0;
  514. while (copied < parea.len) {
  515. if (request == PTRACE_PEEKUSR_AREA)
  516. ret = peek_user_emu31(child, addr, data);
  517. else {
  518. __u32 utmp;
  519. if (get_user(utmp,
  520. (__u32 __force __user *) data))
  521. return -EFAULT;
  522. ret = poke_user_emu31(child, addr, utmp);
  523. }
  524. if (ret)
  525. return ret;
  526. addr += sizeof(unsigned int);
  527. data += sizeof(unsigned int);
  528. copied += sizeof(unsigned int);
  529. }
  530. return 0;
  531. case PTRACE_GETEVENTMSG:
  532. return put_user((__u32) child->ptrace_message,
  533. (unsigned int __force __user *) data);
  534. case PTRACE_GETSIGINFO:
  535. if (child->last_siginfo == NULL)
  536. return -EINVAL;
  537. return copy_siginfo_to_user32((compat_siginfo_t
  538. __force __user *) data,
  539. child->last_siginfo);
  540. case PTRACE_SETSIGINFO:
  541. if (child->last_siginfo == NULL)
  542. return -EINVAL;
  543. return copy_siginfo_from_user32(child->last_siginfo,
  544. (compat_siginfo_t
  545. __force __user *) data);
  546. }
  547. return ptrace_request(child, request, addr, data);
  548. }
  549. #endif
  550. #define PT32_IEEE_IP 0x13c
  551. static int
  552. do_ptrace(struct task_struct *child, long request, long addr, long data)
  553. {
  554. int ret;
  555. if (request == PTRACE_ATTACH)
  556. return ptrace_attach(child);
  557. /*
  558. * Special cases to get/store the ieee instructions pointer.
  559. */
  560. if (child == current) {
  561. if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
  562. return peek_user(child, addr, data);
  563. if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
  564. return poke_user(child, addr, data);
  565. #ifdef CONFIG_COMPAT
  566. if (request == PTRACE_PEEKUSR &&
  567. addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
  568. return peek_user_emu31(child, addr, data);
  569. if (request == PTRACE_POKEUSR &&
  570. addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
  571. return poke_user_emu31(child, addr, data);
  572. #endif
  573. }
  574. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  575. if (ret < 0)
  576. return ret;
  577. switch (request) {
  578. case PTRACE_SYSCALL:
  579. /* continue and stop at next (return from) syscall */
  580. case PTRACE_CONT:
  581. /* restart after signal. */
  582. if (!valid_signal(data))
  583. return -EIO;
  584. if (request == PTRACE_SYSCALL)
  585. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  586. else
  587. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  588. child->exit_code = data;
  589. /* make sure the single step bit is not set. */
  590. user_disable_single_step(child);
  591. wake_up_process(child);
  592. return 0;
  593. case PTRACE_KILL:
  594. /*
  595. * make the child exit. Best I can do is send it a sigkill.
  596. * perhaps it should be put in the status that it wants to
  597. * exit.
  598. */
  599. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  600. return 0;
  601. child->exit_code = SIGKILL;
  602. /* make sure the single step bit is not set. */
  603. user_disable_single_step(child);
  604. wake_up_process(child);
  605. return 0;
  606. case PTRACE_SINGLESTEP:
  607. /* set the trap flag. */
  608. if (!valid_signal(data))
  609. return -EIO;
  610. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  611. child->exit_code = data;
  612. user_enable_single_step(child);
  613. /* give it a chance to run. */
  614. wake_up_process(child);
  615. return 0;
  616. /* Do requests that differ for 31/64 bit */
  617. default:
  618. #ifdef CONFIG_COMPAT
  619. if (test_thread_flag(TIF_31BIT))
  620. return do_ptrace_emu31(child, request, addr, data);
  621. #endif
  622. return do_ptrace_normal(child, request, addr, data);
  623. }
  624. /* Not reached. */
  625. return -EIO;
  626. }
  627. asmlinkage long
  628. sys_ptrace(long request, long pid, long addr, long data)
  629. {
  630. struct task_struct *child;
  631. int ret;
  632. lock_kernel();
  633. if (request == PTRACE_TRACEME) {
  634. ret = ptrace_traceme();
  635. goto out;
  636. }
  637. child = ptrace_get_task_struct(pid);
  638. if (IS_ERR(child)) {
  639. ret = PTR_ERR(child);
  640. goto out;
  641. }
  642. ret = do_ptrace(child, request, addr, data);
  643. put_task_struct(child);
  644. out:
  645. unlock_kernel();
  646. return ret;
  647. }
  648. asmlinkage void
  649. syscall_trace(struct pt_regs *regs, int entryexit)
  650. {
  651. if (unlikely(current->audit_context) && entryexit)
  652. audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
  653. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  654. goto out;
  655. if (!(current->ptrace & PT_PTRACED))
  656. goto out;
  657. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  658. ? 0x80 : 0));
  659. /*
  660. * If the debuffer has set an invalid system call number,
  661. * we prepare to skip the system call restart handling.
  662. */
  663. if (!entryexit && regs->gprs[2] >= NR_syscalls)
  664. regs->trap = -1;
  665. /*
  666. * this isn't the same as continuing with a signal, but it will do
  667. * for normal use. strace only continues with a signal if the
  668. * stopping signal is not SIGTRAP. -brl
  669. */
  670. if (current->exit_code) {
  671. send_sig(current->exit_code, current, 1);
  672. current->exit_code = 0;
  673. }
  674. out:
  675. if (unlikely(current->audit_context) && !entryexit)
  676. audit_syscall_entry(test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
  677. regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
  678. regs->gprs[4], regs->gprs[5]);
  679. }