ptrace.c 34 KB

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