pm.c 4.1 KB

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