Subversion Repositories HelenOS

Rev

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

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