ptrace.c 32 KB

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