ptrace.c 42 KB

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