ptrace.c 36 KB

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