ptrace.c 20 KB

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