ptrace.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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,
  309. unsigned long addr, unsigned long data)
  310. {
  311. ptrace_area parea;
  312. int copied, ret;
  313. switch (request) {
  314. case PTRACE_PEEKUSR:
  315. /* read the word at location addr in the USER area. */
  316. return peek_user(child, addr, data);
  317. case PTRACE_POKEUSR:
  318. /* write the word at location addr in the USER area */
  319. return poke_user(child, addr, data);
  320. case PTRACE_PEEKUSR_AREA:
  321. case PTRACE_POKEUSR_AREA:
  322. if (copy_from_user(&parea, (void __force __user *) addr,
  323. sizeof(parea)))
  324. return -EFAULT;
  325. addr = parea.kernel_addr;
  326. data = parea.process_addr;
  327. copied = 0;
  328. while (copied < parea.len) {
  329. if (request == PTRACE_PEEKUSR_AREA)
  330. ret = peek_user(child, addr, data);
  331. else {
  332. addr_t utmp;
  333. if (get_user(utmp,
  334. (addr_t __force __user *) data))
  335. return -EFAULT;
  336. ret = poke_user(child, addr, utmp);
  337. }
  338. if (ret)
  339. return ret;
  340. addr += sizeof(unsigned long);
  341. data += sizeof(unsigned long);
  342. copied += sizeof(unsigned long);
  343. }
  344. return 0;
  345. case PTRACE_GET_LAST_BREAK:
  346. put_user(task_thread_info(child)->last_break,
  347. (unsigned long __user *) data);
  348. return 0;
  349. default:
  350. /* Removing high order bit from addr (only for 31 bit). */
  351. addr &= PSW_ADDR_INSN;
  352. return ptrace_request(child, request, addr, data);
  353. }
  354. }
  355. #ifdef CONFIG_COMPAT
  356. /*
  357. * Now the fun part starts... a 31 bit program running in the
  358. * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
  359. * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
  360. * to handle, the difference to the 64 bit versions of the requests
  361. * is that the access is done in multiples of 4 byte instead of
  362. * 8 bytes (sizeof(unsigned long) on 31/64 bit).
  363. * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
  364. * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
  365. * is a 31 bit program too, the content of struct user can be
  366. * emulated. A 31 bit program peeking into the struct user of
  367. * a 64 bit program is a no-no.
  368. */
  369. /*
  370. * Same as peek_user but for a 31 bit program.
  371. */
  372. static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
  373. {
  374. struct user32 *dummy32 = NULL;
  375. per_struct32 *dummy_per32 = NULL;
  376. addr_t offset;
  377. __u32 tmp;
  378. if (addr < (addr_t) &dummy32->regs.acrs) {
  379. /*
  380. * psw and gprs are stored on the stack
  381. */
  382. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  383. /* Fake a 31 bit psw mask. */
  384. tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
  385. tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
  386. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  387. /* Fake a 31 bit psw address. */
  388. tmp = (__u32) task_pt_regs(child)->psw.addr |
  389. PSW32_ADDR_AMODE31;
  390. } else {
  391. /* gpr 0-15 */
  392. tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
  393. addr*2 + 4);
  394. }
  395. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  396. /*
  397. * access registers are stored in the thread structure
  398. */
  399. offset = addr - (addr_t) &dummy32->regs.acrs;
  400. tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
  401. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  402. /*
  403. * orig_gpr2 is stored on the kernel stack
  404. */
  405. tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
  406. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  407. /*
  408. * prevent reads of padding hole between
  409. * orig_gpr2 and fp_regs on s390.
  410. */
  411. tmp = 0;
  412. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  413. /*
  414. * floating point regs. are stored in the thread structure
  415. */
  416. offset = addr - (addr_t) &dummy32->regs.fp_regs;
  417. tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
  418. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  419. /*
  420. * per_info is found in the thread structure
  421. */
  422. offset = addr - (addr_t) &dummy32->regs.per_info;
  423. /* This is magic. See per_struct and per_struct32. */
  424. if ((offset >= (addr_t) &dummy_per32->control_regs &&
  425. offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
  426. (offset >= (addr_t) &dummy_per32->starting_addr &&
  427. offset <= (addr_t) &dummy_per32->ending_addr) ||
  428. offset == (addr_t) &dummy_per32->lowcore.words.address)
  429. offset = offset*2 + 4;
  430. else
  431. offset = offset*2;
  432. tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
  433. } else
  434. tmp = 0;
  435. return tmp;
  436. }
  437. static int peek_user_compat(struct task_struct *child,
  438. addr_t addr, addr_t data)
  439. {
  440. __u32 tmp;
  441. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
  442. return -EIO;
  443. tmp = __peek_user_compat(child, addr);
  444. return put_user(tmp, (__u32 __user *) data);
  445. }
  446. /*
  447. * Same as poke_user but for a 31 bit program.
  448. */
  449. static int __poke_user_compat(struct task_struct *child,
  450. addr_t addr, addr_t data)
  451. {
  452. struct user32 *dummy32 = NULL;
  453. per_struct32 *dummy_per32 = NULL;
  454. __u32 tmp = (__u32) data;
  455. addr_t offset;
  456. if (addr < (addr_t) &dummy32->regs.acrs) {
  457. /*
  458. * psw, gprs, acrs and orig_gpr2 are stored on the stack
  459. */
  460. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  461. /* Build a 64 bit psw mask from 31 bit mask. */
  462. if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
  463. /* Invalid psw mask. */
  464. return -EINVAL;
  465. task_pt_regs(child)->psw.mask =
  466. PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
  467. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  468. /* Build a 64 bit psw address from 31 bit address. */
  469. task_pt_regs(child)->psw.addr =
  470. (__u64) tmp & PSW32_ADDR_INSN;
  471. } else {
  472. /* gpr 0-15 */
  473. *(__u32*)((addr_t) &task_pt_regs(child)->psw
  474. + addr*2 + 4) = tmp;
  475. }
  476. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  477. /*
  478. * access registers are stored in the thread structure
  479. */
  480. offset = addr - (addr_t) &dummy32->regs.acrs;
  481. *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
  482. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  483. /*
  484. * orig_gpr2 is stored on the kernel stack
  485. */
  486. *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
  487. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  488. /*
  489. * prevent writess of padding hole between
  490. * orig_gpr2 and fp_regs on s390.
  491. */
  492. return 0;
  493. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  494. /*
  495. * floating point regs. are stored in the thread structure
  496. */
  497. if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
  498. (tmp & ~FPC_VALID_MASK) != 0)
  499. /* Invalid floating point control. */
  500. return -EINVAL;
  501. offset = addr - (addr_t) &dummy32->regs.fp_regs;
  502. *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
  503. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  504. /*
  505. * per_info is found in the thread structure.
  506. */
  507. offset = addr - (addr_t) &dummy32->regs.per_info;
  508. /*
  509. * This is magic. See per_struct and per_struct32.
  510. * By incident the offsets in per_struct are exactly
  511. * twice the offsets in per_struct32 for all fields.
  512. * The 8 byte fields need special handling though,
  513. * because the second half (bytes 4-7) is needed and
  514. * not the first half.
  515. */
  516. if ((offset >= (addr_t) &dummy_per32->control_regs &&
  517. offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
  518. (offset >= (addr_t) &dummy_per32->starting_addr &&
  519. offset <= (addr_t) &dummy_per32->ending_addr) ||
  520. offset == (addr_t) &dummy_per32->lowcore.words.address)
  521. offset = offset*2 + 4;
  522. else
  523. offset = offset*2;
  524. *(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
  525. }
  526. FixPerRegisters(child);
  527. return 0;
  528. }
  529. static int poke_user_compat(struct task_struct *child,
  530. addr_t addr, addr_t data)
  531. {
  532. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user32) - 3)
  533. return -EIO;
  534. return __poke_user_compat(child, addr, data);
  535. }
  536. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  537. compat_ulong_t caddr, compat_ulong_t cdata)
  538. {
  539. unsigned long addr = caddr;
  540. unsigned long data = cdata;
  541. ptrace_area_emu31 parea;
  542. int copied, ret;
  543. switch (request) {
  544. case PTRACE_PEEKUSR:
  545. /* read the word at location addr in the USER area. */
  546. return peek_user_compat(child, addr, data);
  547. case PTRACE_POKEUSR:
  548. /* write the word at location addr in the USER area */
  549. return poke_user_compat(child, addr, data);
  550. case PTRACE_PEEKUSR_AREA:
  551. case PTRACE_POKEUSR_AREA:
  552. if (copy_from_user(&parea, (void __force __user *) addr,
  553. sizeof(parea)))
  554. return -EFAULT;
  555. addr = parea.kernel_addr;
  556. data = parea.process_addr;
  557. copied = 0;
  558. while (copied < parea.len) {
  559. if (request == PTRACE_PEEKUSR_AREA)
  560. ret = peek_user_compat(child, addr, data);
  561. else {
  562. __u32 utmp;
  563. if (get_user(utmp,
  564. (__u32 __force __user *) data))
  565. return -EFAULT;
  566. ret = poke_user_compat(child, addr, utmp);
  567. }
  568. if (ret)
  569. return ret;
  570. addr += sizeof(unsigned int);
  571. data += sizeof(unsigned int);
  572. copied += sizeof(unsigned int);
  573. }
  574. return 0;
  575. case PTRACE_GET_LAST_BREAK:
  576. put_user(task_thread_info(child)->last_break,
  577. (unsigned int __user *) data);
  578. return 0;
  579. }
  580. return compat_ptrace_request(child, request, addr, data);
  581. }
  582. #endif
  583. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  584. {
  585. long ret = 0;
  586. /* Do the secure computing check first. */
  587. secure_computing(regs->gprs[2]);
  588. /*
  589. * The sysc_tracesys code in entry.S stored the system
  590. * call number to gprs[2].
  591. */
  592. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  593. (tracehook_report_syscall_entry(regs) ||
  594. regs->gprs[2] >= NR_syscalls)) {
  595. /*
  596. * Tracing decided this syscall should not happen or the
  597. * debugger stored an invalid system call number. Skip
  598. * the system call and the system call restart handling.
  599. */
  600. regs->svcnr = 0;
  601. ret = -1;
  602. }
  603. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  604. trace_sys_enter(regs, regs->gprs[2]);
  605. if (unlikely(current->audit_context))
  606. audit_syscall_entry(is_compat_task() ?
  607. AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
  608. regs->gprs[2], regs->orig_gpr2,
  609. regs->gprs[3], regs->gprs[4],
  610. regs->gprs[5]);
  611. return ret ?: regs->gprs[2];
  612. }
  613. asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
  614. {
  615. if (unlikely(current->audit_context))
  616. audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]),
  617. regs->gprs[2]);
  618. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  619. trace_sys_exit(regs, regs->gprs[2]);
  620. if (test_thread_flag(TIF_SYSCALL_TRACE))
  621. tracehook_report_syscall_exit(regs, 0);
  622. }
  623. /*
  624. * user_regset definitions.
  625. */
  626. static int s390_regs_get(struct task_struct *target,
  627. const struct user_regset *regset,
  628. unsigned int pos, unsigned int count,
  629. void *kbuf, void __user *ubuf)
  630. {
  631. if (target == current)
  632. save_access_regs(target->thread.acrs);
  633. if (kbuf) {
  634. unsigned long *k = kbuf;
  635. while (count > 0) {
  636. *k++ = __peek_user(target, pos);
  637. count -= sizeof(*k);
  638. pos += sizeof(*k);
  639. }
  640. } else {
  641. unsigned long __user *u = ubuf;
  642. while (count > 0) {
  643. if (__put_user(__peek_user(target, pos), u++))
  644. return -EFAULT;
  645. count -= sizeof(*u);
  646. pos += sizeof(*u);
  647. }
  648. }
  649. return 0;
  650. }
  651. static int s390_regs_set(struct task_struct *target,
  652. const struct user_regset *regset,
  653. unsigned int pos, unsigned int count,
  654. const void *kbuf, const void __user *ubuf)
  655. {
  656. int rc = 0;
  657. if (target == current)
  658. save_access_regs(target->thread.acrs);
  659. if (kbuf) {
  660. const unsigned long *k = kbuf;
  661. while (count > 0 && !rc) {
  662. rc = __poke_user(target, pos, *k++);
  663. count -= sizeof(*k);
  664. pos += sizeof(*k);
  665. }
  666. } else {
  667. const unsigned long __user *u = ubuf;
  668. while (count > 0 && !rc) {
  669. unsigned long word;
  670. rc = __get_user(word, u++);
  671. if (rc)
  672. break;
  673. rc = __poke_user(target, pos, word);
  674. count -= sizeof(*u);
  675. pos += sizeof(*u);
  676. }
  677. }
  678. if (rc == 0 && target == current)
  679. restore_access_regs(target->thread.acrs);
  680. return rc;
  681. }
  682. static int s390_fpregs_get(struct task_struct *target,
  683. const struct user_regset *regset, unsigned int pos,
  684. unsigned int count, void *kbuf, void __user *ubuf)
  685. {
  686. if (target == current)
  687. save_fp_regs(&target->thread.fp_regs);
  688. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  689. &target->thread.fp_regs, 0, -1);
  690. }
  691. static int s390_fpregs_set(struct task_struct *target,
  692. const struct user_regset *regset, unsigned int pos,
  693. unsigned int count, const void *kbuf,
  694. const void __user *ubuf)
  695. {
  696. int rc = 0;
  697. if (target == current)
  698. save_fp_regs(&target->thread.fp_regs);
  699. /* If setting FPC, must validate it first. */
  700. if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
  701. u32 fpc[2] = { target->thread.fp_regs.fpc, 0 };
  702. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpc,
  703. 0, offsetof(s390_fp_regs, fprs));
  704. if (rc)
  705. return rc;
  706. if ((fpc[0] & ~FPC_VALID_MASK) != 0 || fpc[1] != 0)
  707. return -EINVAL;
  708. target->thread.fp_regs.fpc = fpc[0];
  709. }
  710. if (rc == 0 && count > 0)
  711. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  712. target->thread.fp_regs.fprs,
  713. offsetof(s390_fp_regs, fprs), -1);
  714. if (rc == 0 && target == current)
  715. restore_fp_regs(&target->thread.fp_regs);
  716. return rc;
  717. }
  718. #ifdef CONFIG_64BIT
  719. static int s390_last_break_get(struct task_struct *target,
  720. const struct user_regset *regset,
  721. unsigned int pos, unsigned int count,
  722. void *kbuf, void __user *ubuf)
  723. {
  724. if (count > 0) {
  725. if (kbuf) {
  726. unsigned long *k = kbuf;
  727. *k = task_thread_info(target)->last_break;
  728. } else {
  729. unsigned long __user *u = ubuf;
  730. if (__put_user(task_thread_info(target)->last_break, u))
  731. return -EFAULT;
  732. }
  733. }
  734. return 0;
  735. }
  736. #endif
  737. static const struct user_regset s390_regsets[] = {
  738. [REGSET_GENERAL] = {
  739. .core_note_type = NT_PRSTATUS,
  740. .n = sizeof(s390_regs) / sizeof(long),
  741. .size = sizeof(long),
  742. .align = sizeof(long),
  743. .get = s390_regs_get,
  744. .set = s390_regs_set,
  745. },
  746. [REGSET_FP] = {
  747. .core_note_type = NT_PRFPREG,
  748. .n = sizeof(s390_fp_regs) / sizeof(long),
  749. .size = sizeof(long),
  750. .align = sizeof(long),
  751. .get = s390_fpregs_get,
  752. .set = s390_fpregs_set,
  753. },
  754. #ifdef CONFIG_64BIT
  755. [REGSET_LAST_BREAK] = {
  756. .core_note_type = NT_S390_LAST_BREAK,
  757. .n = 1,
  758. .size = sizeof(long),
  759. .align = sizeof(long),
  760. .get = s390_last_break_get,
  761. },
  762. #endif
  763. };
  764. static const struct user_regset_view user_s390_view = {
  765. .name = UTS_MACHINE,
  766. .e_machine = EM_S390,
  767. .regsets = s390_regsets,
  768. .n = ARRAY_SIZE(s390_regsets)
  769. };
  770. #ifdef CONFIG_COMPAT
  771. static int s390_compat_regs_get(struct task_struct *target,
  772. const struct user_regset *regset,
  773. unsigned int pos, unsigned int count,
  774. void *kbuf, void __user *ubuf)
  775. {
  776. if (target == current)
  777. save_access_regs(target->thread.acrs);
  778. if (kbuf) {
  779. compat_ulong_t *k = kbuf;
  780. while (count > 0) {
  781. *k++ = __peek_user_compat(target, pos);
  782. count -= sizeof(*k);
  783. pos += sizeof(*k);
  784. }
  785. } else {
  786. compat_ulong_t __user *u = ubuf;
  787. while (count > 0) {
  788. if (__put_user(__peek_user_compat(target, pos), u++))
  789. return -EFAULT;
  790. count -= sizeof(*u);
  791. pos += sizeof(*u);
  792. }
  793. }
  794. return 0;
  795. }
  796. static int s390_compat_regs_set(struct task_struct *target,
  797. const struct user_regset *regset,
  798. unsigned int pos, unsigned int count,
  799. const void *kbuf, const void __user *ubuf)
  800. {
  801. int rc = 0;
  802. if (target == current)
  803. save_access_regs(target->thread.acrs);
  804. if (kbuf) {
  805. const compat_ulong_t *k = kbuf;
  806. while (count > 0 && !rc) {
  807. rc = __poke_user_compat(target, pos, *k++);
  808. count -= sizeof(*k);
  809. pos += sizeof(*k);
  810. }
  811. } else {
  812. const compat_ulong_t __user *u = ubuf;
  813. while (count > 0 && !rc) {
  814. compat_ulong_t word;
  815. rc = __get_user(word, u++);
  816. if (rc)
  817. break;
  818. rc = __poke_user_compat(target, pos, word);
  819. count -= sizeof(*u);
  820. pos += sizeof(*u);
  821. }
  822. }
  823. if (rc == 0 && target == current)
  824. restore_access_regs(target->thread.acrs);
  825. return rc;
  826. }
  827. static int s390_compat_regs_high_get(struct task_struct *target,
  828. const struct user_regset *regset,
  829. unsigned int pos, unsigned int count,
  830. void *kbuf, void __user *ubuf)
  831. {
  832. compat_ulong_t *gprs_high;
  833. gprs_high = (compat_ulong_t *)
  834. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  835. if (kbuf) {
  836. compat_ulong_t *k = kbuf;
  837. while (count > 0) {
  838. *k++ = *gprs_high;
  839. gprs_high += 2;
  840. count -= sizeof(*k);
  841. }
  842. } else {
  843. compat_ulong_t __user *u = ubuf;
  844. while (count > 0) {
  845. if (__put_user(*gprs_high, u++))
  846. return -EFAULT;
  847. gprs_high += 2;
  848. count -= sizeof(*u);
  849. }
  850. }
  851. return 0;
  852. }
  853. static int s390_compat_regs_high_set(struct task_struct *target,
  854. const struct user_regset *regset,
  855. unsigned int pos, unsigned int count,
  856. const void *kbuf, const void __user *ubuf)
  857. {
  858. compat_ulong_t *gprs_high;
  859. int rc = 0;
  860. gprs_high = (compat_ulong_t *)
  861. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  862. if (kbuf) {
  863. const compat_ulong_t *k = kbuf;
  864. while (count > 0) {
  865. *gprs_high = *k++;
  866. *gprs_high += 2;
  867. count -= sizeof(*k);
  868. }
  869. } else {
  870. const compat_ulong_t __user *u = ubuf;
  871. while (count > 0 && !rc) {
  872. unsigned long word;
  873. rc = __get_user(word, u++);
  874. if (rc)
  875. break;
  876. *gprs_high = word;
  877. *gprs_high += 2;
  878. count -= sizeof(*u);
  879. }
  880. }
  881. return rc;
  882. }
  883. static int s390_compat_last_break_get(struct task_struct *target,
  884. const struct user_regset *regset,
  885. unsigned int pos, unsigned int count,
  886. void *kbuf, void __user *ubuf)
  887. {
  888. compat_ulong_t last_break;
  889. if (count > 0) {
  890. last_break = task_thread_info(target)->last_break;
  891. if (kbuf) {
  892. unsigned long *k = kbuf;
  893. *k = last_break;
  894. } else {
  895. unsigned long __user *u = ubuf;
  896. if (__put_user(last_break, u))
  897. return -EFAULT;
  898. }
  899. }
  900. return 0;
  901. }
  902. static const struct user_regset s390_compat_regsets[] = {
  903. [REGSET_GENERAL] = {
  904. .core_note_type = NT_PRSTATUS,
  905. .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
  906. .size = sizeof(compat_long_t),
  907. .align = sizeof(compat_long_t),
  908. .get = s390_compat_regs_get,
  909. .set = s390_compat_regs_set,
  910. },
  911. [REGSET_FP] = {
  912. .core_note_type = NT_PRFPREG,
  913. .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
  914. .size = sizeof(compat_long_t),
  915. .align = sizeof(compat_long_t),
  916. .get = s390_fpregs_get,
  917. .set = s390_fpregs_set,
  918. },
  919. [REGSET_LAST_BREAK] = {
  920. .core_note_type = NT_S390_LAST_BREAK,
  921. .n = 1,
  922. .size = sizeof(long),
  923. .align = sizeof(long),
  924. .get = s390_compat_last_break_get,
  925. },
  926. [REGSET_GENERAL_EXTENDED] = {
  927. .core_note_type = NT_S390_HIGH_GPRS,
  928. .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
  929. .size = sizeof(compat_long_t),
  930. .align = sizeof(compat_long_t),
  931. .get = s390_compat_regs_high_get,
  932. .set = s390_compat_regs_high_set,
  933. },
  934. };
  935. static const struct user_regset_view user_s390_compat_view = {
  936. .name = "s390",
  937. .e_machine = EM_S390,
  938. .regsets = s390_compat_regsets,
  939. .n = ARRAY_SIZE(s390_compat_regsets)
  940. };
  941. #endif
  942. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  943. {
  944. #ifdef CONFIG_COMPAT
  945. if (test_tsk_thread_flag(task, TIF_31BIT))
  946. return &user_s390_compat_view;
  947. #endif
  948. return &user_s390_view;
  949. }
  950. static const char *gpr_names[NUM_GPRS] = {
  951. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  952. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  953. };
  954. unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
  955. {
  956. if (offset >= NUM_GPRS)
  957. return 0;
  958. return regs->gprs[offset];
  959. }
  960. int regs_query_register_offset(const char *name)
  961. {
  962. unsigned long offset;
  963. if (!name || *name != 'r')
  964. return -EINVAL;
  965. if (strict_strtoul(name + 1, 10, &offset))
  966. return -EINVAL;
  967. if (offset >= NUM_GPRS)
  968. return -EINVAL;
  969. return offset;
  970. }
  971. const char *regs_query_register_name(unsigned int offset)
  972. {
  973. if (offset >= NUM_GPRS)
  974. return NULL;
  975. return gpr_names[offset];
  976. }
  977. static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  978. {
  979. unsigned long ksp = kernel_stack_pointer(regs);
  980. return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
  981. }
  982. /**
  983. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  984. * @regs:pt_regs which contains kernel stack pointer.
  985. * @n:stack entry number.
  986. *
  987. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  988. * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
  989. * this returns 0.
  990. */
  991. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  992. {
  993. unsigned long addr;
  994. addr = kernel_stack_pointer(regs) + n * sizeof(long);
  995. if (!regs_within_kernel_stack(regs, addr))
  996. return 0;
  997. return *(unsigned long *)addr;
  998. }