Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
1 jermar 1
#
2
# Copyright (C) 2001-2004 Jakub Jermar
693 decky 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
 
1287 vana 37
 
38
 
39
 
40
 
41
 
42
 
43
 
44
 
406 jermar 45
.section K_TEXT_START, "ax"
1 jermar 46
 
267 decky 47
KTEXT=8
48
KDATA=16
49
 
112 jermar 50
.code32
222 decky 51
.align 4
693 decky 52
.global multiboot_image_start
222 decky 53
multiboot_header:
54
	.long MULTIBOOT_HEADER_MAGIC
55
	.long MULTIBOOT_HEADER_FLAGS
56
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
693 decky 57
	.long multiboot_header
58
	.long unmapped_ktext_start
222 decky 59
	.long 0
60
	.long 0
693 decky 61
	.long multiboot_image_start
222 decky 62
 
63
multiboot_image_start:
693 decky 64
	movl $START_STACK, %esp			# initialize stack pointer
65
	lgdt KA2PA(bootstrap_gdtr)		# initialize Global Descriptor Table register
112 jermar 66
 
280 decky 67
	movw $KDATA, %cx
68
	movw %cx, %es
69
	movw %cx, %gs
70
	movw %cx, %fs
693 decky 71
	movw %cx, %ds					# kernel data + stack
280 decky 72
	movw %cx, %ss
235 decky 73
 
693 decky 74
	jmpl $KTEXT, $multiboot_meeting_point
271 decky 75
	multiboot_meeting_point:
76
 
323 jermar 77
	pushl %ebx							# save parameters from GRUB
280 decky 78
	pushl %eax
1287 vana 79
 
80
#ifdef CONFIG_FB
81
	mov $vesa_init,%esi;
82
	mov $VESA_INIT_SEGMENT<<4,%edi;
83
	mov $e_vesa_init-vesa_init,%ecx;
84
	cld;
85
	rep movsb;
86
 
87
	mov $VESA_INIT_SEGMENT<<4,%edi;
88
	call *%edi;
89
	mov %esi,KA2PA(vesa_ph_addr);
90
	mov %di,KA2PA(vesa_height);
91
	shr $16,%edi;
92
	mov %di,KA2PA(vesa_width);
93
	mov %bx,KA2PA(vesa_scanline);
94
	shr $16,%ebx;
95
	mov %bx,KA2PA(vesa_bpp);
96
#endif	
280 decky 97
 
323 jermar 98
	call map_kernel							# map kernel and turn paging on
235 decky 99
 
280 decky 100
	popl %eax
101
	popl %ebx
323 jermar 102
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
280 decky 103
	je valid_boot
104
 
285 decky 105
		xorl %ecx, %ecx							# no memory size or map available
106
		movl %ecx, e801memorysize
107
		movl %ecx, e820counter
108
 
280 decky 109
		jmp invalid_boot
285 decky 110
 
280 decky 111
	valid_boot:
112
 
285 decky 113
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
280 decky 114
 
285 decky 115
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
116
		jc mem_valid
117
 
118
			xorl %ecx, %ecx
119
			jmp mem_invalid
120
 
121
		mem_valid:
122
		movl 4(%ebx), %ecx						# mbi->mem_lower
123
		addl 8(%ebx), %ecx						# mbi->mem_upper
280 decky 124
 
285 decky 125
		mem_invalid:
280 decky 126
		movl %ecx, e801memorysize
127
 
1037 decky 128
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
506 decky 129
		jc mods_valid
130
 
131
			xorl %ecx, %ecx
1037 decky 132
			movl %ecx, init
133
			jmp mods_end
506 decky 134
 
135
		mods_valid:
1037 decky 136
 
628 decky 137
		movl 20(%ebx), %ecx						# mbi->mods_count
1037 decky 138
		movl %ecx, init
139
 
628 decky 140
		cmpl $0, %ecx
1037 decky 141
		je mods_end
506 decky 142
 
628 decky 143
		movl 24(%ebx), %esi						# mbi->mods_addr
1037 decky 144
		movl $init, %edi
628 decky 145
 
1037 decky 146
		mods_loop:
506 decky 147
 
1037 decky 148
			movl 0(%esi), %edx					# mods->mod_start
149
			addl $0x80000000, %edx
150
			movl %edx, 4(%edi)
151
 
152
			movl 4(%esi), %edx
153
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
154
			movl %edx, 8(%edi)
155
 
156
			addl $16, %esi
157
			addl $8	, %edi
158
 
159
			loop mods_loop
160
 
161
		mods_end:
162
 
285 decky 163
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
164
		jc mmap_valid
165
 
166
			xorl %edx, %edx
167
			jmp mmap_invalid
168
 
169
		mmap_valid:
170
		movl 44(%ebx), %ecx						# mbi->mmap_length
