Subversion Repositories HelenOS-historic

Rev

Rev 680 | Rev 1037 | 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
 
406 jermar 37
.section K_TEXT_START, "ax"
1 jermar 38
 
267 decky 39
KTEXT=8
40
KDATA=16
41
 
112 jermar 42
.code32
222 decky 43
.align 4
693 decky 44
.global multiboot_image_start
222 decky 45
multiboot_header:
46
	.long MULTIBOOT_HEADER_MAGIC
47
	.long MULTIBOOT_HEADER_FLAGS
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
693 decky 49
	.long multiboot_header
50
	.long unmapped_ktext_start
222 decky 51
	.long 0
52
	.long 0
693 decky 53
	.long multiboot_image_start
222 decky 54
 
55
multiboot_image_start:
693 decky 56
	movl $START_STACK, %esp			# initialize stack pointer
57
	lgdt KA2PA(bootstrap_gdtr)		# initialize Global Descriptor Table register
112 jermar 58
 
280 decky 59
	movw $KDATA, %cx
60
	movw %cx, %es
61
	movw %cx, %gs
62
	movw %cx, %fs
693 decky 63
	movw %cx, %ds					# kernel data + stack
280 decky 64
	movw %cx, %ss
235 decky 65
 
693 decky 66
	jmpl $KTEXT, $multiboot_meeting_point
271 decky 67
	multiboot_meeting_point:
68
 
323 jermar 69
	pushl %ebx							# save parameters from GRUB
280 decky 70
	pushl %eax
71
 
323 jermar 72
	call map_kernel							# map kernel and turn paging on
235 decky 73
 
280 decky 74
	popl %eax
75
	popl %ebx
323 jermar 76
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
280 decky 77
	je valid_boot
78
 
285 decky 79
		xorl %ecx, %ecx							# no memory size or map available
80
		movl %ecx, e801memorysize
81
		movl %ecx, e820counter
82
 
280 decky 83
		jmp invalid_boot
285 decky 84
 
280 decky 85
	valid_boot:
86
 
285 decky 87
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
280 decky 88
 
285 decky 89
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
90
		jc mem_valid
91
 
92
			xorl %ecx, %ecx
93
			jmp mem_invalid
94
 
95
		mem_valid:
96
		movl 4(%ebx), %ecx						# mbi->mem_lower
97
		addl 8(%ebx), %ecx						# mbi->mem_upper
280 decky 98
 
285 decky 99
		mem_invalid:
280 decky 100
		movl %ecx, e801memorysize
101
 
506 decky 102
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)	
103
		jc mods_valid
104
 
105
			xorl %ecx, %ecx
106
			xorl %edx, %edx
107
			jmp mods_invalid
108
 
109
		mods_valid:
628 decky 110
		movl 20(%ebx), %ecx						# mbi->mods_count
111
		cmpl $0, %ecx
112
		je mods_invalid
506 decky 113
 
628 decky 114
		movl 24(%ebx), %esi						# mbi->mods_addr
115
		movl 0(%esi), %edx						# mods->mod_start
116
		movl 4(%esi), %ecx						# mods->mod_end
117
		subl %edx, %ecx
629 decky 118
		addl $0x80000000, %edx
628 decky 119
 
506 decky 120
		mods_invalid:
628 decky 121
		movl %ecx, init_size
122
		movl %edx, init_addr
506 decky 123
 
285 decky 124
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
125
		jc mmap_valid
126
 
127
			xorl %edx, %edx
128
			jmp mmap_invalid
129
 
130
		mmap_valid:
131
		movl 44(%ebx), %ecx						# mbi->mmap_length
132
		movl 48(%ebx), %esi						# mbi->mmap_addr
133
		movl $e820table, %edi
134
		xorl %edx, %edx
280 decky 135
 
285 decky 136
		mmap_loop:
137
			cmpl $0, %ecx
138
			jle mmap_end
139
 
140
			movl 4(%esi), %eax					# mmap->base_addr_low
141
			movl %eax, (%edi)
142
 
143
			movl 8(%esi), %eax					# mmap->base_addr_high
144
			movl %eax, 4(%edi)
145
 
146
			movl 12(%esi), %eax					# mmap->length_low
147
			movl %eax, 8(%edi)
148
 
149
			movl 16(%esi), %eax					# mmap->length_high
150
			movl %eax, 12(%edi)
151
 
152
			movl 20(%esi), %eax					# mmap->type
153
			movl %eax, 16(%edi)
154
 
155
			movl (%esi), %eax					# mmap->size
156
			addl $0x4, %eax
157
			addl %eax, %esi
158
			subl %eax, %ecx
159
			addl $MEMMAP_E820_RECORD_SIZE, %edi
160
			incl %edx
161
			jmp mmap_loop
162
 
163
		mmap_end:
164
 
165
		mmap_invalid:
166
		movl %edx, e820counter
167
 
280 decky 168
	invalid_boot:
169
 
693 decky 170
#ifdef CONFIG_SMP
171
 
172
	# copy AP bootstrap routines below 1 MB
173
 
174
	movl $BOOT_OFFSET, %esi
175
	movl $AP_BOOT_OFFSET, %edi
176
	movl $_hardcoded_unmapped_size, %ecx
177
	cld
178
	rep movsb
179
 
180
#endif
181
 
182
	call main_bsp								# never returns
110 jermar 183
 
184
	cli
185
	hlt
186
 
187
.global map_kernel
188
map_kernel:
105 jermar 189
	#
190
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
371 jermar 191
	# For simplicity, we map the entire 4G space.
105 jermar 192
	#
193
	movl %cr4, %ecx
194
	orl $(1<<4), %ecx
323 jermar 195
	movl %ecx, %cr4							# turn PSE on
1 jermar 196
 
371 jermar 197
	movl $(page_directory+0), %esi
198
	movl $(page_directory+2048), %edi
199
	xorl %ecx, %ecx
200
	xorl %ebx, %ebx
201
0:
105 jermar 202
	movl $((1<<7)|(1<<0)), %eax
371 jermar 203
	orl %ebx, %eax
204
	movl %eax, (%esi,%ecx,4)					# mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
205
	movl %eax, (%edi,%ecx,4)					# mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
206
	addl $(4*1024*1024), %ebx
1 jermar 207
 
371 jermar 208
	incl %ecx
209
	cmpl $512, %ecx
210
	jl 0b
105 jermar 211
 
371 jermar 212
	movl %esi, %cr3
105 jermar 213
 
177 jermar 214
	# turn paging on
105 jermar 215
	movl %cr0, %ebx
216
	orl $(1<<31), %ebx
217
	movl %ebx, %cr0
110 jermar 218
	ret
105 jermar 219
 
220
 
406 jermar 221
.section K_DATA_START, "aw", @progbits
105 jermar 222
 
223
.align 4096
224
page_directory:
225
	.space 4096, 0