Subversion Repositories HelenOS-historic

Rev

Rev 693 | Rev 1287 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 693 Rev 1037
1
#
1
#
2
# Copyright (C) 2001-2004 Jakub Jermar
2
# Copyright (C) 2001-2004 Jakub Jermar
3
# Copyright (C) 2005-2006 Martin Decky
3
# Copyright (C) 2005-2006 Martin Decky
4
# All rights reserved.
4
# All rights reserved.
5
#
5
#
6
# Redistribution and use in source and binary forms, with or without
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
7
# modification, are permitted provided that the following conditions
8
# are met:
8
# are met:
9
#
9
#
10
# - Redistributions of source code must retain the above copyright
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
13
#   notice, this list of conditions and the following disclaimer in the
14
#   documentation and/or other materials provided with the distribution.
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
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.
16
#   derived from this software without specific prior written permission.
17
#
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
28
#
29
 
29
 
30
#include <arch/boot/boot.h>
30
#include <arch/boot/boot.h>
31
#include <arch/boot/memmap.h>
31
#include <arch/boot/memmap.h>
32
#include <arch/mm/page.h>
32
#include <arch/mm/page.h>
33
#include <arch/pm.h>
33
#include <arch/pm.h>
34
 
34
 
35
#define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
35
#define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
36
 
36
 
37
.section K_TEXT_START, "ax"
37
.section K_TEXT_START, "ax"
38
 
38
 
39
KTEXT=8
39
KTEXT=8
40
KDATA=16
40
KDATA=16
41
 
41
 
42
.code32
42
.code32
43
.align 4
43
.align 4
44
.global multiboot_image_start
44
.global multiboot_image_start
45
multiboot_header:
45
multiboot_header:
46
	.long MULTIBOOT_HEADER_MAGIC
46
	.long MULTIBOOT_HEADER_MAGIC
47
	.long MULTIBOOT_HEADER_FLAGS
47
	.long MULTIBOOT_HEADER_FLAGS
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
49
	.long multiboot_header
49
	.long multiboot_header
50
	.long unmapped_ktext_start
50
	.long unmapped_ktext_start
51
	.long 0
51
	.long 0
52
	.long 0
52
	.long 0
53
	.long multiboot_image_start
53
	.long multiboot_image_start
54
	
54
	
55
multiboot_image_start:
55
multiboot_image_start:
56
	movl $START_STACK, %esp			# initialize stack pointer
56
	movl $START_STACK, %esp			# initialize stack pointer
57
	lgdt KA2PA(bootstrap_gdtr)		# initialize Global Descriptor Table register
57
	lgdt KA2PA(bootstrap_gdtr)		# initialize Global Descriptor Table register
58
 
58
 
59
	movw $KDATA, %cx
59
	movw $KDATA, %cx
60
	movw %cx, %es
60
	movw %cx, %es
61
	movw %cx, %gs
61
	movw %cx, %gs
62
	movw %cx, %fs
62
	movw %cx, %fs
63
	movw %cx, %ds					# kernel data + stack
63
	movw %cx, %ds					# kernel data + stack
64
	movw %cx, %ss
64
	movw %cx, %ss
65
	
65
	
66
	jmpl $KTEXT, $multiboot_meeting_point
66
	jmpl $KTEXT, $multiboot_meeting_point
67
	multiboot_meeting_point:
67
	multiboot_meeting_point:
68
	
68
	
69
	pushl %ebx							# save parameters from GRUB
69
	pushl %ebx							# save parameters from GRUB
70
	pushl %eax
70
	pushl %eax
71
	
71
	
72
	call map_kernel							# map kernel and turn paging on
72
	call map_kernel							# map kernel and turn paging on
73
	
73
	
74
	popl %eax
74
	popl %eax
75
	popl %ebx
75
	popl %ebx
76
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
76
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
77
	je valid_boot
77
	je valid_boot
78
		
78
		
79
		xorl %ecx, %ecx							# no memory size or map available
79
		xorl %ecx, %ecx							# no memory size or map available
80
		movl %ecx, e801memorysize
80
		movl %ecx, e801memorysize
81
		movl %ecx, e820counter
81
		movl %ecx, e820counter
82
		
82
		
83
		jmp invalid_boot
83
		jmp invalid_boot
84
		
84
		
85
	valid_boot:
85
	valid_boot:
86
		
86
		
87
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
87
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
88
		
88
		
89
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
89
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
90
		jc mem_valid
