pm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* -*- linux-c -*- ------------------------------------------------------- *
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright 2007 rPath, Inc. - All Rights Reserved
  6. *
  7. * ----------------------------------------------------------------------- */
  8. /*
  9. * Prepare the machine for transition to protected mode.
  10. */
  11. #include "boot.h"
  12. #include <asm/segment.h>
  13. /*
  14. * Invoke the realmode switch hook if present; otherwise
  15. * disable all interrupts.
  16. */
  17. static void realmode_switch_hook(void)
  18. {
  19. if (boot_params.hdr.realmode_swtch) {
  20. asm volatile("lcallw *%0"
  21. : : "m" (boot_params.hdr.realmode_swtch)
  22. : "eax", "ebx", "ecx", "edx");
  23. } else {
  24. asm volatile("cli");
  25. outb(0x80, 0x70); /* Disable NMI */
  26. io_delay();
  27. }
  28. }
  29. /*
  30. * Disable all interrupts at the legacy PIC.
  31. */
  32. static void mask_all_interrupts(void)
  33. {
  34. outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */
  35. io_delay();
  36. outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */
  37. io_delay();
  38. }
  39. /*
  40. * Reset IGNNE# if asserted in the FPU.
  41. */
  42. static void reset_coprocessor(void)
  43. {
  44. outb(0, 0xf0);
  45. io_delay();
  46. outb(0, 0xf1);
  47. io_delay();
  48. }
  49. /*
  50. * Set up the GDT
  51. */
  52. struct gdt_ptr {
  53. u16 len;
  54. u32 ptr;
  55. } __attribute__((packed));
  56. static void setup_gdt(void)
  57. {
  58. /* There are machines which are known to not boot with the GDT
  59. being 8-byte unaligned. Intel recommends 16 byte alignment. */
  60. static const u64 boot_gdt[] __attribute__((aligned(16))) = {
  61. /* CS: code, read/execute, 4 GB, base 0 */
  62. [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
  63. /* DS: data, read/write, 4 GB, base 0 */
  64. [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
  65. /* TSS: 32-bit tss, 104 bytes, base 4096 */
  66. /* We only have a TSS here to keep Intel VT happy;
  67. we don't actually use it for anything. */
  68. [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
  69. };
  70. /* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
  71. of the gdt_ptr contents. Thus, make it static so it will
  72. stay in memory, at least long enough that we switch to the
  73. proper kernel GDT. */
  74. static struct gdt_ptr gdt;
  75. gdt.len = sizeof(boot_gdt)-1;
  76. gdt.ptr = (u32)&boot_gdt + (ds() << 4);
  77. asm volatile("lgdtl %0" : : "m" (gdt));
  78. }
  79. /*
  80. * Set up the IDT
  81. */
  82. static void setup_idt(void)
  83. {
  84. static const struct gdt_ptr null_idt = {0, 0};
  85. asm volatile("lidtl %0" : : "m" (null_idt));
  86. }
  87. /*
  88. * Actual invocation sequence
  89. */
  90. void go_to_protected_mode(void)
  91. {
  92. /* Hook before leaving real mode, also disables interrupts */
  93. realmode_switch_hook();
  94. /* Enable the A20 gate */
  95. if (enable_a20()) {
  96. puts("A20 gate not responding, unable to boot...\n");
  97. die();
  98. }
  99. /* Reset coprocessor (IGNNE#) */
  100. reset_coprocessor();
  101. /* Mask all interrupts in the PIC */
  102. mask_all_interrupts();
  103. /* Actual transition to protected mode... */
  104. setup_idt();
  105. setup_gdt();
  106. protected_mode_jump(boot_params.hdr.code32_start,
  107. (u32)&boot_params + (ds() << 4));
  108. }