traps.c 41 KB

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