ptrace.c 38 KB

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