ptrace.c 43 KB

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