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