2 * $NetBSD: loadfile.c,v 1.10 1998/06/25 06:45:46 ross Exp $
3 * $FreeBSD: src/sys/boot/efi/libefi/elf_freebsd.c,v 1.11 2003/05/01 03:56:29 peter Exp $
4 * $DragonFly: src/sys/boot/efi/libefi/elf_freebsd.c,v 1.1 2003/11/10 06:08:33 dillon Exp $
8 * Copyright (c) 1997 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
13 * NASA Ames Research Center.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
45 * Copyright (c) 1992, 1993
46 * The Regents of the University of California. All rights reserved.
48 * This code is derived from software contributed to Berkeley by
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. All advertising materials mentioning features or use of this software
60 * must display the following acknowledgement:
61 * This product includes software developed by the University of
62 * California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79 * @(#)boot.c 8.1 (Berkeley) 6/10/93
85 #include <sys/param.h>
86 #include <sys/linker.h>
87 #include <machine/elf.h>
88 #include <machine/bootinfo.h>
89 #include <machine/ia64_cpu.h>
90 #include <machine/pte.h>
91 #include <machine/vmparam.h>
96 #include "bootstrap.h"
100 static int elf64_exec(struct preloaded_file
*amp
);
102 struct file_format ia64_elf
= { elf64_loadfile
, elf64_exec
};
104 static __inline u_int64_t
108 __asm
__volatile("mov %0=psr;;" : "=r" (psr
));
109 __asm
__volatile("rsm psr.ic|psr.i;; srlz.i;;");
114 restore_ic(u_int64_t psr
)
116 __asm
__volatile("mov psr.l=%0;; srlz.i" :: "r" (psr
));
120 * Entered with psr.ic and psr.i both zero.
123 enter_kernel(u_int64_t start
, struct bootinfo
*bi
)
127 __asm
__volatile("srlz.i;;");
128 __asm
__volatile("mov cr.ipsr=%0"
134 __asm
__volatile("mov cr.iip=%0" :: "r"(start
));
135 __asm
__volatile("mov cr.ifs=r0;;");
136 __asm
__volatile("mov ar.rsc=0;; flushrs;;");
137 __asm
__volatile("mov r8=%0" :: "r" (bi
));
138 __asm
__volatile("rfi;;");
142 elf64_exec(struct preloaded_file
*fp
)
144 struct file_metadata
*md
;
149 UINTN mapkey
, pages
, size
;
154 if ((md
= file_findmetadata(fp
, MODINFOMD_ELFHDR
)) == NULL
)
155 return(EFTYPE
); /* XXX actually EFUCKUP */
156 hdr
= (Elf_Ehdr
*)&(md
->md_data
);
159 * Allocate enough pages to hold the bootinfo block and the memory
160 * map EFI will return to us. The memory map has an unknown size,
161 * so we have to determine that first. Note that the AllocatePages
162 * call can itself modify the memory map, so we have to take that
163 * into account as well. The changes to the memory map are caused
164 * by splitting a range of free memory into two (AFAICT), so that
165 * one is marked as being loader data.
168 descsz
= sizeof(EFI_MEMORY_DESCRIPTOR
);
169 BS
->GetMemoryMap(&size
, NULL
, &mapkey
, &descsz
, &descver
);
170 size
+= descsz
+ ((sizeof(struct bootinfo
) + 0x0f) & ~0x0f);
171 pages
= EFI_SIZE_TO_PAGES(size
);
172 status
= BS
->AllocatePages(AllocateAnyPages
, EfiLoaderData
, pages
,
174 if (EFI_ERROR(status
)) {
175 printf("unable to create bootinfo block (status=0x%lx)\n",
180 bzero(bi
, sizeof(struct bootinfo
));
181 bi_load(bi
, fp
, &mapkey
, pages
);
183 printf("Entering %s at 0x%lx...\n", fp
->f_name
, hdr
->e_entry
);
185 status
= BS
->ExitBootServices(IH
, mapkey
);
186 if (EFI_ERROR(status
)) {
187 printf("ExitBootServices returned 0x%lx\n", status
);
194 * Region 6 is direct mapped UC and region 7 is direct mapped
195 * WC. The details of this is controlled by the Alt {I,D}TLB
196 * handlers. Here we just make sure that they have the largest
197 * possible page size to minimise TLB usage.
199 ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
200 ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
202 bzero(&pte
, sizeof(pte
));
204 pte
.pte_ma
= PTE_MA_WB
;
207 pte
.pte_pl
= PTE_PL_KERN
;
208 pte
.pte_ar
= PTE_AR_RWX
;
211 __asm
__volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
212 __asm
__volatile("mov cr.itir=%0" :: "r"(28 << 2));
213 __asm
__volatile("ptr.i %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
214 __asm
__volatile("ptr.d %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
215 __asm
__volatile("srlz.i;;");
216 __asm
__volatile("itr.i itr[%0]=%1;;"
217 :: "r"(0), "r"(*(u_int64_t
*)&pte
));
218 __asm
__volatile("srlz.i;;");
219 __asm
__volatile("itr.d dtr[%0]=%1;;"
220 :: "r"(0), "r"(*(u_int64_t
*)&pte
));
221 __asm
__volatile("srlz.i;;");
223 enter_kernel(hdr
->e_entry
, bi
);