Subversion Repositories HelenOS

Rev

Rev 2222 | Rev 2692 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2222 Rev 2302
1
#
1
#
2
# Copyright (c) 2005 Ondrej Palkovsky
2
# Copyright (c) 2005 Ondrej Palkovsky
3
# Copyright (c) 2006 Martin Decky
3
# Copyright (c) 2006 Martin Decky
4
# All rights reserved.
4
# All rights reserved.
5
#
5
#
6
# Redistribution and use in source and binary forms, with or without
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
7
# modification, are permitted provided that the following conditions
8
# are met:
8
# are met:
9
#
9
#
10
# - Redistributions of source code must retain the above copyright
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
13
#   notice, this list of conditions and the following disclaimer in the
14
#   documentation and/or other materials provided with the distribution.
14
#   documentation and/or other materials provided with the distribution.
15
# - The name of the author may not be used to endorse or promote products
15
# - The name of the author may not be used to endorse or promote products
16
#   derived from this software without specific prior written permission.
16
#   derived from this software without specific prior written permission.
17
#
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
28
#
29
 
29
 
30
#include <arch/boot/boot.h>
30
#include <arch/boot/boot.h>
31
#include <arch/boot/memmap.h>
31
#include <arch/boot/memmap.h>
32
#include <arch/mm/page.h>	
32
#include <arch/mm/page.h>	
33
#include <arch/mm/ptl.h>
33
#include <arch/mm/ptl.h>
34
#include <arch/pm.h>
34
#include <arch/pm.h>
35
#include <arch/cpu.h>
35
#include <arch/cpu.h>
36
#include <arch/cpuid.h>
36
#include <arch/cpuid.h>
37
 
37
 
38
#define START_STACK	(BOOT_OFFSET - BOOT_STACK_SIZE)
38
#define START_STACK	(BOOT_OFFSET - BOOT_STACK_SIZE)
39
	
39
	
40
.section K_TEXT_START, "ax"
40
.section K_TEXT_START, "ax"
41
 
41
 
42
.code32
42
.code32
43
.align 4
43
.align 4
44
.global multiboot_image_start
44
.global multiboot_image_start
45
multiboot_header:
45
multiboot_header:
46
	.long MULTIBOOT_HEADER_MAGIC
46
	.long MULTIBOOT_HEADER_MAGIC
47
	.long MULTIBOOT_HEADER_FLAGS
47
	.long MULTIBOOT_HEADER_FLAGS
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
49
	.long multiboot_header
49
	.long multiboot_header
50
	.long unmapped_ktext_start
50
	.long unmapped_ktext_start
51
	.long 0
51
	.long 0
52
	.long 0
52
	.long 0
53
	.long multiboot_image_start
53
	.long multiboot_image_start
54
 
54
 
55
multiboot_image_start:
55
multiboot_image_start:
56
	movl $START_STACK, %esp			# initialize stack pointer
56
	movl $START_STACK, %esp			# initialize stack pointer
57
	lgdt bootstrap_gdtr				# initialize Global Descriptor Table register
57
	lgdt bootstrap_gdtr				# initialize Global Descriptor Table register
58
 
58
 
59
	movw $gdtselector(KDATA_DES), %cx
59
	movw $gdtselector(KDATA_DES), %cx
60
	movw %cx, %es
60
	movw %cx, %es
61
	movw %cx, %ds							# kernel data + stack
61
	movw %cx, %ds							# kernel data + stack
62
	movw %cx, %ss
62
	movw %cx, %ss
63
	# Simics seems to remove hidden part of GS on entering user mode
63
	# Simics seems to remove hidden part of GS on entering user mode
64
	#  when _visible_ part of GS does not point to user-mode segment
64
	#  when _visible_ part of GS does not point to user-mode segment
65
	movw $gdtselector(UDATA_DES), %cx
65
	movw $gdtselector(UDATA_DES), %cx
66
	movw %cx, %fs
66
	movw %cx, %fs
67
	movw %cx, %gs
67
	movw %cx, %gs
68
	
68
	
69
	jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
69
	jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
70
	multiboot_meeting_point:
70
	multiboot_meeting_point:
71
	
71
	
72
	movl %eax, grub_eax							# save parameters from GRUB
72
	movl %eax, grub_eax							# save parameters from GRUB
73
	movl %ebx, grub_ebx
73
	movl %ebx, grub_ebx
74
	
74
	
75
	# Protected 32-bit. We want to reuse the code-seg descriptor,
75
	# Protected 32-bit. We want to reuse the code-seg descriptor,
76
	# the Default operand size must not be 1 when entering long mode
76
	# the Default operand size must not be 1 when entering long mode
77
	
77
	
