Subversion Repositories HelenOS

Rev

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

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