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