78
	movl $0x80000000, %eax  
78
	movl $0x80000000, %eax  
79
 	cpuid
79
 	cpuid
80
 	cmp $0x80000000, %eax						# any function > 80000000h?
80
 	cmp $0x80000000, %eax						# any function > 80000000h?
81
	jbe long_mode_unsupported
81
	jbe long_mode_unsupported
82
	movl $(AMD_CPUID_EXTENDED), %eax			# Extended function code 80000001
82
	movl $(AMD_CPUID_EXTENDED), %eax			# Extended function code 80000001
83
	cpuid
83
	cpuid
84
	bt $29, %edx								# Test if long mode is supported.
84
	bt $29, %edx								# Test if long mode is supported.
85
 	jc long_mode_supported
85
 	jc long_mode_supported
86
 
86
 
87
	long_mode_unsupported:
87
	long_mode_unsupported:
88
		movl $long_mode_msg, %esi
88
		movl $long_mode_msg, %esi
89
		jmp error_halt
89
		jmp error_halt
90
	
90
	
91
	long_mode_supported:
91
	long_mode_supported:
92
	
92
	
93
#ifdef CONFIG_FB
93
#ifdef CONFIG_FB
94
	mov $vesa_init, %esi
94
	mov $vesa_init, %esi
95
	mov $VESA_INIT_SEGMENT << 4, %edi
95
	mov $VESA_INIT_SEGMENT << 4, %edi
96
	mov $e_vesa_init - vesa_init, %ecx
96
	mov $e_vesa_init - vesa_init, %ecx
97
	cld
97
	cld
98
	rep movsb
98
	rep movsb
99
 
99
 
100
	mov $VESA_INIT_SEGMENT << 4, %edi
100
	mov $VESA_INIT_SEGMENT << 4, %edi
101
	jmpl *%edi
101
	jmpl *%edi
102
	
102
	
103
	vesa_meeting_point:
103
	vesa_meeting_point:
104
	
104
	
105
	mov %esi, KA2PA(vesa_ph_addr)
105
	mov %esi, KA2PA(vesa_ph_addr)
106
	mov %di, KA2PA(vesa_height)
106
	mov %di, KA2PA(vesa_height)
107
	shr $16, %edi
107
	shr $16, %edi
108
	mov %di, KA2PA(vesa_width)
108
	mov %di, KA2PA(vesa_width)
109
	mov %bx, KA2PA(vesa_scanline)
109
	mov %bx, KA2PA(vesa_scanline)
110
	shr $16, %ebx
110
	shr $16, %ebx
111
	mov %bx, KA2PA(vesa_bpp)
111
	mov %bx, KA2PA(vesa_bpp)
112
#endif	
112
#endif	
113
	
113
	
114
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
114
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
115
	# Paging is not enabled until after long mode is enabled
115
	# Paging is not enabled until after long mode is enabled
116
	
116
	
117
	movl %cr4, %eax
117
	movl %cr4, %eax
118
	btsl $5, %eax
118
	btsl $5, %eax
119
	movl %eax, %cr4
119
	movl %eax, %cr4
120
 
120
 
121
	# Set up paging tables
121
	# Set up paging tables
122
	
122
	
123
	leal ptl_0, %eax
123
	leal ptl_0, %eax
124
	movl %eax, %cr3
124
	movl %eax, %cr3
125
	
125
	
126
	# Enable long mode
126
	# Enable long mode
127
	
127
	
128
	movl $EFER_MSR_NUM, %ecx	# EFER MSR number
128
	movl $EFER_MSR_NUM, %ecx	# EFER MSR number
129
	rdmsr						# Read EFER
129
	rdmsr						# Read EFER
130
	btsl $AMD_LME_FLAG, %eax	# Set LME=1
130
	btsl $AMD_LME_FLAG, %eax	# Set LME=1
131
	wrmsr						# Write EFER
131
	wrmsr						# Write EFER
132
	
132
	
133
	# Enable paging to activate long mode (set CR0.PG=1)
133
	# Enable paging to activate long mode (set CR0.PG=1)
134
	
134
	
135
	movl %cr0, %eax
135
	movl %cr0, %eax
136
	btsl $31, %eax
136
	btsl $31, %eax
137
	movl %eax, %cr0
137
	movl %eax, %cr0
138
	
138
	
139
	# At this point we are in compatibility mode
139
	# At this point we are in compatibility mode
140
	
140
	
141
	jmpl $gdtselector(KTEXT_DES), $start64
141
	jmpl $gdtselector(KTEXT_DES), $start64
142
 
142
 
143
.code64
143
.code64
144
start64:
144
start64:
145
	movq $(PA2KA(START_STACK)), %rsp
