ptrace.c 35 KB

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