1 /* $NetBSD: exec.c,v 1.8 2019/05/07 02:05:17 scole Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Ralph Campbell.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)boot.c 8.1 (Berkeley) 6/10/93
64 */
65
66 #include <sys/cdefs.h>
67 /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/elf_freebsd.c,v 1.13 2004/04/05 23:41:28 imp Exp $"); */
68
69 #include <lib/libsa/stand.h>
70 #include <lib/libsa/loadfile.h>
71 #include <lib/libkern/libkern.h>
72
73 #include <sys/param.h>
74 #include <sys/lock.h>
75
76 #include <machine/elf_machdep.h>
77 #include <machine/bootinfo.h>
78 #include <machine/ia64_cpu.h>
79 #include <machine/pte.h>
80 #include <machine/vmparam.h>
81
82 #include <efi.h>
83 #include <efilib.h>
84 #include <efiboot.h>
85
86 #include <machine/efilib.h>
87
88 #include "bootstrap.h"
89
90 #define _KERNEL
91
92 static int elf64_exec(struct preloaded_file *amp);
93
94 struct file_format ia64_elf = { elf64_loadfile, elf64_exec };
95
96 static __inline u_int64_t
disable_ic(void)97 disable_ic(void)
98 {
99 u_int64_t psr;
100 __asm __volatile("mov %0=psr;;" : "=r" (psr));
101 __asm __volatile("rsm psr.ic|psr.i;; srlz.i;;");
102 return psr;
103 }
104
105 static __inline void
restore_ic(u_int64_t psr)106 restore_ic(u_int64_t psr)
107 {
108 __asm __volatile("mov psr.l=%0;; srlz.i" :: "r" (psr));
109 }
110
111 /*
112 * Entered with psr.ic and psr.i both zero.
113 */
114 void
enter_kernel(u_int64_t start,struct bootinfo * bi)115 enter_kernel(u_int64_t start, struct bootinfo *bi)
116 {
117 u_int64_t psr;
118
119 __asm __volatile("srlz.i;;");
120 __asm __volatile("mov cr.ipsr=%0"
121 :: "r"(IA64_PSR_IC
122 | IA64_PSR_DT
123 | IA64_PSR_RT
124 | IA64_PSR_IT
125 | IA64_PSR_BN));
126 __asm __volatile("mov cr.iip=%0" :: "r"(start));
127 __asm __volatile("mov cr.ifs=r0;;");
128 __asm __volatile("mov ar.rsc=0;; flushrs;;");
129 __asm __volatile("mov r8=%0" :: "r" (bi));
130 __asm __volatile("rfi;;");
131 }
132
133 static int
elf64_exec(struct preloaded_file * fp)134 elf64_exec(struct preloaded_file *fp)
135 {
136 pt_entry_t pte;
137 struct bootinfo *bi;
138 u_int64_t psr;
139 UINTN mapkey, pages, size;
140 UINTN descsz;
141 UINT32 descver;
142 EFI_STATUS status;
143
144 if (strcmp(fp->f_type, ELF64_KERNELTYPE)) return(EFTYPE);
145
146 /*
147 * Allocate enough pages to hold the bootinfo block and the memory
148 * map EFI will return to us. The memory map has an unknown size,
149 * so we have to determine that first. Note that the AllocatePages
150 * call can itself modify the memory map, so we have to take that
151 * into account as well. The changes to the memory map are caused
152 * by splitting a range of free memory into two (AFAICT), so that
153 * one is marked as being loader data.
154 */
155 size = 0;
156 descsz = sizeof(EFI_MEMORY_DESCRIPTOR);
157 BS->GetMemoryMap(&size, NULL, &mapkey, &descsz, &descver);
158 size += descsz + ((sizeof(struct bootinfo) + 0x0f) & ~0x0f);
159 pages = EFI_SIZE_TO_PAGES(size);
160 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
161 (void*)&bi);
162 if (EFI_ERROR(status)) {
163 printf("unable to create bootinfo block (status=0x%lx)\n",
164 (long)status);
165 return (ENOMEM);
166 }
167
168 memset(bi, 0, sizeof(struct bootinfo));
169 bi_load(bi, fp, &mapkey, pages);
170
171 printf("Entering %s at 0x%lx...\n", fp->f_name, fp->marks[MARK_ENTRY]);
172
173 status = BS->ExitBootServices(IH, mapkey);
174 if (EFI_ERROR(status)) {
175 printf("ExitBootServices returned 0x%lx\n", status);
176 return (EINVAL);
177 }
178
179 psr = disable_ic();
180
181 /*
182 * Region 6 is direct mapped UC and region 7 is direct mapped
183 * WC. The details of this is controlled by the Alt {I,D}TLB
184 * handlers. Here we just make sure that they have the largest
185 * possible page size to minimise TLB usage.
186 */
187 ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
188 ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
189
190 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
191 PTE_PL_KERN | PTE_AR_RWX;
192
193 __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
194 __asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
195 __asm __volatile("srlz.d;;");
196
197 __asm __volatile("ptr.d %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
198 __asm __volatile("srlz.d;;");
199 __asm __volatile("itr.d dtr[%0]=%1;;" :: "r"(0), "r"(pte));
200 __asm __volatile("srlz.d;;");
201
202 __asm __volatile("ptr.i %0,%1;;" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
203 __asm __volatile("srlz.i;;");
204 __asm __volatile("itr.i itr[%0]=%1;;" :: "r"(0), "r"(pte));
205 __asm __volatile("srlz.i;;");
206
207 enter_kernel(fp->marks[MARK_ENTRY], bi);
208
209 restore_ic(psr);
210 }
211