90
		jc mem_valid
91
			
91
			
92
			xorl %ecx, %ecx
92
			xorl %ecx, %ecx
93
			jmp mem_invalid
93
			jmp mem_invalid
94
			
94
			
95
		mem_valid:
95
		mem_valid:
96
		movl 4(%ebx), %ecx						# mbi->mem_lower
96
		movl 4(%ebx), %ecx						# mbi->mem_lower
97
		addl 8(%ebx), %ecx						# mbi->mem_upper
97
		addl 8(%ebx), %ecx						# mbi->mem_upper
98
		
98
		
99
		mem_invalid:
99
		mem_invalid:
100
		movl %ecx, e801memorysize
100
		movl %ecx, e801memorysize
101
		
101
		
102
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)	
102
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)
103
		jc mods_valid
103
		jc mods_valid
104
			
104
			
105
			xorl %ecx, %ecx
105
			xorl %ecx, %ecx
106
			xorl %edx, %edx
106
			movl %ecx, init
107
			jmp mods_invalid
107
			jmp mods_end
108
		
108
		
109
		mods_valid:
109
		mods_valid:
-
 
110
		
110
		movl 20(%ebx), %ecx						# mbi->mods_count
111
		movl 20(%ebx), %ecx						# mbi->mods_count
-
 
112
		movl %ecx, init
-
 
113
		
111
		cmpl $0, %ecx
114
		cmpl $0, %ecx
112
		je mods_invalid
115
		je mods_end
113
		
116
		
114
		movl 24(%ebx), %esi						# mbi->mods_addr
117
		movl 24(%ebx), %esi						# mbi->mods_addr
-
 
118
		movl $init, %edi
-
 
119
		
-
 
120
		mods_loop:
-
 
121
		
115
		movl 0(%esi), %edx						# mods->mod_start
122
			movl 0(%esi), %edx					# mods->mod_start
-
 
123
			addl $0x80000000, %edx
-
 
124
			movl %edx, 4(%edi)
-
 
125
			
-
 
126
			movl 4(%esi), %edx
116
		movl 4(%esi), %ecx						# mods->mod_end
127
			subl 0(%esi), %edx					# mods->mod_end - mods->mod_start
117
		subl %edx, %ecx
128
			movl %edx, 8(%edi)
-
 
129
			
-
 
130
			addl $16, %esi
118
		addl $0x80000000, %edx
131
			addl $8	, %edi
119
		
132
			
120
		mods_invalid:
133
			loop mods_loop
121
		movl %ecx, init_size
134
			
122
		movl %edx, init_addr
135
		mods_end:
123
		
136
		
124
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
137
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
125
		jc mmap_valid
138
		jc mmap_valid
126
			
139
			
127
			xorl %edx, %edx
140
			xorl %edx, %edx
128
			jmp mmap_invalid
141
			jmp mmap_invalid
129
			
142
			
130
		mmap_valid:
143
		mmap_valid:
131
		movl 44(%ebx), %ecx						# mbi->mmap_length
144
		movl 44(%ebx), %ecx						# mbi->mmap_length
132
		movl 48(%ebx), %esi						# mbi->mmap_addr
145
		movl 48(%ebx), %esi						# mbi->mmap_addr
133
		movl $e820table, %edi
146
		movl $e820table, %edi
134
		xorl %edx, %edx
147
		xorl %edx, %edx
135
		
148
		
136
		mmap_loop:
149
		mmap_loop:
137
			cmpl $0, %ecx
150
			cmpl $0, %ecx
138
			jle mmap_end
151
			jle mmap_end
139
			
152
			
140
			movl 4(%esi), %eax					# mmap->base_addr_low
153
			movl 4(%esi), %eax					# mmap->base_addr_low
141
			movl %eax, (%edi)
154
			movl %eax, (%edi)
142
			
155
			
143
			movl 8(%esi), %eax					# mmap->base_addr_high
156
			movl 8(%esi), %eax					# mmap->base_addr_high
144
			movl %eax, 4(%edi)
157
			movl %eax, 4(%edi)
145
			
158
			
146
			movl 12(%esi), %eax					# mmap->length_low
159
			movl 12(%esi), %eax					# mmap->length_low
147
			movl %eax, 8(%edi)
160
			movl %eax, 8(%edi)
148
			
161
			
149
			movl 16(%esi), %eax					# mmap->length_high
162
			movl 16(%esi), %eax					# mmap->length_high
