ptrace.c 35 KB

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