traps.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. *
  5. * Pentium III FXSR, SSE support
  6. * Gareth Hughes <[email protected]>, May 2000
  7. */
  8. /*
  9. * Handle hardware traps and faults.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/context_tracking.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/kmsan.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/kprobes.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/kgdb.h>
  21. #include <linux/kernel.h>
  22. #include <linux/export.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/uprobes.h>
  25. #include <linux/string.h>
  26. #include <linux/delay.h>
  27. #include <linux/errno.h>
  28. #include <linux/kexec.h>
  29. #include <linux/sched.h>
  30. #include <linux/sched/task_stack.h>
  31. #include <linux/timer.h>
  32. #include <linux/init.h>
  33. #include <linux/bug.h>
  34. #include <linux/nmi.h>
  35. #include <linux/mm.h>
  36. #include <linux/smp.h>
  37. #include <linux/io.h>
  38. #include <linux/hardirq.h>
  39. #include <linux/atomic.h>
  40. #include <linux/ioasid.h>
  41. #include <asm/stacktrace.h>
  42. #include <asm/processor.h>
  43. #include <asm/debugreg.h>
  44. #include <asm/realmode.h>
  45. #include <asm/text-patching.h>
  46. #include <asm/ftrace.h>
  47. #include <asm/traps.h>
  48. #include <asm/desc.h>
  49. #include <asm/fpu/api.h>
  50. #include <asm/cpu.h>
  51. #include <asm/cpu_entry_area.h>
  52. #include <asm/mce.h>
  53. #include <asm/fixmap.h>
  54. #include <asm/mach_traps.h>
  55. #include <asm/alternative.h>
  56. #include <asm/fpu/xstate.h>
  57. #include <asm/vm86.h>
  58. #include <asm/umip.h>
  59. #include <asm/insn.h>
  60. #include <asm/insn-eval.h>
  61. #include <asm/vdso.h>
  62. #include <asm/tdx.h>
  63. #include <asm/cfi.h>
  64. #ifdef CONFIG_X86_64
  65. #include <asm/x86_init.h>
  66. #include <asm/proto.h>
  67. #else
  68. #include <asm/processor-flags.h>
  69. #include <asm/setup.h>
  70. #include <asm/proto.h>
  71. #endif
  72. DECLARE_BITMAP(system_vectors, NR_VECTORS);
  73. static inline void cond_local_irq_enable(struct pt_regs *regs)
  74. {
  75. if (regs->flags & X86_EFLAGS_IF)
  76. local_irq_enable();
  77. }
  78. static inline void cond_local_irq_disable(struct pt_regs *regs)
  79. {
  80. if (regs->flags & X86_EFLAGS_IF)
  81. local_irq_disable();
  82. }
  83. __always_inline int is_valid_bugaddr(unsigned long addr)
  84. {
  85. if (addr < TASK_SIZE_MAX)
  86. return 0;
  87. /*
  88. * We got #UD, if the text isn't readable we'd have gotten
  89. * a different exception.
  90. */
  91. return *(unsigned short *)addr == INSN_UD2;
  92. }
  93. static nokprobe_inline int
  94. do_trap_no_signal(struct task_struct *tsk, int trapnr, const char *str,
  95. struct pt_regs *regs, long error_code)
  96. {
  97. if (v8086_mode(regs)) {
  98. /*
  99. * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
  100. * On nmi (interrupt 2), do_trap should not be called.
  101. */
  102. if (trapnr < X86_TRAP_UD) {
  103. if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
  104. error_code, trapnr))
  105. return 0;
  106. }
  107. } else if (!user_mode(regs)) {
  108. if (fixup_exception(regs, trapnr, error_code, 0))
  109. return 0;
  110. tsk->thread.error_code = error_code;
  111. tsk->thread.trap_nr = trapnr;
  112. die(str, regs, error_code);
  113. } else {
  114. if (fixup_vdso_exception(regs, trapnr, error_code, 0))
  115. return 0;
  116. }
  117. /*
  118. * We want error_code and trap_nr set for userspace faults and
  119. * kernelspace faults which result in die(), but not
  120. * kernelspace faults which are fixed up. die() gives the
  121. * process no chance to handle the signal and notice the
  122. * kernel fault information, so that won't result in polluting
  123. * the information about previously queued, but not yet
  124. * delivered, faults. See also exc_general_protection below.
  125. */
  126. tsk->thread.error_code = error_code;
  127. tsk->thread.trap_nr = trapnr;
  128. return -1;
  129. }
  130. static void show_signal(struct task_struct *tsk, int signr,
  131. const char *type, const char *desc,
  132. struct pt_regs *regs, long error_code)
  133. {
  134. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  135. printk_ratelimit()) {
  136. pr_info("%s[%d] %s%s ip:%lx sp:%lx error:%lx",
  137. tsk->comm, task_pid_nr(tsk), type, desc,
  138. regs->ip, regs->sp, error_code);
  139. print_vma_addr(KERN_CONT " in ", regs->ip);
  140. pr_cont("\n");
  141. }
  142. }
  143. static void
  144. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  145. long error_code, int sicode, void __user *addr)
  146. {
  147. struct task_struct *tsk = current;
  148. if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
  149. return;
  150. show_signal(tsk, signr, "trap ", str, regs, error_code);
  151. if (!sicode)
  152. force_sig(signr);
  153. else
  154. force_sig_fault(signr, sicode, addr);
  155. }
  156. NOKPROBE_SYMBOL(do_trap);
  157. static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
  158. unsigned long trapnr, int signr, int sicode, void __user *addr)
  159. {
  160. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  161. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
  162. NOTIFY_STOP) {
  163. cond_local_irq_enable(regs);
  164. do_trap(trapnr, signr, str, regs, error_code, sicode, addr);
  165. cond_local_irq_disable(regs);
  166. }
  167. }
  168. /*
  169. * Posix requires to provide the address of the faulting instruction for
  170. * SIGILL (#UD) and SIGFPE (#DE) in the si_addr member of siginfo_t.
  171. *
  172. * This address is usually regs->ip, but when an uprobe moved the code out
  173. * of line then regs->ip points to the XOL code which would confuse
  174. * anything which analyzes the fault address vs. the unmodified binary. If
  175. * a trap happened in XOL code then uprobe maps regs->ip back to the
  176. * original instruction address.
  177. */
  178. static __always_inline void __user *error_get_trap_addr(struct pt_regs *regs)
  179. {
  180. return (void __user *)uprobe_get_trap_addr(regs);
  181. }
  182. DEFINE_IDTENTRY(exc_divide_error)
  183. {
  184. do_error_trap(regs, 0, "divide error", X86_TRAP_DE, SIGFPE,
  185. FPE_INTDIV, error_get_trap_addr(regs));
  186. }
  187. DEFINE_IDTENTRY(exc_overflow)
  188. {
  189. do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
  190. }
  191. #ifdef CONFIG_X86_KERNEL_IBT
  192. static __ro_after_init bool ibt_fatal = true;
  193. extern void ibt_selftest_ip(void); /* code label defined in asm below */
  194. enum cp_error_code {
  195. CP_EC = (1 << 15) - 1,
  196. CP_RET = 1,
  197. CP_IRET = 2,
  198. CP_ENDBR = 3,
  199. CP_RSTRORSSP = 4,
  200. CP_SETSSBSY = 5,
  201. CP_ENCL = 1 << 15,
  202. };
  203. DEFINE_IDTENTRY_ERRORCODE(exc_control_protection)
  204. {
  205. if (!cpu_feature_enabled(X86_FEATURE_IBT)) {
  206. pr_err("Unexpected #CP\n");
  207. BUG();
  208. }
  209. if (WARN_ON_ONCE(user_mode(regs) || (error_code & CP_EC) != CP_ENDBR))
  210. return;
  211. if (unlikely(regs->ip == (unsigned long)&ibt_selftest_ip)) {
  212. regs->ax = 0;
  213. return;
  214. }
  215. pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
  216. if (!ibt_fatal) {
  217. printk(KERN_DEFAULT CUT_HERE);
  218. __warn(__FILE__, __LINE__, (void *)regs->ip, TAINT_WARN, regs, NULL);
  219. return;
  220. }
  221. BUG();
  222. }
  223. /* Must be noinline to ensure uniqueness of ibt_selftest_ip. */
  224. noinline bool ibt_selftest(void)
  225. {
  226. unsigned long ret;
  227. asm (" lea ibt_selftest_ip(%%rip), %%rax\n\t"
  228. ANNOTATE_RETPOLINE_SAFE
  229. " jmp *%%rax\n\t"
  230. "ibt_selftest_ip:\n\t"
  231. UNWIND_HINT_FUNC
  232. ANNOTATE_NOENDBR
  233. " nop\n\t"
  234. : "=a" (ret) : : "memory");
  235. return !ret;
  236. }
  237. static int __init ibt_setup(char *str)
  238. {
  239. if (!strcmp(str, "off"))
  240. setup_clear_cpu_cap(X86_FEATURE_IBT);
  241. if (!strcmp(str, "warn"))
  242. ibt_fatal = false;
  243. return 1;
  244. }
  245. __setup("ibt=", ibt_setup);
  246. #endif /* CONFIG_X86_KERNEL_IBT */
  247. #ifdef CONFIG_X86_F00F_BUG
  248. void handle_invalid_op(struct pt_regs *regs)
  249. #else
  250. static inline void handle_invalid_op(struct pt_regs *regs)
  251. #endif
  252. {
  253. do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
  254. ILL_ILLOPN, error_get_trap_addr(regs));
  255. }
  256. static noinstr bool handle_bug(struct pt_regs *regs)
  257. {
  258. bool handled = false;
  259. /*
  260. * Normally @regs are unpoisoned by irqentry_enter(), but handle_bug()
  261. * is a rare case that uses @regs without passing them to
  262. * irqentry_enter().
  263. */
  264. kmsan_unpoison_entry_regs(regs);
  265. if (!is_valid_bugaddr(regs->ip))
  266. return handled;
  267. /*
  268. * All lies, just get the WARN/BUG out.
  269. */
  270. instrumentation_begin();
  271. /*
  272. * Since we're emulating a CALL with exceptions, restore the interrupt
  273. * state to what it was at the exception site.
  274. */
  275. if (regs->flags & X86_EFLAGS_IF)
  276. raw_local_irq_enable();
  277. if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN ||
  278. handle_cfi_failure(regs) == BUG_TRAP_TYPE_WARN) {
  279. regs->ip += LEN_UD2;
  280. handled = true;
  281. }
  282. if (regs->flags & X86_EFLAGS_IF)
  283. raw_local_irq_disable();
  284. instrumentation_end();
  285. return handled;
  286. }
  287. DEFINE_IDTENTRY_RAW(exc_invalid_op)
  288. {
  289. irqentry_state_t state;
  290. /*
  291. * We use UD2 as a short encoding for 'CALL __WARN', as such
  292. * handle it before exception entry to avoid recursive WARN
  293. * in case exception entry is the one triggering WARNs.
  294. */
  295. if (!user_mode(regs) && handle_bug(regs))
  296. return;
  297. state = irqentry_enter(regs);
  298. instrumentation_begin();
  299. handle_invalid_op(regs);
  300. instrumentation_end();
  301. irqentry_exit(regs, state);
  302. }
  303. DEFINE_IDTENTRY(exc_coproc_segment_overrun)
  304. {
  305. do_error_trap(regs, 0, "coprocessor segment overrun",
  306. X86_TRAP_OLD_MF, SIGFPE, 0, NULL);
  307. }
  308. DEFINE_IDTENTRY_ERRORCODE(exc_invalid_tss)
  309. {
  310. do_error_trap(regs, error_code, "invalid TSS", X86_TRAP_TS, SIGSEGV,
  311. 0, NULL);
  312. }
  313. DEFINE_IDTENTRY_ERRORCODE(exc_segment_not_present)
  314. {
  315. do_error_trap(regs, error_code, "segment not present", X86_TRAP_NP,
  316. SIGBUS, 0, NULL);
  317. }
  318. DEFINE_IDTENTRY_ERRORCODE(exc_stack_segment)
  319. {
  320. do_error_trap(regs, error_code, "stack segment", X86_TRAP_SS, SIGBUS,
  321. 0, NULL);
  322. }
  323. DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check)
  324. {
  325. char *str = "alignment check";
  326. if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP)
  327. return;
  328. if (!user_mode(regs))
  329. die("Split lock detected\n", regs, error_code);
  330. local_irq_enable();
  331. if (handle_user_split_lock(regs, error_code))
  332. goto out;
  333. do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs,
  334. error_code, BUS_ADRALN, NULL);
  335. out:
  336. local_irq_disable();
  337. }
  338. #ifdef CONFIG_VMAP_STACK
  339. __visible void __noreturn handle_stack_overflow(struct pt_regs *regs,
  340. unsigned long fault_address,
  341. struct stack_info *info)
  342. {
  343. const char *name = stack_type_name(info->type);
  344. printk(KERN_EMERG "BUG: %s stack guard page was hit at %p (stack is %p..%p)\n",
  345. name, (void *)fault_address, info->begin, info->end);
  346. die("stack guard page", regs, 0);
  347. /* Be absolutely certain we don't return. */
  348. panic("%s stack guard hit", name);
  349. }
  350. #endif
  351. /*
  352. * Runs on an IST stack for x86_64 and on a special task stack for x86_32.
  353. *
  354. * On x86_64, this is more or less a normal kernel entry. Notwithstanding the
  355. * SDM's warnings about double faults being unrecoverable, returning works as
  356. * expected. Presumably what the SDM actually means is that the CPU may get
  357. * the register state wrong on entry, so returning could be a bad idea.
  358. *
  359. * Various CPU engineers have promised that double faults due to an IRET fault
  360. * while the stack is read-only are, in fact, recoverable.
  361. *
  362. * On x86_32, this is entered through a task gate, and regs are synthesized
  363. * from the TSS. Returning is, in principle, okay, but changes to regs will
  364. * be lost. If, for some reason, we need to return to a context with modified
  365. * regs, the shim code could be adjusted to synchronize the registers.
  366. *
  367. * The 32bit #DF shim provides CR2 already as an argument. On 64bit it needs
  368. * to be read before doing anything else.
  369. */
  370. DEFINE_IDTENTRY_DF(exc_double_fault)
  371. {
  372. static const char str[] = "double fault";
  373. struct task_struct *tsk = current;
  374. #ifdef CONFIG_VMAP_STACK
  375. unsigned long address = read_cr2();
  376. struct stack_info info;
  377. #endif
  378. #ifdef CONFIG_X86_ESPFIX64
  379. extern unsigned char native_irq_return_iret[];
  380. /*
  381. * If IRET takes a non-IST fault on the espfix64 stack, then we
  382. * end up promoting it to a doublefault. In that case, take
  383. * advantage of the fact that we're not using the normal (TSS.sp0)
  384. * stack right now. We can write a fake #GP(0) frame at TSS.sp0
  385. * and then modify our own IRET frame so that, when we return,
  386. * we land directly at the #GP(0) vector with the stack already
  387. * set up according to its expectations.
  388. *
  389. * The net result is that our #GP handler will think that we
  390. * entered from usermode with the bad user context.
  391. *
  392. * No need for nmi_enter() here because we don't use RCU.
  393. */
  394. if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
  395. regs->cs == __KERNEL_CS &&
  396. regs->ip == (unsigned long)native_irq_return_iret)
  397. {
  398. struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
  399. unsigned long *p = (unsigned long *)regs->sp;
  400. /*
  401. * regs->sp points to the failing IRET frame on the
  402. * ESPFIX64 stack. Copy it to the entry stack. This fills
  403. * in gpregs->ss through gpregs->ip.
  404. *
  405. */
  406. gpregs->ip = p[0];
  407. gpregs->cs = p[1];
  408. gpregs->flags = p[2];
  409. gpregs->sp = p[3];
  410. gpregs->ss = p[4];
  411. gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
  412. /*
  413. * Adjust our frame so that we return straight to the #GP
  414. * vector with the expected RSP value. This is safe because
  415. * we won't enable interrupts or schedule before we invoke
  416. * general_protection, so nothing will clobber the stack
  417. * frame we just set up.
  418. *
  419. * We will enter general_protection with kernel GSBASE,
  420. * which is what the stub expects, given that the faulting
  421. * RIP will be the IRET instruction.
  422. */
  423. regs->ip = (unsigned long)asm_exc_general_protection;
  424. regs->sp = (unsigned long)&gpregs->orig_ax;
  425. return;
  426. }
  427. #endif
  428. irqentry_nmi_enter(regs);
  429. instrumentation_begin();
  430. notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
  431. tsk->thread.error_code = error_code;
  432. tsk->thread.trap_nr = X86_TRAP_DF;
  433. #ifdef CONFIG_VMAP_STACK
  434. /*
  435. * If we overflow the stack into a guard page, the CPU will fail
  436. * to deliver #PF and will send #DF instead. Similarly, if we
  437. * take any non-IST exception while too close to the bottom of
  438. * the stack, the processor will get a page fault while
  439. * delivering the exception and will generate a double fault.
  440. *
  441. * According to the SDM (footnote in 6.15 under "Interrupt 14 -
  442. * Page-Fault Exception (#PF):
  443. *
  444. * Processors update CR2 whenever a page fault is detected. If a
  445. * second page fault occurs while an earlier page fault is being
  446. * delivered, the faulting linear address of the second fault will
  447. * overwrite the contents of CR2 (replacing the previous
  448. * address). These updates to CR2 occur even if the page fault
  449. * results in a double fault or occurs during the delivery of a
  450. * double fault.
  451. *
  452. * The logic below has a small possibility of incorrectly diagnosing
  453. * some errors as stack overflows. For example, if the IDT or GDT
  454. * gets corrupted such that #GP delivery fails due to a bad descriptor
  455. * causing #GP and we hit this condition while CR2 coincidentally
  456. * points to the stack guard page, we'll think we overflowed the
  457. * stack. Given that we're going to panic one way or another
  458. * if this happens, this isn't necessarily worth fixing.
  459. *
  460. * If necessary, we could improve the test by only diagnosing
  461. * a stack overflow if the saved RSP points within 47 bytes of
  462. * the bottom of the stack: if RSP == tsk_stack + 48 and we
  463. * take an exception, the stack is already aligned and there
  464. * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
  465. * possible error code, so a stack overflow would *not* double
  466. * fault. With any less space left, exception delivery could
  467. * fail, and, as a practical matter, we've overflowed the
  468. * stack even if the actual trigger for the double fault was
  469. * something else.
  470. */
  471. if (get_stack_guard_info((void *)address, &info))
  472. handle_stack_overflow(regs, address, &info);
  473. #endif
  474. pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
  475. die("double fault", regs, error_code);
  476. panic("Machine halted.");
  477. instrumentation_end();
  478. }
  479. DEFINE_IDTENTRY(exc_bounds)
  480. {
  481. if (notify_die(DIE_TRAP, "bounds", regs, 0,
  482. X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
  483. return;
  484. cond_local_irq_enable(regs);
  485. if (!user_mode(regs))
  486. die("bounds", regs, 0);
  487. do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL);
  488. cond_local_irq_disable(regs);
  489. }
  490. enum kernel_gp_hint {
  491. GP_NO_HINT,
  492. GP_NON_CANONICAL,
  493. GP_CANONICAL
  494. };
  495. /*
  496. * When an uncaught #GP occurs, try to determine the memory address accessed by
  497. * the instruction and return that address to the caller. Also, try to figure
  498. * out whether any part of the access to that address was non-canonical.
  499. */
  500. static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
  501. unsigned long *addr)
  502. {
  503. u8 insn_buf[MAX_INSN_SIZE];
  504. struct insn insn;
  505. int ret;
  506. if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip,
  507. MAX_INSN_SIZE))
  508. return GP_NO_HINT;
  509. ret = insn_decode_kernel(&insn, insn_buf);
  510. if (ret < 0)
  511. return GP_NO_HINT;
  512. *addr = (unsigned long)insn_get_addr_ref(&insn, regs);
  513. if (*addr == -1UL)
  514. return GP_NO_HINT;
  515. #ifdef CONFIG_X86_64
  516. /*
  517. * Check that:
  518. * - the operand is not in the kernel half
  519. * - the last byte of the operand is not in the user canonical half
  520. */
  521. if (*addr < ~__VIRTUAL_MASK &&
  522. *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
  523. return GP_NON_CANONICAL;
  524. #endif
  525. return GP_CANONICAL;
  526. }
  527. #define GPFSTR "general protection fault"
  528. static bool fixup_iopl_exception(struct pt_regs *regs)
  529. {
  530. struct thread_struct *t = &current->thread;
  531. unsigned char byte;
  532. unsigned long ip;
  533. if (!IS_ENABLED(CONFIG_X86_IOPL_IOPERM) || t->iopl_emul != 3)
  534. return false;
  535. if (insn_get_effective_ip(regs, &ip))
  536. return false;
  537. if (get_user(byte, (const char __user *)ip))
  538. return false;
  539. if (byte != 0xfa && byte != 0xfb)
  540. return false;
  541. if (!t->iopl_warn && printk_ratelimit()) {
  542. pr_err("%s[%d] attempts to use CLI/STI, pretending it's a NOP, ip:%lx",
  543. current->comm, task_pid_nr(current), ip);
  544. print_vma_addr(KERN_CONT " in ", ip);
  545. pr_cont("\n");
  546. t->iopl_warn = 1;
  547. }
  548. regs->ip += 1;
  549. return true;
  550. }
  551. /*
  552. * The unprivileged ENQCMD instruction generates #GPs if the
  553. * IA32_PASID MSR has not been populated. If possible, populate
  554. * the MSR from a PASID previously allocated to the mm.
  555. */
  556. static bool try_fixup_enqcmd_gp(void)
  557. {
  558. #ifdef CONFIG_IOMMU_SVA
  559. u32 pasid;
  560. /*
  561. * MSR_IA32_PASID is managed using XSAVE. Directly
  562. * writing to the MSR is only possible when fpregs
  563. * are valid and the fpstate is not. This is
  564. * guaranteed when handling a userspace exception
  565. * in *before* interrupts are re-enabled.
  566. */
  567. lockdep_assert_irqs_disabled();
  568. /*
  569. * Hardware without ENQCMD will not generate
  570. * #GPs that can be fixed up here.
  571. */
  572. if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
  573. return false;
  574. pasid = current->mm->pasid;
  575. /*
  576. * If the mm has not been allocated a
  577. * PASID, the #GP can not be fixed up.
  578. */
  579. if (!pasid_valid(pasid))
  580. return false;
  581. /*
  582. * Did this thread already have its PASID activated?
  583. * If so, the #GP must be from something else.
  584. */
  585. if (current->pasid_activated)
  586. return false;
  587. wrmsrl(MSR_IA32_PASID, pasid | MSR_IA32_PASID_VALID);
  588. current->pasid_activated = 1;
  589. return true;
  590. #else
  591. return false;
  592. #endif
  593. }
  594. static bool gp_try_fixup_and_notify(struct pt_regs *regs, int trapnr,
  595. unsigned long error_code, const char *str,
  596. unsigned long address)
  597. {
  598. if (fixup_exception(regs, trapnr, error_code, address))
  599. return true;
  600. current->thread.error_code = error_code;
  601. current->thread.trap_nr = trapnr;
  602. /*
  603. * To be potentially processing a kprobe fault and to trust the result
  604. * from kprobe_running(), we have to be non-preemptible.
  605. */
  606. if (!preemptible() && kprobe_running() &&
  607. kprobe_fault_handler(regs, trapnr))
  608. return true;
  609. return notify_die(DIE_GPF, str, regs, error_code, trapnr, SIGSEGV) == NOTIFY_STOP;
  610. }
  611. static void gp_user_force_sig_segv(struct pt_regs *regs, int trapnr,
  612. unsigned long error_code, const char *str)
  613. {
  614. current->thread.error_code = error_code;
  615. current->thread.trap_nr = trapnr;
  616. show_signal(current, SIGSEGV, "", str, regs, error_code);
  617. force_sig(SIGSEGV);
  618. }
  619. DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
  620. {
  621. char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
  622. enum kernel_gp_hint hint = GP_NO_HINT;
  623. unsigned long gp_addr;
  624. if (user_mode(regs) && try_fixup_enqcmd_gp())
  625. return;
  626. cond_local_irq_enable(regs);
  627. if (static_cpu_has(X86_FEATURE_UMIP)) {
  628. if (user_mode(regs) && fixup_umip_exception(regs))
  629. goto exit;
  630. }
  631. if (v8086_mode(regs)) {
  632. local_irq_enable();
  633. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  634. local_irq_disable();
  635. return;
  636. }
  637. if (user_mode(regs)) {
  638. if (fixup_iopl_exception(regs))
  639. goto exit;
  640. if (fixup_vdso_exception(regs, X86_TRAP_GP, error_code, 0))
  641. goto exit;
  642. gp_user_force_sig_segv(regs, X86_TRAP_GP, error_code, desc);
  643. goto exit;
  644. }
  645. if (gp_try_fixup_and_notify(regs, X86_TRAP_GP, error_code, desc, 0))
  646. goto exit;
  647. if (error_code)
  648. snprintf(desc, sizeof(desc), "segment-related " GPFSTR);
  649. else
  650. hint = get_kernel_gp_address(regs, &gp_addr);
  651. if (hint != GP_NO_HINT)
  652. snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
  653. (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
  654. : "maybe for address",
  655. gp_addr);
  656. /*
  657. * KASAN is interested only in the non-canonical case, clear it
  658. * otherwise.
  659. */
  660. if (hint != GP_NON_CANONICAL)
  661. gp_addr = 0;
  662. die_addr(desc, regs, error_code, gp_addr);
  663. exit:
  664. cond_local_irq_disable(regs);
  665. }
  666. static bool do_int3(struct pt_regs *regs)
  667. {
  668. int res;
  669. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  670. if (kgdb_ll_trap(DIE_INT3, "int3", regs, 0, X86_TRAP_BP,
  671. SIGTRAP) == NOTIFY_STOP)
  672. return true;
  673. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  674. #ifdef CONFIG_KPROBES
  675. if (kprobe_int3_handler(regs))
  676. return true;
  677. #endif
  678. res = notify_die(DIE_INT3, "int3", regs, 0, X86_TRAP_BP, SIGTRAP);
  679. return res == NOTIFY_STOP;
  680. }
  681. NOKPROBE_SYMBOL(do_int3);
  682. static void do_int3_user(struct pt_regs *regs)
  683. {
  684. if (do_int3(regs))
  685. return;
  686. cond_local_irq_enable(regs);
  687. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, 0, 0, NULL);
  688. cond_local_irq_disable(regs);
  689. }
  690. DEFINE_IDTENTRY_RAW(exc_int3)
  691. {
  692. /*
  693. * poke_int3_handler() is completely self contained code; it does (and
  694. * must) *NOT* call out to anything, lest it hits upon yet another
  695. * INT3.
  696. */
  697. if (poke_int3_handler(regs))
  698. return;
  699. /*
  700. * irqentry_enter_from_user_mode() uses static_branch_{,un}likely()
  701. * and therefore can trigger INT3, hence poke_int3_handler() must
  702. * be done before. If the entry came from kernel mode, then use
  703. * nmi_enter() because the INT3 could have been hit in any context
  704. * including NMI.
  705. */
  706. if (user_mode(regs)) {
  707. irqentry_enter_from_user_mode(regs);
  708. instrumentation_begin();
  709. do_int3_user(regs);
  710. instrumentation_end();
  711. irqentry_exit_to_user_mode(regs);
  712. } else {
  713. irqentry_state_t irq_state = irqentry_nmi_enter(regs);
  714. instrumentation_begin();
  715. if (!do_int3(regs))
  716. die("int3", regs, 0);
  717. instrumentation_end();
  718. irqentry_nmi_exit(regs, irq_state);
  719. }
  720. }
  721. #ifdef CONFIG_X86_64
  722. /*
  723. * Help handler running on a per-cpu (IST or entry trampoline) stack
  724. * to switch to the normal thread stack if the interrupted code was in
  725. * user mode. The actual stack switch is done in entry_64.S
  726. */
  727. asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs)
  728. {
  729. struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
  730. if (regs != eregs)
  731. *regs = *eregs;
  732. return regs;
  733. }
  734. #ifdef CONFIG_AMD_MEM_ENCRYPT
  735. asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *regs)
  736. {
  737. unsigned long sp, *stack;
  738. struct stack_info info;
  739. struct pt_regs *regs_ret;
  740. /*
  741. * In the SYSCALL entry path the RSP value comes from user-space - don't
  742. * trust it and switch to the current kernel stack
  743. */
  744. if (ip_within_syscall_gap(regs)) {
  745. sp = this_cpu_read(cpu_current_top_of_stack);
  746. goto sync;
  747. }
  748. /*
  749. * From here on the RSP value is trusted. Now check whether entry
  750. * happened from a safe stack. Not safe are the entry or unknown stacks,
  751. * use the fall-back stack instead in this case.
  752. */
  753. sp = regs->sp;
  754. stack = (unsigned long *)sp;
  755. if (!get_stack_info_noinstr(stack, current, &info) || info.type == STACK_TYPE_ENTRY ||
  756. info.type > STACK_TYPE_EXCEPTION_LAST)
  757. sp = __this_cpu_ist_top_va(VC2);
  758. sync:
  759. /*
  760. * Found a safe stack - switch to it as if the entry didn't happen via
  761. * IST stack. The code below only copies pt_regs, the real switch happens
  762. * in assembly code.
  763. */
  764. sp = ALIGN_DOWN(sp, 8) - sizeof(*regs_ret);
  765. regs_ret = (struct pt_regs *)sp;
  766. *regs_ret = *regs;
  767. return regs_ret;
  768. }
  769. #endif
  770. asmlinkage __visible noinstr struct pt_regs *fixup_bad_iret(struct pt_regs *bad_regs)
  771. {
  772. struct pt_regs tmp, *new_stack;
  773. /*
  774. * This is called from entry_64.S early in handling a fault
  775. * caused by a bad iret to user mode. To handle the fault
  776. * correctly, we want to move our stack frame to where it would
  777. * be had we entered directly on the entry stack (rather than
  778. * just below the IRET frame) and we want to pretend that the
  779. * exception came from the IRET target.
  780. */
  781. new_stack = (struct pt_regs *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
  782. /* Copy the IRET target to the temporary storage. */
  783. __memcpy(&tmp.ip, (void *)bad_regs->sp, 5*8);
  784. /* Copy the remainder of the stack from the current stack. */
  785. __memcpy(&tmp, bad_regs, offsetof(struct pt_regs, ip));
  786. /* Update the entry stack */
  787. __memcpy(new_stack, &tmp, sizeof(tmp));
  788. BUG_ON(!user_mode(new_stack));
  789. return new_stack;
  790. }
  791. #endif
  792. static bool is_sysenter_singlestep(struct pt_regs *regs)
  793. {
  794. /*
  795. * We don't try for precision here. If we're anywhere in the region of
  796. * code that can be single-stepped in the SYSENTER entry path, then
  797. * assume that this is a useless single-step trap due to SYSENTER
  798. * being invoked with TF set. (We don't know in advance exactly
  799. * which instructions will be hit because BTF could plausibly
  800. * be set.)
  801. */
  802. #ifdef CONFIG_X86_32
  803. return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
  804. (unsigned long)__end_SYSENTER_singlestep_region -
  805. (unsigned long)__begin_SYSENTER_singlestep_region;
  806. #elif defined(CONFIG_IA32_EMULATION)
  807. return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
  808. (unsigned long)__end_entry_SYSENTER_compat -
  809. (unsigned long)entry_SYSENTER_compat;
  810. #else
  811. return false;
  812. #endif
  813. }
  814. static __always_inline unsigned long debug_read_clear_dr6(void)
  815. {
  816. unsigned long dr6;
  817. /*
  818. * The Intel SDM says:
  819. *
  820. * Certain debug exceptions may clear bits 0-3. The remaining
  821. * contents of the DR6 register are never cleared by the
  822. * processor. To avoid confusion in identifying debug
  823. * exceptions, debug handlers should clear the register before
  824. * returning to the interrupted task.
  825. *
  826. * Keep it simple: clear DR6 immediately.
  827. */
  828. get_debugreg(dr6, 6);
  829. set_debugreg(DR6_RESERVED, 6);
  830. dr6 ^= DR6_RESERVED; /* Flip to positive polarity */
  831. return dr6;
  832. }
  833. /*
  834. * Our handling of the processor debug registers is non-trivial.
  835. * We do not clear them on entry and exit from the kernel. Therefore
  836. * it is possible to get a watchpoint trap here from inside the kernel.
  837. * However, the code in ./ptrace.c has ensured that the user can
  838. * only set watchpoints on userspace addresses. Therefore the in-kernel
  839. * watchpoint trap can only occur in code which is reading/writing
  840. * from user space. Such code must not hold kernel locks (since it
  841. * can equally take a page fault), therefore it is safe to call
  842. * force_sig_info even though that claims and releases locks.
  843. *
  844. * Code in ./signal.c ensures that the debug control register
  845. * is restored before we deliver any signal, and therefore that
  846. * user code runs with the correct debug control register even though
  847. * we clear it here.
  848. *
  849. * Being careful here means that we don't have to be as careful in a
  850. * lot of more complicated places (task switching can be a bit lazy
  851. * about restoring all the debug state, and ptrace doesn't have to
  852. * find every occurrence of the TF bit that could be saved away even
  853. * by user code)
  854. *
  855. * May run on IST stack.
  856. */
  857. static bool notify_debug(struct pt_regs *regs, unsigned long *dr6)
  858. {
  859. /*
  860. * Notifiers will clear bits in @dr6 to indicate the event has been
  861. * consumed - hw_breakpoint_handler(), single_stop_cont().
  862. *
  863. * Notifiers will set bits in @virtual_dr6 to indicate the desire
  864. * for signals - ptrace_triggered(), kgdb_hw_overflow_handler().
  865. */
  866. if (notify_die(DIE_DEBUG, "debug", regs, (long)dr6, 0, SIGTRAP) == NOTIFY_STOP)
  867. return true;
  868. return false;
  869. }
  870. static __always_inline void exc_debug_kernel(struct pt_regs *regs,
  871. unsigned long dr6)
  872. {
  873. /*
  874. * Disable breakpoints during exception handling; recursive exceptions
  875. * are exceedingly 'fun'.
  876. *
  877. * Since this function is NOKPROBE, and that also applies to
  878. * HW_BREAKPOINT_X, we can't hit a breakpoint before this (XXX except a
  879. * HW_BREAKPOINT_W on our stack)
  880. *
  881. * Entry text is excluded for HW_BP_X and cpu_entry_area, which
  882. * includes the entry stack is excluded for everything.
  883. */
  884. unsigned long dr7 = local_db_save();
  885. irqentry_state_t irq_state = irqentry_nmi_enter(regs);
  886. instrumentation_begin();
  887. /*
  888. * If something gets miswired and we end up here for a user mode
  889. * #DB, we will malfunction.
  890. */
  891. WARN_ON_ONCE(user_mode(regs));
  892. if (test_thread_flag(TIF_BLOCKSTEP)) {
  893. /*
  894. * The SDM says "The processor clears the BTF flag when it
  895. * generates a debug exception." but PTRACE_BLOCKSTEP requested
  896. * it for userspace, but we just took a kernel #DB, so re-set
  897. * BTF.
  898. */
  899. unsigned long debugctl;
  900. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  901. debugctl |= DEBUGCTLMSR_BTF;
  902. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  903. }
  904. /*
  905. * Catch SYSENTER with TF set and clear DR_STEP. If this hit a
  906. * watchpoint at the same time then that will still be handled.
  907. */
  908. if ((dr6 & DR_STEP) && is_sysenter_singlestep(regs))
  909. dr6 &= ~DR_STEP;
  910. /*
  911. * The kernel doesn't use INT1
  912. */
  913. if (!dr6)
  914. goto out;
  915. if (notify_debug(regs, &dr6))
  916. goto out;
  917. /*
  918. * The kernel doesn't use TF single-step outside of:
  919. *
  920. * - Kprobes, consumed through kprobe_debug_handler()
  921. * - KGDB, consumed through notify_debug()
  922. *
  923. * So if we get here with DR_STEP set, something is wonky.
  924. *
  925. * A known way to trigger this is through QEMU's GDB stub,
  926. * which leaks #DB into the guest and causes IST recursion.
  927. */
  928. if (WARN_ON_ONCE(dr6 & DR_STEP))
  929. regs->flags &= ~X86_EFLAGS_TF;
  930. out:
  931. instrumentation_end();
  932. irqentry_nmi_exit(regs, irq_state);
  933. local_db_restore(dr7);
  934. }
  935. static __always_inline void exc_debug_user(struct pt_regs *regs,
  936. unsigned long dr6)
  937. {
  938. bool icebp;
  939. /*
  940. * If something gets miswired and we end up here for a kernel mode
  941. * #DB, we will malfunction.
  942. */
  943. WARN_ON_ONCE(!user_mode(regs));
  944. /*
  945. * NB: We can't easily clear DR7 here because
  946. * irqentry_exit_to_usermode() can invoke ptrace, schedule, access
  947. * user memory, etc. This means that a recursive #DB is possible. If
  948. * this happens, that #DB will hit exc_debug_kernel() and clear DR7.
  949. * Since we're not on the IST stack right now, everything will be
  950. * fine.
  951. */
  952. irqentry_enter_from_user_mode(regs);
  953. instrumentation_begin();
  954. /*
  955. * Start the virtual/ptrace DR6 value with just the DR_STEP mask
  956. * of the real DR6. ptrace_triggered() will set the DR_TRAPn bits.
  957. *
  958. * Userspace expects DR_STEP to be visible in ptrace_get_debugreg(6)
  959. * even if it is not the result of PTRACE_SINGLESTEP.
  960. */
  961. current->thread.virtual_dr6 = (dr6 & DR_STEP);
  962. /*
  963. * The SDM says "The processor clears the BTF flag when it
  964. * generates a debug exception." Clear TIF_BLOCKSTEP to keep
  965. * TIF_BLOCKSTEP in sync with the hardware BTF flag.
  966. */
  967. clear_thread_flag(TIF_BLOCKSTEP);
  968. /*
  969. * If dr6 has no reason to give us about the origin of this trap,
  970. * then it's very likely the result of an icebp/int01 trap.
  971. * User wants a sigtrap for that.
  972. */
  973. icebp = !dr6;
  974. if (notify_debug(regs, &dr6))
  975. goto out;
  976. /* It's safe to allow irq's after DR6 has been saved */
  977. local_irq_enable();
  978. if (v8086_mode(regs)) {
  979. handle_vm86_trap((struct kernel_vm86_regs *)regs, 0, X86_TRAP_DB);
  980. goto out_irq;
  981. }
  982. /* #DB for bus lock can only be triggered from userspace. */
  983. if (dr6 & DR_BUS_LOCK)
  984. handle_bus_lock(regs);
  985. /* Add the virtual_dr6 bits for signals. */
  986. dr6 |= current->thread.virtual_dr6;
  987. if (dr6 & (DR_STEP | DR_TRAP_BITS) || icebp)
  988. send_sigtrap(regs, 0, get_si_code(dr6));
  989. out_irq:
  990. local_irq_disable();
  991. out:
  992. instrumentation_end();
  993. irqentry_exit_to_user_mode(regs);
  994. }
  995. #ifdef CONFIG_X86_64
  996. /* IST stack entry */
  997. DEFINE_IDTENTRY_DEBUG(exc_debug)
  998. {
  999. exc_debug_kernel(regs, debug_read_clear_dr6());
  1000. }
  1001. /* User entry, runs on regular task stack */
  1002. DEFINE_IDTENTRY_DEBUG_USER(exc_debug)
  1003. {
  1004. exc_debug_user(regs, debug_read_clear_dr6());
  1005. }
  1006. #else
  1007. /* 32 bit does not have separate entry points. */
  1008. DEFINE_IDTENTRY_RAW(exc_debug)
  1009. {
  1010. unsigned long dr6 = debug_read_clear_dr6();
  1011. if (user_mode(regs))
  1012. exc_debug_user(regs, dr6);
  1013. else
  1014. exc_debug_kernel(regs, dr6);
  1015. }
  1016. #endif
  1017. /*
  1018. * Note that we play around with the 'TS' bit in an attempt to get
  1019. * the correct behaviour even in the presence of the asynchronous
  1020. * IRQ13 behaviour
  1021. */
  1022. static void math_error(struct pt_regs *regs, int trapnr)
  1023. {
  1024. struct task_struct *task = current;
  1025. struct fpu *fpu = &task->thread.fpu;
  1026. int si_code;
  1027. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  1028. "simd exception";
  1029. cond_local_irq_enable(regs);
  1030. if (!user_mode(regs)) {
  1031. if (fixup_exception(regs, trapnr, 0, 0))
  1032. goto exit;
  1033. task->thread.error_code = 0;
  1034. task->thread.trap_nr = trapnr;
  1035. if (notify_die(DIE_TRAP, str, regs, 0, trapnr,
  1036. SIGFPE) != NOTIFY_STOP)
  1037. die(str, regs, 0);
  1038. goto exit;
  1039. }
  1040. /*
  1041. * Synchronize the FPU register state to the memory register state
  1042. * if necessary. This allows the exception handler to inspect it.
  1043. */
  1044. fpu_sync_fpstate(fpu);
  1045. task->thread.trap_nr = trapnr;
  1046. task->thread.error_code = 0;
  1047. si_code = fpu__exception_code(fpu, trapnr);
  1048. /* Retry when we get spurious exceptions: */
  1049. if (!si_code)
  1050. goto exit;
  1051. if (fixup_vdso_exception(regs, trapnr, 0, 0))
  1052. goto exit;
  1053. force_sig_fault(SIGFPE, si_code,
  1054. (void __user *)uprobe_get_trap_addr(regs));
  1055. exit:
  1056. cond_local_irq_disable(regs);
  1057. }
  1058. DEFINE_IDTENTRY(exc_coprocessor_error)
  1059. {
  1060. math_error(regs, X86_TRAP_MF);
  1061. }
  1062. DEFINE_IDTENTRY(exc_simd_coprocessor_error)
  1063. {
  1064. if (IS_ENABLED(CONFIG_X86_INVD_BUG)) {
  1065. /* AMD 486 bug: INVD in CPL 0 raises #XF instead of #GP */
  1066. if (!static_cpu_has(X86_FEATURE_XMM)) {
  1067. __exc_general_protection(regs, 0);
  1068. return;
  1069. }
  1070. }
  1071. math_error(regs, X86_TRAP_XF);
  1072. }
  1073. DEFINE_IDTENTRY(exc_spurious_interrupt_bug)
  1074. {
  1075. /*
  1076. * This addresses a Pentium Pro Erratum:
  1077. *
  1078. * PROBLEM: If the APIC subsystem is configured in mixed mode with
  1079. * Virtual Wire mode implemented through the local APIC, an
  1080. * interrupt vector of 0Fh (Intel reserved encoding) may be
  1081. * generated by the local APIC (Int 15). This vector may be
  1082. * generated upon receipt of a spurious interrupt (an interrupt
  1083. * which is removed before the system receives the INTA sequence)
  1084. * instead of the programmed 8259 spurious interrupt vector.
  1085. *
  1086. * IMPLICATION: The spurious interrupt vector programmed in the
  1087. * 8259 is normally handled by an operating system's spurious
  1088. * interrupt handler. However, a vector of 0Fh is unknown to some
  1089. * operating systems, which would crash if this erratum occurred.
  1090. *
  1091. * In theory this could be limited to 32bit, but the handler is not
  1092. * hurting and who knows which other CPUs suffer from this.
  1093. */
  1094. }
  1095. static bool handle_xfd_event(struct pt_regs *regs)
  1096. {
  1097. u64 xfd_err;
  1098. int err;
  1099. if (!IS_ENABLED(CONFIG_X86_64) || !cpu_feature_enabled(X86_FEATURE_XFD))
  1100. return false;
  1101. rdmsrl(MSR_IA32_XFD_ERR, xfd_err);
  1102. if (!xfd_err)
  1103. return false;
  1104. wrmsrl(MSR_IA32_XFD_ERR, 0);
  1105. /* Die if that happens in kernel space */
  1106. if (WARN_ON(!user_mode(regs)))
  1107. return false;
  1108. local_irq_enable();
  1109. err = xfd_enable_feature(xfd_err);
  1110. switch (err) {
  1111. case -EPERM:
  1112. force_sig_fault(SIGILL, ILL_ILLOPC, error_get_trap_addr(regs));
  1113. break;
  1114. case -EFAULT:
  1115. force_sig(SIGSEGV);
  1116. break;
  1117. }
  1118. local_irq_disable();
  1119. return true;
  1120. }
  1121. DEFINE_IDTENTRY(exc_device_not_available)
  1122. {
  1123. unsigned long cr0 = read_cr0();
  1124. if (handle_xfd_event(regs))
  1125. return;
  1126. #ifdef CONFIG_MATH_EMULATION
  1127. if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) {
  1128. struct math_emu_info info = { };
  1129. cond_local_irq_enable(regs);
  1130. info.regs = regs;
  1131. math_emulate(&info);
  1132. cond_local_irq_disable(regs);
  1133. return;
  1134. }
  1135. #endif
  1136. /* This should not happen. */
  1137. if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
  1138. /* Try to fix it up and carry on. */
  1139. write_cr0(cr0 & ~X86_CR0_TS);
  1140. } else {
  1141. /*
  1142. * Something terrible happened, and we're better off trying
  1143. * to kill the task than getting stuck in a never-ending
  1144. * loop of #NM faults.
  1145. */
  1146. die("unexpected #NM exception", regs, 0);
  1147. }
  1148. }
  1149. #ifdef CONFIG_INTEL_TDX_GUEST
  1150. #define VE_FAULT_STR "VE fault"
  1151. static void ve_raise_fault(struct pt_regs *regs, long error_code,
  1152. unsigned long address)
  1153. {
  1154. if (user_mode(regs)) {
  1155. gp_user_force_sig_segv(regs, X86_TRAP_VE, error_code, VE_FAULT_STR);
  1156. return;
  1157. }
  1158. if (gp_try_fixup_and_notify(regs, X86_TRAP_VE, error_code,
  1159. VE_FAULT_STR, address)) {
  1160. return;
  1161. }
  1162. die_addr(VE_FAULT_STR, regs, error_code, address);
  1163. }
  1164. /*
  1165. * Virtualization Exceptions (#VE) are delivered to TDX guests due to
  1166. * specific guest actions which may happen in either user space or the
  1167. * kernel:
  1168. *
  1169. * * Specific instructions (WBINVD, for example)
  1170. * * Specific MSR accesses
  1171. * * Specific CPUID leaf accesses
  1172. * * Access to specific guest physical addresses
  1173. *
  1174. * In the settings that Linux will run in, virtualization exceptions are
  1175. * never generated on accesses to normal, TD-private memory that has been
  1176. * accepted (by BIOS or with tdx_enc_status_changed()).
  1177. *
  1178. * Syscall entry code has a critical window where the kernel stack is not
  1179. * yet set up. Any exception in this window leads to hard to debug issues
  1180. * and can be exploited for privilege escalation. Exceptions in the NMI
  1181. * entry code also cause issues. Returning from the exception handler with
  1182. * IRET will re-enable NMIs and nested NMI will corrupt the NMI stack.
  1183. *
  1184. * For these reasons, the kernel avoids #VEs during the syscall gap and
  1185. * the NMI entry code. Entry code paths do not access TD-shared memory,
  1186. * MMIO regions, use #VE triggering MSRs, instructions, or CPUID leaves
  1187. * that might generate #VE. VMM can remove memory from TD at any point,
  1188. * but access to unaccepted (or missing) private memory leads to VM
  1189. * termination, not to #VE.
  1190. *
  1191. * Similarly to page faults and breakpoints, #VEs are allowed in NMI
  1192. * handlers once the kernel is ready to deal with nested NMIs.
  1193. *
  1194. * During #VE delivery, all interrupts, including NMIs, are blocked until
  1195. * TDGETVEINFO is called. It prevents #VE nesting until the kernel reads
  1196. * the VE info.
  1197. *
  1198. * If a guest kernel action which would normally cause a #VE occurs in
  1199. * the interrupt-disabled region before TDGETVEINFO, a #DF (fault
  1200. * exception) is delivered to the guest which will result in an oops.
  1201. *
  1202. * The entry code has been audited carefully for following these expectations.
  1203. * Changes in the entry code have to be audited for correctness vs. this
  1204. * aspect. Similarly to #PF, #VE in these places will expose kernel to
  1205. * privilege escalation or may lead to random crashes.
  1206. */
  1207. DEFINE_IDTENTRY(exc_virtualization_exception)
  1208. {
  1209. struct ve_info ve;
  1210. /*
  1211. * NMIs/Machine-checks/Interrupts will be in a disabled state
  1212. * till TDGETVEINFO TDCALL is executed. This ensures that VE
  1213. * info cannot be overwritten by a nested #VE.
  1214. */
  1215. tdx_get_ve_info(&ve);
  1216. cond_local_irq_enable(regs);
  1217. /*
  1218. * If tdx_handle_virt_exception() could not process
  1219. * it successfully, treat it as #GP(0) and handle it.
  1220. */
  1221. if (!tdx_handle_virt_exception(regs, &ve))
  1222. ve_raise_fault(regs, 0, ve.gla);
  1223. cond_local_irq_disable(regs);
  1224. }
  1225. #endif
  1226. #ifdef CONFIG_X86_32
  1227. DEFINE_IDTENTRY_SW(iret_error)
  1228. {
  1229. local_irq_enable();
  1230. if (notify_die(DIE_TRAP, "iret exception", regs, 0,
  1231. X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
  1232. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, 0,
  1233. ILL_BADSTK, (void __user *)NULL);
  1234. }
  1235. local_irq_disable();
  1236. }
  1237. #endif
  1238. void __init trap_init(void)
  1239. {
  1240. /* Init cpu_entry_area before IST entries are set up */
  1241. setup_cpu_entry_areas();
  1242. /* Init GHCB memory pages when running as an SEV-ES guest */
  1243. sev_es_init_vc_handling();
  1244. /* Initialize TSS before setting up traps so ISTs work */
  1245. cpu_init_exception_handling();
  1246. /* Setup traps as cpu_init() might #GP */
  1247. idt_setup_traps();
  1248. cpu_init();
  1249. }