Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / efi / libefi / elf_freebsd.c
1 /*
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 $
5  */
6
7 /*-
8  * Copyright (c) 1997 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
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.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
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.
30  *
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.
42  */
43
44 /*
45  * Copyright (c) 1992, 1993
46  *      The Regents of the University of California.  All rights reserved.
47  *
48  * This code is derived from software contributed to Berkeley by
49  * Ralph Campbell.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
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.
66  *
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
77  * SUCH DAMAGE.
78  *
79  *      @(#)boot.c      8.1 (Berkeley) 6/10/93
80  */
81
82 #include <stand.h>
83 #include <string.h>
84
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>
92
93 #include <efi.h>
94 #include <efilib.h>
95
96 #include "bootstrap.h"
97
98 #define _KERNEL
99
100 static int      elf64_exec(struct preloaded_file *amp);
101
102 struct file_format ia64_elf = { elf64_loadfile, elf64_exec };
103
104 static __inline u_int64_t
105 disable_ic()
106 {
107         u_int64_t psr;
108         __asm __volatile("mov %0=psr;;" : "=r" (psr));
109         __asm __volatile("rsm psr.ic|psr.i;; srlz.i;;");
110         return psr;
111 }
112
113 static __inline void
114 restore_ic(u_int64_t psr)
115 {
116         __asm __volatile("mov psr.l=%0;; srlz.i" :: "r" (psr));
117 }
118
119 /*
120  * Entered with psr.ic and psr.i both zero.
121  */
122 void
123 enter_kernel(u_int64_t start, struct bootinfo *bi)
124 {
125         u_int64_t               psr;
126
127         __asm __volatile("srlz.i;;");
128         __asm __volatile("mov cr.ipsr=%0"
129                          :: "r"(IA64_PSR_IC
130                                 | IA64_PSR_DT
131                                 | IA64_PSR_RT
132                                 | IA64_PSR_IT
133                                 | IA64_PSR_BN));
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;;");
139 }
140
141 static int
142 elf64_exec(struct preloaded_file *fp)
143 {
144         struct file_metadata    *md;
145         Elf_Ehdr                *hdr;
146         struct ia64_pte         pte;
147         struct bootinfo         *bi;
148         u_int64_t               psr;
149         UINTN                   mapkey, pages, size;
150         UINTN                   descsz;
151         UINT32                  descver;
152         EFI_STATUS              status;
153
154         if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
155                 return(EFTYPE);                 /* XXX actually EFUCKUP */
156         hdr = (Elf_Ehdr *)&(md->md_data);
157
158         /*
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.
166          */
167         size = 0;
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,
173             (void*)&bi);
174         if (EFI_ERROR(status)) {
175                 printf("unable to create bootinfo block (status=0x%lx)\n",
176                     (long)status);
177                 return (ENOMEM);
178         }
179
180         bzero(bi, sizeof(struct bootinfo));
181         bi_load(bi, fp, &mapkey, pages);
182
183         printf("Entering %s at 0x%lx...\n", fp->f_name, hdr->e_entry);
184
185         status = BS->ExitBootServices(IH, mapkey);
186         if (EFI_ERROR(status)) {
187                 printf("ExitBootServices returned 0x%lx\n", status);
188                 return (EINVAL);
189         }
190
191         psr = disable_ic();
192
193         /*
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.
198          */
199         ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
200         ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
201
202         bzero(&pte, sizeof(pte));
203         pte.pte_p = 1;
204         pte.pte_ma = PTE_MA_WB;
205         pte.pte_a = 1;
206         pte.pte_d = 1;
207         pte.pte_pl = PTE_PL_KERN;
208         pte.pte_ar = PTE_AR_RWX;
209         pte.pte_ppn = 0;
210
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;;");
222
223         enter_kernel(hdr->e_entry, bi);
224
225         restore_ic(psr);
226 }