ptrace.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/sched/task_stack.h>
  9. #include <linux/mm.h>
  10. #include <linux/smp.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/tracehook.h>
  15. #include <linux/user.h>
  16. #include <linux/elf.h>
  17. #include <linux/security.h>
  18. #include <linux/audit.h>
  19. #include <linux/seccomp.h>
  20. #include <linux/signal.h>
  21. #include <linux/perf_event.h>
  22. #include <linux/hw_breakpoint.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/export.h>
  25. #include <linux/context_tracking.h>
  26. #include <linux/nospec.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/processor.h>
  30. #include <asm/fpu/internal.h>
  31. #include <asm/fpu/signal.h>
  32. #include <asm/fpu/regset.h>
  33. #include <asm/debugreg.h>
  34. #include <asm/ldt.h>
  35. #include <asm/desc.h>
  36. #include <asm/prctl.h>
  37. #include <asm/proto.h>
  38. #include <asm/hw_breakpoint.h>
  39. #include <asm/traps.h>
  40. #include <asm/syscall.h>
  41. #include <asm/mmu_context.h>
  42. #include "tls.h"
  43. enum x86_regset {
  44. REGSET_GENERAL,
  45. REGSET_FP,
  46. REGSET_XFP,
  47. REGSET_IOPERM64 = REGSET_XFP,
  48. REGSET_XSTATE,
  49. REGSET_TLS,
  50. REGSET_IOPERM32,
  51. };
  52. struct pt_regs_offset {
  53. const char *name;
  54. int offset;
  55. };
  56. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  57. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  58. static const struct pt_regs_offset regoffset_table[] = {
  59. #ifdef CONFIG_X86_64
  60. REG_OFFSET_NAME(r15),
  61. REG_OFFSET_NAME(r14),
  62. REG_OFFSET_NAME(r13),
  63. REG_OFFSET_NAME(r12),
  64. REG_OFFSET_NAME(r11),
  65. REG_OFFSET_NAME(r10),
  66. REG_OFFSET_NAME(r9),
  67. REG_OFFSET_NAME(r8),
  68. #endif
  69. REG_OFFSET_NAME(bx),
  70. REG_OFFSET_NAME(cx),
  71. REG_OFFSET_NAME(dx),
  72. REG_OFFSET_NAME(si),
  73. REG_OFFSET_NAME(di),
  74. REG_OFFSET_NAME(bp),
  75. REG_OFFSET_NAME(ax),
  76. #ifdef CONFIG_X86_32
  77. REG_OFFSET_NAME(ds),
  78. REG_OFFSET_NAME(es),
  79. REG_OFFSET_NAME(fs),
  80. REG_OFFSET_NAME(gs),
  81. #endif
  82. REG_OFFSET_NAME(orig_ax),
  83. REG_OFFSET_NAME(ip),
  84. REG_OFFSET_NAME(cs),
  85. REG_OFFSET_NAME(flags),
  86. REG_OFFSET_NAME(sp),
  87. REG_OFFSET_NAME(ss),
  88. REG_OFFSET_END,
  89. };
  90. /**
  91. * regs_query_register_offset() - query register offset from its name
  92. * @name: the name of a register
  93. *
  94. * regs_query_register_offset() returns the offset of a register in struct
  95. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  96. */
  97. int regs_query_register_offset(const char *name)
  98. {
  99. const struct pt_regs_offset *roff;
  100. for (roff = regoffset_table; roff->name != NULL; roff++)
  101. if (!strcmp(roff->name, name))
  102. return roff->offset;
  103. return -EINVAL;
  104. }
  105. /**
  106. * regs_query_register_name() - query register name from its offset
  107. * @offset: the offset of a register in struct pt_regs.
  108. *
  109. * regs_query_register_name() returns the name of a register from its
  110. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  111. */
  112. const char *regs_query_register_name(unsigned int offset)
  113. {
  114. const struct pt_regs_offset *roff;
  115. for (roff = regoffset_table; roff->name != NULL; roff++)
  116. if (roff->offset == offset)
  117. return roff->name;
  118. return NULL;
  119. }
  120. /*
  121. * does not yet catch signals sent when the child dies.
  122. * in exit.c or in signal.c.
  123. */
  124. /*
  125. * Determines which flags the user has access to [1 = access, 0 = no access].
  126. */
  127. #define FLAG_MASK_32 ((unsigned long) \
  128. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  129. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  130. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  131. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  132. X86_EFLAGS_RF | X86_EFLAGS_AC))
  133. /*
  134. * Determines whether a value may be installed in a segment register.
  135. */
  136. static inline bool invalid_selector(u16 value)
  137. {
  138. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  139. }
  140. #ifdef CONFIG_X86_32
  141. #define FLAG_MASK FLAG_MASK_32
  142. /*
  143. * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
  144. * when it traps. The previous stack will be directly underneath the saved
  145. * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  146. *
  147. * Now, if the stack is empty, '&regs->sp' is out of range. In this
  148. * case we try to take the previous stack. To always return a non-null
  149. * stack pointer we fall back to regs as stack if no previous stack
  150. * exists.
  151. *
  152. * This is valid only for kernel mode traps.
  153. */
  154. unsigned long kernel_stack_pointer(struct pt_regs *regs)
  155. {
  156. unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
  157. unsigned long sp = (unsigned long)&regs->sp;
  158. u32 *prev_esp;
  159. if (context == (sp & ~(THREAD_SIZE - 1)))
  160. return sp;
  161. prev_esp = (u32 *)(context);
  162. if (*prev_esp)
  163. return (unsigned long)*prev_esp;
  164. return (unsigned long)regs;
  165. }
  166. EXPORT_SYMBOL_GPL(kernel_stack_pointer);
  167. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  168. {
  169. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  170. return &regs->bx + (regno >> 2);
  171. }
  172. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  173. {
  174. /*
  175. * Returning the value truncates it to 16 bits.
  176. */
  177. unsigned int retval;
  178. if (offset != offsetof(struct user_regs_struct, gs))
  179. retval = *pt_regs_access(task_pt_regs(task), offset);
  180. else {
  181. if (task == current)
  182. retval = get_user_gs(task_pt_regs(task));
  183. else
  184. retval = task_user_gs(task);
  185. }
  186. return retval;
  187. }
  188. static int set_segment_reg(struct task_struct *task,
  189. unsigned long offset, u16 value)
  190. {
  191. /*
  192. * The value argument was already truncated to 16 bits.
  193. */
  194. if (invalid_selector(value))
  195. return -EIO;
  196. /*
  197. * For %cs and %ss we cannot permit a null selector.
  198. * We can permit a bogus selector as long as it has USER_RPL.
  199. * Null selectors are fine for other segment registers, but
  200. * we will never get back to user mode with invalid %cs or %ss
  201. * and will take the trap in iret instead. Much code relies
  202. * on user_mode() to distinguish a user trap frame (which can
  203. * safely use invalid selectors) from a kernel trap frame.
  204. */
  205. switch (offset) {
  206. case offsetof(struct user_regs_struct, cs):
  207. case offsetof(struct user_regs_struct, ss):
  208. if (unlikely(value == 0))
  209. return -EIO;
  210. default:
  211. *pt_regs_access(task_pt_regs(task), offset) = value;
  212. break;
  213. case offsetof(struct user_regs_struct, gs):
  214. if (task == current)
  215. set_user_gs(task_pt_regs(task), value);
  216. else
  217. task_user_gs(task) = value;
  218. }
  219. return 0;
  220. }
  221. #else /* CONFIG_X86_64 */
  222. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  223. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  224. {
  225. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  226. return &regs->r15 + (offset / sizeof(regs->r15));
  227. }
  228. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  229. {
  230. /*
  231. * Returning the value truncates it to 16 bits.
  232. */
  233. unsigned int seg;
  234. switch (offset) {
  235. case offsetof(struct user_regs_struct, fs):
  236. if (task == current) {
  237. /* Older gas can't assemble movq %?s,%r?? */
  238. asm("movl %%fs,%0" : "=r" (seg));
  239. return seg;
  240. }
  241. return task->thread.fsindex;
  242. case offsetof(struct user_regs_struct, gs):
  243. if (task == current) {
  244. asm("movl %%gs,%0" : "=r" (seg));
  245. return seg;
  246. }
  247. return task->thread.gsindex;
  248. case offsetof(struct user_regs_struct, ds):
  249. if (task == current) {
  250. asm("movl %%ds,%0" : "=r" (seg));
  251. return seg;
  252. }
  253. return task->thread.ds;
  254. case offsetof(struct user_regs_struct, es):
  255. if (task == current) {
  256. asm("movl %%es,%0" : "=r" (seg));
  257. return seg;
  258. }
  259. return task->thread.es;
  260. case offsetof(struct user_regs_struct, cs):
  261. case offsetof(struct user_regs_struct, ss):
  262. break;
  263. }
  264. return *pt_regs_access(task_pt_regs(task), offset);
  265. }
  266. static int set_segment_reg(struct task_struct *task,
  267. unsigned long offset, u16 value)
  268. {
  269. /*
  270. * The value argument was already truncated to 16 bits.
  271. */
  272. if (invalid_selector(value))
  273. return -EIO;
  274. switch (offset) {
  275. case offsetof(struct user_regs_struct,fs):
  276. task->thread.fsindex = value;
  277. if (task == current)
  278. loadsegment(fs, task->thread.fsindex);
  279. break;
  280. case offsetof(struct user_regs_struct,gs):
  281. task->thread.gsindex = value;
  282. if (task == current)
  283. load_gs_index(task->thread.gsindex);
  284. break;
  285. case offsetof(struct user_regs_struct,ds):
  286. task->thread.ds = value;
  287. if (task == current)
  288. loadsegment(ds, task->thread.ds);
  289. break;
  290. case offsetof(struct user_regs_struct,es):
  291. task->thread.es = value;
  292. if (task == current)
  293. loadsegment(es, task->thread.es);
  294. break;
  295. /*
  296. * Can't actually change these in 64-bit mode.
  297. */
  298. case offsetof(struct user_regs_struct,cs):
  299. if (unlikely(value == 0))
  300. return -EIO;
  301. task_pt_regs(task)->cs = value;
  302. break;
  303. case offsetof(struct user_regs_struct,ss):
  304. if (unlikely(value == 0))
  305. return -EIO;
  306. task_pt_regs(task)->ss = value;
  307. break;
  308. }
  309. return 0;
  310. }
  311. static unsigned long task_seg_base(struct task_struct *task,
  312. unsigned short selector)
  313. {
  314. unsigned short idx = selector >> 3;
  315. unsigned long base;
  316. if (likely((selector & SEGMENT_TI_MASK) == 0)) {
  317. if (unlikely(idx >= GDT_ENTRIES))
  318. return 0;
  319. /*
  320. * There are no user segments in the GDT with nonzero bases
  321. * other than the TLS segments.
  322. */
  323. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  324. return 0;
  325. idx -= GDT_ENTRY_TLS_MIN;
  326. base = get_desc_base(&task->thread.tls_array[idx]);
  327. } else {
  328. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  329. struct ldt_struct *ldt;
  330. /*
  331. * If performance here mattered, we could protect the LDT
  332. * with RCU. This is a slow path, though, so we can just
  333. * take the mutex.
  334. */
  335. mutex_lock(&task->mm->context.lock);
  336. ldt = task->mm->context.ldt;
  337. if (unlikely(!ldt || idx >= ldt->nr_entries))
  338. base = 0;
  339. else
  340. base = get_desc_base(ldt->entries + idx);
  341. mutex_unlock(&task->mm->context.lock);
  342. #else
  343. base = 0;
  344. #endif
  345. }
  346. return base;
  347. }
  348. #endif /* CONFIG_X86_32 */
  349. static unsigned long get_flags(struct task_struct *task)
  350. {
  351. unsigned long retval = task_pt_regs(task)->flags;
  352. /*
  353. * If the debugger set TF, hide it from the readout.
  354. */
  355. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  356. retval &= ~X86_EFLAGS_TF;
  357. return retval;
  358. }
  359. static int set_flags(struct task_struct *task, unsigned long value)
  360. {
  361. struct pt_regs *regs = task_pt_regs(task);
  362. /*
  363. * If the user value contains TF, mark that
  364. * it was not "us" (the debugger) that set it.
  365. * If not, make sure it stays set if we had.
  366. */
  367. if (value & X86_EFLAGS_TF)
  368. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  369. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  370. value |= X86_EFLAGS_TF;
  371. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  372. return 0;
  373. }
  374. static int putreg(struct task_struct *child,
  375. unsigned long offset, unsigned long value)
  376. {
  377. switch (offset) {
  378. case offsetof(struct user_regs_struct, cs):
  379. case offsetof(struct user_regs_struct, ds):
  380. case offsetof(struct user_regs_struct, es):
  381. case offsetof(struct user_regs_struct, fs):
  382. case offsetof(struct user_regs_struct, gs):
  383. case offsetof(struct user_regs_struct, ss):
  384. return set_segment_reg(child, offset, value);
  385. case offsetof(struct user_regs_struct, flags):
  386. return set_flags(child, value);
  387. #ifdef CONFIG_X86_64
  388. case offsetof(struct user_regs_struct,fs_base):
  389. if (value >= TASK_SIZE_MAX)
  390. return -EIO;
  391. /*
  392. * When changing the segment base, use do_arch_prctl_64
  393. * to set either thread.fs or thread.fsindex and the
  394. * corresponding GDT slot.
  395. */
  396. if (child->thread.fsbase != value)
  397. return do_arch_prctl_64(child, ARCH_SET_FS, value);
  398. return 0;
  399. case offsetof(struct user_regs_struct,gs_base):
  400. /*
  401. * Exactly the same here as the %fs handling above.
  402. */
  403. if (value >= TASK_SIZE_MAX)
  404. return -EIO;
  405. if (child->thread.gsbase != value)
  406. return do_arch_prctl_64(child, ARCH_SET_GS, value);
  407. return 0;
  408. #endif
  409. }
  410. *pt_regs_access(task_pt_regs(child), offset) = value;
  411. return 0;
  412. }
  413. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  414. {
  415. switch (offset) {
  416. case offsetof(struct user_regs_struct, cs):
  417. case offsetof(struct user_regs_struct, ds):
  418. case offsetof(struct user_regs_struct, es):
  419. case offsetof(struct user_regs_struct, fs):
  420. case offsetof(struct user_regs_struct, gs):
  421. case offsetof(struct user_regs_struct, ss):
  422. return get_segment_reg(task, offset);
  423. case offsetof(struct user_regs_struct, flags):
  424. return get_flags(task);
  425. #ifdef CONFIG_X86_64
  426. case offsetof(struct user_regs_struct, fs_base): {
  427. if (task->thread.fsindex == 0)
  428. return task->thread.fsbase;
  429. else
  430. return task_seg_base(task, task->thread.fsindex);
  431. }
  432. case offsetof(struct user_regs_struct, gs_base): {
  433. if (task->thread.gsindex == 0)
  434. return task->thread.gsbase;
  435. else
  436. return task_seg_base(task, task->thread.gsindex);
  437. }
  438. #endif
  439. }
  440. return *pt_regs_access(task_pt_regs(task), offset);
  441. }
  442. static int genregs_get(struct task_struct *target,
  443. const struct user_regset *regset,
  444. unsigned int pos, unsigned int count,
  445. void *kbuf, void __user *ubuf)
  446. {
  447. if (kbuf) {
  448. unsigned long *k = kbuf;
  449. while (count >= sizeof(*k)) {
  450. *k++ = getreg(target, pos);
  451. count -= sizeof(*k);
  452. pos += sizeof(*k);
  453. }
  454. } else {
  455. unsigned long __user *u = ubuf;
  456. while (count >= sizeof(*u)) {
  457. if (__put_user(getreg(target, pos), u++))
  458. return -EFAULT;
  459. count -= sizeof(*u);
  460. pos += sizeof(*u);
  461. }
  462. }
  463. return 0;
  464. }
  465. static int genregs_set(struct task_struct *target,
  466. const struct user_regset *regset,
  467. unsigned int pos, unsigned int count,
  468. const void *kbuf, const void __user *ubuf)
  469. {
  470. int ret = 0;
  471. if (kbuf) {
  472. const unsigned long *k = kbuf;
  473. while (count >= sizeof(*k) && !ret) {
  474. ret = putreg(target, pos, *k++);
  475. count -= sizeof(*k);
  476. pos += sizeof(*k);
  477. }
  478. } else {
  479. const unsigned long __user *u = ubuf;
  480. while (count >= sizeof(*u) && !ret) {
  481. unsigned long word;
  482. ret = __get_user(word, u++);
  483. if (ret)
  484. break;
  485. ret = putreg(target, pos, word);
  486. count -= sizeof(*u);
  487. pos += sizeof(*u);
  488. }
  489. }
  490. return ret;
  491. }
  492. static void ptrace_triggered(struct perf_event *bp,
  493. struct perf_sample_data *data,
  494. struct pt_regs *regs)
  495. {
  496. int i;
  497. struct thread_struct *thread = &(current->thread);
  498. /*
  499. * Store in the virtual DR6 register the fact that the breakpoint
  500. * was hit so the thread's debugger will see it.
  501. */
  502. for (i = 0; i < HBP_NUM; i++) {
  503. if (thread->ptrace_bps[i] == bp)
  504. break;
  505. }
  506. thread->debugreg6 |= (DR_TRAP0 << i);
  507. }
  508. /*
  509. * Walk through every ptrace breakpoints for this thread and
  510. * build the dr7 value on top of their attributes.
  511. *
  512. */
  513. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  514. {
  515. int i;
  516. int dr7 = 0;
  517. struct arch_hw_breakpoint *info;
  518. for (i = 0; i < HBP_NUM; i++) {
  519. if (bp[i] && !bp[i]->attr.disabled) {
  520. info = counter_arch_bp(bp[i]);
  521. dr7 |= encode_dr7(i, info->len, info->type);
  522. }
  523. }
  524. return dr7;
  525. }
  526. static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
  527. int len, int type, bool disabled)
  528. {
  529. int err, bp_len, bp_type;
  530. err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
  531. if (!err) {
  532. attr->bp_len = bp_len;
  533. attr->bp_type = bp_type;
  534. attr->disabled = disabled;
  535. }
  536. return err;
  537. }
  538. static struct perf_event *
  539. ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
  540. unsigned long addr, bool disabled)
  541. {
  542. struct perf_event_attr attr;
  543. int err;
  544. ptrace_breakpoint_init(&attr);
  545. attr.bp_addr = addr;
  546. err = ptrace_fill_bp_fields(&attr, len, type, disabled);
  547. if (err)
  548. return ERR_PTR(err);
  549. return register_user_hw_breakpoint(&attr, ptrace_triggered,
  550. NULL, tsk);
  551. }
  552. static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
  553. int disabled)
  554. {
  555. struct perf_event_attr attr = bp->attr;
  556. int err;
  557. err = ptrace_fill_bp_fields(&attr, len, type, disabled);
  558. if (err)
  559. return err;
  560. return modify_user_hw_breakpoint(bp, &attr);
  561. }
  562. /*
  563. * Handle ptrace writes to debug register 7.
  564. */
  565. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  566. {
  567. struct thread_struct *thread = &tsk->thread;
  568. unsigned long old_dr7;
  569. bool second_pass = false;
  570. int i, rc, ret = 0;
  571. data &= ~DR_CONTROL_RESERVED;
  572. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  573. restore:
  574. rc = 0;
  575. for (i = 0; i < HBP_NUM; i++) {
  576. unsigned len, type;
  577. bool disabled = !decode_dr7(data, i, &len, &type);
  578. struct perf_event *bp = thread->ptrace_bps[i];
  579. if (!bp) {
  580. if (disabled)
  581. continue;
  582. bp = ptrace_register_breakpoint(tsk,
  583. len, type, 0, disabled);
  584. if (IS_ERR(bp)) {
  585. rc = PTR_ERR(bp);
  586. break;
  587. }
  588. thread->ptrace_bps[i] = bp;
  589. continue;
  590. }
  591. rc = ptrace_modify_breakpoint(bp, len, type, disabled);
  592. if (rc)
  593. break;
  594. }
  595. /* Restore if the first pass failed, second_pass shouldn't fail. */
  596. if (rc && !WARN_ON(second_pass)) {
  597. ret = rc;
  598. data = old_dr7;
  599. second_pass = true;
  600. goto restore;
  601. }
  602. return ret;
  603. }
  604. /*
  605. * Handle PTRACE_PEEKUSR calls for the debug register area.
  606. */
  607. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  608. {
  609. struct thread_struct *thread = &tsk->thread;
  610. unsigned long val = 0;
  611. if (n < HBP_NUM) {
  612. int index = array_index_nospec(n, HBP_NUM);
  613. struct perf_event *bp = thread->ptrace_bps[index];
  614. if (bp)
  615. val = bp->hw.info.address;
  616. } else if (n == 6) {
  617. val = thread->debugreg6;
  618. } else if (n == 7) {
  619. val = thread->ptrace_dr7;
  620. }
  621. return val;
  622. }
  623. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  624. unsigned long addr)
  625. {
  626. struct thread_struct *t = &tsk->thread;
  627. struct perf_event *bp = t->ptrace_bps[nr];
  628. int err = 0;
  629. if (!bp) {
  630. /*
  631. * Put stub len and type to create an inactive but correct bp.
  632. *
  633. * CHECKME: the previous code returned -EIO if the addr wasn't
  634. * a valid task virtual addr. The new one will return -EINVAL in
  635. * this case.
  636. * -EINVAL may be what we want for in-kernel breakpoints users,
  637. * but -EIO looks better for ptrace, since we refuse a register
  638. * writing for the user. And anyway this is the previous
  639. * behaviour.
  640. */
  641. bp = ptrace_register_breakpoint(tsk,
  642. X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
  643. addr, true);
  644. if (IS_ERR(bp))
  645. err = PTR_ERR(bp);
  646. else
  647. t->ptrace_bps[nr] = bp;
  648. } else {
  649. struct perf_event_attr attr = bp->attr;
  650. attr.bp_addr = addr;
  651. err = modify_user_hw_breakpoint(bp, &attr);
  652. }
  653. return err;
  654. }
  655. /*
  656. * Handle PTRACE_POKEUSR calls for the debug register area.
  657. */
  658. static int ptrace_set_debugreg(struct task_struct *tsk, int n,
  659. unsigned long val)
  660. {
  661. struct thread_struct *thread = &tsk->thread;
  662. /* There are no DR4 or DR5 registers */
  663. int rc = -EIO;
  664. if (n < HBP_NUM) {
  665. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  666. } else if (n == 6) {
  667. thread->debugreg6 = val;
  668. rc = 0;
  669. } else if (n == 7) {
  670. rc = ptrace_write_dr7(tsk, val);
  671. if (!rc)
  672. thread->ptrace_dr7 = val;
  673. }
  674. return rc;
  675. }
  676. /*
  677. * These access the current or another (stopped) task's io permission
  678. * bitmap for debugging or core dump.
  679. */
  680. static int ioperm_active(struct task_struct *target,
  681. const struct user_regset *regset)
  682. {
  683. return target->thread.io_bitmap_max / regset->size;
  684. }
  685. static int ioperm_get(struct task_struct *target,
  686. const struct user_regset *regset,
  687. unsigned int pos, unsigned int count,
  688. void *kbuf, void __user *ubuf)
  689. {
  690. if (!target->thread.io_bitmap_ptr)
  691. return -ENXIO;
  692. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  693. target->thread.io_bitmap_ptr,
  694. 0, IO_BITMAP_BYTES);
  695. }
  696. /*
  697. * Called by kernel/ptrace.c when detaching..
  698. *
  699. * Make sure the single step bit is not set.
  700. */
  701. void ptrace_disable(struct task_struct *child)
  702. {
  703. user_disable_single_step(child);
  704. #ifdef TIF_SYSCALL_EMU
  705. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  706. #endif
  707. }
  708. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  709. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  710. #endif
  711. long arch_ptrace(struct task_struct *child, long request,
  712. unsigned long addr, unsigned long data)
  713. {
  714. int ret;
  715. unsigned long __user *datap = (unsigned long __user *)data;
  716. switch (request) {
  717. /* read the word at location addr in the USER area. */
  718. case PTRACE_PEEKUSR: {
  719. unsigned long tmp;
  720. ret = -EIO;
  721. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  722. break;
  723. tmp = 0; /* Default return condition */
  724. if (addr < sizeof(struct user_regs_struct))
  725. tmp = getreg(child, addr);
  726. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  727. addr <= offsetof(struct user, u_debugreg[7])) {
  728. addr -= offsetof(struct user, u_debugreg[0]);
  729. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  730. }
  731. ret = put_user(tmp, datap);
  732. break;
  733. }
  734. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  735. ret = -EIO;
  736. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  737. break;
  738. if (addr < sizeof(struct user_regs_struct))
  739. ret = putreg(child, addr, data);
  740. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  741. addr <= offsetof(struct user, u_debugreg[7])) {
  742. addr -= offsetof(struct user, u_debugreg[0]);
  743. ret = ptrace_set_debugreg(child,
  744. addr / sizeof(data), data);
  745. }
  746. break;
  747. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  748. return copy_regset_to_user(child,
  749. task_user_regset_view(current),
  750. REGSET_GENERAL,
  751. 0, sizeof(struct user_regs_struct),
  752. datap);
  753. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  754. return copy_regset_from_user(child,
  755. task_user_regset_view(current),
  756. REGSET_GENERAL,
  757. 0, sizeof(struct user_regs_struct),
  758. datap);
  759. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  760. return copy_regset_to_user(child,
  761. task_user_regset_view(current),
  762. REGSET_FP,
  763. 0, sizeof(struct user_i387_struct),
  764. datap);
  765. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  766. return copy_regset_from_user(child,
  767. task_user_regset_view(current),
  768. REGSET_FP,
  769. 0, sizeof(struct user_i387_struct),
  770. datap);
  771. #ifdef CONFIG_X86_32
  772. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  773. return copy_regset_to_user(child, &user_x86_32_view,
  774. REGSET_XFP,
  775. 0, sizeof(struct user_fxsr_struct),
  776. datap) ? -EIO : 0;
  777. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  778. return copy_regset_from_user(child, &user_x86_32_view,
  779. REGSET_XFP,
  780. 0, sizeof(struct user_fxsr_struct),
  781. datap) ? -EIO : 0;
  782. #endif
  783. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  784. case PTRACE_GET_THREAD_AREA:
  785. if ((int) addr < 0)
  786. return -EIO;
  787. ret = do_get_thread_area(child, addr,
  788. (struct user_desc __user *)data);
  789. break;
  790. case PTRACE_SET_THREAD_AREA:
  791. if ((int) addr < 0)
  792. return -EIO;
  793. ret = do_set_thread_area(child, addr,
  794. (struct user_desc __user *)data, 0);
  795. break;
  796. #endif
  797. #ifdef CONFIG_X86_64
  798. /* normal 64bit interface to access TLS data.
  799. Works just like arch_prctl, except that the arguments
  800. are reversed. */
  801. case PTRACE_ARCH_PRCTL:
  802. ret = do_arch_prctl_64(child, data, addr);
  803. break;
  804. #endif
  805. default:
  806. ret = ptrace_request(child, request, addr, data);
  807. break;
  808. }
  809. return ret;
  810. }
  811. #ifdef CONFIG_IA32_EMULATION
  812. #include <linux/compat.h>
  813. #include <linux/syscalls.h>
  814. #include <asm/ia32.h>
  815. #include <asm/user32.h>
  816. #define R32(l,q) \
  817. case offsetof(struct user32, regs.l): \
  818. regs->q = value; break
  819. #define SEG32(rs) \
  820. case offsetof(struct user32, regs.rs): \
  821. return set_segment_reg(child, \
  822. offsetof(struct user_regs_struct, rs), \
  823. value); \
  824. break
  825. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  826. {
  827. struct pt_regs *regs = task_pt_regs(child);
  828. switch (regno) {
  829. SEG32(cs);
  830. SEG32(ds);
  831. SEG32(es);
  832. SEG32(fs);
  833. SEG32(gs);
  834. SEG32(ss);
  835. R32(ebx, bx);
  836. R32(ecx, cx);
  837. R32(edx, dx);
  838. R32(edi, di);
  839. R32(esi, si);
  840. R32(ebp, bp);
  841. R32(eax, ax);
  842. R32(eip, ip);
  843. R32(esp, sp);
  844. case offsetof(struct user32, regs.orig_eax):
  845. /*
  846. * Warning: bizarre corner case fixup here. A 32-bit
  847. * debugger setting orig_eax to -1 wants to disable
  848. * syscall restart. Make sure that the syscall
  849. * restart code sign-extends orig_ax. Also make sure
  850. * we interpret the -ERESTART* codes correctly if
  851. * loaded into regs->ax in case the task is not
  852. * actually still sitting at the exit from a 32-bit
  853. * syscall with TS_COMPAT still set.
  854. */
  855. regs->orig_ax = value;
  856. if (syscall_get_nr(child, regs) >= 0)
  857. child->thread_info.status |= TS_I386_REGS_POKED;
  858. break;
  859. case offsetof(struct user32, regs.eflags):
  860. return set_flags(child, value);
  861. case offsetof(struct user32, u_debugreg[0]) ...
  862. offsetof(struct user32, u_debugreg[7]):
  863. regno -= offsetof(struct user32, u_debugreg[0]);
  864. return ptrace_set_debugreg(child, regno / 4, value);
  865. default:
  866. if (regno > sizeof(struct user32) || (regno & 3))
  867. return -EIO;
  868. /*
  869. * Other dummy fields in the virtual user structure
  870. * are ignored
  871. */
  872. break;
  873. }
  874. return 0;
  875. }
  876. #undef R32
  877. #undef SEG32
  878. #define R32(l,q) \
  879. case offsetof(struct user32, regs.l): \
  880. *val = regs->q; break
  881. #define SEG32(rs) \
  882. case offsetof(struct user32, regs.rs): \
  883. *val = get_segment_reg(child, \
  884. offsetof(struct user_regs_struct, rs)); \
  885. break
  886. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  887. {
  888. struct pt_regs *regs = task_pt_regs(child);
  889. switch (regno) {
  890. SEG32(ds);
  891. SEG32(es);
  892. SEG32(fs);
  893. SEG32(gs);
  894. R32(cs, cs);
  895. R32(ss, ss);
  896. R32(ebx, bx);
  897. R32(ecx, cx);
  898. R32(edx, dx);
  899. R32(edi, di);
  900. R32(esi, si);
  901. R32(ebp, bp);
  902. R32(eax, ax);
  903. R32(orig_eax, orig_ax);
  904. R32(eip, ip);
  905. R32(esp, sp);
  906. case offsetof(struct user32, regs.eflags):
  907. *val = get_flags(child);
  908. break;
  909. case offsetof(struct user32, u_debugreg[0]) ...
  910. offsetof(struct user32, u_debugreg[7]):
  911. regno -= offsetof(struct user32, u_debugreg[0]);
  912. *val = ptrace_get_debugreg(child, regno / 4);
  913. break;
  914. default:
  915. if (regno > sizeof(struct user32) || (regno & 3))
  916. return -EIO;
  917. /*
  918. * Other dummy fields in the virtual user structure
  919. * are ignored
  920. */
  921. *val = 0;
  922. break;
  923. }
  924. return 0;
  925. }
  926. #undef R32
  927. #undef SEG32
  928. static int genregs32_get(struct task_struct *target,
  929. const struct user_regset *regset,
  930. unsigned int pos, unsigned int count,
  931. void *kbuf, void __user *ubuf)
  932. {
  933. if (kbuf) {
  934. compat_ulong_t *k = kbuf;
  935. while (count >= sizeof(*k)) {
  936. getreg32(target, pos, k++);
  937. count -= sizeof(*k);
  938. pos += sizeof(*k);
  939. }
  940. } else {
  941. compat_ulong_t __user *u = ubuf;
  942. while (count >= sizeof(*u)) {
  943. compat_ulong_t word;
  944. getreg32(target, pos, &word);
  945. if (__put_user(word, u++))
  946. return -EFAULT;
  947. count -= sizeof(*u);
  948. pos += sizeof(*u);
  949. }
  950. }
  951. return 0;
  952. }
  953. static int genregs32_set(struct task_struct *target,
  954. const struct user_regset *regset,
  955. unsigned int pos, unsigned int count,
  956. const void *kbuf, const void __user *ubuf)
  957. {
  958. int ret = 0;
  959. if (kbuf) {
  960. const compat_ulong_t *k = kbuf;
  961. while (count >= sizeof(*k) && !ret) {
  962. ret = putreg32(target, pos, *k++);
  963. count -= sizeof(*k);
  964. pos += sizeof(*k);
  965. }
  966. } else {
  967. const compat_ulong_t __user *u = ubuf;
  968. while (count >= sizeof(*u) && !ret) {
  969. compat_ulong_t word;
  970. ret = __get_user(word, u++);
  971. if (ret)
  972. break;
  973. ret = putreg32(target, pos, word);
  974. count -= sizeof(*u);
  975. pos += sizeof(*u);
  976. }
  977. }
  978. return ret;
  979. }
  980. static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
  981. compat_ulong_t caddr, compat_ulong_t cdata)
  982. {
  983. unsigned long addr = caddr;
  984. unsigned long data = cdata;
  985. void __user *datap = compat_ptr(data);
  986. int ret;
  987. __u32 val;
  988. switch (request) {
  989. case PTRACE_PEEKUSR:
  990. ret = getreg32(child, addr, &val);
  991. if (ret == 0)
  992. ret = put_user(val, (__u32 __user *)datap);
  993. break;
  994. case PTRACE_POKEUSR:
  995. ret = putreg32(child, addr, data);
  996. break;
  997. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  998. return copy_regset_to_user(child, &user_x86_32_view,
  999. REGSET_GENERAL,
  1000. 0, sizeof(struct user_regs_struct32),
  1001. datap);
  1002. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1003. return copy_regset_from_user(child, &user_x86_32_view,
  1004. REGSET_GENERAL, 0,
  1005. sizeof(struct user_regs_struct32),
  1006. datap);
  1007. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1008. return copy_regset_to_user(child, &user_x86_32_view,
  1009. REGSET_FP, 0,
  1010. sizeof(struct user_i387_ia32_struct),
  1011. datap);
  1012. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1013. return copy_regset_from_user(
  1014. child, &user_x86_32_view, REGSET_FP,
  1015. 0, sizeof(struct user_i387_ia32_struct), datap);
  1016. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1017. return copy_regset_to_user(child, &user_x86_32_view,
  1018. REGSET_XFP, 0,
  1019. sizeof(struct user32_fxsr_struct),
  1020. datap);
  1021. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1022. return copy_regset_from_user(child, &user_x86_32_view,
  1023. REGSET_XFP, 0,
  1024. sizeof(struct user32_fxsr_struct),
  1025. datap);
  1026. case PTRACE_GET_THREAD_AREA:
  1027. case PTRACE_SET_THREAD_AREA:
  1028. return arch_ptrace(child, request, addr, data);
  1029. default:
  1030. return compat_ptrace_request(child, request, addr, data);
  1031. }
  1032. return ret;
  1033. }
  1034. #endif /* CONFIG_IA32_EMULATION */
  1035. #ifdef CONFIG_X86_X32_ABI
  1036. static long x32_arch_ptrace(struct task_struct *child,
  1037. compat_long_t request, compat_ulong_t caddr,
  1038. compat_ulong_t cdata)
  1039. {
  1040. unsigned long addr = caddr;
  1041. unsigned long data = cdata;
  1042. void __user *datap = compat_ptr(data);
  1043. int ret;
  1044. switch (request) {
  1045. /* Read 32bits at location addr in the USER area. Only allow
  1046. to return the lower 32bits of segment and debug registers. */
  1047. case PTRACE_PEEKUSR: {
  1048. u32 tmp;
  1049. ret = -EIO;
  1050. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
  1051. addr < offsetof(struct user_regs_struct, cs))
  1052. break;
  1053. tmp = 0; /* Default return condition */
  1054. if (addr < sizeof(struct user_regs_struct))
  1055. tmp = getreg(child, addr);
  1056. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1057. addr <= offsetof(struct user, u_debugreg[7])) {
  1058. addr -= offsetof(struct user, u_debugreg[0]);
  1059. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  1060. }
  1061. ret = put_user(tmp, (__u32 __user *)datap);
  1062. break;
  1063. }
  1064. /* Write the word at location addr in the USER area. Only allow
  1065. to update segment and debug registers with the upper 32bits
  1066. zero-extended. */
  1067. case PTRACE_POKEUSR:
  1068. ret = -EIO;
  1069. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
  1070. addr < offsetof(struct user_regs_struct, cs))
  1071. break;
  1072. if (addr < sizeof(struct user_regs_struct))
  1073. ret = putreg(child, addr, data);
  1074. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1075. addr <= offsetof(struct user, u_debugreg[7])) {
  1076. addr -= offsetof(struct user, u_debugreg[0]);
  1077. ret = ptrace_set_debugreg(child,
  1078. addr / sizeof(data), data);
  1079. }
  1080. break;
  1081. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1082. return copy_regset_to_user(child,
  1083. task_user_regset_view(current),
  1084. REGSET_GENERAL,
  1085. 0, sizeof(struct user_regs_struct),
  1086. datap);
  1087. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1088. return copy_regset_from_user(child,
  1089. task_user_regset_view(current),
  1090. REGSET_GENERAL,
  1091. 0, sizeof(struct user_regs_struct),
  1092. datap);
  1093. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1094. return copy_regset_to_user(child,
  1095. task_user_regset_view(current),
  1096. REGSET_FP,
  1097. 0, sizeof(struct user_i387_struct),
  1098. datap);
  1099. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1100. return copy_regset_from_user(child,
  1101. task_user_regset_view(current),
  1102. REGSET_FP,
  1103. 0, sizeof(struct user_i387_struct),
  1104. datap);
  1105. default:
  1106. return compat_ptrace_request(child, request, addr, data);
  1107. }
  1108. return ret;
  1109. }
  1110. #endif
  1111. #ifdef CONFIG_COMPAT
  1112. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1113. compat_ulong_t caddr, compat_ulong_t cdata)
  1114. {
  1115. #ifdef CONFIG_X86_X32_ABI
  1116. if (!in_ia32_syscall())
  1117. return x32_arch_ptrace(child, request, caddr, cdata);
  1118. #endif
  1119. #ifdef CONFIG_IA32_EMULATION
  1120. return ia32_arch_ptrace(child, request, caddr, cdata);
  1121. #else
  1122. return 0;
  1123. #endif
  1124. }
  1125. #endif /* CONFIG_COMPAT */
  1126. #ifdef CONFIG_X86_64
  1127. static struct user_regset x86_64_regsets[] __ro_after_init = {
  1128. [REGSET_GENERAL] = {
  1129. .core_note_type = NT_PRSTATUS,
  1130. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1131. .size = sizeof(long), .align = sizeof(long),
  1132. .get = genregs_get, .set = genregs_set
  1133. },
  1134. [REGSET_FP] = {
  1135. .core_note_type = NT_PRFPREG,
  1136. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1137. .size = sizeof(long), .align = sizeof(long),
  1138. .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1139. },
  1140. [REGSET_XSTATE] = {
  1141. .core_note_type = NT_X86_XSTATE,
  1142. .size = sizeof(u64), .align = sizeof(u64),
  1143. .active = xstateregs_active, .get = xstateregs_get,
  1144. .set = xstateregs_set
  1145. },
  1146. [REGSET_IOPERM64] = {
  1147. .core_note_type = NT_386_IOPERM,
  1148. .n = IO_BITMAP_LONGS,
  1149. .size = sizeof(long), .align = sizeof(long),
  1150. .active = ioperm_active, .get = ioperm_get
  1151. },
  1152. };
  1153. static const struct user_regset_view user_x86_64_view = {
  1154. .name = "x86_64", .e_machine = EM_X86_64,
  1155. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1156. };
  1157. #else /* CONFIG_X86_32 */
  1158. #define user_regs_struct32 user_regs_struct
  1159. #define genregs32_get genregs_get
  1160. #define genregs32_set genregs_set
  1161. #endif /* CONFIG_X86_64 */
  1162. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1163. static struct user_regset x86_32_regsets[] __ro_after_init = {
  1164. [REGSET_GENERAL] = {
  1165. .core_note_type = NT_PRSTATUS,
  1166. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1167. .size = sizeof(u32), .align = sizeof(u32),
  1168. .get = genregs32_get, .set = genregs32_set
  1169. },
  1170. [REGSET_FP] = {
  1171. .core_note_type = NT_PRFPREG,
  1172. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1173. .size = sizeof(u32), .align = sizeof(u32),
  1174. .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
  1175. },
  1176. [REGSET_XFP] = {
  1177. .core_note_type = NT_PRXFPREG,
  1178. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1179. .size = sizeof(u32), .align = sizeof(u32),
  1180. .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1181. },
  1182. [REGSET_XSTATE] = {
  1183. .core_note_type = NT_X86_XSTATE,
  1184. .size = sizeof(u64), .align = sizeof(u64),
  1185. .active = xstateregs_active, .get = xstateregs_get,
  1186. .set = xstateregs_set
  1187. },
  1188. [REGSET_TLS] = {
  1189. .core_note_type = NT_386_TLS,
  1190. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1191. .size = sizeof(struct user_desc),
  1192. .align = sizeof(struct user_desc),
  1193. .active = regset_tls_active,
  1194. .get = regset_tls_get, .set = regset_tls_set
  1195. },
  1196. [REGSET_IOPERM32] = {
  1197. .core_note_type = NT_386_IOPERM,
  1198. .n = IO_BITMAP_BYTES / sizeof(u32),
  1199. .size = sizeof(u32), .align = sizeof(u32),
  1200. .active = ioperm_active, .get = ioperm_get
  1201. },
  1202. };
  1203. static const struct user_regset_view user_x86_32_view = {
  1204. .name = "i386", .e_machine = EM_386,
  1205. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1206. };
  1207. #endif
  1208. /*
  1209. * This represents bytes 464..511 in the memory layout exported through
  1210. * the REGSET_XSTATE interface.
  1211. */
  1212. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1213. void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1214. {
  1215. #ifdef CONFIG_X86_64
  1216. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1217. #endif
  1218. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1219. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1220. #endif
  1221. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1222. }
  1223. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1224. {
  1225. #ifdef CONFIG_IA32_EMULATION
  1226. if (!user_64bit_mode(task_pt_regs(task)))
  1227. #endif
  1228. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1229. return &user_x86_32_view;
  1230. #endif
  1231. #ifdef CONFIG_X86_64
  1232. return &user_x86_64_view;
  1233. #endif
  1234. }
  1235. static void fill_sigtrap_info(struct task_struct *tsk,
  1236. struct pt_regs *regs,
  1237. int error_code, int si_code,
  1238. struct siginfo *info)
  1239. {
  1240. tsk->thread.trap_nr = X86_TRAP_DB;
  1241. tsk->thread.error_code = error_code;
  1242. info->si_signo = SIGTRAP;
  1243. info->si_code = si_code;
  1244. info->si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
  1245. }
  1246. void user_single_step_siginfo(struct task_struct *tsk,
  1247. struct pt_regs *regs,
  1248. struct siginfo *info)
  1249. {
  1250. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1251. }
  1252. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1253. int error_code, int si_code)
  1254. {
  1255. struct siginfo info;
  1256. clear_siginfo(&info);
  1257. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1258. /* Send us the fake SIGTRAP */
  1259. force_sig_info(SIGTRAP, &info, tsk);
  1260. }