ptrace.c 43 KB

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