ptrace.c 43 KB

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