ptrace.c 38 KB

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