fred.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/kernel.h>
  3. #include <asm/desc.h>
  4. #include <asm/fred.h>
  5. #include <asm/tlbflush.h>
  6. #include <asm/traps.h>
  7. /* #DB in the kernel would imply the use of a kernel debugger. */
  8. #define FRED_DB_STACK_LEVEL 1UL
  9. #define FRED_NMI_STACK_LEVEL 2UL
  10. #define FRED_MC_STACK_LEVEL 2UL
  11. /*
  12. * #DF is the highest level because a #DF means "something went wrong
  13. * *while delivering an exception*." The number of cases for which that
  14. * can happen with FRED is drastically reduced and basically amounts to
  15. * "the stack you pointed me to is broken." Thus, always change stacks
  16. * on #DF, which means it should be at the highest level.
  17. */
  18. #define FRED_DF_STACK_LEVEL 3UL
  19. #define FRED_STKLVL(vector, lvl) ((lvl) << (2 * (vector)))
  20. DEFINE_PER_CPU(unsigned long, fred_rsp0);
  21. EXPORT_PER_CPU_SYMBOL(fred_rsp0);
  22. void cpu_init_fred_exceptions(void)
  23. {
  24. /* When FRED is enabled by default, remove this log message */
  25. pr_info("Initialize FRED on CPU%d\n", smp_processor_id());
  26. /*
  27. * If a kernel event is delivered before a CPU goes to user level for
  28. * the first time, its SS is NULL thus NULL is pushed into the SS field
  29. * of the FRED stack frame. But before ERETS is executed, the CPU may
  30. * context switch to another task and go to user level. Then when the
  31. * CPU comes back to kernel mode, SS is changed to __KERNEL_DS. Later
  32. * when ERETS is executed to return from the kernel event handler, a #GP
  33. * fault is generated because SS doesn't match the SS saved in the FRED
  34. * stack frame.
  35. *
  36. * Initialize SS to __KERNEL_DS when enabling FRED to avoid such #GPs.
  37. */
  38. loadsegment(ss, __KERNEL_DS);
  39. wrmsrl(MSR_IA32_FRED_CONFIG,
  40. /* Reserve for CALL emulation */
  41. FRED_CONFIG_REDZONE |
  42. FRED_CONFIG_INT_STKLVL(0) |
  43. FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));
  44. wrmsrl(MSR_IA32_FRED_STKLVLS, 0);
  45. /*
  46. * Ater a CPU offline/online cycle, the FRED RSP0 MSR should be
  47. * resynchronized with its per-CPU cache.
  48. */
  49. wrmsrl(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0));
  50. wrmsrl(MSR_IA32_FRED_RSP1, 0);
  51. wrmsrl(MSR_IA32_FRED_RSP2, 0);
  52. wrmsrl(MSR_IA32_FRED_RSP3, 0);
  53. /* Enable FRED */
  54. cr4_set_bits(X86_CR4_FRED);
  55. /* Any further IDT use is a bug */
  56. idt_invalidate();
  57. /* Use int $0x80 for 32-bit system calls in FRED mode */
  58. setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
  59. setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
  60. }
  61. /* Must be called after setup_cpu_entry_areas() */
  62. void cpu_init_fred_rsps(void)
  63. {
  64. /*
  65. * The purpose of separate stacks for NMI, #DB and #MC *in the kernel*
  66. * (remember that user space faults are always taken on stack level 0)
  67. * is to avoid overflowing the kernel stack.
  68. */
  69. wrmsrl(MSR_IA32_FRED_STKLVLS,
  70. FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) |
  71. FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
  72. FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) |
  73. FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL));
  74. /* The FRED equivalents to IST stacks... */
  75. wrmsrl(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
  76. wrmsrl(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
  77. wrmsrl(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
  78. }