Subversion Repositories HelenOS-historic

Rev

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

Rev 285 Rev 298
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
#include <arch/boot/boot.h>
29
#include <arch/boot/boot.h>
30
#include <arch/boot/memmapasm.h>
30
#include <arch/boot/memmapasm.h>
31
 
31
 
32
.section K_TEXT_START
32
.section K_TEXT_START
33
.global kernel_image_start
33
.global kernel_image_start
34
 
34
 
35
KTEXT=8
35
KTEXT=8
36
KDATA=16
36
KDATA=16
37
 
37
 
38
.code16
38
.code16
39
#
39
#
40
# This is where we require any SPARTAN-kernel-compatible boot loader
40
# This is where we require any SPARTAN-kernel-compatible boot loader
41
# to pass control in real mode.
41
# to pass control in real mode.
42
#
42
#
43
# Protected mode tables are statically initialised during compile
43
# Protected mode tables are statically initialised during compile
44
# time. So we can just load the respective table registers and
44
# time. So we can just load the respective table registers and
45
# switch to protected mode.
45
# switch to protected mode.
46
#
46
#
47
kernel_image_start:
47
kernel_image_start:
48
	cli
48
	cli
49
	xorw %ax, %ax
49
	xorw %ax, %ax
50
	movw %ax, %ds
50
	movw %ax, %ds
51
	movw %ax, %ss							# initialize stack segment register
51
	movw %ax, %ss							# initialize stack segment register
52
	movl $BOOTSTRAP_OFFSET - 0x400, %esp	# initialize stack pointer
52
	movl $BOOTSTRAP_OFFSET - 0x400, %esp	# initialize stack pointer
53
	
53
	
54
	call memmap_arch_init
54
	call memmap_arch_init
55
	
55
	
56
	lgdt real_bootstrap_gdtr				# initialize Global Descriptor Table register
56
	lgdt real_bootstrap_gdtr				# initialize Global Descriptor Table register
57
	
57
	
58
	movl %cr0, %eax
58
	movl %cr0, %eax
59
	orl $0x1, %eax
59
	orl $0x1, %eax
60
	movl %eax, %cr0							# switch to protected mode
60
	movl %eax, %cr0							# switch to protected mode
61
	
61
	
62
	jmpl $KTEXT, $boot_image_start
62
	jmpl $KTEXT, $boot_image_start
63
	
63
	
64
.code32
64
.code32
65
.align 4
65
.align 4
66
multiboot_header:
66
multiboot_header:
67
	.long MULTIBOOT_HEADER_MAGIC
67
	.long MULTIBOOT_HEADER_MAGIC
68
	.long MULTIBOOT_HEADER_FLAGS
68
	.long MULTIBOOT_HEADER_FLAGS
69
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
69
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
70
	.long multiboot_header + BOOT_OFFSET
70
	.long multiboot_header + BOOT_OFFSET
71
	.long unmapped_ktext_start + BOOT_OFFSET
71
	.long unmapped_ktext_start + BOOT_OFFSET
72
	.long 0
72
	.long 0
73
	.long 0
73
	.long 0
74
	.long multiboot_image_start + BOOT_OFFSET
74
	.long multiboot_image_start + BOOT_OFFSET
75
	
75
	
76
boot_image_start:
76
boot_image_start:
77
	movw $KDATA, %ax
77
	movw $KDATA, %ax
78
	movw %ax, %es
78
	movw %ax, %es
79
	movw %ax, %gs
79
	movw %ax, %gs
80
	movw %ax, %fs
80
	movw %ax, %fs
81
	movw %ax, %ds			# kernel data + stack
81
	movw %ax, %ds			# kernel data + stack
82
	movw %ax, %ss
82
	movw %ax, %ss
83
	
83
	
84
	movb $0xd1, %al			# enable A20 using the keyboard controller
84
	movb $0xd1, %al			# enable A20 using the keyboard controller
85
	outb %al, $0x64
85
	outb %al, $0x64
86
	movb $0xdf, %al
86
	movb $0xdf, %al
87
	outb %al, $0x60
87
	outb %al, $0x60
88
	
88
	
89
	movl $BOOTSTRAP_OFFSET, %esi
89
	movl $BOOTSTRAP_OFFSET, %esi
90
	movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %edi
90
	movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %edi
91
	movl $_hardcoded_kernel_size, %ecx
91
	movl $_hardcoded_kernel_size, %ecx
92
	cld
92
	cld
93
	rep movsb
93
	rep movsb
94
	
94
	