145
	movq $(PA2KA(START_STACK)), %rsp
146
	movl grub_eax, %eax
146
	movl grub_eax, %eax
147
	movl grub_ebx, %ebx
147
	movl grub_ebx, %ebx
148
	
148
	
149
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
149
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
150
	je valid_boot
150
	je valid_boot
151
		
151
		
152
		xorl %ecx, %ecx							# no memory size or map available
152
		xorl %ecx, %ecx							# no memory size or map available
153
		movl %ecx, e801memorysize
153
		movl %ecx, e801memorysize
154
		movl %ecx, e820counter
154
		movl %ecx, e820counter
155
		
155
		
156
		jmp invalid_boot
156
		jmp invalid_boot
157
		
157
		
158
	valid_boot:
158
	valid_boot:
159
		
159
		
160
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
160
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
161
		
161
		
162
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
162
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
163
		jc mem_valid
163
		jc mem_valid
164
			
164
			
165
			xorl %ecx, %ecx
165
			xorl %ecx, %ecx
166
			jmp mem_invalid
166
			jmp mem_invalid
167
			
167
			
168
		mem_valid:
168
		mem_valid:
169
		movl 4(%ebx), %ecx						# mbi->mem_lower
169
		movl 4(%ebx), %ecx						# mbi->mem_lower
170
		addl 8(%ebx), %ecx						# mbi->mem_upper
170
		addl 8(%ebx), %ecx						# mbi->mem_upper
171
		
171
		
172
		mem_invalid:
172
		mem_invalid:
173
		movl %ecx, e801memorysize
173
		movl %ecx, e801memorysize
174
		
174
		
175
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
175
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
176
		jc mods_valid
176
		jc mods_valid
177
			
177
			
178
			xorq %rcx, %rcx
178
			xorq %rcx, %rcx
179
			movq %rcx, init
179
			movq %rcx, init
180
			jmp mods_end
180
			jmp mods_end
181
		
181
		
182
		mods_valid:
182
		mods_valid:
183
		
183
		
184
		xorq %rcx, %rcx
184
		xorq %rcx, %rcx
185
		movl 20(%ebx), %ecx						# mbi->mods_count
185
		movl 20(%ebx), %ecx						# mbi->mods_count
186
		movq %rcx, init
186
		movq %rcx, init
187
		
187
		
188
		cmpl $0, %ecx
188
		cmpl $0, %ecx
189
		je mods_end
189
		je mods_end
190
		
190
		
191
		movl 24(%ebx), %esi						# mbi->mods_addr
191
		movl 24(%ebx), %esi						# mbi->mods_addr
192
		movq $init, %rdi
192
		movq $init, %rdi
193
		
193
		
194
		mods_loop:
194
		mods_loop:
195
			
195
			
196
			xorq %rdx, %rdx
196
			xorq %rdx, %rdx
197
			movl 0(%esi), %edx					# mods->mod_start
197
			movl 0(%esi), %edx					# mods->mod_start
198
			movq $0xffff800000000000, %r10
198
			movq $0xffff800000000000, %r10
199
			addq %r10, %rdx
199
			addq %r10, %rdx
200
			movq %rdx, 8(%rdi)
200
			movq %rdx, 8(%rdi)
201
			
201
			
202
			xorq %rdx, %rdx
202
			xorq %rdx, %rdx
203
			movl 4(%esi), %edx
203
			movl 4(%esi), %edx
204
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
204
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
205
			movq %rdx, 16(%rdi)
205
			movq %rdx, 16(%rdi)
206
			
206
			
207
			addl $16, %esi
207
			addl $16, %esi
208
			addq $16, %rdi
208
			addq $16, %rdi
209
			
209
			
210
			loop mods_loop
210
			loop mods_loop
211
			
211
			
212
		mods_end:
212
		mods_end:
213
		
213
		
214
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
214
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
215
		jc mmap_valid
215
		jc mmap_valid
216
			
216
			
217
			xorl %edx, %edx
217
			xorl %edx, %edx
218
			jmp mmap_invalid
218
			jmp mmap_invalid
219
			
219
			
220
		mmap_valid:
220
		mmap_valid:
221
		movl 44(%ebx), %ecx						# mbi->mmap_length
221
		movl 44(%ebx), %ecx						# mbi->mmap_length
222
		movl 48(%ebx), %esi						# mbi->mmap_addr
222
		movl 48(%ebx), %esi						# mbi->mmap_addr
223
		movq $e820table, %rdi
223
		movq $e820table, %rdi
224
		xorl %edx, %edx
224
		xorl %edx, %edx
225
		
225
		
226
		mmap_loop:
226
		mmap_loop:
