Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
164 palkovsky 1
#
288 jermar 2
# Copyright (C) 2005 Ondrej Palkovsky
696 decky 3
# Copyright (C) 2006 Martin Decky
164 palkovsky 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
 
695 decky 30
#include <arch/boot/boot.h>
31
#include <arch/boot/memmap.h>
194 palkovsky 32
#include <arch/mm/page.h>	
188 palkovsky 33
#include <arch/mm/ptl.h>
194 palkovsky 34
#include <arch/pm.h>
251 palkovsky 35
#include <arch/cpu.h>
275 palkovsky 36
#include <arch/cpuid.h>
164 palkovsky 37
 
694 decky 38
#define START_STACK	(BOOT_OFFSET - BOOT_STACK_SIZE)
275 palkovsky 39
 
406 jermar 40
.section K_TEXT_START, "ax"
695 decky 41
 
680 decky 42
.code32
43
.align 4
695 decky 44
.global multiboot_image_start
680 decky 45
multiboot_header:
46
	.long MULTIBOOT_HEADER_MAGIC
47
	.long MULTIBOOT_HEADER_FLAGS
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
694 decky 49
	.long multiboot_header
50
	.long unmapped_ktext_start
680 decky 51
	.long 0
52
	.long 0
694 decky 53
	.long multiboot_image_start
188 palkovsky 54
 
680 decky 55
multiboot_image_start:
695 decky 56
	movl $START_STACK, %esp			# initialize stack pointer
57
	lgdt bootstrap_gdtr				# initialize Global Descriptor Table register
680 decky 58
 
685 decky 59
	movw $gdtselector(KDATA_DES), %cx
60
	movw %cx, %es
61
	movw %cx, %ds							# kernel data + stack
62
	movw %cx, %ss
807 palkovsky 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
65
	movw $gdtselector(UDATA_DES), %cx
66
	movw %cx, %fs
67
	movw %cx, %gs
685 decky 68
 
695 decky 69
	jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
685 decky 70
	multiboot_meeting_point:
71
 
695 decky 72
	movl %eax, grub_eax							# save parameters from GRUB
73
	movl %ebx, grub_ebx
74
 
1289 vana 75
#ifdef CONFIG_FB
76
	mov $vesa_init,%esi;
77
	mov $VESA_INIT_SEGMENT<<4,%edi;
78
	mov $e_vesa_init-vesa_init,%ecx;
79
	cld;
80
	rep movsb;
81
 
82
	mov $VESA_INIT_SEGMENT<<4,%edi;
83
	call *%edi;
84
	mov %esi,KA2PA(vesa_ph_addr);
85
	mov %di,KA2PA(vesa_height);
86
	shr $16,%edi;
87
	mov %di,KA2PA(vesa_width);
88
	mov %bx,KA2PA(vesa_scanline);
89
	shr $16,%ebx;
90
	mov %bx,KA2PA(vesa_bpp);
91
#endif	
92
 
694 decky 93
	# Protected 32-bit. We want to reuse the code-seg descriptor,
94
	# the Default operand size must not be 1 when entering long mode
275 palkovsky 95
 
696 decky 96
	movl $0x80000000, %eax  
97
 	cpuid
98
 	cmp $0x80000000, %eax						# any function > 80000000h?
99
	jbe long_mode_unsupported
100
	movl $(AMD_CPUID_EXTENDED), %eax			# Extended function code 80000001
101
	cpuid
102
	bt $29, %edx								# Test if long mode is supported.
103
 	jc long_mode_supported
104
 
105
	long_mode_unsupported:
106
		cli
107
		hlt
108
 
109
	long_mode_supported:
110
 
188 palkovsky 111
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
112
	# Paging is not enabled until after long mode is enabled
694 decky 113
 
188 palkovsky 114
	movl %cr4, %eax
115
	btsl $5, %eax
116
	movl %eax, %cr4
117
 
118
	# Set up paging tables
694 decky 119
 
188 palkovsky 120
	leal ptl_0, %eax
121
	movl %eax, %cr3
275 palkovsky 122
 
188 palkovsky 123
	# Enable long mode
178 palkovsky 124
 
694 decky 125
	movl $EFER_MSR_NUM, %ecx	# EFER MSR number
126
	rdmsr						# Read EFER
127
	btsl $AMD_LME_FLAG, %eax	# Set LME=1
