ptrace.c 37 KB

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