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