ptrace.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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, void *data)
  505. {
  506. int i;
  507. struct thread_struct *thread = &(current->thread);
  508. /*
  509. * Store in the virtual DR6 register the fact that the breakpoint
  510. * was hit so the thread's debugger will see it.
  511. */
  512. for (i = 0; i < HBP_NUM; i++) {
  513. if (thread->ptrace_bps[i] == bp)
  514. break;
  515. }
  516. thread->debugreg6 |= (DR_TRAP0 << i);
  517. }
  518. /*
  519. * Walk through every ptrace breakpoints for this thread and
  520. * build the dr7 value on top of their attributes.
  521. *
  522. */
  523. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  524. {
  525. int i;
  526. int dr7 = 0;
  527. struct arch_hw_breakpoint *info;
  528. for (i = 0; i < HBP_NUM; i++) {
  529. if (bp[i] && !bp[i]->attr.disabled) {
  530. info = counter_arch_bp(bp[i]);
  531. dr7 |= encode_dr7(i, info->len, info->type);
  532. }
  533. }
  534. return dr7;
  535. }
  536. static struct perf_event *
  537. ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
  538. struct task_struct *tsk)
  539. {
  540. int err;
  541. int gen_len, gen_type;
  542. DEFINE_BREAKPOINT_ATTR(attr);
  543. /*
  544. * We shoud have at least an inactive breakpoint at this
  545. * slot. It means the user is writing dr7 without having
  546. * written the address register first
  547. */
  548. if (!bp)
  549. return ERR_PTR(-EINVAL);
  550. err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
  551. if (err)
  552. return ERR_PTR(err);
  553. attr = bp->attr;
  554. attr.bp_len = gen_len;
  555. attr.bp_type = gen_type;
  556. attr.disabled = 0;
  557. return modify_user_hw_breakpoint(bp, &attr, bp->callback, tsk);
  558. }
  559. /*
  560. * Handle ptrace writes to debug register 7.
  561. */
  562. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  563. {
  564. struct thread_struct *thread = &(tsk->thread);
  565. unsigned long old_dr7;
  566. int i, orig_ret = 0, rc = 0;
  567. int enabled, second_pass = 0;
  568. unsigned len, type;
  569. struct perf_event *bp;
  570. data &= ~DR_CONTROL_RESERVED;
  571. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  572. restore:
  573. /*
  574. * Loop through all the hardware breakpoints, making the
  575. * appropriate changes to each.
  576. */
  577. for (i = 0; i < HBP_NUM; i++) {
  578. enabled = decode_dr7(data, i, &len, &type);
  579. bp = thread->ptrace_bps[i];
  580. if (!enabled) {
  581. if (bp) {
  582. /*
  583. * Don't unregister the breakpoints right-away,
  584. * unless all register_user_hw_breakpoint()
  585. * requests have succeeded. This prevents
  586. * any window of opportunity for debug
  587. * register grabbing by other users.
  588. */
  589. if (!second_pass)
  590. continue;
  591. thread->ptrace_bps[i] = NULL;
  592. unregister_hw_breakpoint(bp);
  593. }
  594. continue;
  595. }
  596. bp = ptrace_modify_breakpoint(bp, len, type, tsk);
  597. /* Incorrect bp, or we have a bug in bp API */
  598. if (IS_ERR(bp)) {
  599. rc = PTR_ERR(bp);
  600. thread->ptrace_bps[i] = NULL;
  601. break;
  602. }
  603. thread->ptrace_bps[i] = bp;
  604. }
  605. /*
  606. * Make a second pass to free the remaining unused breakpoints
  607. * or to restore the original breakpoints if an error occurred.
  608. */
  609. if (!second_pass) {
  610. second_pass = 1;
  611. if (rc < 0) {
  612. orig_ret = rc;
  613. data = old_dr7;
  614. }
  615. goto restore;
  616. }
  617. return ((orig_ret < 0) ? orig_ret : rc);
  618. }
  619. /*
  620. * Handle PTRACE_PEEKUSR calls for the debug register area.
  621. */
  622. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  623. {
  624. struct thread_struct *thread = &(tsk->thread);
  625. unsigned long val = 0;
  626. if (n < HBP_NUM) {
  627. struct perf_event *bp;
  628. bp = thread->ptrace_bps[n];
  629. if (!bp)
  630. return 0;
  631. val = bp->hw.info.address;
  632. } else if (n == 6) {
  633. val = thread->debugreg6;
  634. } else if (n == 7) {
  635. val = ptrace_get_dr7(thread->ptrace_bps);
  636. }
  637. return val;
  638. }
  639. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  640. unsigned long addr)
  641. {
  642. struct perf_event *bp;
  643. struct thread_struct *t = &tsk->thread;
  644. DEFINE_BREAKPOINT_ATTR(attr);
  645. if (!t->ptrace_bps[nr]) {
  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. } else {
  656. bp = t->ptrace_bps[nr];
  657. t->ptrace_bps[nr] = NULL;
  658. attr = bp->attr;
  659. attr.bp_addr = addr;
  660. bp = modify_user_hw_breakpoint(bp, &attr, bp->callback, tsk);
  661. }
  662. /*
  663. * CHECKME: the previous code returned -EIO if the addr wasn't a
  664. * valid task virtual addr. The new one will return -EINVAL in this
  665. * case.
  666. * -EINVAL may be what we want for in-kernel breakpoints users, but
  667. * -EIO looks better for ptrace, since we refuse a register writing
  668. * for the user. And anyway this is the previous behaviour.
  669. */
  670. if (IS_ERR(bp))
  671. return PTR_ERR(bp);
  672. t->ptrace_bps[nr] = bp;
  673. return 0;
  674. }
  675. /*
  676. * Handle PTRACE_POKEUSR calls for the debug register area.
  677. */
  678. int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
  679. {
  680. struct thread_struct *thread = &(tsk->thread);
  681. int rc = 0;
  682. /* There are no DR4 or DR5 registers */
  683. if (n == 4 || n == 5)
  684. return -EIO;
  685. if (n == 6) {
  686. thread->debugreg6 = val;
  687. goto ret_path;
  688. }
  689. if (n < HBP_NUM) {
  690. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  691. if (rc)
  692. return rc;
  693. }
  694. /* All that's left is DR7 */
  695. if (n == 7)
  696. rc = ptrace_write_dr7(tsk, val);
  697. ret_path:
  698. return rc;
  699. }
  700. /*
  701. * These access the current or another (stopped) task's io permission
  702. * bitmap for debugging or core dump.
  703. */
  704. static int ioperm_active(struct task_struct *target,
  705. const struct user_regset *regset)
  706. {
  707. return target->thread.io_bitmap_max / regset->size;
  708. }
  709. static int ioperm_get(struct task_struct *target,
  710. const struct user_regset *regset,
  711. unsigned int pos, unsigned int count,
  712. void *kbuf, void __user *ubuf)
  713. {
  714. if (!target->thread.io_bitmap_ptr)
  715. return -ENXIO;
  716. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  717. target->thread.io_bitmap_ptr,
  718. 0, IO_BITMAP_BYTES);
  719. }
  720. #ifdef CONFIG_X86_PTRACE_BTS
  721. /*
  722. * A branch trace store context.
  723. *
  724. * Contexts may only be installed by ptrace_bts_config() and only for
  725. * ptraced tasks.
  726. *
  727. * Contexts are destroyed when the tracee is detached from the tracer.
  728. * The actual destruction work requires interrupts enabled, so the
  729. * work is deferred and will be scheduled during __ptrace_unlink().
  730. *
  731. * Contexts hold an additional task_struct reference on the traced
  732. * task, as well as a reference on the tracer's mm.
  733. *
  734. * Ptrace already holds a task_struct for the duration of ptrace operations,
  735. * but since destruction is deferred, it may be executed after both
  736. * tracer and tracee exited.
  737. */
  738. struct bts_context {
  739. /* The branch trace handle. */
  740. struct bts_tracer *tracer;
  741. /* The buffer used to store the branch trace and its size. */
  742. void *buffer;
  743. unsigned int size;
  744. /* The mm that paid for the above buffer. */
  745. struct mm_struct *mm;
  746. /* The task this context belongs to. */
  747. struct task_struct *task;
  748. /* The signal to send on a bts buffer overflow. */
  749. unsigned int bts_ovfl_signal;
  750. /* The work struct to destroy a context. */
  751. struct work_struct work;
  752. };
  753. static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
  754. {
  755. void *buffer = NULL;
  756. int err = -ENOMEM;
  757. err = account_locked_memory(current->mm, current->signal->rlim, size);
  758. if (err < 0)
  759. return err;
  760. buffer = kzalloc(size, GFP_KERNEL);
  761. if (!buffer)
  762. goto out_refund;
  763. context->buffer = buffer;
  764. context->size = size;
  765. context->mm = get_task_mm(current);
  766. return 0;
  767. out_refund:
  768. refund_locked_memory(current->mm, size);
  769. return err;
  770. }
  771. static inline void free_bts_buffer(struct bts_context *context)
  772. {
  773. if (!context->buffer)
  774. return;
  775. kfree(context->buffer);
  776. context->buffer = NULL;
  777. refund_locked_memory(context->mm, context->size);
  778. context->size = 0;
  779. mmput(context->mm);
  780. context->mm = NULL;
  781. }
  782. static void free_bts_context_work(struct work_struct *w)
  783. {
  784. struct bts_context *context;
  785. context = container_of(w, struct bts_context, work);
  786. ds_release_bts(context->tracer);
  787. put_task_struct(context->task);
  788. free_bts_buffer(context);
  789. kfree(context);
  790. }
  791. static inline void free_bts_context(struct bts_context *context)
  792. {
  793. INIT_WORK(&context->work, free_bts_context_work);
  794. schedule_work(&context->work);
  795. }
  796. static inline struct bts_context *alloc_bts_context(struct task_struct *task)
  797. {
  798. struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
  799. if (context) {
  800. context->task = task;
  801. task->bts = context;
  802. get_task_struct(task);
  803. }
  804. return context;
  805. }
  806. static int ptrace_bts_read_record(struct task_struct *child, size_t index,
  807. struct bts_struct __user *out)
  808. {
  809. struct bts_context *context;
  810. const struct bts_trace *trace;
  811. struct bts_struct bts;
  812. const unsigned char *at;
  813. int error;
  814. context = child->bts;
  815. if (!context)
  816. return -ESRCH;
  817. trace = ds_read_bts(context->tracer);
  818. if (!trace)
  819. return -ESRCH;
  820. at = trace->ds.top - ((index + 1) * trace->ds.size);
  821. if ((void *)at < trace->ds.begin)
  822. at += (trace->ds.n * trace->ds.size);
  823. if (!trace->read)
  824. return -EOPNOTSUPP;
  825. error = trace->read(context->tracer, at, &bts);
  826. if (error < 0)
  827. return error;
  828. if (copy_to_user(out, &bts, sizeof(bts)))
  829. return -EFAULT;
  830. return sizeof(bts);
  831. }
  832. static int ptrace_bts_drain(struct task_struct *child,
  833. long size,
  834. struct bts_struct __user *out)
  835. {
  836. struct bts_context *context;
  837. const struct bts_trace *trace;
  838. const unsigned char *at;
  839. int error, drained = 0;
  840. context = child->bts;
  841. if (!context)
  842. return -ESRCH;
  843. trace = ds_read_bts(context->tracer);
  844. if (!trace)
  845. return -ESRCH;
  846. if (!trace->read)
  847. return -EOPNOTSUPP;
  848. if (size < (trace->ds.top - trace->ds.begin))
  849. return -EIO;
  850. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  851. out++, drained++, at += trace->ds.size) {
  852. struct bts_struct bts;
  853. error = trace->read(context->tracer, at, &bts);
  854. if (error < 0)
  855. return error;
  856. if (copy_to_user(out, &bts, sizeof(bts)))
  857. return -EFAULT;
  858. }
  859. memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
  860. error = ds_reset_bts(context->tracer);
  861. if (error < 0)
  862. return error;
  863. return drained;
  864. }
  865. static int ptrace_bts_config(struct task_struct *child,
  866. long cfg_size,
  867. const struct ptrace_bts_config __user *ucfg)
  868. {
  869. struct bts_context *context;
  870. struct ptrace_bts_config cfg;
  871. unsigned int flags = 0;
  872. if (cfg_size < sizeof(cfg))
  873. return -EIO;
  874. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  875. return -EFAULT;
  876. context = child->bts;
  877. if (!context)
  878. context = alloc_bts_context(child);
  879. if (!context)
  880. return -ENOMEM;
  881. if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
  882. if (!cfg.signal)
  883. return -EINVAL;
  884. return -EOPNOTSUPP;
  885. context->bts_ovfl_signal = cfg.signal;
  886. }
  887. ds_release_bts(context->tracer);
  888. context->tracer = NULL;
  889. if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
  890. int err;
  891. free_bts_buffer(context);
  892. if (!cfg.size)
  893. return 0;
  894. err = alloc_bts_buffer(context, cfg.size);
  895. if (err < 0)
  896. return err;
  897. }
  898. if (cfg.flags & PTRACE_BTS_O_TRACE)
  899. flags |= BTS_USER;
  900. if (cfg.flags & PTRACE_BTS_O_SCHED)
  901. flags |= BTS_TIMESTAMPS;
  902. context->tracer =
  903. ds_request_bts_task(child, context->buffer, context->size,
  904. NULL, (size_t)-1, flags);
  905. if (unlikely(IS_ERR(context->tracer))) {
  906. int error = PTR_ERR(context->tracer);
  907. free_bts_buffer(context);
  908. context->tracer = NULL;
  909. return error;
  910. }
  911. return sizeof(cfg);
  912. }
  913. static int ptrace_bts_status(struct task_struct *child,
  914. long cfg_size,
  915. struct ptrace_bts_config __user *ucfg)
  916. {
  917. struct bts_context *context;
  918. const struct bts_trace *trace;
  919. struct ptrace_bts_config cfg;
  920. context = child->bts;
  921. if (!context)
  922. return -ESRCH;
  923. if (cfg_size < sizeof(cfg))
  924. return -EIO;
  925. trace = ds_read_bts(context->tracer);
  926. if (!trace)
  927. return -ESRCH;
  928. memset(&cfg, 0, sizeof(cfg));
  929. cfg.size = trace->ds.end - trace->ds.begin;
  930. cfg.signal = context->bts_ovfl_signal;
  931. cfg.bts_size = sizeof(struct bts_struct);
  932. if (cfg.signal)
  933. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  934. if (trace->ds.flags & BTS_USER)
  935. cfg.flags |= PTRACE_BTS_O_TRACE;
  936. if (trace->ds.flags & BTS_TIMESTAMPS)
  937. cfg.flags |= PTRACE_BTS_O_SCHED;
  938. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  939. return -EFAULT;
  940. return sizeof(cfg);
  941. }
  942. static int ptrace_bts_clear(struct task_struct *child)
  943. {
  944. struct bts_context *context;
  945. const struct bts_trace *trace;
  946. context = child->bts;
  947. if (!context)
  948. return -ESRCH;
  949. trace = ds_read_bts(context->tracer);
  950. if (!trace)
  951. return -ESRCH;
  952. memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
  953. return ds_reset_bts(context->tracer);
  954. }
  955. static int ptrace_bts_size(struct task_struct *child)
  956. {
  957. struct bts_context *context;
  958. const struct bts_trace *trace;
  959. context = child->bts;
  960. if (!context)
  961. return -ESRCH;
  962. trace = ds_read_bts(context->tracer);
  963. if (!trace)
  964. return -ESRCH;
  965. return (trace->ds.top - trace->ds.begin) / trace->ds.size;
  966. }
  967. /*
  968. * Called from __ptrace_unlink() after the child has been moved back
  969. * to its original parent.
  970. */
  971. void ptrace_bts_untrace(struct task_struct *child)
  972. {
  973. if (unlikely(child->bts)) {
  974. free_bts_context(child->bts);
  975. child->bts = NULL;
  976. }
  977. }
  978. #endif /* CONFIG_X86_PTRACE_BTS */
  979. /*
  980. * Called by kernel/ptrace.c when detaching..
  981. *
  982. * Make sure the single step bit is not set.
  983. */
  984. void ptrace_disable(struct task_struct *child)
  985. {
  986. user_disable_single_step(child);
  987. #ifdef TIF_SYSCALL_EMU
  988. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  989. #endif
  990. }
  991. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  992. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  993. #endif
  994. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  995. {
  996. int ret;
  997. unsigned long __user *datap = (unsigned long __user *)data;
  998. switch (request) {
  999. /* read the word at location addr in the USER area. */
  1000. case PTRACE_PEEKUSR: {
  1001. unsigned long tmp;
  1002. ret = -EIO;
  1003. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  1004. addr >= sizeof(struct user))
  1005. break;
  1006. tmp = 0; /* Default return condition */
  1007. if (addr < sizeof(struct user_regs_struct))
  1008. tmp = getreg(child, addr);
  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. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  1013. }
  1014. ret = put_user(tmp, datap);
  1015. break;
  1016. }
  1017. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  1018. ret = -EIO;
  1019. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  1020. addr >= sizeof(struct user))
  1021. break;
  1022. if (addr < sizeof(struct user_regs_struct))
  1023. ret = putreg(child, addr, data);
  1024. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1025. addr <= offsetof(struct user, u_debugreg[7])) {
  1026. addr -= offsetof(struct user, u_debugreg[0]);
  1027. ret = ptrace_set_debugreg(child,
  1028. addr / sizeof(data), data);
  1029. }
  1030. break;
  1031. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1032. return copy_regset_to_user(child,
  1033. task_user_regset_view(current),
  1034. REGSET_GENERAL,
  1035. 0, sizeof(struct user_regs_struct),
  1036. datap);
  1037. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1038. return copy_regset_from_user(child,
  1039. task_user_regset_view(current),
  1040. REGSET_GENERAL,
  1041. 0, sizeof(struct user_regs_struct),
  1042. datap);
  1043. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1044. return copy_regset_to_user(child,
  1045. task_user_regset_view(current),
  1046. REGSET_FP,
  1047. 0, sizeof(struct user_i387_struct),
  1048. datap);
  1049. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1050. return copy_regset_from_user(child,
  1051. task_user_regset_view(current),
  1052. REGSET_FP,
  1053. 0, sizeof(struct user_i387_struct),
  1054. datap);
  1055. #ifdef CONFIG_X86_32
  1056. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1057. return copy_regset_to_user(child, &user_x86_32_view,
  1058. REGSET_XFP,
  1059. 0, sizeof(struct user_fxsr_struct),
  1060. datap) ? -EIO : 0;
  1061. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1062. return copy_regset_from_user(child, &user_x86_32_view,
  1063. REGSET_XFP,
  1064. 0, sizeof(struct user_fxsr_struct),
  1065. datap) ? -EIO : 0;
  1066. #endif
  1067. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1068. case PTRACE_GET_THREAD_AREA:
  1069. if (addr < 0)
  1070. return -EIO;
  1071. ret = do_get_thread_area(child, addr,
  1072. (struct user_desc __user *) data);
  1073. break;
  1074. case PTRACE_SET_THREAD_AREA:
  1075. if (addr < 0)
  1076. return -EIO;
  1077. ret = do_set_thread_area(child, addr,
  1078. (struct user_desc __user *) data, 0);
  1079. break;
  1080. #endif
  1081. #ifdef CONFIG_X86_64
  1082. /* normal 64bit interface to access TLS data.
  1083. Works just like arch_prctl, except that the arguments
  1084. are reversed. */
  1085. case PTRACE_ARCH_PRCTL:
  1086. ret = do_arch_prctl(child, data, addr);
  1087. break;
  1088. #endif
  1089. /*
  1090. * These bits need more cooking - not enabled yet:
  1091. */
  1092. #ifdef CONFIG_X86_PTRACE_BTS
  1093. case PTRACE_BTS_CONFIG:
  1094. ret = ptrace_bts_config
  1095. (child, data, (struct ptrace_bts_config __user *)addr);
  1096. break;
  1097. case PTRACE_BTS_STATUS:
  1098. ret = ptrace_bts_status
  1099. (child, data, (struct ptrace_bts_config __user *)addr);
  1100. break;
  1101. case PTRACE_BTS_SIZE:
  1102. ret = ptrace_bts_size(child);
  1103. break;
  1104. case PTRACE_BTS_GET:
  1105. ret = ptrace_bts_read_record
  1106. (child, data, (struct bts_struct __user *) addr);
  1107. break;
  1108. case PTRACE_BTS_CLEAR:
  1109. ret = ptrace_bts_clear(child);
  1110. break;
  1111. case PTRACE_BTS_DRAIN:
  1112. ret = ptrace_bts_drain
  1113. (child, data, (struct bts_struct __user *) addr);
  1114. break;
  1115. #endif /* CONFIG_X86_PTRACE_BTS */
  1116. default:
  1117. ret = ptrace_request(child, request, addr, data);
  1118. break;
  1119. }
  1120. return ret;
  1121. }
  1122. #ifdef CONFIG_IA32_EMULATION
  1123. #include <linux/compat.h>
  1124. #include <linux/syscalls.h>
  1125. #include <asm/ia32.h>
  1126. #include <asm/user32.h>
  1127. #define R32(l,q) \
  1128. case offsetof(struct user32, regs.l): \
  1129. regs->q = value; break
  1130. #define SEG32(rs) \
  1131. case offsetof(struct user32, regs.rs): \
  1132. return set_segment_reg(child, \
  1133. offsetof(struct user_regs_struct, rs), \
  1134. value); \
  1135. break
  1136. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  1137. {
  1138. struct pt_regs *regs = task_pt_regs(child);
  1139. switch (regno) {
  1140. SEG32(cs);
  1141. SEG32(ds);
  1142. SEG32(es);
  1143. SEG32(fs);
  1144. SEG32(gs);
  1145. SEG32(ss);
  1146. R32(ebx, bx);
  1147. R32(ecx, cx);
  1148. R32(edx, dx);
  1149. R32(edi, di);
  1150. R32(esi, si);
  1151. R32(ebp, bp);
  1152. R32(eax, ax);
  1153. R32(eip, ip);
  1154. R32(esp, sp);
  1155. case offsetof(struct user32, regs.orig_eax):
  1156. /*
  1157. * A 32-bit debugger setting orig_eax means to restore
  1158. * the state of the task restarting a 32-bit syscall.
  1159. * Make sure we interpret the -ERESTART* codes correctly
  1160. * in case the task is not actually still sitting at the
  1161. * exit from a 32-bit syscall with TS_COMPAT still set.
  1162. */
  1163. regs->orig_ax = value;
  1164. if (syscall_get_nr(child, regs) >= 0)
  1165. task_thread_info(child)->status |= TS_COMPAT;
  1166. break;
  1167. case offsetof(struct user32, regs.eflags):
  1168. return set_flags(child, value);
  1169. case offsetof(struct user32, u_debugreg[0]) ...
  1170. offsetof(struct user32, u_debugreg[7]):
  1171. regno -= offsetof(struct user32, u_debugreg[0]);
  1172. return ptrace_set_debugreg(child, regno / 4, value);
  1173. default:
  1174. if (regno > sizeof(struct user32) || (regno & 3))
  1175. return -EIO;
  1176. /*
  1177. * Other dummy fields in the virtual user structure
  1178. * are ignored
  1179. */
  1180. break;
  1181. }
  1182. return 0;
  1183. }
  1184. #undef R32
  1185. #undef SEG32
  1186. #define R32(l,q) \
  1187. case offsetof(struct user32, regs.l): \
  1188. *val = regs->q; break
  1189. #define SEG32(rs) \
  1190. case offsetof(struct user32, regs.rs): \
  1191. *val = get_segment_reg(child, \
  1192. offsetof(struct user_regs_struct, rs)); \
  1193. break
  1194. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  1195. {
  1196. struct pt_regs *regs = task_pt_regs(child);
  1197. switch (regno) {
  1198. SEG32(ds);
  1199. SEG32(es);
  1200. SEG32(fs);
  1201. SEG32(gs);
  1202. R32(cs, cs);
  1203. R32(ss, ss);
  1204. R32(ebx, bx);
  1205. R32(ecx, cx);
  1206. R32(edx, dx);
  1207. R32(edi, di);
  1208. R32(esi, si);
  1209. R32(ebp, bp);
  1210. R32(eax, ax);
  1211. R32(orig_eax, orig_ax);
  1212. R32(eip, ip);
  1213. R32(esp, sp);
  1214. case offsetof(struct user32, regs.eflags):
  1215. *val = get_flags(child);
  1216. break;
  1217. case offsetof(struct user32, u_debugreg[0]) ...
  1218. offsetof(struct user32, u_debugreg[7]):
  1219. regno -= offsetof(struct user32, u_debugreg[0]);
  1220. *val = ptrace_get_debugreg(child, regno / 4);
  1221. break;
  1222. default:
  1223. if (regno > sizeof(struct user32) || (regno & 3))
  1224. return -EIO;
  1225. /*
  1226. * Other dummy fields in the virtual user structure
  1227. * are ignored
  1228. */
  1229. *val = 0;
  1230. break;
  1231. }
  1232. return 0;
  1233. }
  1234. #undef R32
  1235. #undef SEG32
  1236. static int genregs32_get(struct task_struct *target,
  1237. const struct user_regset *regset,
  1238. unsigned int pos, unsigned int count,
  1239. void *kbuf, void __user *ubuf)
  1240. {
  1241. if (kbuf) {
  1242. compat_ulong_t *k = kbuf;
  1243. while (count > 0) {
  1244. getreg32(target, pos, k++);
  1245. count -= sizeof(*k);
  1246. pos += sizeof(*k);
  1247. }
  1248. } else {
  1249. compat_ulong_t __user *u = ubuf;
  1250. while (count > 0) {
  1251. compat_ulong_t word;
  1252. getreg32(target, pos, &word);
  1253. if (__put_user(word, u++))
  1254. return -EFAULT;
  1255. count -= sizeof(*u);
  1256. pos += sizeof(*u);
  1257. }
  1258. }
  1259. return 0;
  1260. }
  1261. static int genregs32_set(struct task_struct *target,
  1262. const struct user_regset *regset,
  1263. unsigned int pos, unsigned int count,
  1264. const void *kbuf, const void __user *ubuf)
  1265. {
  1266. int ret = 0;
  1267. if (kbuf) {
  1268. const compat_ulong_t *k = kbuf;
  1269. while (count > 0 && !ret) {
  1270. ret = putreg32(target, pos, *k++);
  1271. count -= sizeof(*k);
  1272. pos += sizeof(*k);
  1273. }
  1274. } else {
  1275. const compat_ulong_t __user *u = ubuf;
  1276. while (count > 0 && !ret) {
  1277. compat_ulong_t word;
  1278. ret = __get_user(word, u++);
  1279. if (ret)
  1280. break;
  1281. ret = putreg32(target, pos, word);
  1282. count -= sizeof(*u);
  1283. pos += sizeof(*u);
  1284. }
  1285. }
  1286. return ret;
  1287. }
  1288. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1289. compat_ulong_t caddr, compat_ulong_t cdata)
  1290. {
  1291. unsigned long addr = caddr;
  1292. unsigned long data = cdata;
  1293. void __user *datap = compat_ptr(data);
  1294. int ret;
  1295. __u32 val;
  1296. switch (request) {
  1297. case PTRACE_PEEKUSR:
  1298. ret = getreg32(child, addr, &val);
  1299. if (ret == 0)
  1300. ret = put_user(val, (__u32 __user *)datap);
  1301. break;
  1302. case PTRACE_POKEUSR:
  1303. ret = putreg32(child, addr, data);
  1304. break;
  1305. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1306. return copy_regset_to_user(child, &user_x86_32_view,
  1307. REGSET_GENERAL,
  1308. 0, sizeof(struct user_regs_struct32),
  1309. datap);
  1310. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1311. return copy_regset_from_user(child, &user_x86_32_view,
  1312. REGSET_GENERAL, 0,
  1313. sizeof(struct user_regs_struct32),
  1314. datap);
  1315. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1316. return copy_regset_to_user(child, &user_x86_32_view,
  1317. REGSET_FP, 0,
  1318. sizeof(struct user_i387_ia32_struct),
  1319. datap);
  1320. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1321. return copy_regset_from_user(
  1322. child, &user_x86_32_view, REGSET_FP,
  1323. 0, sizeof(struct user_i387_ia32_struct), datap);
  1324. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1325. return copy_regset_to_user(child, &user_x86_32_view,
  1326. REGSET_XFP, 0,
  1327. sizeof(struct user32_fxsr_struct),
  1328. datap);
  1329. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1330. return copy_regset_from_user(child, &user_x86_32_view,
  1331. REGSET_XFP, 0,
  1332. sizeof(struct user32_fxsr_struct),
  1333. datap);
  1334. case PTRACE_GET_THREAD_AREA:
  1335. case PTRACE_SET_THREAD_AREA:
  1336. #ifdef CONFIG_X86_PTRACE_BTS
  1337. case PTRACE_BTS_CONFIG:
  1338. case PTRACE_BTS_STATUS:
  1339. case PTRACE_BTS_SIZE:
  1340. case PTRACE_BTS_GET:
  1341. case PTRACE_BTS_CLEAR:
  1342. case PTRACE_BTS_DRAIN:
  1343. #endif /* CONFIG_X86_PTRACE_BTS */
  1344. return arch_ptrace(child, request, addr, data);
  1345. default:
  1346. return compat_ptrace_request(child, request, addr, data);
  1347. }
  1348. return ret;
  1349. }
  1350. #endif /* CONFIG_IA32_EMULATION */
  1351. #ifdef CONFIG_X86_64
  1352. static const struct user_regset x86_64_regsets[] = {
  1353. [REGSET_GENERAL] = {
  1354. .core_note_type = NT_PRSTATUS,
  1355. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1356. .size = sizeof(long), .align = sizeof(long),
  1357. .get = genregs_get, .set = genregs_set
  1358. },
  1359. [REGSET_FP] = {
  1360. .core_note_type = NT_PRFPREG,
  1361. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1362. .size = sizeof(long), .align = sizeof(long),
  1363. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1364. },
  1365. [REGSET_IOPERM64] = {
  1366. .core_note_type = NT_386_IOPERM,
  1367. .n = IO_BITMAP_LONGS,
  1368. .size = sizeof(long), .align = sizeof(long),
  1369. .active = ioperm_active, .get = ioperm_get
  1370. },
  1371. };
  1372. static const struct user_regset_view user_x86_64_view = {
  1373. .name = "x86_64", .e_machine = EM_X86_64,
  1374. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1375. };
  1376. #else /* CONFIG_X86_32 */
  1377. #define user_regs_struct32 user_regs_struct
  1378. #define genregs32_get genregs_get
  1379. #define genregs32_set genregs_set
  1380. #define user_i387_ia32_struct user_i387_struct
  1381. #define user32_fxsr_struct user_fxsr_struct
  1382. #endif /* CONFIG_X86_64 */
  1383. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1384. static const struct user_regset x86_32_regsets[] = {
  1385. [REGSET_GENERAL] = {
  1386. .core_note_type = NT_PRSTATUS,
  1387. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1388. .size = sizeof(u32), .align = sizeof(u32),
  1389. .get = genregs32_get, .set = genregs32_set
  1390. },
  1391. [REGSET_FP] = {
  1392. .core_note_type = NT_PRFPREG,
  1393. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1394. .size = sizeof(u32), .align = sizeof(u32),
  1395. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1396. },
  1397. [REGSET_XFP] = {
  1398. .core_note_type = NT_PRXFPREG,
  1399. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1400. .size = sizeof(u32), .align = sizeof(u32),
  1401. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1402. },
  1403. [REGSET_TLS] = {
  1404. .core_note_type = NT_386_TLS,
  1405. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1406. .size = sizeof(struct user_desc),
  1407. .align = sizeof(struct user_desc),
  1408. .active = regset_tls_active,
  1409. .get = regset_tls_get, .set = regset_tls_set
  1410. },
  1411. [REGSET_IOPERM32] = {
  1412. .core_note_type = NT_386_IOPERM,
  1413. .n = IO_BITMAP_BYTES / sizeof(u32),
  1414. .size = sizeof(u32), .align = sizeof(u32),
  1415. .active = ioperm_active, .get = ioperm_get
  1416. },
  1417. };
  1418. static const struct user_regset_view user_x86_32_view = {
  1419. .name = "i386", .e_machine = EM_386,
  1420. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1421. };
  1422. #endif
  1423. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1424. {
  1425. #ifdef CONFIG_IA32_EMULATION
  1426. if (test_tsk_thread_flag(task, TIF_IA32))
  1427. #endif
  1428. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1429. return &user_x86_32_view;
  1430. #endif
  1431. #ifdef CONFIG_X86_64
  1432. return &user_x86_64_view;
  1433. #endif
  1434. }
  1435. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1436. int error_code, int si_code)
  1437. {
  1438. struct siginfo info;
  1439. tsk->thread.trap_no = 1;
  1440. tsk->thread.error_code = error_code;
  1441. memset(&info, 0, sizeof(info));
  1442. info.si_signo = SIGTRAP;
  1443. info.si_code = si_code;
  1444. /* User-mode ip? */
  1445. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  1446. /* Send us the fake SIGTRAP */
  1447. force_sig_info(SIGTRAP, &info, tsk);
  1448. }
  1449. #ifdef CONFIG_X86_32
  1450. # define IS_IA32 1
  1451. #elif defined CONFIG_IA32_EMULATION
  1452. # define IS_IA32 is_compat_task()
  1453. #else
  1454. # define IS_IA32 0
  1455. #endif
  1456. /*
  1457. * We must return the syscall number to actually look up in the table.
  1458. * This can be -1L to skip running any syscall at all.
  1459. */
  1460. asmregparm long syscall_trace_enter(struct pt_regs *regs)
  1461. {
  1462. long ret = 0;
  1463. /*
  1464. * If we stepped into a sysenter/syscall insn, it trapped in
  1465. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1466. * If user-mode had set TF itself, then it's still clear from
  1467. * do_debug() and we need to set it again to restore the user
  1468. * state. If we entered on the slow path, TF was already set.
  1469. */
  1470. if (test_thread_flag(TIF_SINGLESTEP))
  1471. regs->flags |= X86_EFLAGS_TF;
  1472. /* do the secure computing check first */
  1473. secure_computing(regs->orig_ax);
  1474. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1475. ret = -1L;
  1476. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1477. tracehook_report_syscall_entry(regs))
  1478. ret = -1L;
  1479. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1480. trace_sys_enter(regs, regs->orig_ax);
  1481. if (unlikely(current->audit_context)) {
  1482. if (IS_IA32)
  1483. audit_syscall_entry(AUDIT_ARCH_I386,
  1484. regs->orig_ax,
  1485. regs->bx, regs->cx,
  1486. regs->dx, regs->si);
  1487. #ifdef CONFIG_X86_64
  1488. else
  1489. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1490. regs->orig_ax,
  1491. regs->di, regs->si,
  1492. regs->dx, regs->r10);
  1493. #endif
  1494. }
  1495. return ret ?: regs->orig_ax;
  1496. }
  1497. asmregparm void syscall_trace_leave(struct pt_regs *regs)
  1498. {
  1499. if (unlikely(current->audit_context))
  1500. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1501. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1502. trace_sys_exit(regs, regs->ax);
  1503. if (test_thread_flag(TIF_SYSCALL_TRACE))
  1504. tracehook_report_syscall_exit(regs, 0);
  1505. /*
  1506. * If TIF_SYSCALL_EMU is set, we only get here because of
  1507. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1508. * We already reported this syscall instruction in
  1509. * syscall_trace_enter(), so don't do any more now.
  1510. */
  1511. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1512. return;
  1513. /*
  1514. * If we are single-stepping, synthesize a trap to follow the
  1515. * system call instruction.
  1516. */
  1517. if (test_thread_flag(TIF_SINGLESTEP) &&
  1518. tracehook_consider_fatal_signal(current, SIGTRAP))
  1519. send_sigtrap(current, regs, 0, TRAP_BRKPT);
  1520. }