// SPDX-License-Identifier: GPL-2.0-only1/* -*- linux-c -*- ------------------------------------------------------- *2*3* Copyright (C) 1991, 1992 Linus Torvalds4* Copyright 2007 rPath, Inc. - All Rights Reserved5*6* ----------------------------------------------------------------------- */78/*9* Prepare the machine for transition to protected mode.10*/1112#include "boot.h"13#include <asm/desc_defs.h>14#include <asm/segment.h>1516/*17* Invoke the realmode switch hook if present; otherwise18* disable all interrupts.19*/20static void realmode_switch_hook(void)21{22if (boot_params.hdr.realmode_swtch) {23asm volatile("lcallw *%0"24: : "m" (boot_params.hdr.realmode_swtch)25: "eax", "ebx", "ecx", "edx");26} else {27asm volatile("cli");28outb(0x80, 0x70); /* Disable NMI */29io_delay();30}31}3233/*34* Disable all interrupts at the legacy PIC.35*/36static void mask_all_interrupts(void)37{38outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */39io_delay();40outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */41io_delay();42}4344/*45* Reset IGNNE# if asserted in the FPU.46*/47static void reset_coprocessor(void)48{49outb(0, 0xf0);50io_delay();51outb(0, 0xf1);52io_delay();53}5455/*56* Set up the GDT57*/5859struct gdt_ptr {60u16 len;61u32 ptr;62} __attribute__((packed));6364static void setup_gdt(void)65{66/* There are machines which are known to not boot with the GDT67being 8-byte unaligned. Intel recommends 16 byte alignment. */68static const u64 boot_gdt[] __attribute__((aligned(16))) = {69/* CS: code, read/execute, 4 GB, base 0 */70[GDT_ENTRY_BOOT_CS] = GDT_ENTRY(DESC_CODE32, 0, 0xfffff),71/* DS: data, read/write, 4 GB, base 0 */72[GDT_ENTRY_BOOT_DS] = GDT_ENTRY(DESC_DATA32, 0, 0xfffff),73/* TSS: 32-bit tss, 104 bytes, base 4096 */74/* We only have a TSS here to keep Intel VT happy;75we don't actually use it for anything. */76[GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(DESC_TSS32, 4096, 103),77};78/* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead79of the gdt_ptr contents. Thus, make it static so it will80stay in memory, at least long enough that we switch to the81proper kernel GDT. */82static struct gdt_ptr gdt;8384gdt.len = sizeof(boot_gdt)-1;85gdt.ptr = (u32)&boot_gdt + (ds() << 4);8687asm volatile("lgdtl %0" : : "m" (gdt));88}8990/*91* Set up the IDT92*/93static void setup_idt(void)94{95static const struct gdt_ptr null_idt = {0, 0};96asm volatile("lidtl %0" : : "m" (null_idt));97}9899/*100* Actual invocation sequence101*/102void go_to_protected_mode(void)103{104/* Hook before leaving real mode, also disables interrupts */105realmode_switch_hook();106107/* Enable the A20 gate */108if (enable_a20()) {109puts("A20 gate not responding, unable to boot...\n");110die();111}112113/* Reset coprocessor (IGNNE#) */114reset_coprocessor();115116/* Mask all interrupts in the PIC */117mask_all_interrupts();118119/* Actual transition to protected mode... */120setup_idt();121setup_gdt();122protected_mode_jump(boot_params.hdr.code32_start,123(u32)&boot_params + (ds() << 4));124}125126127