171
		movl 48(%ebx), %esi						# mbi->mmap_addr
172
		movl $e820table, %edi
173
		xorl %edx, %edx
280 decky 174
 
285 decky 175
		mmap_loop:
176
			cmpl $0, %ecx
177
			jle mmap_end
178
 
179
			movl 4(%esi), %eax					# mmap->base_addr_low
180
			movl %eax, (%edi)
181
 
182
			movl 8(%esi), %eax					# mmap->base_addr_high
183
			movl %eax, 4(%edi)
184
 
185
			movl 12(%esi), %eax					# mmap->length_low
186
			movl %eax, 8(%edi)
187
 
188
			movl 16(%esi), %eax					# mmap->length_high
189
			movl %eax, 12(%edi)
190
 
191
			movl 20(%esi), %eax					# mmap->type
192
			movl %eax, 16(%edi)
193
 
194
			movl (%esi), %eax					# mmap->size
195
			addl $0x4, %eax
196
			addl %eax, %esi
197
			subl %eax, %ecx
198
			addl $MEMMAP_E820_RECORD_SIZE, %edi
199
			incl %edx
200
			jmp mmap_loop
201
 
202
		mmap_end:
203
 
204
		mmap_invalid:
205
		movl %edx, e820counter
206
 
280 decky 207
	invalid_boot:
208
 
693 decky 209
#ifdef CONFIG_SMP
210
 
211
	# copy AP bootstrap routines below 1 MB
212
 
213
	movl $BOOT_OFFSET, %esi
214
	movl $AP_BOOT_OFFSET, %edi
215
	movl $_hardcoded_unmapped_size, %ecx
216
	cld
217
	rep movsb
218
 
219
#endif
220
 
221
	call main_bsp								# never returns
110 jermar 222
 
223
	cli
224
	hlt
225
 
226
.global map_kernel
227
map_kernel:
105 jermar 228
	#
229
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
371 jermar 230
	# For simplicity, we map the entire 4G space.
105 jermar 231
	#
232
	movl %cr4, %ecx
233
	orl $(1<<4), %ecx
323 jermar 234
	movl %ecx, %cr4							# turn PSE on
1 jermar 235
 
371 jermar 236
	movl $(page_directory+0), %esi
237
	movl $(page_directory+2048), %edi
238
	xorl %ecx, %ecx
239
	xorl %ebx, %ebx
240
0:
105 jermar 241
	movl $((1<<7)|(1<<0)), %eax
371 jermar 242
	orl %ebx, %eax
243
	movl %eax, (%esi,%ecx,4)					# mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
244
	movl %eax, (%edi,%ecx,4)					# mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
245
	addl $(4*1024*1024), %ebx
1 jermar 246
 
371 jermar 247
	incl %ecx
248
	cmpl $512, %ecx
249
	jl 0b
105 jermar 250
 
371 jermar 251
	movl %esi, %cr3
105 jermar 252
 
177 jermar 253
	# turn paging on
105 jermar 254
	movl %cr0, %ebx
255
	orl $(1<<31), %ebx
256
	movl %ebx, %cr0
110 jermar 257
	ret
105 jermar 258
 
1287 vana 259
#ifdef CONFIG_FB
260
vesa_init:
261
	jmp $selector(VESA_INIT_DES),$vesa_init_real-vesa_init;
262
.code16
263
vesa_init_real:	
105 jermar 264
 
1287 vana 265
	mov %cr0,%eax;
266
	and $~1,%eax;
267
	mov %eax,%cr0;
268
 
269
 
270
	jmp $VESA_INIT_SEGMENT,$vesa_init_real2-vesa_init;
271
 
272
vesa_init_real2:	
273
 
274
 
275
	mov %esp,%ebp;
276
	mov %ss,%cx;
277
	mov $VESA_INIT_SEGMENT,%bx;
278
	mov %bx,%ss;
279
	mov $0x0000fffc,%esp;
280
	push %ds;
281
	push %es;
282
	push %fs;
283
	push %gs;
284
	push %ebp;
285
	push %cx;
286
 
287
	mov %bx,%ds;
288
	mov %bx,%es;
289
	mov %bx,%fs;
290
	mov %bx,%gs;
291
 
292
 
293
	mov $vesa_idt-vesa_init,%ebx;
294
	lidtl (%ebx);
295
 
296
#define VESA_INFO_SIZE 1024
297
 
298
#define VESA_MODE_LIST_PTR_OFFSET 14 
299
#define VESA_MODE_WIDTH_OFFSET 18
300
#define VESA_MODE_HEIGHT_OFFSET 20
301
#define VESA_MODE_BPP_OFFSET 25
302
#define VESA_MODE_SCANLINE_OFFSET 16
303
#define VESA_MODE_PHADDR_OFFSET 40
304
 
