ptrace.c 46 KB

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