This source file includes following definitions.
- realmode_switch_hook
- mask_all_interrupts
- reset_coprocessor
- setup_gdt
- setup_idt
- go_to_protected_mode
1
2
3
4
5
6
7
8
9
10
11
12
13 #include "boot.h"
14 #include <asm/segment.h>
15
16
17
18
19
20 static void realmode_switch_hook(void)
21 {
22 if (boot_params.hdr.realmode_swtch) {
23 asm volatile("lcallw *%0"
24 : : "m" (boot_params.hdr.realmode_swtch)
25 : "eax", "ebx", "ecx", "edx");
26 } else {
27 asm volatile("cli");
28 outb(0x80, 0x70);
29 io_delay();
30 }
31 }
32
33
34
35
36 static void mask_all_interrupts(void)
37 {
38 outb(0xff, 0xa1);
39 io_delay();
40 outb(0xfb, 0x21);
41 io_delay();
42 }
43
44
45
46
47 static void reset_coprocessor(void)
48 {
49 outb(0, 0xf0);
50 io_delay();
51 outb(0, 0xf1);
52 io_delay();
53 }
54
55
56
57
58
59 struct gdt_ptr {
60 u16 len;
61 u32 ptr;
62 } __attribute__((packed));
63
64 static void setup_gdt(void)
65 {
66
67
68 static const u64 boot_gdt[] __attribute__((aligned(16))) = {
69
70 [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
71
72 [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
73
74
75
76 [GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
77 };
78
79
80
81
82 static struct gdt_ptr gdt;
83
84 gdt.len = sizeof(boot_gdt)-1;
85 gdt.ptr = (u32)&boot_gdt + (ds() << 4);
86
87 asm volatile("lgdtl %0" : : "m" (gdt));
88 }
89
90
91
92
93 static void setup_idt(void)
94 {
95 static const struct gdt_ptr null_idt = {0, 0};
96 asm volatile("lidtl %0" : : "m" (null_idt));
97 }
98
99
100
101
102 void go_to_protected_mode(void)
103 {
104
105 realmode_switch_hook();
106
107
108 if (enable_a20()) {
109 puts("A20 gate not responding, unable to boot...\n");
110 die();
111 }
112
113
114 reset_coprocessor();
115
116
117 mask_all_interrupts();
118
119
120 setup_idt();
121 setup_gdt();
122 protected_mode_jump(boot_params.hdr.code32_start,
123 (u32)&boot_params + (ds() << 4));
124 }