ptrace.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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 < 0)
  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. long size,
  525. struct bts_struct __user *out)
  526. {
  527. int end, i;
  528. void *ds = (void *)child->thread.ds_area_msr;
  529. if (!ds)
  530. return -ENXIO;
  531. end = ds_get_bts_index(ds);
  532. if (end <= 0)
  533. return end;
  534. if (size < (end * sizeof(struct bts_struct)))
  535. return -EIO;
  536. for (i = 0; i < end; i++, out++) {
  537. struct bts_struct ret;
  538. int retval;
  539. retval = ds_read_bts(ds, i, &ret);
  540. if (retval < 0)
  541. return retval;
  542. if (copy_to_user(out, &ret, sizeof(ret)))
  543. return -EFAULT;
  544. }
  545. ds_clear(ds);
  546. return end;
  547. }
  548. static int ptrace_bts_realloc(struct task_struct *child,
  549. int size, int reduce_size)
  550. {
  551. unsigned long rlim, vm;
  552. int ret, old_size;
  553. if (size < 0)
  554. return -EINVAL;
  555. old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
  556. if (old_size < 0)
  557. return old_size;
  558. ret = ds_free((void **)&child->thread.ds_area_msr);
  559. if (ret < 0)
  560. goto out;
  561. size >>= PAGE_SHIFT;
  562. old_size >>= PAGE_SHIFT;
  563. current->mm->total_vm -= old_size;
  564. current->mm->locked_vm -= old_size;
  565. if (size == 0)
  566. goto out;
  567. rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  568. vm = current->mm->total_vm + size;
  569. if (rlim < vm) {
  570. ret = -ENOMEM;
  571. if (!reduce_size)
  572. goto out;
  573. size = rlim - current->mm->total_vm;
  574. if (size <= 0)
  575. goto out;
  576. }
  577. rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  578. vm = current->mm->locked_vm + size;
  579. if (rlim < vm) {
  580. ret = -ENOMEM;
  581. if (!reduce_size)
  582. goto out;
  583. size = rlim - current->mm->locked_vm;
  584. if (size <= 0)
  585. goto out;
  586. }
  587. ret = ds_allocate((void **)&child->thread.ds_area_msr,
  588. size << PAGE_SHIFT);
  589. if (ret < 0)
  590. goto out;
  591. current->mm->total_vm += size;
  592. current->mm->locked_vm += size;
  593. out:
  594. if (child->thread.ds_area_msr)
  595. set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  596. else
  597. clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  598. return ret;
  599. }
  600. static int ptrace_bts_config(struct task_struct *child,
  601. long cfg_size,
  602. const struct ptrace_bts_config __user *ucfg)
  603. {
  604. struct ptrace_bts_config cfg;
  605. int bts_size, ret = 0;
  606. void *ds;
  607. if (cfg_size < sizeof(cfg))
  608. return -EIO;
  609. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  610. return -EFAULT;
  611. if ((int)cfg.size < 0)
  612. return -EINVAL;
  613. bts_size = 0;
  614. ds = (void *)child->thread.ds_area_msr;
  615. if (ds) {
  616. bts_size = ds_get_bts_size(ds);
  617. if (bts_size < 0)
  618. return bts_size;
  619. }
  620. cfg.size = PAGE_ALIGN(cfg.size);
  621. if (bts_size != cfg.size) {
  622. ret = ptrace_bts_realloc(child, cfg.size,
  623. cfg.flags & PTRACE_BTS_O_CUT_SIZE);
  624. if (ret < 0)
  625. goto errout;
  626. ds = (void *)child->thread.ds_area_msr;
  627. }
  628. if (cfg.flags & PTRACE_BTS_O_SIGNAL)
  629. ret = ds_set_overflow(ds, DS_O_SIGNAL);
  630. else
  631. ret = ds_set_overflow(ds, DS_O_WRAP);
  632. if (ret < 0)
  633. goto errout;
  634. if (cfg.flags & PTRACE_BTS_O_TRACE)
  635. child->thread.debugctlmsr |= ds_debugctl_mask();
  636. else
  637. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  638. if (cfg.flags & PTRACE_BTS_O_SCHED)
  639. set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  640. else
  641. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  642. ret = sizeof(cfg);
  643. out:
  644. if (child->thread.debugctlmsr)
  645. set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  646. else
  647. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  648. return ret;
  649. errout:
  650. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  651. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  652. goto out;
  653. }
  654. static int ptrace_bts_status(struct task_struct *child,
  655. long cfg_size,
  656. struct ptrace_bts_config __user *ucfg)
  657. {
  658. void *ds = (void *)child->thread.ds_area_msr;
  659. struct ptrace_bts_config cfg;
  660. if (cfg_size < sizeof(cfg))
  661. return -EIO;
  662. memset(&cfg, 0, sizeof(cfg));
  663. if (ds) {
  664. cfg.size = ds_get_bts_size(ds);
  665. if (ds_get_overflow(ds) == DS_O_SIGNAL)
  666. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  667. if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
  668. child->thread.debugctlmsr & ds_debugctl_mask())
  669. cfg.flags |= PTRACE_BTS_O_TRACE;
  670. if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
  671. cfg.flags |= PTRACE_BTS_O_SCHED;
  672. }
  673. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  674. return -EFAULT;
  675. return sizeof(cfg);
  676. }
  677. void ptrace_bts_take_timestamp(struct task_struct *tsk,
  678. enum bts_qualifier qualifier)
  679. {
  680. struct bts_struct rec = {
  681. .qualifier = qualifier,
  682. .variant.jiffies = jiffies_64
  683. };
  684. ptrace_bts_write_record(tsk, &rec);
  685. }
  686. /*
  687. * Called by kernel/ptrace.c when detaching..
  688. *
  689. * Make sure the single step bit is not set.
  690. */
  691. void ptrace_disable(struct task_struct *child)
  692. {
  693. user_disable_single_step(child);
  694. #ifdef TIF_SYSCALL_EMU
  695. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  696. #endif
  697. if (child->thread.ds_area_msr) {
  698. ptrace_bts_realloc(child, 0, 0);
  699. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  700. if (!child->thread.debugctlmsr)
  701. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  702. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  703. }
  704. }
  705. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  706. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  707. #endif
  708. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  709. {
  710. int ret;
  711. unsigned long __user *datap = (unsigned long __user *)data;
  712. switch (request) {
  713. /* read the word at location addr in the USER area. */
  714. case PTRACE_PEEKUSR: {
  715. unsigned long tmp;
  716. ret = -EIO;
  717. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  718. addr >= sizeof(struct user))
  719. break;
  720. tmp = 0; /* Default return condition */
  721. if (addr < sizeof(struct user_regs_struct))
  722. tmp = getreg(child, addr);
  723. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  724. addr <= offsetof(struct user, u_debugreg[7])) {
  725. addr -= offsetof(struct user, u_debugreg[0]);
  726. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  727. }
  728. ret = put_user(tmp, datap);
  729. break;
  730. }
  731. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  732. ret = -EIO;
  733. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  734. addr >= sizeof(struct user))
  735. break;
  736. if (addr < sizeof(struct user_regs_struct))
  737. ret = putreg(child, addr, data);
  738. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  739. addr <= offsetof(struct user, u_debugreg[7])) {
  740. addr -= offsetof(struct user, u_debugreg[0]);
  741. ret = ptrace_set_debugreg(child,
  742. addr / sizeof(data), data);
  743. }
  744. break;
  745. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  746. return copy_regset_to_user(child,
  747. task_user_regset_view(current),
  748. REGSET_GENERAL,
  749. 0, sizeof(struct user_regs_struct),
  750. datap);
  751. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  752. return copy_regset_from_user(child,
  753. task_user_regset_view(current),
  754. REGSET_GENERAL,
  755. 0, sizeof(struct user_regs_struct),
  756. datap);
  757. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  758. return copy_regset_to_user(child,
  759. task_user_regset_view(current),
  760. REGSET_FP,
  761. 0, sizeof(struct user_i387_struct),
  762. datap);
  763. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  764. return copy_regset_from_user(child,
  765. task_user_regset_view(current),
  766. REGSET_FP,
  767. 0, sizeof(struct user_i387_struct),
  768. datap);
  769. #ifdef CONFIG_X86_32
  770. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  771. return copy_regset_to_user(child, &user_x86_32_view,
  772. REGSET_XFP,
  773. 0, sizeof(struct user_fxsr_struct),
  774. datap);
  775. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  776. return copy_regset_from_user(child, &user_x86_32_view,
  777. REGSET_XFP,
  778. 0, sizeof(struct user_fxsr_struct),
  779. datap);
  780. #endif
  781. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  782. case PTRACE_GET_THREAD_AREA:
  783. if (addr < 0)
  784. return -EIO;
  785. ret = do_get_thread_area(child, addr,
  786. (struct user_desc __user *) data);
  787. break;
  788. case PTRACE_SET_THREAD_AREA:
  789. if (addr < 0)
  790. return -EIO;
  791. ret = do_set_thread_area(child, addr,
  792. (struct user_desc __user *) data, 0);
  793. break;
  794. #endif
  795. #ifdef CONFIG_X86_64
  796. /* normal 64bit interface to access TLS data.
  797. Works just like arch_prctl, except that the arguments
  798. are reversed. */
  799. case PTRACE_ARCH_PRCTL:
  800. ret = do_arch_prctl(child, data, addr);
  801. break;
  802. #endif
  803. case PTRACE_BTS_CONFIG:
  804. ret = ptrace_bts_config
  805. (child, data, (struct ptrace_bts_config __user *)addr);
  806. break;
  807. case PTRACE_BTS_STATUS:
  808. ret = ptrace_bts_status
  809. (child, data, (struct ptrace_bts_config __user *)addr);
  810. break;
  811. case PTRACE_BTS_SIZE:
  812. ret = ptrace_bts_get_size(child);
  813. break;
  814. case PTRACE_BTS_GET:
  815. ret = ptrace_bts_read_record
  816. (child, data, (struct bts_struct __user *) addr);
  817. break;
  818. case PTRACE_BTS_CLEAR:
  819. ret = ptrace_bts_clear(child);
  820. break;
  821. case PTRACE_BTS_DRAIN:
  822. ret = ptrace_bts_drain
  823. (child, data, (struct bts_struct __user *) addr);
  824. break;
  825. default:
  826. ret = ptrace_request(child, request, addr, data);
  827. break;
  828. }
  829. return ret;
  830. }
  831. #ifdef CONFIG_IA32_EMULATION
  832. #include <linux/compat.h>
  833. #include <linux/syscalls.h>
  834. #include <asm/ia32.h>
  835. #include <asm/user32.h>
  836. #define R32(l,q) \
  837. case offsetof(struct user32, regs.l): \
  838. regs->q = value; break
  839. #define SEG32(rs) \
  840. case offsetof(struct user32, regs.rs): \
  841. return set_segment_reg(child, \
  842. offsetof(struct user_regs_struct, rs), \
  843. value); \
  844. break
  845. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  846. {
  847. struct pt_regs *regs = task_pt_regs(child);
  848. switch (regno) {
  849. SEG32(cs);
  850. SEG32(ds);
  851. SEG32(es);
  852. SEG32(fs);
  853. SEG32(gs);
  854. SEG32(ss);
  855. R32(ebx, bx);
  856. R32(ecx, cx);
  857. R32(edx, dx);
  858. R32(edi, di);
  859. R32(esi, si);
  860. R32(ebp, bp);
  861. R32(eax, ax);
  862. R32(orig_eax, orig_ax);
  863. R32(eip, ip);
  864. R32(esp, sp);
  865. case offsetof(struct user32, regs.eflags):
  866. return set_flags(child, value);
  867. case offsetof(struct user32, u_debugreg[0]) ...
  868. offsetof(struct user32, u_debugreg[7]):
  869. regno -= offsetof(struct user32, u_debugreg[0]);
  870. return ptrace_set_debugreg(child, regno / 4, value);
  871. default:
  872. if (regno > sizeof(struct user32) || (regno & 3))
  873. return -EIO;
  874. /*
  875. * Other dummy fields in the virtual user structure
  876. * are ignored
  877. */
  878. break;
  879. }
  880. return 0;
  881. }
  882. #undef R32
  883. #undef SEG32
  884. #define R32(l,q) \
  885. case offsetof(struct user32, regs.l): \
  886. *val = regs->q; break
  887. #define SEG32(rs) \
  888. case offsetof(struct user32, regs.rs): \
  889. *val = get_segment_reg(child, \
  890. offsetof(struct user_regs_struct, rs)); \
  891. break
  892. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  893. {
  894. struct pt_regs *regs = task_pt_regs(child);
  895. switch (regno) {
  896. SEG32(ds);
  897. SEG32(es);
  898. SEG32(fs);
  899. SEG32(gs);
  900. R32(cs, cs);
  901. R32(ss, ss);
  902. R32(ebx, bx);
  903. R32(ecx, cx);
  904. R32(edx, dx);
  905. R32(edi, di);
  906. R32(esi, si);
  907. R32(ebp, bp);
  908. R32(eax, ax);
  909. R32(orig_eax, orig_ax);
  910. R32(eip, ip);
  911. R32(esp, sp);
  912. case offsetof(struct user32, regs.eflags):
  913. *val = get_flags(child);
  914. break;
  915. case offsetof(struct user32, u_debugreg[0]) ...
  916. offsetof(struct user32, u_debugreg[7]):
  917. regno -= offsetof(struct user32, u_debugreg[0]);
  918. *val = ptrace_get_debugreg(child, regno / 4);
  919. break;
  920. default:
  921. if (regno > sizeof(struct user32) || (regno & 3))
  922. return -EIO;
  923. /*
  924. * Other dummy fields in the virtual user structure
  925. * are ignored
  926. */
  927. *val = 0;
  928. break;
  929. }
  930. return 0;
  931. }
  932. #undef R32
  933. #undef SEG32
  934. static int genregs32_get(struct task_struct *target,
  935. const struct user_regset *regset,
  936. unsigned int pos, unsigned int count,
  937. void *kbuf, void __user *ubuf)
  938. {
  939. if (kbuf) {
  940. compat_ulong_t *k = kbuf;
  941. while (count > 0) {
  942. getreg32(target, pos, k++);
  943. count -= sizeof(*k);
  944. pos += sizeof(*k);
  945. }
  946. } else {
  947. compat_ulong_t __user *u = ubuf;
  948. while (count > 0) {
  949. compat_ulong_t word;
  950. getreg32(target, pos, &word);
  951. if (__put_user(word, u++))
  952. return -EFAULT;
  953. count -= sizeof(*u);
  954. pos += sizeof(*u);
  955. }
  956. }
  957. return 0;
  958. }
  959. static int genregs32_set(struct task_struct *target,
  960. const struct user_regset *regset,
  961. unsigned int pos, unsigned int count,
  962. const void *kbuf, const void __user *ubuf)
  963. {
  964. int ret = 0;
  965. if (kbuf) {
  966. const compat_ulong_t *k = kbuf;
  967. while (count > 0 && !ret) {
  968. ret = putreg(target, pos, *k++);
  969. count -= sizeof(*k);
  970. pos += sizeof(*k);
  971. }
  972. } else {
  973. const compat_ulong_t __user *u = ubuf;
  974. while (count > 0 && !ret) {
  975. compat_ulong_t word;
  976. ret = __get_user(word, u++);
  977. if (ret)
  978. break;
  979. ret = putreg(target, pos, word);
  980. count -= sizeof(*u);
  981. pos += sizeof(*u);
  982. }
  983. }
  984. return ret;
  985. }
  986. static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
  987. {
  988. siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
  989. compat_siginfo_t __user *si32 = compat_ptr(data);
  990. siginfo_t ssi;
  991. int ret;
  992. if (request == PTRACE_SETSIGINFO) {
  993. memset(&ssi, 0, sizeof(siginfo_t));
  994. ret = copy_siginfo_from_user32(&ssi, si32);
  995. if (ret)
  996. return ret;
  997. if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
  998. return -EFAULT;
  999. }
  1000. ret = sys_ptrace(request, pid, addr, (unsigned long)si);
  1001. if (ret)
  1002. return ret;
  1003. if (request == PTRACE_GETSIGINFO) {
  1004. if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
  1005. return -EFAULT;
  1006. ret = copy_siginfo_to_user32(si32, &ssi);
  1007. }
  1008. return ret;
  1009. }
  1010. asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
  1011. {
  1012. struct task_struct *child;
  1013. struct pt_regs *childregs;
  1014. void __user *datap = compat_ptr(data);
  1015. int ret;
  1016. __u32 val;
  1017. switch (request) {
  1018. case PTRACE_TRACEME:
  1019. case PTRACE_ATTACH:
  1020. case PTRACE_KILL:
  1021. case PTRACE_CONT:
  1022. case PTRACE_SINGLESTEP:
  1023. case PTRACE_SINGLEBLOCK:
  1024. case PTRACE_DETACH:
  1025. case PTRACE_SYSCALL:
  1026. case PTRACE_OLDSETOPTIONS:
  1027. case PTRACE_SETOPTIONS:
  1028. case PTRACE_SET_THREAD_AREA:
  1029. case PTRACE_GET_THREAD_AREA:
  1030. case PTRACE_BTS_CONFIG:
  1031. case PTRACE_BTS_STATUS:
  1032. case PTRACE_BTS_SIZE:
  1033. case PTRACE_BTS_GET:
  1034. case PTRACE_BTS_CLEAR:
  1035. case PTRACE_BTS_DRAIN:
  1036. return sys_ptrace(request, pid, addr, data);
  1037. default:
  1038. return -EINVAL;
  1039. case PTRACE_PEEKTEXT:
  1040. case PTRACE_PEEKDATA:
  1041. case PTRACE_POKEDATA:
  1042. case PTRACE_POKETEXT:
  1043. case PTRACE_POKEUSR:
  1044. case PTRACE_PEEKUSR:
  1045. case PTRACE_GETREGS:
  1046. case PTRACE_SETREGS:
  1047. case PTRACE_SETFPREGS:
  1048. case PTRACE_GETFPREGS:
  1049. case PTRACE_SETFPXREGS:
  1050. case PTRACE_GETFPXREGS:
  1051. case PTRACE_GETEVENTMSG:
  1052. break;
  1053. case PTRACE_SETSIGINFO:
  1054. case PTRACE_GETSIGINFO:
  1055. return ptrace32_siginfo(request, pid, addr, data);
  1056. }
  1057. child = ptrace_get_task_struct(pid);
  1058. if (IS_ERR(child))
  1059. return PTR_ERR(child);
  1060. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  1061. if (ret < 0)
  1062. goto out;
  1063. childregs = task_pt_regs(child);
  1064. switch (request) {
  1065. case PTRACE_PEEKUSR:
  1066. ret = getreg32(child, addr, &val);
  1067. if (ret == 0)
  1068. ret = put_user(val, (__u32 __user *)datap);
  1069. break;
  1070. case PTRACE_POKEUSR:
  1071. ret = putreg32(child, addr, data);
  1072. break;
  1073. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1074. return copy_regset_to_user(child, &user_x86_32_view,
  1075. REGSET_GENERAL,
  1076. 0, sizeof(struct user_regs_struct32),
  1077. datap);
  1078. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1079. return copy_regset_from_user(child, &user_x86_32_view,
  1080. REGSET_GENERAL, 0,
  1081. sizeof(struct user_regs_struct32),
  1082. datap);
  1083. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1084. return copy_regset_to_user(child, &user_x86_32_view,
  1085. REGSET_FP, 0,
  1086. sizeof(struct user_i387_ia32_struct),
  1087. datap);
  1088. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1089. return copy_regset_from_user(
  1090. child, &user_x86_32_view, REGSET_FP,
  1091. 0, sizeof(struct user_i387_ia32_struct), datap);
  1092. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1093. return copy_regset_to_user(child, &user_x86_32_view,
  1094. REGSET_XFP, 0,
  1095. sizeof(struct user32_fxsr_struct),
  1096. datap);
  1097. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1098. return copy_regset_from_user(child, &user_x86_32_view,
  1099. REGSET_XFP, 0,
  1100. sizeof(struct user32_fxsr_struct),
  1101. datap);
  1102. default:
  1103. return compat_ptrace_request(child, request, addr, data);
  1104. }
  1105. out:
  1106. put_task_struct(child);
  1107. return ret;
  1108. }
  1109. #endif /* CONFIG_IA32_EMULATION */
  1110. #ifdef CONFIG_X86_64
  1111. static const struct user_regset x86_64_regsets[] = {
  1112. [REGSET_GENERAL] = {
  1113. .core_note_type = NT_PRSTATUS,
  1114. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1115. .size = sizeof(long), .align = sizeof(long),
  1116. .get = genregs_get, .set = genregs_set
  1117. },
  1118. [REGSET_FP] = {
  1119. .core_note_type = NT_PRFPREG,
  1120. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1121. .size = sizeof(long), .align = sizeof(long),
  1122. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1123. },
  1124. };
  1125. static const struct user_regset_view user_x86_64_view = {
  1126. .name = "x86_64", .e_machine = EM_X86_64,
  1127. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1128. };
  1129. #else /* CONFIG_X86_32 */
  1130. #define user_regs_struct32 user_regs_struct
  1131. #define genregs32_get genregs_get
  1132. #define genregs32_set genregs_set
  1133. #endif /* CONFIG_X86_64 */
  1134. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1135. static const struct user_regset x86_32_regsets[] = {
  1136. [REGSET_GENERAL] = {
  1137. .core_note_type = NT_PRSTATUS,
  1138. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1139. .size = sizeof(u32), .align = sizeof(u32),
  1140. .get = genregs32_get, .set = genregs32_set
  1141. },
  1142. [REGSET_FP] = {
  1143. .core_note_type = NT_PRFPREG,
  1144. .n = sizeof(struct user_i387_struct) / sizeof(u32),
  1145. .size = sizeof(u32), .align = sizeof(u32),
  1146. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1147. },
  1148. [REGSET_XFP] = {
  1149. .core_note_type = NT_PRXFPREG,
  1150. .n = sizeof(struct user_i387_struct) / sizeof(u32),
  1151. .size = sizeof(u32), .align = sizeof(u32),
  1152. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1153. },
  1154. [REGSET_TLS] = {
  1155. .core_note_type = NT_386_TLS,
  1156. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1157. .size = sizeof(struct user_desc),
  1158. .align = sizeof(struct user_desc),
  1159. .active = regset_tls_active,
  1160. .get = regset_tls_get, .set = regset_tls_set
  1161. },
  1162. };
  1163. static const struct user_regset_view user_x86_32_view = {
  1164. .name = "i386", .e_machine = EM_386,
  1165. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1166. };
  1167. #endif
  1168. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1169. {
  1170. #ifdef CONFIG_IA32_EMULATION
  1171. if (test_tsk_thread_flag(task, TIF_IA32))
  1172. #endif
  1173. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1174. return &user_x86_32_view;
  1175. #endif
  1176. #ifdef CONFIG_X86_64
  1177. return &user_x86_64_view;
  1178. #endif
  1179. }
  1180. #ifdef CONFIG_X86_32
  1181. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  1182. {
  1183. struct siginfo info;
  1184. tsk->thread.trap_no = 1;
  1185. tsk->thread.error_code = error_code;
  1186. memset(&info, 0, sizeof(info));
  1187. info.si_signo = SIGTRAP;
  1188. info.si_code = TRAP_BRKPT;
  1189. /* User-mode ip? */
  1190. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  1191. /* Send us the fake SIGTRAP */
  1192. force_sig_info(SIGTRAP, &info, tsk);
  1193. }
  1194. /* notification of system call entry/exit
  1195. * - triggered by current->work.syscall_trace
  1196. */
  1197. __attribute__((regparm(3)))
  1198. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  1199. {
  1200. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  1201. /*
  1202. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  1203. * interception
  1204. */
  1205. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  1206. int ret = 0;
  1207. /* do the secure computing check first */
  1208. if (!entryexit)
  1209. secure_computing(regs->orig_ax);
  1210. if (unlikely(current->audit_context)) {
  1211. if (entryexit)
  1212. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  1213. regs->ax);
  1214. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  1215. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  1216. * not used, entry.S will call us only on syscall exit, not
  1217. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  1218. * calling send_sigtrap() on syscall entry.
  1219. *
  1220. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  1221. * is_singlestep is false, despite his name, so we will still do
  1222. * the correct thing.
  1223. */
  1224. else if (is_singlestep)
  1225. goto out;
  1226. }
  1227. if (!(current->ptrace & PT_PTRACED))
  1228. goto out;
  1229. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  1230. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  1231. * here. We have to check this and return */
  1232. if (is_sysemu && entryexit)
  1233. return 0;
  1234. /* Fake a debug trap */
  1235. if (is_singlestep)
  1236. send_sigtrap(current, regs, 0);
  1237. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  1238. goto out;
  1239. /* the 0x80 provides a way for the tracing parent to distinguish
  1240. between a syscall stop and SIGTRAP delivery */
  1241. /* Note that the debugger could change the result of test_thread_flag!*/
  1242. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  1243. /*
  1244. * this isn't the same as continuing with a signal, but it will do
  1245. * for normal use. strace only continues with a signal if the
  1246. * stopping signal is not SIGTRAP. -brl
  1247. */
  1248. if (current->exit_code) {
  1249. send_sig(current->exit_code, current, 1);
  1250. current->exit_code = 0;
  1251. }
  1252. ret = is_sysemu;
  1253. out:
  1254. if (unlikely(current->audit_context) && !entryexit)
  1255. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  1256. regs->bx, regs->cx, regs->dx, regs->si);
  1257. if (ret == 0)
  1258. return 0;
  1259. regs->orig_ax = -1; /* force skip of syscall restarting */
  1260. if (unlikely(current->audit_context))
  1261. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1262. return 1;
  1263. }
  1264. #else /* CONFIG_X86_64 */
  1265. static void syscall_trace(struct pt_regs *regs)
  1266. {
  1267. #if 0
  1268. printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  1269. current->comm,
  1270. regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
  1271. current_thread_info()->flags, current->ptrace);
  1272. #endif
  1273. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  1274. ? 0x80 : 0));
  1275. /*
  1276. * this isn't the same as continuing with a signal, but it will do
  1277. * for normal use. strace only continues with a signal if the
  1278. * stopping signal is not SIGTRAP. -brl
  1279. */
  1280. if (current->exit_code) {
  1281. send_sig(current->exit_code, current, 1);
  1282. current->exit_code = 0;
  1283. }
  1284. }
  1285. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  1286. {
  1287. /* do the secure computing check first */
  1288. secure_computing(regs->orig_ax);
  1289. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1290. && (current->ptrace & PT_PTRACED))
  1291. syscall_trace(regs);
  1292. if (unlikely(current->audit_context)) {
  1293. if (test_thread_flag(TIF_IA32)) {
  1294. audit_syscall_entry(AUDIT_ARCH_I386,
  1295. regs->orig_ax,
  1296. regs->bx, regs->cx,
  1297. regs->dx, regs->si);
  1298. } else {
  1299. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1300. regs->orig_ax,
  1301. regs->di, regs->si,
  1302. regs->dx, regs->r10);
  1303. }
  1304. }
  1305. }
  1306. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  1307. {
  1308. if (unlikely(current->audit_context))
  1309. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1310. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  1311. || test_thread_flag(TIF_SINGLESTEP))
  1312. && (current->ptrace & PT_PTRACED))
  1313. syscall_trace(regs);
  1314. }
  1315. #endif /* CONFIG_X86_32 */