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