ptrace.c 43 KB

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