pm.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * arch/i386/boot/pm.c
  12. *
  13. * Prepare the machine for transition to protected mode.
  14. */
  15. #include "boot.h"
  16. #include <asm/segment.h>
  17. /*
  18. * Invoke the realmode switch hook if present; otherwise
  19. * disable all interrupts.
  20. */
  21. static void realmode_switch_hook(void)
  22. {
  23. if (boot_params.hdr.realmode_swtch) {
  24. asm volatile("lcallw *%0"
  25. : : "m" (boot_params.hdr.realmode_swtch)
  26. : "eax", "ebx", "ecx", "edx");
  27. } else {
  28. asm volatile("cli");
  29. outb(0x80, 0x70); /* Disable NMI */
  30. io_delay();
  31. }
  32. }
  33. /*
  34. * A zImage kernel is loaded at 0x10000 but wants to run at 0x1000.
  35. * A bzImage kernel is loaded and runs at 0x100000.
  36. */
  37. static void move_kernel_around(void)
  38. {
  39. /* Note: rely on the compile-time option here rather than
  40. the LOADED_HIGH flag. The Qemu kernel loader unconditionally
  41. sets the loadflags to zero. */
  42. #ifndef __BIG_KERNEL__
  43. u16 dst_seg, src_seg;
  44. u32 syssize;
  45. dst_seg = 0x1000 >> 4;
  46. src_seg = 0x10000 >> 4;
  47. syssize = boot_params.hdr.syssize; /* Size in 16-byte paragraphs */
  48. while (syssize) {
  49. int paras = (syssize >= 0x1000) ? 0x1000 : syssize;
  50. int dwords = paras << 2;
  51. asm volatile("pushw %%es ; "
  52. "pushw %%ds ; "
  53. "movw %1,%%es ; "
  54. "movw %2,%%ds ; "
  55. "xorw %%di,%%di ; "
  56. "xorw %%si,%%si ; "
  57. "rep;movsl ; "
  58. "popw %%ds ; "
  59. "popw %%es"
  60. : "+c" (dwords)
  61. : "r" (dst_seg), "r" (src_seg)
  62. : "esi", "edi");
  63. syssize -= paras;
  64. dst_seg += paras;
  65. src_seg += paras;
  66. }
  67. #endif
  68. }
  69. /*
  70. * Disable all interrupts at the legacy PIC.
  71. */
  72. static void mask_all_interrupts(void)
  73. {
  74. outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */
  75. io_delay();
  76. outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */
  77. io_delay();
  78. }
  79. /*
  80. * Reset IGNNE# if asserted in the FPU.
  81. */
  82. static void reset_coprocessor(void)
  83. {
  84. outb(0, 0xf0);
  85. io_delay();
  86. outb(0, 0xf1);
  87. io_delay();
  88. }
  89. /*
  90. * Set up the GDT
  91. */
  92. #define GDT_ENTRY(flags,base,limit) \
  93. (((u64)(base & 0xff000000) << 32) | \
  94. ((u64)flags << 40) | \
  95. ((u64)(limit & 0x00ff0000) << 32) | \
  96. ((u64)(base & 0x00ffff00) << 16) | \
  97. ((u64)(limit & 0x0000ffff)))
  98. struct gdt_ptr {
  99. u16 len;
  100. u32 ptr;
  101. } __attribute__((packed));
  102. static void setup_gdt(void)
  103. {
  104. /* There are machines which are known to not boot with the GDT
  105. being 8-byte unaligned. Intel recommends 16 byte alignment. */
  106. static const u64 boot_gdt[] __attribute__((aligned(16))) = {
  107. /* CS: code, read/execute, 4 GB, base 0 */
  108. [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
  109. /* DS: data, read/write, 4 GB, base 0 */
  110. [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
  111. };
  112. /* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
  113. of the gdt_ptr contents. Thus, make it static so it will
  114. stay in memory, at least long enough that we switch to the
  115. proper kernel GDT. */
  116. static struct gdt_ptr gdt;
  117. gdt.len = sizeof(boot_gdt)-1;
  118. gdt.ptr = (u32)&boot_gdt + (ds() << 4);
  119. asm volatile("lgdtl %0" : : "m" (gdt));
  120. }
  121. /*
  122. * Set up the IDT
  123. */
  124. static void setup_idt(void)
  125. {
  126. static const struct gdt_ptr null_idt = {0, 0};
  127. asm volatile("lidtl %0" : : "m" (null_idt));
  128. }
  129. /*
  130. * Actual invocation sequence
  131. */
  132. void go_to_protected_mode(void)
  133. {
  134. /* Hook before leaving real mode, also disables interrupts */
  135. realmode_switch_hook();
  136. /* Move the kernel/setup to their final resting places */
  137. move_kernel_around();
  138. /* Enable the A20 gate */
  139. if (enable_a20()) {
  140. puts("A20 gate not responding, unable to boot...\n");
  141. die();
  142. }
  143. /* Reset coprocessor (IGNNE#) */
  144. reset_coprocessor();
  145. /* Mask all interrupts in the PIC */
  146. mask_all_interrupts();
  147. /* Actual transition to protected mode... */
  148. setup_idt();
  149. setup_gdt();
  150. protected_mode_jump(boot_params.hdr.code32_start,
  151. (u32)&boot_params + (ds() << 4));
  152. }