1114379Speter/*-
2114379Speter * Copyright (c) 2003  Peter Wemm <peter@FreeBSD.org>
3114379Speter * All rights reserved.
4114379Speter *
5114379Speter * Redistribution and use in source and binary forms, with or without
6114379Speter * modification, are permitted provided that the following conditions
7114379Speter * are met:
8114379Speter * 1. Redistributions of source code must retain the above copyright
9114379Speter *    notice, this list of conditions and the following disclaimer.
10114379Speter * 2. Redistributions in binary form must reproduce the above copyright
11114379Speter *    notice, this list of conditions and the following disclaimer in the
12114379Speter *    documentation and/or other materials provided with the distribution.
13114379Speter *
14114379Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15114379Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16114379Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17114379Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18114379Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19114379Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20114379Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21114379Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22114379Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23114379Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24114379Speter * SUCH DAMAGE.
25114379Speter *
26114379Speter * $FreeBSD$
27114379Speter */
28114379Speter
29114379Speter/*
30114379Speter * Quick and dirty trampoline to get into 64 bit (long) mode and running
31114379Speter * with paging enabled so that we enter the kernel at its linked address.
32114379Speter */
33114379Speter#define MSR_EFER	0xc0000080
34114379Speter#define EFER_LME	0x00000100
35114379Speter#define CR4_PAE		0x00000020
36114379Speter#define CR4_PSE		0x00000010
37114379Speter#define CR0_PG		0x80000000
38114379Speter
39114379Speter/* GRRR. Deal with BTX that links us for a non-zero location */
40114379Speter#define VPBASE	0xa000
41114379Speter#define VTOP(x)	((x) + VPBASE)
42114379Speter
43114379Speter	.data
44114379Speter
45114379Speter	.p2align 12,0x40
46114379Speter
47114379Speter	.globl	PT4
48114379SpeterPT4:
49114379Speter	.space	0x1000
50114379Speter	.globl	PT3
51114379SpeterPT3:
52114379Speter	.space	0x1000
53114379Speter	.globl	PT2
54114379SpeterPT2:
55114379Speter	.space	0x1000
56114379Speter
57114379Spetergdtdesc:
58114379Speter	.word	gdtend - gdt
59129240Sru	.long	VTOP(gdt)		# low
60129240Sru	.long	0			# high
61114379Speter
62114379Spetergdt:
63129240Sru	.long	0			# null descriptor
64114379Speter	.long	0
65129240Sru	.long	0x00000000		# %cs
66114379Speter	.long	0x00209800
67129240Sru	.long	0x00000000		# %ds
68114379Speter	.long	0x00008000
69114379Spetergdtend:
70114379Speter
71114379Speter	.text
72114379Speter	.code32
73114379Speter
74114379Speter	.globl	amd64_tramp
75114379Speteramd64_tramp:
76114379Speter	/* Be sure that interrupts are disabled */
77114379Speter	cli
78114379Speter
79114379Speter	/* Turn on EFER.LME */
80114379Speter	movl	$MSR_EFER, %ecx
81114379Speter	rdmsr
82114379Speter	orl	$EFER_LME, %eax
83114379Speter	wrmsr
84114379Speter
85114379Speter	/* Turn on PAE */
86114379Speter	movl	%cr4, %eax
87114379Speter	orl	$(CR4_PAE | CR4_PSE), %eax
88114379Speter	movl	%eax, %cr4
89114379Speter
90114379Speter	/* Set %cr3 for PT4 */
91114379Speter	movl	$VTOP(PT4), %eax
92114379Speter	movl	%eax, %cr3
93114379Speter
94114379Speter	/* Turn on paging (implicitly sets EFER.LMA) */
95114379Speter	movl	%cr0, %eax
96114379Speter	orl	$CR0_PG, %eax
97114379Speter	movl	%eax, %cr0
98114379Speter
99114379Speter	/* Now we're in compatability mode. set %cs for long mode */
100114379Speter	movl	$VTOP(gdtdesc), %eax
101114379Speter	movl	VTOP(entry_hi), %esi
102114379Speter	movl	VTOP(entry_lo), %edi
103114379Speter	lgdt	(%eax)
104114379Speter	ljmp	$0x8, $VTOP(longmode)
105114379Speter
106114379Speter	.code64
107114379Speterlongmode:
108114379Speter	/* We're still running V=P, jump to entry point */
109114379Speter	movl	%esi, %eax
110114379Speter	salq	$32, %rax
111115091Speter	orq	%rdi, %rax
112114379Speter	pushq	%rax
113114379Speter	ret
114