227
			cmpl $0, %ecx
227
			cmpl $0, %ecx
228
			jle mmap_end
228
			jle mmap_end
229
			
229
			
230
			movl 4(%esi), %eax					# mmap->base_addr_low
230
			movl 4(%esi), %eax					# mmap->base_addr_low
231
			movl %eax, (%rdi)
231
			movl %eax, (%rdi)
232
			
232
			
233
			movl 8(%esi), %eax					# mmap->base_addr_high
233
			movl 8(%esi), %eax					# mmap->base_addr_high
234
			movl %eax, 4(%rdi)
234
			movl %eax, 4(%rdi)
235
			
235
			
236
			movl 12(%esi), %eax					# mmap->length_low
236
			movl 12(%esi), %eax					# mmap->length_low
237
			movl %eax, 8(%rdi)
237
			movl %eax, 8(%rdi)
238
			
238
			
239
			movl 16(%esi), %eax					# mmap->length_high
239
			movl 16(%esi), %eax					# mmap->length_high
240
			movl %eax, 12(%rdi)
240
			movl %eax, 12(%rdi)
241
			
241
			
242
			movl 20(%esi), %eax					# mmap->type
242
			movl 20(%esi), %eax					# mmap->type
243
			movl %eax, 16(%rdi)
243
			movl %eax, 16(%rdi)
244
			
244
			
245
			movl (%esi), %eax					# mmap->size
245
			movl (%esi), %eax					# mmap->size
246
			addl $0x4, %eax
246
			addl $0x4, %eax
247
			addl %eax, %esi
247
			addl %eax, %esi
248
			subl %eax, %ecx
248
			subl %eax, %ecx
249
			addq $MEMMAP_E820_RECORD_SIZE, %rdi
249
			addq $MEMMAP_E820_RECORD_SIZE, %rdi
250
			incl %edx
250
			incl %edx
251
			jmp mmap_loop
251
			jmp mmap_loop
252
		
252
		
253
		mmap_end:
253
		mmap_end:
254
		
254
		
255
		mmap_invalid:
255
		mmap_invalid:
256
		movl %edx, e820counter
256
		movl %edx, e820counter
257
		
257
		
258
	invalid_boot:
258
	invalid_boot:
259
	
259
	
260
#ifdef CONFIG_SMP
260
#ifdef CONFIG_SMP
261
	
261
	
262
	# copy AP bootstrap routines below 1 MB
262
	# copy AP bootstrap routines below 1 MB
263
	
263
	
264
	movq $BOOT_OFFSET, %rsi
264
	movq $BOOT_OFFSET, %rsi
265
	movq $AP_BOOT_OFFSET, %rdi
265
	movq $AP_BOOT_OFFSET, %rdi
266
	movq $_hardcoded_unmapped_size, %rcx
266
	movq $_hardcoded_unmapped_size, %rcx
267
	cld
267
	cld
268
	rep movsb
268
	rep movsb
269
	
269
	
270
#endif
270
#endif
271
	
271
	
272
	call main_bsp   # never returns
272
	call main_bsp   # never returns
273
	
273
	
274
	cli
274
	cli
275
	hlt
275
	hlt
276
 
276
 
277
#ifdef CONFIG_FB
277
#ifdef CONFIG_FB
278
.code32
278
.code32
279
vesa_init:
279
vesa_init:
280
	jmp $gdtselector(VESA_INIT_DES), $vesa_init_real - vesa_init
280
	jmp $gdtselector(VESA_INIT_DES), $vesa_init_real - vesa_init
281
	
281
	
282
.code16
282
.code16
283
vesa_init_real:
283
vesa_init_real:
284
	
284
	
285
	mov %cr0, %eax
285
	mov %cr0, %eax
286
	and $~1, %eax
286
	and $~1, %eax
287
	mov %eax, %cr0
287
	mov %eax, %cr0
288
	
288
	
289
	jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
289
	jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
290
	
290
	
291
vesa_init_real2:
291
vesa_init_real2:
292
	
292
	
293
	mov $VESA_INIT_SEGMENT, %bx
293
	mov $VESA_INIT_SEGMENT, %bx
294
	
294
	
295
	mov %bx, %es
295
	mov %bx, %es
296
	mov %bx, %fs
296
	mov %bx, %fs
297
	mov %bx, %gs
297
	mov %bx, %gs
298
	mov %bx, %ds
298
	mov %bx, %ds
299
	mov %bx, %ss
299
	mov %bx, %ss
300
	
300
	
301
	movl $0x0000fffc, %esp
301
	movl $0x0000fffc, %esp
302
    movl $0x0000fffc, %ebp
302
    movl $0x0000fffc, %ebp
