ptrace.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/regset.h>
  14. #include <linux/tracehook.h>
  15. #include <linux/user.h>
  16. #include <linux/elf.h>
  17. #include <linux/security.h>
  18. #include <linux/audit.h>
  19. #include <linux/seccomp.h>
  20. #include <linux/signal.h>
  21. #include <linux/perf_event.h>
  22. #include <linux/hw_breakpoint.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/export.h>
  25. #include <linux/context_tracking.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/processor.h>
  29. #include <asm/i387.h>
  30. #include <asm/fpu-internal.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/hw_breakpoint.h>
  37. #include <asm/traps.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. * does not yet catch signals sent when the child dies.
  134. * in exit.c or in signal.c.
  135. */
  136. /*
  137. * Determines which flags the user has access to [1 = access, 0 = no access].
  138. */
  139. #define FLAG_MASK_32 ((unsigned long) \
  140. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  141. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  142. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  143. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  144. X86_EFLAGS_RF | X86_EFLAGS_AC))
  145. /*
  146. * Determines whether a value may be installed in a segment register.
  147. */
  148. static inline bool invalid_selector(u16 value)
  149. {
  150. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  151. }
  152. #ifdef CONFIG_X86_32
  153. #define FLAG_MASK FLAG_MASK_32
  154. /*
  155. * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
  156. * when it traps. The previous stack will be directly underneath the saved
  157. * registers, and 'sp/ss' won't even have been saved. Thus the '&regs->sp'.
  158. *
  159. * Now, if the stack is empty, '&regs->sp' is out of range. In this
  160. * case we try to take the previous stack. To always return a non-null
  161. * stack pointer we fall back to regs as stack if no previous stack
  162. * exists.
  163. *
  164. * This is valid only for kernel mode traps.
  165. */
  166. unsigned long kernel_stack_pointer(struct pt_regs *regs)
  167. {
  168. unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
  169. unsigned long sp = (unsigned long)&regs->sp;
  170. struct thread_info *tinfo;
  171. if (context == (sp & ~(THREAD_SIZE - 1)))
  172. return sp;
  173. tinfo = (struct thread_info *)context;
  174. if (tinfo->previous_esp)
  175. return tinfo->previous_esp;
  176. return (unsigned long)regs;
  177. }
  178. EXPORT_SYMBOL_GPL(kernel_stack_pointer);
  179. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  180. {
  181. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  182. return &regs->bx + (regno >> 2);
  183. }
  184. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  185. {
  186. /*
  187. * Returning the value truncates it to 16 bits.
  188. */
  189. unsigned int retval;
  190. if (offset != offsetof(struct user_regs_struct, gs))
  191. retval = *pt_regs_access(task_pt_regs(task), offset);
  192. else {
  193. if (task == current)
  194. retval = get_user_gs(task_pt_regs(task));
  195. else
  196. retval = task_user_gs(task);
  197. }
  198. return retval;
  199. }
  200. static int set_segment_reg(struct task_struct *task,
  201. unsigned long offset, u16 value)
  202. {
  203. /*
  204. * The value argument was already truncated to 16 bits.
  205. */
  206. if (invalid_selector(value))
  207. return -EIO;
  208. /*
  209. * For %cs and %ss we cannot permit a null selector.
  210. * We can permit a bogus selector as long as it has USER_RPL.
  211. * Null selectors are fine for other segment registers, but
  212. * we will never get back to user mode with invalid %cs or %ss
  213. * and will take the trap in iret instead. Much code relies
  214. * on user_mode() to distinguish a user trap frame (which can
  215. * safely use invalid selectors) from a kernel trap frame.
  216. */
  217. switch (offset) {
  218. case offsetof(struct user_regs_struct, cs):
  219. case offsetof(struct user_regs_struct, ss):
  220. if (unlikely(value == 0))
  221. return -EIO;
  222. default:
  223. *pt_regs_access(task_pt_regs(task), offset) = value;
  224. break;
  225. case offsetof(struct user_regs_struct, gs):
  226. if (task == current)
  227. set_user_gs(task_pt_regs(task), value);
  228. else
  229. task_user_gs(task) = value;
  230. }
  231. return 0;
  232. }
  233. #else /* CONFIG_X86_64 */
  234. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  235. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  236. {
  237. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  238. return &regs->r15 + (offset / sizeof(regs->r15));
  239. }
  240. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  241. {
  242. /*
  243. * Returning the value truncates it to 16 bits.
  244. */
  245. unsigned int seg;
  246. switch (offset) {
  247. case offsetof(struct user_regs_struct, fs):
  248. if (task == current) {
  249. /* Older gas can't assemble movq %?s,%r?? */
  250. asm("movl %%fs,%0" : "=r" (seg));
  251. return seg;
  252. }
  253. return task->thread.fsindex;
  254. case offsetof(struct user_regs_struct, gs):
  255. if (task == current) {
  256. asm("movl %%gs,%0" : "=r" (seg));
  257. return seg;
  258. }
  259. return task->thread.gsindex;
  260. case offsetof(struct user_regs_struct, ds):
  261. if (task == current) {
  262. asm("movl %%ds,%0" : "=r" (seg));
  263. return seg;
  264. }
  265. return task->thread.ds;
  266. case offsetof(struct user_regs_struct, es):
  267. if (task == current) {
  268. asm("movl %%es,%0" : "=r" (seg));
  269. return seg;
  270. }
  271. return task->thread.es;
  272. case offsetof(struct user_regs_struct, cs):
  273. case offsetof(struct user_regs_struct, ss):
  274. break;
  275. }
  276. return *pt_regs_access(task_pt_regs(task), offset);
  277. }
  278. static int set_segment_reg(struct task_struct *task,
  279. unsigned long offset, u16 value)
  280. {
  281. /*
  282. * The value argument was already truncated to 16 bits.
  283. */
  284. if (invalid_selector(value))
  285. return -EIO;
  286. switch (offset) {
  287. case offsetof(struct user_regs_struct,fs):
  288. /*
  289. * If this is setting fs as for normal 64-bit use but
  290. * setting fs_base has implicitly changed it, leave it.
  291. */
  292. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  293. task->thread.fs != 0) ||
  294. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  295. task->thread.fs == 0))
  296. break;
  297. task->thread.fsindex = value;
  298. if (task == current)
  299. loadsegment(fs, task->thread.fsindex);
  300. break;
  301. case offsetof(struct user_regs_struct,gs):
  302. /*
  303. * If this is setting gs as for normal 64-bit use but
  304. * setting gs_base has implicitly changed it, leave it.
  305. */
  306. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  307. task->thread.gs != 0) ||
  308. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  309. task->thread.gs == 0))
  310. break;
  311. task->thread.gsindex = value;
  312. if (task == current)
  313. load_gs_index(task->thread.gsindex);
  314. break;
  315. case offsetof(struct user_regs_struct,ds):
  316. task->thread.ds = value;
  317. if (task == current)
  318. loadsegment(ds, task->thread.ds);
  319. break;
  320. case offsetof(struct user_regs_struct,es):
  321. task->thread.es = value;
  322. if (task == current)
  323. loadsegment(es, task->thread.es);
  324. break;
  325. /*
  326. * Can't actually change these in 64-bit mode.
  327. */
  328. case offsetof(struct user_regs_struct,cs):
  329. if (unlikely(value == 0))
  330. return -EIO;
  331. #ifdef CONFIG_IA32_EMULATION
  332. if (test_tsk_thread_flag(task, TIF_IA32))
  333. task_pt_regs(task)->cs = value;
  334. #endif
  335. break;
  336. case offsetof(struct user_regs_struct,ss):
  337. if (unlikely(value == 0))
  338. return -EIO;
  339. #ifdef CONFIG_IA32_EMULATION
  340. if (test_tsk_thread_flag(task, TIF_IA32))
  341. task_pt_regs(task)->ss = value;
  342. #endif
  343. break;
  344. }
  345. return 0;
  346. }
  347. #endif /* CONFIG_X86_32 */
  348. static unsigned long get_flags(struct task_struct *task)
  349. {
  350. unsigned long retval = task_pt_regs(task)->flags;
  351. /*
  352. * If the debugger set TF, hide it from the readout.
  353. */
  354. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  355. retval &= ~X86_EFLAGS_TF;
  356. return retval;
  357. }
  358. static int set_flags(struct task_struct *task, unsigned long value)
  359. {
  360. struct pt_regs *regs = task_pt_regs(task);
  361. /*
  362. * If the user value contains TF, mark that
  363. * it was not "us" (the debugger) that set it.
  364. * If not, make sure it stays set if we had.
  365. */
  366. if (value & X86_EFLAGS_TF)
  367. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  368. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  369. value |= X86_EFLAGS_TF;
  370. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  371. return 0;
  372. }
  373. static int putreg(struct task_struct *child,
  374. unsigned long offset, unsigned long value)
  375. {
  376. switch (offset) {
  377. case offsetof(struct user_regs_struct, cs):
  378. case offsetof(struct user_regs_struct, ds):
  379. case offsetof(struct user_regs_struct, es):
  380. case offsetof(struct user_regs_struct, fs):
  381. case offsetof(struct user_regs_struct, gs):
  382. case offsetof(struct user_regs_struct, ss):
  383. return set_segment_reg(child, offset, value);
  384. case offsetof(struct user_regs_struct, flags):
  385. return set_flags(child, value);
  386. #ifdef CONFIG_X86_64
  387. case offsetof(struct user_regs_struct,fs_base):
  388. if (value >= TASK_SIZE_OF(child))
  389. return -EIO;
  390. /*
  391. * When changing the segment base, use do_arch_prctl
  392. * to set either thread.fs or thread.fsindex and the
  393. * corresponding GDT slot.
  394. */
  395. if (child->thread.fs != value)
  396. return do_arch_prctl(child, ARCH_SET_FS, value);
  397. return 0;
  398. case offsetof(struct user_regs_struct,gs_base):
  399. /*
  400. * Exactly the same here as the %fs handling above.
  401. */
  402. if (value >= TASK_SIZE_OF(child))
  403. return -EIO;
  404. if (child->thread.gs != value)
  405. return do_arch_prctl(child, ARCH_SET_GS, value);
  406. return 0;
  407. #endif
  408. }
  409. *pt_regs_access(task_pt_regs(child), offset) = value;
  410. return 0;
  411. }
  412. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  413. {
  414. switch (offset) {
  415. case offsetof(struct user_regs_struct, cs):
  416. case offsetof(struct user_regs_struct, ds):
  417. case offsetof(struct user_regs_struct, es):
  418. case offsetof(struct user_regs_struct, fs):
  419. case offsetof(struct user_regs_struct, gs):
  420. case offsetof(struct user_regs_struct, ss):
  421. return get_segment_reg(task, offset);
  422. case offsetof(struct user_regs_struct, flags):
  423. return get_flags(task);
  424. #ifdef CONFIG_X86_64
  425. case offsetof(struct user_regs_struct, fs_base): {
  426. /*
  427. * do_arch_prctl may have used a GDT slot instead of
  428. * the MSR. To userland, it appears the same either
  429. * way, except the %fs segment selector might not be 0.
  430. */
  431. unsigned int seg = task->thread.fsindex;
  432. if (task->thread.fs != 0)
  433. return task->thread.fs;
  434. if (task == current)
  435. asm("movl %%fs,%0" : "=r" (seg));
  436. if (seg != FS_TLS_SEL)
  437. return 0;
  438. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  439. }
  440. case offsetof(struct user_regs_struct, gs_base): {
  441. /*
  442. * Exactly the same here as the %fs handling above.
  443. */
  444. unsigned int seg = task->thread.gsindex;
  445. if (task->thread.gs != 0)
  446. return task->thread.gs;
  447. if (task == current)
  448. asm("movl %%gs,%0" : "=r" (seg));
  449. if (seg != GS_TLS_SEL)
  450. return 0;
  451. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  452. }
  453. #endif
  454. }
  455. return *pt_regs_access(task_pt_regs(task), offset);
  456. }
  457. static int genregs_get(struct task_struct *target,
  458. const struct user_regset *regset,
  459. unsigned int pos, unsigned int count,
  460. void *kbuf, void __user *ubuf)
  461. {
  462. if (kbuf) {
  463. unsigned long *k = kbuf;
  464. while (count >= sizeof(*k)) {
  465. *k++ = getreg(target, pos);
  466. count -= sizeof(*k);
  467. pos += sizeof(*k);
  468. }
  469. } else {
  470. unsigned long __user *u = ubuf;
  471. while (count >= sizeof(*u)) {
  472. if (__put_user(getreg(target, pos), u++))
  473. return -EFAULT;
  474. count -= sizeof(*u);
  475. pos += sizeof(*u);
  476. }
  477. }
  478. return 0;
  479. }
  480. static int genregs_set(struct task_struct *target,
  481. const struct user_regset *regset,
  482. unsigned int pos, unsigned int count,
  483. const void *kbuf, const void __user *ubuf)
  484. {
  485. int ret = 0;
  486. if (kbuf) {
  487. const unsigned long *k = kbuf;
  488. while (count >= sizeof(*k) && !ret) {
  489. ret = putreg(target, pos, *k++);
  490. count -= sizeof(*k);
  491. pos += sizeof(*k);
  492. }
  493. } else {
  494. const unsigned long __user *u = ubuf;
  495. while (count >= sizeof(*u) && !ret) {
  496. unsigned long word;
  497. ret = __get_user(word, u++);
  498. if (ret)
  499. break;
  500. ret = putreg(target, pos, word);
  501. count -= sizeof(*u);
  502. pos += sizeof(*u);
  503. }
  504. }
  505. return ret;
  506. }
  507. static void ptrace_triggered(struct perf_event *bp,
  508. struct perf_sample_data *data,
  509. struct pt_regs *regs)
  510. {
  511. int i;
  512. struct thread_struct *thread = &(current->thread);
  513. /*
  514. * Store in the virtual DR6 register the fact that the breakpoint
  515. * was hit so the thread's debugger will see it.
  516. */
  517. for (i = 0; i < HBP_NUM; i++) {
  518. if (thread->ptrace_bps[i] == bp)
  519. break;
  520. }
  521. thread->debugreg6 |= (DR_TRAP0 << i);
  522. }
  523. /*
  524. * Walk through every ptrace breakpoints for this thread and
  525. * build the dr7 value on top of their attributes.
  526. *
  527. */
  528. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  529. {
  530. int i;
  531. int dr7 = 0;
  532. struct arch_hw_breakpoint *info;
  533. for (i = 0; i < HBP_NUM; i++) {
  534. if (bp[i] && !bp[i]->attr.disabled) {
  535. info = counter_arch_bp(bp[i]);
  536. dr7 |= encode_dr7(i, info->len, info->type);
  537. }
  538. }
  539. return dr7;
  540. }
  541. static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
  542. int len, int type, bool disabled)
  543. {
  544. int err, bp_len, bp_type;
  545. err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
  546. if (!err) {
  547. attr->bp_len = bp_len;
  548. attr->bp_type = bp_type;
  549. attr->disabled = disabled;
  550. }
  551. return err;
  552. }
  553. static struct perf_event *
  554. ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
  555. unsigned long addr, bool disabled)
  556. {
  557. struct perf_event_attr attr;
  558. int err;
  559. ptrace_breakpoint_init(&attr);
  560. attr.bp_addr = addr;
  561. err = ptrace_fill_bp_fields(&attr, len, type, disabled);
  562. if (err)
  563. return ERR_PTR(err);
  564. return register_user_hw_breakpoint(&attr, ptrace_triggered,
  565. NULL, tsk);
  566. }
  567. static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
  568. int disabled)
  569. {
  570. struct perf_event_attr attr = bp->attr;
  571. int err;
  572. err = ptrace_fill_bp_fields(&attr, len, type, disabled);
  573. if (err)
  574. return err;
  575. return modify_user_hw_breakpoint(bp, &attr);
  576. }
  577. /*
  578. * Handle ptrace writes to debug register 7.
  579. */
  580. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  581. {
  582. struct thread_struct *thread = &tsk->thread;
  583. unsigned long old_dr7;
  584. bool second_pass = false;
  585. int i, rc, ret = 0;
  586. data &= ~DR_CONTROL_RESERVED;
  587. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  588. restore:
  589. rc = 0;
  590. for (i = 0; i < HBP_NUM; i++) {
  591. unsigned len, type;
  592. bool disabled = !decode_dr7(data, i, &len, &type);
  593. struct perf_event *bp = thread->ptrace_bps[i];
  594. if (!bp) {
  595. if (disabled)
  596. continue;
  597. bp = ptrace_register_breakpoint(tsk,
  598. len, type, 0, disabled);
  599. if (IS_ERR(bp)) {
  600. rc = PTR_ERR(bp);
  601. break;
  602. }
  603. thread->ptrace_bps[i] = bp;
  604. continue;
  605. }
  606. rc = ptrace_modify_breakpoint(bp, len, type, disabled);
  607. if (rc)
  608. break;
  609. }
  610. /* Restore if the first pass failed, second_pass shouldn't fail. */
  611. if (rc && !WARN_ON(second_pass)) {
  612. ret = rc;
  613. data = old_dr7;
  614. second_pass = true;
  615. goto restore;
  616. }
  617. return ret;
  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 = thread->ptrace_bps[n];
  628. if (bp)
  629. val = bp->hw.info.address;
  630. } else if (n == 6) {
  631. val = thread->debugreg6;
  632. } else if (n == 7) {
  633. val = thread->ptrace_dr7;
  634. }
  635. return val;
  636. }
  637. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  638. unsigned long addr)
  639. {
  640. struct thread_struct *t = &tsk->thread;
  641. struct perf_event *bp = t->ptrace_bps[nr];
  642. int err = 0;
  643. if (!bp) {
  644. /*
  645. * Put stub len and type to create an inactive but correct bp.
  646. *
  647. * CHECKME: the previous code returned -EIO if the addr wasn't
  648. * a valid task virtual addr. The new one will return -EINVAL in
  649. * this case.
  650. * -EINVAL may be what we want for in-kernel breakpoints users,
  651. * but -EIO looks better for ptrace, since we refuse a register
  652. * writing for the user. And anyway this is the previous
  653. * behaviour.
  654. */
  655. bp = ptrace_register_breakpoint(tsk,
  656. X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
  657. addr, true);
  658. if (IS_ERR(bp))
  659. err = PTR_ERR(bp);
  660. else
  661. t->ptrace_bps[nr] = bp;
  662. } else {
  663. struct perf_event_attr attr = bp->attr;
  664. attr.bp_addr = addr;
  665. err = modify_user_hw_breakpoint(bp, &attr);
  666. }
  667. return err;
  668. }
  669. /*
  670. * Handle PTRACE_POKEUSR calls for the debug register area.
  671. */
  672. static int ptrace_set_debugreg(struct task_struct *tsk, int n,
  673. unsigned long val)
  674. {
  675. struct thread_struct *thread = &tsk->thread;
  676. /* There are no DR4 or DR5 registers */
  677. int rc = -EIO;
  678. if (n < HBP_NUM) {
  679. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  680. } else if (n == 6) {
  681. thread->debugreg6 = val;
  682. rc = 0;
  683. } else if (n == 7) {
  684. rc = ptrace_write_dr7(tsk, val);
  685. if (!rc)
  686. thread->ptrace_dr7 = val;
  687. }
  688. return rc;
  689. }
  690. /*
  691. * These access the current or another (stopped) task's io permission
  692. * bitmap for debugging or core dump.
  693. */
  694. static int ioperm_active(struct task_struct *target,
  695. const struct user_regset *regset)
  696. {
  697. return target->thread.io_bitmap_max / regset->size;
  698. }
  699. static int ioperm_get(struct task_struct *target,
  700. const struct user_regset *regset,
  701. unsigned int pos, unsigned int count,
  702. void *kbuf, void __user *ubuf)
  703. {
  704. if (!target->thread.io_bitmap_ptr)
  705. return -ENXIO;
  706. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  707. target->thread.io_bitmap_ptr,
  708. 0, IO_BITMAP_BYTES);
  709. }
  710. /*
  711. * Called by kernel/ptrace.c when detaching..
  712. *
  713. * Make sure the single step bit is not set.
  714. */
  715. void ptrace_disable(struct task_struct *child)
  716. {
  717. user_disable_single_step(child);
  718. #ifdef TIF_SYSCALL_EMU
  719. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  720. #endif
  721. }
  722. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  723. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  724. #endif
  725. long arch_ptrace(struct task_struct *child, long request,
  726. unsigned long addr, unsigned long data)
  727. {
  728. int ret;
  729. unsigned long __user *datap = (unsigned long __user *)data;
  730. switch (request) {
  731. /* read the word at location addr in the USER area. */
  732. case PTRACE_PEEKUSR: {
  733. unsigned long tmp;
  734. ret = -EIO;
  735. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  736. break;
  737. tmp = 0; /* Default return condition */
  738. if (addr < sizeof(struct user_regs_struct))
  739. tmp = getreg(child, addr);
  740. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  741. addr <= offsetof(struct user, u_debugreg[7])) {
  742. addr -= offsetof(struct user, u_debugreg[0]);
  743. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  744. }
  745. ret = put_user(tmp, datap);
  746. break;
  747. }
  748. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  749. ret = -EIO;
  750. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  751. break;
  752. if (addr < sizeof(struct user_regs_struct))
  753. ret = putreg(child, addr, data);
  754. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  755. addr <= offsetof(struct user, u_debugreg[7])) {
  756. addr -= offsetof(struct user, u_debugreg[0]);
  757. ret = ptrace_set_debugreg(child,
  758. addr / sizeof(data), data);
  759. }
  760. break;
  761. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  762. return copy_regset_to_user(child,
  763. task_user_regset_view(current),
  764. REGSET_GENERAL,
  765. 0, sizeof(struct user_regs_struct),
  766. datap);
  767. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  768. return copy_regset_from_user(child,
  769. task_user_regset_view(current),
  770. REGSET_GENERAL,
  771. 0, sizeof(struct user_regs_struct),
  772. datap);
  773. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  774. return copy_regset_to_user(child,
  775. task_user_regset_view(current),
  776. REGSET_FP,
  777. 0, sizeof(struct user_i387_struct),
  778. datap);
  779. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  780. return copy_regset_from_user(child,
  781. task_user_regset_view(current),
  782. REGSET_FP,
  783. 0, sizeof(struct user_i387_struct),
  784. datap);
  785. #ifdef CONFIG_X86_32
  786. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  787. return copy_regset_to_user(child, &user_x86_32_view,
  788. REGSET_XFP,
  789. 0, sizeof(struct user_fxsr_struct),
  790. datap) ? -EIO : 0;
  791. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  792. return copy_regset_from_user(child, &user_x86_32_view,
  793. REGSET_XFP,
  794. 0, sizeof(struct user_fxsr_struct),
  795. datap) ? -EIO : 0;
  796. #endif
  797. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  798. case PTRACE_GET_THREAD_AREA:
  799. if ((int) addr < 0)
  800. return -EIO;
  801. ret = do_get_thread_area(child, addr,
  802. (struct user_desc __user *)data);
  803. break;
  804. case PTRACE_SET_THREAD_AREA:
  805. if ((int) addr < 0)
  806. return -EIO;
  807. ret = do_set_thread_area(child, addr,
  808. (struct user_desc __user *)data, 0);
  809. break;
  810. #endif
  811. #ifdef CONFIG_X86_64
  812. /* normal 64bit interface to access TLS data.
  813. Works just like arch_prctl, except that the arguments
  814. are reversed. */
  815. case PTRACE_ARCH_PRCTL:
  816. ret = do_arch_prctl(child, data, addr);
  817. break;
  818. #endif
  819. default:
  820. ret = ptrace_request(child, request, addr, data);
  821. break;
  822. }
  823. return ret;
  824. }
  825. #ifdef CONFIG_IA32_EMULATION
  826. #include <linux/compat.h>
  827. #include <linux/syscalls.h>
  828. #include <asm/ia32.h>
  829. #include <asm/user32.h>
  830. #define R32(l,q) \
  831. case offsetof(struct user32, regs.l): \
  832. regs->q = value; break
  833. #define SEG32(rs) \
  834. case offsetof(struct user32, regs.rs): \
  835. return set_segment_reg(child, \
  836. offsetof(struct user_regs_struct, rs), \
  837. value); \
  838. break
  839. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  840. {
  841. struct pt_regs *regs = task_pt_regs(child);
  842. switch (regno) {
  843. SEG32(cs);
  844. SEG32(ds);
  845. SEG32(es);
  846. SEG32(fs);
  847. SEG32(gs);
  848. SEG32(ss);
  849. R32(ebx, bx);
  850. R32(ecx, cx);
  851. R32(edx, dx);
  852. R32(edi, di);
  853. R32(esi, si);
  854. R32(ebp, bp);
  855. R32(eax, ax);
  856. R32(eip, ip);
  857. R32(esp, sp);
  858. case offsetof(struct user32, regs.orig_eax):
  859. /*
  860. * A 32-bit debugger setting orig_eax means to restore
  861. * the state of the task restarting a 32-bit syscall.
  862. * Make sure we interpret the -ERESTART* codes correctly
  863. * in case the task is not actually still sitting at the
  864. * exit from a 32-bit syscall with TS_COMPAT still set.
  865. */
  866. regs->orig_ax = value;
  867. if (syscall_get_nr(child, regs) >= 0)
  868. task_thread_info(child)->status |= TS_COMPAT;
  869. break;
  870. case offsetof(struct user32, regs.eflags):
  871. return set_flags(child, value);
  872. case offsetof(struct user32, u_debugreg[0]) ...
  873. offsetof(struct user32, u_debugreg[7]):
  874. regno -= offsetof(struct user32, u_debugreg[0]);
  875. return ptrace_set_debugreg(child, regno / 4, value);
  876. default:
  877. if (regno > sizeof(struct user32) || (regno & 3))
  878. return -EIO;
  879. /*
  880. * Other dummy fields in the virtual user structure
  881. * are ignored
  882. */
  883. break;
  884. }
  885. return 0;
  886. }
  887. #undef R32
  888. #undef SEG32
  889. #define R32(l,q) \
  890. case offsetof(struct user32, regs.l): \
  891. *val = regs->q; break
  892. #define SEG32(rs) \
  893. case offsetof(struct user32, regs.rs): \
  894. *val = get_segment_reg(child, \
  895. offsetof(struct user_regs_struct, rs)); \
  896. break
  897. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  898. {
  899. struct pt_regs *regs = task_pt_regs(child);
  900. switch (regno) {
  901. SEG32(ds);
  902. SEG32(es);
  903. SEG32(fs);
  904. SEG32(gs);
  905. R32(cs, cs);
  906. R32(ss, ss);
  907. R32(ebx, bx);
  908. R32(ecx, cx);
  909. R32(edx, dx);
  910. R32(edi, di);
  911. R32(esi, si);
  912. R32(ebp, bp);
  913. R32(eax, ax);
  914. R32(orig_eax, orig_ax);
  915. R32(eip, ip);
  916. R32(esp, sp);
  917. case offsetof(struct user32, regs.eflags):
  918. *val = get_flags(child);
  919. break;
  920. case offsetof(struct user32, u_debugreg[0]) ...
  921. offsetof(struct user32, u_debugreg[7]):
  922. regno -= offsetof(struct user32, u_debugreg[0]);
  923. *val = ptrace_get_debugreg(child, regno / 4);
  924. break;
  925. default:
  926. if (regno > sizeof(struct user32) || (regno & 3))
  927. return -EIO;
  928. /*
  929. * Other dummy fields in the virtual user structure
  930. * are ignored
  931. */
  932. *val = 0;
  933. break;
  934. }
  935. return 0;
  936. }
  937. #undef R32
  938. #undef SEG32
  939. static int genregs32_get(struct task_struct *target,
  940. const struct user_regset *regset,
  941. unsigned int pos, unsigned int count,
  942. void *kbuf, void __user *ubuf)
  943. {
  944. if (kbuf) {
  945. compat_ulong_t *k = kbuf;
  946. while (count >= sizeof(*k)) {
  947. getreg32(target, pos, k++);
  948. count -= sizeof(*k);
  949. pos += sizeof(*k);
  950. }
  951. } else {
  952. compat_ulong_t __user *u = ubuf;
  953. while (count >= sizeof(*u)) {
  954. compat_ulong_t word;
  955. getreg32(target, pos, &word);
  956. if (__put_user(word, u++))
  957. return -EFAULT;
  958. count -= sizeof(*u);
  959. pos += sizeof(*u);
  960. }
  961. }
  962. return 0;
  963. }
  964. static int genregs32_set(struct task_struct *target,
  965. const struct user_regset *regset,
  966. unsigned int pos, unsigned int count,
  967. const void *kbuf, const void __user *ubuf)
  968. {
  969. int ret = 0;
  970. if (kbuf) {
  971. const compat_ulong_t *k = kbuf;
  972. while (count >= sizeof(*k) && !ret) {
  973. ret = putreg32(target, pos, *k++);
  974. count -= sizeof(*k);
  975. pos += sizeof(*k);
  976. }
  977. } else {
  978. const compat_ulong_t __user *u = ubuf;
  979. while (count >= sizeof(*u) && !ret) {
  980. compat_ulong_t word;
  981. ret = __get_user(word, u++);
  982. if (ret)
  983. break;
  984. ret = putreg32(target, pos, word);
  985. count -= sizeof(*u);
  986. pos += sizeof(*u);
  987. }
  988. }
  989. return ret;
  990. }
  991. #ifdef CONFIG_X86_X32_ABI
  992. static long x32_arch_ptrace(struct task_struct *child,
  993. compat_long_t request, compat_ulong_t caddr,
  994. compat_ulong_t cdata)
  995. {
  996. unsigned long addr = caddr;
  997. unsigned long data = cdata;
  998. void __user *datap = compat_ptr(data);
  999. int ret;
  1000. switch (request) {
  1001. /* Read 32bits at location addr in the USER area. Only allow
  1002. to return the lower 32bits of segment and debug registers. */
  1003. case PTRACE_PEEKUSR: {
  1004. u32 tmp;
  1005. ret = -EIO;
  1006. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
  1007. addr < offsetof(struct user_regs_struct, cs))
  1008. break;
  1009. tmp = 0; /* Default return condition */
  1010. if (addr < sizeof(struct user_regs_struct))
  1011. tmp = getreg(child, addr);
  1012. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1013. addr <= offsetof(struct user, u_debugreg[7])) {
  1014. addr -= offsetof(struct user, u_debugreg[0]);
  1015. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  1016. }
  1017. ret = put_user(tmp, (__u32 __user *)datap);
  1018. break;
  1019. }
  1020. /* Write the word at location addr in the USER area. Only allow
  1021. to update segment and debug registers with the upper 32bits
  1022. zero-extended. */
  1023. case PTRACE_POKEUSR:
  1024. ret = -EIO;
  1025. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
  1026. addr < offsetof(struct user_regs_struct, cs))
  1027. break;
  1028. if (addr < sizeof(struct user_regs_struct))
  1029. ret = putreg(child, addr, data);
  1030. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1031. addr <= offsetof(struct user, u_debugreg[7])) {
  1032. addr -= offsetof(struct user, u_debugreg[0]);
  1033. ret = ptrace_set_debugreg(child,
  1034. addr / sizeof(data), data);
  1035. }
  1036. break;
  1037. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1038. return copy_regset_to_user(child,
  1039. task_user_regset_view(current),
  1040. REGSET_GENERAL,
  1041. 0, sizeof(struct user_regs_struct),
  1042. datap);
  1043. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1044. return copy_regset_from_user(child,
  1045. task_user_regset_view(current),
  1046. REGSET_GENERAL,
  1047. 0, sizeof(struct user_regs_struct),
  1048. datap);
  1049. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1050. return copy_regset_to_user(child,
  1051. task_user_regset_view(current),
  1052. REGSET_FP,
  1053. 0, sizeof(struct user_i387_struct),
  1054. datap);
  1055. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1056. return copy_regset_from_user(child,
  1057. task_user_regset_view(current),
  1058. REGSET_FP,
  1059. 0, sizeof(struct user_i387_struct),
  1060. datap);
  1061. default:
  1062. return compat_ptrace_request(child, request, addr, data);
  1063. }
  1064. return ret;
  1065. }
  1066. #endif
  1067. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1068. compat_ulong_t caddr, compat_ulong_t cdata)
  1069. {
  1070. unsigned long addr = caddr;
  1071. unsigned long data = cdata;
  1072. void __user *datap = compat_ptr(data);
  1073. int ret;
  1074. __u32 val;
  1075. #ifdef CONFIG_X86_X32_ABI
  1076. if (!is_ia32_task())
  1077. return x32_arch_ptrace(child, request, caddr, cdata);
  1078. #endif
  1079. switch (request) {
  1080. case PTRACE_PEEKUSR:
  1081. ret = getreg32(child, addr, &val);
  1082. if (ret == 0)
  1083. ret = put_user(val, (__u32 __user *)datap);
  1084. break;
  1085. case PTRACE_POKEUSR:
  1086. ret = putreg32(child, addr, data);
  1087. break;
  1088. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1089. return copy_regset_to_user(child, &user_x86_32_view,
  1090. REGSET_GENERAL,
  1091. 0, sizeof(struct user_regs_struct32),
  1092. datap);
  1093. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1094. return copy_regset_from_user(child, &user_x86_32_view,
  1095. REGSET_GENERAL, 0,
  1096. sizeof(struct user_regs_struct32),
  1097. datap);
  1098. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1099. return copy_regset_to_user(child, &user_x86_32_view,
  1100. REGSET_FP, 0,
  1101. sizeof(struct user_i387_ia32_struct),
  1102. datap);
  1103. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1104. return copy_regset_from_user(
  1105. child, &user_x86_32_view, REGSET_FP,
  1106. 0, sizeof(struct user_i387_ia32_struct), datap);
  1107. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1108. return copy_regset_to_user(child, &user_x86_32_view,
  1109. REGSET_XFP, 0,
  1110. sizeof(struct user32_fxsr_struct),
  1111. datap);
  1112. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1113. return copy_regset_from_user(child, &user_x86_32_view,
  1114. REGSET_XFP, 0,
  1115. sizeof(struct user32_fxsr_struct),
  1116. datap);
  1117. case PTRACE_GET_THREAD_AREA:
  1118. case PTRACE_SET_THREAD_AREA:
  1119. return arch_ptrace(child, request, addr, data);
  1120. default:
  1121. return compat_ptrace_request(child, request, addr, data);
  1122. }
  1123. return ret;
  1124. }
  1125. #endif /* CONFIG_IA32_EMULATION */
  1126. #ifdef CONFIG_X86_64
  1127. static struct user_regset x86_64_regsets[] __read_mostly = {
  1128. [REGSET_GENERAL] = {
  1129. .core_note_type = NT_PRSTATUS,
  1130. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1131. .size = sizeof(long), .align = sizeof(long),
  1132. .get = genregs_get, .set = genregs_set
  1133. },
  1134. [REGSET_FP] = {
  1135. .core_note_type = NT_PRFPREG,
  1136. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1137. .size = sizeof(long), .align = sizeof(long),
  1138. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1139. },
  1140. [REGSET_XSTATE] = {
  1141. .core_note_type = NT_X86_XSTATE,
  1142. .size = sizeof(u64), .align = sizeof(u64),
  1143. .active = xstateregs_active, .get = xstateregs_get,
  1144. .set = xstateregs_set
  1145. },
  1146. [REGSET_IOPERM64] = {
  1147. .core_note_type = NT_386_IOPERM,
  1148. .n = IO_BITMAP_LONGS,
  1149. .size = sizeof(long), .align = sizeof(long),
  1150. .active = ioperm_active, .get = ioperm_get
  1151. },
  1152. };
  1153. static const struct user_regset_view user_x86_64_view = {
  1154. .name = "x86_64", .e_machine = EM_X86_64,
  1155. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1156. };
  1157. #else /* CONFIG_X86_32 */
  1158. #define user_regs_struct32 user_regs_struct
  1159. #define genregs32_get genregs_get
  1160. #define genregs32_set genregs_set
  1161. #endif /* CONFIG_X86_64 */
  1162. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1163. static struct user_regset x86_32_regsets[] __read_mostly = {
  1164. [REGSET_GENERAL] = {
  1165. .core_note_type = NT_PRSTATUS,
  1166. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1167. .size = sizeof(u32), .align = sizeof(u32),
  1168. .get = genregs32_get, .set = genregs32_set
  1169. },
  1170. [REGSET_FP] = {
  1171. .core_note_type = NT_PRFPREG,
  1172. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1173. .size = sizeof(u32), .align = sizeof(u32),
  1174. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1175. },
  1176. [REGSET_XFP] = {
  1177. .core_note_type = NT_PRXFPREG,
  1178. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1179. .size = sizeof(u32), .align = sizeof(u32),
  1180. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1181. },
  1182. [REGSET_XSTATE] = {
  1183. .core_note_type = NT_X86_XSTATE,
  1184. .size = sizeof(u64), .align = sizeof(u64),
  1185. .active = xstateregs_active, .get = xstateregs_get,
  1186. .set = xstateregs_set
  1187. },
  1188. [REGSET_TLS] = {
  1189. .core_note_type = NT_386_TLS,
  1190. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1191. .size = sizeof(struct user_desc),
  1192. .align = sizeof(struct user_desc),
  1193. .active = regset_tls_active,
  1194. .get = regset_tls_get, .set = regset_tls_set
  1195. },
  1196. [REGSET_IOPERM32] = {
  1197. .core_note_type = NT_386_IOPERM,
  1198. .n = IO_BITMAP_BYTES / sizeof(u32),
  1199. .size = sizeof(u32), .align = sizeof(u32),
  1200. .active = ioperm_active, .get = ioperm_get
  1201. },
  1202. };
  1203. static const struct user_regset_view user_x86_32_view = {
  1204. .name = "i386", .e_machine = EM_386,
  1205. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1206. };
  1207. #endif
  1208. /*
  1209. * This represents bytes 464..511 in the memory layout exported through
  1210. * the REGSET_XSTATE interface.
  1211. */
  1212. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1213. void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1214. {
  1215. #ifdef CONFIG_X86_64
  1216. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1217. #endif
  1218. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1219. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1220. #endif
  1221. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1222. }
  1223. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1224. {
  1225. #ifdef CONFIG_IA32_EMULATION
  1226. if (test_tsk_thread_flag(task, TIF_IA32))
  1227. #endif
  1228. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1229. return &user_x86_32_view;
  1230. #endif
  1231. #ifdef CONFIG_X86_64
  1232. return &user_x86_64_view;
  1233. #endif
  1234. }
  1235. static void fill_sigtrap_info(struct task_struct *tsk,
  1236. struct pt_regs *regs,
  1237. int error_code, int si_code,
  1238. struct siginfo *info)
  1239. {
  1240. tsk->thread.trap_nr = X86_TRAP_DB;
  1241. tsk->thread.error_code = error_code;
  1242. memset(info, 0, sizeof(*info));
  1243. info->si_signo = SIGTRAP;
  1244. info->si_code = si_code;
  1245. info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
  1246. }
  1247. void user_single_step_siginfo(struct task_struct *tsk,
  1248. struct pt_regs *regs,
  1249. struct siginfo *info)
  1250. {
  1251. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1252. }
  1253. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1254. int error_code, int si_code)
  1255. {
  1256. struct siginfo info;
  1257. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1258. /* Send us the fake SIGTRAP */
  1259. force_sig_info(SIGTRAP, &info, tsk);
  1260. }
  1261. #ifdef CONFIG_X86_32
  1262. # define IS_IA32 1
  1263. #elif defined CONFIG_IA32_EMULATION
  1264. # define IS_IA32 is_compat_task()
  1265. #else
  1266. # define IS_IA32 0
  1267. #endif
  1268. /*
  1269. * We must return the syscall number to actually look up in the table.
  1270. * This can be -1L to skip running any syscall at all.
  1271. */
  1272. long syscall_trace_enter(struct pt_regs *regs)
  1273. {
  1274. long ret = 0;
  1275. user_exit();
  1276. /*
  1277. * If we stepped into a sysenter/syscall insn, it trapped in
  1278. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1279. * If user-mode had set TF itself, then it's still clear from
  1280. * do_debug() and we need to set it again to restore the user
  1281. * state. If we entered on the slow path, TF was already set.
  1282. */
  1283. if (test_thread_flag(TIF_SINGLESTEP))
  1284. regs->flags |= X86_EFLAGS_TF;
  1285. /* do the secure computing check first */
  1286. if (secure_computing(regs->orig_ax)) {
  1287. /* seccomp failures shouldn't expose any additional code. */
  1288. ret = -1L;
  1289. goto out;
  1290. }
  1291. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1292. ret = -1L;
  1293. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1294. tracehook_report_syscall_entry(regs))
  1295. ret = -1L;
  1296. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1297. trace_sys_enter(regs, regs->orig_ax);
  1298. if (IS_IA32)
  1299. audit_syscall_entry(AUDIT_ARCH_I386,
  1300. regs->orig_ax,
  1301. regs->bx, regs->cx,
  1302. regs->dx, regs->si);
  1303. #ifdef CONFIG_X86_64
  1304. else
  1305. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1306. regs->orig_ax,
  1307. regs->di, regs->si,
  1308. regs->dx, regs->r10);
  1309. #endif
  1310. out:
  1311. return ret ?: regs->orig_ax;
  1312. }
  1313. void syscall_trace_leave(struct pt_regs *regs)
  1314. {
  1315. bool step;
  1316. /*
  1317. * We may come here right after calling schedule_user()
  1318. * or do_notify_resume(), in which case we can be in RCU
  1319. * user mode.
  1320. */
  1321. user_exit();
  1322. audit_syscall_exit(regs);
  1323. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1324. trace_sys_exit(regs, regs->ax);
  1325. /*
  1326. * If TIF_SYSCALL_EMU is set, we only get here because of
  1327. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1328. * We already reported this syscall instruction in
  1329. * syscall_trace_enter().
  1330. */
  1331. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  1332. !test_thread_flag(TIF_SYSCALL_EMU);
  1333. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1334. tracehook_report_syscall_exit(regs, step);
  1335. user_enter();
  1336. }