Home | History | Annotate | Line # | Download | only in libefi
exec.c revision 1.2.2.2
      1 /* $NetBSD: exec.c,v 1.2.2.2 2006/04/22 11:37:39 simonb 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  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 
     75 #include <lib/libsa/stand.h>
     76 #include <lib/libsa/loadfile.h>
     77 
     78 #include <sys/param.h>
     79 #include <sys/lock.h>
     80 
     81 #include <machine/elf_machdep.h>
     82 #include <machine/bootinfo.h>
     83 #include <machine/ia64_cpu.h>
     84 #include <machine/pte.h>
     85 #include <machine/vmparam.h>
     86 
     87 #include <efi.h>
     88 #include <efilib.h>
     89 
     90 #include "bootstrap.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 static __inline u_int64_t
     99 disable_ic()
    100 {
    101 	u_int64_t psr;
    102 	__asm __volatile("mov %0=psr;;" : "=r" (psr));
    103 	__asm __volatile("rsm psr.ic|psr.i;; srlz.i;;");
    104 	return psr;
    105 }
    106 
    107 static __inline void
    108 restore_ic(u_int64_t psr)
    109 {
    110 	__asm __volatile("mov psr.l=%0;; srlz.i" :: "r" (psr));
    111 }
    112 
    113 /*
    114  * Entered with psr.ic and psr.i both zero.
    115  */
    116 void
    117 enter_kernel(u_int64_t start, struct bootinfo *bi)
    118 {
    119 	u_int64_t		psr;
    120 
    121 	__asm __volatile("srlz.i;;");
    122 	__asm __volatile("mov cr.ipsr=%0"
    123 			 :: "r"(IA64_PSR_IC
    124 				| IA64_PSR_DT
    125 				| IA64_PSR_RT
    126 				| IA64_PSR_IT
    127 				| IA64_PSR_BN));
    128 	__asm __volatile("mov cr.iip=%0" :: "r"(start));
    129 	__asm __volatile("mov cr.ifs=r0;;");
    130 	__asm __volatile("mov ar.rsc=0;; flushrs;;");
    131 	__asm __volatile("mov r8=%0" :: "r" (bi));
    132 	__asm __volatile("rfi;;");
    133 }
    134 
    135 static int
    136 elf64_exec(struct preloaded_file *fp)
    137 {
    138 	pt_entry_t		pte;
    139 	struct bootinfo		*bi;
    140 	u_int64_t		psr;
    141 	UINTN			mapkey, pages, size;
    142 	UINTN			descsz;
    143 	UINT32			descver;
    144 	EFI_STATUS		status;
    145 
    146 	if (strcmp(fp->f_type, ELF64_KERNELTYPE)) return(EFTYPE);
    147 
    148 	/*
    149 	 * Allocate enough pages to hold the bootinfo block and the memory
    150 	 * map EFI will return to us. The memory map has an unknown size,
    151 	 * so we have to determine that first. Note that the AllocatePages
    152 	 * call can itself modify the memory map, so we have to take that
    153 	 * into account as well. The changes to the memory map are caused
    154 	 * by splitting a range of free memory into two (AFAICT), so that
    155 	 * one is marked as being loader data.
    156 	 */
    157 	size = 0;
    158 	descsz = sizeof(EFI_MEMORY_DESCRIPTOR);
    159 	BS->GetMemoryMap(&size, NULL, &mapkey, &descsz, &descver);
    160 	size += descsz + ((sizeof(struct bootinfo) + 0x0f) & ~0x0f);
    161 	pages = EFI_SIZE_TO_PAGES(size);
    162 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
    163 	    (void*)&bi);
    164 	if (EFI_ERROR(status)) {
    165 		printf("unable to create bootinfo block (status=0x%lx)\n",
    166 		    (long)status);
    167 		return (ENOMEM);
    168 	}
    169 
    170 	bzero(bi, sizeof(struct bootinfo));
    171 	bi_load(bi, fp, &mapkey, pages);
    172 
    173 	printf("Entering %s at 0x%lx...\n", fp->f_name, fp->marks[MARK_ENTRY]);
    174 
    175 	status = BS->ExitBootServices(IH, mapkey);
    176 	if (EFI_ERROR(status)) {
    177 		printf("ExitBootServices returned 0x%lx\n", status);
    178 		return (EINVAL);
    179 	}
    180 
    181 	psr = disable_ic();
    182 
    183 	/*
    184 	 * Region 6 is direct mapped UC and region 7 is direct mapped
    185 	 * WC. The details of this is controlled by the Alt {I,D}TLB
    186 	 * handlers. Here we just make sure that they have the largest
    187 	 * possible page size to minimise TLB usage.
    188 	 */
    189 	ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
    190 	ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
    191 
    192 	pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
    193 	    PTE_PL_KERN | PTE_AR_RWX;
    194 
    195 	__asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
    196 	__asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
    197 	__asm __volatile("ptr.i %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
    198 	__asm __volatile("ptr.d %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
    199 	__asm __volatile("srlz.i;;");
    200 	__asm __volatile("itr.i itr[%0]=%1;;" :: "r"(0), "r"(pte));
    201 	__asm __volatile("srlz.i;;");
    202 	__asm __volatile("itr.d dtr[%0]=%1;;" :: "r"(0), "r"(pte));
    203 	__asm __volatile("srlz.i;;");
    204 
    205 	enter_kernel(fp->marks[MARK_ENTRY], bi);
    206 
    207 	restore_ic(psr);
    208 }
    209