Subversion Repositories HelenOS

Rev

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

Rev 685 Rev 694
1
#
1
#
2
# Copyright (C) 2005 Ondrej Palkovsky
2
# Copyright (C) 2005 Ondrej Palkovsky
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/mm/page.h>	
29
#include <arch/mm/page.h>	
30
#include <arch/mm/ptl.h>
30
#include <arch/mm/ptl.h>
31
#include <arch/pm.h>
31
#include <arch/pm.h>
32
#include <arch/cpu.h>
32
#include <arch/cpu.h>
33
#include <arch/cpuid.h>
33
#include <arch/cpuid.h>
34
#include <arch/boot/boot.h>
34
#include <arch/boot/boot.h>
35
 
35
 
-
 
36
#define START_STACK	(BOOT_OFFSET - BOOT_STACK_SIZE)
36
#define START_STACK_64  0xffffffff80007c00
37
#define START_STACK_64  0xffffffff80007c00
37
					
38
	
-
 
39
.section K_TEXT_START, "ax"
-
 
40
# .code16
-
 
41
# .global kernel_image_start
-
 
42
# .global multiboot_image_start
-
 
43
# kernel_image_start:
-
 
44
# 	cli
-
 
45
# 	xorw %ax,%ax
-
 
46
# 	movw %ax,%ds
-
 
47
# 	movw %ax,%es
-
 
48
# 	movw %ax,%ss            # initialize stack segment register
-
 
49
# 	movl $(START_STACK), %esp	# initialize stack pointer
38
#
50
# 	
39
# This is where we require any SPARTAN-kernel-compatible boot loader
-
 
40
# to pass control in real mode.
51
# 	call memmap_arch_init
41
#
52
# 	
-
 
53
# 	movl $0x80000000, %eax  
-
 
54
# 	cpuid
-
 
55
# 	cmp $0x80000000, %eax 	# any function > 80000000h?
-
 
56
# 	jbe no_long_mode
42
# Protected mode tables are statically initialised during compile
57
# 	movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001
-
 
58
# 	cpuid
43
# time. So we can just load the respective table registers and
59
# 	bt $29, %edx 		# Test if long mode is supported.
-
 
60
# 	jnc no_long_mode
-
 
61
# 
-
 
62
# 	# Load gdtr, idtr
-
 
63
# 	lgdt real_bootstrap_gdtr_boot
-
 
64
# 	
-
 
65
# 	movl %cr0,%eax
-
 
66
# 	orl $0x1,%eax
44
# switch to protected mode.
67
# 	movl %eax,%cr0			# switch to protected mode
-
 
68
# 
-
 
69
# 	jmpl $gdtselector(KTEXT32_DES), $now_in_prot
-
 
70
# 
-
 
71
# no_long_mode:
-
 
72
# 1:
-
 
73
# 	jmp 1b
45
#
74
# 	
46
 
-
 
47
#define START_STACK (BOOTSTRAP_OFFSET-0x400)
-
 
48
	
-
 
49
.section K_TEXT_START, "ax"
-
 
50
.code16
-
 
51
.global kernel_image_start
-
 
52
.global multiboot_image_start
-
 
53
kernel_image_start:
-
 
54
	cli
-
 
55
	xorw %ax,%ax
-
 
56
	movw %ax,%ds
-
 
57
	movw %ax,%es
-
 
58
	movw %ax,%ss            # initialize stack segment register
-
 
59
	movl $(START_STACK), %esp	# initialize stack pointer
-
 
60
	
-
 
61
	call memmap_arch_init
-
 
62
	
-
 
63
	movl $0x80000000, %eax  
-
 
64
	cpuid
-
 
65
	cmp $0x80000000, %eax 	# any function > 80000000h?
-
 
66
	jbe no_long_mode
-
 
67
	movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001
-
 
68
	cpuid
-
 
69
	bt $29, %edx 		# Test if long mode is supported.
-
 
70
	jnc no_long_mode
-
 
71
 
-
 
72
	# Load gdtr, idtr
-
 
73
	lgdt real_bootstrap_gdtr_boot
-
 
74
	
-
 
75
	movl %cr0,%eax
-
 
76
	orl $0x1,%eax
-
 
77
	movl %eax,%cr0			# switch to protected mode
-
 
78
 