305
#define VESA_END_OF_MODES 0xffff 
306
 
307
#define VESA_OK 0x4f 
308
 
309
#define VESA_GET_INFO 0x4f00 
310
#define VESA_GET_MODE_INFO 0x4f01
311
#define VESA_SET_MODE 0x4f02
312
 
313
#define CONFIG_VESA_BPP_a 255
314
 
315
#if CONFIG_VESA_BPP==24
316
#undef CONFIG_VESA_BPP_a
317
#define CONFIG_VESA_BPP_a 32
318
#endif
319
 
320
 
321
	mov $VESA_GET_INFO,%ax;
322
	mov $e_vesa_init-vesa_init,%di
323
	push %di;
324
	int $0x10;	
325
	pop %di;
326
	cmp $VESA_OK,%al;
327
	jnz 0f;
328
	mov 2+VESA_MODE_LIST_PTR_OFFSET(%di),%si;
329
	mov %si,%gs;
330
	mov VESA_MODE_LIST_PTR_OFFSET(%di),%si;
331
 
332
	add $VESA_INFO_SIZE,%di;
333
 
334
1:# Try next mode
335
	mov %gs:(%si),%cx;
336
	cmp $VESA_END_OF_MODES,%cx;
337
	jz 0f;
338
	inc %si;
339
	inc %si;
340
	push %cx;
341
	push %di;
342
	push %si;
343
	mov $VESA_GET_MODE_INFO,%ax;
344
	int $0x10;
345
	pop %si;
346
	pop %di;
347
	pop %cx;
348
	cmp $VESA_OK,%al;
349
	jnz 0f;
350
 
351
 
352
	mov $CONFIG_VESA_WIDTH,%ax;
353
	cmp VESA_MODE_WIDTH_OFFSET(%di),%ax;
354
	jnz 1b;
355
	mov $CONFIG_VESA_HEIGHT,%ax;
356
	cmp VESA_MODE_HEIGHT_OFFSET(%di),%ax;
357
	jnz 1b;
358
	mov $CONFIG_VESA_BPP,%al;
359
	cmp VESA_MODE_BPP_OFFSET(%di),%al;
360
	jz 2f;
361
	mov $CONFIG_VESA_BPP_a,%al;
362
	cmp VESA_MODE_BPP_OFFSET(%di),%al;
363
	jnz 1b;
364
 
365
2:
366
 
367
	mov %cx,%bx;
368
	or $0xC000,%bx;
369
	push %di;
370
	mov $VESA_SET_MODE,%ax;
371
	int $0x10;
372
	pop %di;
373
	cmp $VESA_OK,%al;
374
	jnz 0f;
375
 
376
	mov VESA_MODE_PHADDR_OFFSET(%di),%esi;
377
	mov VESA_MODE_WIDTH_OFFSET(%di),%ax;
378
	shl $16,%eax;
379
	mov VESA_MODE_HEIGHT_OFFSET(%di),%ax;
380
	mov VESA_MODE_BPP_OFFSET(%di),%bl;
381
	xor %bh,%bh;
382
	shl $16,%ebx;
383
	mov VESA_MODE_SCANLINE_OFFSET(%di),%bx;
384
	mov %eax,%edi; 
385
 
386
 
387
 
388
8:	
389
 
390
	mov %cr0,%eax;
391
	or $1,%eax;
392
	mov %eax,%cr0;
393
 
394
	jmp 9f;
395
9:
396
 
397
	pop %cx;
398
	pop %ebp;
399
	pop %gs;
400
	pop %fs;
401
	pop %es;
402
	pop %ds;
403
	mov %cx,%ss;
404
	mov %ebp,%esp;
405
 
406
	ljmpl $KTEXT,$(vesa_init_protect-vesa_init+VESA_INIT_SEGMENT<<4);
407
 
408
vesa_init_protect:	
409
.code32
410
	ret;
411
 
412
0:	#Error no Prefered mode found
413
 
414
	mov $0x111,%cx;
415
	push %di;
416
	push %cx;
417
	mov $VESA_GET_MODE_INFO,%ax;
418
	int $0x10;
419
	pop %cx;
420
	pop %di;
421
	cmp $VESA_OK,%al;
422
	jnz 1f;
423
	jmp 2b;
424
 
425
1:mov $0x0003,%ax;
426
	int $0x10;
427
	mov $0xffffffff,%edi; /* EGA text mode used, because of problems with VESA */
428
	jmp 8;
429
 
430
 
431
vesa_idt:
432
.word 0x03ff
433
.long 0
434
.align 4
435
e_vesa_init:
436
#endif	
437
 
438
 
439
 
406 jermar 440
.section K_DATA_START, "aw", @progbits
105 jermar 441
 
1287 vana 442
 
443
 
105 jermar 444
.align 4096
445
page_directory:
446
	.space 4096, 0