xref: /illumos-gate/usr/src/boot/efi/loader/arch/amd64/elf64_freebsd.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2014 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #define __ELF_WORD_SIZE 64
32 #include <sys/param.h>
33 #include <sys/exec.h>
34 #include <sys/linker.h>
35 #include <string.h>
36 #include <machine/elf.h>
37 #include <stand.h>
38 
39 #include <efi.h>
40 #include <efilib.h>
41 
42 #include "bootstrap.h"
43 
44 #include "platform/acfreebsd.h"
45 #include "acconfig.h"
46 #define ACPI_SYSTEM_XFACE
47 #include "actypes.h"
48 #include "actbl.h"
49 
50 #include "loader_efi.h"
51 
52 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
53 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
54 
55 extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
56 
57 static int	elf64_exec(struct preloaded_file *amp);
58 static int	elf64_obj_exec(struct preloaded_file *amp);
59 
60 static struct file_format amd64_elf = { elf64_loadfile, elf64_exec };
61 static struct file_format amd64_elf_obj = { elf64_obj_loadfile, elf64_obj_exec };
62 
63 struct file_format *file_formats[] = {
64 	&amd64_elf,
65 	&amd64_elf_obj,
66 	NULL
67 };
68 
69 #define PG_V    0x001
70 #define PG_RW   0x002
71 #define PG_U    0x004
72 #define PG_PS   0x080
73 
74 typedef u_int64_t p4_entry_t;
75 typedef u_int64_t p3_entry_t;
76 typedef u_int64_t p2_entry_t;
77 static p4_entry_t *PT4;
78 static p3_entry_t *PT3;
79 static p2_entry_t *PT2;
80 
81 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
82 			  uint64_t modulep, p4_entry_t *pagetable,
83 			  uint64_t entry);
84 
85 extern uintptr_t amd64_tramp;
86 extern uint32_t amd64_tramp_size;
87 
88 /*
89  * There is an ELF kernel and one or more ELF modules loaded.
90  * We wish to start executing the kernel image, so make such
91  * preparations as are required, and do so.
92  */
93 static int
94 elf64_exec(struct preloaded_file *fp)
95 {
96 	struct file_metadata	*md;
97 	Elf_Ehdr 		*ehdr;
98 	vm_offset_t		modulep, kernend, trampcode, trampstack;
99 	int			err, i;
100 	ACPI_TABLE_RSDP		*rsdp;
101 	char			buf[24];
102 	int			revision;
103 
104 	rsdp = efi_get_table(&acpi20_guid);
105 	if (rsdp == NULL) {
106 		rsdp = efi_get_table(&acpi_guid);
107 	}
108 	if (rsdp != NULL) {
109 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
110 		setenv("hint.acpi.0.rsdp", buf, 1);
111 		revision = rsdp->Revision;
112 		if (revision == 0)
113 			revision = 1;
114 		sprintf(buf, "%d", revision);
115 		setenv("hint.acpi.0.revision", buf, 1);
116 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
117 		buf[sizeof(rsdp->OemId)] = '\0';
118 		setenv("hint.acpi.0.oem", buf, 1);
119 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
120 		setenv("hint.acpi.0.rsdt", buf, 1);
121 		if (revision >= 2) {
122 			/* XXX extended checksum? */
123 			sprintf(buf, "0x%016llx",
124 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
125 			setenv("hint.acpi.0.xsdt", buf, 1);
126 			sprintf(buf, "%d", rsdp->Length);
127 			setenv("hint.acpi.0.xsdt_length", buf, 1);
128 		}
129 	}
130 
131 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
132 		return(EFTYPE);
133 	ehdr = (Elf_Ehdr *)&(md->md_data);
134 
135 	trampcode = (vm_offset_t)0x0000000040000000;
136 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
137 	    (EFI_PHYSICAL_ADDRESS *)&trampcode);
138 	bzero((void *)trampcode, EFI_PAGE_SIZE);
139 	trampstack = trampcode + EFI_PAGE_SIZE - 8;
140 	bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
141 	trampoline = (void *)trampcode;
142 
143 	PT4 = (p4_entry_t *)0x0000000040000000;
144 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
145 	    (EFI_PHYSICAL_ADDRESS *)&PT4);
146 	bzero(PT4, 3 * EFI_PAGE_SIZE);
147 
148 	PT3 = &PT4[512];
149 	PT2 = &PT3[512];
150 
151 	/*
152 	 * This is kinda brutal, but every single 1GB VM memory segment points
153 	 * to the same first 1GB of physical memory.  But it is more than
154 	 * adequate.
155 	 */
156 	for (i = 0; i < 512; i++) {
157 		/* Each slot of the L4 pages points to the same L3 page. */
158 		PT4[i] = (p4_entry_t)PT3;
159 		PT4[i] |= PG_V | PG_RW | PG_U;
160 
161 		/* Each slot of the L3 pages points to the same L2 page. */
162 		PT3[i] = (p3_entry_t)PT2;
163 		PT3[i] |= PG_V | PG_RW | PG_U;
164 
165 		/* The L2 page slots are mapped with 2MB pages for 1GB. */
166 		PT2[i] = i * (2 * 1024 * 1024);
167 		PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
168 	}
169 
170 	printf("Start @ 0x%lx ...\n", ehdr->e_entry);
171 
172 	efi_time_fini();
173 	err = bi_load(fp->f_args, &modulep, &kernend);
174 	if (err != 0) {
175 		efi_time_init();
176 		return(err);
177 	}
178 
179 	dev_cleanup();
180 
181 	trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4,
182 	    ehdr->e_entry);
183 
184 	panic("exec returned");
185 }
186 
187 static int
188 elf64_obj_exec(struct preloaded_file *fp __attribute((unused)))
189 {
190 	return (EFTYPE);
191 }
192