ptrace.c 28 KB

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