ptrace.c 39 KB

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