xref: /freebsd/sys/amd64/amd64/locore.S (revision 1f1e2261)
1/*-
2 * Copyright (c) 2003 Peter Wemm <peter@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Copyright (c) 2020, 2021 The FreeBSD Foundation
6 *
7 * Portions of this software were developed by
8 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
9 * the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD$
33 */
34
35#include <machine/asmacros.h>
36#include <machine/psl.h>
37#include <machine/pmap.h>
38#include <machine/specialreg.h>
39
40#include "assym.inc"
41
42/*
43 * Compiled KERNBASE location
44 */
45	.globl	kernbase, kernload, dmapbase, dmapend
46	.set	kernbase,KERNBASE
47	.set    kernload,KERNLOAD
48	.set	dmapbase,DMAP_MIN_ADDRESS
49	.set	dmapend,DMAP_MAX_ADDRESS
50
51#define	BOOTSTACK_SIZE	4096
52
53	.text
54/**********************************************************************
55 *
56 * This is where the loader trampoline start us, set the ball rolling...
57 *
58 * We are called with the stack looking like this:
59 * 0(%rsp) = 32 bit return address (cannot be used)
60 * 4(%rsp) = 32 bit modulep
61 * 8(%rsp) = 32 bit kernend
62 *
63 * We are already in long mode, on a 64 bit %cs and running at KERNBASE.
64 */
65ENTRY(btext)
66
67	/* Don't trust what the loader gives for rflags. */
68	pushq	$PSL_KERNEL
69	popfq
70
71	/* Get onto a stack that we can trust - there is no going back now. */
72	movq	%rsp, %rbp
73	movq	$bootstack,%rsp
74
75#ifdef KASAN
76	/* Bootstrap a shadow map for the boot stack. */
77	movq	$bootstack, %rdi
78	subq	$BOOTSTACK_SIZE, %rdi
79	movq	$BOOTSTACK_SIZE, %rsi
80	call	kasan_init_early
81#endif
82
83	/* Grab metadata pointers from the loader. */
84	movl	4(%rbp),%edi		/* modulep (arg 1) */
85	movl	8(%rbp),%esi		/* kernend (arg 2) */
86	xorq	%rbp, %rbp
87
88	call	hammer_time		/* set up cpu for unix operation */
89	movq	%rax,%rsp		/* set up kstack for mi_startup() */
90	call	mi_startup		/* autoconfiguration, mountroot etc */
910:	hlt
92	jmp	0b
93
94/* la57_trampoline(%rdi pml5) */
95ENTRY(la57_trampoline)
96	movq	%rsp,%r11
97	movq	%rbx,%r10
98	leaq	la57_trampoline_end(%rip),%rsp
99
100	movq	%cr0,%rdx
101	lgdtq	la57_trampoline_gdt_desc(%rip)
102
103	pushq	$(2<<3)
104	leaq	l1(%rip),%rax
105	leaq	l2(%rip),%rbx
106
107	pushq	%rax
108	lretq
109	.code32
110
111l1:	movl	$(3<<3),%eax
112	movl	%eax,%ss
113
114	movl	%edx,%eax
115	andl	$~CR0_PG,%eax
116	movl	%eax,%cr0
117
118	movl	%cr4,%eax
119	orl	$CR4_LA57,%eax
120	movl	%eax,%cr4
121
122	movl	%edi,%cr3
123	movl	%edx,%cr0
124
125	pushl	$(1<<3)
126	pushl	%ebx
127	lretl
128	.code64
129
130l2:	movq	%r11,%rsp
131	movq	%r10,%rbx
132	retq
133	.p2align 4,0
134ENTRY(la57_trampoline_gdt_desc)
135	.word	la57_trampoline_end - la57_trampoline_gdt
136	.long	0		/* filled by pmap_bootstrap_la57 */
137	.p2align 4,0
138ENTRY(la57_trampoline_gdt)
139	.long	0x00000000	/* null desc */
140	.long	0x00000000
141	.long	0x00000000	/* 64bit code */
142	.long	0x00209800
143	.long	0x0000ffff	/* 32bit code */
144	.long	0x00cf9b00
145	.long	0x0000ffff	/* universal data */
146	.long	0x00cf9300
147	.dcb.l	16,0
148ENTRY(la57_trampoline_end)
149
150	.bss
151	ALIGN_DATA			/* just to be sure */
152	.globl	bootstack
153	.space	BOOTSTACK_SIZE		/* space for bootstack - temporary stack */
154bootstack:
155