Loading...
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/*
12 * Prepare the machine for transition to protected mode.
13 */
14
15#include "boot.h"
16#include <asm/segment.h>
17
18/*
19 * Invoke the realmode switch hook if present; otherwise
20 * disable all interrupts.
21 */
22static void realmode_switch_hook(void)
23{
24 if (boot_params.hdr.realmode_swtch) {
25 asm volatile("lcallw *%0"
26 : : "m" (boot_params.hdr.realmode_swtch)
27 : "eax", "ebx", "ecx", "edx");
28 } else {
29 asm volatile("cli");
30 outb(0x80, 0x70); /* Disable NMI */
31 io_delay();
32 }
33}
34
35/*
36 * Disable all interrupts at the legacy PIC.
37 */
38static void mask_all_interrupts(void)
39{
40 outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */
41 io_delay();
42 outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */
43 io_delay();
44}
45
46/*
47 * Reset IGNNE# if asserted in the FPU.
48 */
49static void reset_coprocessor(void)
50{
51 outb(0, 0xf0);
52 io_delay();
53 outb(0, 0xf1);
54 io_delay();
55}
56
57/*
58 * Set up the GDT
59 */
60
61struct gdt_ptr {
62 u16 len;
63 u32 ptr;
64} __attribute__((packed));
65
66static void setup_gdt(void)
67{
68 /* There are machines which are known to not boot with the GDT
69 being 8-byte unaligned. Intel recommends 16 byte alignment. */
70 static const u64 boot_gdt[] __attribute__((aligned(16))) = {
71 /* CS: code, read/execute, 4 GB, base 0 */
72 [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
73 /* DS: data, read/write, 4 GB, base 0 */
74 [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
75 /* TSS: 32-bit tss, 104 bytes, base 4096 */
76 /* We only have a TSS here to keep Intel VT happy;
77 we don't actually use it for anything. */
78 [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
79 };
80 /* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
81 of the gdt_ptr contents. Thus, make it static so it will
82 stay in memory, at least long enough that we switch to the
83 proper kernel GDT. */
84 static struct gdt_ptr gdt;
85
86 gdt.len = sizeof(boot_gdt)-1;
87 gdt.ptr = (u32)&boot_gdt + (ds() << 4);
88
89 asm volatile("lgdtl %0" : : "m" (gdt));
90}
91
92/*
93 * Set up the IDT
94 */
95static void setup_idt(void)
96{
97 static const struct gdt_ptr null_idt = {0, 0};
98 asm volatile("lidtl %0" : : "m" (null_idt));
99}
100
101/*
102 * Actual invocation sequence
103 */
104void go_to_protected_mode(void)
105{
106 /* Hook before leaving real mode, also disables interrupts */
107 realmode_switch_hook();
108
109 /* Enable the A20 gate */
110 if (enable_a20()) {
111 puts("A20 gate not responding, unable to boot...\n");
112 die();
113 }
114
115 /* Reset coprocessor (IGNNE#) */
116 reset_coprocessor();
117
118 /* Mask all interrupts in the PIC */
119 mask_all_interrupts();
120
121 /* Actual transition to protected mode... */
122 setup_idt();
123 setup_gdt();
124 protected_mode_jump(boot_params.hdr.code32_start,
125 (u32)&boot_params + (ds() << 4));
126}
1// SPDX-License-Identifier: GPL-2.0-only
2/* -*- linux-c -*- ------------------------------------------------------- *
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright 2007 rPath, Inc. - All Rights Reserved
6 *
7 * ----------------------------------------------------------------------- */
8
9/*
10 * Prepare the machine for transition to protected mode.
11 */
12
13#include "boot.h"
14#include <asm/desc_defs.h>
15#include <asm/segment.h>
16
17/*
18 * Invoke the realmode switch hook if present; otherwise
19 * disable all interrupts.
20 */
21static 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/*
35 * Disable all interrupts at the legacy PIC.
36 */
37static void mask_all_interrupts(void)
38{
39 outb(0xff, 0xa1); /* Mask all interrupts on the secondary PIC */
40 io_delay();
41 outb(0xfb, 0x21); /* Mask all but cascade on the primary PIC */
42 io_delay();
43}
44
45/*
46 * Reset IGNNE# if asserted in the FPU.
47 */
48static void reset_coprocessor(void)
49{
50 outb(0, 0xf0);
51 io_delay();
52 outb(0, 0xf1);
53 io_delay();
54}
55
56/*
57 * Set up the GDT
58 */
59
60struct gdt_ptr {
61 u16 len;
62 u32 ptr;
63} __attribute__((packed));
64
65static void setup_gdt(void)
66{
67 /* There are machines which are known to not boot with the GDT
68 being 8-byte unaligned. Intel recommends 16 byte alignment. */
69 static const u64 boot_gdt[] __attribute__((aligned(16))) = {
70 /* CS: code, read/execute, 4 GB, base 0 */
71 [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(DESC_CODE32, 0, 0xfffff),
72 /* DS: data, read/write, 4 GB, base 0 */
73 [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(DESC_DATA32, 0, 0xfffff),
74 /* TSS: 32-bit tss, 104 bytes, base 4096 */
75 /* We only have a TSS here to keep Intel VT happy;
76 we don't actually use it for anything. */
77 [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(DESC_TSS32, 4096, 103),
78 };
79 /* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
80 of the gdt_ptr contents. Thus, make it static so it will
81 stay in memory, at least long enough that we switch to the
82 proper kernel GDT. */
83 static struct gdt_ptr gdt;
84
85 gdt.len = sizeof(boot_gdt)-1;
86 gdt.ptr = (u32)&boot_gdt + (ds() << 4);
87
88 asm volatile("lgdtl %0" : : "m" (gdt));
89}
90
91/*
92 * Set up the IDT
93 */
94static void setup_idt(void)
95{
96 static const struct gdt_ptr null_idt = {0, 0};
97 asm volatile("lidtl %0" : : "m" (null_idt));
98}
99
100/*
101 * Actual invocation sequence
102 */
103void go_to_protected_mode(void)
104{
105 /* Hook before leaving real mode, also disables interrupts */
106 realmode_switch_hook();
107
108 /* Enable the A20 gate */
109 if (enable_a20()) {
110 puts("A20 gate not responding, unable to boot...\n");
111 die();
112 }
113
114 /* Reset coprocessor (IGNNE#) */
115 reset_coprocessor();
116
117 /* Mask all interrupts in the PIC */
118 mask_all_interrupts();
119
120 /* Actual transition to protected mode... */
121 setup_idt();
122 setup_gdt();
123 protected_mode_jump(boot_params.hdr.code32_start,
124 (u32)&boot_params + (ds() << 4));
125}