Subversion Repositories HelenOS

Rev

Rev 188 | Rev 206 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
164 palkovsky 1
#
194 palkovsky 2
# Copyright (C) 2001-2004 Ondrej Palkovsky
164 palkovsky 3
# All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
7
# are met:
8
#
9
# - Redistributions of source code must retain the above copyright
10
#   notice, this list of conditions and the following disclaimer.
11
# - Redistributions in binary form must reproduce the above copyright
12
#   notice, this list of conditions and the following disclaimer in the
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
15
#   derived from this software without specific prior written permission.
16
#
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
19
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#
28
 
194 palkovsky 29
#define __ASM__
30
 
31
#include <arch/mm/page.h>	
188 palkovsky 32
#include <arch/mm/ptl.h>
194 palkovsky 33
#include <arch/pm.h>
164 palkovsky 34
 
188 palkovsky 35
#define START_STACK     0x7c00	
36
#define START_STACK_64  $0xffffffff80007c00
37
 
164 palkovsky 38
#
39
# This is where we require any SPARTAN-kernel-compatible boot loader
40
# to pass control in real mode.
41
#
42
# Protected mode tables are statically initialised during compile
43
# time. So we can just load the respective table registers and
44
# switch to protected mode.
45
#
188 palkovsky 46
.section K_TEXT_START
47
.code16
48
.global kernel_image_start
164 palkovsky 49
kernel_image_start:
178 palkovsky 50
	cli
51
	xorw %ax,%ax
52
	movw %ax,%ds
53
	movw %ax,%ss            # initialize stack segment register
188 palkovsky 54
	movl START_STACK,%esp	# initialize stack pointer
178 palkovsky 55
 
188 palkovsky 56
#	call memmap_arch_init
178 palkovsky 57
 
194 palkovsky 58
	movl $0x80000000, %eax  
178 palkovsky 59
	cpuid
60
	cmp $0x80000000, %eax 	# any function > 80000000h?
61
	jbe no_long_mode
194 palkovsky 62
	movl $0x80000001, %eax	# Extended function code 80000001
178 palkovsky 63
	cpuid
64
	bt $29, %edx 		# Test if long mode is supported.
65
	jnc no_long_mode
164 palkovsky 66
 
188 palkovsky 67
	# Load gdtr, idtr
68
	lgdt gdtr_inst
69
	lidt idtr_inst
70
 
71
	movl %cr0,%eax
72
	orl $0x1,%eax
73
	movl %eax,%cr0			# switch to protected mode
164 palkovsky 74
 
194 palkovsky 75
	jmpl $selector(KTEXT32_DES), $now_in_prot
164 palkovsky 76
 
188 palkovsky 77
no_long_mode:
78
1:
79
	jmp 1b
80
 
81
# Protected 16-bit. We want to reuse the code-seg descriptor,
82
# the Default operand size must not be 1 when entering long mode	
83
now_in_prot:  
84
	# Set up stack & data descriptors
194 palkovsky 85
	movw $selector(KDATA_DES), %ax
188 palkovsky 86
	movw %ax, %ds
87
	movw %ax, %fs
88
	movw %ax, %gs
89
	movw %ax, %ss
90
 
91
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
92
	# Paging is not enabled until after long mode is enabled
93
	movl %cr4, %eax
94
	btsl $5, %eax
95
	movl %eax, %cr4
96
 
97
	# Set up paging tables
98
	leal ptl_0, %eax
99
	movl %eax, %cr3
100
 
101
	# Enable long mode
102
	movl $0xc0000080, %ecx   # EFER MSR number
103
	rdmsr                   # Read EFER
104
	btsl $8, %eax            # Set LME=1
105
	wrmsr                   # Write EFER
178 palkovsky 106
 
188 palkovsky 107
	# Enable paging to activate long mode (set CR0.PG=1)
108
	movl %cr0, %eax
109
	btsl $31, %eax
110
	movl %eax, %cr0
111
 
112
	# At this point we are in compatibility mode
194 palkovsky 113
	jmpl $selector(KTEXT_DES), $start64
164 palkovsky 114
 
188 palkovsky 115
.code64
116
start64:
117
	movq START_STACK_64, %rsp
178 palkovsky 118
 
188 palkovsky 119
	lidt idtr_inst
178 palkovsky 120
 
188 palkovsky 121
	call main_bsp   # never returns
178 palkovsky 122
1:
123
	jmp 1b
124
 
125
 
188 palkovsky 126
.section K_DATA_START
164 palkovsky 127
.align 4096
188 palkovsky 128
.global ptl_2
129
ptl_2:	
130
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
131
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
132
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
133
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
134
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
135
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
136
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
137
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
138
 
139
.align 4096
140
.global ptl_1
141
ptl_1:
142
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
143
	.fill 509,8,0
144
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
145
	.fill 2,8,0
146
 
147
.align 4096
148
.global ptl_0
149
ptl_0:
150
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
151
	.fill 510,8,0
152
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
178 palkovsky 153
 
188 palkovsky 154
.global gdtr_inst				
155
gdtr_inst:
194 palkovsky 156
	.word selector(GDT_ITEMS)
157
	.long KA2PA(gdt)
188 palkovsky 158
 
159
.global idtr_inst
160
idtr_inst:
161
	.word 0
194 palkovsky 162
	.long KA2PA(idt)