ptrace.c 36 KB

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