-
 
79
	jmpl $gdtselector(KTEXT32_DES), $now_in_prot
-
 
80
 
-
 
81
no_long_mode:
-
 
82
1:
-
 
83
	jmp 1b
-
 
84
	
-
 
85
.code32
75
.code32
86
.align 4
76
.align 4
87
multiboot_header:
77
multiboot_header:
88
	.long MULTIBOOT_HEADER_MAGIC
78
	.long MULTIBOOT_HEADER_MAGIC
89
	.long MULTIBOOT_HEADER_FLAGS
79
	.long MULTIBOOT_HEADER_FLAGS
90
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
80
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
91
	.long multiboot_header + BOOT_OFFSET
81
	.long multiboot_header
92
	.long unmapped_ktext_start + BOOT_OFFSET
82
	.long unmapped_ktext_start
93
	.long 0
83
	.long 0
94
	.long 0
84
	.long 0
95
	.long multiboot_image_start + BOOT_OFFSET
85
	.long multiboot_image_start
96
 
86
 
97
multiboot_image_start:
87
multiboot_image_start:
98
	movl $START_STACK, %esp				# initialize stack pointer
88
	movl $START_STACK, %esp				# initialize stack pointer
99
	
89
	
100
	lgdt protected_bootstrap_gdtr + BOOT_OFFSET		# initialize Global Descriptor Table register
90
	lgdt protected_bootstrap_gdtr + BOOT_OFFSET		# initialize Global Descriptor Table register
101
 
91
 
102
	movw $gdtselector(KDATA_DES), %cx
92
	movw $gdtselector(KDATA_DES), %cx
103
	movw %cx, %es
93
	movw %cx, %es
104
	movw %cx, %gs
94
	movw %cx, %gs
105
	movw %cx, %fs
95
	movw %cx, %fs
106
	movw %cx, %ds							# kernel data + stack
96
	movw %cx, %ds							# kernel data + stack
107
	movw %cx, %ss
97
	movw %cx, %ss
108
	
98
	
109
	jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point + BOOT_OFFSET
99
	jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point + BOOT_OFFSET
110
	multiboot_meeting_point:
100
	multiboot_meeting_point:
111
	
101
	
112
	hlt
-
 
113
 
-
 
114
# Protected 32-bit. We want to reuse the code-seg descriptor,
102
	# Protected 32-bit. We want to reuse the code-seg descriptor,
115
# the Default operand size must not be 1 when entering long mode
103
	# the Default operand size must not be 1 when entering long mode
116
now_in_prot:  
-
 
117
	# Set up stack & data descriptors
-
 
118
	movw $gdtselector(KDATA_DES), %ax
-
 
119
	movw %ax, %ds
-
 
120
	movw %ax, %ss
-
 
121
 
-
 
122
	movb $0xd1, %al		# enable A20 using the keyboard controller
-
 
123
	outb %al, $0x64
-
 
124
	movb $0xdf, %al
-
 
125
	outb %al, $0x60
-
 
126
 
104
	
-
 
105
	pushl %ebx							# save parameters from GRUB
-
 
106
	pushl %eax
127
	
107
	
128
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
108
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
129
	# Paging is not enabled until after long mode is enabled
109
	# Paging is not enabled until after long mode is enabled
-
 
110
	
130
	movl %cr4, %eax
111
	movl %cr4, %eax
131
	btsl $5, %eax
112
	btsl $5, %eax
132
	movl %eax, %cr4
113
	movl %eax, %cr4
133
 
114
 
134
	# Set up paging tables
115
	# Set up paging tables
-
 
116
	
135
	leal ptl_0, %eax
117
	leal ptl_0, %eax
136
	movl %eax, %cr3
118
	movl %eax, %cr3
137
	
119
	
138
	# Enable long mode
120
	# Enable long mode
-
 
121
	
139
	movl $EFER_MSR_NUM, %ecx   # EFER MSR number
122
	movl $EFER_MSR_NUM, %ecx	# EFER MSR number
140
	rdmsr                   # Read EFER
123
	rdmsr						# Read EFER
141
	btsl $AMD_LME_FLAG, %eax            # Set LME=1
124
	btsl $AMD_LME_FLAG, %eax	# Set LME=1
142
	wrmsr                   # Write EFER
125
	wrmsr						# Write EFER
