ptrace.c 37 KB

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