1199767f8SToomas Soome/*-
2199767f8SToomas Soome * Copyright (c) 2003  Peter Wemm <peter@FreeBSD.org>
3199767f8SToomas Soome * All rights reserved.
4199767f8SToomas Soome *
5199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome * modification, are permitted provided that the following conditions
7199767f8SToomas Soome * are met:
8199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome *
14199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome * SUCH DAMAGE.
25199767f8SToomas Soome *
26199767f8SToomas Soome * $FreeBSD$
27199767f8SToomas Soome */
28199767f8SToomas Soome
29199767f8SToomas Soome/*
30199767f8SToomas Soome * Quick and dirty trampoline to get into 64 bit (long) mode and running
31199767f8SToomas Soome * with paging enabled so that we enter the kernel at its linked address.
32199767f8SToomas Soome */
33199767f8SToomas Soome#define MSR_EFER	0xc0000080
34199767f8SToomas Soome#define EFER_LME	0x00000100
35199767f8SToomas Soome#define CR4_PAE		0x00000020
36199767f8SToomas Soome#define CR4_PSE		0x00000010
37199767f8SToomas Soome#define CR0_PG		0x80000000
38199767f8SToomas Soome
39199767f8SToomas Soome/* GRRR. Deal with BTX that links us for a non-zero location */
40199767f8SToomas Soome#define VPBASE	0xa000
41199767f8SToomas Soome#define VTOP(x)	((x) + VPBASE)
42199767f8SToomas Soome
43199767f8SToomas Soome	.data
44199767f8SToomas Soome
45199767f8SToomas Soome	.p2align 12,0x40
46199767f8SToomas Soome
47199767f8SToomas Soome	.globl	PT4
48199767f8SToomas SoomePT4:
49199767f8SToomas Soome	.space	0x1000
50199767f8SToomas Soome	.globl	PT3
51199767f8SToomas SoomePT3:
52199767f8SToomas Soome	.space	0x1000
53199767f8SToomas Soome	.globl	PT2
54199767f8SToomas SoomePT2:
55199767f8SToomas Soome	.space	0x1000
56199767f8SToomas Soome
57199767f8SToomas Soomegdtdesc:
58199767f8SToomas Soome	.word	gdtend - gdt
59199767f8SToomas Soome	.long	VTOP(gdt)		# low
60199767f8SToomas Soome	.long	0			# high
61199767f8SToomas Soome
62199767f8SToomas Soomegdt:
63199767f8SToomas Soome	.long	0			# null descriptor
64199767f8SToomas Soome	.long	0
65199767f8SToomas Soome	.long	0x00000000		# %cs
66199767f8SToomas Soome	.long	0x00209800
67199767f8SToomas Soome	.long	0x00000000		# %ds
68199767f8SToomas Soome	.long	0x00008000
69199767f8SToomas Soomegdtend:
70*55fea89dSDan Cross
71199767f8SToomas Soome	.text
72199767f8SToomas Soome	.code32
73199767f8SToomas Soome
74199767f8SToomas Soome	.globl	amd64_tramp
75199767f8SToomas Soomeamd64_tramp:
76199767f8SToomas Soome	/* Be sure that interrupts are disabled */
77199767f8SToomas Soome	cli
78199767f8SToomas Soome
79199767f8SToomas Soome	/* Turn on EFER.LME */
80199767f8SToomas Soome	movl	$MSR_EFER, %ecx
81199767f8SToomas Soome	rdmsr
82199767f8SToomas Soome	orl	$EFER_LME, %eax
83199767f8SToomas Soome	wrmsr
84199767f8SToomas Soome
85199767f8SToomas Soome	/* Turn on PAE */
86199767f8SToomas Soome	movl	%cr4, %eax
87199767f8SToomas Soome	orl	$CR4_PAE, %eax
88199767f8SToomas Soome	movl	%eax, %cr4
89199767f8SToomas Soome
90199767f8SToomas Soome	/* Set %cr3 for PT4 */
91199767f8SToomas Soome	movl	$VTOP(PT4), %eax
92199767f8SToomas Soome	movl	%eax, %cr3
93199767f8SToomas Soome
94199767f8SToomas Soome	/* Turn on paging (implicitly sets EFER.LMA) */
95199767f8SToomas Soome	movl	%cr0, %eax
96199767f8SToomas Soome	orl	$CR0_PG, %eax
97199767f8SToomas Soome	movl	%eax, %cr0
9803502720SToomas Soome
9903502720SToomas Soome	/* Now we're in compatibility mode. set %cs for long mode */
100199767f8SToomas Soome	movl	$VTOP(gdtdesc), %eax
101199767f8SToomas Soome	movl	VTOP(entry_hi), %esi
102199767f8SToomas Soome	movl	VTOP(entry_lo), %edi
103199767f8SToomas Soome	lgdt	(%eax)
104199767f8SToomas Soome	ljmp	$0x8, $VTOP(longmode)
105199767f8SToomas Soome
106199767f8SToomas Soome	.code64
107199767f8SToomas Soomelongmode:
108199767f8SToomas Soome	/* We're still running V=P, jump to entry point */
109199767f8SToomas Soome	movl	%esi, %eax
110199767f8SToomas Soome	salq	$32, %rax
111199767f8SToomas Soome	orq	%rdi, %rax
112199767f8SToomas Soome	pushq	%rax
113199767f8SToomas Soome	ret
114