Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / ia64 / libski / elf_freebsd.c
1 /* $NetBSD: loadfile.c,v 1.10 1998/06/25 06:45:46 ross 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  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *      This product includes software developed by the University of
57  *      California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  *      @(#)boot.c      8.1 (Berkeley) 6/10/93
75  * $FreeBSD: src/sys/boot/ia64/libski/elf_freebsd.c,v 1.9 2003/09/08 09:11:32 obrien Exp $
76  * $DragonFly: src/sys/boot/ia64/libski/elf_freebsd.c,v 1.1 2003/11/10 06:08:37 dillon Exp $
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/ia64_cpu.h>
87 #include <machine/vmparam.h>
88
89 #include "bootstrap.h"
90 #include "libski.h"
91
92 #define _KERNEL
93
94 static int      elf64_exec(struct preloaded_file *amp);
95
96 struct file_format ia64_elf = { elf64_loadfile, elf64_exec };
97
98 #define PTE_MA_WB       0
99 #define PTE_MA_UC       4
100 #define PTE_MA_UCE      5
101 #define PTE_MA_WC       6
102 #define PTE_MA_NATPAGE  7
103
104 #define PTE_PL_KERN     0
105 #define PTE_PL_USER     3
106
107 #define PTE_AR_R        0
108 #define PTE_AR_RX       1
109 #define PTE_AR_RW       2
110 #define PTE_AR_RWX      3
111 #define PTE_AR_R_RW     4
112 #define PTE_AR_RX_RWX   5
113 #define PTE_AR_RWX_RW   6
114 #define PTE_AR_X_RX     7
115
116 /*
117  * A short-format VHPT entry. Also matches the TLB insertion format.
118  */
119 struct ia64_pte {
120         u_int64_t       pte_p   :1;     /* bits 0..0 */
121         u_int64_t       pte_rv1 :1;     /* bits 1..1 */
122         u_int64_t       pte_ma  :3;     /* bits 2..4 */
123         u_int64_t       pte_a   :1;     /* bits 5..5 */
124         u_int64_t       pte_d   :1;     /* bits 6..6 */
125         u_int64_t       pte_pl  :2;     /* bits 7..8 */
126         u_int64_t       pte_ar  :3;     /* bits 9..11 */
127         u_int64_t       pte_ppn :38;    /* bits 12..49 */
128         u_int64_t       pte_rv2 :2;     /* bits 50..51 */
129         u_int64_t       pte_ed  :1;     /* bits 52..52 */
130         u_int64_t       pte_ig  :11;    /* bits 53..63 */
131 };
132
133 static struct bootinfo bootinfo;
134
135 void
136 enter_kernel(const char* filename, u_int64_t start, struct bootinfo *bi)
137 {
138         printf("Entering %s at 0x%lx...\n", filename, start);
139
140         while (*filename == '/')
141                 filename++;
142         ssc(0, (u_int64_t) filename, 0, 0, SSC_LOAD_SYMBOLS);
143
144         __asm __volatile("mov cr.ipsr=%0"
145                          :: "r"(IA64_PSR_IC
146                                 | IA64_PSR_DT
147                                 | IA64_PSR_RT
148                                 | IA64_PSR_IT
149                                 | IA64_PSR_BN));
150         __asm __volatile("mov cr.iip=%0" :: "r"(start));
151         __asm __volatile("mov cr.ifs=r0;;");
152         __asm __volatile("mov r8=%0" :: "r" (bi));
153         __asm __volatile("rfi;;");
154 }
155
156 static int
157 elf64_exec(struct preloaded_file *fp)
158 {
159         struct file_metadata    *md;
160         Elf_Ehdr                *hdr;
161         struct ia64_pte         pte;
162         struct bootinfo         *bi;
163
164         if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
165                 return(EFTYPE);                 /* XXX actually EFUCKUP */
166         hdr = (Elf_Ehdr *)&(md->md_data);
167
168         /*
169          * Ugly hack, similar to linux. Dump the bootinfo into a
170          * special page reserved in the link map.
171          */
172         bi = &bootinfo;
173         bzero(bi, sizeof(struct bootinfo));
174         bi_load(bi, fp);
175
176         /*
177          * Region 6 is direct mapped UC and region 7 is direct mapped
178          * WC. The details of this is controlled by the Alt {I,D}TLB
179          * handlers. Here we just make sure that they have the largest 
180          * possible page size to minimise TLB usage.
181          */
182         ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
183         ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
184
185         bzero(&pte, sizeof(pte));
186         pte.pte_p = 1;
187         pte.pte_ma = PTE_MA_WB;
188         pte.pte_a = 1;
189         pte.pte_d = 1;
190         pte.pte_pl = PTE_PL_KERN;
191         pte.pte_ar = PTE_AR_RWX;
192         pte.pte_ppn = 0;
193
194         __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
195         __asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
196         __asm __volatile("srlz.i;;");
197         __asm __volatile("itr.i itr[%0]=%1;;"
198                          :: "r"(0), "r"(*(u_int64_t*)&pte));
199         __asm __volatile("srlz.i;;");
200         __asm __volatile("itr.d dtr[%0]=%1;;"
201                          :: "r"(0), "r"(*(u_int64_t*)&pte));
202         __asm __volatile("srlz.i;;");
203
204         enter_kernel(fp->f_name, hdr->e_entry, bi);
205 }