303
	
303
	
304
#define VESA_INFO_SIZE 1024
304
#define VESA_INFO_SIZE 1024
305
 
305
 
-
 
306
#define VESA_MODE_ATTRIBUTES_OFFSET 0
306
#define VESA_MODE_LIST_PTR_OFFSET 14
307
#define VESA_MODE_LIST_PTR_OFFSET 14
-
 
308
#define VESA_MODE_SCANLINE_OFFSET 16
307
#define VESA_MODE_WIDTH_OFFSET 18
309
#define VESA_MODE_WIDTH_OFFSET 18
308
#define VESA_MODE_HEIGHT_OFFSET 20
310
#define VESA_MODE_HEIGHT_OFFSET 20
309
#define VESA_MODE_BPP_OFFSET 25
311
#define VESA_MODE_BPP_OFFSET 25
310
#define VESA_MODE_SCANLINE_OFFSET 16
-
 
311
#define VESA_MODE_PHADDR_OFFSET 40
312
#define VESA_MODE_PHADDR_OFFSET 40
312
 
313
 
313
#define VESA_END_OF_MODES 0xffff
314
#define VESA_END_OF_MODES 0xffff
314
 
315
 
315
#define VESA_OK 0x4f
316
#define VESA_OK 0x4f
316
 
317
 
317
#define VESA_GET_INFO 0x4f00
318
#define VESA_GET_INFO 0x4f00
318
#define VESA_GET_MODE_INFO 0x4f01
319
#define VESA_GET_MODE_INFO 0x4f01
319
#define VESA_SET_MODE 0x4f02
320
#define VESA_SET_MODE 0x4f02
-
 
321
#define VESA_SET_PALETTE 0x4f09
320
 
322
 
321
#define CONFIG_VESA_BPP_a 255
323
#define CONFIG_VESA_BPP_a 255
322
 
324
 
323
#if CONFIG_VESA_BPP == 24
325
#if CONFIG_VESA_BPP == 24
324
#undef CONFIG_VESA_BPP_a
-
 
325
#define CONFIG_VESA_BPP_a 32
326
#define CONFIG_VESA_BPP_VARIANT 32
326
#endif
327
#endif
327
	
328
 
328
	mov $VESA_GET_INFO, %ax
329
	mov $VESA_GET_INFO, %ax
329
	mov $e_vesa_init - vesa_init, %di
330
	mov $e_vesa_init - vesa_init, %di
330
	push %di
331
	push %di
331
	int $0x10
332
	int $0x10
332
	
333
	
333
	pop %di
334
	pop %di
334
	cmp $VESA_OK, %al
335
	cmp $VESA_OK, %al
335
	jnz 0f
336
	jnz 0f
336
	
337
	
337
	mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
338
	mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
338
	mov %si, %gs
339
	mov %si, %gs
339
	mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
340
	mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
340
	
341
	
341
	add $VESA_INFO_SIZE, %di
342
	add $VESA_INFO_SIZE, %di
342
	
343
 
343
1:# Try next mode
344
1:# Try next mode
344
	mov %gs:(%si), %cx
345
	mov %gs:(%si), %cx
345
	cmp $VESA_END_OF_MODES, %cx
346
	cmp $VESA_END_OF_MODES, %cx
346
	jz 0f
347
	jz 0f
347
	
348
	
348
	inc %si
349
	inc %si
349
	inc %si
350
	inc %si
350
	push %cx
351
	push %cx
351
	push %di
352
	push %di
352
	push %si
353
	push %si
353
	mov $VESA_GET_MODE_INFO, %ax
354
	mov $VESA_GET_MODE_INFO, %ax
354
	int $0x10
355
	int $0x10
355
	
356
	
356
	pop %si
357
	pop %si
357
	pop %di
358
	pop %di
358
	pop %cx
359
	pop %cx
359
	cmp $VESA_OK, %al
360
	cmp $VESA_OK, %al
360
	jnz 0f
361
	jnz 0f
361
	
362
	
362
	mov $CONFIG_VESA_WIDTH, %ax
363
	mov $CONFIG_VESA_WIDTH, %ax
363
	cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
364
	cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
364
	jnz 1b
365
	jnz 1b
365
	
366
	
366
	mov $CONFIG_VESA_HEIGHT, %ax
367
	mov $CONFIG_VESA_HEIGHT, %ax
367
	cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
368
	cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
368
	jnz 1b
369
	jnz 1b
369
	
370
	
370
	mov $CONFIG_VESA_BPP, %al
371
	mov $CONFIG_VESA_BPP, %al
371
	cmp VESA_MODE_BPP_OFFSET(%di), %al
372
	cmp VESA_MODE_BPP_OFFSET(%di), %al
