ptrace.c 38 KB

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