1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #define __ELF_WORD_SIZE 64
28 #include <sys/param.h>
29 #include <sys/exec.h>
30 #include <sys/linker.h>
31 #ifdef DEBUG
32 #include <machine/_inttypes.h>
33 #endif
34 #include <string.h>
35 #include <i386/include/bootinfo.h>
36 #include <machine/elf.h>
37 #include <stand.h>
38 
39 #include "bootstrap.h"
40 #include "libuserboot.h"
41 
42 static int	elf64_exec(struct preloaded_file *amp);
43 static int	elf64_obj_exec(struct preloaded_file *amp);
44 
45 struct file_format amd64_elf = { elf64_loadfile, elf64_exec };
46 struct file_format amd64_elf_obj = { elf64_obj_loadfile, elf64_obj_exec };
47 
48 #define MSR_EFER        0xc0000080
49 #define EFER_LME        0x00000100
50 #define	EFER_LMA 	0x00000400	/* Long mode active (R) */
51 #define CR4_PAE         0x00000020
52 #define	CR4_VMXE	(1UL << 13)
53 #define CR4_PSE         0x00000010
54 #define CR0_PG          0x80000000
55 #define	CR0_PE		0x00000001	/* Protected mode Enable */
56 #define	CR0_NE		0x00000020	/* Numeric Error enable (EX16 vs IRQ13) */
57 
58 #define PG_V	0x001
59 #define PG_RW	0x002
60 #define PG_PS	0x080
61 
62 typedef uint64_t p4_entry_t;
63 typedef uint64_t p3_entry_t;
64 typedef uint64_t p2_entry_t;
65 
66 #define	GUEST_NULL_SEL		0
67 #define	GUEST_CODE_SEL		1
68 #define	GUEST_DATA_SEL		2
69 #define	GUEST_GDTR_LIMIT	(3 * 8 - 1)
70 
71 static void
72 setup_freebsd_gdt(uint64_t *gdtr)
73 {
74 	gdtr[GUEST_NULL_SEL] = 0;
75 	gdtr[GUEST_CODE_SEL] = 0x0020980000000000;
76 	gdtr[GUEST_DATA_SEL] = 0x0000900000000000;
77 }
78 
79 /*
80  * There is an ELF kernel and one or more ELF modules loaded.
81  * We wish to start executing the kernel image, so make such
82  * preparations as are required, and do so.
83  */
84 static int
85 elf64_exec(struct preloaded_file *fp)
86 {
87 	struct file_metadata	*md;
88 	Elf_Ehdr 		*ehdr;
89 	vm_offset_t		modulep, kernend;
90 	int			err;
91 	int			i;
92 	uint32_t		stack[1024];
93 	p4_entry_t		PT4[512];
94 	p3_entry_t		PT3[512];
95 	p2_entry_t		PT2[512];
96 	uint64_t		gdtr[3];
97 
98 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
99 		return(EFTYPE);
100 	ehdr = (Elf_Ehdr *)&(md->md_data);
101 
102 	err = bi_load64(fp->f_args, &modulep, &kernend);
103 	if (err != 0)
104 		return(err);
105 
106 	bzero(PT4, PAGE_SIZE);
107 	bzero(PT3, PAGE_SIZE);
108 	bzero(PT2, PAGE_SIZE);
109 
110 	/*
111 	 * Build a scratch stack at physical 0x1000, page tables:
112 	 *	PT4 at 0x2000,
113 	 *	PT3 at 0x3000,
114 	 *	PT2 at 0x4000,
115 	 *      gdtr at 0x5000,
116 	 */
117 
118 	/*
119 	 * This is kinda brutal, but every single 1GB VM memory segment
120 	 * points to the same first 1GB of physical memory.  But it is
121 	 * more than adequate.
122 	 */
123 	for (i = 0; i < 512; i++) {
124 		/* Each slot of the level 4 pages points to the same level 3 page */
125 		PT4[i] = (p4_entry_t) 0x3000;
126 		PT4[i] |= PG_V | PG_RW;
127 
128 		/* Each slot of the level 3 pages points to the same level 2 page */
129 		PT3[i] = (p3_entry_t) 0x4000;
130 		PT3[i] |= PG_V | PG_RW;
131 
132 		/* The level 2 page slots are mapped with 2MB pages for 1GB. */
133 		PT2[i] = i * (2 * 1024 * 1024);
134 		PT2[i] |= PG_V | PG_RW | PG_PS;
135 	}
136 
137 #ifdef DEBUG
138 	printf("Start @ %#"PRIx64" ...\n", ehdr->e_entry);
139 #endif
140 
141 	dev_cleanup();
142 
143 	stack[0] = 0;		/* return address */
144 	stack[1] = modulep;
145 	stack[2] = kernend;
146 	CALLBACK(copyin, stack, 0x1000, sizeof(stack));
147 	CALLBACK(copyin, PT4, 0x2000, sizeof(PT4));
148 	CALLBACK(copyin, PT3, 0x3000, sizeof(PT3));
149 	CALLBACK(copyin, PT2, 0x4000, sizeof(PT2));
150 	CALLBACK(setreg, 4, 0x1000);
151 
152 	CALLBACK(setmsr, MSR_EFER, EFER_LMA | EFER_LME);
153 	CALLBACK(setcr, 4, CR4_PAE | CR4_VMXE);
154 	CALLBACK(setcr, 3, 0x2000);
155 	CALLBACK(setcr, 0, CR0_PG | CR0_PE | CR0_NE);
156 
157 	setup_freebsd_gdt(gdtr);
158 	CALLBACK(copyin, gdtr, 0x5000, sizeof(gdtr));
159         CALLBACK(setgdt, 0x5000, sizeof(gdtr));
160 
161 	CALLBACK(exec, ehdr->e_entry);
162 
163 	panic("exec returned");
164 }
165 
166 static int
167 elf64_obj_exec(struct preloaded_file *fp)
168 {
169 
170 	return (EFTYPE);
171 }
172