Home | History | Annotate | Line # | Download | only in libefi
exec.c revision 1.1.4.3
      1  1.1.4.3  elad /* $NetBSD: exec.c,v 1.1.4.3 2006/05/11 23:26:48 elad Exp $ */
      2  1.1.4.2  elad 
      3  1.1.4.2  elad /*-
      4  1.1.4.2  elad  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.1.4.2  elad  * All rights reserved.
      6  1.1.4.2  elad  *
      7  1.1.4.2  elad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.4.2  elad  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1.4.2  elad  * NASA Ames Research Center.
     10  1.1.4.2  elad  *
     11  1.1.4.2  elad  * Redistribution and use in source and binary forms, with or without
     12  1.1.4.2  elad  * modification, are permitted provided that the following conditions
     13  1.1.4.2  elad  * are met:
     14  1.1.4.2  elad  * 1. Redistributions of source code must retain the above copyright
     15  1.1.4.2  elad  *    notice, this list of conditions and the following disclaimer.
     16  1.1.4.2  elad  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1.4.2  elad  *    notice, this list of conditions and the following disclaimer in the
     18  1.1.4.2  elad  *    documentation and/or other materials provided with the distribution.
     19  1.1.4.2  elad  * 3. All advertising materials mentioning features or use of this software
     20  1.1.4.2  elad  *    must display the following acknowledgement:
     21  1.1.4.2  elad  *	This product includes software developed by the NetBSD
     22  1.1.4.2  elad  *	Foundation, Inc. and its contributors.
     23  1.1.4.2  elad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1.4.2  elad  *    contributors may be used to endorse or promote products derived
     25  1.1.4.2  elad  *    from this software without specific prior written permission.
     26  1.1.4.2  elad  *
     27  1.1.4.2  elad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1.4.2  elad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1.4.2  elad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1.4.2  elad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1.4.2  elad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1.4.2  elad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1.4.2  elad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1.4.2  elad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1.4.2  elad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1.4.2  elad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1.4.2  elad  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1.4.2  elad  */
     39  1.1.4.2  elad /*
     40  1.1.4.2  elad  * Copyright (c) 1992, 1993
     41  1.1.4.2  elad  *	The Regents of the University of California.  All rights reserved.
     42  1.1.4.2  elad  *
     43  1.1.4.2  elad  * This code is derived from software contributed to Berkeley by
     44  1.1.4.2  elad  * Ralph Campbell.
     45  1.1.4.2  elad  *
     46  1.1.4.2  elad  * Redistribution and use in source and binary forms, with or without
     47  1.1.4.2  elad  * modification, are permitted provided that the following conditions
     48  1.1.4.2  elad  * are met:
     49  1.1.4.2  elad  * 1. Redistributions of source code must retain the above copyright
     50  1.1.4.2  elad  *    notice, this list of conditions and the following disclaimer.
     51  1.1.4.2  elad  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.1.4.2  elad  *    notice, this list of conditions and the following disclaimer in the
     53  1.1.4.2  elad  *    documentation and/or other materials provided with the distribution.
     54  1.1.4.2  elad  * 4. Neither the name of the University nor the names of its contributors
     55  1.1.4.2  elad  *    may be used to endorse or promote products derived from this software
     56  1.1.4.2  elad  *    without specific prior written permission.
     57  1.1.4.2  elad  *
     58  1.1.4.2  elad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.1.4.2  elad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.1.4.2  elad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.1.4.2  elad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.1.4.2  elad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.1.4.2  elad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.1.4.2  elad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.1.4.2  elad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.1.4.2  elad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.1.4.2  elad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.1.4.2  elad  * SUCH DAMAGE.
     69  1.1.4.2  elad  *
     70  1.1.4.2  elad  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     71  1.1.4.2  elad  */
     72  1.1.4.2  elad 
     73  1.1.4.2  elad #include <sys/cdefs.h>
     74  1.1.4.3  elad /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/elf_freebsd.c,v 1.13 2004/04/05 23:41:28 imp Exp $"); */
     75  1.1.4.2  elad 
     76  1.1.4.2  elad #include <lib/libsa/stand.h>
     77  1.1.4.2  elad #include <lib/libsa/loadfile.h>
     78  1.1.4.2  elad 
     79  1.1.4.2  elad #include <sys/param.h>
     80  1.1.4.2  elad #include <sys/lock.h>
     81  1.1.4.2  elad 
     82  1.1.4.2  elad #include <machine/elf_machdep.h>
     83  1.1.4.2  elad #include <machine/bootinfo.h>
     84  1.1.4.2  elad #include <machine/ia64_cpu.h>
     85  1.1.4.2  elad #include <machine/pte.h>
     86  1.1.4.2  elad #include <machine/vmparam.h>
     87  1.1.4.2  elad 
     88  1.1.4.2  elad #include <efi.h>
     89  1.1.4.2  elad #include <efilib.h>
     90  1.1.4.2  elad 
     91  1.1.4.2  elad #include "bootstrap.h"
     92  1.1.4.2  elad 
     93  1.1.4.2  elad #define _KERNEL
     94  1.1.4.2  elad 
     95  1.1.4.2  elad static int	elf64_exec(struct preloaded_file *amp);
     96  1.1.4.2  elad 
     97  1.1.4.2  elad struct file_format ia64_elf = { elf64_loadfile, elf64_exec };
     98  1.1.4.2  elad 
     99  1.1.4.2  elad static __inline u_int64_t
    100  1.1.4.2  elad disable_ic()
    101  1.1.4.2  elad {
    102  1.1.4.2  elad 	u_int64_t psr;
    103  1.1.4.2  elad 	__asm __volatile("mov %0=psr;;" : "=r" (psr));
    104  1.1.4.2  elad 	__asm __volatile("rsm psr.ic|psr.i;; srlz.i;;");
    105  1.1.4.2  elad 	return psr;
    106  1.1.4.2  elad }
    107  1.1.4.2  elad 
    108  1.1.4.2  elad static __inline void
    109  1.1.4.2  elad restore_ic(u_int64_t psr)
    110  1.1.4.2  elad {
    111  1.1.4.2  elad 	__asm __volatile("mov psr.l=%0;; srlz.i" :: "r" (psr));
    112  1.1.4.2  elad }
    113  1.1.4.2  elad 
    114  1.1.4.2  elad /*
    115  1.1.4.2  elad  * Entered with psr.ic and psr.i both zero.
    116  1.1.4.2  elad  */
    117  1.1.4.2  elad void
    118  1.1.4.2  elad enter_kernel(u_int64_t start, struct bootinfo *bi)
    119  1.1.4.2  elad {
    120  1.1.4.2  elad 	u_int64_t		psr;
    121  1.1.4.2  elad 
    122  1.1.4.2  elad 	__asm __volatile("srlz.i;;");
    123  1.1.4.2  elad 	__asm __volatile("mov cr.ipsr=%0"
    124  1.1.4.2  elad 			 :: "r"(IA64_PSR_IC
    125  1.1.4.2  elad 				| IA64_PSR_DT
    126  1.1.4.2  elad 				| IA64_PSR_RT
    127  1.1.4.2  elad 				| IA64_PSR_IT
    128  1.1.4.2  elad 				| IA64_PSR_BN));
    129  1.1.4.2  elad 	__asm __volatile("mov cr.iip=%0" :: "r"(start));
    130  1.1.4.2  elad 	__asm __volatile("mov cr.ifs=r0;;");
    131  1.1.4.2  elad 	__asm __volatile("mov ar.rsc=0;; flushrs;;");
    132  1.1.4.2  elad 	__asm __volatile("mov r8=%0" :: "r" (bi));
    133  1.1.4.2  elad 	__asm __volatile("rfi;;");
    134  1.1.4.2  elad }
    135  1.1.4.2  elad 
    136  1.1.4.2  elad static int
    137  1.1.4.2  elad elf64_exec(struct preloaded_file *fp)
    138  1.1.4.2  elad {
    139  1.1.4.2  elad 	pt_entry_t		pte;
    140  1.1.4.2  elad 	struct bootinfo		*bi;
    141  1.1.4.2  elad 	u_int64_t		psr;
    142  1.1.4.2  elad 	UINTN			mapkey, pages, size;
    143  1.1.4.2  elad 	UINTN			descsz;
    144  1.1.4.2  elad 	UINT32			descver;
    145  1.1.4.2  elad 	EFI_STATUS		status;
    146  1.1.4.2  elad 
    147  1.1.4.2  elad 	if (strcmp(fp->f_type, ELF64_KERNELTYPE)) return(EFTYPE);
    148  1.1.4.2  elad 
    149  1.1.4.2  elad 	/*
    150  1.1.4.2  elad 	 * Allocate enough pages to hold the bootinfo block and the memory
    151  1.1.4.2  elad 	 * map EFI will return to us. The memory map has an unknown size,
    152  1.1.4.2  elad 	 * so we have to determine that first. Note that the AllocatePages
    153  1.1.4.2  elad 	 * call can itself modify the memory map, so we have to take that
    154  1.1.4.2  elad 	 * into account as well. The changes to the memory map are caused
    155  1.1.4.2  elad 	 * by splitting a range of free memory into two (AFAICT), so that
    156  1.1.4.2  elad 	 * one is marked as being loader data.
    157  1.1.4.2  elad 	 */
    158  1.1.4.2  elad 	size = 0;
    159  1.1.4.2  elad 	descsz = sizeof(EFI_MEMORY_DESCRIPTOR);
    160  1.1.4.2  elad 	BS->GetMemoryMap(&size, NULL, &mapkey, &descsz, &descver);
    161  1.1.4.2  elad 	size += descsz + ((sizeof(struct bootinfo) + 0x0f) & ~0x0f);
    162  1.1.4.2  elad 	pages = EFI_SIZE_TO_PAGES(size);
    163  1.1.4.2  elad 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
    164  1.1.4.2  elad 	    (void*)&bi);
    165  1.1.4.2  elad 	if (EFI_ERROR(status)) {
    166  1.1.4.2  elad 		printf("unable to create bootinfo block (status=0x%lx)\n",
    167  1.1.4.2  elad 		    (long)status);
    168  1.1.4.2  elad 		return (ENOMEM);
    169  1.1.4.2  elad 	}
    170  1.1.4.2  elad 
    171  1.1.4.2  elad 	bzero(bi, sizeof(struct bootinfo));
    172  1.1.4.2  elad 	bi_load(bi, fp, &mapkey, pages);
    173  1.1.4.2  elad 
    174  1.1.4.2  elad 	printf("Entering %s at 0x%lx...\n", fp->f_name, fp->marks[MARK_ENTRY]);
    175  1.1.4.2  elad 
    176  1.1.4.2  elad 	status = BS->ExitBootServices(IH, mapkey);
    177  1.1.4.2  elad 	if (EFI_ERROR(status)) {
    178  1.1.4.2  elad 		printf("ExitBootServices returned 0x%lx\n", status);
    179  1.1.4.2  elad 		return (EINVAL);
    180  1.1.4.2  elad 	}
    181  1.1.4.2  elad 
    182  1.1.4.2  elad 	psr = disable_ic();
    183  1.1.4.2  elad 
    184  1.1.4.2  elad 	/*
    185  1.1.4.2  elad 	 * Region 6 is direct mapped UC and region 7 is direct mapped
    186  1.1.4.2  elad 	 * WC. The details of this is controlled by the Alt {I,D}TLB
    187  1.1.4.2  elad 	 * handlers. Here we just make sure that they have the largest
    188  1.1.4.2  elad 	 * possible page size to minimise TLB usage.
    189  1.1.4.2  elad 	 */
    190  1.1.4.2  elad 	ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (28 << 2));
    191  1.1.4.2  elad 	ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (28 << 2));
    192  1.1.4.2  elad 
    193  1.1.4.2  elad 	pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
    194  1.1.4.2  elad 	    PTE_PL_KERN | PTE_AR_RWX;
    195  1.1.4.2  elad 
    196  1.1.4.2  elad 	__asm __volatile("mov cr.ifa=%0" :: "r"(IA64_RR_BASE(7)));
    197  1.1.4.2  elad 	__asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
    198  1.1.4.2  elad 	__asm __volatile("ptr.i %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
    199  1.1.4.2  elad 	__asm __volatile("ptr.d %0,%1" :: "r"(IA64_RR_BASE(7)), "r"(28<<2));
    200  1.1.4.2  elad 	__asm __volatile("srlz.i;;");
    201  1.1.4.2  elad 	__asm __volatile("itr.i itr[%0]=%1;;" :: "r"(0), "r"(pte));
    202  1.1.4.2  elad 	__asm __volatile("srlz.i;;");
    203  1.1.4.2  elad 	__asm __volatile("itr.d dtr[%0]=%1;;" :: "r"(0), "r"(pte));
    204  1.1.4.2  elad 	__asm __volatile("srlz.i;;");
    205  1.1.4.2  elad 
    206  1.1.4.2  elad 	enter_kernel(fp->marks[MARK_ENTRY], bi);
    207  1.1.4.2  elad 
    208  1.1.4.2  elad 	restore_ic(psr);
    209  1.1.4.2  elad }
    210