Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
164 palkovsky 1
#
288 jermar 2
# Copyright (C) 2005 Ondrej Palkovsky
696 decky 3
# Copyright (C) 2006 Martin Decky
164 palkovsky 4
# All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
8
# are met:
9
#
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
14
#   documentation and/or other materials provided with the distribution.
15
# - The name of the author may not be used to endorse or promote products
16
#   derived from this software without specific prior written permission.
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
29
 
695 decky 30
#include <arch/boot/boot.h>
31
#include <arch/boot/memmap.h>
194 palkovsky 32
#include <arch/mm/page.h>	
188 palkovsky 33
#include <arch/mm/ptl.h>
194 palkovsky 34
#include <arch/pm.h>
251 palkovsky 35
#include <arch/cpu.h>
275 palkovsky 36
#include <arch/cpuid.h>
164 palkovsky 37
 
694 decky 38
#define START_STACK	(BOOT_OFFSET - BOOT_STACK_SIZE)
275 palkovsky 39
 
406 jermar 40
.section K_TEXT_START, "ax"
695 decky 41
 
680 decky 42
.code32
43
.align 4
695 decky 44
.global multiboot_image_start
680 decky 45
multiboot_header:
46
	.long MULTIBOOT_HEADER_MAGIC
47
	.long MULTIBOOT_HEADER_FLAGS
48
	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
694 decky 49
	.long multiboot_header
50
	.long unmapped_ktext_start
680 decky 51
	.long 0
52
	.long 0
694 decky 53
	.long multiboot_image_start
188 palkovsky 54
 
680 decky 55
multiboot_image_start:
695 decky 56
	movl $START_STACK, %esp			# initialize stack pointer
57
	lgdt bootstrap_gdtr				# initialize Global Descriptor Table register
680 decky 58
 
685 decky 59
	movw $gdtselector(KDATA_DES), %cx
60
	movw %cx, %es
61
	movw %cx, %gs
62
	movw %cx, %fs
63
	movw %cx, %ds							# kernel data + stack
64
	movw %cx, %ss
65
 
695 decky 66
	jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
685 decky 67
	multiboot_meeting_point:
68
 
695 decky 69
	movl %eax, grub_eax							# save parameters from GRUB
70
	movl %ebx, grub_ebx
71
 
694 decky 72
	# Protected 32-bit. We want to reuse the code-seg descriptor,
73
	# the Default operand size must not be 1 when entering long mode
275 palkovsky 74
 
696 decky 75
	movl $0x80000000, %eax  
76
 	cpuid
77
 	cmp $0x80000000, %eax						# any function > 80000000h?
78
	jbe long_mode_unsupported
79
	movl $(AMD_CPUID_EXTENDED), %eax			# Extended function code 80000001
80
	cpuid
81
	bt $29, %edx								# Test if long mode is supported.
82
 	jc long_mode_supported
83
 
84
	long_mode_unsupported:
85
		cli
86
		hlt
87
 
88
	long_mode_supported:
89
 
188 palkovsky 90
	# Enable 64-bit page transaltion entries - CR4.PAE = 1.
91
	# Paging is not enabled until after long mode is enabled
694 decky 92
 
188 palkovsky 93
	movl %cr4, %eax
94
	btsl $5, %eax
95
	movl %eax, %cr4
96
 
97
	# Set up paging tables
694 decky 98
 
188 palkovsky 99
	leal ptl_0, %eax
100
	movl %eax, %cr3
275 palkovsky 101
 
188 palkovsky 102
	# Enable long mode
178 palkovsky 103
 
694 decky 104
	movl $EFER_MSR_NUM, %ecx	# EFER MSR number
105
	rdmsr						# Read EFER
106
	btsl $AMD_LME_FLAG, %eax	# Set LME=1
107
	wrmsr						# Write EFER
108
 
188 palkovsky 109
	# Enable paging to activate long mode (set CR0.PG=1)
694 decky 110
 
188 palkovsky 111
	movl %cr0, %eax
112
	btsl $31, %eax
113
	movl %eax, %cr0
114
 
115
	# At this point we are in compatibility mode
694 decky 116
 
206 palkovsky 117
	jmpl $gdtselector(KTEXT_DES), $start64
164 palkovsky 118
 
188 palkovsky 119
.code64
120
start64:
275 palkovsky 121
	movq $(PA2KA(START_STACK)), %rsp
695 decky 122
	movl grub_eax, %eax
123
	movl grub_ebx, %ebx
124
 
125
	cmpl $MULTIBOOT_LOADER_MAGIC, %eax				# compare GRUB signature
126
	je valid_boot
127
 
128
		xorl %ecx, %ecx							# no memory size or map available
129
		movl %ecx, e801memorysize
130
		movl %ecx, e820counter
131
 
132
		jmp invalid_boot
133
 
134
	valid_boot:
135
 
136
		movl (%ebx), %eax						# ebx = physical address of struct multiboot_info
137
 
138
		bt $0, %eax								# mbi->flags[0] (mem_lower, mem_upper valid)
