ptrace.c 32 KB

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