ptrace.c 44 KB

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