1 /*- 2 * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #define __ELF_WORD_SIZE 64 29 30 #include <sys/param.h> 31 #include <sys/endian.h> 32 #include <sys/linker.h> 33 34 #include <machine/metadata.h> 35 #include <machine/elf.h> 36 37 #include <stand.h> 38 39 #include "bootstrap.h" 40 #include "syscall_nr.h" 41 #include "host_syscall.h" 42 #include "modinfo.h" 43 #include "kboot.h" 44 45 extern char end[]; 46 extern void *kerneltramp; 47 extern size_t szkerneltramp; 48 49 struct trampoline_data { 50 uint32_t kernel_entry; 51 uint32_t dtb; 52 uint32_t phys_mem_offset; 53 uint32_t of_entry; 54 uint32_t mdp; 55 uint32_t mdp_size; 56 }; 57 58 int 59 ppc64_elf_loadfile(char *filename, uint64_t dest, 60 struct preloaded_file **result) 61 { 62 int r; 63 64 r = __elfN(loadfile)(filename, dest, result); 65 if (r != 0) 66 return (r); 67 68 return (0); 69 } 70 71 int 72 ppc64_elf_exec(struct preloaded_file *fp) 73 { 74 struct file_metadata *fmp; 75 vm_offset_t mdp, dtb; 76 Elf_Ehdr *e; 77 int error; 78 uint32_t *trampoline; 79 uint64_t entry; 80 uint64_t trampolinebase; 81 struct trampoline_data *trampoline_data; 82 int nseg; 83 void *kseg; 84 85 if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) { 86 return(EFTYPE); 87 } 88 e = (Elf_Ehdr *)&fmp->md_data; 89 90 /* 91 * Figure out where to put it. 92 * 93 * Linux does not allow to do kexec_load into 94 * any part of memory. Ask arch_loadaddr to 95 * resolve the first available chunk of physical 96 * memory where loading is possible (load_addr). 97 * 98 * Memory organization is shown below. 99 * It is assumed, that text segment offset of 100 * kernel ELF (KERNPHYSADDR) is non-zero, 101 * which is true for PPC/PPC64 architectures, 102 * where default is 0x100000. 103 * 104 * load_addr: trampoline code 105 * load_addr + KERNPHYSADDR: kernel text segment 106 */ 107 trampolinebase = archsw.arch_loadaddr(LOAD_RAW, NULL, 0); 108 printf("Load address at %#jx\n", (uintmax_t)trampolinebase); 109 printf("Relocation offset is %#jx\n", (uintmax_t)elf64_relocation_offset); 110 111 /* Set up loader trampoline */ 112 trampoline = malloc(szkerneltramp); 113 memcpy(trampoline, &kerneltramp, szkerneltramp); 114 115 /* Parse function descriptor for ELFv1 kernels */ 116 if ((e->e_flags & 3) == 2) 117 entry = e->e_entry; 118 else { 119 archsw.arch_copyout(e->e_entry + elf64_relocation_offset, 120 &entry, 8); 121 entry = be64toh(entry); 122 } 123 124 /* 125 * Placeholder for trampoline data is at trampolinebase + 0x08 126 * CAUTION: all data must be Big Endian 127 */ 128 trampoline_data = (void*)&trampoline[2]; 129 trampoline_data->kernel_entry = htobe32(entry + elf64_relocation_offset); 130 trampoline_data->phys_mem_offset = htobe32(0); 131 trampoline_data->of_entry = htobe32(0); 132 133 if ((error = md_load64(fp->f_args, &mdp, &dtb)) != 0) 134 return (error); 135 136 trampoline_data->dtb = htobe32(dtb); 137 trampoline_data->mdp = htobe32(mdp); 138 trampoline_data->mdp_size = htobe32(0xfb5d104d); 139 140 printf("Kernel entry at %#jx (%#x) ...\n", 141 entry, be32toh(trampoline_data->kernel_entry)); 142 printf("DTB at %#x, mdp at %#x\n", 143 be32toh(trampoline_data->dtb), be32toh(trampoline_data->mdp)); 144 145 dev_cleanup(); 146 147 archsw.arch_copyin(trampoline, trampolinebase, szkerneltramp); 148 free(trampoline); 149 150 kboot_kseg_get(&nseg, &kseg); 151 152 error = host_kexec_load(trampolinebase, nseg, kseg, HOST_KEXEC_ARCH_PPC64); 153 if (error != 0) 154 panic("kexec_load returned error: %d", error); 155 156 error = host_reboot(HOST_REBOOT_MAGIC1, HOST_REBOOT_MAGIC2, HOST_REBOOT_CMD_KEXEC, 157 (uintptr_t)NULL); 158 if (error != 0) 159 panic("reboot returned error: %d", error); 160 161 while (1) {} 162 } 163 164 struct file_format ppc_elf64 = 165 { 166 ppc64_elf_loadfile, 167 ppc64_elf_exec 168 }; 169 170 /* 171 * Sort formats so that those that can detect based on arguments rather than 172 * reading the file first. 173 */ 174 175 struct file_format *file_formats[] = { 176 &ppc_elf64, 177 NULL 178 }; 179