ptrace.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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 <asm/uaccess.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/system.h>
  26. #include <asm/processor.h>
  27. #include <asm/i387.h>
  28. #include <asm/debugreg.h>
  29. #include <asm/ldt.h>
  30. #include <asm/desc.h>
  31. #include <asm/prctl.h>
  32. #include <asm/proto.h>
  33. #include <asm/ds.h>
  34. #include "tls.h"
  35. enum x86_regset {
  36. REGSET_GENERAL,
  37. REGSET_FP,
  38. REGSET_XFP,
  39. REGSET_TLS,
  40. };
  41. /*
  42. * does not yet catch signals sent when the child dies.
  43. * in exit.c or in signal.c.
  44. */
  45. /*
  46. * Determines which flags the user has access to [1 = access, 0 = no access].
  47. */
  48. #define FLAG_MASK_32 ((unsigned long) \
  49. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  50. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  51. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  52. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  53. X86_EFLAGS_RF | X86_EFLAGS_AC))
  54. /*
  55. * Determines whether a value may be installed in a segment register.
  56. */
  57. static inline bool invalid_selector(u16 value)
  58. {
  59. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  60. }
  61. #ifdef CONFIG_X86_32
  62. #define FLAG_MASK FLAG_MASK_32
  63. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  64. {
  65. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  66. regno >>= 2;
  67. if (regno > FS)
  68. --regno;
  69. return &regs->bx + regno;
  70. }
  71. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  72. {
  73. /*
  74. * Returning the value truncates it to 16 bits.
  75. */
  76. unsigned int retval;
  77. if (offset != offsetof(struct user_regs_struct, gs))
  78. retval = *pt_regs_access(task_pt_regs(task), offset);
  79. else {
  80. retval = task->thread.gs;
  81. if (task == current)
  82. savesegment(gs, retval);
  83. }
  84. return retval;
  85. }
  86. static int set_segment_reg(struct task_struct *task,
  87. unsigned long offset, u16 value)
  88. {
  89. /*
  90. * The value argument was already truncated to 16 bits.
  91. */
  92. if (invalid_selector(value))
  93. return -EIO;
  94. /*
  95. * For %cs and %ss we cannot permit a null selector.
  96. * We can permit a bogus selector as long as it has USER_RPL.
  97. * Null selectors are fine for other segment registers, but
  98. * we will never get back to user mode with invalid %cs or %ss
  99. * and will take the trap in iret instead. Much code relies
  100. * on user_mode() to distinguish a user trap frame (which can
  101. * safely use invalid selectors) from a kernel trap frame.
  102. */
  103. switch (offset) {
  104. case offsetof(struct user_regs_struct, cs):
  105. case offsetof(struct user_regs_struct, ss):
  106. if (unlikely(value == 0))
  107. return -EIO;
  108. default:
  109. *pt_regs_access(task_pt_regs(task), offset) = value;
  110. break;
  111. case offsetof(struct user_regs_struct, gs):
  112. task->thread.gs = value;
  113. if (task == current)
  114. /*
  115. * The user-mode %gs is not affected by
  116. * kernel entry, so we must update the CPU.
  117. */
  118. loadsegment(gs, 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_SIZE64 - 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. #ifdef X86_BTS
  502. static int ptrace_bts_get_size(struct task_struct *child)
  503. {
  504. if (!child->thread.ds_area_msr)
  505. return -ENXIO;
  506. return ds_get_bts_index((void *)child->thread.ds_area_msr);
  507. }
  508. static int ptrace_bts_read_record(struct task_struct *child,
  509. long index,
  510. struct bts_struct __user *out)
  511. {
  512. struct bts_struct ret;
  513. int retval;
  514. int bts_end;
  515. int bts_index;
  516. if (!child->thread.ds_area_msr)
  517. return -ENXIO;
  518. if (index < 0)
  519. return -EINVAL;
  520. bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
  521. if (bts_end <= index)
  522. return -EINVAL;
  523. /* translate the ptrace bts index into the ds bts index */
  524. bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
  525. bts_index -= (index + 1);
  526. if (bts_index < 0)
  527. bts_index += bts_end;
  528. retval = ds_read_bts((void *)child->thread.ds_area_msr,
  529. bts_index, &ret);
  530. if (retval < 0)
  531. return retval;
  532. if (copy_to_user(out, &ret, sizeof(ret)))
  533. return -EFAULT;
  534. return sizeof(ret);
  535. }
  536. static int ptrace_bts_clear(struct task_struct *child)
  537. {
  538. if (!child->thread.ds_area_msr)
  539. return -ENXIO;
  540. return ds_clear((void *)child->thread.ds_area_msr);
  541. }
  542. static int ptrace_bts_drain(struct task_struct *child,
  543. long size,
  544. struct bts_struct __user *out)
  545. {
  546. int end, i;
  547. void *ds = (void *)child->thread.ds_area_msr;
  548. if (!ds)
  549. return -ENXIO;
  550. end = ds_get_bts_index(ds);
  551. if (end <= 0)
  552. return end;
  553. if (size < (end * sizeof(struct bts_struct)))
  554. return -EIO;
  555. for (i = 0; i < end; i++, out++) {
  556. struct bts_struct ret;
  557. int retval;
  558. retval = ds_read_bts(ds, i, &ret);
  559. if (retval < 0)
  560. return retval;
  561. if (copy_to_user(out, &ret, sizeof(ret)))
  562. return -EFAULT;
  563. }
  564. ds_clear(ds);
  565. return end;
  566. }
  567. static int ptrace_bts_config(struct task_struct *child,
  568. long cfg_size,
  569. const struct ptrace_bts_config __user *ucfg)
  570. {
  571. struct ptrace_bts_config cfg;
  572. int bts_size, ret = 0;
  573. void *ds;
  574. if (cfg_size < sizeof(cfg))
  575. return -EIO;
  576. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  577. return -EFAULT;
  578. if ((int)cfg.size < 0)
  579. return -EINVAL;
  580. bts_size = 0;
  581. ds = (void *)child->thread.ds_area_msr;
  582. if (ds) {
  583. bts_size = ds_get_bts_size(ds);
  584. if (bts_size < 0)
  585. return bts_size;
  586. }
  587. cfg.size = PAGE_ALIGN(cfg.size);
  588. if (bts_size != cfg.size) {
  589. ret = ptrace_bts_realloc(child, cfg.size,
  590. cfg.flags & PTRACE_BTS_O_CUT_SIZE);
  591. if (ret < 0)
  592. goto errout;
  593. ds = (void *)child->thread.ds_area_msr;
  594. }
  595. if (cfg.flags & PTRACE_BTS_O_SIGNAL)
  596. ret = ds_set_overflow(ds, DS_O_SIGNAL);
  597. else
  598. ret = ds_set_overflow(ds, DS_O_WRAP);
  599. if (ret < 0)
  600. goto errout;
  601. if (cfg.flags & PTRACE_BTS_O_TRACE)
  602. child->thread.debugctlmsr |= ds_debugctl_mask();
  603. else
  604. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  605. if (cfg.flags & PTRACE_BTS_O_SCHED)
  606. set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  607. else
  608. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  609. ret = sizeof(cfg);
  610. out:
  611. if (child->thread.debugctlmsr)
  612. set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  613. else
  614. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  615. return ret;
  616. errout:
  617. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  618. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  619. goto out;
  620. }
  621. static int ptrace_bts_status(struct task_struct *child,
  622. long cfg_size,
  623. struct ptrace_bts_config __user *ucfg)
  624. {
  625. void *ds = (void *)child->thread.ds_area_msr;
  626. struct ptrace_bts_config cfg;
  627. if (cfg_size < sizeof(cfg))
  628. return -EIO;
  629. memset(&cfg, 0, sizeof(cfg));
  630. if (ds) {
  631. cfg.size = ds_get_bts_size(ds);
  632. if (ds_get_overflow(ds) == DS_O_SIGNAL)
  633. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  634. if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
  635. child->thread.debugctlmsr & ds_debugctl_mask())
  636. cfg.flags |= PTRACE_BTS_O_TRACE;
  637. if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
  638. cfg.flags |= PTRACE_BTS_O_SCHED;
  639. }
  640. cfg.bts_size = sizeof(struct bts_struct);
  641. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  642. return -EFAULT;
  643. return sizeof(cfg);
  644. }
  645. static int ptrace_bts_write_record(struct task_struct *child,
  646. const struct bts_struct *in)
  647. {
  648. int retval;
  649. if (!child->thread.ds_area_msr)
  650. return -ENXIO;
  651. retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
  652. if (retval)
  653. return retval;
  654. return sizeof(*in);
  655. }
  656. static int ptrace_bts_realloc(struct task_struct *child,
  657. int size, int reduce_size)
  658. {
  659. unsigned long rlim, vm;
  660. int ret, old_size;
  661. if (size < 0)
  662. return -EINVAL;
  663. old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
  664. if (old_size < 0)
  665. return old_size;
  666. ret = ds_free((void **)&child->thread.ds_area_msr);
  667. if (ret < 0)
  668. goto out;
  669. size >>= PAGE_SHIFT;
  670. old_size >>= PAGE_SHIFT;
  671. current->mm->total_vm -= old_size;
  672. current->mm->locked_vm -= old_size;
  673. if (size == 0)
  674. goto out;
  675. rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  676. vm = current->mm->total_vm + size;
  677. if (rlim < vm) {
  678. ret = -ENOMEM;
  679. if (!reduce_size)
  680. goto out;
  681. size = rlim - current->mm->total_vm;
  682. if (size <= 0)
  683. goto out;
  684. }
  685. rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  686. vm = current->mm->locked_vm + size;
  687. if (rlim < vm) {
  688. ret = -ENOMEM;
  689. if (!reduce_size)
  690. goto out;
  691. size = rlim - current->mm->locked_vm;
  692. if (size <= 0)
  693. goto out;
  694. }
  695. ret = ds_allocate((void **)&child->thread.ds_area_msr,
  696. size << PAGE_SHIFT);
  697. if (ret < 0)
  698. goto out;
  699. current->mm->total_vm += size;
  700. current->mm->locked_vm += size;
  701. out:
  702. if (child->thread.ds_area_msr)
  703. set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  704. else
  705. clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  706. return ret;
  707. }
  708. void ptrace_bts_take_timestamp(struct task_struct *tsk,
  709. enum bts_qualifier qualifier)
  710. {
  711. struct bts_struct rec = {
  712. .qualifier = qualifier,
  713. .variant.jiffies = jiffies_64
  714. };
  715. ptrace_bts_write_record(tsk, &rec);
  716. }
  717. #endif /* X86_BTS */
  718. /*
  719. * Called by kernel/ptrace.c when detaching..
  720. *
  721. * Make sure the single step bit is not set.
  722. */
  723. void ptrace_disable(struct task_struct *child)
  724. {
  725. user_disable_single_step(child);
  726. #ifdef TIF_SYSCALL_EMU
  727. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  728. #endif
  729. if (child->thread.ds_area_msr) {
  730. #ifdef X86_BTS
  731. ptrace_bts_realloc(child, 0, 0);
  732. #endif
  733. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  734. if (!child->thread.debugctlmsr)
  735. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  736. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  737. }
  738. }
  739. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  740. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  741. #endif
  742. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  743. {
  744. int ret;
  745. unsigned long __user *datap = (unsigned long __user *)data;
  746. switch (request) {
  747. /* read the word at location addr in the USER area. */
  748. case PTRACE_PEEKUSR: {
  749. unsigned long tmp;
  750. ret = -EIO;
  751. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  752. addr >= sizeof(struct user))
  753. break;
  754. tmp = 0; /* Default return condition */
  755. if (addr < sizeof(struct user_regs_struct))
  756. tmp = getreg(child, addr);
  757. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  758. addr <= offsetof(struct user, u_debugreg[7])) {
  759. addr -= offsetof(struct user, u_debugreg[0]);
  760. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  761. }
  762. ret = put_user(tmp, datap);
  763. break;
  764. }
  765. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  766. ret = -EIO;
  767. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  768. addr >= sizeof(struct user))
  769. break;
  770. if (addr < sizeof(struct user_regs_struct))
  771. ret = putreg(child, addr, data);
  772. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  773. addr <= offsetof(struct user, u_debugreg[7])) {
  774. addr -= offsetof(struct user, u_debugreg[0]);
  775. ret = ptrace_set_debugreg(child,
  776. addr / sizeof(data), data);
  777. }
  778. break;
  779. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  780. return copy_regset_to_user(child,
  781. task_user_regset_view(current),
  782. REGSET_GENERAL,
  783. 0, sizeof(struct user_regs_struct),
  784. datap);
  785. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  786. return copy_regset_from_user(child,
  787. task_user_regset_view(current),
  788. REGSET_GENERAL,
  789. 0, sizeof(struct user_regs_struct),
  790. datap);
  791. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  792. return copy_regset_to_user(child,
  793. task_user_regset_view(current),
  794. REGSET_FP,
  795. 0, sizeof(struct user_i387_struct),
  796. datap);
  797. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  798. return copy_regset_from_user(child,
  799. task_user_regset_view(current),
  800. REGSET_FP,
  801. 0, sizeof(struct user_i387_struct),
  802. datap);
  803. #ifdef CONFIG_X86_32
  804. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  805. return copy_regset_to_user(child, &user_x86_32_view,
  806. REGSET_XFP,
  807. 0, sizeof(struct user_fxsr_struct),
  808. datap) ? -EIO : 0;
  809. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  810. return copy_regset_from_user(child, &user_x86_32_view,
  811. REGSET_XFP,
  812. 0, sizeof(struct user_fxsr_struct),
  813. datap) ? -EIO : 0;
  814. #endif
  815. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  816. case PTRACE_GET_THREAD_AREA:
  817. if (addr < 0)
  818. return -EIO;
  819. ret = do_get_thread_area(child, addr,
  820. (struct user_desc __user *) data);
  821. break;
  822. case PTRACE_SET_THREAD_AREA:
  823. if (addr < 0)
  824. return -EIO;
  825. ret = do_set_thread_area(child, addr,
  826. (struct user_desc __user *) data, 0);
  827. break;
  828. #endif
  829. #ifdef CONFIG_X86_64
  830. /* normal 64bit interface to access TLS data.
  831. Works just like arch_prctl, except that the arguments
  832. are reversed. */
  833. case PTRACE_ARCH_PRCTL:
  834. ret = do_arch_prctl(child, data, addr);
  835. break;
  836. #endif
  837. /*
  838. * These bits need more cooking - not enabled yet:
  839. */
  840. #ifdef X86_BTS
  841. case PTRACE_BTS_CONFIG:
  842. ret = ptrace_bts_config
  843. (child, data, (struct ptrace_bts_config __user *)addr);
  844. break;
  845. case PTRACE_BTS_STATUS:
  846. ret = ptrace_bts_status
  847. (child, data, (struct ptrace_bts_config __user *)addr);
  848. break;
  849. case PTRACE_BTS_SIZE:
  850. ret = ptrace_bts_get_size(child);
  851. break;
  852. case PTRACE_BTS_GET:
  853. ret = ptrace_bts_read_record
  854. (child, data, (struct bts_struct __user *) addr);
  855. break;
  856. case PTRACE_BTS_CLEAR:
  857. ret = ptrace_bts_clear(child);
  858. break;
  859. case PTRACE_BTS_DRAIN:
  860. ret = ptrace_bts_drain
  861. (child, data, (struct bts_struct __user *) addr);
  862. break;
  863. #endif
  864. default:
  865. ret = ptrace_request(child, request, addr, data);
  866. break;
  867. }
  868. return ret;
  869. }
  870. #ifdef CONFIG_IA32_EMULATION
  871. #include <linux/compat.h>
  872. #include <linux/syscalls.h>
  873. #include <asm/ia32.h>
  874. #include <asm/user32.h>
  875. #define R32(l,q) \
  876. case offsetof(struct user32, regs.l): \
  877. regs->q = value; break
  878. #define SEG32(rs) \
  879. case offsetof(struct user32, regs.rs): \
  880. return set_segment_reg(child, \
  881. offsetof(struct user_regs_struct, rs), \
  882. value); \
  883. break
  884. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  885. {
  886. struct pt_regs *regs = task_pt_regs(child);
  887. switch (regno) {
  888. SEG32(cs);
  889. SEG32(ds);
  890. SEG32(es);
  891. SEG32(fs);
  892. SEG32(gs);
  893. SEG32(ss);
  894. R32(ebx, bx);
  895. R32(ecx, cx);
  896. R32(edx, dx);
  897. R32(edi, di);
  898. R32(esi, si);
  899. R32(ebp, bp);
  900. R32(eax, ax);
  901. R32(eip, ip);
  902. R32(esp, sp);
  903. case offsetof(struct user32, regs.orig_eax):
  904. /*
  905. * Sign-extend the value so that orig_eax = -1
  906. * causes (long)orig_ax < 0 tests to fire correctly.
  907. */
  908. regs->orig_ax = (long) (s32) value;
  909. break;
  910. case offsetof(struct user32, regs.eflags):
  911. return set_flags(child, value);
  912. case offsetof(struct user32, u_debugreg[0]) ...
  913. offsetof(struct user32, u_debugreg[7]):
  914. regno -= offsetof(struct user32, u_debugreg[0]);
  915. return ptrace_set_debugreg(child, regno / 4, value);
  916. default:
  917. if (regno > sizeof(struct user32) || (regno & 3))
  918. return -EIO;
  919. /*
  920. * Other dummy fields in the virtual user structure
  921. * are ignored
  922. */
  923. break;
  924. }
  925. return 0;
  926. }
  927. #undef R32
  928. #undef SEG32
  929. #define R32(l,q) \
  930. case offsetof(struct user32, regs.l): \
  931. *val = regs->q; break
  932. #define SEG32(rs) \
  933. case offsetof(struct user32, regs.rs): \
  934. *val = get_segment_reg(child, \
  935. offsetof(struct user_regs_struct, rs)); \
  936. break
  937. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  938. {
  939. struct pt_regs *regs = task_pt_regs(child);
  940. switch (regno) {
  941. SEG32(ds);
  942. SEG32(es);
  943. SEG32(fs);
  944. SEG32(gs);
  945. R32(cs, cs);
  946. R32(ss, 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(orig_eax, orig_ax);
  955. R32(eip, ip);
  956. R32(esp, sp);
  957. case offsetof(struct user32, regs.eflags):
  958. *val = get_flags(child);
  959. break;
  960. case offsetof(struct user32, u_debugreg[0]) ...
  961. offsetof(struct user32, u_debugreg[7]):
  962. regno -= offsetof(struct user32, u_debugreg[0]);
  963. *val = ptrace_get_debugreg(child, regno / 4);
  964. break;
  965. default:
  966. if (regno > sizeof(struct user32) || (regno & 3))
  967. return -EIO;
  968. /*
  969. * Other dummy fields in the virtual user structure
  970. * are ignored
  971. */
  972. *val = 0;
  973. break;
  974. }
  975. return 0;
  976. }
  977. #undef R32
  978. #undef SEG32
  979. static int genregs32_get(struct task_struct *target,
  980. const struct user_regset *regset,
  981. unsigned int pos, unsigned int count,
  982. void *kbuf, void __user *ubuf)
  983. {
  984. if (kbuf) {
  985. compat_ulong_t *k = kbuf;
  986. while (count > 0) {
  987. getreg32(target, pos, k++);
  988. count -= sizeof(*k);
  989. pos += sizeof(*k);
  990. }
  991. } else {
  992. compat_ulong_t __user *u = ubuf;
  993. while (count > 0) {
  994. compat_ulong_t word;
  995. getreg32(target, pos, &word);
  996. if (__put_user(word, u++))
  997. return -EFAULT;
  998. count -= sizeof(*u);
  999. pos += sizeof(*u);
  1000. }
  1001. }
  1002. return 0;
  1003. }
  1004. static int genregs32_set(struct task_struct *target,
  1005. const struct user_regset *regset,
  1006. unsigned int pos, unsigned int count,
  1007. const void *kbuf, const void __user *ubuf)
  1008. {
  1009. int ret = 0;
  1010. if (kbuf) {
  1011. const compat_ulong_t *k = kbuf;
  1012. while (count > 0 && !ret) {
  1013. ret = putreg32(target, pos, *k++);
  1014. count -= sizeof(*k);
  1015. pos += sizeof(*k);
  1016. }
  1017. } else {
  1018. const compat_ulong_t __user *u = ubuf;
  1019. while (count > 0 && !ret) {
  1020. compat_ulong_t word;
  1021. ret = __get_user(word, u++);
  1022. if (ret)
  1023. break;
  1024. ret = putreg32(target, pos, word);
  1025. count -= sizeof(*u);
  1026. pos += sizeof(*u);
  1027. }
  1028. }
  1029. return ret;
  1030. }
  1031. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1032. compat_ulong_t caddr, compat_ulong_t cdata)
  1033. {
  1034. unsigned long addr = caddr;
  1035. unsigned long data = cdata;
  1036. void __user *datap = compat_ptr(data);
  1037. int ret;
  1038. __u32 val;
  1039. switch (request) {
  1040. case PTRACE_PEEKUSR:
  1041. ret = getreg32(child, addr, &val);
  1042. if (ret == 0)
  1043. ret = put_user(val, (__u32 __user *)datap);
  1044. break;
  1045. case PTRACE_POKEUSR:
  1046. ret = putreg32(child, addr, data);
  1047. break;
  1048. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1049. return copy_regset_to_user(child, &user_x86_32_view,
  1050. REGSET_GENERAL,
  1051. 0, sizeof(struct user_regs_struct32),
  1052. datap);
  1053. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1054. return copy_regset_from_user(child, &user_x86_32_view,
  1055. REGSET_GENERAL, 0,
  1056. sizeof(struct user_regs_struct32),
  1057. datap);
  1058. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1059. return copy_regset_to_user(child, &user_x86_32_view,
  1060. REGSET_FP, 0,
  1061. sizeof(struct user_i387_ia32_struct),
  1062. datap);
  1063. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1064. return copy_regset_from_user(
  1065. child, &user_x86_32_view, REGSET_FP,
  1066. 0, sizeof(struct user_i387_ia32_struct), datap);
  1067. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1068. return copy_regset_to_user(child, &user_x86_32_view,
  1069. REGSET_XFP, 0,
  1070. sizeof(struct user32_fxsr_struct),
  1071. datap);
  1072. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1073. return copy_regset_from_user(child, &user_x86_32_view,
  1074. REGSET_XFP, 0,
  1075. sizeof(struct user32_fxsr_struct),
  1076. datap);
  1077. case PTRACE_GET_THREAD_AREA:
  1078. case PTRACE_SET_THREAD_AREA:
  1079. return arch_ptrace(child, request, addr, data);
  1080. default:
  1081. return compat_ptrace_request(child, request, addr, data);
  1082. }
  1083. return ret;
  1084. }
  1085. #endif /* CONFIG_IA32_EMULATION */
  1086. #ifdef CONFIG_X86_64
  1087. static const struct user_regset x86_64_regsets[] = {
  1088. [REGSET_GENERAL] = {
  1089. .core_note_type = NT_PRSTATUS,
  1090. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1091. .size = sizeof(long), .align = sizeof(long),
  1092. .get = genregs_get, .set = genregs_set
  1093. },
  1094. [REGSET_FP] = {
  1095. .core_note_type = NT_PRFPREG,
  1096. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1097. .size = sizeof(long), .align = sizeof(long),
  1098. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1099. },
  1100. };
  1101. static const struct user_regset_view user_x86_64_view = {
  1102. .name = "x86_64", .e_machine = EM_X86_64,
  1103. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1104. };
  1105. #else /* CONFIG_X86_32 */
  1106. #define user_regs_struct32 user_regs_struct
  1107. #define genregs32_get genregs_get
  1108. #define genregs32_set genregs_set
  1109. #define user_i387_ia32_struct user_i387_struct
  1110. #define user32_fxsr_struct user_fxsr_struct
  1111. #endif /* CONFIG_X86_64 */
  1112. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1113. static const struct user_regset x86_32_regsets[] = {
  1114. [REGSET_GENERAL] = {
  1115. .core_note_type = NT_PRSTATUS,
  1116. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1117. .size = sizeof(u32), .align = sizeof(u32),
  1118. .get = genregs32_get, .set = genregs32_set
  1119. },
  1120. [REGSET_FP] = {
  1121. .core_note_type = NT_PRFPREG,
  1122. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1123. .size = sizeof(u32), .align = sizeof(u32),
  1124. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1125. },
  1126. [REGSET_XFP] = {
  1127. .core_note_type = NT_PRXFPREG,
  1128. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1129. .size = sizeof(u32), .align = sizeof(u32),
  1130. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1131. },
  1132. [REGSET_TLS] = {
  1133. .core_note_type = NT_386_TLS,
  1134. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1135. .size = sizeof(struct user_desc),
  1136. .align = sizeof(struct user_desc),
  1137. .active = regset_tls_active,
  1138. .get = regset_tls_get, .set = regset_tls_set
  1139. },
  1140. };
  1141. static const struct user_regset_view user_x86_32_view = {
  1142. .name = "i386", .e_machine = EM_386,
  1143. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1144. };
  1145. #endif
  1146. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1147. {
  1148. #ifdef CONFIG_IA32_EMULATION
  1149. if (test_tsk_thread_flag(task, TIF_IA32))
  1150. #endif
  1151. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1152. return &user_x86_32_view;
  1153. #endif
  1154. #ifdef CONFIG_X86_64
  1155. return &user_x86_64_view;
  1156. #endif
  1157. }
  1158. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1159. int error_code, int si_code)
  1160. {
  1161. struct siginfo info;
  1162. tsk->thread.trap_no = 1;
  1163. tsk->thread.error_code = error_code;
  1164. memset(&info, 0, sizeof(info));
  1165. info.si_signo = SIGTRAP;
  1166. info.si_code = si_code;
  1167. /* User-mode ip? */
  1168. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  1169. /* Send us the fake SIGTRAP */
  1170. force_sig_info(SIGTRAP, &info, tsk);
  1171. }
  1172. #ifdef CONFIG_X86_32
  1173. # define IS_IA32 1
  1174. #elif defined CONFIG_IA32_EMULATION
  1175. # define IS_IA32 test_thread_flag(TIF_IA32)
  1176. #else
  1177. # define IS_IA32 0
  1178. #endif
  1179. /*
  1180. * We must return the syscall number to actually look up in the table.
  1181. * This can be -1L to skip running any syscall at all.
  1182. */
  1183. asmregparm long syscall_trace_enter(struct pt_regs *regs)
  1184. {
  1185. long ret = 0;
  1186. /*
  1187. * If we stepped into a sysenter/syscall insn, it trapped in
  1188. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1189. * If user-mode had set TF itself, then it's still clear from
  1190. * do_debug() and we need to set it again to restore the user
  1191. * state. If we entered on the slow path, TF was already set.
  1192. */
  1193. if (test_thread_flag(TIF_SINGLESTEP))
  1194. regs->flags |= X86_EFLAGS_TF;
  1195. /* do the secure computing check first */
  1196. secure_computing(regs->orig_ax);
  1197. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1198. ret = -1L;
  1199. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1200. tracehook_report_syscall_entry(regs))
  1201. ret = -1L;
  1202. if (unlikely(current->audit_context)) {
  1203. if (IS_IA32)
  1204. audit_syscall_entry(AUDIT_ARCH_I386,
  1205. regs->orig_ax,
  1206. regs->bx, regs->cx,
  1207. regs->dx, regs->si);
  1208. #ifdef CONFIG_X86_64
  1209. else
  1210. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1211. regs->orig_ax,
  1212. regs->di, regs->si,
  1213. regs->dx, regs->r10);
  1214. #endif
  1215. }
  1216. return ret ?: regs->orig_ax;
  1217. }
  1218. asmregparm void syscall_trace_leave(struct pt_regs *regs)
  1219. {
  1220. if (unlikely(current->audit_context))
  1221. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1222. if (test_thread_flag(TIF_SYSCALL_TRACE))
  1223. tracehook_report_syscall_exit(regs, 0);
  1224. /*
  1225. * If TIF_SYSCALL_EMU is set, we only get here because of
  1226. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1227. * We already reported this syscall instruction in
  1228. * syscall_trace_enter(), so don't do any more now.
  1229. */
  1230. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1231. return;
  1232. /*
  1233. * If we are single-stepping, synthesize a trap to follow the
  1234. * system call instruction.
  1235. */
  1236. if (test_thread_flag(TIF_SINGLESTEP) &&
  1237. tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
  1238. send_sigtrap(current, regs, 0, TRAP_BRKPT);
  1239. }