95
	call map_kernel			# map kernel and turn paging on
95
	call map_kernel			# map kernel and turn paging on
96
	
96
	
97
	call main_bsp			# never returns
97
	call main_bsp			# never returns
98
 
98
 
99
	cli
99
	cli
100
	hlt
100
	hlt
101
	
101
	
102
multiboot_image_start:
102
multiboot_image_start:
103
	movl $BOOTSTRAP_OFFSET - 0x400, %esp			# initialize stack pointer
103
	movl $BOOTSTRAP_OFFSET - 0x400, %esp			# initialize stack pointer
104
	
104
	
105
	lgdt protected_bootstrap_gdtr - 0x80000000		# initialize Global Descriptor Table register
105
	lgdt protected_bootstrap_gdtr - 0x80000000		# initialize Global Descriptor Table register
106
 
106
 
107
	movw $KDATA, %cx
107
	movw $KDATA, %cx
108
	movw %cx, %es
108
	movw %cx, %es
109
	movw %cx, %gs
109
	movw %cx, %gs
110
	movw %cx, %fs
110
	movw %cx, %fs
111
	movw %cx, %ds									# kernel data + stack
111
	movw %cx, %ds									# kernel data + stack
112
	movw %cx, %ss
112
	movw %cx, %ss
113
	
113
	
114
	jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET
114
	jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET
115
	multiboot_meeting_point:
115
	multiboot_meeting_point:
116
	
116
	
117
	pushl %ebx										# save parameters from GRUB
117
	pushl %ebx										# save parameters from GRUB
118
	pushl %eax
118
	pushl %eax
119
	
119
	
120
	movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi
120
	movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi
121
	movl $BOOTSTRAP_OFFSET, %edi
121
	movl $BOOTSTRAP_OFFSET, %edi
122
	movl $_hardcoded_unmapped_size, %ecx
122
	movl $_hardcoded_unmapped_size, %ecx
123
	cld
123
	cld
124
	rep movsb
124
	rep movsb
125
	
125
	
126
	call map_kernel									# map kernel and turn paging on
126
	call map_kernel									# map kernel and turn paging on
127
	
127
	
128
	popl %eax
128
	popl %eax
129
	popl %ebx
129
	popl %ebx
130
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax			# compare GRUB signature
130
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax			# compare GRUB signature
131
	je valid_boot
131
	je valid_boot
132
		
132
		
133
		xorl %ecx, %ecx							# no memory size or map available
133
		xorl %ecx, %ecx							# no memory size or map available
134
		movl %ecx, e801memorysize
134
		movl %ecx, e801memorysize
135
		movl %ecx, e820counter
135
		movl %ecx, e820counter
136
		
136
		
137
		jmp invalid_boot
137
		jmp invalid_boot
138
		
138
		
139
	valid_boot:
139
	valid_boot:
140
		
140
		
141
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
141
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
142
		
142
		
143
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
143
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
144
		jc mem_valid
144
		jc mem_valid
145
			
145
			
146
			xorl %ecx, %ecx
146
			xorl %ecx, %ecx
147
			jmp mem_invalid
147
			jmp mem_invalid
148
			
148
			
149
		mem_valid:
149
		mem_valid:
150
		movl 4(%ebx), %ecx						# mbi->mem_lower
150
		movl 4(%ebx), %ecx						# mbi->mem_lower
151
		addl 8(%ebx), %ecx						# mbi->mem_upper
151
		addl 8(%ebx), %ecx						# mbi->mem_upper
152
		
152
		
153
		mem_invalid:
153
		mem_invalid:
154
		movl %ecx, e801memorysize
154
		movl %ecx, e801memorysize
155
		
155
		
156
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
156
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
157
		jc mmap_valid
157
		jc mmap_valid
158
			
158
			
159
			xorl %edx, %edx
159
			xorl %edx, %edx
160
			jmp mmap_invalid
160
			jmp mmap_invalid
161
			
161
			
162
		mmap_valid:
162
		mmap_valid:
163
		movl 44(%ebx), %ecx						# mbi->mmap_length
163
		movl 44(%ebx), %ecx						# mbi->mmap_length
164
		movl 48(%ebx), %esi						# mbi->mmap_addr
164
		movl 48(%ebx), %esi						# mbi->mmap_addr
165
		movl $e820table, %edi
165
		movl $e820table, %edi
166
		xorl %edx, %edx
166
		xorl %edx, %edx
167
		
167
		
168
		mmap_loop:
168
		mmap_loop:
