Subversion Repositories HelenOS

Rev

Rev 2221 | Rev 2302 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
#
2071 jermar 2
# Copyright (c) 2001-2004 Jakub Jermar
3
# Copyright (c) 2005-2006 Martin Decky
1 jermar 4
# All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
8
# are met:
9
#
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
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
16
#   derived from this software without specific prior written permission.
17
#
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
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
29
 
222 decky 30
#include <arch/boot/boot.h>
506 decky 31
#include <arch/boot/memmap.h>
300 palkovsky 32
#include <arch/mm/page.h>
33
#include <arch/pm.h>
222 decky 34
 
693 decky 35
#define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
36
 
406 jermar 37
.section K_TEXT_START, "ax"
1 jermar 38
 
112 jermar 39
.code32
222 decky 40
.align 4
693 decky 41
.global multiboot_image_start
222 decky 42
multiboot_header:
43
	.long MULTIBOOT_HEADER_MAGIC
44
	.long MULTIBOOT_HEADER_FLAGS
45
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
693 decky 46
	.long multiboot_header
47
	.long unmapped_ktext_start
222 decky 48
	.long 0
49
	.long 0
693 decky 50
	.long multiboot_image_start
222 decky 51
 
52
multiboot_image_start:
693 decky 53
	movl $START_STACK, %esp			# initialize stack pointer
54
	lgdt KA2PA(bootstrap_gdtr)		# initialize Global Descriptor Table register
112 jermar 55
 
1811 decky 56
	movw $selector(KDATA_DES), %cx
280 decky 57
	movw %cx, %es
1639 decky 58
	movw %cx, %fs
280 decky 59
	movw %cx, %gs
693 decky 60
	movw %cx, %ds					# kernel data + stack
280 decky 61
	movw %cx, %ss
235 decky 62
 
1811 decky 63
	jmpl $selector(KTEXT_DES), $multiboot_meeting_point
271 decky 64
	multiboot_meeting_point:
65
 
2222 decky 66
	movl %eax, grub_eax					# save parameters from GRUB
67
	movl %ebx, grub_ebx
1639 decky 68
 
2220 decky 69
	xorl %eax, %eax  
70
 	cpuid
71
 	cmp $0x0, %eax						# any function > 0?
72
	jbe pse_unsupported
2221 decky 73
	movl $0x1, %eax						# Basic function code 1
2220 decky 74
	cpuid
75
	bt $3, %edx							# Test if PSE is supported
76
	jc pse_supported
77
 
78
	pse_unsupported:
79
		movl $pse_msg, %esi
80
		jmp error_halt
81
 
82
	pse_supported:
83
 
1287 vana 84
#ifdef CONFIG_FB
1639 decky 85
	mov $vesa_init, %esi
86
	mov $VESA_INIT_SEGMENT << 4, %edi
87
	mov $e_vesa_init - vesa_init, %ecx
88
	cld
89
	rep movsb
1287 vana 90
 
1639 decky 91
	mov $VESA_INIT_SEGMENT << 4, %edi
2089 decky 92
	jmpl *%edi
1639 decky 93
 
94
	vesa_meeting_point:
95
 
96
	mov %esi, KA2PA(vesa_ph_addr)
97
	mov %di, KA2PA(vesa_height)
98
	shr $16, %edi
99
	mov %di, KA2PA(vesa_width)
100
	mov %bx, KA2PA(vesa_scanline)
101
	shr $16, %ebx
102
	mov %bx, KA2PA(vesa_bpp)
1287 vana 103
#endif	
280 decky 104
 
323 jermar 105
	call map_kernel							# map kernel and turn paging on
235 decky 106
 
2222 decky 107
	movl grub_eax, %eax
108
	movl grub_ebx, %ebx
323 jermar 109
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
280 decky 110
	je valid_boot
111
 
285 decky 112
		xorl %ecx, %ecx							# no memory size or map available
113
		movl %ecx, e801memorysize
114
		movl %ecx, e820counter
115
 
280 decky 116
		jmp invalid_boot
285 decky 117
 
280 decky 118
	valid_boot:
119
 
285 decky 120
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
280 decky 121
 
285 decky 122
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
123
		jc mem_valid
124
 
125
			xorl %ecx, %ecx
126
			jmp mem_invalid
127
 
128
		mem_valid:
129
		movl 4(%ebx), %ecx						# mbi->mem_lower
