ptrace.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 = PSW_ADDR_INSN;
  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. PSW_ADDR_INSN : 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. /* Removing high order bit from addr (only for 31 bit). */
  451. addr &= PSW_ADDR_INSN;
  452. return ptrace_request(child, request, addr, data);
  453. }
  454. }
  455. #ifdef CONFIG_COMPAT
  456. /*
  457. * Now the fun part starts... a 31 bit program running in the
  458. * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
  459. * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
  460. * to handle, the difference to the 64 bit versions of the requests
  461. * is that the access is done in multiples of 4 byte instead of
  462. * 8 bytes (sizeof(unsigned long) on 31/64 bit).
  463. * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
  464. * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
  465. * is a 31 bit program too, the content of struct user can be
  466. * emulated. A 31 bit program peeking into the struct user of
  467. * a 64 bit program is a no-no.
  468. */
  469. /*
  470. * Same as peek_user_per but for a 31 bit program.
  471. */
  472. static inline __u32 __peek_user_per_compat(struct task_struct *child,
  473. addr_t addr)
  474. {
  475. struct compat_per_struct_kernel *dummy32 = NULL;
  476. if (addr == (addr_t) &dummy32->cr9)
  477. /* Control bits of the active per set. */
  478. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  479. PER_EVENT_IFETCH : child->thread.per_user.control;
  480. else if (addr == (addr_t) &dummy32->cr10)
  481. /* Start address of the active per set. */
  482. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  483. 0 : child->thread.per_user.start;
  484. else if (addr == (addr_t) &dummy32->cr11)
  485. /* End address of the active per set. */
  486. return test_thread_flag(TIF_SINGLE_STEP) ?
  487. PSW32_ADDR_INSN : child->thread.per_user.end;
  488. else if (addr == (addr_t) &dummy32->bits)
  489. /* Single-step bit. */
  490. return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
  491. 0x80000000 : 0;
  492. else if (addr == (addr_t) &dummy32->starting_addr)
  493. /* Start address of the user specified per set. */
  494. return (__u32) child->thread.per_user.start;
  495. else if (addr == (addr_t) &dummy32->ending_addr)
  496. /* End address of the user specified per set. */
  497. return (__u32) child->thread.per_user.end;
  498. else if (addr == (addr_t) &dummy32->perc_atmid)
  499. /* PER code, ATMID and AI of the last PER trap */
  500. return (__u32) child->thread.per_event.cause << 16;
  501. else if (addr == (addr_t) &dummy32->address)
  502. /* Address of the last PER trap */
  503. return (__u32) child->thread.per_event.address;
  504. else if (addr == (addr_t) &dummy32->access_id)
  505. /* Access id of the last PER trap */
  506. return (__u32) child->thread.per_event.paid << 24;
  507. return 0;
  508. }
  509. /*
  510. * Same as peek_user but for a 31 bit program.
  511. */
  512. static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
  513. {
  514. struct compat_user *dummy32 = NULL;
  515. addr_t offset;
  516. __u32 tmp;
  517. if (addr < (addr_t) &dummy32->regs.acrs) {
  518. struct pt_regs *regs = task_pt_regs(child);
  519. /*
  520. * psw and gprs are stored on the stack
  521. */
  522. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  523. /* Fake a 31 bit psw mask. */
  524. tmp = (__u32)(regs->psw.mask >> 32);
  525. tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
  526. tmp |= PSW32_USER_BITS;
  527. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  528. /* Fake a 31 bit psw address. */
  529. tmp = (__u32) regs->psw.addr |
  530. (__u32)(regs->psw.mask & PSW_MASK_BA);
  531. } else {
  532. /* gpr 0-15 */
  533. tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
  534. }
  535. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  536. /*
  537. * access registers are stored in the thread structure
  538. */
  539. offset = addr - (addr_t) &dummy32->regs.acrs;
  540. tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
  541. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  542. /*
  543. * orig_gpr2 is stored on the kernel stack
  544. */
  545. tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
  546. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  547. /*
  548. * prevent reads of padding hole between
  549. * orig_gpr2 and fp_regs on s390.
  550. */
  551. tmp = 0;
  552. } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
  553. /*
  554. * floating point control reg. is in the thread structure
  555. */
  556. tmp = child->thread.fpu.fpc;
  557. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  558. /*
  559. * floating point regs. are either in child->thread.fpu
  560. * or the child->thread.fpu.vxrs array
  561. */
  562. offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
  563. if (MACHINE_HAS_VX)
  564. tmp = *(__u32 *)
  565. ((addr_t) child->thread.fpu.vxrs + 2*offset);
  566. else
  567. tmp = *(__u32 *)
  568. ((addr_t) child->thread.fpu.fprs + offset);
  569. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  570. /*
  571. * Handle access to the per_info structure.
  572. */
  573. addr -= (addr_t) &dummy32->regs.per_info;
  574. tmp = __peek_user_per_compat(child, addr);
  575. } else
  576. tmp = 0;
  577. return tmp;
  578. }
  579. static int peek_user_compat(struct task_struct *child,
  580. addr_t addr, addr_t data)
  581. {
  582. __u32 tmp;
  583. if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
  584. return -EIO;
  585. tmp = __peek_user_compat(child, addr);
  586. return put_user(tmp, (__u32 __user *) data);
  587. }
  588. /*
  589. * Same as poke_user_per but for a 31 bit program.
  590. */
  591. static inline void __poke_user_per_compat(struct task_struct *child,
  592. addr_t addr, __u32 data)
  593. {
  594. struct compat_per_struct_kernel *dummy32 = NULL;
  595. if (addr == (addr_t) &dummy32->cr9)
  596. /* PER event mask of the user specified per set. */
  597. child->thread.per_user.control =
  598. data & (PER_EVENT_MASK | PER_CONTROL_MASK);
  599. else if (addr == (addr_t) &dummy32->starting_addr)
  600. /* Starting address of the user specified per set. */
  601. child->thread.per_user.start = data;
  602. else if (addr == (addr_t) &dummy32->ending_addr)
  603. /* Ending address of the user specified per set. */
  604. child->thread.per_user.end = data;
  605. }
  606. /*
  607. * Same as poke_user but for a 31 bit program.
  608. */
  609. static int __poke_user_compat(struct task_struct *child,
  610. addr_t addr, addr_t data)
  611. {
  612. struct compat_user *dummy32 = NULL;
  613. __u32 tmp = (__u32) data;
  614. addr_t offset;
  615. if (addr < (addr_t) &dummy32->regs.acrs) {
  616. struct pt_regs *regs = task_pt_regs(child);
  617. /*
  618. * psw, gprs, acrs and orig_gpr2 are stored on the stack
  619. */
  620. if (addr == (addr_t) &dummy32->regs.psw.mask) {
  621. __u32 mask = PSW32_MASK_USER;
  622. mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
  623. /* Build a 64 bit psw mask from 31 bit mask. */
  624. if ((tmp ^ PSW32_USER_BITS) & ~mask)
  625. /* Invalid psw mask. */
  626. return -EINVAL;
  627. if ((data & PSW32_MASK_ASC) == PSW32_ASC_HOME)
  628. /* Invalid address-space-control bits */
  629. return -EINVAL;
  630. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
  631. (regs->psw.mask & PSW_MASK_BA) |
  632. (__u64)(tmp & mask) << 32;
  633. } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
  634. /* Build a 64 bit psw address from 31 bit address. */
  635. regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
  636. /* Transfer 31 bit amode bit to psw mask. */
  637. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
  638. (__u64)(tmp & PSW32_ADDR_AMODE);
  639. } else {
  640. /* gpr 0-15 */
  641. *(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
  642. }
  643. } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
  644. /*
  645. * access registers are stored in the thread structure
  646. */
  647. offset = addr - (addr_t) &dummy32->regs.acrs;
  648. *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
  649. } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
  650. /*
  651. * orig_gpr2 is stored on the kernel stack
  652. */
  653. *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
  654. } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
  655. /*
  656. * prevent writess of padding hole between
  657. * orig_gpr2 and fp_regs on s390.
  658. */
  659. return 0;
  660. } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
  661. /*
  662. * floating point control reg. is in the thread structure
  663. */
  664. if (test_fp_ctl(tmp))
  665. return -EINVAL;
  666. child->thread.fpu.fpc = data;
  667. } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
  668. /*
  669. * floating point regs. are either in child->thread.fpu
  670. * or the child->thread.fpu.vxrs array
  671. */
  672. offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
  673. if (MACHINE_HAS_VX)
  674. *(__u32 *)((addr_t)
  675. child->thread.fpu.vxrs + 2*offset) = tmp;
  676. else
  677. *(__u32 *)((addr_t)
  678. child->thread.fpu.fprs + offset) = tmp;
  679. } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
  680. /*
  681. * Handle access to the per_info structure.
  682. */
  683. addr -= (addr_t) &dummy32->regs.per_info;
  684. __poke_user_per_compat(child, addr, data);
  685. }
  686. return 0;
  687. }
  688. static int poke_user_compat(struct task_struct *child,
  689. addr_t addr, addr_t data)
  690. {
  691. if (!is_compat_task() || (addr & 3) ||
  692. addr > sizeof(struct compat_user) - 3)
  693. return -EIO;
  694. return __poke_user_compat(child, addr, data);
  695. }
  696. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  697. compat_ulong_t caddr, compat_ulong_t cdata)
  698. {
  699. unsigned long addr = caddr;
  700. unsigned long data = cdata;
  701. compat_ptrace_area parea;
  702. int copied, ret;
  703. switch (request) {
  704. case PTRACE_PEEKUSR:
  705. /* read the word at location addr in the USER area. */
  706. return peek_user_compat(child, addr, data);
  707. case PTRACE_POKEUSR:
  708. /* write the word at location addr in the USER area */
  709. return poke_user_compat(child, addr, data);
  710. case PTRACE_PEEKUSR_AREA:
  711. case PTRACE_POKEUSR_AREA:
  712. if (copy_from_user(&parea, (void __force __user *) addr,
  713. sizeof(parea)))
  714. return -EFAULT;
  715. addr = parea.kernel_addr;
  716. data = parea.process_addr;
  717. copied = 0;
  718. while (copied < parea.len) {
  719. if (request == PTRACE_PEEKUSR_AREA)
  720. ret = peek_user_compat(child, addr, data);
  721. else {
  722. __u32 utmp;
  723. if (get_user(utmp,
  724. (__u32 __force __user *) data))
  725. return -EFAULT;
  726. ret = poke_user_compat(child, addr, utmp);
  727. }
  728. if (ret)
  729. return ret;
  730. addr += sizeof(unsigned int);
  731. data += sizeof(unsigned int);
  732. copied += sizeof(unsigned int);
  733. }
  734. return 0;
  735. case PTRACE_GET_LAST_BREAK:
  736. put_user(task_thread_info(child)->last_break,
  737. (unsigned int __user *) data);
  738. return 0;
  739. }
  740. return compat_ptrace_request(child, request, addr, data);
  741. }
  742. #endif
  743. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  744. {
  745. long ret = 0;
  746. /* Do the secure computing check first. */
  747. if (secure_computing()) {
  748. /* seccomp failures shouldn't expose any additional code. */
  749. ret = -1;
  750. goto out;
  751. }
  752. /*
  753. * The sysc_tracesys code in entry.S stored the system
  754. * call number to gprs[2].
  755. */
  756. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  757. (tracehook_report_syscall_entry(regs) ||
  758. regs->gprs[2] >= NR_syscalls)) {
  759. /*
  760. * Tracing decided this syscall should not happen or the
  761. * debugger stored an invalid system call number. Skip
  762. * the system call and the system call restart handling.
  763. */
  764. clear_pt_regs_flag(regs, PIF_SYSCALL);
  765. ret = -1;
  766. }
  767. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  768. trace_sys_enter(regs, regs->gprs[2]);
  769. audit_syscall_entry(regs->gprs[2], regs->orig_gpr2,
  770. regs->gprs[3], regs->gprs[4],
  771. regs->gprs[5]);
  772. out:
  773. return ret ?: regs->gprs[2];
  774. }
  775. asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
  776. {
  777. audit_syscall_exit(regs);
  778. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  779. trace_sys_exit(regs, regs->gprs[2]);
  780. if (test_thread_flag(TIF_SYSCALL_TRACE))
  781. tracehook_report_syscall_exit(regs, 0);
  782. }
  783. /*
  784. * user_regset definitions.
  785. */
  786. static int s390_regs_get(struct task_struct *target,
  787. const struct user_regset *regset,
  788. unsigned int pos, unsigned int count,
  789. void *kbuf, void __user *ubuf)
  790. {
  791. if (target == current)
  792. save_access_regs(target->thread.acrs);
  793. if (kbuf) {
  794. unsigned long *k = kbuf;
  795. while (count > 0) {
  796. *k++ = __peek_user(target, pos);
  797. count -= sizeof(*k);
  798. pos += sizeof(*k);
  799. }
  800. } else {
  801. unsigned long __user *u = ubuf;
  802. while (count > 0) {
  803. if (__put_user(__peek_user(target, pos), u++))
  804. return -EFAULT;
  805. count -= sizeof(*u);
  806. pos += sizeof(*u);
  807. }
  808. }
  809. return 0;
  810. }
  811. static int s390_regs_set(struct task_struct *target,
  812. const struct user_regset *regset,
  813. unsigned int pos, unsigned int count,
  814. const void *kbuf, const void __user *ubuf)
  815. {
  816. int rc = 0;
  817. if (target == current)
  818. save_access_regs(target->thread.acrs);
  819. if (kbuf) {
  820. const unsigned long *k = kbuf;
  821. while (count > 0 && !rc) {
  822. rc = __poke_user(target, pos, *k++);
  823. count -= sizeof(*k);
  824. pos += sizeof(*k);
  825. }
  826. } else {
  827. const unsigned long __user *u = ubuf;
  828. while (count > 0 && !rc) {
  829. unsigned long word;
  830. rc = __get_user(word, u++);
  831. if (rc)
  832. break;
  833. rc = __poke_user(target, pos, word);
  834. count -= sizeof(*u);
  835. pos += sizeof(*u);
  836. }
  837. }
  838. if (rc == 0 && target == current)
  839. restore_access_regs(target->thread.acrs);
  840. return rc;
  841. }
  842. static int s390_fpregs_get(struct task_struct *target,
  843. const struct user_regset *regset, unsigned int pos,
  844. unsigned int count, void *kbuf, void __user *ubuf)
  845. {
  846. _s390_fp_regs fp_regs;
  847. if (target == current)
  848. save_fpu_regs();
  849. fp_regs.fpc = target->thread.fpu.fpc;
  850. fpregs_store(&fp_regs, &target->thread.fpu);
  851. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  852. &fp_regs, 0, -1);
  853. }
  854. static int s390_fpregs_set(struct task_struct *target,
  855. const struct user_regset *regset, unsigned int pos,
  856. unsigned int count, const void *kbuf,
  857. const void __user *ubuf)
  858. {
  859. int rc = 0;
  860. freg_t fprs[__NUM_FPRS];
  861. if (target == current)
  862. save_fpu_regs();
  863. if (MACHINE_HAS_VX)
  864. convert_vx_to_fp(fprs, target->thread.fpu.vxrs);
  865. else
  866. memcpy(&fprs, target->thread.fpu.fprs, sizeof(fprs));
  867. /* If setting FPC, must validate it first. */
  868. if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
  869. u32 ufpc[2] = { target->thread.fpu.fpc, 0 };
  870. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
  871. 0, offsetof(s390_fp_regs, fprs));
  872. if (rc)
  873. return rc;
  874. if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
  875. return -EINVAL;
  876. target->thread.fpu.fpc = ufpc[0];
  877. }
  878. if (rc == 0 && count > 0)
  879. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  880. fprs, offsetof(s390_fp_regs, fprs), -1);
  881. if (rc)
  882. return rc;
  883. if (MACHINE_HAS_VX)
  884. convert_fp_to_vx(target->thread.fpu.vxrs, fprs);
  885. else
  886. memcpy(target->thread.fpu.fprs, &fprs, sizeof(fprs));
  887. return rc;
  888. }
  889. static int s390_last_break_get(struct task_struct *target,
  890. const struct user_regset *regset,
  891. unsigned int pos, unsigned int count,
  892. void *kbuf, void __user *ubuf)
  893. {
  894. if (count > 0) {
  895. if (kbuf) {
  896. unsigned long *k = kbuf;
  897. *k = task_thread_info(target)->last_break;
  898. } else {
  899. unsigned long __user *u = ubuf;
  900. if (__put_user(task_thread_info(target)->last_break, u))
  901. return -EFAULT;
  902. }
  903. }
  904. return 0;
  905. }
  906. static int s390_last_break_set(struct task_struct *target,
  907. const struct user_regset *regset,
  908. unsigned int pos, unsigned int count,
  909. const void *kbuf, const void __user *ubuf)
  910. {
  911. return 0;
  912. }
  913. static int s390_tdb_get(struct task_struct *target,
  914. const struct user_regset *regset,
  915. unsigned int pos, unsigned int count,
  916. void *kbuf, void __user *ubuf)
  917. {
  918. struct pt_regs *regs = task_pt_regs(target);
  919. unsigned char *data;
  920. if (!(regs->int_code & 0x200))
  921. return -ENODATA;
  922. data = target->thread.trap_tdb;
  923. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
  924. }
  925. static int s390_tdb_set(struct task_struct *target,
  926. const struct user_regset *regset,
  927. unsigned int pos, unsigned int count,
  928. const void *kbuf, const void __user *ubuf)
  929. {
  930. return 0;
  931. }
  932. static int s390_vxrs_low_get(struct task_struct *target,
  933. const struct user_regset *regset,
  934. unsigned int pos, unsigned int count,
  935. void *kbuf, void __user *ubuf)
  936. {
  937. __u64 vxrs[__NUM_VXRS_LOW];
  938. int i;
  939. if (!MACHINE_HAS_VX)
  940. return -ENODEV;
  941. if (target == current)
  942. save_fpu_regs();
  943. for (i = 0; i < __NUM_VXRS_LOW; i++)
  944. vxrs[i] = *((__u64 *)(target->thread.fpu.vxrs + i) + 1);
  945. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  946. }
  947. static int s390_vxrs_low_set(struct task_struct *target,
  948. const struct user_regset *regset,
  949. unsigned int pos, unsigned int count,
  950. const void *kbuf, const void __user *ubuf)
  951. {
  952. __u64 vxrs[__NUM_VXRS_LOW];
  953. int i, rc;
  954. if (!MACHINE_HAS_VX)
  955. return -ENODEV;
  956. if (target == current)
  957. save_fpu_regs();
  958. for (i = 0; i < __NUM_VXRS_LOW; i++)
  959. vxrs[i] = *((__u64 *)(target->thread.fpu.vxrs + i) + 1);
  960. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  961. if (rc == 0)
  962. for (i = 0; i < __NUM_VXRS_LOW; i++)
  963. *((__u64 *)(target->thread.fpu.vxrs + i) + 1) = vxrs[i];
  964. return rc;
  965. }
  966. static int s390_vxrs_high_get(struct task_struct *target,
  967. const struct user_regset *regset,
  968. unsigned int pos, unsigned int count,
  969. void *kbuf, void __user *ubuf)
  970. {
  971. __vector128 vxrs[__NUM_VXRS_HIGH];
  972. if (!MACHINE_HAS_VX)
  973. return -ENODEV;
  974. if (target == current)
  975. save_fpu_regs();
  976. memcpy(vxrs, target->thread.fpu.vxrs + __NUM_VXRS_LOW, sizeof(vxrs));
  977. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
  978. }
  979. static int s390_vxrs_high_set(struct task_struct *target,
  980. const struct user_regset *regset,
  981. unsigned int pos, unsigned int count,
  982. const void *kbuf, const void __user *ubuf)
  983. {
  984. int rc;
  985. if (!MACHINE_HAS_VX)
  986. return -ENODEV;
  987. if (target == current)
  988. save_fpu_regs();
  989. rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  990. target->thread.fpu.vxrs + __NUM_VXRS_LOW, 0, -1);
  991. return rc;
  992. }
  993. static int s390_system_call_get(struct task_struct *target,
  994. const struct user_regset *regset,
  995. unsigned int pos, unsigned int count,
  996. void *kbuf, void __user *ubuf)
  997. {
  998. unsigned int *data = &task_thread_info(target)->system_call;
  999. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1000. data, 0, sizeof(unsigned int));
  1001. }
  1002. static int s390_system_call_set(struct task_struct *target,
  1003. const struct user_regset *regset,
  1004. unsigned int pos, unsigned int count,
  1005. const void *kbuf, const void __user *ubuf)
  1006. {
  1007. unsigned int *data = &task_thread_info(target)->system_call;
  1008. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1009. data, 0, sizeof(unsigned int));
  1010. }
  1011. static const struct user_regset s390_regsets[] = {
  1012. {
  1013. .core_note_type = NT_PRSTATUS,
  1014. .n = sizeof(s390_regs) / sizeof(long),
  1015. .size = sizeof(long),
  1016. .align = sizeof(long),
  1017. .get = s390_regs_get,
  1018. .set = s390_regs_set,
  1019. },
  1020. {
  1021. .core_note_type = NT_PRFPREG,
  1022. .n = sizeof(s390_fp_regs) / sizeof(long),
  1023. .size = sizeof(long),
  1024. .align = sizeof(long),
  1025. .get = s390_fpregs_get,
  1026. .set = s390_fpregs_set,
  1027. },
  1028. {
  1029. .core_note_type = NT_S390_SYSTEM_CALL,
  1030. .n = 1,
  1031. .size = sizeof(unsigned int),
  1032. .align = sizeof(unsigned int),
  1033. .get = s390_system_call_get,
  1034. .set = s390_system_call_set,
  1035. },
  1036. {
  1037. .core_note_type = NT_S390_LAST_BREAK,
  1038. .n = 1,
  1039. .size = sizeof(long),
  1040. .align = sizeof(long),
  1041. .get = s390_last_break_get,
  1042. .set = s390_last_break_set,
  1043. },
  1044. {
  1045. .core_note_type = NT_S390_TDB,
  1046. .n = 1,
  1047. .size = 256,
  1048. .align = 1,
  1049. .get = s390_tdb_get,
  1050. .set = s390_tdb_set,
  1051. },
  1052. {
  1053. .core_note_type = NT_S390_VXRS_LOW,
  1054. .n = __NUM_VXRS_LOW,
  1055. .size = sizeof(__u64),
  1056. .align = sizeof(__u64),
  1057. .get = s390_vxrs_low_get,
  1058. .set = s390_vxrs_low_set,
  1059. },
  1060. {
  1061. .core_note_type = NT_S390_VXRS_HIGH,
  1062. .n = __NUM_VXRS_HIGH,
  1063. .size = sizeof(__vector128),
  1064. .align = sizeof(__vector128),
  1065. .get = s390_vxrs_high_get,
  1066. .set = s390_vxrs_high_set,
  1067. },
  1068. };
  1069. static const struct user_regset_view user_s390_view = {
  1070. .name = UTS_MACHINE,
  1071. .e_machine = EM_S390,
  1072. .regsets = s390_regsets,
  1073. .n = ARRAY_SIZE(s390_regsets)
  1074. };
  1075. #ifdef CONFIG_COMPAT
  1076. static int s390_compat_regs_get(struct task_struct *target,
  1077. const struct user_regset *regset,
  1078. unsigned int pos, unsigned int count,
  1079. void *kbuf, void __user *ubuf)
  1080. {
  1081. if (target == current)
  1082. save_access_regs(target->thread.acrs);
  1083. if (kbuf) {
  1084. compat_ulong_t *k = kbuf;
  1085. while (count > 0) {
  1086. *k++ = __peek_user_compat(target, pos);
  1087. count -= sizeof(*k);
  1088. pos += sizeof(*k);
  1089. }
  1090. } else {
  1091. compat_ulong_t __user *u = ubuf;
  1092. while (count > 0) {
  1093. if (__put_user(__peek_user_compat(target, pos), u++))
  1094. return -EFAULT;
  1095. count -= sizeof(*u);
  1096. pos += sizeof(*u);
  1097. }
  1098. }
  1099. return 0;
  1100. }
  1101. static int s390_compat_regs_set(struct task_struct *target,
  1102. const struct user_regset *regset,
  1103. unsigned int pos, unsigned int count,
  1104. const void *kbuf, const void __user *ubuf)
  1105. {
  1106. int rc = 0;
  1107. if (target == current)
  1108. save_access_regs(target->thread.acrs);
  1109. if (kbuf) {
  1110. const compat_ulong_t *k = kbuf;
  1111. while (count > 0 && !rc) {
  1112. rc = __poke_user_compat(target, pos, *k++);
  1113. count -= sizeof(*k);
  1114. pos += sizeof(*k);
  1115. }
  1116. } else {
  1117. const compat_ulong_t __user *u = ubuf;
  1118. while (count > 0 && !rc) {
  1119. compat_ulong_t word;
  1120. rc = __get_user(word, u++);
  1121. if (rc)
  1122. break;
  1123. rc = __poke_user_compat(target, pos, word);
  1124. count -= sizeof(*u);
  1125. pos += sizeof(*u);
  1126. }
  1127. }
  1128. if (rc == 0 && target == current)
  1129. restore_access_regs(target->thread.acrs);
  1130. return rc;
  1131. }
  1132. static int s390_compat_regs_high_get(struct task_struct *target,
  1133. const struct user_regset *regset,
  1134. unsigned int pos, unsigned int count,
  1135. void *kbuf, void __user *ubuf)
  1136. {
  1137. compat_ulong_t *gprs_high;
  1138. gprs_high = (compat_ulong_t *)
  1139. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  1140. if (kbuf) {
  1141. compat_ulong_t *k = kbuf;
  1142. while (count > 0) {
  1143. *k++ = *gprs_high;
  1144. gprs_high += 2;
  1145. count -= sizeof(*k);
  1146. }
  1147. } else {
  1148. compat_ulong_t __user *u = ubuf;
  1149. while (count > 0) {
  1150. if (__put_user(*gprs_high, u++))
  1151. return -EFAULT;
  1152. gprs_high += 2;
  1153. count -= sizeof(*u);
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. static int s390_compat_regs_high_set(struct task_struct *target,
  1159. const struct user_regset *regset,
  1160. unsigned int pos, unsigned int count,
  1161. const void *kbuf, const void __user *ubuf)
  1162. {
  1163. compat_ulong_t *gprs_high;
  1164. int rc = 0;
  1165. gprs_high = (compat_ulong_t *)
  1166. &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
  1167. if (kbuf) {
  1168. const compat_ulong_t *k = kbuf;
  1169. while (count > 0) {
  1170. *gprs_high = *k++;
  1171. *gprs_high += 2;
  1172. count -= sizeof(*k);
  1173. }
  1174. } else {
  1175. const compat_ulong_t __user *u = ubuf;
  1176. while (count > 0 && !rc) {
  1177. unsigned long word;
  1178. rc = __get_user(word, u++);
  1179. if (rc)
  1180. break;
  1181. *gprs_high = word;
  1182. *gprs_high += 2;
  1183. count -= sizeof(*u);
  1184. }
  1185. }
  1186. return rc;
  1187. }
  1188. static int s390_compat_last_break_get(struct task_struct *target,
  1189. const struct user_regset *regset,
  1190. unsigned int pos, unsigned int count,
  1191. void *kbuf, void __user *ubuf)
  1192. {
  1193. compat_ulong_t last_break;
  1194. if (count > 0) {
  1195. last_break = task_thread_info(target)->last_break;
  1196. if (kbuf) {
  1197. unsigned long *k = kbuf;
  1198. *k = last_break;
  1199. } else {
  1200. unsigned long __user *u = ubuf;
  1201. if (__put_user(last_break, u))
  1202. return -EFAULT;
  1203. }
  1204. }
  1205. return 0;
  1206. }
  1207. static int s390_compat_last_break_set(struct task_struct *target,
  1208. const struct user_regset *regset,
  1209. unsigned int pos, unsigned int count,
  1210. const void *kbuf, const void __user *ubuf)
  1211. {
  1212. return 0;
  1213. }
  1214. static const struct user_regset s390_compat_regsets[] = {
  1215. {
  1216. .core_note_type = NT_PRSTATUS,
  1217. .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
  1218. .size = sizeof(compat_long_t),
  1219. .align = sizeof(compat_long_t),
  1220. .get = s390_compat_regs_get,
  1221. .set = s390_compat_regs_set,
  1222. },
  1223. {
  1224. .core_note_type = NT_PRFPREG,
  1225. .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
  1226. .size = sizeof(compat_long_t),
  1227. .align = sizeof(compat_long_t),
  1228. .get = s390_fpregs_get,
  1229. .set = s390_fpregs_set,
  1230. },
  1231. {
  1232. .core_note_type = NT_S390_SYSTEM_CALL,
  1233. .n = 1,
  1234. .size = sizeof(compat_uint_t),
  1235. .align = sizeof(compat_uint_t),
  1236. .get = s390_system_call_get,
  1237. .set = s390_system_call_set,
  1238. },
  1239. {
  1240. .core_note_type = NT_S390_LAST_BREAK,
  1241. .n = 1,
  1242. .size = sizeof(long),
  1243. .align = sizeof(long),
  1244. .get = s390_compat_last_break_get,
  1245. .set = s390_compat_last_break_set,
  1246. },
  1247. {
  1248. .core_note_type = NT_S390_TDB,
  1249. .n = 1,
  1250. .size = 256,
  1251. .align = 1,
  1252. .get = s390_tdb_get,
  1253. .set = s390_tdb_set,
  1254. },
  1255. {
  1256. .core_note_type = NT_S390_VXRS_LOW,
  1257. .n = __NUM_VXRS_LOW,
  1258. .size = sizeof(__u64),
  1259. .align = sizeof(__u64),
  1260. .get = s390_vxrs_low_get,
  1261. .set = s390_vxrs_low_set,
  1262. },
  1263. {
  1264. .core_note_type = NT_S390_VXRS_HIGH,
  1265. .n = __NUM_VXRS_HIGH,
  1266. .size = sizeof(__vector128),
  1267. .align = sizeof(__vector128),
  1268. .get = s390_vxrs_high_get,
  1269. .set = s390_vxrs_high_set,
  1270. },
  1271. {
  1272. .core_note_type = NT_S390_HIGH_GPRS,
  1273. .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
  1274. .size = sizeof(compat_long_t),
  1275. .align = sizeof(compat_long_t),
  1276. .get = s390_compat_regs_high_get,
  1277. .set = s390_compat_regs_high_set,
  1278. },
  1279. };
  1280. static const struct user_regset_view user_s390_compat_view = {
  1281. .name = "s390",
  1282. .e_machine = EM_S390,
  1283. .regsets = s390_compat_regsets,
  1284. .n = ARRAY_SIZE(s390_compat_regsets)
  1285. };
  1286. #endif
  1287. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1288. {
  1289. #ifdef CONFIG_COMPAT
  1290. if (test_tsk_thread_flag(task, TIF_31BIT))
  1291. return &user_s390_compat_view;
  1292. #endif
  1293. return &user_s390_view;
  1294. }
  1295. static const char *gpr_names[NUM_GPRS] = {
  1296. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  1297. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  1298. };
  1299. unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
  1300. {
  1301. if (offset >= NUM_GPRS)
  1302. return 0;
  1303. return regs->gprs[offset];
  1304. }
  1305. int regs_query_register_offset(const char *name)
  1306. {
  1307. unsigned long offset;
  1308. if (!name || *name != 'r')
  1309. return -EINVAL;
  1310. if (kstrtoul(name + 1, 10, &offset))
  1311. return -EINVAL;
  1312. if (offset >= NUM_GPRS)
  1313. return -EINVAL;
  1314. return offset;
  1315. }
  1316. const char *regs_query_register_name(unsigned int offset)
  1317. {
  1318. if (offset >= NUM_GPRS)
  1319. return NULL;
  1320. return gpr_names[offset];
  1321. }
  1322. static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  1323. {
  1324. unsigned long ksp = kernel_stack_pointer(regs);
  1325. return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
  1326. }
  1327. /**
  1328. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  1329. * @regs:pt_regs which contains kernel stack pointer.
  1330. * @n:stack entry number.
  1331. *
  1332. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  1333. * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
  1334. * this returns 0.
  1335. */
  1336. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  1337. {
  1338. unsigned long addr;
  1339. addr = kernel_stack_pointer(regs) + n * sizeof(long);
  1340. if (!regs_within_kernel_stack(regs, addr))
  1341. return 0;
  1342. return *(unsigned long *)addr;
  1343. }