1 /* $NetBSD: exec.c,v 1.6 2012/12/27 20:21:51 martin 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /*- 33 * Copyright (c) 1992, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Ralph Campbell. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 4. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)boot.c 8.1 (Berkeley) 6/10/93 64 */ 65 66 #include <sys/cdefs.h> 67 /* __FBSDID("$FreeBSD: src/sys/boot/ia64/libski/elf_freebsd.c,v 1.10 2004/04/05 23:41:28 imp Exp $"); */ 68 69 #include <lib/libsa/stand.h> 70 #include <lib/libsa/loadfile.h> 71 #include <lib/libkern/libkern.h> 72 73 #include <sys/param.h> 74 #include <sys/lock.h> 75 76 #include <machine/elf_machdep.h> 77 #include <machine/bootinfo.h> 78 #include <machine/ia64_cpu.h> 79 #include <machine/vmparam.h> 80 81 #include "bootstrap.h" 82 #include "libski.h" 83 84 #define _KERNEL 85 86 static int elf64_exec(struct preloaded_file *amp); 87 88 struct file_format ia64_elf = { elf64_loadfile, elf64_exec }; 89 90 #define PTE_MA_WB 0 91 #define PTE_MA_UC 4 92 #define PTE_MA_UCE 5 93 #define PTE_MA_WC 6 94 #define PTE_MA_NATPAGE 7 95 96 #define PTE_PL_KERN 0 97 #define PTE_PL_USER 3 98 99 #define PTE_AR_R 0 100 #define PTE_AR_RX 1 101 #define PTE_AR_RW 2 102 #define PTE_AR_RWX 3 103 #define PTE_AR_R_RW 4 104 #define PTE_AR_RX_RWX 5 105 #define PTE_AR_RWX_RW 6 106 #define PTE_AR_X_RX 7 107 108 /* 109 * A short-format VHPT entry. Also matches the TLB insertion format. 110 */ 111 struct ia64_pte { 112 u_int64_t pte_p :1; /* bits 0..0 */ 113 u_int64_t pte_rv1 :1; /* bits 1..1 */ 114 u_int64_t pte_ma :3; /* bits 2..4 */ 115 u_int64_t pte_a :1; /* bits 5..5 */ 116 u_int64_t pte_d :1; /* bits 6..6 */ 117 u_int64_t pte_pl :2; /* bits 7..8 */ 118 u_int64_t pte_ar :3; /* bits 9..11 */ 119 u_int64_t pte_ppn :38; /* bits 12..49 */ 120 u_int64_t pte_rv2 :2; /* bits 50..51 */ 121 u_int64_t pte_ed :1; /* bits 52..52 */ 122 u_int64_t pte_ig :11; /* bits 53..63 */ 123 }; 124 125 struct bootinfo bootinfo; 126 127 void 128 enter_kernel(const char* filename, u_int64_t start, struct bootinfo *bi) 129 { 130 printf("Entering %s at 0x%lx...\n", filename, start); 131 132 while (*filename == '/') 133 filename++; 134 ssc(0, (u_int64_t) filename, 0, 0, SSC_LOAD_SYMBOLS); 135 136 __asm __volatile("mov cr.ipsr=%0" 137 :: "r"(IA64_PSR_IC 138 | IA64_PSR_DT 139 | IA64_PSR_RT 140 | IA64_PSR_IT 141 | IA64_PSR_BN)); 142 __asm __volatile("mov cr.iip=%0" :: "r"(start)); 143 __asm __volatile("mov cr.ifs=r0;;"); 144 __asm __volatile("mov r8=%0" :: "r" (bi)); 145 __asm __volatile("rfi;;"); 146 } 147 148 static int 149 elf64_exec(struct preloaded_file *fp) 150 { 151 struct ia64_pte pte; 152 struct bootinfo *bi; 153 154 if (strcmp(fp->f_type, ELF64_KERNELTYPE)) return(EFTYPE); 155 156 157 /* 158 * Ugly hack, similar to linux. Dump the bootinfo into a 159 * special page reserved in the link map. 160 */ 161 bi = &bootinfo; 162 memset(bi, 0, sizeof(struct bootinfo)); 163 bi_load(bi, fp, ""); 164 165 /* 166 * Region 6 is direct mapped UC and region 7 is direct mapped 167 * WC. The details of this is controlled by the Alt {I,D}TLB 168 * handlers. Here we just make sure that they have the largest 169 * possible page size to minimise TLB usage. 170 */ 171 ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2)); 172 ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2)); 173 174 memset(&pte, 0, sizeof(pte)); 175 pte.pte_p = 1; 176 pte.pte_ma = PTE_MA_WB; 177 pte.pte_a = 1; 178 pte.pte_d = 1; 179 pte.pte_pl = PTE_PL_KERN; 180 pte.pte_ar = PTE_AR_RWX; 181 pte.pte_ppn = 0; 182 183 __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7))); 184 __asm __volatile("mov cr.itir=%0" :: "r"(28 << 2)); 185 __asm __volatile("srlz.i;;"); 186 __asm __volatile("itr.i itr[%0]=%1;;" 187 :: "r"(0), "r"(*(u_int64_t*)&pte)); 188 __asm __volatile("srlz.i;;"); 189 __asm __volatile("itr.d dtr[%0]=%1;;" 190 :: "r"(0), "r"(*(u_int64_t*)&pte)); 191 __asm __volatile("srlz.i;;"); 192 193 enter_kernel(fp->f_name, fp->marks[MARK_ENTRY], bi); 194 } 195