ptrace.c 34 KB

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