9597 Want hypervisor API for FPU management
[unleashed.git] / usr / src / uts / i86pc / dboot / dboot_elfload.c
blob937bf9c14411d4641ff9df1377db0e238962d93a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <sys/types.h>
29 #include <sys/inttypes.h>
30 #include <sys/systm.h>
31 #include <sys/elf.h>
32 #include <sys/elf_notes.h>
34 #include <util/memcpy.h>
36 #include "dboot_xboot.h"
37 #include "dboot_elfload.h"
38 #include "dboot_printf.h"
40 static caddr_t elf_file = 0;
42 #define PGETBYTES(offset) ((void *)(elf_file + (offset)))
44 static void *
45 getehdr(void)
47 uchar_t *ident;
48 void *hdr = NULL;
50 ident = PGETBYTES(0);
51 if (ident == NULL)
52 dboot_panic("Cannot read kernel ELF header");
54 if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
55 ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
56 dboot_panic("not an ELF file!");
58 if (ident[EI_CLASS] == ELFCLASS32)
59 hdr = PGETBYTES(0);
60 else if (ident[EI_CLASS] == ELFCLASS64)
61 hdr = PGETBYTES(0);
62 else
63 dboot_panic("Unknown ELF class");
65 return (hdr);
70 * parse the elf file for program information
72 int
73 dboot_elfload64(uintptr_t file_image)
75 Elf64_Ehdr *eh;
76 Elf64_Phdr *phdr;
77 Elf64_Shdr *shdr;
78 caddr_t allphdrs, sechdrs;
79 int i;
80 paddr_t src;
81 paddr_t dst;
82 paddr_t next_addr;
84 elf_file = (caddr_t)file_image;
86 allphdrs = NULL;
88 eh = getehdr();
89 if (eh == NULL)
90 dboot_panic("getehdr() failed");
92 if (eh->e_type != ET_EXEC)
93 dboot_panic("not ET_EXEC, e_type = 0x%x", eh->e_type);
95 if (eh->e_phnum == 0 || eh->e_phoff == 0)
96 dboot_panic("no program headers");
99 * Get the program headers.
101 allphdrs = PGETBYTES(eh->e_phoff);
102 if (allphdrs == NULL)
103 dboot_panic("Failed to get program headers e_phnum = %d",
104 eh->e_phnum);
107 * Get the section headers.
109 sechdrs = PGETBYTES(eh->e_shoff);
110 if (sechdrs == NULL)
111 dboot_panic("Failed to get section headers e_shnum = %d",
112 eh->e_shnum);
115 * Next look for interesting program headers.
117 for (i = 0; i < eh->e_phnum; i++) {
118 /*LINTED [ELF program header alignment]*/
119 phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
122 * Dynamically-linked executable.
123 * Complain.
125 if (phdr->p_type == PT_INTERP) {
126 dboot_printf("warning: PT_INTERP section\n");
127 continue;
131 * at this point we only care about PT_LOAD segments
133 if (phdr->p_type != PT_LOAD)
134 continue;
136 if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) {
137 dboot_printf("warning: krtld reloc info?\n");
138 continue;
142 * If memory size is zero just ignore this header.
144 if (phdr->p_memsz == 0)
145 continue;
148 * If load address 1:1 then ignore this header.
150 if (phdr->p_paddr == phdr->p_vaddr) {
151 if (prom_debug)
152 dboot_printf("Skipping PT_LOAD segment for "
153 "paddr = 0x%lx\n", (ulong_t)phdr->p_paddr);
154 continue;
158 * copy the data to kernel area
160 if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG)
161 dboot_panic("Bad paddr for kernel nucleus segment");
162 src = (uintptr_t)PGETBYTES(phdr->p_offset);
163 dst = ktext_phys + phdr->p_paddr - FOUR_MEG;
164 if (prom_debug)
165 dboot_printf("copying %ld bytes from ELF offset 0x%lx "
166 "to physaddr 0x%lx (va=0x%lx)\n",
167 (ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset,
168 (ulong_t)dst, (ulong_t)phdr->p_vaddr);
169 (void) memcpy((void *)(uintptr_t)dst,
170 (void *)(uintptr_t)src, (size_t)phdr->p_filesz);
172 next_addr = dst + phdr->p_filesz;
177 * Next look for bss
179 for (i = 0; i < eh->e_shnum; i++) {
180 shdr = (Elf64_Shdr *)(sechdrs + eh->e_shentsize * i);
182 /* zero out bss */
183 if (shdr->sh_type == SHT_NOBITS) {
184 if (prom_debug)
185 dboot_printf("zeroing BSS %ld bytes from "
186 "physaddr 0x%llx (end=0x%llx)\n",
187 (ulong_t)shdr->sh_size,
188 (long long unsigned)next_addr,
189 next_addr + shdr->sh_size);
190 (void) memset((void *)(uintptr_t)next_addr, 0,
191 shdr->sh_size);
192 break;
197 * Ignore the intepreter (or should we die if there is one??)
199 return (0);