130
		addl 8(%ebx), %ecx						# mbi->mem_upper
280 decky 131
 
285 decky 132
		mem_invalid:
280 decky 133
		movl %ecx, e801memorysize
134
 
1037 decky 135
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
506 decky 136
		jc mods_valid
137
 
138
			xorl %ecx, %ecx
1037 decky 139
			movl %ecx, init
140
			jmp mods_end
506 decky 141
 
142
		mods_valid:
1037 decky 143
 
628 decky 144
		movl 20(%ebx), %ecx						# mbi->mods_count
1037 decky 145
		movl %ecx, init
146
 
628 decky 147
		cmpl $0, %ecx
1037 decky 148
		je mods_end
506 decky 149
 
628 decky 150
		movl 24(%ebx), %esi						# mbi->mods_addr
1037 decky 151
		movl $init, %edi
628 decky 152
 
1037 decky 153
		mods_loop:
506 decky 154
 
1037 decky 155
			movl 0(%esi), %edx					# mods->mod_start
156
			addl $0x80000000, %edx
157
			movl %edx, 4(%edi)
158
 
159
			movl 4(%esi), %edx
160
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
161
			movl %edx, 8(%edi)
162
 
163
			addl $16, %esi
164
			addl $8	, %edi
165
 
166
			loop mods_loop
167
 
168
		mods_end:
169
 
285 decky 170
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
171
		jc mmap_valid
172
 
173
			xorl %edx, %edx
174
			jmp mmap_invalid
175
 
176
		mmap_valid:
177
		movl 44(%ebx), %ecx						# mbi->mmap_length
178
		movl 48(%ebx), %esi						# mbi->mmap_addr
179
		movl $e820table, %edi
180
		xorl %edx, %edx
280 decky 181
 
285 decky 182
		mmap_loop:
183
			cmpl $0, %ecx
184
			jle mmap_end
185
 
186
			movl 4(%esi), %eax					# mmap->base_addr_low
187
			movl %eax, (%edi)
188
 
189
			movl 8(%esi), %eax					# mmap->base_addr_high
190
			movl %eax, 4(%edi)
191
 
192
			movl 12(%esi), %eax					# mmap->length_low
193
			movl %eax, 8(%edi)
194
 
195
			movl 16(%esi), %eax					# mmap->length_high
196
			movl %eax, 12(%edi)
197
 
198
			movl 20(%esi), %eax					# mmap->type
199
			movl %eax, 16(%edi)
200
 
201
			movl (%esi), %eax					# mmap->size
202
			addl $0x4, %eax
203
			addl %eax, %esi
204
			subl %eax, %ecx
205
			addl $MEMMAP_E820_RECORD_SIZE, %edi
206
			incl %edx
207
			jmp mmap_loop
208
 
209
		mmap_end:
210
 
211
		mmap_invalid:
212
		movl %edx, e820counter
213
 
280 decky 214
	invalid_boot:
215
 
693 decky 216
#ifdef CONFIG_SMP
217
 
218
	# copy AP bootstrap routines below 1 MB
219
 
220
	movl $BOOT_OFFSET, %esi
221
	movl $AP_BOOT_OFFSET, %edi
222
	movl $_hardcoded_unmapped_size, %ecx
223
	cld
224
	rep movsb
225
 
226
#endif
2220 decky 227
 
693 decky 228
	call main_bsp								# never returns
110 jermar 229
 
230
	cli
231
	hlt
232
 
233
.global map_kernel
234
map_kernel:
105 jermar 235
	#
236
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
371 jermar 237
	# For simplicity, we map the entire 4G space.
105 jermar 238
	#
239
	movl %cr4, %ecx
2220 decky 240
	orl $(1 << 4), %ecx							# turn PSE on
241
	andl $(~(1 << 5)), %ecx						# turn PAE off
242
	movl %ecx, %cr4
1 jermar 243
 
2220 decky 244
	movl $(page_directory + 0), %esi
245
	movl $(page_directory + 2048), %edi
371 jermar 246
	xorl %ecx, %ecx
247
	xorl %ebx, %ebx
248
0:
2220 decky 249
	movl $((1 << 7) | (1 << 1) | (1 << 0)), %eax
371 jermar 250
	orl %ebx, %eax
2220 decky 251
	movl %eax, (%esi, %ecx, 4)					# mapping 0x00000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
252
	movl %eax, (%edi, %ecx, 4)					# mapping 0x80000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