128
	wrmsr						# Write EFER
129
 
188 palkovsky 130
	# Enable paging to activate long mode (set CR0.PG=1)
694 decky 131
 
188 palkovsky 132
	movl %cr0, %eax
133
	btsl $31, %eax
134
	movl %eax, %cr0
135
 
136
	# At this point we are in compatibility mode
694 decky 137
 
206 palkovsky 138
	jmpl $gdtselector(KTEXT_DES), $start64
164 palkovsky 139
 
188 palkovsky 140
.code64
141
start64:
275 palkovsky 142
	movq $(PA2KA(START_STACK)), %rsp
695 decky 143
	movl grub_eax, %eax
144
	movl grub_ebx, %ebx
145
 
146
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
147
	je valid_boot
148
 
149
		xorl %ecx, %ecx							# no memory size or map available
150
		movl %ecx, e801memorysize
151
		movl %ecx, e820counter
152
 
153
		jmp invalid_boot
154
 
155
	valid_boot:
156
 
157
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
158
 
159
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
160
		jc mem_valid
161
 
162
			xorl %ecx, %ecx
163
			jmp mem_invalid
164
 
165
		mem_valid:
166
		movl 4(%ebx), %ecx						# mbi->mem_lower
167
		addl 8(%ebx), %ecx						# mbi->mem_upper
168
 
169
		mem_invalid:
170
		movl %ecx, e801memorysize
171
 
1039 decky 172
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
695 decky 173
		jc mods_valid
174
 
1052 decky 175
			xorq %rcx, %rcx
176
			movq %rcx, init
1039 decky 177
			jmp mods_end
695 decky 178
 
179
		mods_valid:
1039 decky 180
 
181
		xorq %rcx, %rcx
695 decky 182
		movl 20(%ebx), %ecx						# mbi->mods_count
1052 decky 183
		movq %rcx, init
1039 decky 184
 
695 decky 185
		cmpl $0, %ecx
1039 decky 186
		je mods_end
695 decky 187
 
188
		movl 24(%ebx), %esi						# mbi->mods_addr
1039 decky 189
		movq $init, %rdi
695 decky 190
 
1039 decky 191
		mods_loop:
192
 
193
			xorq %rdx, %rdx
194
			movl 0(%esi), %edx					# mods->mod_start
1063 palkovsky 195
			movq $0xffff800000000000, %r10
196
			addq %r10, %rdx
1052 decky 197
			movq %rdx, 8(%rdi)
1039 decky 198
 
199
			xorq %rdx, %rdx
200
			movl 4(%esi), %edx
201
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
1052 decky 202
			movq %rdx, 16(%rdi)
1039 decky 203
 
204
			addl $16, %esi
205
			addq $16, %rdi
206
 
207
			loop mods_loop
208
 
209
		mods_end:
695 decky 210
 
211
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
212
		jc mmap_valid
213
 
214
			xorl %edx, %edx
215
			jmp mmap_invalid
216
 
217
		mmap_valid:
218
		movl 44(%ebx), %ecx						# mbi->mmap_length
219
		movl 48(%ebx), %esi						# mbi->mmap_addr
220
		movq $e820table, %rdi
221
		xorl %edx, %edx
222
 
223
		mmap_loop:
224
			cmpl $0, %ecx
225
			jle mmap_end
226
 
227
			movl 4(%esi), %eax					# mmap->base_addr_low
228
			movl %eax, (%rdi)
229
 
230
			movl 8(%esi), %eax					# mmap->base_addr_high
231
			movl %eax, 4(%rdi)
232
 
233
			movl 12(%esi), %eax					# mmap->length_low
234
			movl %eax, 8(%rdi)
235
 
236
			movl 16(%esi), %eax					# mmap->length_high
237
			movl %eax, 12(%rdi)
238
 
239
			movl 20(%esi), %eax					# mmap->type
240
			movl %eax, 16(%rdi)
241
 
242
			movl (%esi), %eax					# mmap->size
243
			addl $0x4, %eax
244
			addl %eax, %esi
245
			subl %eax, %ecx
246
			addq $MEMMAP_E820_RECORD_SIZE, %rdi
247
			incl %edx
248
			jmp mmap_loop
249
 
250
		mmap_end:
251
 
252
		mmap_invalid:
