ptrace.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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. /*
  274. * If this is setting fs as for normal 64-bit use but
  275. * setting fs_base has implicitly changed it, leave it.
  276. */
  277. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  278. task->thread.fs != 0) ||
  279. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  280. task->thread.fs == 0))
  281. break;
  282. task->thread.fsindex = value;
  283. if (task == current)
  284. loadsegment(fs, task->thread.fsindex);
  285. break;
  286. case offsetof(struct user_regs_struct,gs):
  287. /*
  288. * If this is setting gs as for normal 64-bit use but
  289. * setting gs_base has implicitly changed it, leave it.
  290. */
  291. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  292. task->thread.gs != 0) ||
  293. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  294. task->thread.gs == 0))
  295. break;
  296. task->thread.gsindex = value;
  297. if (task == current)
  298. load_gs_index(task->thread.gsindex);
  299. break;
  300. case offsetof(struct user_regs_struct,ds):
  301. task->thread.ds = value;
  302. if (task == current)
  303. loadsegment(ds, task->thread.ds);
  304. break;
  305. case offsetof(struct user_regs_struct,es):
  306. task->thread.es = value;
  307. if (task == current)
  308. loadsegment(es, task->thread.es);
  309. break;
  310. /*
  311. * Can't actually change these in 64-bit mode.
  312. */
  313. case offsetof(struct user_regs_struct,cs):
  314. if (unlikely(value == 0))
  315. return -EIO;
  316. task_pt_regs(task)->cs = value;
  317. break;
  318. case offsetof(struct user_regs_struct,ss):
  319. if (unlikely(value == 0))
  320. return -EIO;
  321. task_pt_regs(task)->ss = value;
  322. break;
  323. }
  324. return 0;
  325. }
  326. #endif /* CONFIG_X86_32 */
  327. static unsigned long get_flags(struct task_struct *task)
  328. {
  329. unsigned long retval = task_pt_regs(task)->flags;
  330. /*
  331. * If the debugger set TF, hide it from the readout.
  332. */
  333. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  334. retval &= ~X86_EFLAGS_TF;
  335. return retval;
  336. }
  337. static int set_flags(struct task_struct *task, unsigned long value)
  338. {
  339. struct pt_regs *regs = task_pt_regs(task);
  340. /*
  341. * If the user value contains TF, mark that
  342. * it was not "us" (the debugger) that set it.
  343. * If not, make sure it stays set if we had.
  344. */
  345. if (value & X86_EFLAGS_TF)
  346. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  347. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  348. value |= X86_EFLAGS_TF;
  349. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  350. return 0;
  351. }
  352. static int putreg(struct task_struct *child,
  353. unsigned long offset, unsigned long value)
  354. {
  355. switch (offset) {
  356. case offsetof(struct user_regs_struct, cs):
  357. case offsetof(struct user_regs_struct, ds):
  358. case offsetof(struct user_regs_struct, es):
  359. case offsetof(struct user_regs_struct, fs):
  360. case offsetof(struct user_regs_struct, gs):
  361. case offsetof(struct user_regs_struct, ss):
  362. return set_segment_reg(child, offset, value);
  363. case offsetof(struct user_regs_struct, flags):
  364. return set_flags(child, value);
  365. #ifdef CONFIG_X86_64
  366. case offsetof(struct user_regs_struct,fs_base):
  367. if (value >= TASK_SIZE_OF(child))
  368. return -EIO;
  369. /*
  370. * When changing the segment base, use do_arch_prctl
  371. * to set either thread.fs or thread.fsindex and the
  372. * corresponding GDT slot.
  373. */
  374. if (child->thread.fs != value)
  375. return do_arch_prctl(child, ARCH_SET_FS, value);
  376. return 0;
  377. case offsetof(struct user_regs_struct,gs_base):
  378. /*
  379. * Exactly the same here as the %fs handling above.
  380. */
  381. if (value >= TASK_SIZE_OF(child))
  382. return -EIO;
  383. if (child->thread.gs != value)
  384. return do_arch_prctl(child, ARCH_SET_GS, value);
  385. return 0;
  386. #endif
  387. }
  388. *pt_regs_access(task_pt_regs(child), offset) = value;
  389. return 0;
  390. }
  391. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  392. {
  393. switch (offset) {
  394. case offsetof(struct user_regs_struct, cs):
  395. case offsetof(struct user_regs_struct, ds):
  396. case offsetof(struct user_regs_struct, es):
  397. case offsetof(struct user_regs_struct, fs):
  398. case offsetof(struct user_regs_struct, gs):
  399. case offsetof(struct user_regs_struct, ss):
  400. return get_segment_reg(task, offset);
  401. case offsetof(struct user_regs_struct, flags):
  402. return get_flags(task);
  403. #ifdef CONFIG_X86_64
  404. case offsetof(struct user_regs_struct, fs_base): {
  405. /*
  406. * do_arch_prctl may have used a GDT slot instead of
  407. * the MSR. To userland, it appears the same either
  408. * way, except the %fs segment selector might not be 0.
  409. */
  410. unsigned int seg = task->thread.fsindex;
  411. if (task->thread.fs != 0)
  412. return task->thread.fs;
  413. if (task == current)
  414. asm("movl %%fs,%0" : "=r" (seg));
  415. if (seg != FS_TLS_SEL)
  416. return 0;
  417. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  418. }
  419. case offsetof(struct user_regs_struct, gs_base): {
  420. /*
  421. * Exactly the same here as the %fs handling above.
  422. */
  423. unsigned int seg = task->thread.gsindex;
  424. if (task->thread.gs != 0)
  425. return task->thread.gs;
  426. if (task == current)
  427. asm("movl %%gs,%0" : "=r" (seg));
  428. if (seg != GS_TLS_SEL)
  429. return 0;
  430. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  431. }
  432. #endif
  433. }
  434. return *pt_regs_access(task_pt_regs(task), offset);
  435. }
  436. static int genregs_get(struct task_struct *target,
  437. const struct user_regset *regset,
  438. unsigned int pos, unsigned int count,
  439. void *kbuf, void __user *ubuf)
  440. {
  441. if (kbuf) {
  442. unsigned long *k = kbuf;
  443. while (count >= sizeof(*k)) {
  444. *k++ = getreg(target, pos);
  445. count -= sizeof(*k);
  446. pos += sizeof(*k);
  447. }
  448. } else {
  449. unsigned long __user *u = ubuf;
  450. while (count >= sizeof(*u)) {
  451. if (__put_user(getreg(target, pos), u++))
  452. return -EFAULT;
  453. count -= sizeof(*u);
  454. pos += sizeof(*u);
  455. }
  456. }
  457. return 0;
  458. }
  459. static int genregs_set(struct task_struct *target,
  460. const struct user_regset *regset,
  461. unsigned int pos, unsigned int count,
  462. const void *kbuf, const void __user *ubuf)
  463. {
  464. int ret = 0;
  465. if (kbuf) {
  466. const unsigned long *k = kbuf;
  467. while (count >= sizeof(*k) && !ret) {
  468. ret = putreg(target, pos, *k++);
  469. count -= sizeof(*k);
  470. pos += sizeof(*k);
  471. }
  472. } else {
  473. const unsigned long __user *u = ubuf;
  474. while (count >= sizeof(*u) && !ret) {
  475. unsigned long word;
  476. ret = __get_user(word, u++);
  477. if (ret)
  478. break;
  479. ret = putreg(target, pos, word);
  480. count -= sizeof(*u);
  481. pos += sizeof(*u);
  482. }
  483. }
  484. return ret;
  485. }
  486. static void ptrace_triggered(struct perf_event *bp,
  487. struct perf_sample_data *data,
  488. struct pt_regs *regs)
  489. {
  490. int i;
  491. struct thread_struct *thread = &(current->thread);
  492. /*
  493. * Store in the virtual DR6 register the fact that the breakpoint
  494. * was hit so the thread's debugger will see it.
  495. */
  496. for (i = 0; i < HBP_NUM; i++) {
  497. if (thread->ptrace_bps[i] == bp)
  498. break;
  499. }
  500. thread->debugreg6 |= (DR_TRAP0 << i);
  501. }
  502. /*
  503. * Walk through every ptrace breakpoints for this thread and
  504. * build the dr7 value on top of their attributes.
  505. *
  506. */
  507. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  508. {
  509. int i;
  510. int dr7 = 0;
  511. struct arch_hw_breakpoint *info;
  512. for (i = 0; i < HBP_NUM; i++) {
  513. if (bp[i] && !bp[i]->attr.disabled) {
  514. info = counter_arch_bp(bp[i]);
  515. dr7 |= encode_dr7(i, info->len, info->type);
  516. }
  517. }
  518. return dr7;
  519. }
  520. static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
  521. int len, int type, bool disabled)
  522. {
  523. int err, bp_len, bp_type;
  524. err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
  525. if (!err) {
  526. attr->bp_len = bp_len;
  527. attr->bp_type = bp_type;
  528. attr->disabled = disabled;
  529. }
  530. return err;
  531. }
  532. static struct perf_event *
  533. ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
  534. unsigned long addr, bool disabled)
  535. {
  536. struct perf_event_attr attr;
  537. int err;
  538. ptrace_breakpoint_init(&attr);
  539. attr.bp_addr = addr;
  540. err = ptrace_fill_bp_fields(&attr, len, type, disabled);
  541. if (err)
  542. return ERR_PTR(err);
  543. return register_user_hw_breakpoint(&attr, ptrace_triggered,
  544. NULL, tsk);
  545. }
  546. static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
  547. int disabled)
  548. {
  549. struct perf_event_attr attr = bp->attr;
  550. int err;
  551. err = ptrace_fill_bp_fields(&attr, len, type, disabled);
  552. if (err)
  553. return err;
  554. return modify_user_hw_breakpoint(bp, &attr);
  555. }
  556. /*
  557. * Handle ptrace writes to debug register 7.
  558. */
  559. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  560. {
  561. struct thread_struct *thread = &tsk->thread;
  562. unsigned long old_dr7;
  563. bool second_pass = false;
  564. int i, rc, ret = 0;
  565. data &= ~DR_CONTROL_RESERVED;
  566. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  567. restore:
  568. rc = 0;
  569. for (i = 0; i < HBP_NUM; i++) {
  570. unsigned len, type;
  571. bool disabled = !decode_dr7(data, i, &len, &type);
  572. struct perf_event *bp = thread->ptrace_bps[i];
  573. if (!bp) {
  574. if (disabled)
  575. continue;
  576. bp = ptrace_register_breakpoint(tsk,
  577. len, type, 0, disabled);
  578. if (IS_ERR(bp)) {
  579. rc = PTR_ERR(bp);
  580. break;
  581. }
  582. thread->ptrace_bps[i] = bp;
  583. continue;
  584. }
  585. rc = ptrace_modify_breakpoint(bp, len, type, disabled);
  586. if (rc)
  587. break;
  588. }
  589. /* Restore if the first pass failed, second_pass shouldn't fail. */
  590. if (rc && !WARN_ON(second_pass)) {
  591. ret = rc;
  592. data = old_dr7;
  593. second_pass = true;
  594. goto restore;
  595. }
  596. return ret;
  597. }
  598. /*
  599. * Handle PTRACE_PEEKUSR calls for the debug register area.
  600. */
  601. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  602. {
  603. struct thread_struct *thread = &tsk->thread;
  604. unsigned long val = 0;
  605. if (n < HBP_NUM) {
  606. struct perf_event *bp = thread->ptrace_bps[n];
  607. if (bp)
  608. val = bp->hw.info.address;
  609. } else if (n == 6) {
  610. val = thread->debugreg6;
  611. } else if (n == 7) {
  612. val = thread->ptrace_dr7;
  613. }
  614. return val;
  615. }
  616. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  617. unsigned long addr)
  618. {
  619. struct thread_struct *t = &tsk->thread;
  620. struct perf_event *bp = t->ptrace_bps[nr];
  621. int err = 0;
  622. if (!bp) {
  623. /*
  624. * Put stub len and type to create an inactive but correct bp.
  625. *
  626. * CHECKME: the previous code returned -EIO if the addr wasn't
  627. * a valid task virtual addr. The new one will return -EINVAL in
  628. * this case.
  629. * -EINVAL may be what we want for in-kernel breakpoints users,
  630. * but -EIO looks better for ptrace, since we refuse a register
  631. * writing for the user. And anyway this is the previous
  632. * behaviour.
  633. */
  634. bp = ptrace_register_breakpoint(tsk,
  635. X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
  636. addr, true);
  637. if (IS_ERR(bp))
  638. err = PTR_ERR(bp);
  639. else
  640. t->ptrace_bps[nr] = bp;
  641. } else {
  642. struct perf_event_attr attr = bp->attr;
  643. attr.bp_addr = addr;
  644. err = modify_user_hw_breakpoint(bp, &attr);
  645. }
  646. return err;
  647. }
  648. /*
  649. * Handle PTRACE_POKEUSR calls for the debug register area.
  650. */
  651. static int ptrace_set_debugreg(struct task_struct *tsk, int n,
  652. unsigned long val)
  653. {
  654. struct thread_struct *thread = &tsk->thread;
  655. /* There are no DR4 or DR5 registers */
  656. int rc = -EIO;
  657. if (n < HBP_NUM) {
  658. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  659. } else if (n == 6) {
  660. thread->debugreg6 = val;
  661. rc = 0;
  662. } else if (n == 7) {
  663. rc = ptrace_write_dr7(tsk, val);
  664. if (!rc)
  665. thread->ptrace_dr7 = val;
  666. }
  667. return rc;
  668. }
  669. /*
  670. * These access the current or another (stopped) task's io permission
  671. * bitmap for debugging or core dump.
  672. */
  673. static int ioperm_active(struct task_struct *target,
  674. const struct user_regset *regset)
  675. {
  676. return target->thread.io_bitmap_max / regset->size;
  677. }
  678. static int ioperm_get(struct task_struct *target,
  679. const struct user_regset *regset,
  680. unsigned int pos, unsigned int count,
  681. void *kbuf, void __user *ubuf)
  682. {
  683. if (!target->thread.io_bitmap_ptr)
  684. return -ENXIO;
  685. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  686. target->thread.io_bitmap_ptr,
  687. 0, IO_BITMAP_BYTES);
  688. }
  689. /*
  690. * Called by kernel/ptrace.c when detaching..
  691. *
  692. * Make sure the single step bit is not set.
  693. */
  694. void ptrace_disable(struct task_struct *child)
  695. {
  696. user_disable_single_step(child);
  697. #ifdef TIF_SYSCALL_EMU
  698. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  699. #endif
  700. }
  701. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  702. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  703. #endif
  704. long arch_ptrace(struct task_struct *child, long request,
  705. unsigned long addr, unsigned long data)
  706. {
  707. int ret;
  708. unsigned long __user *datap = (unsigned long __user *)data;
  709. switch (request) {
  710. /* read the word at location addr in the USER area. */
  711. case PTRACE_PEEKUSR: {
  712. unsigned long tmp;
  713. ret = -EIO;
  714. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  715. break;
  716. tmp = 0; /* Default return condition */
  717. if (addr < sizeof(struct user_regs_struct))
  718. tmp = getreg(child, addr);
  719. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  720. addr <= offsetof(struct user, u_debugreg[7])) {
  721. addr -= offsetof(struct user, u_debugreg[0]);
  722. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  723. }
  724. ret = put_user(tmp, datap);
  725. break;
  726. }
  727. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  728. ret = -EIO;
  729. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  730. break;
  731. if (addr < sizeof(struct user_regs_struct))
  732. ret = putreg(child, addr, data);
  733. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  734. addr <= offsetof(struct user, u_debugreg[7])) {
  735. addr -= offsetof(struct user, u_debugreg[0]);
  736. ret = ptrace_set_debugreg(child,
  737. addr / sizeof(data), data);
  738. }
  739. break;
  740. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  741. return copy_regset_to_user(child,
  742. task_user_regset_view(current),
  743. REGSET_GENERAL,
  744. 0, sizeof(struct user_regs_struct),
  745. datap);
  746. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  747. return copy_regset_from_user(child,
  748. task_user_regset_view(current),
  749. REGSET_GENERAL,
  750. 0, sizeof(struct user_regs_struct),
  751. datap);
  752. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  753. return copy_regset_to_user(child,
  754. task_user_regset_view(current),
  755. REGSET_FP,
  756. 0, sizeof(struct user_i387_struct),
  757. datap);
  758. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  759. return copy_regset_from_user(child,
  760. task_user_regset_view(current),
  761. REGSET_FP,
  762. 0, sizeof(struct user_i387_struct),
  763. datap);
  764. #ifdef CONFIG_X86_32
  765. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  766. return copy_regset_to_user(child, &user_x86_32_view,
  767. REGSET_XFP,
  768. 0, sizeof(struct user_fxsr_struct),
  769. datap) ? -EIO : 0;
  770. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  771. return copy_regset_from_user(child, &user_x86_32_view,
  772. REGSET_XFP,
  773. 0, sizeof(struct user_fxsr_struct),
  774. datap) ? -EIO : 0;
  775. #endif
  776. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  777. case PTRACE_GET_THREAD_AREA:
  778. if ((int) addr < 0)
  779. return -EIO;
  780. ret = do_get_thread_area(child, addr,
  781. (struct user_desc __user *)data);
  782. break;
  783. case PTRACE_SET_THREAD_AREA:
  784. if ((int) addr < 0)
  785. return -EIO;
  786. ret = do_set_thread_area(child, addr,
  787. (struct user_desc __user *)data, 0);
  788. break;
  789. #endif
  790. #ifdef CONFIG_X86_64
  791. /* normal 64bit interface to access TLS data.
  792. Works just like arch_prctl, except that the arguments
  793. are reversed. */
  794. case PTRACE_ARCH_PRCTL:
  795. ret = do_arch_prctl(child, data, addr);
  796. break;
  797. #endif
  798. default:
  799. ret = ptrace_request(child, request, addr, data);
  800. break;
  801. }
  802. return ret;
  803. }
  804. #ifdef CONFIG_IA32_EMULATION
  805. #include <linux/compat.h>
  806. #include <linux/syscalls.h>
  807. #include <asm/ia32.h>
  808. #include <asm/user32.h>
  809. #define R32(l,q) \
  810. case offsetof(struct user32, regs.l): \
  811. regs->q = value; break
  812. #define SEG32(rs) \
  813. case offsetof(struct user32, regs.rs): \
  814. return set_segment_reg(child, \
  815. offsetof(struct user_regs_struct, rs), \
  816. value); \
  817. break
  818. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  819. {
  820. struct pt_regs *regs = task_pt_regs(child);
  821. switch (regno) {
  822. SEG32(cs);
  823. SEG32(ds);
  824. SEG32(es);
  825. SEG32(fs);
  826. SEG32(gs);
  827. SEG32(ss);
  828. R32(ebx, bx);
  829. R32(ecx, cx);
  830. R32(edx, dx);
  831. R32(edi, di);
  832. R32(esi, si);
  833. R32(ebp, bp);
  834. R32(eax, ax);
  835. R32(eip, ip);
  836. R32(esp, sp);
  837. case offsetof(struct user32, regs.orig_eax):
  838. /*
  839. * A 32-bit debugger setting orig_eax means to restore
  840. * the state of the task restarting a 32-bit syscall.
  841. * Make sure we interpret the -ERESTART* codes correctly
  842. * in case the task is not actually still sitting at the
  843. * exit from a 32-bit syscall with TS_COMPAT still set.
  844. */
  845. regs->orig_ax = value;
  846. if (syscall_get_nr(child, regs) >= 0)
  847. task_thread_info(child)->status |= TS_COMPAT;
  848. break;
  849. case offsetof(struct user32, regs.eflags):
  850. return set_flags(child, value);
  851. case offsetof(struct user32, u_debugreg[0]) ...
  852. offsetof(struct user32, u_debugreg[7]):
  853. regno -= offsetof(struct user32, u_debugreg[0]);
  854. return ptrace_set_debugreg(child, regno / 4, value);
  855. default:
  856. if (regno > sizeof(struct user32) || (regno & 3))
  857. return -EIO;
  858. /*
  859. * Other dummy fields in the virtual user structure
  860. * are ignored
  861. */
  862. break;
  863. }
  864. return 0;
  865. }
  866. #undef R32
  867. #undef SEG32
  868. #define R32(l,q) \
  869. case offsetof(struct user32, regs.l): \
  870. *val = regs->q; break
  871. #define SEG32(rs) \
  872. case offsetof(struct user32, regs.rs): \
  873. *val = get_segment_reg(child, \
  874. offsetof(struct user_regs_struct, rs)); \
  875. break
  876. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  877. {
  878. struct pt_regs *regs = task_pt_regs(child);
  879. switch (regno) {
  880. SEG32(ds);
  881. SEG32(es);
  882. SEG32(fs);
  883. SEG32(gs);
  884. R32(cs, cs);
  885. R32(ss, ss);
  886. R32(ebx, bx);
  887. R32(ecx, cx);
  888. R32(edx, dx);
  889. R32(edi, di);
  890. R32(esi, si);
  891. R32(ebp, bp);
  892. R32(eax, ax);
  893. R32(orig_eax, orig_ax);
  894. R32(eip, ip);
  895. R32(esp, sp);
  896. case offsetof(struct user32, regs.eflags):
  897. *val = get_flags(child);
  898. break;
  899. case offsetof(struct user32, u_debugreg[0]) ...
  900. offsetof(struct user32, u_debugreg[7]):
  901. regno -= offsetof(struct user32, u_debugreg[0]);
  902. *val = ptrace_get_debugreg(child, regno / 4);
  903. break;
  904. default:
  905. if (regno > sizeof(struct user32) || (regno & 3))
  906. return -EIO;
  907. /*
  908. * Other dummy fields in the virtual user structure
  909. * are ignored
  910. */
  911. *val = 0;
  912. break;
  913. }
  914. return 0;
  915. }
  916. #undef R32
  917. #undef SEG32
  918. static int genregs32_get(struct task_struct *target,
  919. const struct user_regset *regset,
  920. unsigned int pos, unsigned int count,
  921. void *kbuf, void __user *ubuf)
  922. {
  923. if (kbuf) {
  924. compat_ulong_t *k = kbuf;
  925. while (count >= sizeof(*k)) {
  926. getreg32(target, pos, k++);
  927. count -= sizeof(*k);
  928. pos += sizeof(*k);
  929. }
  930. } else {
  931. compat_ulong_t __user *u = ubuf;
  932. while (count >= sizeof(*u)) {
  933. compat_ulong_t word;
  934. getreg32(target, pos, &word);
  935. if (__put_user(word, u++))
  936. return -EFAULT;
  937. count -= sizeof(*u);
  938. pos += sizeof(*u);
  939. }
  940. }
  941. return 0;
  942. }
  943. static int genregs32_set(struct task_struct *target,
  944. const struct user_regset *regset,
  945. unsigned int pos, unsigned int count,
  946. const void *kbuf, const void __user *ubuf)
  947. {
  948. int ret = 0;
  949. if (kbuf) {
  950. const compat_ulong_t *k = kbuf;
  951. while (count >= sizeof(*k) && !ret) {
  952. ret = putreg32(target, pos, *k++);
  953. count -= sizeof(*k);
  954. pos += sizeof(*k);
  955. }
  956. } else {
  957. const compat_ulong_t __user *u = ubuf;
  958. while (count >= sizeof(*u) && !ret) {
  959. compat_ulong_t word;
  960. ret = __get_user(word, u++);
  961. if (ret)
  962. break;
  963. ret = putreg32(target, pos, word);
  964. count -= sizeof(*u);
  965. pos += sizeof(*u);
  966. }
  967. }
  968. return ret;
  969. }
  970. static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
  971. compat_ulong_t caddr, compat_ulong_t cdata)
  972. {
  973. unsigned long addr = caddr;
  974. unsigned long data = cdata;
  975. void __user *datap = compat_ptr(data);
  976. int ret;
  977. __u32 val;
  978. switch (request) {
  979. case PTRACE_PEEKUSR:
  980. ret = getreg32(child, addr, &val);
  981. if (ret == 0)
  982. ret = put_user(val, (__u32 __user *)datap);
  983. break;
  984. case PTRACE_POKEUSR:
  985. ret = putreg32(child, addr, data);
  986. break;
  987. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  988. return copy_regset_to_user(child, &user_x86_32_view,
  989. REGSET_GENERAL,
  990. 0, sizeof(struct user_regs_struct32),
  991. datap);
  992. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  993. return copy_regset_from_user(child, &user_x86_32_view,
  994. REGSET_GENERAL, 0,
  995. sizeof(struct user_regs_struct32),
  996. datap);
  997. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  998. return copy_regset_to_user(child, &user_x86_32_view,
  999. REGSET_FP, 0,
  1000. sizeof(struct user_i387_ia32_struct),
  1001. datap);
  1002. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1003. return copy_regset_from_user(
  1004. child, &user_x86_32_view, REGSET_FP,
  1005. 0, sizeof(struct user_i387_ia32_struct), datap);
  1006. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1007. return copy_regset_to_user(child, &user_x86_32_view,
  1008. REGSET_XFP, 0,
  1009. sizeof(struct user32_fxsr_struct),
  1010. datap);
  1011. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1012. return copy_regset_from_user(child, &user_x86_32_view,
  1013. REGSET_XFP, 0,
  1014. sizeof(struct user32_fxsr_struct),
  1015. datap);
  1016. case PTRACE_GET_THREAD_AREA:
  1017. case PTRACE_SET_THREAD_AREA:
  1018. return arch_ptrace(child, request, addr, data);
  1019. default:
  1020. return compat_ptrace_request(child, request, addr, data);
  1021. }
  1022. return ret;
  1023. }
  1024. #endif /* CONFIG_IA32_EMULATION */
  1025. #ifdef CONFIG_X86_X32_ABI
  1026. static long x32_arch_ptrace(struct task_struct *child,
  1027. compat_long_t request, compat_ulong_t caddr,
  1028. compat_ulong_t cdata)
  1029. {
  1030. unsigned long addr = caddr;
  1031. unsigned long data = cdata;
  1032. void __user *datap = compat_ptr(data);
  1033. int ret;
  1034. switch (request) {
  1035. /* Read 32bits at location addr in the USER area. Only allow
  1036. to return the lower 32bits of segment and debug registers. */
  1037. case PTRACE_PEEKUSR: {
  1038. u32 tmp;
  1039. ret = -EIO;
  1040. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
  1041. addr < offsetof(struct user_regs_struct, cs))
  1042. break;
  1043. tmp = 0; /* Default return condition */
  1044. if (addr < sizeof(struct user_regs_struct))
  1045. tmp = getreg(child, addr);
  1046. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1047. addr <= offsetof(struct user, u_debugreg[7])) {
  1048. addr -= offsetof(struct user, u_debugreg[0]);
  1049. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  1050. }
  1051. ret = put_user(tmp, (__u32 __user *)datap);
  1052. break;
  1053. }
  1054. /* Write the word at location addr in the USER area. Only allow
  1055. to update segment and debug registers with the upper 32bits
  1056. zero-extended. */
  1057. case PTRACE_POKEUSR:
  1058. ret = -EIO;
  1059. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
  1060. addr < offsetof(struct user_regs_struct, cs))
  1061. break;
  1062. if (addr < sizeof(struct user_regs_struct))
  1063. ret = putreg(child, addr, data);
  1064. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1065. addr <= offsetof(struct user, u_debugreg[7])) {
  1066. addr -= offsetof(struct user, u_debugreg[0]);
  1067. ret = ptrace_set_debugreg(child,
  1068. addr / sizeof(data), data);
  1069. }
  1070. break;
  1071. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1072. return copy_regset_to_user(child,
  1073. task_user_regset_view(current),
  1074. REGSET_GENERAL,
  1075. 0, sizeof(struct user_regs_struct),
  1076. datap);
  1077. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1078. return copy_regset_from_user(child,
  1079. task_user_regset_view(current),
  1080. REGSET_GENERAL,
  1081. 0, sizeof(struct user_regs_struct),
  1082. datap);
  1083. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1084. return copy_regset_to_user(child,
  1085. task_user_regset_view(current),
  1086. REGSET_FP,
  1087. 0, sizeof(struct user_i387_struct),
  1088. datap);
  1089. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1090. return copy_regset_from_user(child,
  1091. task_user_regset_view(current),
  1092. REGSET_FP,
  1093. 0, sizeof(struct user_i387_struct),
  1094. datap);
  1095. default:
  1096. return compat_ptrace_request(child, request, addr, data);
  1097. }
  1098. return ret;
  1099. }
  1100. #endif
  1101. #ifdef CONFIG_COMPAT
  1102. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1103. compat_ulong_t caddr, compat_ulong_t cdata)
  1104. {
  1105. #ifdef CONFIG_X86_X32_ABI
  1106. if (!is_ia32_task())
  1107. return x32_arch_ptrace(child, request, caddr, cdata);
  1108. #endif
  1109. #ifdef CONFIG_IA32_EMULATION
  1110. return ia32_arch_ptrace(child, request, caddr, cdata);
  1111. #else
  1112. return 0;
  1113. #endif
  1114. }
  1115. #endif /* CONFIG_COMPAT */
  1116. #ifdef CONFIG_X86_64
  1117. static struct user_regset x86_64_regsets[] __read_mostly = {
  1118. [REGSET_GENERAL] = {
  1119. .core_note_type = NT_PRSTATUS,
  1120. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1121. .size = sizeof(long), .align = sizeof(long),
  1122. .get = genregs_get, .set = genregs_set
  1123. },
  1124. [REGSET_FP] = {
  1125. .core_note_type = NT_PRFPREG,
  1126. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1127. .size = sizeof(long), .align = sizeof(long),
  1128. .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1129. },
  1130. [REGSET_XSTATE] = {
  1131. .core_note_type = NT_X86_XSTATE,
  1132. .size = sizeof(u64), .align = sizeof(u64),
  1133. .active = xstateregs_active, .get = xstateregs_get,
  1134. .set = xstateregs_set
  1135. },
  1136. [REGSET_IOPERM64] = {
  1137. .core_note_type = NT_386_IOPERM,
  1138. .n = IO_BITMAP_LONGS,
  1139. .size = sizeof(long), .align = sizeof(long),
  1140. .active = ioperm_active, .get = ioperm_get
  1141. },
  1142. };
  1143. static const struct user_regset_view user_x86_64_view = {
  1144. .name = "x86_64", .e_machine = EM_X86_64,
  1145. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1146. };
  1147. #else /* CONFIG_X86_32 */
  1148. #define user_regs_struct32 user_regs_struct
  1149. #define genregs32_get genregs_get
  1150. #define genregs32_set genregs_set
  1151. #endif /* CONFIG_X86_64 */
  1152. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1153. static struct user_regset x86_32_regsets[] __read_mostly = {
  1154. [REGSET_GENERAL] = {
  1155. .core_note_type = NT_PRSTATUS,
  1156. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1157. .size = sizeof(u32), .align = sizeof(u32),
  1158. .get = genregs32_get, .set = genregs32_set
  1159. },
  1160. [REGSET_FP] = {
  1161. .core_note_type = NT_PRFPREG,
  1162. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1163. .size = sizeof(u32), .align = sizeof(u32),
  1164. .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
  1165. },
  1166. [REGSET_XFP] = {
  1167. .core_note_type = NT_PRXFPREG,
  1168. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1169. .size = sizeof(u32), .align = sizeof(u32),
  1170. .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1171. },
  1172. [REGSET_XSTATE] = {
  1173. .core_note_type = NT_X86_XSTATE,
  1174. .size = sizeof(u64), .align = sizeof(u64),
  1175. .active = xstateregs_active, .get = xstateregs_get,
  1176. .set = xstateregs_set
  1177. },
  1178. [REGSET_TLS] = {
  1179. .core_note_type = NT_386_TLS,
  1180. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1181. .size = sizeof(struct user_desc),
  1182. .align = sizeof(struct user_desc),
  1183. .active = regset_tls_active,
  1184. .get = regset_tls_get, .set = regset_tls_set
  1185. },
  1186. [REGSET_IOPERM32] = {
  1187. .core_note_type = NT_386_IOPERM,
  1188. .n = IO_BITMAP_BYTES / sizeof(u32),
  1189. .size = sizeof(u32), .align = sizeof(u32),
  1190. .active = ioperm_active, .get = ioperm_get
  1191. },
  1192. };
  1193. static const struct user_regset_view user_x86_32_view = {
  1194. .name = "i386", .e_machine = EM_386,
  1195. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1196. };
  1197. #endif
  1198. /*
  1199. * This represents bytes 464..511 in the memory layout exported through
  1200. * the REGSET_XSTATE interface.
  1201. */
  1202. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1203. void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1204. {
  1205. #ifdef CONFIG_X86_64
  1206. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1207. #endif
  1208. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1209. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1210. #endif
  1211. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1212. }
  1213. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1214. {
  1215. #ifdef CONFIG_IA32_EMULATION
  1216. if (test_tsk_thread_flag(task, TIF_IA32))
  1217. #endif
  1218. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1219. return &user_x86_32_view;
  1220. #endif
  1221. #ifdef CONFIG_X86_64
  1222. return &user_x86_64_view;
  1223. #endif
  1224. }
  1225. static void fill_sigtrap_info(struct task_struct *tsk,
  1226. struct pt_regs *regs,
  1227. int error_code, int si_code,
  1228. struct siginfo *info)
  1229. {
  1230. tsk->thread.trap_nr = X86_TRAP_DB;
  1231. tsk->thread.error_code = error_code;
  1232. memset(info, 0, sizeof(*info));
  1233. info->si_signo = SIGTRAP;
  1234. info->si_code = si_code;
  1235. info->si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
  1236. }
  1237. void user_single_step_siginfo(struct task_struct *tsk,
  1238. struct pt_regs *regs,
  1239. struct siginfo *info)
  1240. {
  1241. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1242. }
  1243. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1244. int error_code, int si_code)
  1245. {
  1246. struct siginfo info;
  1247. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1248. /* Send us the fake SIGTRAP */
  1249. force_sig_info(SIGTRAP, &info, tsk);
  1250. }