Subversion Repositories HelenOS-historic

Rev

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

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

Generated by GNU Enscript 1.6.6.
-
 
250
 
-
 
251
 
-
 
252