aboutsummaryrefslogtreecommitdiff
path: root/mubios/arch/mode_switch.S
blob: 9dc0ab5063ef5391ee1fd91722e3fc234127cfbe (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# RCC-Project
#
# Copyright (C) 2026 Richard Qin
#
# This file is part of RCC-Project.
#
# RCC-Project is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# RCC-Project is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# RCC-Project; see the file COPYING.  If not see
# <http://www.gnu.org/licenses/>.

	.intel_syntax noprefix
	.code16

	.section .text16.mode, "ax"

	.extern post_main
	.extern bios_service_dispatch
	
	.global enter_protected_mode
	.type enter_protected_mode, @function

enter_protected_mode:
	cli

	# Enable A20 through the fast A20 gate.

	in al, 0x92
	or al, 0x02
	and al, 0xFE
	out 0x92, al

	# Make data references address the ROM code segment.

	push cs
	pop ds

	call 1f
1:
	pop bx
	add bx, gdt_descriptor - 1b
	lgdt [bx]

	# Enable protected mode
	mov eax, cr0
	or eax, 0x00000001
	mov cr0, eax

	# Reload CS with the 32-bit code selector.
	.byte 0x66
	.byte 0xEA
	.long protected_mode_entry
	.word 0x0008

	.global enter_protected_service
	.type enter_protected_service, @function

enter_protected_service:
	cli

	in al, 0x92
	or al, 0x02
	and al, 0xFE
	out 0x92, al

	push cs
	pop ds

	call 2f
2:
	pop bx
	add bx, gdt_descriptor - 2b
	lgdt [bx]

	mov eax, cr0
	or eax, 0x00000001
	mov cr0, eax

	.byte 0x66
	.byte 0xEA
	.long protected_service_entry
	.word 0x0008

	.align 8

gdt_start:
	.quad 0x0000000000000000

gdt_code:
	.word 0xFFFF
	.word 0x0000
	.byte 0x00
	.byte 0x9A
	.byte 0xCF
	.byte 0x00

gdt_data:
	.word 0xFFFF
	.word 0x0000
	.byte 0x00
	.byte 0x92
	.byte 0xCF
	.byte 0x00

gdt_code16:
	.word 0xFFFF
	.word 0x0000
	.byte 0x0F
	.byte 0x9A
	.byte 0x0F
	.byte 0x00

gdt_end:

gdt_descriptor:
	.word gdt_end - gdt_start - 1
	.long gdt_start

	.code32

	.section .text32, "ax"

	.global protected_mode_entry
	.type protected_mode_entry, @function

protected_mode_entry:
	mov ax, 0x0010
	mov ds, ax
	mov es, ax
	mov ss, ax
	mov fs, ax
	mov gs, ax

	mov esp, 0x00007000

	call post_main

	call leave_protected_mode

protected_mode_halt:
	hlt
	jmp protected_mode_halt

	.global protected_service_entry
	.type protected_service_entry, @function

protected_service_entry:
	mov ax, 0x0010
	mov ds, ax
	mov es, ax
	mov ss, ax
	mov fs, ax
	mov gs, ax

	mov esp, 0x00007000

	push esi
	push edi
	call bios_service_dispatch
	add esp, 8

	call leave_service_protected

protected_service_halt:
	hlt
	jmp protected_service_halt

	.global leave_protected_mode
	.type leave_protected_mode, @function

	.extern __rom_start
	.extern protected16_return
	.extern real_mode_return

leave_protected_mode:
	# Keep the original 16-bit return address at 0x7FFE intact.
	sub esp, 12

	# Stack[0]: protected-mode 16-bit code offset
	# Stack[2]: protected-mode 16-bit code selector
	mov eax, OFFSET FLAT:protected16_return
	mov ebx, OFFSET FLAT:__rom_start
	sub eax, ebx
	mov word ptr [esp], ax
	mov word ptr [esp + 2], 0x0018

	# Stack[4]: real-mode offset
	# Stack[6]: real-mode segment
	mov eax, OFFSET FLAT:real_mode_return
	mov ebx, OFFSET FLAT:__rom_start
	sub eax, ebx
	mov word ptr [esp + 4], ax

	shr ebx, 4
	mov word ptr [esp + 6], bx

	# Enter the 16-bit protected-mode code segment.
	.byte 0x66
	.byte 0xFF, 0x2C, 0x24

	.global leave_service_protected
	.type leave_service_protected, @function

leave_service_protected:
	sub esp, 16

	mov eax, OFFSET FLAT:protected16_service_return
	mov ebx, OFFSET FLAT:__rom_start
	sub eax, ebx
	mov word ptr [esp], ax
	mov word ptr [esp + 2], 0x0018

	mov eax, OFFSET FLAT:real_mode_service_return
	mov ebx, OFFSET FLAT:__rom_start
	sub eax, ebx
	mov word ptr [esp + 4], ax

	shr ebx, 4
	mov word ptr [esp + 6], bx
	mov dword ptr [esp + 8], ebp

	.byte 0x66
	.byte 0xFF, 0x2C, 0x24

	.code16

	.section .text16.mode, "ax"

	.global protected16_return
	.type protected16_return, @function

protected16_return:
	mov bx, sp
	add bx, 4

	# Leave protected mode while already using a 16-bit code segment.
	mov eax, cr0
	and eax, 0xFFFFFFFE
	mov cr0, eax

	# Jump to the prepared real-mode CS:IP pointer.
	.byte 0xFF, 0x2F

	.global protected16_service_return
	.type protected16_service_return, @function

protected16_service_return:
	mov bx, sp
	add bx, 4

	mov eax, cr0
	and eax, 0xFFFFFFFE
	mov cr0, eax

	.byte 0xFF, 0x2F

	.global real_mode_return
	.type real_mode_return, @function

real_mode_return:
	xor ax, ax
	mov ds, ax
	mov es, ax
	mov ss, ax
	mov fs, ax
	mov gs, ax
	mov sp, 0x7FFE

	ret

	.global real_mode_service_return
	.type real_mode_service_return, @function

real_mode_service_return:
	mov bx, sp
	mov si, word ptr [bx + 8]
	mov di, word ptr [bx + 10]

	mov ss, di
	mov sp, si
	ret