pm.c 3.0 KB

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