ptrace.c 42 KB

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