traps.c 40 KB

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