ptrace.c 38 KB

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