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