ptrace.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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/uaccess.h>
  29. #include <asm/unistd.h>
  30. #include <asm/switch_to.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. void update_cr_regs(struct task_struct *task)
  38. {
  39. struct pt_regs *regs = task_pt_regs(task);
  40. struct thread_struct *thread = &task->thread;
  41. struct per_regs old, new;
  42. /* Take care of the enable/disable of transactional execution. */
  43. if (MACHINE_HAS_TE) {
  44. unsigned long cr, cr_new;
  45. __ctl_store(cr, 0, 0);
  46. /* Set or clear transaction execution TXC bit 8. */
  47. cr_new = cr | (1UL << 55);
  48. if (task->thread.per_flags & PER_FLAG_NO_TE)
  49. cr_new &= ~(1UL << 55);
  50. if (cr_new != cr)
  51. __ctl_load(cr_new, 0, 0);
  52. /* Set or clear transaction execution TDC bits 62 and 63. */
  53. __ctl_store(cr, 2, 2);
  54. cr_new = cr & ~3UL;
  55. if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
  56. if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND_TEND)
  57. cr_new |= 1UL;
  58. else
  59. cr_new |= 2UL;
  60. }
  61. if (cr_new != cr)
  62. __ctl_load(cr_new, 2, 2);
  63. }
  64. /* Copy user specified PER registers */
  65. new.control = thread->per_user.control;
  66. new.start = thread->per_user.start;
  67. new.end = thread->per_user.end;
  68. /* merge TIF_SINGLE_STEP into user specified PER registers. */
  69. if (test_tsk_thread_flag(task, TIF_SINGLE_STEP) ||
  70. test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP)) {
  71. if (test_tsk_thread_flag(task, TIF_BLOCK_STEP))
  72. new.control |= PER_EVENT_BRANCH;
  73. else
  74. new.control |= PER_EVENT_IFETCH;
  75. new.control |= PER_CONTROL_SUSPENSION;
  76. new.control |= PER_EVENT_TRANSACTION_END;
  77. if (test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP))
  78. new.control |= PER_EVENT_IFETCH;
  79. new.start = 0;
  80. new.end = -1UL;
  81. }
  82. /* Take care of the PER enablement bit in the PSW. */
  83. if (!(new.control & PER_EVENT_MASK)) {
  84. regs->psw.mask &= ~PSW_MASK_PER;
  85. return;
  86. }
  87. regs->psw.mask |= PSW_MASK_PER;
  88. __ctl_store(old, 9, 11);
  89. if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
  90. __ctl_load(new, 9, 11);
  91. }
  92. void user_enable_single_step(struct task_struct *task)
  93. {
  94. clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
  95. set_tsk_thread_flag(task, TIF_SINGLE_STEP);
  96. }
  97. void user_disable_single_step(struct task_struct *task)
  98. {
  99. clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
  100. clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
  101. }
  102. void user_enable_block_step(struct task_struct *task)
  103. {
  104. set_tsk_thread_flag(task, TIF_SINGLE_STEP);
  105. set_tsk_thread_flag(task, TIF_BLOCK_STEP);
  106. }
  107. /*
  108. * Called by kernel/ptrace.c when detaching..
  109. *
  110. * Clear all debugging related fields.
  111. */
  112. void ptrace_disable(struct task_struct *task)
  113. {
  114. memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
  115. memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
  116. clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
  117. clear_pt_regs_flag(task_pt_regs(task), PIF_PER_TRAP);
  118. task->thread.per_flags = 0;
  119. }
  120. #define __ADDR_MASK 7
  121. static inline unsigned long __peek_user_per(struct task_struct *child,
  122. addr_t addr)
  123. {
  124. struct per_struct_kernel *dummy = NULL;
  125. if (addr == (addr_t) &dummy->cr9)
  126. /* Control bits of the active per set. */
  127. return test_thread_flag(TIF_SINGLE_STEP) ?
  128. PER_EVENT_IFETCH : child->thread.per_user.control;
  129. else if (addr == (addr_t) &dummy->cr10)
  130. /* Start address of the active per set. */
  131. return test_thread_flag(TIF_SINGLE_STEP) ?
  132. 0 : child->thread.per_user.start;
  133. else if (addr == (addr_t) &dummy->cr11)
  134. /* End address of the active per set. */
  135. return test_thread_flag(TIF_SINGLE_STEP) ?
  136. -1UL : child->thread.per_user.end;
  137. else if (addr == (addr_t) &dummy->bits)
  138. /* Single-step bit. */
  139. return test_thread_flag(TIF_SINGLE_STEP) ?
  140. (1UL << (BITS_PER_LONG - 1)) : 0;
  141. else if (addr == (addr_t) &dummy->starting_addr)
  142. /* Start address of the user specified per set. */
  143. return child->thread.per_user.start;
  144. else if (addr == (addr_t) &dummy->ending_addr)
  145. /* End address of the user specified per set. */
  146. return child->thread.per_user.end;
  147. else if (addr == (addr_t) &dummy->perc_atmid)
  148. /* PER code, ATMID and AI of the last PER trap */
  149. return (unsigned long)
  150. child->thread.per_event.cause << (BITS_PER_LONG - 16);
  151. else if (addr == (addr_t) &dummy->address)
  152. /* Address of the last PER trap */
  153. return child->thread.per_event.address;
  154. else if (addr == (addr_t) &dummy->access_id)
  155. /* Access id of the last PER trap */
  156. return (unsigned long)
  157. child->thread.per_event.paid << (BITS_PER_LONG - 8);
  158. return 0;
  159. }
  160. /*
  161. * Read the word at offset addr from the user area of a process. The
  162. * trouble here is that the information is littered over different
  163. * locations. The process registers are found on the kernel stack,
  164. * the floating point stuff and the trace settings are stored in
  165. * the task structure. In addition the different structures in
  166. * struct user contain pad bytes that should be read as zeroes.
  167. * Lovely...
  168. */
  169. static unsigned long __peek_user(struct task_struct *child, addr_t addr)
  170. {
  171. struct user *dummy = NULL;
  172. addr_t offset, tmp;
  173. if (addr < (addr_t) &dummy->regs.acrs) {
  174. /*
  175. * psw and gprs are stored on the stack
  176. */
  177. tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
  178. if (addr == (addr_t) &dummy->regs.psw.mask) {
  179. /* Return a clean psw mask. */
  180. tmp &= PSW_MASK_USER | PSW_MASK_RI;
  181. tmp |= PSW_USER_BITS;
  182. }
  183. } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
  184. /*
  185. * access registers are stored in the thread structure
  186. */
  187. offset = addr - (addr_t) &dummy->regs.acrs;
  188. /*
  189. * Very special case: old & broken 64 bit gdb reading
  190. * from acrs[15]. Result is a 64 bit value. Read the
  191. * 32 bit acrs[15] value and shift it by 32. Sick...
  192. */
  193. if (addr == (addr_t) &dummy->regs.acrs[15])
  194. tmp = ((unsigned long) child->thread.acrs[15]) << 32;
  195. else
  196. tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
  197. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  198. /*
  199. * orig_gpr2 is stored on the kernel stack
  200. */
  201. tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
  202. } else if (addr < (addr_t) &dummy->regs.fp_regs) {
  203. /*
  204. * prevent reads of padding hole between
  205. * orig_gpr2 and fp_regs on s390.
  206. */
  207. tmp = 0;
  208. } else if (addr == (addr_t) &dummy->regs.fp_regs.fpc) {
  209. /*
  210. * floating point control reg. is in the thread structure
  211. */
  212. tmp = child->thread.fpu.fpc;
  213. tmp <<= BITS_PER_LONG - 32;
  214. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  215. /*
  216. * floating point regs. are either in child->thread.fpu
  217. * or the child->thread.fpu.vxrs array
  218. */
  219. offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
  220. if (MACHINE_HAS_VX)
  221. tmp = *(addr_t *)
  222. ((addr_t) child->thread.fpu.vxrs + 2*offset);
  223. else
  224. tmp = *(addr_t *)
  225. ((addr_t) child->thread.fpu.fprs + offset);
  226. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  227. /*
  228. * Handle access to the per_info structure.
  229. */
  230. addr -= (addr_t) &dummy->regs.per_info;
  231. tmp = __peek_user_per(child, addr);
  232. } else
  233. tmp = 0;
  234. return tmp;
  235. }
  236. static int
  237. peek_user(struct task_struct *child, addr_t addr, addr_t data)
  238. {
  239. addr_t tmp, mask;
  240. /*
  241. * Stupid gdb peeks/pokes the access registers in 64 bit with
  242. * an alignment of 4. Programmers from hell...
  243. */
  244. mask = __ADDR_MASK;
  245. if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
  246. addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
  247. mask = 3;
  248. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  249. return -EIO;
  250. tmp = __peek_user(child, addr);
  251. return put_user(tmp, (addr_t __user *) data);
  252. }
  253. static inline void __poke_user_per(struct task_struct *child,
  254. addr_t addr, addr_t data)
  255. {
  256. struct per_struct_kernel *dummy = NULL;
  257. /*
  258. * There are only three fields in the per_info struct that the
  259. * debugger user can write to.
  260. * 1) cr9: the debugger wants to set a new PER event mask
  261. * 2) starting_addr: the debugger wants to set a new starting
  262. * address to use with the PER event mask.
  263. * 3) ending_addr: the debugger wants to set a new ending
  264. * address to use with the PER event mask.
  265. * The user specified PER event mask and the start and end
  266. * addresses are used only if single stepping is not in effect.
  267. * Writes to any other field in per_info are ignored.
  268. */
  269. if (addr == (addr_t) &dummy->cr9)
  270. /* PER event mask of the user specified per set. */
  271. child->thread.per_user.control =
  272. data & (PER_EVENT_MASK | PER_CONTROL_MASK);
  273. else if (addr == (addr_t) &dummy->starting_addr)
  274. /* Starting address of the user specified per set. */
  275. child->thread.per_user.start = data;
  276. else if (addr == (addr_t) &dummy->ending_addr)
  277. /* Ending address of the user specified per set. */
  278. child->thread.per_user.end = data;
  279. }
  280. /*
  281. * Write a word to the user area of a process at location addr. This
  282. * operation does have an additional problem compared to peek_user.
  283. * Stores to the program status word and on the floating point
  284. * control register needs to get checked for validity.
  285. */
  286. static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
  287. {
  288. struct user *dummy = NULL;
  289. addr_t offset;
  290. if (addr < (addr_t) &dummy->regs.acrs) {
  291. /*
  292. * psw and gprs are stored on the stack
  293. */
  294. if (addr == (addr_t) &dummy->regs.psw.mask) {
  295. unsigned long mask = PSW_MASK_USER;
  296. mask |= is_ri_task(child) ? PSW_MASK_RI : 0;
  297. if ((data ^ PSW_USER_BITS) & ~mask)
  298. /* Invalid psw mask. */
  299. return -EINVAL;
  300. if ((data & PSW_MASK_ASC) == PSW_ASC_HOME)
  301. /* Invalid address-space-control bits */
  302. return -EINVAL;
  303. if ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))
  304. /* Invalid addressing mode bits */
  305. return -EINVAL;
  306. }
  307. *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
  308. } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
  309. /*
  310. * access registers are stored in the thread structure
  311. */
  312. offset = addr - (addr_t) &dummy->regs.acrs;
  313. /*
  314. * Very special case: old & broken 64 bit gdb writing
  315. * to acrs[15] with a 64 bit value. Ignore the lower
  316. * half of the value and write the upper 32 bit to
  317. * acrs[15]. Sick...
  318. */
  319. if (addr == (addr_t) &dummy->regs.acrs[15])
  320. child->thread.acrs[15] = (unsigned int) (data >> 32);
  321. else
  322. *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
  323. } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
  324. /*
  325. * orig_gpr2 is stored on the kernel stack
  326. */
  327. task_pt_regs(child)->orig_gpr2 = data;
  328. } else if (addr < (addr_t) &dummy->regs.fp_regs) {
  329. /*
  330. * prevent writes of padding hole between
  331. * orig_gpr2 and fp_regs on s390.
  332. */
  333. return 0;
  334. } else if (addr == (addr_t) &dummy->regs.fp_regs.fpc) {
  335. /*
  336. * floating point control reg. is in the thread structure
  337. */
  338. if ((unsigned int) data != 0 ||
  339. test_fp_ctl(data >> (BITS_PER_LONG - 32)))
  340. return -EINVAL;
  341. child->thread.fpu.fpc = data >> (BITS_PER_LONG - 32);
  342. } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
  343. /*
  344. * floating point regs. are either in child->thread.fpu
  345. * or the child->thread.fpu.vxrs array
  346. */
  347. offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
  348. if (MACHINE_HAS_VX)
  349. *(addr_t *)((addr_t)
  350. child->thread.fpu.vxrs + 2*offset) = data;
  351. else
  352. *(addr_t *)((addr_t)
  353. child->thread.fpu.fprs + offset) = data;
  354. } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
  355. /*
  356. * Handle access to the per_info structure.
  357. */
  358. addr -= (addr_t) &dummy->regs.per_info;
  359. __poke_user_per(child, addr, data);
  360. }
  361. return 0;
  362. }
  363. static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
  364. {
  365. addr_t mask;
  366. /*
  367. * Stupid gdb peeks/pokes the access registers in 64 bit with
  368. * an alignment of 4. Programmers from hell indeed...
  369. */
  370. mask = __ADDR_MASK;
  371. if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
  372. addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
  373. mask = 3;
  374. if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
  375. return -EIO;
  376. return __poke_user(child, addr, data);
  377. }
  378. long arch_ptrace(struct task_struct *child, long request,
  379. unsigned long addr, unsigned long data)
  380. {
  381. ptrace_area parea;
  382. int copied, ret;
  383. switch (request) {
  384. case PTRACE_PEEKUSR:
  385. /* read the word at location addr in the USER area. */
  386. return peek_user(child, addr, data);
  387. case PTRACE_POKEUSR:
  388. /* write the word at location addr in the USER area */
  389. return poke_user(child, addr, data);
  390. case PTRACE_PEEKUSR_AREA:
  391. case PTRACE_POKEUSR_AREA:
  392. if (copy_from_user(&parea, (void __force __user *) addr,
  393. sizeof(parea)))
  394. return -EFAULT;
  395. addr = parea.kernel_addr;
  396. data = parea.process_addr;
  397. copied = 0;
  398. while (copied < parea.len) {
  399. if (request == PTRACE_PEEKUSR_AREA)
  400. ret = peek_user(child, addr, data);
  401. else {
  402. addr_t utmp;
  403. if (get_user(utmp,
  404. (addr_t __force __user *) data))
  405. return -EFAULT;
  406. ret = poke_user(child, addr, utmp);
  407. }
  408. if (ret)
  409. return ret;
  410. addr += sizeof(unsigned long);
  411. data += sizeof(unsigned long);
  412. copied += sizeof(unsigned long);
  413. }
  414. return 0;
  415. case PTRACE_GET_LAST_BREAK:
  416. put_user(task_thread_info(child)->last_break,
  417. (unsigned long __user *) data);
  418. return 0;
  419. case PTRACE_ENABLE_TE:
  420. if (!MACHINE_HAS_TE)
  421. return -EIO;
  422. child->thread.per_flags &= ~PER_FLAG_NO_TE;
  423. return 0;
  424. case PTRACE_DISABLE_TE:
  425. if (!MACHINE_HAS_TE)
  426. return -EIO;
  427. child->thread.per_flags |= PER_FLAG_NO_TE;
  428. child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
  429. return 0;
  430. case PTRACE_TE_ABORT_RAND:
  431. if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
  432. return -EIO;
  433. switch (data) {
  434. case 0UL:
  435. child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
  436. break;
  437. case 1UL:
  438. child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
  439. child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
  440. break;
  441. case 2UL:
  442. child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
  443. child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. return 0;
  449. default:
  450. return ptrace_request(child, request, addr, data);
  451. }
  452. }
  453. #ifdef CONFIG_COMPAT
  454. /*
  455. * Now the fun part starts... a 31 bit program running in the
  456. * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
  457. * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
  458. * to handle, the difference to the 64 bit versions of the requests
  459. * is that the access is done in multiples of 4 byte instead of
  460. * 8 bytes (sizeof(unsigned long) on 31/64 bit).
  461. * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
  462. * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
  463. * is a 31 bit program too, the content of struct user can be
  464. * emulated. A 31 bit program peeking into the struct user of
  465. * a 64 bit program is a no-no.
  466. */
  467. /*
  468. * Same as peek_user_per but for a 31 bit program.
  469. */
  470. static inline __u32 __peek_user_per_compat(struct task_struct *child,
  471. addr_t addr)
  472. {
  473. struct compat_per_struct_kernel *dummy32 = NULL;
  474. if (addr == (addr_t) &dummy32->cr9)
  475. /* Control bits of the active per set. */
  476. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  477. PER_EVENT_IFETCH : child->thread.per_user.control;
  478. else if (addr == (addr_t) &dummy32->cr10)
  479. /* Start address of the active per set. */
  480. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  481. 0 : child->thread.per_user.start;
  482. else if (addr == (addr_t) &dummy32->cr11)
  483. /* End address of the active per set. */
  484. return test_thread_flag(TIF_SINGLE_STEP) ?
  485. PSW32_ADDR_INSN : child->thread.per_user.end;
  486. else if (addr == (addr_t) &dummy32->bits)
  487. /* Single-step bit. */
  488. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  489. 0x80000000 : 0;
  490. else if (addr == (addr_t) &dummy32->starting_addr)
  491. /* Start address of the user specified per set. */
  492. return (__u32) child->thread.per_user.start;
  493. else if (addr == (addr_t) &dummy32->ending_addr)
  494. /* End address of the user specified per set. */
  495. return (__u32) child->thread.per_user.end;
  496. else if (addr == (addr_t) &dummy32->perc_atmid)
  497. /* PER code, ATMID and AI of the last PER trap */
  498. return (__u32) child->thread.per_event.cause << 16;
  499. else if (addr == (addr_t) &dummy32->address)
  500. /* Address of the last PER trap */
  501. return (__u32) child->thread.per_event.address;
  502. else if (addr == (addr_t) &dummy32->access_id)
  503. /* Access id of the last PER trap */
  504. return (__u32) child->thread.per_event.paid << 24;
  505. return 0;
  506. }
  507. /*
  508. * Same as peek_user but for a 31 bit program.
  509. */
  510. static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
  511. {
  512. struct compat_user *dummy32 = NULL;
  513. addr_t offset;
  514. __u32 tmp;
  515. if (addr < (addr_t) &dummy32->regs.acrs) {
  516. struct pt_regs *regs = task_pt_regs(child);
  517. /*
  518. * psw and gprs are stored on the stack
  519. */
  520. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  521. /* Fake a 31 bit psw mask. */
  522. tmp = (__u32)(regs->psw.mask >> 32);
  523. tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
  524. tmp |= PSW32_USER_BITS;
  525. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  526. /* Fake a 31 bit psw address. */
  527. tmp = (__u32) regs->psw.addr |
  528. (__u32)(regs->psw.mask & PSW_MASK_BA);
  529. } else {
  530. /* gpr 0-15 */
  531. tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
  532. }
  533. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  534. /*
  535. * access registers are stored in the thread structure
  536. */
  537. offset = addr - (addr_t) &dummy32->regs.acrs;
  538. tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
  539. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  540. /*
  541. * orig_gpr2 is stored on the kernel stack
  542. */
  543. tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
  544. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  545. /*
  546. * prevent reads of padding hole between
  547. * orig_gpr2 and fp_regs on s390.
  548. */
  549. tmp = 0;
  550. } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
  551. /*
  552. * floating point control reg. is in the thread structure
  553. */
  554. tmp = child->thread.fpu.fpc;
  555. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  556. /*
  557. * floating point regs. are either in child->thread.fpu
  558. * or the child->thread.fpu.vxrs array
  559. */
  560. offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
  561. if (MACHINE_HAS_VX)
  562. tmp = *(__u32 *)
  563. ((addr_t) child->thread.fpu.vxrs + 2*offset);
  564. else
  565. tmp = *(__u32 *)
  566. ((addr_t) child->thread.fpu.fprs + offset);
  567. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  568. /*
  569. * Handle access to the per_info structure.
  570. */
  571. addr -= (addr_t) &dummy32->regs.per_info;
  572. tmp = __peek_user_per_compat(child, addr);
  573. } else
  574. tmp = 0;
  575. return tmp;
  576. }
  577. static int peek_user_compat(struct task_struct *child,
  578. addr_t addr, addr_t data)
  579. {
  580. __u32 tmp;
  581. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
  582. return -EIO;
  583. tmp = __peek_user_compat(child, addr);
  584. return put_user(tmp, (__u32 __user *) data);
  585. }
  586. /*
  587. * Same as poke_user_per but for a 31 bit program.
  588. */
  589. static inline void __poke_user_per_compat(struct task_struct *child,
  590. addr_t addr, __u32 data)
  591. {
  592. struct compat_per_struct_kernel *dummy32 = NULL;
  593. if (addr == (addr_t) &dummy32->cr9)
  594. /* PER event mask of the user specified per set. */
  595. child->thread.per_user.control =
  596. data & (PER_EVENT_MASK | PER_CONTROL_MASK);
  597. else if (addr == (addr_t) &dummy32->starting_addr)
  598. /* Starting address of the user specified per set. */
  599. child->thread.per_user.start = data;
  600. else if (addr == (addr_t) &dummy32->ending_addr)
  601. /* Ending address of the user specified per set. */
  602. child->thread.per_user.end = data;
  603. }
  604. /*
  605. * Same as poke_user but for a 31 bit program.
  606. */
  607. static int __poke_user_compat(struct task_struct *child,
  608. addr_t addr, addr_t data)
  609. {
  610. struct compat_user *dummy32 = NULL;
  611. __u32 tmp = (__u32) data;
  612. addr_t offset;
  613. if (addr < (addr_t) &dummy32->regs.acrs) {
  614. struct pt_regs *regs = task_pt_regs(child);
  615. /*
  616. * psw, gprs, acrs and orig_gpr2 are stored on the stack
  617. */
  618. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  619. __u32 mask = PSW32_MASK_USER;
  620. mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
  621. /* Build a 64 bit psw mask from 31 bit mask. */
  622. if ((tmp ^ PSW32_USER_BITS) & ~mask)
  623. /* Invalid psw mask. */
  624. return -EINVAL;
  625. if ((data & PSW32_MASK_ASC) == PSW32_ASC_HOME)
  626. /* Invalid address-space-control bits */
  627. return -EINVAL;
  628. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
  629. (regs->psw.mask & PSW_MASK_BA) |
  630. (__u64)(tmp & mask) << 32;
  631. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  632. /* Build a 64 bit psw address from 31 bit address. */
  633. regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
  634. /* Transfer 31 bit amode bit to psw mask. */
  635. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
  636. (__u64)(tmp & PSW32_ADDR_AMODE);
  637. } else {
  638. /* gpr 0-15 */
  639. *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
  640. }
  641. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  642. /*
  643. * access registers are stored in the thread structure
  644. */
  645. offset = addr - (addr_t) &dummy32->regs.acrs;
  646. *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
  647. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  648. /*
  649. * orig_gpr2 is stored on the kernel stack
  650. */
  651. *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
  652. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  653. /*
  654. * prevent writess of padding hole between
  655. * orig_gpr2 and fp_regs on s390.
  656. */
  657. return 0;
  658. } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
  659. /*
  660. * floating point control reg. is in the thread structure
  661. */
  662. if (test_fp_ctl(tmp))
  663. return -EINVAL;
  664. child->thread.fpu.fpc = data;
  665. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  666. /*
  667. * floating point regs. are either in child->thread.fpu
  668. * or the child->thread.fpu.vxrs array
  669. */
  670. offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
  671. if (MACHINE_HAS_VX)
  672. *(__u32 *)((addr_t)
  673. child->thread.fpu.vxrs + 2*offset) = tmp;
  674. else
  675. *(__u32 *)((addr_t)
  676. child->thread.fpu.fprs + offset) = tmp;
  677. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  678. /*
  679. * Handle access to the per_info structure.
  680. */
  681. addr -= (addr_t) &dummy32->regs.per_info;
  682. __poke_user_per_compat(child, addr, data);
  683. }
  684. return 0;
  685. }
  686. static int poke_user_compat(struct task_struct *child,
  687. addr_t addr, addr_t data)
  688. {
  689. if (!is_compat_task() || (addr & 3) ||
  690. addr > sizeof(struct compat_user) - 3)
  691. return -EIO;
  692. return __poke_user_compat(child, addr, data);
  693. }
  694. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  695. compat_ulong_t caddr, compat_ulong_t cdata)
  696. {
  697. unsigned long addr = caddr;
  698. unsigned long data = cdata;
  699. compat_ptrace_area parea;
  700. int copied, ret;
  701. switch (request) {
  702. case PTRACE_PEEKUSR:
  703. /* read the word at location addr in the USER area. */
  704. return peek_user_compat(child, addr, data);
  705. case PTRACE_POKEUSR:
  706. /* write the word at location addr in the USER area */
  707. return poke_user_compat(child, addr, data);
  708. case PTRACE_PEEKUSR_AREA:
  709. case PTRACE_POKEUSR_AREA:
  710. if (copy_from_user(&parea, (void __force __user *) addr,
  711. sizeof(parea)))
  712. return -EFAULT;
  713. addr = parea.kernel_addr;
  714. data = parea.process_addr;
  715. copied = 0;
  716. while (copied < parea.len) {
  717. if (request == PTRACE_PEEKUSR_AREA)
  718. ret = peek_user_compat(child, addr, data);
  719. else {
  720. __u32 utmp;
  721. if (get_user(utmp,
  722. (__u32 __force __user *) data))
  723. return -EFAULT;
  724. ret = poke_user_compat(child, addr, utmp);
  725. }
  726. if (ret)
  727. return ret;
  728. addr += sizeof(unsigned int);
  729. data += sizeof(unsigned int);
  730. copied += sizeof(unsigned int);
  731. }
  732. return 0;
  733. case PTRACE_GET_LAST_BREAK:
  734. put_user(task_thread_info(child)->last_break,
  735. (unsigned int __user *) data);
  736. return 0;
  737. }
  738. return compat_ptrace_request(child, request, addr, data);
  739. }
  740. #endif
  741. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  742. {
  743. unsigned long mask = -1UL;
  744. /*
  745. * The sysc_tracesys code in entry.S stored the system
  746. * call number to gprs[2].
  747. */
  748. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  749. (tracehook_report_syscall_entry(regs) ||
  750. regs->gprs[2] >= NR_syscalls)) {
  751. /*
  752. * Tracing decided this syscall should not happen or the
  753. * debugger stored an invalid system call number. Skip
  754. * the system call and the system call restart handling.
  755. */
  756. clear_pt_regs_flag(regs, PIF_SYSCALL);
  757. return -1;
  758. }
  759. /* Do the secure computing check after ptrace. */
  760. if (secure_computing(NULL)) {
  761. /* seccomp failures shouldn't expose any additional code. */
  762. return -1;
  763. }
  764. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  765. trace_sys_enter(regs, regs->gprs[2]);
  766. if (is_compat_task())
  767. mask = 0xffffffff;
  768. audit_syscall_entry(regs->gprs[2], regs->orig_gpr2 & mask,
  769. regs->gprs[3] &mask, regs->gprs[4] &mask,
  770. regs->gprs[5] &mask);
  771. return regs->gprs[2];
  772. }
  773. asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
  774. {
  775. audit_syscall_exit(regs);
  776. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  777. trace_sys_exit(regs, regs->gprs[2]);
  778. if (test_thread_flag(TIF_SYSCALL_TRACE))
  779. tracehook_report_syscall_exit(regs, 0);
  780. }
  781. /*
  782. * user_regset definitions.
  783. */
  784. static int s390_regs_get(struct task_struct *target,
  785. const struct user_regset *regset,
  786. unsigned int pos, unsigned int count,
  787. void *kbuf, void __user *ubuf)
  788. {
  789. if (target == current)
  790. save_access_regs(target->thread.acrs);
  791. if (kbuf) {
  792. unsigned long *k = kbuf;
  793. while (count > 0) {
  794. *k++ = __peek_user(target, pos);
  795. count -= sizeof(*k);
  796. pos += sizeof(*k);
  797. }
  798. } else {
  799. unsigned long __user *u = ubuf;
  800. while (count > 0) {
  801. if (__put_user(__peek_user(target, pos), u++))
  802. return -EFAULT;
  803. count -= sizeof(*u);
  804. pos += sizeof(*u);
  805. }
  806. }
  807. return 0;
  808. }
  809. static int s390_regs_set(struct task_struct *target,
  810. const struct user_regset *regset,
  811. unsigned int pos, unsigned int count,
  812. const void *kbuf, const void __user *ubuf)
  813. {
  814. int rc = 0;
  815. if (target == current)
  816. save_access_regs(target->thread.acrs);
  817. if (kbuf) {
  818. const unsigned long *k = kbuf;
  819. while (count > 0 && !rc) {
  820. rc = __poke_user(target, pos, *k++);
  821. count -= sizeof(*k);
  822. pos += sizeof(*k);
  823. }
  824. } else {
  825. const unsigned long __user *u = ubuf;
  826. while (count > 0 && !rc) {
  827. unsigned long word;
  828. rc = __get_user(word, u++);
  829. if (rc)
  830. break;
  831. rc = __poke_user(target, pos, word);
  832. count -= sizeof(*u);
  833. pos += sizeof(*u);
  834. }
  835. }
  836. if (rc == 0 && target == current)
  837. restore_access_regs(target->thread.acrs);
  838. return rc;
  839. }
  840. static int s390_fpregs_get(struct task_struct *target,
  841. const struct user_regset *regset, unsigned int pos,
  842. unsigned int count, void *kbuf, void __user *ubuf)
  843. {
  844. _s390_fp_regs fp_regs;
  845. if (target == current)
  846. save_fpu_regs();
  847. fp_regs.fpc = target->thread.fpu.fpc;
  848. fpregs_store(&fp_regs, &target->thread.fpu);
  849. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  850. &fp_regs, 0, -1);
  851. }
  852. static int s390_fpregs_set(struct task_struct *target,
  853. const struct user_regset *regset, unsigned int pos,
  854. unsigned int count, const void *kbuf,
  855. const void __user *ubuf)
  856. {
  857. int rc = 0;
  858. freg_t fprs[__NUM_FPRS];
  859. if (target == current)
  860. save_fpu_regs();
  861. if (MACHINE_HAS_VX)
  862. convert_vx_to_fp(fprs, target->thread.fpu.vxrs);
  863. else
  864. memcpy(&fprs, target->thread.fpu.fprs, sizeof(fprs));
  865. /* If setting FPC, must validate it first. */
  866. if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
  867. u32 ufpc[2] = { target->thread.fpu.fpc, 0 };
  868. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
  869. 0, offsetof(s390_fp_regs, fprs));
  870. if (rc)
  871. return rc;
  872. if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
  873. return -EINVAL;
  874. target->thread.fpu.fpc = ufpc[0];
  875. }
  876. if (rc == 0 && count > 0)
  877. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  878. fprs, offsetof(s390_fp_regs, fprs), -1);
  879. if (rc)
  880. return rc;
  881. if (MACHINE_HAS_VX)
  882. convert_fp_to_vx(target->thread.fpu.vxrs, fprs);
  883. else
  884. memcpy(target->thread.fpu.fprs, &fprs, sizeof(fprs));
  885. return rc;
  886. }
  887. static int s390_last_break_get(struct task_struct *target,
  888. const struct user_regset *regset,
  889. unsigned int pos, unsigned int count,
  890. void *kbuf, void __user *ubuf)
  891. {
  892. if (count > 0) {
  893. if (kbuf) {
  894. unsigned long *k = kbuf;
  895. *k = task_thread_info(target)->last_break;
  896. } else {
  897. unsigned long __user *u = ubuf;
  898. if (__put_user(task_thread_info(target)->last_break, u))
  899. return -EFAULT;
  900. }
  901. }
  902. return 0;
  903. }
  904. static int s390_last_break_set(struct task_struct *target,
  905. const struct user_regset *regset,
  906. unsigned int pos, unsigned int count,
  907. const void *kbuf, const void __user *ubuf)
  908. {
  909. return 0;
  910. }
  911. static int s390_tdb_get(struct task_struct *target,
  912. const struct user_regset *regset,
  913. unsigned int pos, unsigned int count,
  914. void *kbuf, void __user *ubuf)
  915. {
  916. struct pt_regs *regs = task_pt_regs(target);
  917. unsigned char *data;
  918. if (!(regs->int_code & 0x200))
  919. return -ENODATA;
  920. data = target->thread.trap_tdb;
  921. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
  922. }
  923. static int s390_tdb_set(struct task_struct *target,
  924. const struct user_regset *regset,
  925. unsigned int pos, unsigned int count,
  926. const void *kbuf, const void __user *ubuf)
  927. {
  928. return 0;
  929. }
  930. static int s390_vxrs_low_get(struct task_struct *target,
  931. const struct user_regset *regset,
  932. unsigned int pos, unsigned int count,
  933. void *kbuf, void __user *ubuf)
  934. {
  935. __u64 vxrs[__NUM_VXRS_LOW];
  936. int i;
  937. if (!MACHINE_HAS_VX)
  938. return -ENODEV;
  939. if (target == current)
  940. save_fpu_regs();
  941. for (i = 0; i < __NUM_VXRS_LOW; i++)
  942. vxrs[i] = *((__u64 *)(target->thread.fpu.vxrs + i) + 1);
  943. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  944. }
  945. static int s390_vxrs_low_set(struct task_struct *target,
  946. const struct user_regset *regset,
  947. unsigned int pos, unsigned int count,
  948. const void *kbuf, const void __user *ubuf)
  949. {
  950. __u64 vxrs[__NUM_VXRS_LOW];
  951. int i, rc;
  952. if (!MACHINE_HAS_VX)
  953. return -ENODEV;
  954. if (target == current)
  955. save_fpu_regs();
  956. for (i = 0; i < __NUM_VXRS_LOW; i++)
  957. vxrs[i] = *((__u64 *)(target->thread.fpu.vxrs + i) + 1);
  958. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  959. if (rc == 0)
  960. for (i = 0; i < __NUM_VXRS_LOW; i++)
  961. *((__u64 *)(target->thread.fpu.vxrs + i) + 1) = vxrs[i];
  962. return rc;
  963. }
  964. static int s390_vxrs_high_get(struct task_struct *target,
  965. const struct user_regset *regset,
  966. unsigned int pos, unsigned int count,
  967. void *kbuf, void __user *ubuf)
  968. {
  969. __vector128 vxrs[__NUM_VXRS_HIGH];
  970. if (!MACHINE_HAS_VX)
  971. return -ENODEV;
  972. if (target == current)
  973. save_fpu_regs();
  974. memcpy(vxrs, target->thread.fpu.vxrs + __NUM_VXRS_LOW, sizeof(vxrs));
  975. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  976. }
  977. static int s390_vxrs_high_set(struct task_struct *target,
  978. const struct user_regset *regset,
  979. unsigned int pos, unsigned int count,
  980. const void *kbuf, const void __user *ubuf)
  981. {
  982. int rc;
  983. if (!MACHINE_HAS_VX)
  984. return -ENODEV;
  985. if (target == current)
  986. save_fpu_regs();
  987. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  988. target->thread.fpu.vxrs + __NUM_VXRS_LOW, 0, -1);
  989. return rc;
  990. }
  991. static int s390_system_call_get(struct task_struct *target,
  992. const struct user_regset *regset,
  993. unsigned int pos, unsigned int count,
  994. void *kbuf, void __user *ubuf)
  995. {
  996. unsigned int *data = &task_thread_info(target)->system_call;
  997. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  998. data, 0, sizeof(unsigned int));
  999. }
  1000. static int s390_system_call_set(struct task_struct *target,
  1001. const struct user_regset *regset,
  1002. unsigned int pos, unsigned int count,
  1003. const void *kbuf, const void __user *ubuf)
  1004. {
  1005. unsigned int *data = &task_thread_info(target)->system_call;
  1006. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1007. data, 0, sizeof(unsigned int));
  1008. }
  1009. static const struct user_regset s390_regsets[] = {
  1010. {
  1011. .core_note_type = NT_PRSTATUS,
  1012. .n = sizeof(s390_regs) / sizeof(long),
  1013. .size = sizeof(long),
  1014. .align = sizeof(long),
  1015. .get = s390_regs_get,
  1016. .set = s390_regs_set,
  1017. },
  1018. {
  1019. .core_note_type = NT_PRFPREG,
  1020. .n = sizeof(s390_fp_regs) / sizeof(long),
  1021. .size = sizeof(long),
  1022. .align = sizeof(long),
  1023. .get = s390_fpregs_get,
  1024. .set = s390_fpregs_set,
  1025. },
  1026. {
  1027. .core_note_type = NT_S390_SYSTEM_CALL,
  1028. .n = 1,
  1029. .size = sizeof(unsigned int),
  1030. .align = sizeof(unsigned int),
  1031. .get = s390_system_call_get,
  1032. .set = s390_system_call_set,
  1033. },
  1034. {
  1035. .core_note_type = NT_S390_LAST_BREAK,
  1036. .n = 1,
  1037. .size = sizeof(long),
  1038. .align = sizeof(long),
  1039. .get = s390_last_break_get,
  1040. .set = s390_last_break_set,
  1041. },
  1042. {
  1043. .core_note_type = NT_S390_TDB,
  1044. .n = 1,
  1045. .size = 256,
  1046. .align = 1,
  1047. .get = s390_tdb_get,
  1048. .set = s390_tdb_set,
  1049. },
  1050. {
  1051. .core_note_type = NT_S390_VXRS_LOW,
  1052. .n = __NUM_VXRS_LOW,
  1053. .size = sizeof(__u64),
  1054. .align = sizeof(__u64),
  1055. .get = s390_vxrs_low_get,
  1056. .set = s390_vxrs_low_set,
  1057. },
  1058. {
  1059. .core_note_type = NT_S390_VXRS_HIGH,
  1060. .n = __NUM_VXRS_HIGH,
  1061. .size = sizeof(__vector128),
  1062. .align = sizeof(__vector128),
  1063. .get = s390_vxrs_high_get,
  1064. .set = s390_vxrs_high_set,
  1065. },
  1066. };
  1067. static const struct user_regset_view user_s390_view = {
  1068. .name = UTS_MACHINE,
  1069. .e_machine = EM_S390,
  1070. .regsets = s390_regsets,
  1071. .n = ARRAY_SIZE(s390_regsets)
  1072. };
  1073. #ifdef CONFIG_COMPAT
  1074. static int s390_compat_regs_get(struct task_struct *target,
  1075. const struct user_regset *regset,
  1076. unsigned int pos, unsigned int count,
  1077. void *kbuf, void __user *ubuf)
  1078. {
  1079. if (target == current)
  1080. save_access_regs(target->thread.acrs);
  1081. if (kbuf) {
  1082. compat_ulong_t *k = kbuf;
  1083. while (count > 0) {
  1084. *k++ = __peek_user_compat(target, pos);
  1085. count -= sizeof(*k);
  1086. pos += sizeof(*k);
  1087. }
  1088. } else {
  1089. compat_ulong_t __user *u = ubuf;
  1090. while (count > 0) {
  1091. if (__put_user(__peek_user_compat(target, pos), u++))
  1092. return -EFAULT;
  1093. count -= sizeof(*u);
  1094. pos += sizeof(*u);
  1095. }
  1096. }
  1097. return 0;
  1098. }
  1099. static int s390_compat_regs_set(struct task_struct *target,
  1100. const struct user_regset *regset,
  1101. unsigned int pos, unsigned int count,
  1102. const void *kbuf, const void __user *ubuf)
  1103. {
  1104. int rc = 0;
  1105. if (target == current)
  1106. save_access_regs(target->thread.acrs);
  1107. if (kbuf) {
  1108. const compat_ulong_t *k = kbuf;
  1109. while (count > 0 && !rc) {
  1110. rc = __poke_user_compat(target, pos, *k++);
  1111. count -= sizeof(*k);
  1112. pos += sizeof(*k);
  1113. }
  1114. } else {
  1115. const compat_ulong_t __user *u = ubuf;
  1116. while (count > 0 && !rc) {
  1117. compat_ulong_t word;
  1118. rc = __get_user(word, u++);
  1119. if (rc)
  1120. break;
  1121. rc = __poke_user_compat(target, pos, word);
  1122. count -= sizeof(*u);
  1123. pos += sizeof(*u);
  1124. }
  1125. }
  1126. if (rc == 0 && target == current)
  1127. restore_access_regs(target->thread.acrs);
  1128. return rc;
  1129. }
  1130. static int s390_compat_regs_high_get(struct task_struct *target,
  1131. const struct user_regset *regset,
  1132. unsigned int pos, unsigned int count,
  1133. void *kbuf, void __user *ubuf)
  1134. {
  1135. compat_ulong_t *gprs_high;
  1136. gprs_high = (compat_ulong_t *)
  1137. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  1138. if (kbuf) {
  1139. compat_ulong_t *k = kbuf;
  1140. while (count > 0) {
  1141. *k++ = *gprs_high;
  1142. gprs_high += 2;
  1143. count -= sizeof(*k);
  1144. }
  1145. } else {
  1146. compat_ulong_t __user *u = ubuf;
  1147. while (count > 0) {
  1148. if (__put_user(*gprs_high, u++))
  1149. return -EFAULT;
  1150. gprs_high += 2;
  1151. count -= sizeof(*u);
  1152. }
  1153. }
  1154. return 0;
  1155. }
  1156. static int s390_compat_regs_high_set(struct task_struct *target,
  1157. const struct user_regset *regset,
  1158. unsigned int pos, unsigned int count,
  1159. const void *kbuf, const void __user *ubuf)
  1160. {
  1161. compat_ulong_t *gprs_high;
  1162. int rc = 0;
  1163. gprs_high = (compat_ulong_t *)
  1164. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  1165. if (kbuf) {
  1166. const compat_ulong_t *k = kbuf;
  1167. while (count > 0) {
  1168. *gprs_high = *k++;
  1169. *gprs_high += 2;
  1170. count -= sizeof(*k);
  1171. }
  1172. } else {
  1173. const compat_ulong_t __user *u = ubuf;
  1174. while (count > 0 && !rc) {
  1175. unsigned long word;
  1176. rc = __get_user(word, u++);
  1177. if (rc)
  1178. break;
  1179. *gprs_high = word;
  1180. *gprs_high += 2;
  1181. count -= sizeof(*u);
  1182. }
  1183. }
  1184. return rc;
  1185. }
  1186. static int s390_compat_last_break_get(struct task_struct *target,
  1187. const struct user_regset *regset,
  1188. unsigned int pos, unsigned int count,
  1189. void *kbuf, void __user *ubuf)
  1190. {
  1191. compat_ulong_t last_break;
  1192. if (count > 0) {
  1193. last_break = task_thread_info(target)->last_break;
  1194. if (kbuf) {
  1195. unsigned long *k = kbuf;
  1196. *k = last_break;
  1197. } else {
  1198. unsigned long __user *u = ubuf;
  1199. if (__put_user(last_break, u))
  1200. return -EFAULT;
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. static int s390_compat_last_break_set(struct task_struct *target,
  1206. const struct user_regset *regset,
  1207. unsigned int pos, unsigned int count,
  1208. const void *kbuf, const void __user *ubuf)
  1209. {
  1210. return 0;
  1211. }
  1212. static const struct user_regset s390_compat_regsets[] = {
  1213. {
  1214. .core_note_type = NT_PRSTATUS,
  1215. .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
  1216. .size = sizeof(compat_long_t),
  1217. .align = sizeof(compat_long_t),
  1218. .get = s390_compat_regs_get,
  1219. .set = s390_compat_regs_set,
  1220. },
  1221. {
  1222. .core_note_type = NT_PRFPREG,
  1223. .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
  1224. .size = sizeof(compat_long_t),
  1225. .align = sizeof(compat_long_t),
  1226. .get = s390_fpregs_get,
  1227. .set = s390_fpregs_set,
  1228. },
  1229. {
  1230. .core_note_type = NT_S390_SYSTEM_CALL,
  1231. .n = 1,
  1232. .size = sizeof(compat_uint_t),
  1233. .align = sizeof(compat_uint_t),
  1234. .get = s390_system_call_get,
  1235. .set = s390_system_call_set,
  1236. },
  1237. {
  1238. .core_note_type = NT_S390_LAST_BREAK,
  1239. .n = 1,
  1240. .size = sizeof(long),
  1241. .align = sizeof(long),
  1242. .get = s390_compat_last_break_get,
  1243. .set = s390_compat_last_break_set,
  1244. },
  1245. {
  1246. .core_note_type = NT_S390_TDB,
  1247. .n = 1,
  1248. .size = 256,
  1249. .align = 1,
  1250. .get = s390_tdb_get,
  1251. .set = s390_tdb_set,
  1252. },
  1253. {
  1254. .core_note_type = NT_S390_VXRS_LOW,
  1255. .n = __NUM_VXRS_LOW,
  1256. .size = sizeof(__u64),
  1257. .align = sizeof(__u64),
  1258. .get = s390_vxrs_low_get,
  1259. .set = s390_vxrs_low_set,
  1260. },
  1261. {
  1262. .core_note_type = NT_S390_VXRS_HIGH,
  1263. .n = __NUM_VXRS_HIGH,
  1264. .size = sizeof(__vector128),
  1265. .align = sizeof(__vector128),
  1266. .get = s390_vxrs_high_get,
  1267. .set = s390_vxrs_high_set,
  1268. },
  1269. {
  1270. .core_note_type = NT_S390_HIGH_GPRS,
  1271. .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
  1272. .size = sizeof(compat_long_t),
  1273. .align = sizeof(compat_long_t),
  1274. .get = s390_compat_regs_high_get,
  1275. .set = s390_compat_regs_high_set,
  1276. },
  1277. };
  1278. static const struct user_regset_view user_s390_compat_view = {
  1279. .name = "s390",
  1280. .e_machine = EM_S390,
  1281. .regsets = s390_compat_regsets,
  1282. .n = ARRAY_SIZE(s390_compat_regsets)
  1283. };
  1284. #endif
  1285. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1286. {
  1287. #ifdef CONFIG_COMPAT
  1288. if (test_tsk_thread_flag(task, TIF_31BIT))
  1289. return &user_s390_compat_view;
  1290. #endif
  1291. return &user_s390_view;
  1292. }
  1293. static const char *gpr_names[NUM_GPRS] = {
  1294. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  1295. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  1296. };
  1297. unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
  1298. {
  1299. if (offset >= NUM_GPRS)
  1300. return 0;
  1301. return regs->gprs[offset];
  1302. }
  1303. int regs_query_register_offset(const char *name)
  1304. {
  1305. unsigned long offset;
  1306. if (!name || *name != 'r')
  1307. return -EINVAL;
  1308. if (kstrtoul(name + 1, 10, &offset))
  1309. return -EINVAL;
  1310. if (offset >= NUM_GPRS)
  1311. return -EINVAL;
  1312. return offset;
  1313. }
  1314. const char *regs_query_register_name(unsigned int offset)
  1315. {
  1316. if (offset >= NUM_GPRS)
  1317. return NULL;
  1318. return gpr_names[offset];
  1319. }
  1320. static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  1321. {
  1322. unsigned long ksp = kernel_stack_pointer(regs);
  1323. return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
  1324. }
  1325. /**
  1326. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  1327. * @regs:pt_regs which contains kernel stack pointer.
  1328. * @n:stack entry number.
  1329. *
  1330. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  1331. * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
  1332. * this returns 0.
  1333. */
  1334. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  1335. {
  1336. unsigned long addr;
  1337. addr = kernel_stack_pointer(regs) + n * sizeof(long);
  1338. if (!regs_within_kernel_stack(regs, addr))
  1339. return 0;
  1340. return *(unsigned long *)addr;
  1341. }