253
	addl $(4 * 1024 * 1024), %ebx
1 jermar 254
 
371 jermar 255
	incl %ecx
256
	cmpl $512, %ecx
257
	jl 0b
105 jermar 258
 
371 jermar 259
	movl %esi, %cr3
105 jermar 260
 
261
	movl %cr0, %ebx
2220 decky 262
	orl $(1 << 31), %ebx						# turn paging on
105 jermar 263
	movl %ebx, %cr0
110 jermar 264
	ret
105 jermar 265
 
2220 decky 266
# Print string from %esi to EGA display (in red) and halt
267
error_halt:
268
	movl $0xb8000, %edi						# base of EGA text mode memory
269
	xorl %eax, %eax
270
 
271
	movw $0x3d4, %dx						# read bits 8 - 15 of the cursor address
272
	movb $0xe, %al
273
	outb %al, %dx
274
 
275
	movw $0x3d5, %dx
276
	inb %dx, %al
277
	shl $8, %ax
278
 
279
	movw $0x3d4, %dx						# read bits 0 - 7 of the cursor address
280
	movb $0xf, %al
281
	outb %al, %dx
282
 
283
	movw $0x3d5, %dx
284
	inb %dx, %al
285
 
286
	cmp $1920, %ax
287
	jbe cursor_ok
288
		movw $1920, %ax						# sanity check for the cursor on the last line
289
	cursor_ok:
290
 
291
	movw %ax, %bx
292
	shl $1, %eax
293
	addl %eax, %edi
294
 
295
	movw $0x0c00, %ax						# black background, light red foreground
296
	cld
297
 
298
	ploop:
299
		lodsb
300
		cmp $0, %al
301
		je ploop_end
302
		stosw
303
		inc %bx	
304
		jmp ploop
305
	ploop_end:
306
 
307
	movw $0x3d4, %dx						# write bits 8 - 15 of the cursor address
308
	movb $0xe, %al
309
	outb %al, %dx
310
 
311
	movw $0x3d5, %dx
312
	movb %bh, %al
313
	outb %al, %dx
314
 
315
	movw $0x3d4, %dx						# write bits 0 - 7 of the cursor address
316
	movb $0xf, %al
317
	outb %al, %dx
318
 
319
	movw $0x3d5, %dx
320
	movb %bl, %al
321
	outb %al, %dx
322
 
323
	cli
324
	hlt
325
 
1287 vana 326
#ifdef CONFIG_FB
327
vesa_init:
1639 decky 328
	jmp $selector(VESA_INIT_DES), $vesa_init_real - vesa_init
329
 
1287 vana 330
.code16
1639 decky 331
vesa_init_real:
1287 vana 332
 
1639 decky 333
	mov %cr0, %eax
334
	and $~1, %eax
335
	mov %eax, %cr0
336
 
337
	jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
338
 
339
vesa_init_real2:
340
 
341
	mov $VESA_INIT_SEGMENT, %bx
342
 
343
	mov %bx, %es
344
	mov %bx, %fs
345
	mov %bx, %gs
346
	mov %bx, %ds
347
	mov %bx, %ss
348
 
349
	movl %esp, %eax
350
	movl $0x0000fffc, %esp
351
	movl $0x0000fffc, %ebp
352
	pushl %eax
353
 
1287 vana 354
#define VESA_INFO_SIZE 1024
355
 
1639 decky 356
#define VESA_MODE_LIST_PTR_OFFSET 14
1287 vana 357
#define VESA_MODE_WIDTH_OFFSET 18
358
#define VESA_MODE_HEIGHT_OFFSET 20
359
#define VESA_MODE_BPP_OFFSET 25
360
#define VESA_MODE_SCANLINE_OFFSET 16
361
#define VESA_MODE_PHADDR_OFFSET 40
362
 
1639 decky 363
#define VESA_END_OF_MODES 0xffff
1287 vana 364
 
1639 decky 365
#define VESA_OK 0x4f
1287 vana 366
 
1639 decky 367
#define VESA_GET_INFO 0x4f00
1287 vana 368
#define VESA_GET_MODE_INFO 0x4f01
369
#define VESA_SET_MODE 0x4f02
370
 
371
#define CONFIG_VESA_BPP_a 255
372
 
1639 decky 373
#if CONFIG_VESA_BPP == 24
1287 vana 374
#undef CONFIG_VESA_BPP_a
375
#define CONFIG_VESA_BPP_a 32
376
#endif
377
 
