ptrace.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. *
  6. * BTS tracing
  7. * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/regset.h>
  16. #include <linux/tracehook.h>
  17. #include <linux/user.h>
  18. #include <linux/elf.h>
  19. #include <linux/security.h>
  20. #include <linux/audit.h>
  21. #include <linux/seccomp.h>
  22. #include <linux/signal.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/hw_breakpoint.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/system.h>
  29. #include <asm/processor.h>
  30. #include <asm/i387.h>
  31. #include <asm/debugreg.h>
  32. #include <asm/ldt.h>
  33. #include <asm/desc.h>
  34. #include <asm/prctl.h>
  35. #include <asm/proto.h>
  36. #include <asm/ds.h>
  37. #include <asm/hw_breakpoint.h>
  38. #include "tls.h"
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/syscalls.h>
  41. enum x86_regset {
  42. REGSET_GENERAL,
  43. REGSET_FP,
  44. REGSET_XFP,
  45. REGSET_IOPERM64 = REGSET_XFP,
  46. REGSET_XSTATE,
  47. REGSET_TLS,
  48. REGSET_IOPERM32,
  49. };
  50. struct pt_regs_offset {
  51. const char *name;
  52. int offset;
  53. };
  54. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  55. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  56. static const struct pt_regs_offset regoffset_table[] = {
  57. #ifdef CONFIG_X86_64
  58. REG_OFFSET_NAME(r15),
  59. REG_OFFSET_NAME(r14),
  60. REG_OFFSET_NAME(r13),
  61. REG_OFFSET_NAME(r12),
  62. REG_OFFSET_NAME(r11),
  63. REG_OFFSET_NAME(r10),
  64. REG_OFFSET_NAME(r9),
  65. REG_OFFSET_NAME(r8),
  66. #endif
  67. REG_OFFSET_NAME(bx),
  68. REG_OFFSET_NAME(cx),
  69. REG_OFFSET_NAME(dx),
  70. REG_OFFSET_NAME(si),
  71. REG_OFFSET_NAME(di),
  72. REG_OFFSET_NAME(bp),
  73. REG_OFFSET_NAME(ax),
  74. #ifdef CONFIG_X86_32
  75. REG_OFFSET_NAME(ds),
  76. REG_OFFSET_NAME(es),
  77. REG_OFFSET_NAME(fs),
  78. REG_OFFSET_NAME(gs),
  79. #endif
  80. REG_OFFSET_NAME(orig_ax),
  81. REG_OFFSET_NAME(ip),
  82. REG_OFFSET_NAME(cs),
  83. REG_OFFSET_NAME(flags),
  84. REG_OFFSET_NAME(sp),
  85. REG_OFFSET_NAME(ss),
  86. REG_OFFSET_END,
  87. };
  88. /**
  89. * regs_query_register_offset() - query register offset from its name
  90. * @name: the name of a register
  91. *
  92. * regs_query_register_offset() returns the offset of a register in struct
  93. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  94. */
  95. int regs_query_register_offset(const char *name)
  96. {
  97. const struct pt_regs_offset *roff;
  98. for (roff = regoffset_table; roff->name != NULL; roff++)
  99. if (!strcmp(roff->name, name))
  100. return roff->offset;
  101. return -EINVAL;
  102. }
  103. /**
  104. * regs_query_register_name() - query register name from its offset
  105. * @offset: the offset of a register in struct pt_regs.
  106. *
  107. * regs_query_register_name() returns the name of a register from its
  108. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  109. */
  110. const char *regs_query_register_name(unsigned int offset)
  111. {
  112. const struct pt_regs_offset *roff;
  113. for (roff = regoffset_table; roff->name != NULL; roff++)
  114. if (roff->offset == offset)
  115. return roff->name;
  116. return NULL;
  117. }
  118. static const int arg_offs_table[] = {
  119. #ifdef CONFIG_X86_32
  120. [0] = offsetof(struct pt_regs, ax),
  121. [1] = offsetof(struct pt_regs, dx),
  122. [2] = offsetof(struct pt_regs, cx)
  123. #else /* CONFIG_X86_64 */
  124. [0] = offsetof(struct pt_regs, di),
  125. [1] = offsetof(struct pt_regs, si),
  126. [2] = offsetof(struct pt_regs, dx),
  127. [3] = offsetof(struct pt_regs, cx),
  128. [4] = offsetof(struct pt_regs, r8),
  129. [5] = offsetof(struct pt_regs, r9)
  130. #endif
  131. };
  132. /*
  133. * does not yet catch signals sent when the child dies.
  134. * in exit.c or in signal.c.
  135. */
  136. /*
  137. * Determines which flags the user has access to [1 = access, 0 = no access].
  138. */
  139. #define FLAG_MASK_32 ((unsigned long) \
  140. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  141. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  142. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  143. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  144. X86_EFLAGS_RF | X86_EFLAGS_AC))
  145. /*
  146. * Determines whether a value may be installed in a segment register.
  147. */
  148. static inline bool invalid_selector(u16 value)
  149. {
  150. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  151. }
  152. #ifdef CONFIG_X86_32
  153. #define FLAG_MASK FLAG_MASK_32
  154. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  155. {
  156. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  157. return &regs->bx + (regno >> 2);
  158. }
  159. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  160. {
  161. /*
  162. * Returning the value truncates it to 16 bits.
  163. */
  164. unsigned int retval;
  165. if (offset != offsetof(struct user_regs_struct, gs))
  166. retval = *pt_regs_access(task_pt_regs(task), offset);
  167. else {
  168. if (task == current)
  169. retval = get_user_gs(task_pt_regs(task));
  170. else
  171. retval = task_user_gs(task);
  172. }
  173. return retval;
  174. }
  175. static int set_segment_reg(struct task_struct *task,
  176. unsigned long offset, u16 value)
  177. {
  178. /*
  179. * The value argument was already truncated to 16 bits.
  180. */
  181. if (invalid_selector(value))
  182. return -EIO;
  183. /*
  184. * For %cs and %ss we cannot permit a null selector.
  185. * We can permit a bogus selector as long as it has USER_RPL.
  186. * Null selectors are fine for other segment registers, but
  187. * we will never get back to user mode with invalid %cs or %ss
  188. * and will take the trap in iret instead. Much code relies
  189. * on user_mode() to distinguish a user trap frame (which can
  190. * safely use invalid selectors) from a kernel trap frame.
  191. */
  192. switch (offset) {
  193. case offsetof(struct user_regs_struct, cs):
  194. case offsetof(struct user_regs_struct, ss):
  195. if (unlikely(value == 0))
  196. return -EIO;
  197. default:
  198. *pt_regs_access(task_pt_regs(task), offset) = value;
  199. break;
  200. case offsetof(struct user_regs_struct, gs):
  201. if (task == current)
  202. set_user_gs(task_pt_regs(task), value);
  203. else
  204. task_user_gs(task) = value;
  205. }
  206. return 0;
  207. }
  208. #else /* CONFIG_X86_64 */
  209. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  210. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  211. {
  212. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  213. return &regs->r15 + (offset / sizeof(regs->r15));
  214. }
  215. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  216. {
  217. /*
  218. * Returning the value truncates it to 16 bits.
  219. */
  220. unsigned int seg;
  221. switch (offset) {
  222. case offsetof(struct user_regs_struct, fs):
  223. if (task == current) {
  224. /* Older gas can't assemble movq %?s,%r?? */
  225. asm("movl %%fs,%0" : "=r" (seg));
  226. return seg;
  227. }
  228. return task->thread.fsindex;
  229. case offsetof(struct user_regs_struct, gs):
  230. if (task == current) {
  231. asm("movl %%gs,%0" : "=r" (seg));
  232. return seg;
  233. }
  234. return task->thread.gsindex;
  235. case offsetof(struct user_regs_struct, ds):
  236. if (task == current) {
  237. asm("movl %%ds,%0" : "=r" (seg));
  238. return seg;
  239. }
  240. return task->thread.ds;
  241. case offsetof(struct user_regs_struct, es):
  242. if (task == current) {
  243. asm("movl %%es,%0" : "=r" (seg));
  244. return seg;
  245. }
  246. return task->thread.es;
  247. case offsetof(struct user_regs_struct, cs):
  248. case offsetof(struct user_regs_struct, ss):
  249. break;
  250. }
  251. return *pt_regs_access(task_pt_regs(task), offset);
  252. }
  253. static int set_segment_reg(struct task_struct *task,
  254. unsigned long offset, u16 value)
  255. {
  256. /*
  257. * The value argument was already truncated to 16 bits.
  258. */
  259. if (invalid_selector(value))
  260. return -EIO;
  261. switch (offset) {
  262. case offsetof(struct user_regs_struct,fs):
  263. /*
  264. * If this is setting fs as for normal 64-bit use but
  265. * setting fs_base has implicitly changed it, leave it.
  266. */
  267. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  268. task->thread.fs != 0) ||
  269. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  270. task->thread.fs == 0))
  271. break;
  272. task->thread.fsindex = value;
  273. if (task == current)
  274. loadsegment(fs, task->thread.fsindex);
  275. break;
  276. case offsetof(struct user_regs_struct,gs):
  277. /*
  278. * If this is setting gs as for normal 64-bit use but
  279. * setting gs_base has implicitly changed it, leave it.
  280. */
  281. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  282. task->thread.gs != 0) ||
  283. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  284. task->thread.gs == 0))
  285. break;
  286. task->thread.gsindex = value;
  287. if (task == current)
  288. load_gs_index(task->thread.gsindex);
  289. break;
  290. case offsetof(struct user_regs_struct,ds):
  291. task->thread.ds = value;
  292. if (task == current)
  293. loadsegment(ds, task->thread.ds);
  294. break;
  295. case offsetof(struct user_regs_struct,es):
  296. task->thread.es = value;
  297. if (task == current)
  298. loadsegment(es, task->thread.es);
  299. break;
  300. /*
  301. * Can't actually change these in 64-bit mode.
  302. */
  303. case offsetof(struct user_regs_struct,cs):
  304. if (unlikely(value == 0))
  305. return -EIO;
  306. #ifdef CONFIG_IA32_EMULATION
  307. if (test_tsk_thread_flag(task, TIF_IA32))
  308. task_pt_regs(task)->cs = value;
  309. #endif
  310. break;
  311. case offsetof(struct user_regs_struct,ss):
  312. if (unlikely(value == 0))
  313. return -EIO;
  314. #ifdef CONFIG_IA32_EMULATION
  315. if (test_tsk_thread_flag(task, TIF_IA32))
  316. task_pt_regs(task)->ss = value;
  317. #endif
  318. break;
  319. }
  320. return 0;
  321. }
  322. #endif /* CONFIG_X86_32 */
  323. static unsigned long get_flags(struct task_struct *task)
  324. {
  325. unsigned long retval = task_pt_regs(task)->flags;
  326. /*
  327. * If the debugger set TF, hide it from the readout.
  328. */
  329. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  330. retval &= ~X86_EFLAGS_TF;
  331. return retval;
  332. }
  333. static int set_flags(struct task_struct *task, unsigned long value)
  334. {
  335. struct pt_regs *regs = task_pt_regs(task);
  336. /*
  337. * If the user value contains TF, mark that
  338. * it was not "us" (the debugger) that set it.
  339. * If not, make sure it stays set if we had.
  340. */
  341. if (value & X86_EFLAGS_TF)
  342. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  343. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  344. value |= X86_EFLAGS_TF;
  345. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  346. return 0;
  347. }
  348. static int putreg(struct task_struct *child,
  349. unsigned long offset, unsigned long value)
  350. {
  351. switch (offset) {
  352. case offsetof(struct user_regs_struct, cs):
  353. case offsetof(struct user_regs_struct, ds):
  354. case offsetof(struct user_regs_struct, es):
  355. case offsetof(struct user_regs_struct, fs):
  356. case offsetof(struct user_regs_struct, gs):
  357. case offsetof(struct user_regs_struct, ss):
  358. return set_segment_reg(child, offset, value);
  359. case offsetof(struct user_regs_struct, flags):
  360. return set_flags(child, value);
  361. #ifdef CONFIG_X86_64
  362. case offsetof(struct user_regs_struct,fs_base):
  363. if (value >= TASK_SIZE_OF(child))
  364. return -EIO;
  365. /*
  366. * When changing the segment base, use do_arch_prctl
  367. * to set either thread.fs or thread.fsindex and the
  368. * corresponding GDT slot.
  369. */
  370. if (child->thread.fs != value)
  371. return do_arch_prctl(child, ARCH_SET_FS, value);
  372. return 0;
  373. case offsetof(struct user_regs_struct,gs_base):
  374. /*
  375. * Exactly the same here as the %fs handling above.
  376. */
  377. if (value >= TASK_SIZE_OF(child))
  378. return -EIO;
  379. if (child->thread.gs != value)
  380. return do_arch_prctl(child, ARCH_SET_GS, value);
  381. return 0;
  382. #endif
  383. }
  384. *pt_regs_access(task_pt_regs(child), offset) = value;
  385. return 0;
  386. }
  387. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  388. {
  389. switch (offset) {
  390. case offsetof(struct user_regs_struct, cs):
  391. case offsetof(struct user_regs_struct, ds):
  392. case offsetof(struct user_regs_struct, es):
  393. case offsetof(struct user_regs_struct, fs):
  394. case offsetof(struct user_regs_struct, gs):
  395. case offsetof(struct user_regs_struct, ss):
  396. return get_segment_reg(task, offset);
  397. case offsetof(struct user_regs_struct, flags):
  398. return get_flags(task);
  399. #ifdef CONFIG_X86_64
  400. case offsetof(struct user_regs_struct, fs_base): {
  401. /*
  402. * do_arch_prctl may have used a GDT slot instead of
  403. * the MSR. To userland, it appears the same either
  404. * way, except the %fs segment selector might not be 0.
  405. */
  406. unsigned int seg = task->thread.fsindex;
  407. if (task->thread.fs != 0)
  408. return task->thread.fs;
  409. if (task == current)
  410. asm("movl %%fs,%0" : "=r" (seg));
  411. if (seg != FS_TLS_SEL)
  412. return 0;
  413. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  414. }
  415. case offsetof(struct user_regs_struct, gs_base): {
  416. /*
  417. * Exactly the same here as the %fs handling above.
  418. */
  419. unsigned int seg = task->thread.gsindex;
  420. if (task->thread.gs != 0)
  421. return task->thread.gs;
  422. if (task == current)
  423. asm("movl %%gs,%0" : "=r" (seg));
  424. if (seg != GS_TLS_SEL)
  425. return 0;
  426. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  427. }
  428. #endif
  429. }
  430. return *pt_regs_access(task_pt_regs(task), offset);
  431. }
  432. static int genregs_get(struct task_struct *target,
  433. const struct user_regset *regset,
  434. unsigned int pos, unsigned int count,
  435. void *kbuf, void __user *ubuf)
  436. {
  437. if (kbuf) {
  438. unsigned long *k = kbuf;
  439. while (count >= sizeof(*k)) {
  440. *k++ = getreg(target, pos);
  441. count -= sizeof(*k);
  442. pos += sizeof(*k);
  443. }
  444. } else {
  445. unsigned long __user *u = ubuf;
  446. while (count >= sizeof(*u)) {
  447. if (__put_user(getreg(target, pos), u++))
  448. return -EFAULT;
  449. count -= sizeof(*u);
  450. pos += sizeof(*u);
  451. }
  452. }
  453. return 0;
  454. }
  455. static int genregs_set(struct task_struct *target,
  456. const struct user_regset *regset,
  457. unsigned int pos, unsigned int count,
  458. const void *kbuf, const void __user *ubuf)
  459. {
  460. int ret = 0;
  461. if (kbuf) {
  462. const unsigned long *k = kbuf;
  463. while (count >= sizeof(*k) && !ret) {
  464. ret = putreg(target, pos, *k++);
  465. count -= sizeof(*k);
  466. pos += sizeof(*k);
  467. }
  468. } else {
  469. const unsigned long __user *u = ubuf;
  470. while (count >= sizeof(*u) && !ret) {
  471. unsigned long word;
  472. ret = __get_user(word, u++);
  473. if (ret)
  474. break;
  475. ret = putreg(target, pos, word);
  476. count -= sizeof(*u);
  477. pos += sizeof(*u);
  478. }
  479. }
  480. return ret;
  481. }
  482. static void ptrace_triggered(struct perf_event *bp, int nmi,
  483. struct perf_sample_data *data,
  484. struct pt_regs *regs)
  485. {
  486. int i;
  487. struct thread_struct *thread = &(current->thread);
  488. /*
  489. * Store in the virtual DR6 register the fact that the breakpoint
  490. * was hit so the thread's debugger will see it.
  491. */
  492. for (i = 0; i < HBP_NUM; i++) {
  493. if (thread->ptrace_bps[i] == bp)
  494. break;
  495. }
  496. thread->debugreg6 |= (DR_TRAP0 << i);
  497. }
  498. /*
  499. * Walk through every ptrace breakpoints for this thread and
  500. * build the dr7 value on top of their attributes.
  501. *
  502. */
  503. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  504. {
  505. int i;
  506. int dr7 = 0;
  507. struct arch_hw_breakpoint *info;
  508. for (i = 0; i < HBP_NUM; i++) {
  509. if (bp[i] && !bp[i]->attr.disabled) {
  510. info = counter_arch_bp(bp[i]);
  511. dr7 |= encode_dr7(i, info->len, info->type);
  512. }
  513. }
  514. return dr7;
  515. }
  516. static int
  517. ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
  518. struct task_struct *tsk, int disabled)
  519. {
  520. int err;
  521. int gen_len, gen_type;
  522. struct perf_event_attr attr;
  523. /*
  524. * We should have at least an inactive breakpoint at this
  525. * slot. It means the user is writing dr7 without having
  526. * written the address register first
  527. */
  528. if (!bp)
  529. return -EINVAL;
  530. err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
  531. if (err)
  532. return err;
  533. attr = bp->attr;
  534. attr.bp_len = gen_len;
  535. attr.bp_type = gen_type;
  536. attr.disabled = disabled;
  537. return modify_user_hw_breakpoint(bp, &attr);
  538. }
  539. /*
  540. * Handle ptrace writes to debug register 7.
  541. */
  542. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  543. {
  544. struct thread_struct *thread = &(tsk->thread);
  545. unsigned long old_dr7;
  546. int i, orig_ret = 0, rc = 0;
  547. int enabled, second_pass = 0;
  548. unsigned len, type;
  549. struct perf_event *bp;
  550. data &= ~DR_CONTROL_RESERVED;
  551. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  552. restore:
  553. /*
  554. * Loop through all the hardware breakpoints, making the
  555. * appropriate changes to each.
  556. */
  557. for (i = 0; i < HBP_NUM; i++) {
  558. enabled = decode_dr7(data, i, &len, &type);
  559. bp = thread->ptrace_bps[i];
  560. if (!enabled) {
  561. if (bp) {
  562. /*
  563. * Don't unregister the breakpoints right-away,
  564. * unless all register_user_hw_breakpoint()
  565. * requests have succeeded. This prevents
  566. * any window of opportunity for debug
  567. * register grabbing by other users.
  568. */
  569. if (!second_pass)
  570. continue;
  571. rc = ptrace_modify_breakpoint(bp, len, type,
  572. tsk, 1);
  573. if (rc)
  574. break;
  575. }
  576. continue;
  577. }
  578. rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
  579. if (rc)
  580. break;
  581. }
  582. /*
  583. * Make a second pass to free the remaining unused breakpoints
  584. * or to restore the original breakpoints if an error occurred.
  585. */
  586. if (!second_pass) {
  587. second_pass = 1;
  588. if (rc < 0) {
  589. orig_ret = rc;
  590. data = old_dr7;
  591. }
  592. goto restore;
  593. }
  594. return ((orig_ret < 0) ? orig_ret : rc);
  595. }
  596. /*
  597. * Handle PTRACE_PEEKUSR calls for the debug register area.
  598. */
  599. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  600. {
  601. struct thread_struct *thread = &(tsk->thread);
  602. unsigned long val = 0;
  603. if (n < HBP_NUM) {
  604. struct perf_event *bp;
  605. bp = thread->ptrace_bps[n];
  606. if (!bp)
  607. return 0;
  608. val = bp->hw.info.address;
  609. } else if (n == 6) {
  610. val = thread->debugreg6;
  611. } else if (n == 7) {
  612. val = thread->ptrace_dr7;
  613. }
  614. return val;
  615. }
  616. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  617. unsigned long addr)
  618. {
  619. struct perf_event *bp;
  620. struct thread_struct *t = &tsk->thread;
  621. struct perf_event_attr attr;
  622. if (!t->ptrace_bps[nr]) {
  623. hw_breakpoint_init(&attr);
  624. /*
  625. * Put stub len and type to register (reserve) an inactive but
  626. * correct bp
  627. */
  628. attr.bp_addr = addr;
  629. attr.bp_len = HW_BREAKPOINT_LEN_1;
  630. attr.bp_type = HW_BREAKPOINT_W;
  631. attr.disabled = 1;
  632. bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
  633. /*
  634. * CHECKME: the previous code returned -EIO if the addr wasn't
  635. * a valid task virtual addr. The new one will return -EINVAL in
  636. * this case.
  637. * -EINVAL may be what we want for in-kernel breakpoints users,
  638. * but -EIO looks better for ptrace, since we refuse a register
  639. * writing for the user. And anyway this is the previous
  640. * behaviour.
  641. */
  642. if (IS_ERR(bp))
  643. return PTR_ERR(bp);
  644. t->ptrace_bps[nr] = bp;
  645. } else {
  646. int err;
  647. bp = t->ptrace_bps[nr];
  648. attr = bp->attr;
  649. attr.bp_addr = addr;
  650. err = modify_user_hw_breakpoint(bp, &attr);
  651. if (err)
  652. return err;
  653. }
  654. return 0;
  655. }
  656. /*
  657. * Handle PTRACE_POKEUSR calls for the debug register area.
  658. */
  659. int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
  660. {
  661. struct thread_struct *thread = &(tsk->thread);
  662. int rc = 0;
  663. /* There are no DR4 or DR5 registers */
  664. if (n == 4 || n == 5)
  665. return -EIO;
  666. if (n == 6) {
  667. thread->debugreg6 = val;
  668. goto ret_path;
  669. }
  670. if (n < HBP_NUM) {
  671. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  672. if (rc)
  673. return rc;
  674. }
  675. /* All that's left is DR7 */
  676. if (n == 7) {
  677. rc = ptrace_write_dr7(tsk, val);
  678. if (!rc)
  679. thread->ptrace_dr7 = val;
  680. }
  681. ret_path:
  682. return rc;
  683. }
  684. /*
  685. * These access the current or another (stopped) task's io permission
  686. * bitmap for debugging or core dump.
  687. */
  688. static int ioperm_active(struct task_struct *target,
  689. const struct user_regset *regset)
  690. {
  691. return target->thread.io_bitmap_max / regset->size;
  692. }
  693. static int ioperm_get(struct task_struct *target,
  694. const struct user_regset *regset,
  695. unsigned int pos, unsigned int count,
  696. void *kbuf, void __user *ubuf)
  697. {
  698. if (!target->thread.io_bitmap_ptr)
  699. return -ENXIO;
  700. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  701. target->thread.io_bitmap_ptr,
  702. 0, IO_BITMAP_BYTES);
  703. }
  704. #ifdef CONFIG_X86_PTRACE_BTS
  705. /*
  706. * A branch trace store context.
  707. *
  708. * Contexts may only be installed by ptrace_bts_config() and only for
  709. * ptraced tasks.
  710. *
  711. * Contexts are destroyed when the tracee is detached from the tracer.
  712. * The actual destruction work requires interrupts enabled, so the
  713. * work is deferred and will be scheduled during __ptrace_unlink().
  714. *
  715. * Contexts hold an additional task_struct reference on the traced
  716. * task, as well as a reference on the tracer's mm.
  717. *
  718. * Ptrace already holds a task_struct for the duration of ptrace operations,
  719. * but since destruction is deferred, it may be executed after both
  720. * tracer and tracee exited.
  721. */
  722. struct bts_context {
  723. /* The branch trace handle. */
  724. struct bts_tracer *tracer;
  725. /* The buffer used to store the branch trace and its size. */
  726. void *buffer;
  727. unsigned int size;
  728. /* The mm that paid for the above buffer. */
  729. struct mm_struct *mm;
  730. /* The task this context belongs to. */
  731. struct task_struct *task;
  732. /* The signal to send on a bts buffer overflow. */
  733. unsigned int bts_ovfl_signal;
  734. /* The work struct to destroy a context. */
  735. struct work_struct work;
  736. };
  737. static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
  738. {
  739. void *buffer = NULL;
  740. int err = -ENOMEM;
  741. err = account_locked_memory(current->mm, current->signal->rlim, size);
  742. if (err < 0)
  743. return err;
  744. buffer = kzalloc(size, GFP_KERNEL);
  745. if (!buffer)
  746. goto out_refund;
  747. context->buffer = buffer;
  748. context->size = size;
  749. context->mm = get_task_mm(current);
  750. return 0;
  751. out_refund:
  752. refund_locked_memory(current->mm, size);
  753. return err;
  754. }
  755. static inline void free_bts_buffer(struct bts_context *context)
  756. {
  757. if (!context->buffer)
  758. return;
  759. kfree(context->buffer);
  760. context->buffer = NULL;
  761. refund_locked_memory(context->mm, context->size);
  762. context->size = 0;
  763. mmput(context->mm);
  764. context->mm = NULL;
  765. }
  766. static void free_bts_context_work(struct work_struct *w)
  767. {
  768. struct bts_context *context;
  769. context = container_of(w, struct bts_context, work);
  770. ds_release_bts(context->tracer);
  771. put_task_struct(context->task);
  772. free_bts_buffer(context);
  773. kfree(context);
  774. }
  775. static inline void free_bts_context(struct bts_context *context)
  776. {
  777. INIT_WORK(&context->work, free_bts_context_work);
  778. schedule_work(&context->work);
  779. }
  780. static inline struct bts_context *alloc_bts_context(struct task_struct *task)
  781. {
  782. struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
  783. if (context) {
  784. context->task = task;
  785. task->bts = context;
  786. get_task_struct(task);
  787. }
  788. return context;
  789. }
  790. static int ptrace_bts_read_record(struct task_struct *child, size_t index,
  791. struct bts_struct __user *out)
  792. {
  793. struct bts_context *context;
  794. const struct bts_trace *trace;
  795. struct bts_struct bts;
  796. const unsigned char *at;
  797. int error;
  798. context = child->bts;
  799. if (!context)
  800. return -ESRCH;
  801. trace = ds_read_bts(context->tracer);
  802. if (!trace)
  803. return -ESRCH;
  804. at = trace->ds.top - ((index + 1) * trace->ds.size);
  805. if ((void *)at < trace->ds.begin)
  806. at += (trace->ds.n * trace->ds.size);
  807. if (!trace->read)
  808. return -EOPNOTSUPP;
  809. error = trace->read(context->tracer, at, &bts);
  810. if (error < 0)
  811. return error;
  812. if (copy_to_user(out, &bts, sizeof(bts)))
  813. return -EFAULT;
  814. return sizeof(bts);
  815. }
  816. static int ptrace_bts_drain(struct task_struct *child,
  817. long size,
  818. struct bts_struct __user *out)
  819. {
  820. struct bts_context *context;
  821. const struct bts_trace *trace;
  822. const unsigned char *at;
  823. int error, drained = 0;
  824. context = child->bts;
  825. if (!context)
  826. return -ESRCH;
  827. trace = ds_read_bts(context->tracer);
  828. if (!trace)
  829. return -ESRCH;
  830. if (!trace->read)
  831. return -EOPNOTSUPP;
  832. if (size < (trace->ds.top - trace->ds.begin))
  833. return -EIO;
  834. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  835. out++, drained++, at += trace->ds.size) {
  836. struct bts_struct bts;
  837. error = trace->read(context->tracer, at, &bts);
  838. if (error < 0)
  839. return error;
  840. if (copy_to_user(out, &bts, sizeof(bts)))
  841. return -EFAULT;
  842. }
  843. memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
  844. error = ds_reset_bts(context->tracer);
  845. if (error < 0)
  846. return error;
  847. return drained;
  848. }
  849. static int ptrace_bts_config(struct task_struct *child,
  850. long cfg_size,
  851. const struct ptrace_bts_config __user *ucfg)
  852. {
  853. struct bts_context *context;
  854. struct ptrace_bts_config cfg;
  855. unsigned int flags = 0;
  856. if (cfg_size < sizeof(cfg))
  857. return -EIO;
  858. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  859. return -EFAULT;
  860. context = child->bts;
  861. if (!context)
  862. context = alloc_bts_context(child);
  863. if (!context)
  864. return -ENOMEM;
  865. if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
  866. if (!cfg.signal)
  867. return -EINVAL;
  868. return -EOPNOTSUPP;
  869. context->bts_ovfl_signal = cfg.signal;
  870. }
  871. ds_release_bts(context->tracer);
  872. context->tracer = NULL;
  873. if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
  874. int err;
  875. free_bts_buffer(context);
  876. if (!cfg.size)
  877. return 0;
  878. err = alloc_bts_buffer(context, cfg.size);
  879. if (err < 0)
  880. return err;
  881. }
  882. if (cfg.flags & PTRACE_BTS_O_TRACE)
  883. flags |= BTS_USER;
  884. if (cfg.flags & PTRACE_BTS_O_SCHED)
  885. flags |= BTS_TIMESTAMPS;
  886. context->tracer =
  887. ds_request_bts_task(child, context->buffer, context->size,
  888. NULL, (size_t)-1, flags);
  889. if (unlikely(IS_ERR(context->tracer))) {
  890. int error = PTR_ERR(context->tracer);
  891. free_bts_buffer(context);
  892. context->tracer = NULL;
  893. return error;
  894. }
  895. return sizeof(cfg);
  896. }
  897. static int ptrace_bts_status(struct task_struct *child,
  898. long cfg_size,
  899. struct ptrace_bts_config __user *ucfg)
  900. {
  901. struct bts_context *context;
  902. const struct bts_trace *trace;
  903. struct ptrace_bts_config cfg;
  904. context = child->bts;
  905. if (!context)
  906. return -ESRCH;
  907. if (cfg_size < sizeof(cfg))
  908. return -EIO;
  909. trace = ds_read_bts(context->tracer);
  910. if (!trace)
  911. return -ESRCH;
  912. memset(&cfg, 0, sizeof(cfg));
  913. cfg.size = trace->ds.end - trace->ds.begin;
  914. cfg.signal = context->bts_ovfl_signal;
  915. cfg.bts_size = sizeof(struct bts_struct);
  916. if (cfg.signal)
  917. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  918. if (trace->ds.flags & BTS_USER)
  919. cfg.flags |= PTRACE_BTS_O_TRACE;
  920. if (trace->ds.flags & BTS_TIMESTAMPS)
  921. cfg.flags |= PTRACE_BTS_O_SCHED;
  922. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  923. return -EFAULT;
  924. return sizeof(cfg);
  925. }
  926. static int ptrace_bts_clear(struct task_struct *child)
  927. {
  928. struct bts_context *context;
  929. const struct bts_trace *trace;
  930. context = child->bts;
  931. if (!context)
  932. return -ESRCH;
  933. trace = ds_read_bts(context->tracer);
  934. if (!trace)
  935. return -ESRCH;
  936. memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
  937. return ds_reset_bts(context->tracer);
  938. }
  939. static int ptrace_bts_size(struct task_struct *child)
  940. {
  941. struct bts_context *context;
  942. const struct bts_trace *trace;
  943. context = child->bts;
  944. if (!context)
  945. return -ESRCH;
  946. trace = ds_read_bts(context->tracer);
  947. if (!trace)
  948. return -ESRCH;
  949. return (trace->ds.top - trace->ds.begin) / trace->ds.size;
  950. }
  951. /*
  952. * Called from __ptrace_unlink() after the child has been moved back
  953. * to its original parent.
  954. */
  955. void ptrace_bts_untrace(struct task_struct *child)
  956. {
  957. if (unlikely(child->bts)) {
  958. free_bts_context(child->bts);
  959. child->bts = NULL;
  960. }
  961. }
  962. #endif /* CONFIG_X86_PTRACE_BTS */
  963. /*
  964. * Called by kernel/ptrace.c when detaching..
  965. *
  966. * Make sure the single step bit is not set.
  967. */
  968. void ptrace_disable(struct task_struct *child)
  969. {
  970. user_disable_single_step(child);
  971. #ifdef TIF_SYSCALL_EMU
  972. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  973. #endif
  974. }
  975. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  976. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  977. #endif
  978. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  979. {
  980. int ret;
  981. unsigned long __user *datap = (unsigned long __user *)data;
  982. switch (request) {
  983. /* read the word at location addr in the USER area. */
  984. case PTRACE_PEEKUSR: {
  985. unsigned long tmp;
  986. ret = -EIO;
  987. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  988. addr >= sizeof(struct user))
  989. break;
  990. tmp = 0; /* Default return condition */
  991. if (addr < sizeof(struct user_regs_struct))
  992. tmp = getreg(child, addr);
  993. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  994. addr <= offsetof(struct user, u_debugreg[7])) {
  995. addr -= offsetof(struct user, u_debugreg[0]);
  996. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  997. }
  998. ret = put_user(tmp, datap);
  999. break;
  1000. }
  1001. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  1002. ret = -EIO;
  1003. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  1004. addr >= sizeof(struct user))
  1005. break;
  1006. if (addr < sizeof(struct user_regs_struct))
  1007. ret = putreg(child, addr, data);
  1008. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1009. addr <= offsetof(struct user, u_debugreg[7])) {
  1010. addr -= offsetof(struct user, u_debugreg[0]);
  1011. ret = ptrace_set_debugreg(child,
  1012. addr / sizeof(data), data);
  1013. }
  1014. break;
  1015. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1016. return copy_regset_to_user(child,
  1017. task_user_regset_view(current),
  1018. REGSET_GENERAL,
  1019. 0, sizeof(struct user_regs_struct),
  1020. datap);
  1021. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1022. return copy_regset_from_user(child,
  1023. task_user_regset_view(current),
  1024. REGSET_GENERAL,
  1025. 0, sizeof(struct user_regs_struct),
  1026. datap);
  1027. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1028. return copy_regset_to_user(child,
  1029. task_user_regset_view(current),
  1030. REGSET_FP,
  1031. 0, sizeof(struct user_i387_struct),
  1032. datap);
  1033. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1034. return copy_regset_from_user(child,
  1035. task_user_regset_view(current),
  1036. REGSET_FP,
  1037. 0, sizeof(struct user_i387_struct),
  1038. datap);
  1039. #ifdef CONFIG_X86_32
  1040. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1041. return copy_regset_to_user(child, &user_x86_32_view,
  1042. REGSET_XFP,
  1043. 0, sizeof(struct user_fxsr_struct),
  1044. datap) ? -EIO : 0;
  1045. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1046. return copy_regset_from_user(child, &user_x86_32_view,
  1047. REGSET_XFP,
  1048. 0, sizeof(struct user_fxsr_struct),
  1049. datap) ? -EIO : 0;
  1050. #endif
  1051. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1052. case PTRACE_GET_THREAD_AREA:
  1053. if (addr < 0)
  1054. return -EIO;
  1055. ret = do_get_thread_area(child, addr,
  1056. (struct user_desc __user *) data);
  1057. break;
  1058. case PTRACE_SET_THREAD_AREA:
  1059. if (addr < 0)
  1060. return -EIO;
  1061. ret = do_set_thread_area(child, addr,
  1062. (struct user_desc __user *) data, 0);
  1063. break;
  1064. #endif
  1065. #ifdef CONFIG_X86_64
  1066. /* normal 64bit interface to access TLS data.
  1067. Works just like arch_prctl, except that the arguments
  1068. are reversed. */
  1069. case PTRACE_ARCH_PRCTL:
  1070. ret = do_arch_prctl(child, data, addr);
  1071. break;
  1072. #endif
  1073. /*
  1074. * These bits need more cooking - not enabled yet:
  1075. */
  1076. #ifdef CONFIG_X86_PTRACE_BTS
  1077. case PTRACE_BTS_CONFIG:
  1078. ret = ptrace_bts_config
  1079. (child, data, (struct ptrace_bts_config __user *)addr);
  1080. break;
  1081. case PTRACE_BTS_STATUS:
  1082. ret = ptrace_bts_status
  1083. (child, data, (struct ptrace_bts_config __user *)addr);
  1084. break;
  1085. case PTRACE_BTS_SIZE:
  1086. ret = ptrace_bts_size(child);
  1087. break;
  1088. case PTRACE_BTS_GET:
  1089. ret = ptrace_bts_read_record
  1090. (child, data, (struct bts_struct __user *) addr);
  1091. break;
  1092. case PTRACE_BTS_CLEAR:
  1093. ret = ptrace_bts_clear(child);
  1094. break;
  1095. case PTRACE_BTS_DRAIN:
  1096. ret = ptrace_bts_drain
  1097. (child, data, (struct bts_struct __user *) addr);
  1098. break;
  1099. #endif /* CONFIG_X86_PTRACE_BTS */
  1100. default:
  1101. ret = ptrace_request(child, request, addr, data);
  1102. break;
  1103. }
  1104. return ret;
  1105. }
  1106. #ifdef CONFIG_IA32_EMULATION
  1107. #include <linux/compat.h>
  1108. #include <linux/syscalls.h>
  1109. #include <asm/ia32.h>
  1110. #include <asm/user32.h>
  1111. #define R32(l,q) \
  1112. case offsetof(struct user32, regs.l): \
  1113. regs->q = value; break
  1114. #define SEG32(rs) \
  1115. case offsetof(struct user32, regs.rs): \
  1116. return set_segment_reg(child, \
  1117. offsetof(struct user_regs_struct, rs), \
  1118. value); \
  1119. break
  1120. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  1121. {
  1122. struct pt_regs *regs = task_pt_regs(child);
  1123. switch (regno) {
  1124. SEG32(cs);
  1125. SEG32(ds);
  1126. SEG32(es);
  1127. SEG32(fs);
  1128. SEG32(gs);
  1129. SEG32(ss);
  1130. R32(ebx, bx);
  1131. R32(ecx, cx);
  1132. R32(edx, dx);
  1133. R32(edi, di);
  1134. R32(esi, si);
  1135. R32(ebp, bp);
  1136. R32(eax, ax);
  1137. R32(eip, ip);
  1138. R32(esp, sp);
  1139. case offsetof(struct user32, regs.orig_eax):
  1140. /*
  1141. * A 32-bit debugger setting orig_eax means to restore
  1142. * the state of the task restarting a 32-bit syscall.
  1143. * Make sure we interpret the -ERESTART* codes correctly
  1144. * in case the task is not actually still sitting at the
  1145. * exit from a 32-bit syscall with TS_COMPAT still set.
  1146. */
  1147. regs->orig_ax = value;
  1148. if (syscall_get_nr(child, regs) >= 0)
  1149. task_thread_info(child)->status |= TS_COMPAT;
  1150. break;
  1151. case offsetof(struct user32, regs.eflags):
  1152. return set_flags(child, value);
  1153. case offsetof(struct user32, u_debugreg[0]) ...
  1154. offsetof(struct user32, u_debugreg[7]):
  1155. regno -= offsetof(struct user32, u_debugreg[0]);
  1156. return ptrace_set_debugreg(child, regno / 4, value);
  1157. default:
  1158. if (regno > sizeof(struct user32) || (regno & 3))
  1159. return -EIO;
  1160. /*
  1161. * Other dummy fields in the virtual user structure
  1162. * are ignored
  1163. */
  1164. break;
  1165. }
  1166. return 0;
  1167. }
  1168. #undef R32
  1169. #undef SEG32
  1170. #define R32(l,q) \
  1171. case offsetof(struct user32, regs.l): \
  1172. *val = regs->q; break
  1173. #define SEG32(rs) \
  1174. case offsetof(struct user32, regs.rs): \
  1175. *val = get_segment_reg(child, \
  1176. offsetof(struct user_regs_struct, rs)); \
  1177. break
  1178. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  1179. {
  1180. struct pt_regs *regs = task_pt_regs(child);
  1181. switch (regno) {
  1182. SEG32(ds);
  1183. SEG32(es);
  1184. SEG32(fs);
  1185. SEG32(gs);
  1186. R32(cs, cs);
  1187. R32(ss, ss);
  1188. R32(ebx, bx);
  1189. R32(ecx, cx);
  1190. R32(edx, dx);
  1191. R32(edi, di);
  1192. R32(esi, si);
  1193. R32(ebp, bp);
  1194. R32(eax, ax);
  1195. R32(orig_eax, orig_ax);
  1196. R32(eip, ip);
  1197. R32(esp, sp);
  1198. case offsetof(struct user32, regs.eflags):
  1199. *val = get_flags(child);
  1200. break;
  1201. case offsetof(struct user32, u_debugreg[0]) ...
  1202. offsetof(struct user32, u_debugreg[7]):
  1203. regno -= offsetof(struct user32, u_debugreg[0]);
  1204. *val = ptrace_get_debugreg(child, regno / 4);
  1205. break;
  1206. default:
  1207. if (regno > sizeof(struct user32) || (regno & 3))
  1208. return -EIO;
  1209. /*
  1210. * Other dummy fields in the virtual user structure
  1211. * are ignored
  1212. */
  1213. *val = 0;
  1214. break;
  1215. }
  1216. return 0;
  1217. }
  1218. #undef R32
  1219. #undef SEG32
  1220. static int genregs32_get(struct task_struct *target,
  1221. const struct user_regset *regset,
  1222. unsigned int pos, unsigned int count,
  1223. void *kbuf, void __user *ubuf)
  1224. {
  1225. if (kbuf) {
  1226. compat_ulong_t *k = kbuf;
  1227. while (count >= sizeof(*k)) {
  1228. getreg32(target, pos, k++);
  1229. count -= sizeof(*k);
  1230. pos += sizeof(*k);
  1231. }
  1232. } else {
  1233. compat_ulong_t __user *u = ubuf;
  1234. while (count >= sizeof(*u)) {
  1235. compat_ulong_t word;
  1236. getreg32(target, pos, &word);
  1237. if (__put_user(word, u++))
  1238. return -EFAULT;
  1239. count -= sizeof(*u);
  1240. pos += sizeof(*u);
  1241. }
  1242. }
  1243. return 0;
  1244. }
  1245. static int genregs32_set(struct task_struct *target,
  1246. const struct user_regset *regset,
  1247. unsigned int pos, unsigned int count,
  1248. const void *kbuf, const void __user *ubuf)
  1249. {
  1250. int ret = 0;
  1251. if (kbuf) {
  1252. const compat_ulong_t *k = kbuf;
  1253. while (count >= sizeof(*k) && !ret) {
  1254. ret = putreg32(target, pos, *k++);
  1255. count -= sizeof(*k);
  1256. pos += sizeof(*k);
  1257. }
  1258. } else {
  1259. const compat_ulong_t __user *u = ubuf;
  1260. while (count >= sizeof(*u) && !ret) {
  1261. compat_ulong_t word;
  1262. ret = __get_user(word, u++);
  1263. if (ret)
  1264. break;
  1265. ret = putreg32(target, pos, word);
  1266. count -= sizeof(*u);
  1267. pos += sizeof(*u);
  1268. }
  1269. }
  1270. return ret;
  1271. }
  1272. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1273. compat_ulong_t caddr, compat_ulong_t cdata)
  1274. {
  1275. unsigned long addr = caddr;
  1276. unsigned long data = cdata;
  1277. void __user *datap = compat_ptr(data);
  1278. int ret;
  1279. __u32 val;
  1280. switch (request) {
  1281. case PTRACE_PEEKUSR:
  1282. ret = getreg32(child, addr, &val);
  1283. if (ret == 0)
  1284. ret = put_user(val, (__u32 __user *)datap);
  1285. break;
  1286. case PTRACE_POKEUSR:
  1287. ret = putreg32(child, addr, data);
  1288. break;
  1289. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1290. return copy_regset_to_user(child, &user_x86_32_view,
  1291. REGSET_GENERAL,
  1292. 0, sizeof(struct user_regs_struct32),
  1293. datap);
  1294. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1295. return copy_regset_from_user(child, &user_x86_32_view,
  1296. REGSET_GENERAL, 0,
  1297. sizeof(struct user_regs_struct32),
  1298. datap);
  1299. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1300. return copy_regset_to_user(child, &user_x86_32_view,
  1301. REGSET_FP, 0,
  1302. sizeof(struct user_i387_ia32_struct),
  1303. datap);
  1304. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1305. return copy_regset_from_user(
  1306. child, &user_x86_32_view, REGSET_FP,
  1307. 0, sizeof(struct user_i387_ia32_struct), datap);
  1308. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1309. return copy_regset_to_user(child, &user_x86_32_view,
  1310. REGSET_XFP, 0,
  1311. sizeof(struct user32_fxsr_struct),
  1312. datap);
  1313. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1314. return copy_regset_from_user(child, &user_x86_32_view,
  1315. REGSET_XFP, 0,
  1316. sizeof(struct user32_fxsr_struct),
  1317. datap);
  1318. case PTRACE_GET_THREAD_AREA:
  1319. case PTRACE_SET_THREAD_AREA:
  1320. #ifdef CONFIG_X86_PTRACE_BTS
  1321. case PTRACE_BTS_CONFIG:
  1322. case PTRACE_BTS_STATUS:
  1323. case PTRACE_BTS_SIZE:
  1324. case PTRACE_BTS_GET:
  1325. case PTRACE_BTS_CLEAR:
  1326. case PTRACE_BTS_DRAIN:
  1327. #endif /* CONFIG_X86_PTRACE_BTS */
  1328. return arch_ptrace(child, request, addr, data);
  1329. default:
  1330. return compat_ptrace_request(child, request, addr, data);
  1331. }
  1332. return ret;
  1333. }
  1334. #endif /* CONFIG_IA32_EMULATION */
  1335. #ifdef CONFIG_X86_64
  1336. static struct user_regset x86_64_regsets[] __read_mostly = {
  1337. [REGSET_GENERAL] = {
  1338. .core_note_type = NT_PRSTATUS,
  1339. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1340. .size = sizeof(long), .align = sizeof(long),
  1341. .get = genregs_get, .set = genregs_set
  1342. },
  1343. [REGSET_FP] = {
  1344. .core_note_type = NT_PRFPREG,
  1345. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1346. .size = sizeof(long), .align = sizeof(long),
  1347. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1348. },
  1349. [REGSET_XSTATE] = {
  1350. .core_note_type = NT_X86_XSTATE,
  1351. .size = sizeof(u64), .align = sizeof(u64),
  1352. .active = xstateregs_active, .get = xstateregs_get,
  1353. .set = xstateregs_set
  1354. },
  1355. [REGSET_IOPERM64] = {
  1356. .core_note_type = NT_386_IOPERM,
  1357. .n = IO_BITMAP_LONGS,
  1358. .size = sizeof(long), .align = sizeof(long),
  1359. .active = ioperm_active, .get = ioperm_get
  1360. },
  1361. };
  1362. static const struct user_regset_view user_x86_64_view = {
  1363. .name = "x86_64", .e_machine = EM_X86_64,
  1364. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1365. };
  1366. #else /* CONFIG_X86_32 */
  1367. #define user_regs_struct32 user_regs_struct
  1368. #define genregs32_get genregs_get
  1369. #define genregs32_set genregs_set
  1370. #define user_i387_ia32_struct user_i387_struct
  1371. #define user32_fxsr_struct user_fxsr_struct
  1372. #endif /* CONFIG_X86_64 */
  1373. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1374. static struct user_regset x86_32_regsets[] __read_mostly = {
  1375. [REGSET_GENERAL] = {
  1376. .core_note_type = NT_PRSTATUS,
  1377. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1378. .size = sizeof(u32), .align = sizeof(u32),
  1379. .get = genregs32_get, .set = genregs32_set
  1380. },
  1381. [REGSET_FP] = {
  1382. .core_note_type = NT_PRFPREG,
  1383. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1384. .size = sizeof(u32), .align = sizeof(u32),
  1385. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1386. },
  1387. [REGSET_XFP] = {
  1388. .core_note_type = NT_PRXFPREG,
  1389. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1390. .size = sizeof(u32), .align = sizeof(u32),
  1391. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1392. },
  1393. [REGSET_XSTATE] = {
  1394. .core_note_type = NT_X86_XSTATE,
  1395. .size = sizeof(u64), .align = sizeof(u64),
  1396. .active = xstateregs_active, .get = xstateregs_get,
  1397. .set = xstateregs_set
  1398. },
  1399. [REGSET_TLS] = {
  1400. .core_note_type = NT_386_TLS,
  1401. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1402. .size = sizeof(struct user_desc),
  1403. .align = sizeof(struct user_desc),
  1404. .active = regset_tls_active,
  1405. .get = regset_tls_get, .set = regset_tls_set
  1406. },
  1407. [REGSET_IOPERM32] = {
  1408. .core_note_type = NT_386_IOPERM,
  1409. .n = IO_BITMAP_BYTES / sizeof(u32),
  1410. .size = sizeof(u32), .align = sizeof(u32),
  1411. .active = ioperm_active, .get = ioperm_get
  1412. },
  1413. };
  1414. static const struct user_regset_view user_x86_32_view = {
  1415. .name = "i386", .e_machine = EM_386,
  1416. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1417. };
  1418. #endif
  1419. /*
  1420. * This represents bytes 464..511 in the memory layout exported through
  1421. * the REGSET_XSTATE interface.
  1422. */
  1423. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1424. void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1425. {
  1426. #ifdef CONFIG_X86_64
  1427. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1428. #endif
  1429. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1430. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1431. #endif
  1432. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1433. }
  1434. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1435. {
  1436. #ifdef CONFIG_IA32_EMULATION
  1437. if (test_tsk_thread_flag(task, TIF_IA32))
  1438. #endif
  1439. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1440. return &user_x86_32_view;
  1441. #endif
  1442. #ifdef CONFIG_X86_64
  1443. return &user_x86_64_view;
  1444. #endif
  1445. }
  1446. static void fill_sigtrap_info(struct task_struct *tsk,
  1447. struct pt_regs *regs,
  1448. int error_code, int si_code,
  1449. struct siginfo *info)
  1450. {
  1451. tsk->thread.trap_no = 1;
  1452. tsk->thread.error_code = error_code;
  1453. memset(info, 0, sizeof(*info));
  1454. info->si_signo = SIGTRAP;
  1455. info->si_code = si_code;
  1456. info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
  1457. }
  1458. void user_single_step_siginfo(struct task_struct *tsk,
  1459. struct pt_regs *regs,
  1460. struct siginfo *info)
  1461. {
  1462. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1463. }
  1464. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1465. int error_code, int si_code)
  1466. {
  1467. struct siginfo info;
  1468. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1469. /* Send us the fake SIGTRAP */
  1470. force_sig_info(SIGTRAP, &info, tsk);
  1471. }
  1472. #ifdef CONFIG_X86_32
  1473. # define IS_IA32 1
  1474. #elif defined CONFIG_IA32_EMULATION
  1475. # define IS_IA32 is_compat_task()
  1476. #else
  1477. # define IS_IA32 0
  1478. #endif
  1479. /*
  1480. * We must return the syscall number to actually look up in the table.
  1481. * This can be -1L to skip running any syscall at all.
  1482. */
  1483. asmregparm long syscall_trace_enter(struct pt_regs *regs)
  1484. {
  1485. long ret = 0;
  1486. /*
  1487. * If we stepped into a sysenter/syscall insn, it trapped in
  1488. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1489. * If user-mode had set TF itself, then it's still clear from
  1490. * do_debug() and we need to set it again to restore the user
  1491. * state. If we entered on the slow path, TF was already set.
  1492. */
  1493. if (test_thread_flag(TIF_SINGLESTEP))
  1494. regs->flags |= X86_EFLAGS_TF;
  1495. /* do the secure computing check first */
  1496. secure_computing(regs->orig_ax);
  1497. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1498. ret = -1L;
  1499. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1500. tracehook_report_syscall_entry(regs))
  1501. ret = -1L;
  1502. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1503. trace_sys_enter(regs, regs->orig_ax);
  1504. if (unlikely(current->audit_context)) {
  1505. if (IS_IA32)
  1506. audit_syscall_entry(AUDIT_ARCH_I386,
  1507. regs->orig_ax,
  1508. regs->bx, regs->cx,
  1509. regs->dx, regs->si);
  1510. #ifdef CONFIG_X86_64
  1511. else
  1512. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1513. regs->orig_ax,
  1514. regs->di, regs->si,
  1515. regs->dx, regs->r10);
  1516. #endif
  1517. }
  1518. return ret ?: regs->orig_ax;
  1519. }
  1520. asmregparm void syscall_trace_leave(struct pt_regs *regs)
  1521. {
  1522. bool step;
  1523. if (unlikely(current->audit_context))
  1524. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1525. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1526. trace_sys_exit(regs, regs->ax);
  1527. /*
  1528. * If TIF_SYSCALL_EMU is set, we only get here because of
  1529. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1530. * We already reported this syscall instruction in
  1531. * syscall_trace_enter().
  1532. */
  1533. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  1534. !test_thread_flag(TIF_SYSCALL_EMU);
  1535. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1536. tracehook_report_syscall_exit(regs, step);
  1537. }