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>
35 #include <sys/inflate.h>
36 #include <machine/elf.h>
37 #include <machine/cpufunc.h>
38 #include <machine/stdarg.h>
41 * Since we are compiled outside of the normal kernel build process, we
42 * need to include opt_global.h manually.
44 #include "opt_global.h"
45 #include "opt_kernname.h"
47 extern char kernel_start[];
48 extern char kernel_end[];
50 static __inline void *
51 memcpy(void *dst, const void *src, size_t len)
57 if (0 && len >= 4 && !((vm_offset_t)d & 3) &&
58 !((vm_offset_t)s & 3)) {
59 *(uint32_t *)d = *(uint32_t *)s;
72 bzero(void *addr, size_t count)
74 char *tmp = (char *)addr;
77 if (count >= 4 && !((vm_offset_t)tmp & 3)) {
90 * Relocate PT_LOAD segements of kernel ELF image to their respective
91 * virtual addresses and return entry point
94 load_kernel(void * kstart)
98 Elf64_Phdr phdr[64] /* XXX */;
99 Elf64_Shdr shdr[64] /* XXX */;
102 Elf32_Phdr phdr[64] /* XXX */;
103 Elf32_Shdr shdr[64] /* XXX */;
107 vm_offset_t lastaddr = 0;
108 int symtabindex = -1;
109 int symstrindex = -1;
112 eh = (Elf64_Ehdr *)kstart;
114 eh = (Elf32_Ehdr *)kstart;
116 entry_point = (void*)eh->e_entry;
117 memcpy(phdr, (void *)(kstart + eh->e_phoff ),
118 eh->e_phnum * sizeof(phdr[0]));
120 memcpy(shdr, (void *)(kstart + eh->e_shoff),
121 sizeof(*shdr) * eh->e_shnum);
123 if (eh->e_shnum * eh->e_shentsize != 0 && eh->e_shoff != 0) {
124 for (i = 0; i < eh->e_shnum; i++) {
125 if (shdr[i].sh_type == SHT_SYMTAB) {
127 * XXX: check if .symtab is in PT_LOAD?
129 if (shdr[i].sh_offset != 0 &&
130 shdr[i].sh_size != 0) {
132 symstrindex = shdr[i].sh_link;
139 * Copy loadable segments
141 for (i = 0; i < eh->e_phnum; i++) {
144 if (phdr[i].p_type != PT_LOAD)
147 memcpy((void *)(phdr[i].p_vaddr),
148 (void*)(kstart + phdr[i].p_offset), phdr[i].p_filesz);
150 /* Clean space from oversized segments, eg: bss. */
151 if (phdr[i].p_filesz < phdr[i].p_memsz)
152 bzero((void *)(phdr[i].p_vaddr + phdr[i].p_filesz),
153 phdr[i].p_memsz - phdr[i].p_filesz);
155 if (lastaddr < phdr[i].p_vaddr + phdr[i].p_memsz)
156 lastaddr = phdr[i].p_vaddr + phdr[i].p_memsz;
159 /* Now grab the symbol tables. */
160 if (symtabindex >= 0 && symstrindex >= 0) {
161 *(Elf_Size *)lastaddr = SYMTAB_MAGIC;
162 lastaddr += sizeof(Elf_Size);
163 *(Elf_Size *)lastaddr = shdr[symtabindex].sh_size +
164 shdr[symstrindex].sh_size + 2*sizeof(Elf_Size);
165 lastaddr += sizeof(Elf_Size);
167 *(Elf_Size *)lastaddr = shdr[symtabindex].sh_size;
168 lastaddr += sizeof(shdr[symtabindex].sh_size);
170 memcpy((void*)lastaddr,
171 shdr[symtabindex].sh_offset + kstart,
172 shdr[symtabindex].sh_size);
173 lastaddr += shdr[symtabindex].sh_size;
176 *(Elf_Size *)lastaddr = shdr[symstrindex].sh_size;
177 lastaddr += sizeof(shdr[symstrindex].sh_size);
180 memcpy((void*)lastaddr,
181 shdr[symstrindex].sh_offset + kstart,
182 shdr[symstrindex].sh_size);
184 /* Do not take any chances */
185 *(Elf_Size *)lastaddr = 0;
191 _startC(register_t a0, register_t a1, register_t a2, register_t a3)
195 void (*entry_point)(register_t, register_t, register_t, register_t);
198 * Relocate segment to the predefined memory location
199 * Most likely it will be KSEG0/KSEG1 address
201 entry_point = load_kernel(kernel_start);
203 /* Pass saved registers to original _start */
204 entry_point(a0, a1, a2, a3);