ptrace.c 20 KB

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