169
			cmpl $0, %ecx
169
			cmpl $0, %ecx
170
			jle mmap_end
170
			jle mmap_end
171
			
171
			
172
			movl 4(%esi), %eax					# mmap->base_addr_low
172
			movl 4(%esi), %eax					# mmap->base_addr_low
173
			movl %eax, (%edi)
173
			movl %eax, (%edi)
174
			
174
			
175
			movl 8(%esi), %eax					# mmap->base_addr_high
175
			movl 8(%esi), %eax					# mmap->base_addr_high
176
			movl %eax, 4(%edi)
176
			movl %eax, 4(%edi)
177
			
177
			
178
			movl 12(%esi), %eax					# mmap->length_low
178
			movl 12(%esi), %eax					# mmap->length_low
179
			movl %eax, 8(%edi)
179
			movl %eax, 8(%edi)
180
			
180
			
181
			movl 16(%esi), %eax					# mmap->length_high
181
			movl 16(%esi), %eax					# mmap->length_high
182
			movl %eax, 12(%edi)
182
			movl %eax, 12(%edi)
183
			
183
			
184
			movl 20(%esi), %eax					# mmap->type
184
			movl 20(%esi), %eax					# mmap->type
185
			movl %eax, 16(%edi)
185
			movl %eax, 16(%edi)
186
			
186
			
187
			movl (%esi), %eax					# mmap->size
187
			movl (%esi), %eax					# mmap->size
188
			addl $0x4, %eax
188
			addl $0x4, %eax
189
			addl %eax, %esi
189
			addl %eax, %esi
190
			subl %eax, %ecx
190
			subl %eax, %ecx
191
			addl $MEMMAP_E820_RECORD_SIZE, %edi
191
			addl $MEMMAP_E820_RECORD_SIZE, %edi
192
			incl %edx
192
			incl %edx
193
			jmp mmap_loop
193
			jmp mmap_loop
194
		
194
		
195
		mmap_end:
195
		mmap_end:
196
		
196
		
197
		mmap_invalid:
197
		mmap_invalid:
198
		movl %edx, e820counter
198
		movl %edx, e820counter
199
		
199
		
200
	invalid_boot:
200
	invalid_boot:
201
	
201
	
202
	call main_bsp - BOOT_OFFSET						# never returns
202
	call main_bsp - BOOT_OFFSET						# never returns
203
 
203
 
204
	cli
204
	cli
205
	hlt
205
	hlt
206
 
206
 
207
.global map_kernel
207
.global map_kernel
208
map_kernel:
208
map_kernel:
209
	#
209
	#
210
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
210
	# Here we setup mapping for both the unmapped and mapped sections of the kernel.
211
	# For simplicity, we set only one 4M page for 0x00000000 and one for 0x80000000.
211
	# For simplicity, we set only one 4M page for 0x00000000 and one for 0x80000000.
212
	#
212
	#
213
	movl %cr4, %ecx
213
	movl %cr4, %ecx
214
	orl $(1<<4), %ecx
214
	orl $(1<<4), %ecx
215
	movl %ecx, %cr4				# turn PSE on
215
	movl %ecx, %cr4				# turn PSE on
216
	
216
	
217
	movl $((1<<7)|(1<<0)), %eax
217
	movl $((1<<7)|(1<<0)), %eax
218
	movl %eax, page_directory		# mapping 0x00000000 => 0x00000000
218
	movl %eax, page_directory		# mapping 0x00000000 => 0x00000000
219
 
219
 
220
	movl $(page_directory+(4096/2)), %edx
220
	movl $(page_directory+2048), %edx
221
	movl %eax, (%edx)			# mapping 0x80000000 => 0x00000000
221
	movl %eax, (%edx)			# mapping 0x80000000 => 0x00000000
222
 
222
 
223
	leal page_directory, %eax
223
	leal page_directory, %eax
224
	movl %eax, %cr3
224
	movl %eax, %cr3
225
	
225
	
226
	# turn paging on
226
	# turn paging on
227
	movl %cr0, %ebx
227
	movl %cr0, %ebx
228
	orl $(1<<31), %ebx
228
	orl $(1<<31), %ebx
229
	movl %ebx, %cr0
229
	movl %ebx, %cr0
230
	ret
230
	ret
231
 
231
 
232
 
232
 
233
.section K_DATA_START
233
.section K_DATA_START
234
 
234
 
235
.align 4096
235
.align 4096
236
page_directory:
236
page_directory:
237
	.space 4096, 0
237
	.space 4096, 0
238
 
238