ptrace.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. data &= ~DR_CONTROL_RESERVED;
  547. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  548. restore:
  549. /*
  550. * Loop through all the hardware breakpoints, making the
  551. * appropriate changes to each.
  552. */
  553. for (i = 0; i < HBP_NUM; i++) {
  554. enabled = decode_dr7(data, i, &len, &type);
  555. bp = thread->ptrace_bps[i];
  556. if (!enabled) {
  557. if (bp) {
  558. /*
  559. * Don't unregister the breakpoints right-away,
  560. * unless all register_user_hw_breakpoint()
  561. * requests have succeeded. This prevents
  562. * any window of opportunity for debug
  563. * register grabbing by other users.
  564. */
  565. if (!second_pass)
  566. continue;
  567. rc = ptrace_modify_breakpoint(bp, len, type,
  568. tsk, 1);
  569. if (rc)
  570. break;
  571. }
  572. continue;
  573. }
  574. rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
  575. if (rc)
  576. break;
  577. }
  578. /*
  579. * Make a second pass to free the remaining unused breakpoints
  580. * or to restore the original breakpoints if an error occurred.
  581. */
  582. if (!second_pass) {
  583. second_pass = 1;
  584. if (rc < 0) {
  585. orig_ret = rc;
  586. data = old_dr7;
  587. }
  588. goto restore;
  589. }
  590. return ((orig_ret < 0) ? orig_ret : rc);
  591. }
  592. /*
  593. * Handle PTRACE_PEEKUSR calls for the debug register area.
  594. */
  595. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  596. {
  597. struct thread_struct *thread = &(tsk->thread);
  598. unsigned long val = 0;
  599. if (n < HBP_NUM) {
  600. struct perf_event *bp;
  601. bp = thread->ptrace_bps[n];
  602. if (!bp)
  603. return 0;
  604. val = bp->hw.info.address;
  605. } else if (n == 6) {
  606. val = thread->debugreg6;
  607. } else if (n == 7) {
  608. val = thread->ptrace_dr7;
  609. }
  610. return val;
  611. }
  612. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  613. unsigned long addr)
  614. {
  615. struct perf_event *bp;
  616. struct thread_struct *t = &tsk->thread;
  617. struct perf_event_attr attr;
  618. if (!t->ptrace_bps[nr]) {
  619. ptrace_breakpoint_init(&attr);
  620. /*
  621. * Put stub len and type to register (reserve) an inactive but
  622. * correct bp
  623. */
  624. attr.bp_addr = addr;
  625. attr.bp_len = HW_BREAKPOINT_LEN_1;
  626. attr.bp_type = HW_BREAKPOINT_W;
  627. attr.disabled = 1;
  628. bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
  629. /*
  630. * CHECKME: the previous code returned -EIO if the addr wasn't
  631. * a valid task virtual addr. The new one will return -EINVAL in
  632. * this case.
  633. * -EINVAL may be what we want for in-kernel breakpoints users,
  634. * but -EIO looks better for ptrace, since we refuse a register
  635. * writing for the user. And anyway this is the previous
  636. * behaviour.
  637. */
  638. if (IS_ERR(bp))
  639. return PTR_ERR(bp);
  640. t->ptrace_bps[nr] = bp;
  641. } else {
  642. int err;
  643. bp = t->ptrace_bps[nr];
  644. attr = bp->attr;
  645. attr.bp_addr = addr;
  646. err = modify_user_hw_breakpoint(bp, &attr);
  647. if (err)
  648. return err;
  649. }
  650. return 0;
  651. }
  652. /*
  653. * Handle PTRACE_POKEUSR calls for the debug register area.
  654. */
  655. int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
  656. {
  657. struct thread_struct *thread = &(tsk->thread);
  658. int rc = 0;
  659. /* There are no DR4 or DR5 registers */
  660. if (n == 4 || n == 5)
  661. return -EIO;
  662. if (n == 6) {
  663. thread->debugreg6 = val;
  664. goto ret_path;
  665. }
  666. if (n < HBP_NUM) {
  667. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  668. if (rc)
  669. return rc;
  670. }
  671. /* All that's left is DR7 */
  672. if (n == 7) {
  673. rc = ptrace_write_dr7(tsk, val);
  674. if (!rc)
  675. thread->ptrace_dr7 = val;
  676. }
  677. ret_path:
  678. return rc;
  679. }
  680. /*
  681. * These access the current or another (stopped) task's io permission
  682. * bitmap for debugging or core dump.
  683. */
  684. static int ioperm_active(struct task_struct *target,
  685. const struct user_regset *regset)
  686. {
  687. return target->thread.io_bitmap_max / regset->size;
  688. }
  689. static int ioperm_get(struct task_struct *target,
  690. const struct user_regset *regset,
  691. unsigned int pos, unsigned int count,
  692. void *kbuf, void __user *ubuf)
  693. {
  694. if (!target->thread.io_bitmap_ptr)
  695. return -ENXIO;
  696. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  697. target->thread.io_bitmap_ptr,
  698. 0, IO_BITMAP_BYTES);
  699. }
  700. /*
  701. * Called by kernel/ptrace.c when detaching..
  702. *
  703. * Make sure the single step bit is not set.
  704. */
  705. void ptrace_disable(struct task_struct *child)
  706. {
  707. user_disable_single_step(child);
  708. #ifdef TIF_SYSCALL_EMU
  709. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  710. #endif
  711. }
  712. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  713. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  714. #endif
  715. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  716. {
  717. int ret;
  718. unsigned long __user *datap = (unsigned long __user *)data;
  719. switch (request) {
  720. /* read the word at location addr in the USER area. */
  721. case PTRACE_PEEKUSR: {
  722. unsigned long tmp;
  723. ret = -EIO;
  724. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  725. addr >= sizeof(struct user))
  726. break;
  727. tmp = 0; /* Default return condition */
  728. if (addr < sizeof(struct user_regs_struct))
  729. tmp = getreg(child, addr);
  730. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  731. addr <= offsetof(struct user, u_debugreg[7])) {
  732. addr -= offsetof(struct user, u_debugreg[0]);
  733. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  734. }
  735. ret = put_user(tmp, datap);
  736. break;
  737. }
  738. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  739. ret = -EIO;
  740. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  741. addr >= sizeof(struct user))
  742. break;
  743. if (addr < sizeof(struct user_regs_struct))
  744. ret = putreg(child, addr, data);
  745. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  746. addr <= offsetof(struct user, u_debugreg[7])) {
  747. addr -= offsetof(struct user, u_debugreg[0]);
  748. ret = ptrace_set_debugreg(child,
  749. addr / sizeof(data), data);
  750. }
  751. break;
  752. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  753. return copy_regset_to_user(child,
  754. task_user_regset_view(current),
  755. REGSET_GENERAL,
  756. 0, sizeof(struct user_regs_struct),
  757. datap);
  758. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  759. return copy_regset_from_user(child,
  760. task_user_regset_view(current),
  761. REGSET_GENERAL,
  762. 0, sizeof(struct user_regs_struct),
  763. datap);
  764. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  765. return copy_regset_to_user(child,
  766. task_user_regset_view(current),
  767. REGSET_FP,
  768. 0, sizeof(struct user_i387_struct),
  769. datap);
  770. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  771. return copy_regset_from_user(child,
  772. task_user_regset_view(current),
  773. REGSET_FP,
  774. 0, sizeof(struct user_i387_struct),
  775. datap);
  776. #ifdef CONFIG_X86_32
  777. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  778. return copy_regset_to_user(child, &user_x86_32_view,
  779. REGSET_XFP,
  780. 0, sizeof(struct user_fxsr_struct),
  781. datap) ? -EIO : 0;
  782. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  783. return copy_regset_from_user(child, &user_x86_32_view,
  784. REGSET_XFP,
  785. 0, sizeof(struct user_fxsr_struct),
  786. datap) ? -EIO : 0;
  787. #endif
  788. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  789. case PTRACE_GET_THREAD_AREA:
  790. if (addr < 0)
  791. return -EIO;
  792. ret = do_get_thread_area(child, addr,
  793. (struct user_desc __user *) data);
  794. break;
  795. case PTRACE_SET_THREAD_AREA:
  796. if (addr < 0)
  797. return -EIO;
  798. ret = do_set_thread_area(child, addr,
  799. (struct user_desc __user *) data, 0);
  800. break;
  801. #endif
  802. #ifdef CONFIG_X86_64
  803. /* normal 64bit interface to access TLS data.
  804. Works just like arch_prctl, except that the arguments
  805. are reversed. */
  806. case PTRACE_ARCH_PRCTL:
  807. ret = do_arch_prctl(child, data, addr);
  808. break;
  809. #endif
  810. default:
  811. ret = ptrace_request(child, request, addr, data);
  812. break;
  813. }
  814. return ret;
  815. }
  816. #ifdef CONFIG_IA32_EMULATION
  817. #include <linux/compat.h>
  818. #include <linux/syscalls.h>
  819. #include <asm/ia32.h>
  820. #include <asm/user32.h>
  821. #define R32(l,q) \
  822. case offsetof(struct user32, regs.l): \
  823. regs->q = value; break
  824. #define SEG32(rs) \
  825. case offsetof(struct user32, regs.rs): \
  826. return set_segment_reg(child, \
  827. offsetof(struct user_regs_struct, rs), \
  828. value); \
  829. break
  830. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  831. {
  832. struct pt_regs *regs = task_pt_regs(child);
  833. switch (regno) {
  834. SEG32(cs);
  835. SEG32(ds);
  836. SEG32(es);
  837. SEG32(fs);
  838. SEG32(gs);
  839. SEG32(ss);
  840. R32(ebx, bx);
  841. R32(ecx, cx);
  842. R32(edx, dx);
  843. R32(edi, di);
  844. R32(esi, si);
  845. R32(ebp, bp);
  846. R32(eax, ax);
  847. R32(eip, ip);
  848. R32(esp, sp);
  849. case offsetof(struct user32, regs.orig_eax):
  850. /*
  851. * A 32-bit debugger setting orig_eax means to restore
  852. * the state of the task restarting a 32-bit syscall.
  853. * Make sure we interpret the -ERESTART* codes correctly
  854. * in case the task is not actually still sitting at the
  855. * exit from a 32-bit syscall with TS_COMPAT still set.
  856. */
  857. regs->orig_ax = value;
  858. if (syscall_get_nr(child, regs) >= 0)
  859. task_thread_info(child)->status |= TS_COMPAT;
  860. break;
  861. case offsetof(struct user32, regs.eflags):
  862. return set_flags(child, value);
  863. case offsetof(struct user32, u_debugreg[0]) ...
  864. offsetof(struct user32, u_debugreg[7]):
  865. regno -= offsetof(struct user32, u_debugreg[0]);
  866. return ptrace_set_debugreg(child, regno / 4, value);
  867. default:
  868. if (regno > sizeof(struct user32) || (regno & 3))
  869. return -EIO;
  870. /*
  871. * Other dummy fields in the virtual user structure
  872. * are ignored
  873. */
  874. break;
  875. }
  876. return 0;
  877. }
  878. #undef R32
  879. #undef SEG32
  880. #define R32(l,q) \
  881. case offsetof(struct user32, regs.l): \
  882. *val = regs->q; break
  883. #define SEG32(rs) \
  884. case offsetof(struct user32, regs.rs): \
  885. *val = get_segment_reg(child, \
  886. offsetof(struct user_regs_struct, rs)); \
  887. break
  888. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  889. {
  890. struct pt_regs *regs = task_pt_regs(child);
  891. switch (regno) {
  892. SEG32(ds);
  893. SEG32(es);
  894. SEG32(fs);
  895. SEG32(gs);
  896. R32(cs, cs);
  897. R32(ss, ss);
  898. R32(ebx, bx);
  899. R32(ecx, cx);
  900. R32(edx, dx);
  901. R32(edi, di);
  902. R32(esi, si);
  903. R32(ebp, bp);
  904. R32(eax, ax);
  905. R32(orig_eax, orig_ax);
  906. R32(eip, ip);
  907. R32(esp, sp);
  908. case offsetof(struct user32, regs.eflags):
  909. *val = get_flags(child);
  910. break;
  911. case offsetof(struct user32, u_debugreg[0]) ...
  912. offsetof(struct user32, u_debugreg[7]):
  913. regno -= offsetof(struct user32, u_debugreg[0]);
  914. *val = ptrace_get_debugreg(child, regno / 4);
  915. break;
  916. default:
  917. if (regno > sizeof(struct user32) || (regno & 3))
  918. return -EIO;
  919. /*
  920. * Other dummy fields in the virtual user structure
  921. * are ignored
  922. */
  923. *val = 0;
  924. break;
  925. }
  926. return 0;
  927. }
  928. #undef R32
  929. #undef SEG32
  930. static int genregs32_get(struct task_struct *target,
  931. const struct user_regset *regset,
  932. unsigned int pos, unsigned int count,
  933. void *kbuf, void __user *ubuf)
  934. {
  935. if (kbuf) {
  936. compat_ulong_t *k = kbuf;
  937. while (count >= sizeof(*k)) {
  938. getreg32(target, pos, k++);
  939. count -= sizeof(*k);
  940. pos += sizeof(*k);
  941. }
  942. } else {
  943. compat_ulong_t __user *u = ubuf;
  944. while (count >= sizeof(*u)) {
  945. compat_ulong_t word;
  946. getreg32(target, pos, &word);
  947. if (__put_user(word, u++))
  948. return -EFAULT;
  949. count -= sizeof(*u);
  950. pos += sizeof(*u);
  951. }
  952. }
  953. return 0;
  954. }
  955. static int genregs32_set(struct task_struct *target,
  956. const struct user_regset *regset,
  957. unsigned int pos, unsigned int count,
  958. const void *kbuf, const void __user *ubuf)
  959. {
  960. int ret = 0;
  961. if (kbuf) {
  962. const compat_ulong_t *k = kbuf;
  963. while (count >= sizeof(*k) && !ret) {
  964. ret = putreg32(target, pos, *k++);
  965. count -= sizeof(*k);
  966. pos += sizeof(*k);
  967. }
  968. } else {
  969. const compat_ulong_t __user *u = ubuf;
  970. while (count >= sizeof(*u) && !ret) {
  971. compat_ulong_t word;
  972. ret = __get_user(word, u++);
  973. if (ret)
  974. break;
  975. ret = putreg32(target, pos, word);
  976. count -= sizeof(*u);
  977. pos += sizeof(*u);
  978. }
  979. }
  980. return ret;
  981. }
  982. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  983. compat_ulong_t caddr, compat_ulong_t cdata)
  984. {
  985. unsigned long addr = caddr;
  986. unsigned long data = cdata;
  987. void __user *datap = compat_ptr(data);
  988. int ret;
  989. __u32 val;
  990. switch (request) {
  991. case PTRACE_PEEKUSR:
  992. ret = getreg32(child, addr, &val);
  993. if (ret == 0)
  994. ret = put_user(val, (__u32 __user *)datap);
  995. break;
  996. case PTRACE_POKEUSR:
  997. ret = putreg32(child, addr, data);
  998. break;
  999. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1000. return copy_regset_to_user(child, &user_x86_32_view,
  1001. REGSET_GENERAL,
  1002. 0, sizeof(struct user_regs_struct32),
  1003. datap);
  1004. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1005. return copy_regset_from_user(child, &user_x86_32_view,
  1006. REGSET_GENERAL, 0,
  1007. sizeof(struct user_regs_struct32),
  1008. datap);
  1009. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1010. return copy_regset_to_user(child, &user_x86_32_view,
  1011. REGSET_FP, 0,
  1012. sizeof(struct user_i387_ia32_struct),
  1013. datap);
  1014. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1015. return copy_regset_from_user(
  1016. child, &user_x86_32_view, REGSET_FP,
  1017. 0, sizeof(struct user_i387_ia32_struct), datap);
  1018. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1019. return copy_regset_to_user(child, &user_x86_32_view,
  1020. REGSET_XFP, 0,
  1021. sizeof(struct user32_fxsr_struct),
  1022. datap);
  1023. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1024. return copy_regset_from_user(child, &user_x86_32_view,
  1025. REGSET_XFP, 0,
  1026. sizeof(struct user32_fxsr_struct),
  1027. datap);
  1028. case PTRACE_GET_THREAD_AREA:
  1029. case PTRACE_SET_THREAD_AREA:
  1030. return arch_ptrace(child, request, addr, data);
  1031. default:
  1032. return compat_ptrace_request(child, request, addr, data);
  1033. }
  1034. return ret;
  1035. }
  1036. #endif /* CONFIG_IA32_EMULATION */
  1037. #ifdef CONFIG_X86_64
  1038. static struct user_regset x86_64_regsets[] __read_mostly = {
  1039. [REGSET_GENERAL] = {
  1040. .core_note_type = NT_PRSTATUS,
  1041. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1042. .size = sizeof(long), .align = sizeof(long),
  1043. .get = genregs_get, .set = genregs_set
  1044. },
  1045. [REGSET_FP] = {
  1046. .core_note_type = NT_PRFPREG,
  1047. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1048. .size = sizeof(long), .align = sizeof(long),
  1049. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1050. },
  1051. [REGSET_XSTATE] = {
  1052. .core_note_type = NT_X86_XSTATE,
  1053. .size = sizeof(u64), .align = sizeof(u64),
  1054. .active = xstateregs_active, .get = xstateregs_get,
  1055. .set = xstateregs_set
  1056. },
  1057. [REGSET_IOPERM64] = {
  1058. .core_note_type = NT_386_IOPERM,
  1059. .n = IO_BITMAP_LONGS,
  1060. .size = sizeof(long), .align = sizeof(long),
  1061. .active = ioperm_active, .get = ioperm_get
  1062. },
  1063. };
  1064. static const struct user_regset_view user_x86_64_view = {
  1065. .name = "x86_64", .e_machine = EM_X86_64,
  1066. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1067. };
  1068. #else /* CONFIG_X86_32 */
  1069. #define user_regs_struct32 user_regs_struct
  1070. #define genregs32_get genregs_get
  1071. #define genregs32_set genregs_set
  1072. #define user_i387_ia32_struct user_i387_struct
  1073. #define user32_fxsr_struct user_fxsr_struct
  1074. #endif /* CONFIG_X86_64 */
  1075. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1076. static struct user_regset x86_32_regsets[] __read_mostly = {
  1077. [REGSET_GENERAL] = {
  1078. .core_note_type = NT_PRSTATUS,
  1079. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1080. .size = sizeof(u32), .align = sizeof(u32),
  1081. .get = genregs32_get, .set = genregs32_set
  1082. },
  1083. [REGSET_FP] = {
  1084. .core_note_type = NT_PRFPREG,
  1085. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1086. .size = sizeof(u32), .align = sizeof(u32),
  1087. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1088. },
  1089. [REGSET_XFP] = {
  1090. .core_note_type = NT_PRXFPREG,
  1091. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1092. .size = sizeof(u32), .align = sizeof(u32),
  1093. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1094. },
  1095. [REGSET_XSTATE] = {
  1096. .core_note_type = NT_X86_XSTATE,
  1097. .size = sizeof(u64), .align = sizeof(u64),
  1098. .active = xstateregs_active, .get = xstateregs_get,
  1099. .set = xstateregs_set
  1100. },
  1101. [REGSET_TLS] = {
  1102. .core_note_type = NT_386_TLS,
  1103. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1104. .size = sizeof(struct user_desc),
  1105. .align = sizeof(struct user_desc),
  1106. .active = regset_tls_active,
  1107. .get = regset_tls_get, .set = regset_tls_set
  1108. },
  1109. [REGSET_IOPERM32] = {
  1110. .core_note_type = NT_386_IOPERM,
  1111. .n = IO_BITMAP_BYTES / sizeof(u32),
  1112. .size = sizeof(u32), .align = sizeof(u32),
  1113. .active = ioperm_active, .get = ioperm_get
  1114. },
  1115. };
  1116. static const struct user_regset_view user_x86_32_view = {
  1117. .name = "i386", .e_machine = EM_386,
  1118. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1119. };
  1120. #endif
  1121. /*
  1122. * This represents bytes 464..511 in the memory layout exported through
  1123. * the REGSET_XSTATE interface.
  1124. */
  1125. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1126. void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1127. {
  1128. #ifdef CONFIG_X86_64
  1129. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1130. #endif
  1131. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1132. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1133. #endif
  1134. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1135. }
  1136. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1137. {
  1138. #ifdef CONFIG_IA32_EMULATION
  1139. if (test_tsk_thread_flag(task, TIF_IA32))
  1140. #endif
  1141. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1142. return &user_x86_32_view;
  1143. #endif
  1144. #ifdef CONFIG_X86_64
  1145. return &user_x86_64_view;
  1146. #endif
  1147. }
  1148. static void fill_sigtrap_info(struct task_struct *tsk,
  1149. struct pt_regs *regs,
  1150. int error_code, int si_code,
  1151. struct siginfo *info)
  1152. {
  1153. tsk->thread.trap_no = 1;
  1154. tsk->thread.error_code = error_code;
  1155. memset(info, 0, sizeof(*info));
  1156. info->si_signo = SIGTRAP;
  1157. info->si_code = si_code;
  1158. info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
  1159. }
  1160. void user_single_step_siginfo(struct task_struct *tsk,
  1161. struct pt_regs *regs,
  1162. struct siginfo *info)
  1163. {
  1164. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1165. }
  1166. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1167. int error_code, int si_code)
  1168. {
  1169. struct siginfo info;
  1170. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1171. /* Send us the fake SIGTRAP */
  1172. force_sig_info(SIGTRAP, &info, tsk);
  1173. }
  1174. #ifdef CONFIG_X86_32
  1175. # define IS_IA32 1
  1176. #elif defined CONFIG_IA32_EMULATION
  1177. # define IS_IA32 is_compat_task()
  1178. #else
  1179. # define IS_IA32 0
  1180. #endif
  1181. /*
  1182. * We must return the syscall number to actually look up in the table.
  1183. * This can be -1L to skip running any syscall at all.
  1184. */
  1185. asmregparm long syscall_trace_enter(struct pt_regs *regs)
  1186. {
  1187. long ret = 0;
  1188. /*
  1189. * If we stepped into a sysenter/syscall insn, it trapped in
  1190. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1191. * If user-mode had set TF itself, then it's still clear from
  1192. * do_debug() and we need to set it again to restore the user
  1193. * state. If we entered on the slow path, TF was already set.
  1194. */
  1195. if (test_thread_flag(TIF_SINGLESTEP))
  1196. regs->flags |= X86_EFLAGS_TF;
  1197. /* do the secure computing check first */
  1198. secure_computing(regs->orig_ax);
  1199. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1200. ret = -1L;
  1201. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1202. tracehook_report_syscall_entry(regs))
  1203. ret = -1L;
  1204. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1205. trace_sys_enter(regs, regs->orig_ax);
  1206. if (unlikely(current->audit_context)) {
  1207. if (IS_IA32)
  1208. audit_syscall_entry(AUDIT_ARCH_I386,
  1209. regs->orig_ax,
  1210. regs->bx, regs->cx,
  1211. regs->dx, regs->si);
  1212. #ifdef CONFIG_X86_64
  1213. else
  1214. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1215. regs->orig_ax,
  1216. regs->di, regs->si,
  1217. regs->dx, regs->r10);
  1218. #endif
  1219. }
  1220. return ret ?: regs->orig_ax;
  1221. }
  1222. asmregparm void syscall_trace_leave(struct pt_regs *regs)
  1223. {
  1224. bool step;
  1225. if (unlikely(current->audit_context))
  1226. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1227. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1228. trace_sys_exit(regs, regs->ax);
  1229. /*
  1230. * If TIF_SYSCALL_EMU is set, we only get here because of
  1231. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1232. * We already reported this syscall instruction in
  1233. * syscall_trace_enter().
  1234. */
  1235. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  1236. !test_thread_flag(TIF_SYSCALL_EMU);
  1237. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1238. tracehook_report_syscall_exit(regs, step);
  1239. }