1639 decky 378
	mov $VESA_GET_INFO, %ax
379
	mov $e_vesa_init - vesa_init, %di
380
	push %di
381
	int $0x10
382
 
383
	pop %di
384
	cmp $VESA_OK, %al
385
	jnz 0f
386
 
387
	mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
388
	mov %si, %gs
389
	mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
390
 
391
	add $VESA_INFO_SIZE, %di
1287 vana 392
 
393
1:# Try next mode
1639 decky 394
	mov %gs:(%si), %cx
395
	cmp $VESA_END_OF_MODES, %cx
396
	jz 0f
397
 
398
	inc %si
399
	inc %si
400
	push %cx
401
	push %di
402
	push %si
403
	mov $VESA_GET_MODE_INFO, %ax
404
	int $0x10
405
 
406
	pop %si
407
	pop %di
408
	pop %cx
409
	cmp $VESA_OK, %al
410
	jnz 0f
411
 
412
	mov $CONFIG_VESA_WIDTH, %ax
413
	cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
414
	jnz 1b
415
 
416
	mov $CONFIG_VESA_HEIGHT,%ax
417
	cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
418
	jnz 1b
419
 
420
	mov $CONFIG_VESA_BPP, %al
421
	cmp VESA_MODE_BPP_OFFSET(%di), %al
422
	jz 2f
423
 
424
	mov $CONFIG_VESA_BPP_a, %al
425
	cmp VESA_MODE_BPP_OFFSET(%di), %al
426
	jnz 1b
427
 
1287 vana 428
2:
429
 
1639 decky 430
	mov %cx, %bx
431
	or $0xc000, %bx
432
	push %di
433
	mov $VESA_SET_MODE, %ax
434
	int $0x10
1287 vana 435
 
1639 decky 436
	pop %di
437
	cmp $VESA_OK, %al
438
	jnz 0f
1287 vana 439
 
1639 decky 440
	mov VESA_MODE_PHADDR_OFFSET(%di), %esi
441
	mov VESA_MODE_WIDTH_OFFSET(%di), %ax
442
	shl $16, %eax
443
	mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
444
	mov VESA_MODE_BPP_OFFSET(%di), %bl
445
	xor %bh, %bh
446
	shl $16, %ebx
447
	mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
448
	mov %eax, %edi
449
 
1287 vana 450
8:	
1639 decky 451
 
452
	mov %cr0, %eax
453
	or $1, %eax
1641 decky 454
	mov %eax, %cr0
1639 decky 455
 
456
	jmp 9f
1287 vana 457
9:
1639 decky 458
 
1811 decky 459
	ljmpl $selector(KTEXT_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
1287 vana 460
 
1639 decky 461
0:# No prefered mode found
462
	mov $0x111, %cx
463
	push %di
464
	push %cx
465
	mov $VESA_GET_MODE_INFO, %ax
466
	int $0x10
467
 
468
	pop %cx
469
	pop %di
470
	cmp $VESA_OK, %al
471
	jnz 1f
472
	jz 2b						# Force relative jump
473
 
474
1:
475
	mov $0x0003, %ax
476
	int $0x10
477
	mov $0xffffffff, %edi		# EGA text mode used, because of problems with VESA
478
	xor %ax, %ax
479
	jz 8b						# Force relative jump
480
 
1287 vana 481
 
1639 decky 482
.code32
483
vesa_init_protect:
1811 decky 484
	movw $selector(KDATA_DES), %cx
1639 decky 485
	movw %cx, %es
486
	movw %cx, %fs
487
	movw %cx, %gs
488
	movw %cx, %ds					# kernel data + stack
489
	movw %cx, %ss
490
 
2222 decky 491
	movl $START_STACK, %esp			# initialize stack pointer
492
 
1811 decky 493
	jmpl $selector(KTEXT_DES), $vesa_meeting_point
1287 vana 494
 
495
.align 4
496
e_vesa_init:
497
#endif	
498
 
406 jermar 499
.section K_DATA_START, "aw", @progbits
105 jermar 500
 
501
.align 4096
502
page_directory:
503
	.space 4096, 0
2220 decky 504
 
2222 decky 505
grub_eax:
506
	.long 0
507
 
508
grub_ebx:
509
	.long 0
510
 
2220 decky 511
pse_msg:
512
	.ascii "Page Size Extension not supported. System halted.\0"