1*97873a3dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
296ae6ea0SThomas Gleixner /* -*- linux-c -*- ------------------------------------------------------- *
396ae6ea0SThomas Gleixner *
496ae6ea0SThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds
596ae6ea0SThomas Gleixner * Copyright 2007 rPath, Inc. - All Rights Reserved
696ae6ea0SThomas Gleixner *
796ae6ea0SThomas Gleixner * ----------------------------------------------------------------------- */
896ae6ea0SThomas Gleixner
996ae6ea0SThomas Gleixner /*
1096ae6ea0SThomas Gleixner * Prepare the machine for transition to protected mode.
1196ae6ea0SThomas Gleixner */
1296ae6ea0SThomas Gleixner
1396ae6ea0SThomas Gleixner #include "boot.h"
1496ae6ea0SThomas Gleixner #include <asm/segment.h>
1596ae6ea0SThomas Gleixner
1696ae6ea0SThomas Gleixner /*
1796ae6ea0SThomas Gleixner * Invoke the realmode switch hook if present; otherwise
1896ae6ea0SThomas Gleixner * disable all interrupts.
1996ae6ea0SThomas Gleixner */
realmode_switch_hook(void)2096ae6ea0SThomas Gleixner static void realmode_switch_hook(void)
2196ae6ea0SThomas Gleixner {
2296ae6ea0SThomas Gleixner if (boot_params.hdr.realmode_swtch) {
2396ae6ea0SThomas Gleixner asm volatile("lcallw *%0"
2496ae6ea0SThomas Gleixner : : "m" (boot_params.hdr.realmode_swtch)
2596ae6ea0SThomas Gleixner : "eax", "ebx", "ecx", "edx");
2696ae6ea0SThomas Gleixner } else {
2796ae6ea0SThomas Gleixner asm volatile("cli");
2896ae6ea0SThomas Gleixner outb(0x80, 0x70); /* Disable NMI */
2996ae6ea0SThomas Gleixner io_delay();
3096ae6ea0SThomas Gleixner }
3196ae6ea0SThomas Gleixner }
3296ae6ea0SThomas Gleixner
3396ae6ea0SThomas Gleixner /*
3496ae6ea0SThomas Gleixner * Disable all interrupts at the legacy PIC.
3596ae6ea0SThomas Gleixner */
mask_all_interrupts(void)3696ae6ea0SThomas Gleixner static void mask_all_interrupts(void)
3796ae6ea0SThomas Gleixner {
3896ae6ea0SThomas Gleixner outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */
3996ae6ea0SThomas Gleixner io_delay();
4096ae6ea0SThomas Gleixner outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */
4196ae6ea0SThomas Gleixner io_delay();
4296ae6ea0SThomas Gleixner }
4396ae6ea0SThomas Gleixner
4496ae6ea0SThomas Gleixner /*
4596ae6ea0SThomas Gleixner * Reset IGNNE# if asserted in the FPU.
4696ae6ea0SThomas Gleixner */
reset_coprocessor(void)4796ae6ea0SThomas Gleixner static void reset_coprocessor(void)
4896ae6ea0SThomas Gleixner {
4996ae6ea0SThomas Gleixner outb(0, 0xf0);
5096ae6ea0SThomas Gleixner io_delay();
5196ae6ea0SThomas Gleixner outb(0, 0xf1);
5296ae6ea0SThomas Gleixner io_delay();
5396ae6ea0SThomas Gleixner }
5496ae6ea0SThomas Gleixner
5596ae6ea0SThomas Gleixner /*
5696ae6ea0SThomas Gleixner * Set up the GDT
5796ae6ea0SThomas Gleixner */
5896ae6ea0SThomas Gleixner
5996ae6ea0SThomas Gleixner struct gdt_ptr {
6096ae6ea0SThomas Gleixner u16 len;
6196ae6ea0SThomas Gleixner u32 ptr;
6296ae6ea0SThomas Gleixner } __attribute__((packed));
6396ae6ea0SThomas Gleixner
setup_gdt(void)6496ae6ea0SThomas Gleixner static void setup_gdt(void)
6596ae6ea0SThomas Gleixner {
6696ae6ea0SThomas Gleixner /* There are machines which are known to not boot with the GDT
6796ae6ea0SThomas Gleixner being 8-byte unaligned. Intel recommends 16 byte alignment. */
6896ae6ea0SThomas Gleixner static const u64 boot_gdt[] __attribute__((aligned(16))) = {
6996ae6ea0SThomas Gleixner /* CS: code, read/execute, 4 GB, base 0 */
7096ae6ea0SThomas Gleixner [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
7196ae6ea0SThomas Gleixner /* DS: data, read/write, 4 GB, base 0 */
7296ae6ea0SThomas Gleixner [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
7388089519SH. Peter Anvin /* TSS: 32-bit tss, 104 bytes, base 4096 */
7488089519SH. Peter Anvin /* We only have a TSS here to keep Intel VT happy;
7588089519SH. Peter Anvin we don't actually use it for anything. */
7688089519SH. Peter Anvin [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
7796ae6ea0SThomas Gleixner };
7896ae6ea0SThomas Gleixner /* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
7996ae6ea0SThomas Gleixner of the gdt_ptr contents. Thus, make it static so it will
8096ae6ea0SThomas Gleixner stay in memory, at least long enough that we switch to the
8196ae6ea0SThomas Gleixner proper kernel GDT. */
8296ae6ea0SThomas Gleixner static struct gdt_ptr gdt;
8396ae6ea0SThomas Gleixner
8496ae6ea0SThomas Gleixner gdt.len = sizeof(boot_gdt)-1;
8596ae6ea0SThomas Gleixner gdt.ptr = (u32)&boot_gdt + (ds() << 4);
8696ae6ea0SThomas Gleixner
8796ae6ea0SThomas Gleixner asm volatile("lgdtl %0" : : "m" (gdt));
8896ae6ea0SThomas Gleixner }
8996ae6ea0SThomas Gleixner
9096ae6ea0SThomas Gleixner /*
9196ae6ea0SThomas Gleixner * Set up the IDT
9296ae6ea0SThomas Gleixner */
setup_idt(void)9396ae6ea0SThomas Gleixner static void setup_idt(void)
9496ae6ea0SThomas Gleixner {
9596ae6ea0SThomas Gleixner static const struct gdt_ptr null_idt = {0, 0};
9696ae6ea0SThomas Gleixner asm volatile("lidtl %0" : : "m" (null_idt));
9796ae6ea0SThomas Gleixner }
9896ae6ea0SThomas Gleixner
9996ae6ea0SThomas Gleixner /*
10096ae6ea0SThomas Gleixner * Actual invocation sequence
10196ae6ea0SThomas Gleixner */
go_to_protected_mode(void)10296ae6ea0SThomas Gleixner void go_to_protected_mode(void)
10396ae6ea0SThomas Gleixner {
10496ae6ea0SThomas Gleixner /* Hook before leaving real mode, also disables interrupts */
10596ae6ea0SThomas Gleixner realmode_switch_hook();
10696ae6ea0SThomas Gleixner
10796ae6ea0SThomas Gleixner /* Enable the A20 gate */
10896ae6ea0SThomas Gleixner if (enable_a20()) {
10996ae6ea0SThomas Gleixner puts("A20 gate not responding, unable to boot...\n");
11096ae6ea0SThomas Gleixner die();
11196ae6ea0SThomas Gleixner }
11296ae6ea0SThomas Gleixner
11396ae6ea0SThomas Gleixner /* Reset coprocessor (IGNNE#) */
11496ae6ea0SThomas Gleixner reset_coprocessor();
11596ae6ea0SThomas Gleixner
11696ae6ea0SThomas Gleixner /* Mask all interrupts in the PIC */
11796ae6ea0SThomas Gleixner mask_all_interrupts();
11896ae6ea0SThomas Gleixner
11996ae6ea0SThomas Gleixner /* Actual transition to protected mode... */
12096ae6ea0SThomas Gleixner setup_idt();
12196ae6ea0SThomas Gleixner setup_gdt();
12296ae6ea0SThomas Gleixner protected_mode_jump(boot_params.hdr.code32_start,
12396ae6ea0SThomas Gleixner (u32)&boot_params + (ds() << 4));
12496ae6ea0SThomas Gleixner }
125