ptrace.c 39 KB

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