-
 
373
 
-
 
374
#ifdef CONFIG_VESA_BPP_VARIANT
372
	jz 2f
375
	jz 2f
373
	
376
	
374
	mov $CONFIG_VESA_BPP_a, %al
377
	mov $CONFIG_VESA_BPP_VARIANT, %al
375
	cmp VESA_MODE_BPP_OFFSET(%di), %al
378
	cmp VESA_MODE_BPP_OFFSET(%di), %al
-
 
379
#endif
376
	jnz 1b
380
	jnz 1b
377
	
381
	
378
2:
382
2:
379
	
383
	
380
	mov %cx, %bx
384
	mov %cx, %bx
381
	or $0xc000, %bx
385
	or $0xc000, %bx
382
	push %di
386
	push %di
383
	mov $VESA_SET_MODE, %ax
387
	mov $VESA_SET_MODE, %ax
384
	int $0x10
388
	int $0x10
385
	
389
	
386
	pop %di
390
	pop %di
387
	cmp $VESA_OK, %al
391
	cmp $VESA_OK, %al
388
	jnz 0f
392
	jnz 0f
-
 
393
 
-
 
394
#if CONFIG_VESA_BPP == 8
-
 
395
	
-
 
396
	# Set 3:2:3 VGA palette
-
 
397
	
-
 
398
	mov VESA_MODE_ATTRIBUTES_OFFSET(%di), %ax
-
 
399
	push %di
-
 
400
	mov $vga323 - vesa_init, %di
-
 
401
	mov $0x100, %ecx
-
 
402
	
-
 
403
	bt $5, %ax							# Test if VGA compatible registers are present
-
 
404
	jnc vga_compat
-
 
405
		
-
 
406
		# Try VESA routine to set palette
-
 
407
		
-
 
408
		mov $VESA_SET_PALETTE, %ax
-
 
409
		xor %bl, %bl
-
 
410
		xor %dx, %dx
-
 
411
		int $0x10
-
 
412
		
-
 
413
		jmp vga_not_compat
-
 
414
	
-
 
415
	vga_compat:
-
 
416
		
-
 
417
		# Try VGA registers to set palette
-
 
418
		
-
 
419
		movw $0x3c6, %dx						# Set palette mask
-
 
420
		movb $0xff, %al
-
 
421
		outb %al, %dx
-
 
422
		
-
 
423
		movw $0x3c8, %dx						# First index to set
-
 
424
		xor %al, %al
-
 
425
		outb %al, %dx
-
 
426
		
-
 
427
		movw $0x3c9, %dx						# Data port
-
 
428
		vga_loop:
-
 
429
			movb %es:2(%di), %al
-
 
430
			outb %al, %dx
-
 
431
			
-
 
432
			movb %es:1(%di), %al
-
 
433
			outb %al, %dx
-
 
434
			
-
 
435
			movb %es:(%di), %al
-
 
436
			outb %al, %dx
-
 
437
			
-
 
438
			addw $4, %di
-
 
439
			loop vga_loop
-
 
440
		
-
 
441
	vga_not_compat:
-
 
442
	
-
 
443
	pop %di
-
 
444
	
-
 
445
#endif
389
	
446
	
390
	mov VESA_MODE_PHADDR_OFFSET(%di), %esi
447
	mov VESA_MODE_PHADDR_OFFSET(%di), %esi
391
	mov VESA_MODE_WIDTH_OFFSET(%di), %ax
448
	mov VESA_MODE_WIDTH_OFFSET(%di), %ax
392
	shl $16, %eax
449
	shl $16, %eax
393
	mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
450
	mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
394
	mov VESA_MODE_BPP_OFFSET(%di), %bl
451
	mov VESA_MODE_BPP_OFFSET(%di), %bl
395
	xor %bh, %bh
452
	xor %bh, %bh
396
	shl $16, %ebx
453
	shl $16, %ebx
397
	mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
454
	mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
398
	mov %eax, %edi
455
	mov %eax, %edi
399
	
456
	
400
8:
457
8:
401
	
458
	
402
	mov %cr0, %eax
459
	mov %cr0, %eax
403
	or $1, %eax
460
	or $1, %eax
404
	mov %eax, %cr0
461
	mov %eax, %cr0
405
	
462
	
406
	jmp 9f
463
	jmp 9f
407
9:
464
9:
408
	
465
	
