doublefault_32.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include <linux/sched.h>
  4. #include <linux/sched/debug.h>
  5. #include <linux/init_task.h>
  6. #include <linux/fs.h>
  7. #include <linux/uaccess.h>
  8. #include <asm/processor.h>
  9. #include <asm/desc.h>
  10. #include <asm/traps.h>
  11. #define ptr_ok(x) ((x) > PAGE_OFFSET && (x) < PAGE_OFFSET + MAXMEM)
  12. #define TSS(x) this_cpu_read(cpu_tss_rw.x86_tss.x)
  13. static void set_df_gdt_entry(unsigned int cpu);
  14. /*
  15. * Called by double_fault with CR0.TS and EFLAGS.NT cleared. The CPU thinks
  16. * we're running the doublefault task. Cannot return.
  17. */
  18. asmlinkage noinstr void __noreturn doublefault_shim(void)
  19. {
  20. unsigned long cr2;
  21. struct pt_regs regs;
  22. BUILD_BUG_ON(sizeof(struct doublefault_stack) != PAGE_SIZE);
  23. cr2 = native_read_cr2();
  24. /* Reset back to the normal kernel task. */
  25. force_reload_TR();
  26. set_df_gdt_entry(smp_processor_id());
  27. trace_hardirqs_off();
  28. /*
  29. * Fill in pt_regs. A downside of doing this in C is that the unwinder
  30. * won't see it (no ENCODE_FRAME_POINTER), so a nested stack dump
  31. * won't successfully unwind to the source of the double fault.
  32. * The main dump from exc_double_fault() is fine, though, since it
  33. * uses these regs directly.
  34. *
  35. * If anyone ever cares, this could be moved to asm.
  36. */
  37. regs.ss = TSS(ss);
  38. regs.__ssh = 0;
  39. regs.sp = TSS(sp);
  40. regs.flags = TSS(flags);
  41. regs.cs = TSS(cs);
  42. /* We won't go through the entry asm, so we can leave __csh as 0. */
  43. regs.__csh = 0;
  44. regs.ip = TSS(ip);
  45. regs.orig_ax = 0;
  46. regs.gs = TSS(gs);
  47. regs.__gsh = 0;
  48. regs.fs = TSS(fs);
  49. regs.__fsh = 0;
  50. regs.es = TSS(es);
  51. regs.__esh = 0;
  52. regs.ds = TSS(ds);
  53. regs.__dsh = 0;
  54. regs.ax = TSS(ax);
  55. regs.bp = TSS(bp);
  56. regs.di = TSS(di);
  57. regs.si = TSS(si);
  58. regs.dx = TSS(dx);
  59. regs.cx = TSS(cx);
  60. regs.bx = TSS(bx);
  61. exc_double_fault(&regs, 0, cr2);
  62. /*
  63. * x86_32 does not save the original CR3 anywhere on a task switch.
  64. * This means that, even if we wanted to return, we would need to find
  65. * some way to reconstruct CR3. We could make a credible guess based
  66. * on cpu_tlbstate, but that would be racy and would not account for
  67. * PTI.
  68. *
  69. * Instead, don't bother. We can return through
  70. * rewind_stack_do_exit() instead.
  71. */
  72. panic("cannot return from double fault\n");
  73. }
  74. DEFINE_PER_CPU_PAGE_ALIGNED(struct doublefault_stack, doublefault_stack) = {
  75. .tss = {
  76. /*
  77. * No sp0 or ss0 -- we never run CPL != 0 with this TSS
  78. * active. sp is filled in later.
  79. */
  80. .ldt = 0,
  81. .io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
  82. .ip = (unsigned long) asm_exc_double_fault,
  83. .flags = X86_EFLAGS_FIXED,
  84. .es = __USER_DS,
  85. .cs = __KERNEL_CS,
  86. .ss = __KERNEL_DS,
  87. .ds = __USER_DS,
  88. .fs = __KERNEL_PERCPU,
  89. #ifndef CONFIG_X86_32_LAZY_GS
  90. .gs = __KERNEL_STACK_CANARY,
  91. #endif
  92. .__cr3 = __pa_nodebug(swapper_pg_dir),
  93. },
  94. };
  95. static void set_df_gdt_entry(unsigned int cpu)
  96. {
  97. /* Set up doublefault TSS pointer in the GDT */
  98. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS,
  99. &get_cpu_entry_area(cpu)->doublefault_stack.tss);
  100. }
  101. void doublefault_init_cpu_tss(void)
  102. {
  103. unsigned int cpu = smp_processor_id();
  104. struct cpu_entry_area *cea = get_cpu_entry_area(cpu);
  105. /*
  106. * The linker isn't smart enough to initialize percpu variables that
  107. * point to other places in percpu space.
  108. */
  109. this_cpu_write(doublefault_stack.tss.sp,
  110. (unsigned long)&cea->doublefault_stack.stack +
  111. sizeof(doublefault_stack.stack));
  112. set_df_gdt_entry(cpu);
  113. }