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