ptrace.c 29 KB

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