2 * Copyright (c) 2005 Olivier Houchard. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 #include <machine/asm.h>
28 #include <sys/param.h>
31 #include <sys/elf64.h>
33 #include <sys/elf32.h>
37 * Since we are compiled outside of the normal kernel build process, we
38 * need to include opt_global.h manually.
40 #include "opt_global.h"
42 #include <sys/inflate.h>
43 #include <machine/elf.h>
44 #include <machine/cpufunc.h>
45 #include <machine/stdarg.h>
48 #error Kernel name not provided
51 extern char kernel_start[];
52 extern char kernel_end[];
54 static __inline void *
55 memcpy(void *dst, const void *src, size_t len)
61 if (0 && len >= 4 && !((vm_offset_t)d & 3) &&
62 !((vm_offset_t)s & 3)) {
63 *(uint32_t *)d = *(uint32_t *)s;
76 bzero(void *addr, size_t count)
78 char *tmp = (char *)addr;
81 if (count >= 4 && !((vm_offset_t)tmp & 3)) {
94 * Convert number to pointer, truncate on 64->32 case, sign extend
97 #define mkptr(x) ((void *)(intptr_t)(int)(x))
100 * Relocate PT_LOAD segements of kernel ELF image to their respective
101 * virtual addresses and return entry point
104 load_kernel(void * kstart)
108 Elf64_Phdr phdr[64] /* XXX */;
109 Elf64_Shdr shdr[64] /* XXX */;
112 Elf32_Phdr phdr[64] /* XXX */;
113 Elf32_Shdr shdr[64] /* XXX */;
117 vm_offset_t loadend = 0;
119 int symtabindex = -1;
120 int symstrindex = -1;
124 eh = (Elf64_Ehdr *)kstart;
126 eh = (Elf32_Ehdr *)kstart;
128 entry_point = mkptr(eh->e_entry);
129 memcpy(phdr, (void *)(kstart + eh->e_phoff),
130 eh->e_phnum * sizeof(phdr[0]));
132 memcpy(shdr, (void *)(kstart + eh->e_shoff),
133 sizeof(*shdr) * eh->e_shnum);
135 if (eh->e_shnum * eh->e_shentsize != 0 && eh->e_shoff != 0) {
136 for (i = 0; i < eh->e_shnum; i++) {
137 if (shdr[i].sh_type == SHT_SYMTAB) {
139 * XXX: check if .symtab is in PT_LOAD?
141 if (shdr[i].sh_offset != 0 &&
142 shdr[i].sh_size != 0) {
144 symstrindex = shdr[i].sh_link;
151 * Copy loadable segments
153 for (i = 0; i < eh->e_phnum; i++) {
156 if (phdr[i].p_type != PT_LOAD)
159 memcpy(mkptr(phdr[i].p_vaddr),
160 (void*)(kstart + phdr[i].p_offset), phdr[i].p_filesz);
162 /* Clean space from oversized segments, eg: bss. */
163 if (phdr[i].p_filesz < phdr[i].p_memsz)
164 bzero(mkptr(phdr[i].p_vaddr + phdr[i].p_filesz),
165 phdr[i].p_memsz - phdr[i].p_filesz);
167 if (loadend < phdr[i].p_vaddr + phdr[i].p_memsz)
168 loadend = phdr[i].p_vaddr + phdr[i].p_memsz;
171 /* Now grab the symbol tables. */
172 lastaddr = (intptr_t)(int)loadend;
173 if (symtabindex >= 0 && symstrindex >= 0) {
175 memcpy((void *)lastaddr, &tmp, sizeof(tmp));
176 lastaddr += sizeof(Elf_Size);
177 tmp = shdr[symtabindex].sh_size +
178 shdr[symstrindex].sh_size + 2*sizeof(Elf_Size);
179 memcpy((void *)lastaddr, &tmp, sizeof(tmp));
180 lastaddr += sizeof(Elf_Size);
182 tmp = shdr[symtabindex].sh_size;
183 memcpy((void *)lastaddr, &tmp, sizeof(tmp));
184 lastaddr += sizeof(shdr[symtabindex].sh_size);
186 memcpy((void*)lastaddr,
187 shdr[symtabindex].sh_offset + kstart,
188 shdr[symtabindex].sh_size);
189 lastaddr += shdr[symtabindex].sh_size;
192 tmp = shdr[symstrindex].sh_size;
193 memcpy((void *)lastaddr, &tmp, sizeof(tmp));
194 lastaddr += sizeof(shdr[symstrindex].sh_size);
197 memcpy((void*)lastaddr,
198 shdr[symstrindex].sh_offset + kstart,
199 shdr[symstrindex].sh_size);
201 /* Do not take any chances */
203 memcpy((void *)lastaddr, &tmp, sizeof(tmp));
210 _startC(register_t a0, register_t a1, register_t a2, register_t a3)
214 void (*entry_point)(register_t, register_t, register_t, register_t);
217 * Relocate segment to the predefined memory location
218 * Most likely it will be KSEG0/KSEG1 address
220 entry_point = load_kernel(kernel_start);
222 /* Pass saved registers to original _start */
223 entry_point(a0, a1, a2, a3);