150
			movl %eax, 12(%edi)
163
			movl %eax, 12(%edi)
151
			
164
			
152
			movl 20(%esi), %eax					# mmap->type
165
			movl 20(%esi), %eax					# mmap->type
153
			movl %eax, 16(%edi)
166
			movl %eax, 16(%edi)
154
			
167
			
155
			movl (%esi), %eax					# mmap->size
168
			movl (%esi), %eax					# mmap->size
156
			addl $0x4, %eax
169
			addl $0x4, %eax
157
			addl %eax, %esi
170
			addl %eax, %esi
158
			subl %eax, %ecx
171
			subl %eax, %ecx
159
			addl $MEMMAP_E820_RECORD_SIZE, %edi
172
			addl $MEMMAP_E820_RECORD_SIZE, %edi
160
			incl %edx
173
			incl %edx
161
			jmp mmap_loop
174
			jmp mmap_loop
162
		
175
		
163
		mmap_end:
176
		mmap_end:
164
		
177
		
165
		mmap_invalid:
178
		mmap_invalid:
166
		movl %edx, e820counter
179
		movl %edx, e820counter
167
		
180
		
168
	invalid_boot:
181
	invalid_boot:
169
	
182
	
170
#ifdef CONFIG_SMP
183
#ifdef CONFIG_SMP
171
	
184
	
172
	# copy AP bootstrap routines below 1 MB
185
	# copy AP bootstrap routines below 1 MB
173
	
186
	
174
	movl $BOOT_OFFSET, %esi
187
	movl $BOOT_OFFSET, %esi
175
	movl $AP_BOOT_OFFSET, %edi
188
	movl $AP_BOOT_OFFSET, %edi
176
	movl $_hardcoded_unmapped_size, %ecx
189
	movl $_hardcoded_unmapped_size, %ecx
177
	cld
190
	cld
178
	rep movsb
191
	rep movsb
179
	
192
	
180
#endif
193
#endif
181
	
194
	
182
	call main_bsp								# never returns
195
	call main_bsp								# never returns
183
 
196
 
184
	cli
197
	cli
185
	hlt
198
	hlt
186
 
199
 
187
.global map_kernel
200
.global map_kernel
188
map_kernel:
201
map_kernel:
189
	#
202
	#
190
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
203
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
191
	# For simplicity, we map the entire 4G space.
204
	# For simplicity, we map the entire 4G space.
192
	#
205
	#
193
	movl %cr4, %ecx
206
	movl %cr4, %ecx
194
	orl $(1<<4), %ecx
207
	orl $(1<<4), %ecx
195
	movl %ecx, %cr4							# turn PSE on
208
	movl %ecx, %cr4							# turn PSE on
196
	
209
	
197
	movl $(page_directory+0), %esi
210
	movl $(page_directory+0), %esi
198
	movl $(page_directory+2048), %edi
211
	movl $(page_directory+2048), %edi
199
	xorl %ecx, %ecx
212
	xorl %ecx, %ecx
200
	xorl %ebx, %ebx
213
	xorl %ebx, %ebx
201
0:
214
0:
202
	movl $((1<<7)|(1<<0)), %eax
215
	movl $((1<<7)|(1<<0)), %eax
203
	orl %ebx, %eax
216
	orl %ebx, %eax
204
	movl %eax, (%esi,%ecx,4)					# mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
217
	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
218
	movl %eax, (%edi,%ecx,4)					# mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
206
	addl $(4*1024*1024), %ebx
219
	addl $(4*1024*1024), %ebx
207
 
220
 
208
	incl %ecx
221
	incl %ecx
209
	cmpl $512, %ecx
222
	cmpl $512, %ecx
210
	jl 0b
223
	jl 0b
211
 
224
 
212
	movl %esi, %cr3
225
	movl %esi, %cr3
213
	
226
	
214
	# turn paging on
227
	# turn paging on
215
	movl %cr0, %ebx
228
	movl %cr0, %ebx
216
	orl $(1<<31), %ebx
229
	orl $(1<<31), %ebx
217
	movl %ebx, %cr0
230
	movl %ebx, %cr0
218
	ret
231
	ret
219
 
232
 
220
 
233
 
221
.section K_DATA_START, "aw", @progbits
234
.section K_DATA_START, "aw", @progbits
222
 
235
 
223
.align 4096
236
.align 4096
224
page_directory:
237
page_directory:
225
	.space 4096, 0
238
	.space 4096, 0
226
 
239