253
		movl %edx, e820counter
254
 
255
	invalid_boot:
256
 
696 decky 257
#ifdef CONFIG_SMP
258
 
259
	# copy AP bootstrap routines below 1 MB
260
 
261
	movq $BOOT_OFFSET, %rsi
262
	movq $AP_BOOT_OFFSET, %rdi
263
	movq $_hardcoded_unmapped_size, %rcx
264
	cld
265
	rep movsb
266
 
267
#endif
268
 
694 decky 269
	call main_bsp   # never returns
178 palkovsky 270
 
694 decky 271
	cli
272
	hlt
1289 vana 273
 
274
#ifdef CONFIG_FB
275
.code32
276
vesa_init:
277
	jmp $gdtselector(VESA_INIT_DES),$vesa_init_real-vesa_init;
278
.code16
279
vesa_init_real:	
280
 
281
	mov %cr0,%eax;
282
	and $~1,%eax;
283
	mov %eax,%cr0;
284
 
285
 
286
	jmp $VESA_INIT_SEGMENT,$vesa_init_real2-vesa_init;
287
 
288
vesa_init_real2:	
289
 
290
 
291
	mov %esp,%ebp;
292
	mov %ss,%cx;
293
	mov $VESA_INIT_SEGMENT,%bx;
294
	mov %bx,%ss;
295
	mov $0x0000fffc,%esp;
296
	push %ds;
297
	push %es;
298
	push %fs;
299
	push %gs;
300
	push %ebp;
301
	push %cx;
302
 
303
	mov %bx,%ds;
304
	mov %bx,%es;
305
	mov %bx,%fs;
306
	mov %bx,%gs;
307
 
308
 
309
	mov $vesa_idt-vesa_init,%ebx;
310
	lidtl (%ebx);
311
 
312
#define VESA_INFO_SIZE 1024
313
 
314
#define VESA_MODE_LIST_PTR_OFFSET 14 
315
#define VESA_MODE_WIDTH_OFFSET 18
316
#define VESA_MODE_HEIGHT_OFFSET 20
317
#define VESA_MODE_BPP_OFFSET 25
318
#define VESA_MODE_SCANLINE_OFFSET 16
319
#define VESA_MODE_PHADDR_OFFSET 40
320
 
321
#define VESA_END_OF_MODES 0xffff 
322
 
323
#define VESA_OK 0x4f 
324
 
325
#define VESA_GET_INFO 0x4f00 
326
#define VESA_GET_MODE_INFO 0x4f01
327
#define VESA_SET_MODE 0x4f02
328
 
329
#define CONFIG_VESA_BPP_a 255
330
 
331
#if CONFIG_VESA_BPP==24
332
#undef CONFIG_VESA_BPP_a
333
#define CONFIG_VESA_BPP_a 32
334
#endif
335
 
336
 
337
	mov $VESA_GET_INFO,%ax;
338
	mov $e_vesa_init-vesa_init,%di
339
	push %di;
340
	int $0x10;	
341
	pop %di;
342
	cmp $VESA_OK,%al;
343
	jnz 0f;
344
	mov 2+VESA_MODE_LIST_PTR_OFFSET(%di),%si;
345
	mov %si,%gs;
346
	mov VESA_MODE_LIST_PTR_OFFSET(%di),%si;
347
 
348
	add $VESA_INFO_SIZE,%di;
349
 
350
1:# Try next mode
351
	mov %gs:(%si),%cx;
352
	cmp $VESA_END_OF_MODES,%cx;
353
	jz 0f;
354
	inc %si;
355
	inc %si;
356
	push %cx;
357
	push %di;
358
	push %si;
359
	mov $VESA_GET_MODE_INFO,%ax;
360
	int $0x10;
361
	pop %si;
362
	pop %di;
363
	pop %cx;
364
	cmp $VESA_OK,%al;
365
	jnz 0f;
366
 
367
 
368
	mov $CONFIG_VESA_WIDTH,%ax;
369
	cmp VESA_MODE_WIDTH_OFFSET(%di),%ax;
370
	jnz 1b;
371
	mov $CONFIG_VESA_HEIGHT,%ax;
372
	cmp VESA_MODE_HEIGHT_OFFSET(%di),%ax;
373
	jnz 1b;
374
	mov $CONFIG_VESA_BPP,%al;
