1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
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.
9 * ----------------------------------------------------------------------- */
12 * Prepare the machine for transition to protected mode.
16 #include <asm/segment.h>
19 * Invoke the realmode switch hook if present; otherwise
20 * disable all interrupts.
22 static void realmode_switch_hook(void)
24 if (boot_params.hdr.realmode_swtch) {
25 asm volatile("lcallw *%0"
26 : : "m" (boot_params.hdr.realmode_swtch)
27 : "eax", "ebx", "ecx", "edx");
30 outb(0x80, 0x70); /* Disable NMI */
36 * A zImage kernel is loaded at 0x10000 but wants to run at 0x1000.
37 * A bzImage kernel is loaded and runs at 0x100000.
39 static void move_kernel_around(void)
41 /* Note: rely on the compile-time option here rather than
42 the LOADED_HIGH flag. The Qemu kernel loader unconditionally
43 sets the loadflags to zero. */
44 #ifndef __BIG_KERNEL__
48 dst_seg = 0x1000 >> 4;
49 src_seg = 0x10000 >> 4;
50 syssize = boot_params.hdr.syssize; /* Size in 16-byte paragraphs */
53 int paras = (syssize >= 0x1000) ? 0x1000 : syssize;
54 int dwords = paras << 2;
56 asm volatile("pushw %%es ; "
66 : "r" (dst_seg), "r" (src_seg)
77 * Disable all interrupts at the legacy PIC.
79 static void mask_all_interrupts(void)
81 outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */
83 outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */
88 * Reset IGNNE# if asserted in the FPU.
90 static void reset_coprocessor(void)
101 #define GDT_ENTRY(flags, base, limit) \
102 (((u64)(base & 0xff000000) << 32) | \
103 ((u64)flags << 40) | \
104 ((u64)(limit & 0x00ff0000) << 32) | \
105 ((u64)(base & 0x00ffffff) << 16) | \
106 ((u64)(limit & 0x0000ffff)))
111 } __attribute__((packed));
113 static void setup_gdt(void)
115 /* There are machines which are known to not boot with the GDT
116 being 8-byte unaligned. Intel recommends 16 byte alignment. */
117 static const u64 boot_gdt[] __attribute__((aligned(16))) = {
118 /* CS: code, read/execute, 4 GB, base 0 */
119 [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
120 /* DS: data, read/write, 4 GB, base 0 */
121 [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
122 /* TSS: 32-bit tss, 104 bytes, base 4096 */
123 /* We only have a TSS here to keep Intel VT happy;
124 we don't actually use it for anything. */
125 [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
127 /* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
128 of the gdt_ptr contents. Thus, make it static so it will
129 stay in memory, at least long enough that we switch to the
130 proper kernel GDT. */
131 static struct gdt_ptr gdt;
133 gdt.len = sizeof(boot_gdt)-1;
134 gdt.ptr = (u32)&boot_gdt + (ds() << 4);
136 asm volatile("lgdtl %0" : : "m" (gdt));
142 static void setup_idt(void)
144 static const struct gdt_ptr null_idt = {0, 0};
145 asm volatile("lidtl %0" : : "m" (null_idt));
149 * Actual invocation sequence
151 void go_to_protected_mode(void)
153 /* Hook before leaving real mode, also disables interrupts */
154 realmode_switch_hook();
156 /* Move the kernel/setup to their final resting places */
157 move_kernel_around();
159 /* Enable the A20 gate */
161 puts("A20 gate not responding, unable to boot...\n");
165 /* Reset coprocessor (IGNNE#) */
168 /* Mask all interrupts in the PIC */
169 mask_all_interrupts();
171 /* Actual transition to protected mode... */
174 protected_mode_jump(boot_params.hdr.code32_start,
175 (u32)&boot_params + (ds() << 4));