139
		jc mem_valid
140
 
141
			xorl %ecx, %ecx
142
			jmp mem_invalid
143
 
144
		mem_valid:
145
		movl 4(%ebx), %ecx						# mbi->mem_lower
146
		addl 8(%ebx), %ecx						# mbi->mem_upper
147
 
148
		mem_invalid:
149
		movl %ecx, e801memorysize
150
 
151
		bt $3, %eax								# mbi->flags[3] (mods_count, mods_addr valid)	
152
		jc mods_valid
153
 
154
			xorl %ecx, %ecx
155
			xorl %edx, %edx
156
			jmp mods_invalid
157
 
158
		mods_valid:
159
		movl 20(%ebx), %ecx						# mbi->mods_count
160
		cmpl $0, %ecx
161
		je mods_invalid
162
 
163
		movl 24(%ebx), %esi						# mbi->mods_addr
164
		movl 0(%esi), %edx						# mods->mod_start
165
		movl 4(%esi), %ecx						# mods->mod_end
166
		subl %edx, %ecx
167
		addl $0x80000000, %edx
168
 
169
		mods_invalid:
170
		movl %ecx, init_size
171
		movl %edx, init_addr
172
 
173
		bt $6, %eax								# mbi->flags[6] (mmap_length, mmap_addr valid)	
174
		jc mmap_valid
175
 
176
			xorl %edx, %edx
177
			jmp mmap_invalid
178
 
179
		mmap_valid:
180
		movl 44(%ebx), %ecx						# mbi->mmap_length
181
		movl 48(%ebx), %esi						# mbi->mmap_addr
182
		movq $e820table, %rdi
183
		xorl %edx, %edx
184
 
185
		mmap_loop:
186
			cmpl $0, %ecx
187
			jle mmap_end
188
 
189
			movl 4(%esi), %eax					# mmap->base_addr_low
190
			movl %eax, (%rdi)
191
 
192
			movl 8(%esi), %eax					# mmap->base_addr_high
193
			movl %eax, 4(%rdi)
194
 
195
			movl 12(%esi), %eax					# mmap->length_low
196
			movl %eax, 8(%rdi)
197
 
198
			movl 16(%esi), %eax					# mmap->length_high
199
			movl %eax, 12(%rdi)
200
 
201
			movl 20(%esi), %eax					# mmap->type
202
			movl %eax, 16(%rdi)
203
 
204
			movl (%esi), %eax					# mmap->size
205
			addl $0x4, %eax
206
			addl %eax, %esi
207
			subl %eax, %ecx
208
			addq $MEMMAP_E820_RECORD_SIZE, %rdi
209
			incl %edx
210
			jmp mmap_loop
211
 
212
		mmap_end:
213
 
214
		mmap_invalid:
215
		movl %edx, e820counter
216
 
217
	invalid_boot:
218
 
696 decky 219
#ifdef CONFIG_SMP
220
 
221
	# copy AP bootstrap routines below 1 MB
222
 
223
	movq $BOOT_OFFSET, %rsi
224
	movq $AP_BOOT_OFFSET, %rdi
225
	movq $_hardcoded_unmapped_size, %rcx
226
	cld
227
	rep movsb
228
 
229
#endif
230
 
694 decky 231
	call main_bsp   # never returns
178 palkovsky 232
 
694 decky 233
	cli
234
	hlt
206 palkovsky 235
 
406 jermar 236
.section K_DATA_START, "aw", @progbits
164 palkovsky 237
.align 4096
206 palkovsky 238
 
417 palkovsky 239
# Identical mapping of first 64MB and the same of -2GB -> 0	
188 palkovsky 240
.global ptl_2
241
ptl_2:	
242
	.quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
243
	.quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
244
	.quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
245
	.quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
246
	.quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
247
	.quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
248
	.quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
249
	.quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
417 palkovsky 250
	.quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
251
	.quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
252
	.quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
253
	.quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
254
	.quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
255
	.quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
256
	.quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
257
	.quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
258
	.quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
259
	.quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
260
	.quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
261
	.quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
262
	.quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
263
	.quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
264
	.quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
265
	.quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
266
	.quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
267
	.quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
268
	.quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
269
	.quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
270
	.quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
271
	.quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
272
	.quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
273
	.quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
188 palkovsky 274
 
275
.align 4096
276
.global ptl_1
277
ptl_1:
278
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
279
	.fill 509,8,0
280
	.quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
293 palkovsky 281
	.fill 1,8,0
188 palkovsky 282
 
283
.align 4096
284
.global ptl_0
285
ptl_0:
286
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
287
	.fill 510,8,0
288
	.quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
178 palkovsky 289
 
695 decky 290
.global bootstrap_gdtr
291
bootstrap_gdtr:
685 decky 292
	.word gdtselector(GDT_ITEMS)
293
	.long KA2PA(gdt)
695 decky 294
 
295
grub_eax:
296
	.long 0
297
 
298
grub_ebx:
299
	.long 0