375
	cmp VESA_MODE_BPP_OFFSET(%di),%al;
376
	jz 2f;
377
	mov $CONFIG_VESA_BPP_a,%al;
378
	cmp VESA_MODE_BPP_OFFSET(%di),%al;
379
	jnz 1b;
380
 
381
2:
382
 
383
	mov %cx,%bx;
384
	or $0xC000,%bx;
385
	push %di;
386
	mov $VESA_SET_MODE,%ax;
387
	int $0x10;
388
	pop %di;
389
	cmp $VESA_OK,%al;
390
	jnz 0f;
391
 
392
	mov VESA_MODE_PHADDR_OFFSET(%di),%esi;
393
	mov VESA_MODE_WIDTH_OFFSET(%di),%ax;
394
	shl $16,%eax;
395
	mov VESA_MODE_HEIGHT_OFFSET(%di),%ax;
396
	mov VESA_MODE_BPP_OFFSET(%di),%bl;
397
	xor %bh,%bh;
398
	shl $16,%ebx;
399
	mov VESA_MODE_SCANLINE_OFFSET(%di),%bx;
400
	mov %eax,%edi; 
401
 
402
 
403
 
404
8:	
405
 
406
	mov %cr0,%eax;
407
	or $1,%eax;
408
	mov %eax,%cr0;
409
 
410
	jmp 9f;
411
9:
412
 
413
	pop %cx;
414
	pop %ebp;
415
	pop %gs;
416
	pop %fs;
417
	pop %es;
418
	pop %ds;
419
	mov %cx,%ss;
420
	mov %ebp,%esp;
421
 
422
	ljmpl $gdtselector(KTEXT32_DES),$(vesa_init_protect-vesa_init+VESA_INIT_SEGMENT<<4);
423
 
424
vesa_init_protect:	
425
.code32
426
	ret;
427
 
428
0:	#Error no Prefered mode found
429
 
430
	mov $0x111,%cx;
431
	push %di;
432
	push %cx;
433
	mov $VESA_GET_MODE_INFO,%ax;
434
	int $0x10;
435
	pop %cx;
436
	pop %di;
437
	cmp $VESA_OK,%al;
438
	jnz 1f;
439
	jmp 2b;
440
 
441
1:mov $0x0003,%ax;
442
	int $0x10;
443
	mov $0xffffffff,%edi; /* EGA text mode used, because of problems with VESA */
444
	jmp 8;
445
 
446
 
447
vesa_idt:
448
.word 0x03ff
449
.long 0
450
.align 4
451
e_vesa_init:
452
#endif	
453
 
454
 
206 palkovsky 455
 
406 jermar 456
.section K_DATA_START, "aw", @progbits
164 palkovsky 457
.align 4096
206 palkovsky 458
 
417 palkovsky 459
# Identical mapping of first 64MB and the same of -2GB -> 0	
188 palkovsky 460
.global ptl_2
461
ptl_2:	
462
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
463
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
464
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
465
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
466
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
467
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
468
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
469
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
417 palkovsky 470
	.quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
471
	.quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
472
	.quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
473
	.quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
474
	.quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
475
	.quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
476
	.quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
477
	.quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
478
	.quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
479
	.quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
480
	.quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
481
	.quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
482
	.quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
483
	.quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
484
	.quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
485
	.quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
486
	.quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
487
	.quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
488
	.quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
489
	.quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
490
	.quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
491
	.quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
492
	.quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
493
	.quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
188 palkovsky 494
 
495
.align 4096
496
.global ptl_1
497
ptl_1:
498
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
499
	.fill 509,8,0
500
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
293 palkovsky 501
	.fill 1,8,0
188 palkovsky 502
 
503
.align 4096
504
.global ptl_0
505
ptl_0:
506
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
1063 palkovsky 507
	.fill 255,8,0
188 palkovsky 508
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
1063 palkovsky 509
	.fill 254,8,0
510
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
178 palkovsky 511
 
695 decky 512
.global bootstrap_gdtr
513
bootstrap_gdtr:
685 decky 514
	.word gdtselector(GDT_ITEMS)
515
	.long KA2PA(gdt)
695 decky 516
 
517
grub_eax:
518
	.long 0
519
 
520
grub_ebx:
521
	.long 0