ptrace.c 35 KB

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