1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "boot.h"
16#include <asm/segment.h>
17
18
19
20
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);
31 io_delay();
32 }
33}
34
35
36
37
38static void mask_all_interrupts(void)
39{
40 outb(0xff, 0xa1);
41 io_delay();
42 outb(0xfb, 0x21);
43 io_delay();
44}
45
46
47
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
59
60
61struct gdt_ptr {
62 u16 len;
63 u32 ptr;
64} __attribute__((packed));
65
66static void setup_gdt(void)
67{
68
69
70 static const u64 boot_gdt[] __attribute__((aligned(16))) = {
71
72 [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
73
74 [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
75
76
77
78 [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
79 };
80
81
82
83
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
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
103
104void go_to_protected_mode(void)
105{
106
107 realmode_switch_hook();
108
109
110 if (enable_a20()) {
111 puts("A20 gate not responding, unable to boot...\n");
112 die();
113 }
114
115
116 reset_coprocessor();
117
118
119 mask_all_interrupts();
120
121
122 setup_idt();
123 setup_gdt();
124 protected_mode_jump(boot_params.hdr.code32_start,
125 (u32)&boot_params + (ds() << 4));
126}
127