pm.c 3.0 KB

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