xref: /freebsd/stand/userboot/userboot/elf64_freebsd.c (revision 5e8bd45ffb416f6d4e041690e3e656fd907492bf)
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 <vm/vm.h>
35 #include <vm/pmap.h>
36 #include <machine/elf.h>
37 #include <machine/segments.h>
38 #include <stand.h>
39 
40 #include "bootstrap.h"
41 #include "libuserboot.h"
42 
43 static int	elf64_exec(struct preloaded_file *amp);
44 static int	elf64_obj_exec(struct preloaded_file *amp);
45 
46 struct file_format amd64_elf = { elf64_loadfile, elf64_exec };
47 struct file_format amd64_elf_obj = { elf64_obj_loadfile, elf64_obj_exec };
48 
49 #define	GUEST_NULL_SEL		0
50 #define	GUEST_CODE_SEL		1
51 #define	GUEST_DATA_SEL		2
52 #define	GUEST_GDTR_LIMIT	(3 * 8 - 1)
53 
54 static void
setup_freebsd_gdt(struct user_segment_descriptor * gdt)55 setup_freebsd_gdt(struct user_segment_descriptor *gdt)
56 {
57 	gdt[GUEST_NULL_SEL] = (struct user_segment_descriptor) { 0 };
58 	gdt[GUEST_CODE_SEL] = (struct user_segment_descriptor) {
59 	    .sd_p = 1, .sd_long = 1, .sd_type = SDT_MEME
60 	};
61 	gdt[GUEST_DATA_SEL] = (struct user_segment_descriptor) {
62 	    .sd_p = 1, .sd_type = SDT_MEMRO
63 	};
64 }
65 
66 /*
67  * There is an ELF kernel and one or more ELF modules loaded.
68  * We wish to start executing the kernel image, so make such
69  * preparations as are required, and do so.
70  */
71 static int
elf64_exec(struct preloaded_file * fp)72 elf64_exec(struct preloaded_file *fp)
73 {
74 	struct file_metadata	*md;
75 	Elf_Ehdr 		*ehdr;
76 	vm_offset_t		modulep, kernend;
77 	int			err;
78 	int			i;
79 	uint32_t		stack[1024];
80 	pml4_entry_t		PT4[512];
81 	pdp_entry_t		PT3[512];
82 	pd_entry_t		PT2[512];
83 	struct user_segment_descriptor gdt[3];
84 
85 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
86 		return(EFTYPE);
87 	ehdr = (Elf_Ehdr *)&(md->md_data);
88 
89 	err = bi_load64(fp->f_args, &modulep, &kernend);
90 	if (err != 0)
91 		return(err);
92 
93 	bzero(PT4, PAGE_SIZE);
94 	bzero(PT3, PAGE_SIZE);
95 	bzero(PT2, PAGE_SIZE);
96 
97 	/*
98 	 * Build a scratch stack at physical 0x1000, page tables:
99 	 *	PT4 at 0x2000,
100 	 *	PT3 at 0x3000,
101 	 *	PT2 at 0x4000,
102 	 *      gdtr at 0x5000,
103 	 */
104 
105 	/*
106 	 * This is kinda brutal, but every single 1GB VM memory segment
107 	 * points to the same first 1GB of physical memory.  But it is
108 	 * more than adequate.
109 	 */
110 	for (i = 0; i < 512; i++) {
111 		/* Each slot of the level 4 pages points to the same level 3 page */
112 		PT4[i] = (pml4_entry_t) 0x3000;
113 		PT4[i] |= PG_V | PG_RW;
114 
115 		/* Each slot of the level 3 pages points to the same level 2 page */
116 		PT3[i] = (pdp_entry_t) 0x4000;
117 		PT3[i] |= PG_V | PG_RW;
118 
119 		/* The level 2 page slots are mapped with 2MB pages for 1GB. */
120 		PT2[i] = i * (2 * 1024 * 1024);
121 		PT2[i] |= PG_V | PG_RW | PG_PS;
122 	}
123 
124 #ifdef DEBUG
125 	printf("Start @ %#"PRIx64" ...\n", ehdr->e_entry);
126 #endif
127 
128 	dev_cleanup();
129 
130 	stack[0] = 0;		/* return address */
131 	stack[1] = modulep;
132 	stack[2] = kernend;
133 	CALLBACK(copyin, stack, 0x1000, sizeof(stack));
134 	CALLBACK(copyin, PT4, 0x2000, sizeof(PT4));
135 	CALLBACK(copyin, PT3, 0x3000, sizeof(PT3));
136 	CALLBACK(copyin, PT2, 0x4000, sizeof(PT2));
137 	CALLBACK(setreg, 4, 0x1000);
138 
139 	CALLBACK(setmsr, MSR_EFER, EFER_LMA | EFER_LME);
140 	CALLBACK(setcr, 4, CR4_PAE | CR4_VMXE);
141 	CALLBACK(setcr, 3, 0x2000);
142 	CALLBACK(setcr, 0, CR0_PG | CR0_PE | CR0_NE);
143 
144 	setup_freebsd_gdt(gdt);
145 	CALLBACK(copyin, gdt, 0x5000, sizeof(gdt));
146 	CALLBACK(setgdt, 0x5000, sizeof(gdt));
147 
148 	CALLBACK(exec, ehdr->e_entry);
149 
150 	panic("exec returned");
151 }
152 
153 static int
elf64_obj_exec(struct preloaded_file * fp)154 elf64_obj_exec(struct preloaded_file *fp)
155 {
156 
157 	return (EFTYPE);
158 }
159