143
	
126
	
144
	# Enable paging to activate long mode (set CR0.PG=1)
127
	# Enable paging to activate long mode (set CR0.PG=1)
-
 
128
	
145
	movl %cr0, %eax
129
	movl %cr0, %eax
146
	btsl $31, %eax
130
	btsl $31, %eax
147
	movl %eax, %cr0
131
	movl %eax, %cr0
148
	
132
	
149
	# At this point we are in compatibility mode
133
	# At this point we are in compatibility mode
-
 
134
	
150
	jmpl $gdtselector(KTEXT_DES), $start64
135
	jmpl $gdtselector(KTEXT_DES), $start64
151
 
136
 
152
.code64
137
.code64
153
start64:
138
start64:
154
	movq $(PA2KA(START_STACK)), %rsp
139
	movq $(PA2KA(START_STACK)), %rsp
155
 
140
 
156
	# Copy kernel to higher physical memory
-
 
157
	movq $BOOTSTRAP_OFFSET, %rsi
-
 
158
	movq $BOOTSTRAP_OFFSET + BOOT_OFFSET, %rdi
-
 
159
	movq $_hardcoded_kernel_size, %rcx
-
 
160
	cld
-
 
161
	rep movsb
-
 
162
	
-
 
163
	call main_bsp   # never returns
141
	call main_bsp   # never returns
164
1:
142
	
165
	jmp 1b
143
	cli
-
 
144
	hlt
166
				
145
				
167
.section K_DATA_START, "aw", @progbits
146
.section K_DATA_START, "aw", @progbits
168
.align 4096
147
.align 4096
169
 
148
 
170
# Identical mapping of first 64MB and the same of -2GB -> 0	
149
# Identical mapping of first 64MB and the same of -2GB -> 0	
171
.global ptl_2
150
.global ptl_2
172
ptl_2:	
151
ptl_2:	
173
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
152
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
174
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
153
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
175
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
154
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
176
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
155
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
177
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
156
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
178
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
157
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
179
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
158
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
180
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
159
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
181
	.quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
160
	.quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
182
	.quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
161
	.quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
183
	.quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
162
	.quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
184
	.quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
163
	.quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
185
	.quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
164
	.quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
186
	.quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
165
	.quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
187
	.quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
166
	.quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
188
	.quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
167
	.quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
189
	.quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
168
	.quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
190
	.quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
169
	.quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
191
	.quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
170
	.quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
192
	.quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
171
	.quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
193
	.quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
172
	.quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
194
	.quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
173
	.quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
195
	.quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
174
	.quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
196
	.quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
175
	.quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
197
	.quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
176
	.quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
198
	.quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
177
	.quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
199
	.quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
178
	.quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
200
	.quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
179
	.quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
201
	.quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
180
	.quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
202
	.quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
181
	.quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
203
	.quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
182
	.quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
204
	.quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
183
	.quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
205
	
184
	
206
.align 4096
185
.align 4096
207
.global ptl_1
186
.global ptl_1
208
ptl_1:
187
ptl_1:
209
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
188
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
210
	.fill 509,8,0
189
	.fill 509,8,0
211
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
190
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
212
	.fill 1,8,0
191
	.fill 1,8,0
213
	
192
	
214
.align 4096
193
.align 4096
215
.global ptl_0
194
.global ptl_0
216
ptl_0:
195
ptl_0:
217
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
196
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
218
	.fill 510,8,0
197
	.fill 510,8,0
219
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
198
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
220
 
199
 
221
.global real_bootstrap_gdtr_boot
-
 
222
real_bootstrap_gdtr_boot:
-
 
223
	.word gdtselector(GDT_ITEMS)
-
 
224
	.long KA2PA(gdt)-BOOT_OFFSET
-
 
225
 
-
 
226
.global protected_bootstrap_gdtr
200
.global protected_bootstrap_gdtr
227
protected_bootstrap_gdtr:
201
protected_bootstrap_gdtr:
228
	.word gdtselector(GDT_ITEMS)
202
	.word gdtselector(GDT_ITEMS)
229
	.long KA2PA(gdt)
203
	.long KA2PA(gdt)
230
 
204