409
	ljmpl $gdtselector(KTEXT32_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
466
	ljmpl $gdtselector(KTEXT32_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
410
	
467
	
411
0:# No prefered mode found
468
0:# No prefered mode found
412
	mov $0x111, %cx
469
	mov $0x111, %cx
413
	push %di
470
	push %di
414
	push %cx
471
	push %cx
415
	mov $VESA_GET_MODE_INFO, %ax
472
	mov $VESA_GET_MODE_INFO, %ax
416
	int $0x10
473
	int $0x10
417
	
474
	
418
	pop %cx
475
	pop %cx
419
	pop %di
476
	pop %di
420
	cmp $VESA_OK, %al
477
	cmp $VESA_OK, %al
421
	jnz 1f
478
	jnz 1f
422
	jz 2b						# Force relative jump
479
	jz 2b						# Force relative jump
423
 
480
 
424
1:
481
1:
425
	mov $0x0003, %ax
482
	mov $0x0003, %ax
426
	int $0x10
483
	int $0x10
427
	mov $0xffffffff, %edi		# EGA text mode used, because of problems with VESA
484
	mov $0xffffffff, %edi		# EGA text mode used, because of problems with VESA
428
	xor %ax, %ax
485
	xor %ax, %ax
429
	jz 8b						# Force relative jump
486
	jz 8b						# Force relative jump
430
	
487
 
-
 
488
vga323:
-
 
489
#include "vga323.pal"	
431
	
490
	
432
.code32
491
.code32
433
vesa_init_protect:
492
vesa_init_protect:
434
	movw $gdtselector(KDATA_DES), %cx
493
	movw $gdtselector(KDATA_DES), %cx
435
	movw %cx, %es
494
	movw %cx, %es
436
	movw %cx, %ds							# kernel data + stack
495
	movw %cx, %ds							# kernel data + stack
437
	movw %cx, %ss
496
	movw %cx, %ss
438
	# Simics seems to remove hidden part of GS on entering user mode
497
	# Simics seems to remove hidden part of GS on entering user mode
439
	#  when _visible_ part of GS does not point to user-mode segment
498
	#  when _visible_ part of GS does not point to user-mode segment
440
	movw $gdtselector(UDATA_DES), %cx
499
	movw $gdtselector(UDATA_DES), %cx
441
	movw %cx, %fs
500
	movw %cx, %fs
442
	movw %cx, %gs
501
	movw %cx, %gs
443
	
502
	
444
	movl $START_STACK, %esp			# initialize stack pointer
503
	movl $START_STACK, %esp			# initialize stack pointer
445
	
504
	
446
	jmpl $gdtselector(KTEXT32_DES), $vesa_meeting_point
505
	jmpl $gdtselector(KTEXT32_DES), $vesa_meeting_point
447
	
506
	
448
.align 4
507
.align 4
449
e_vesa_init:
508
e_vesa_init:
450
#endif
509
#endif
451
 
510
 
452
# Print string from %esi to EGA display (in red) and halt
511
# Print string from %esi to EGA display (in red) and halt
453
error_halt:
512
error_halt:
454
	movl $0xb8000, %edi						# base of EGA text mode memory
513
	movl $0xb8000, %edi						# base of EGA text mode memory
455
	xorl %eax, %eax
514
	xorl %eax, %eax
456
	
515
	
457
	movw $0x3d4, %dx						# read bits 8 - 15 of the cursor address
516
	movw $0x3d4, %dx						# read bits 8 - 15 of the cursor address
458
	movb $0xe, %al
517
	movb $0xe, %al
459
	outb %al, %dx
518
	outb %al, %dx
460
	
519
	
461
	movw $0x3d5, %dx
520
	movw $0x3d5, %dx
462
	inb %dx, %al
521
	inb %dx, %al
463
	shl $8, %ax
522
	shl $8, %ax
464
	
523
	
465
	movw $0x3d4, %dx						# read bits 0 - 7 of the cursor address
524
	movw $0x3d4, %dx						# read bits 0 - 7 of the cursor address
466
	movb $0xf, %al
525
	movb $0xf, %al
467
	outb %al, %dx
526
	outb %al, %dx
468
	
527
	
469
	movw $0x3d5, %dx
528
	movw $0x3d5, %dx
470
	inb %dx, %al
529
	inb %dx, %al
471
	
530
	
472
	cmp $1920, %ax
531
	cmp $1920, %ax
473
	jbe cursor_ok
532
	jbe cursor_ok
474
		movw $1920, %ax						# sanity check for the cursor on the last line
533
		movw $1920, %ax						# sanity check for the cursor on the last line
475
	cursor_ok:
534
	cursor_ok:
476
	
535
	
477
	movw %ax, %bx
536
	movw %ax, %bx
478
	shl $1, %eax
537
	shl $1, %eax
479
	addl %eax, %edi
538
	addl %eax, %edi
480
	
539
	
481
	movw $0x0c00, %ax						# black background, light red foreground
540
	movw $0x0c00, %ax						# black background, light red foreground
482
	cld
541
	cld
483
	
542
	
484
	ploop:
543
	ploop:
485
		lodsb
544
		lodsb
486
		cmp $0, %al
545
		cmp $0, %al
487
		je ploop_end
546
		je ploop_end
488
		stosw
547
		stosw
489
		inc %bx	
548
		inc %bx	
490
		jmp ploop
549
		jmp ploop
491
	ploop_end:
550
	ploop_end:
492
	
551
	
493
	movw $0x3d4, %dx						# write bits 8 - 15 of the cursor address
552
	movw $0x3d4, %dx						# write bits 8 - 15 of the cursor address
494
	movb $0xe, %al
553
	movb $0xe, %al
495
	outb %al, %dx
554
	outb %al, %dx
496
	
555
	
497
	movw $0x3d5, %dx
556
	movw $0x3d5, %dx
498
	movb %bh, %al
557
	movb %bh, %al
499
	outb %al, %dx
558
	outb %al, %dx
500
	
559
	
501
	movw $0x3d4, %dx						# write bits 0 - 7 of the cursor address
560
	movw $0x3d4, %dx						# write bits 0 - 7 of the cursor address
502
	movb $0xf, %al
561
	movb $0xf, %al
503
	outb %al, %dx
562
	outb %al, %dx
504
	
563
	
505
	movw $0x3d5, %dx
564
	movw $0x3d5, %dx
506
	movb %bl, %al
565
	movb %bl, %al
507
	outb %al, %dx
566
	outb %al, %dx
508
		
567
		
509
	cli
568
	cli
510
	hlt
569
	hlt
511
				
570
				
512
.section K_DATA_START, "aw", @progbits
571
.section K_DATA_START, "aw", @progbits
513
.align 4096
572
.align 4096
514
 
573
 
515
# Identical mapping of first 64MB and the same of -2GB -> 0	
574
# Identical mapping of first 64MB and the same of -2GB -> 0	
516
.global ptl_2
575
.global ptl_2
517
ptl_2:	
576
ptl_2:	
518
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
577
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
519
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
578
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
520
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
579
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
521
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
580
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
522
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
581
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
523
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
582
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
524
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
583
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
525
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
584
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
526
	.quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
585
	.quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
527
	.quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
586
	.quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
528
	.quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
587
	.quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
529
	.quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
588
	.quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
530
	.quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
589
	.quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
531
	.quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
590
	.quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
532
	.quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
591
	.quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
533
	.quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
592
	.quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
534
	.quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
593
	.quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
535
	.quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
594
	.quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
536
	.quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
595
	.quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
537
	.quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
596
	.quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
538
	.quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
597
	.quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
539
	.quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
598
	.quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
540
	.quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
599
	.quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
541
	.quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
600
	.quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
542
	.quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
601
	.quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
543
	.quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
602
	.quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
544
	.quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
603
	.quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
545
	.quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
604
	.quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
546
	.quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
605
	.quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
547
	.quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
606
	.quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
548
	.quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
607
	.quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
549
	.quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
608
	.quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
550
	
609
	
551
.align 4096
610
.align 4096
552
.global ptl_1
611
.global ptl_1
553
ptl_1:
612
ptl_1:
554
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
613
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
555
	.fill 509,8,0
614
	.fill 509,8,0
556
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
615
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
557
	.fill 1,8,0
616
	.fill 1,8,0
558
	
617
	
559
.align 4096
618
.align 4096
560
.global ptl_0
619
.global ptl_0
561
ptl_0:
620
ptl_0:
562
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
621
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
563
	.fill 255,8,0
622
	.fill 255,8,0
564
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
623
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
565
	.fill 254,8,0
624
	.fill 254,8,0
566
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
625
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
567
 
626
 
568
.global bootstrap_gdtr
627
.global bootstrap_gdtr
569
bootstrap_gdtr:
628
bootstrap_gdtr:
570
	.word gdtselector(GDT_ITEMS)
629
	.word gdtselector(GDT_ITEMS)
571
	.long KA2PA(gdt)
630
	.long KA2PA(gdt)
572
 
631
 
573
grub_eax:
632
grub_eax:
574
	.long 0
633
	.long 0
575
 
634
 
576
grub_ebx:
635
grub_ebx:
577
	.long 0
636
	.long 0
578
 
637
 
579
long_mode_msg:
638
long_mode_msg:
580
	.ascii "64 bit long mode not supported. System halted.\0"
639
	.asciz "64 bit long mode not supported. System halted."
581
 
640