Subversion Repositories HelenOS-historic

Rev

Rev 693 | 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
 
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
 
1037 decky 102
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
506 decky 103
		jc mods_valid
104
 
105
			xorl %ecx, %ecx
1037 decky 106
			movl %ecx, init
107
			jmp mods_end
506 decky 108
 
109
		mods_valid:
1037 decky 110
 
628 decky 111
		movl 20(%ebx), %ecx						# mbi->mods_count
1037 decky 112
		movl %ecx, init
113
 
628 decky 114
		cmpl $0, %ecx
1037 decky 115
		je mods_end
506 decky 116
 
628 decky 117
		movl 24(%ebx), %esi						# mbi->mods_addr
1037 decky 118
		movl $init, %edi
628 decky 119
 
1037 decky 120
		mods_loop:
506 decky 121
 
1037 decky 122
			movl 0(%esi), %edx					# mods->mod_start
123
			addl $0x80000000, %edx
124
			movl %edx, 4(%edi)
125
 
126
			movl 4(%esi), %edx
127
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
128
			movl %edx, 8(%edi)
129
 
130
			addl $16, %esi
131
			addl $8	, %edi
132
 
133
			loop mods_loop
134
 
135
		mods_end:
136
 
285 decky 137
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
138
		jc mmap_valid
139
 
140
			xorl %edx, %edx
141
			jmp mmap_invalid
142
 
143
		mmap_valid:
144
		movl 44(%ebx), %ecx						# mbi->mmap_length
145
		movl 48(%ebx), %esi						# mbi->mmap_addr
146
		movl $e820table, %edi
147
		xorl %edx, %edx
280 decky 148
 
285 decky 149
		mmap_loop:
150
			cmpl $0, %ecx
151
			jle mmap_end
152
 
153
			movl 4(%esi), %eax					# mmap->base_addr_low
154
			movl %eax, (%edi)
155
 
156
			movl 8(%esi), %eax					# mmap->base_addr_high
157
			movl %eax, 4(%edi)
158
 
159
			movl 12(%esi), %eax					# mmap->length_low
160
			movl %eax, 8(%edi)
161
 
162
			movl 16(%esi), %eax					# mmap->length_high
163
			movl %eax, 12(%edi)
164
 
165
			movl 20(%esi), %eax					# mmap->type
166
			movl %eax, 16(%edi)
167
 
168
			movl (%esi), %eax					# mmap->size
169
			addl $0x4, %eax
170
			addl %eax, %esi
171
			subl %eax, %ecx
172
			addl $MEMMAP_E820_RECORD_SIZE, %edi
173
			incl %edx
174
			jmp mmap_loop
175
 
176
		mmap_end:
177
 
178
		mmap_invalid:
179
		movl %edx, e820counter
180
 
280 decky 181
	invalid_boot:
182
 
693 decky 183
#ifdef CONFIG_SMP
184
 
185
	# copy AP bootstrap routines below 1 MB
186
 
187
	movl $BOOT_OFFSET, %esi
188
	movl $AP_BOOT_OFFSET, %edi
189
	movl $_hardcoded_unmapped_size, %ecx
190
	cld
191
	rep movsb
192
 
193
#endif
194
 
195
	call main_bsp								# never returns
110 jermar 196
 
197
	cli
198
	hlt
199
 
200
.global map_kernel
201
map_kernel:
105 jermar 202
	#
203
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
371 jermar 204
	# For simplicity, we map the entire 4G space.
105 jermar 205
	#
206
	movl %cr4, %ecx
207
	orl $(1<<4), %ecx
323 jermar 208
	movl %ecx, %cr4							# turn PSE on
1 jermar 209
 
371 jermar 210
	movl $(page_directory+0), %esi
211
	movl $(page_directory+2048), %edi
212
	xorl %ecx, %ecx
213
	xorl %ebx, %ebx
214
0:
105 jermar 215
	movl $((1<<7)|(1<<0)), %eax
371 jermar 216
	orl %ebx, %eax
217
	movl %eax, (%esi,%ecx,4)					# mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
218
	movl %eax, (%edi,%ecx,4)					# mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
219
	addl $(4*1024*1024), %ebx
1 jermar 220
 
371 jermar 221
	incl %ecx
222
	cmpl $512, %ecx
223
	jl 0b
105 jermar 224
 
371 jermar 225
	movl %esi, %cr3
105 jermar 226
 
177 jermar 227
	# turn paging on
105 jermar 228
	movl %cr0, %ebx
229
	orl $(1<<31), %ebx
230
	movl %ebx, %cr0
110 jermar 231
	ret
105 jermar 232
 
233
 
406 jermar 234
.section K_DATA_START, "aw", @progbits
105 jermar 235
 
236
.align 4096
237
page_directory:
238
	.space 4096, 0