Repository navigation
Expand file tree
/
Copy pathloader.S
More file actions
executable file
·175 lines (137 loc) · 5.49 KB
/
Copy pathloader.S
File metadata and controls
executable file
·175 lines (137 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* chapter3/2/loader.S
Author: Wenbo Yang <solrex@gmail.com> <http://solrex.cn>
This file is part of the source code of book "Write Your Own OS with Free
and Open Source Software". Homepage @ <http://share.solrex.cn/WriteOS/>.
This file is licensed under the GNU General Public License; either
version 3 of the License, or (at your option) any later version. */
#include "pm.h"
.code16
.text
jmp LABEL_BEGIN /* jump over the .data section. */
/* NOTE! Wenbo-20080512: Actually here we put the normal .data section into
the .code section. For application SW, it is not allowed. However, we are
writing an OS. That is OK. Because there is no OS to complain about
that behavior. :) */
/* Global Descriptor Table */
LABEL_GDT: Descriptor 0, 0, 0
LABEL_DESC_CODE32: Descriptor 0, (SegCode32Len - 1), (DA_C + DA_32)
LABEL_DESC_DATA: Descriptor 0, (DataLen - 1), DA_DRW
LABEL_DESC_STACK: Descriptor 0, TopOfStack, (DA_DRWA + DA_32)
LABEL_DESC_VIDEO: Descriptor 0xB8000, 0xffff, DA_DRW
LABEL_DESC_LDT: Descriptor 0, (LDTLen - 1), DA_LDT
.set GdtLen, (. - LABEL_GDT) /* GDT Length */
GdtPtr: .2byte (GdtLen - 1) /* GDT Limit */
.4byte 0 /* GDT Base */
/* GDT Selector(TI flag clear) */
.set SelectorCode32, (LABEL_DESC_CODE32 - LABEL_GDT)
.set SelectorData, (LABEL_DESC_DATA - LABEL_GDT)
.set SelectorStack, (LABEL_DESC_STACK - LABEL_GDT)
.set SelectorVideo, (LABEL_DESC_VIDEO - LABEL_GDT)
.set SelectorLDT, (LABEL_DESC_LDT - LABEL_GDT)
/* LDT segment */
LABEL_LDT:
LABEL_LDT_DESC_CODEA: Descriptor 0, (CodeALen - 1), (DA_C + DA_32)
.set LDTLen, (. - LABEL_LDT) /* LDT Length */
/* LDT Selector (TI flag set)*/
.set SelectorLDTCodeA, (LABEL_LDT_DESC_CODEA - LABEL_LDT + SA_TIL)
/* 32-bit global data segment. */
LABEL_DATA:
PMMessage: .ascii "Welcome to protect mode! ^-^\0"
LDTMessage: .ascii "Aha, you jumped into a LDT segment.\0"
.set OffsetPMMessage, (PMMessage - LABEL_DATA)
.set OffsetLDTMessage, (LDTMessage - LABEL_DATA)
.set DataLen, (. - LABEL_DATA)
/* 32-bit global stack segment. */
LABEL_STACK:
.space 512, 0
.set TopOfStack, (. - LABEL_STACK - 1)
/* Program starts here. */
LABEL_BEGIN:
mov %cs, %ax /* Move code segment address(CS) to data segment */
mov %ax, %ds /* register(DS), ES and SS. Because we have */
mov %ax, %es /* embedded .data section into .code section in */
mov %ax, %ss /* the start(mentioned in the NOTE above). */
mov $0x100, %sp
/* Initialize 32-bits code segment descriptor. */
InitDesc LABEL_SEG_CODE32, LABEL_DESC_CODE32
/* Initialize data segment descriptor. */
InitDesc LABEL_DATA, LABEL_DESC_DATA
/* Initialize stack segment descriptor. */
InitDesc LABEL_STACK, LABEL_DESC_STACK
/* Initialize LDT descriptor in GDT. */
InitDesc LABEL_LDT, LABEL_DESC_LDT
/* Initialize code A descriptor in LDT. */
InitDesc LABEL_CODEA, LABEL_LDT_DESC_CODEA
/* Prepared for loading GDTR */
xor %eax, %eax
mov %ds, %ax
shl $4, %eax
add $(LABEL_GDT), %eax /* eax <- gdt base*/
movl %eax, (GdtPtr + 2)
/* Load GDTR(Global Descriptor Table Register) */
lgdtw GdtPtr
/* Clear Interrupt Flags */
cli
/* Open A20 line. */
inb $0x92, %al
orb $0b00000010, %al
outb %al, $0x92
/* Enable protect mode, PE bit of CR0. */
movl %cr0, %eax
orl $1, %eax
movl %eax, %cr0
/* Mixed-Size Jump. */
ljmpl $SelectorCode32, $0 /* Thanks to earthengine@gmail, I got */
/* this mixed-size jump insn of gas. */
/* 32-bit code segment for LDT */
LABEL_CODEA:
.code32
mov $(SelectorVideo), %ax
mov %ax, %gs
movb $0xC, %ah /* 0000: Black Back 1100: Red Front */
xor %esi, %esi
xor %edi, %edi
movl $(OffsetLDTMessage), %esi
movl $((80 * 12 + 0) * 2), %edi
cld /* Clear DF flag. */
/* Display a string from %esi(string offset) to %edi(video segment). */
CODEA.1:
lodsb /* Load a byte from source */
test %al, %al
jz CODEA.2
mov %ax, %gs:(%edi)
add $2, %edi
jmp CODEA.1
CODEA.2:
/* Stop here, infinite loop. */
jmp .
.set CodeALen, (. - LABEL_CODEA)
/* 32-bit code segment for GDT */
LABEL_SEG_CODE32:
mov $(SelectorData), %ax
mov %ax, %ds /* Data segment selector */
mov $(SelectorStack), %ax
mov %ax, %ss /* Stack segment selector */
mov $(SelectorVideo), %ax
mov %ax, %gs /* Video segment selector(dest) */
mov $(TopOfStack), %esp
movb $0xC, %ah /* 0000: Black Back 1100: Red Front */
xor %esi, %esi
xor %edi, %edi
movl $(OffsetPMMessage), %esi
movl $((80 * 10 + 0) * 2), %edi
cld /* Clear DF flag. */
/* Display a string from %esi(string offset) to %edi(video segment). */
CODE32.1:
lodsb /* Load a byte from source */
test %al, %al
jz CODE32.2
mov %ax, %gs:(%edi)
add $2, %edi
jmp CODE32.1
CODE32.2:
mov $(SelectorLDT), %ax
lldt %ax
ljmp $(SelectorLDTCodeA), $0
/* Get the length of 32-bit segment code. */
.set SegCode32Len, . - LABEL_SEG_CODE32