Subversion Repositories HelenOS

Rev

Rev 178 | Rev 194 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 178 Rev 188
Line 24... Line 24...
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
.section K_TEXT_START
-
 
30
.global kernel_image_start
29
#include <arch/mm/ptl.h>
31
 
30
 
-
 
31
#define START_STACK     0x7c00	
-
 
32
#define START_STACK_64  $0xffffffff80007c00
32
.code16
33
					
33
#
34
#
34
# This is where we require any SPARTAN-kernel-compatible boot loader
35
# This is where we require any SPARTAN-kernel-compatible boot loader
35
# to pass control in real mode.
36
# to pass control in real mode.
36
#
37
#
37
# Protected mode tables are statically initialised during compile
38
# Protected mode tables are statically initialised during compile
38
# time. So we can just load the respective table registers and
39
# time. So we can just load the respective table registers and
39
# switch to protected mode.
40
# switch to protected mode.
40
#
41
#
-
 
42
.section K_TEXT_START
-
 
43
.code16
-
 
44
.global kernel_image_start
41
kernel_image_start:
45
kernel_image_start:
42
	cli
46
	cli
43
	xorw %ax,%ax
47
	xorw %ax,%ax
44
	movw %ax,%ds
48
	movw %ax,%ds
45
	movw %ax,%ss            # initialize stack segment register
49
	movw %ax,%ss            # initialize stack segment register
46
	movl $0x7c00,%esp	# initialize stack pointer
50
	movl START_STACK,%esp	# initialize stack pointer
47
	
51
	
48
	call memmap_arch_init
52
#	call memmap_arch_init
49
	
53
	
50
	mov $0x80000000, %eax  
54
	mov $0x80000000, %eax  
51
	cpuid
55
	cpuid
52
	cmp $0x80000000, %eax 	# any function > 80000000h?
56
	cmp $0x80000000, %eax 	# any function > 80000000h?
53
	jbe no_long_mode
57
	jbe no_long_mode
54
	mov $0x80000001, %eax	# Extended function code 80000001
58
	mov $0x80000001, %eax	# Extended function code 80000001
55
	cpuid
59
	cpuid
56
	bt $29, %edx 		# Test if long mode is supported.
60
	bt $29, %edx 		# Test if long mode is supported.
57
	jnc no_long_mode
61
	jnc no_long_mode
58
 
62
 
59
# Fill out GDTR.base, IDTR.base
63
	# Load gdtr, idtr
60
	leal gdtr, %eax
64
	lgdt gdtr_inst
-
 
65
	lidt idtr_inst
-
 
66
	
61
	movl gdt_addr, %ebx
67
	movl %cr0,%eax
62
	movl %ebx, 2(%eax)
68
	orl $0x1,%eax
-
 
69
	movl %eax,%cr0			# switch to protected mode
63
 
70
 
64
	movl idt_addr, %ebx
-
 
65
	leal idtr, %eax
-
 
66
	movl %ebx, 2(%eax)
71
	jmpl $40, $now_in_prot
67
 
72
 
68
# Load gdtr, idtr	
-
 
69
	lgdt gdtr
-
 
70
	lidt idtr
73
no_long_mode:
71
	
74
1:
72
	mov $1, %eax    # Enable protected mode (CR0.PE = 1)
-
 
73
	mov %eax, %cr0 
75
	jmp 1b
74
 
76
 
-
 
77
# Protected 16-bit. We want to reuse the code-seg descriptor,
-
 
78
# the Default operand size must not be 1 when entering long mode	
75
	jmpl $8, $now_in_prot
79
now_in_prot:  
-
 
80
	# Set up stack & data descriptors
-
 
81
	movw $16, %ax
-
 
82
	movw %ax, %ds
-
 
83
	movw %ax, %fs
-
 
84
	movw %ax, %gs
-
 
85
	movw %ax, %ss
-
 
86
 
-
 
87
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
-
 
88
	# Paging is not enabled until after long mode is enabled
-
 
89
	movl %cr4, %eax
-
 
90
	btsl $5, %eax
-
 
91
	movl %eax, %cr4
-
 
92
 
-
 
93
	# Set up paging tables
-
 
94
	leal ptl_0, %eax
-
 
95
	movl %eax, %cr3
-
 
96
		
-
 
97
	# Enable long mode
-
 
98
	movl $0xc0000080, %ecx   # EFER MSR number
-
 
99
	rdmsr                   # Read EFER
-
 
100
	btsl $8, %eax            # Set LME=1
-
 
101
	wrmsr                   # Write EFER
76
	
102
	
-
 
103
	# Enable paging to activate long mode (set CR0.PG=1)
-
 
104
	movl %cr0, %eax
-
 
105
	btsl $31, %eax
77
now_in_prot:
106
	movl %eax, %cr0
78
	
107
	
-
 
108
	# At this point we are in compatibility mode
-
 
109
	jmpl $8, $start64
79
 
110
 
80
no_long_mode:
111
.code64
-
 
112
start64:
-
 
113
	movq START_STACK_64, %rsp
-
 
114
	
-
 
115
	lidt idtr_inst
-
 
116
	
-
 
117
	call main_bsp   # never returns
81
1:
118
1:
82
	jmp 1b
119
	jmp 1b
83
			
120
			
84
 
121
 
85
.section K_DATA_START	
122
.section K_DATA_START
86
.align 4096
123
.align 4096
87
page_directory:
124
.global ptl_2
-
 
125
ptl_2:	
-
 
126
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
127
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
128
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
129
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
130
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
131
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
132
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
133
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
-
 
134
	
-
 
135
.align 4096
-
 
136
.global ptl_1
-
 
137
ptl_1:
-
 
138
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
88
	.space 4096, 0
139
	.fill 509,8,0
-
 
140
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
-
 
141
	.fill 2,8,0
-
 
142
	
-
 
143
.align 4096
-
 
144
.global ptl_0
-
 
145
ptl_0:
-
 
146
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
-
 
147
	.fill 510,8,0
-
 
148
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
89
 
149
 
-
 
150
.global gdtr_inst				
90
gdt_addr:	
151
gdtr_inst:
-
 
152
	.word 7*8  # GDT_ITEMS * 8
91
	.quad gdt + 0x80000000
153
	.long gdt + 0x80000000
-
 
154
 
-
 
155
.global idtr_inst
92
idt_addr:	
156
idtr_inst:
-
 
157
	.word 0
93
	.quad idt + 0x80000000
158
	.long idt + 0x80000000