1/* $FreeBSD: head/sys/boot/ia64/common/exec.c 95026 2002-04-19 06:43:09Z marcel $ */
2/* $NetBSD: loadfile.c,v 1.10 1998/06/25 06:45:46 ross Exp $ */
3
4/*-
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41/*
42 * Copyright (c) 1992, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * This code is derived from software contributed to Berkeley by
46 * Ralph Campbell.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * @(#)boot.c 8.1 (Berkeley) 6/10/93
77 */
78
79#include <stand.h>
80#include <string.h>
81
82#include <sys/param.h>
83#include <sys/linker.h>
84#include <machine/elf.h>
85#include <machine/bootinfo.h>
86#include <machine/pte.h>
87
88#include <efi.h>
89#include <efilib.h>
90
91#include "bootstrap.h"
92
93#define _KERNEL
94
95static int elf_exec(struct preloaded_file *amp);
96
97struct file_format ia64_elf = { elf_loadfile, elf_exec };
98
99static __inline u_int64_t
100disable_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
108static __inline void
109restore_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 */
117void
118enter_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
136static int
137elf_exec(struct preloaded_file *fp)
138{
139 struct file_metadata *md;
140 Elf_Ehdr *hdr;
141 struct ia64_pte pte;
142 struct bootinfo *bi;
143 u_int64_t psr;
144 UINTN mapkey;
145 EFI_STATUS status;
146
147 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
148 return(EFTYPE); /* XXX actually EFUCKUP */
149 hdr = (Elf_Ehdr *)&(md->md_data);
150
151 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
152 EFI_SIZE_TO_PAGES(sizeof(struct bootinfo)), (void*)&bi);
153 if (EFI_ERROR(status)) {
154 printf("unable to create bootinfo block (status=0x%lx)\n",
155 (long)status);
156 return (ENOMEM);
157 }
158
159 bzero(bi, sizeof(struct bootinfo));
160 bi_load(bi, fp, &mapkey);
161
162 printf("Entering %s at 0x%lx...\n", fp->f_name, hdr->e_entry);
163
164 status = BS->ExitBootServices(IH, mapkey);
165 if (EFI_ERROR(status)) {
166 printf("ExitBootServices returned 0x%lx\n", status);
167 return (EINVAL);
168 }
169
170 psr = disable_ic();
171
172 /*
173 * Region 6 is direct mapped UC and region 7 is direct mapped
174 * WC. The details of this is controlled by the Alt {I,D}TLB
175 * handlers. Here we just make sure that they have the largest
176 * possible page size to minimise TLB usage.
177 */
178 ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
179 ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
180
181 bzero(&pte, sizeof(pte));
182 pte.pte_p = 1;
183 pte.pte_ma = PTE_MA_WB;
184 pte.pte_a = 1;
185 pte.pte_d = 1;
186 pte.pte_pl = PTE_PL_KERN;
187 pte.pte_ar = PTE_AR_RWX;
188 pte.pte_ppn = 0;
189
190 __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
191 __asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
192 __asm __volatile("ptr.i %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
193 __asm __volatile("ptr.d %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
194 __asm __volatile("srlz.i;;");
195 __asm __volatile("itr.i itr[%0]=%1;;"
196 :: "r"(0), "r"(*(u_int64_t*)&pte));
197 __asm __volatile("srlz.i;;");
198 __asm __volatile("itr.d dtr[%0]=%1;;"
199 :: "r"(0), "r"(*(u_int64_t*)&pte));
200 __asm __volatile("srlz.i;;");
201
202 bzero(&pte, sizeof(pte));
203 pte.pte_p = 1;
204 pte.pte_ma = PTE_MA_UC;
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 = 0xffffc000000 >> 12;
210
211 __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_PHYS_TO_RR6(0xffffc000000)));
212 __asm __volatile("mov cr.itir=%0" :: "r"(26 << 2));
213 //__asm __volatile("ptr.d %0,%1" :: "r"(IA64_PHYS_TO_RR6(0xffffc000000)), "r"(26<<2));
214 __asm __volatile("srlz.i;;");
215 __asm __volatile("itr.d dtr[%0]=%1;;"
216 :: "r"(1), "r"(*(u_int64_t*)&pte));
217 __asm __volatile("srlz.i;;");
218
219 enter_kernel(hdr->e_entry, bi);
220
221 restore_ic(psr);
222}
2/* $NetBSD: loadfile.c,v 1.10 1998/06/25 06:45:46 ross Exp $ */
3
4/*-
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41/*
42 * Copyright (c) 1992, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * This code is derived from software contributed to Berkeley by
46 * Ralph Campbell.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * @(#)boot.c 8.1 (Berkeley) 6/10/93
77 */
78
79#include <stand.h>
80#include <string.h>
81
82#include <sys/param.h>
83#include <sys/linker.h>
84#include <machine/elf.h>
85#include <machine/bootinfo.h>
86#include <machine/pte.h>
87
88#include <efi.h>
89#include <efilib.h>
90
91#include "bootstrap.h"
92
93#define _KERNEL
94
95static int elf_exec(struct preloaded_file *amp);
96
97struct file_format ia64_elf = { elf_loadfile, elf_exec };
98
99static __inline u_int64_t
100disable_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
108static __inline void
109restore_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 */
117void
118enter_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
136static int
137elf_exec(struct preloaded_file *fp)
138{
139 struct file_metadata *md;
140 Elf_Ehdr *hdr;
141 struct ia64_pte pte;
142 struct bootinfo *bi;
143 u_int64_t psr;
144 UINTN mapkey;
145 EFI_STATUS status;
146
147 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
148 return(EFTYPE); /* XXX actually EFUCKUP */
149 hdr = (Elf_Ehdr *)&(md->md_data);
150
151 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
152 EFI_SIZE_TO_PAGES(sizeof(struct bootinfo)), (void*)&bi);
153 if (EFI_ERROR(status)) {
154 printf("unable to create bootinfo block (status=0x%lx)\n",
155 (long)status);
156 return (ENOMEM);
157 }
158
159 bzero(bi, sizeof(struct bootinfo));
160 bi_load(bi, fp, &mapkey);
161
162 printf("Entering %s at 0x%lx...\n", fp->f_name, hdr->e_entry);
163
164 status = BS->ExitBootServices(IH, mapkey);
165 if (EFI_ERROR(status)) {
166 printf("ExitBootServices returned 0x%lx\n", status);
167 return (EINVAL);
168 }
169
170 psr = disable_ic();
171
172 /*
173 * Region 6 is direct mapped UC and region 7 is direct mapped
174 * WC. The details of this is controlled by the Alt {I,D}TLB
175 * handlers. Here we just make sure that they have the largest
176 * possible page size to minimise TLB usage.
177 */
178 ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
179 ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
180
181 bzero(&pte, sizeof(pte));
182 pte.pte_p = 1;
183 pte.pte_ma = PTE_MA_WB;
184 pte.pte_a = 1;
185 pte.pte_d = 1;
186 pte.pte_pl = PTE_PL_KERN;
187 pte.pte_ar = PTE_AR_RWX;
188 pte.pte_ppn = 0;
189
190 __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
191 __asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
192 __asm __volatile("ptr.i %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
193 __asm __volatile("ptr.d %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
194 __asm __volatile("srlz.i;;");
195 __asm __volatile("itr.i itr[%0]=%1;;"
196 :: "r"(0), "r"(*(u_int64_t*)&pte));
197 __asm __volatile("srlz.i;;");
198 __asm __volatile("itr.d dtr[%0]=%1;;"
199 :: "r"(0), "r"(*(u_int64_t*)&pte));
200 __asm __volatile("srlz.i;;");
201
202 bzero(&pte, sizeof(pte));
203 pte.pte_p = 1;
204 pte.pte_ma = PTE_MA_UC;
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 = 0xffffc000000 >> 12;
210
211 __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_PHYS_TO_RR6(0xffffc000000)));
212 __asm __volatile("mov cr.itir=%0" :: "r"(26 << 2));
213 //__asm __volatile("ptr.d %0,%1" :: "r"(IA64_PHYS_TO_RR6(0xffffc000000)), "r"(26<<2));
214 __asm __volatile("srlz.i;;");
215 __asm __volatile("itr.d dtr[%0]=%1;;"
216 :: "r"(1), "r"(*(u_int64_t*)&pte));
217 __asm __volatile("srlz.i;;");
218
219 enter_kernel(hdr->e_entry, bi);
220
221 restore_ic(psr);
222}