work-in-progress

This commit is contained in:
Steffen Vogel 2016-11-23 20:32:42 -05:00
parent 49c86db30b
commit f57357769f
6 changed files with 1811 additions and 29 deletions

View file

@ -65,7 +65,8 @@ XHYVE_SRC := \
FIRMWARE_SRC := \
src/firmware/kexec.c \
src/firmware/fbsd.c
src/firmware/fbsd.c \
src/firmware/multiboot.c
SRC := \
$(VMM_SRC) \

41
include/sys/elf.h Normal file
View file

@ -0,0 +1,41 @@
/*-
* Copyright (c) 2001 David E. O'Brien.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
/*
* This is a Solaris compatibility header
*/
#ifndef _SYS_ELF_H_
#define _SYS_ELF_H_
#include <sys/types.h>
#include <machine/elf.h>
#include <sys/elf32.h>
#include <sys/elf64.h>
#endif /* !_SYS_ELF_H_ */

257
include/sys/elf32.h Normal file
View file

@ -0,0 +1,257 @@
/*-
* Copyright (c) 1996-1998 John D. Polstra.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _SYS_ELF32_H_
#define _SYS_ELF32_H_ 1
#include <sys/elf_common.h>
/*
* ELF definitions common to all 32-bit architectures.
*/
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef int32_t Elf32_Sword;
typedef uint32_t Elf32_Word;
typedef uint64_t Elf32_Lword;
typedef Elf32_Word Elf32_Hashelt;
/* Non-standard class-dependent datatype used for abstraction. */
typedef Elf32_Word Elf32_Size;
typedef Elf32_Sword Elf32_Ssize;
/*
* ELF header.
*/
typedef struct {
unsigned char e_ident[EI_NIDENT]; /* File identification. */
Elf32_Half e_type; /* File type. */
Elf32_Half e_machine; /* Machine architecture. */
Elf32_Word e_version; /* ELF format version. */
Elf32_Addr e_entry; /* Entry point. */
Elf32_Off e_phoff; /* Program header file offset. */
Elf32_Off e_shoff; /* Section header file offset. */
Elf32_Word e_flags; /* Architecture-specific flags. */
Elf32_Half e_ehsize; /* Size of ELF header in bytes. */
Elf32_Half e_phentsize; /* Size of program header entry. */
Elf32_Half e_phnum; /* Number of program header entries. */
Elf32_Half e_shentsize; /* Size of section header entry. */
Elf32_Half e_shnum; /* Number of section header entries. */
Elf32_Half e_shstrndx; /* Section name strings section. */
} Elf32_Ehdr;
/*
* Shared object information, found in SHT_MIPS_LIBLIST.
*/
typedef struct {
Elf32_Word l_name; /* The name of a shared object. */
Elf32_Word l_time_stamp; /* 32-bit timestamp. */
Elf32_Word l_checksum; /* Checksum of visible symbols, sizes. */
Elf32_Word l_version; /* Interface version string index. */
Elf32_Word l_flags; /* Flags (LL_*). */
} Elf32_Lib;
/*
* Section header.
*/
typedef struct {
Elf32_Word sh_name; /* Section name (index into the
section header string table). */
Elf32_Word sh_type; /* Section type. */
Elf32_Word sh_flags; /* Section flags. */
Elf32_Addr sh_addr; /* Address in memory image. */
Elf32_Off sh_offset; /* Offset in file. */
Elf32_Word sh_size; /* Size in bytes. */
Elf32_Word sh_link; /* Index of a related section. */
Elf32_Word sh_info; /* Depends on section type. */
Elf32_Word sh_addralign; /* Alignment in bytes. */
Elf32_Word sh_entsize; /* Size of each entry in section. */
} Elf32_Shdr;
/*
* Program header.
*/
typedef struct {
Elf32_Word p_type; /* Entry type. */
Elf32_Off p_offset; /* File offset of contents. */
Elf32_Addr p_vaddr; /* Virtual address in memory image. */
Elf32_Addr p_paddr; /* Physical address (not used). */
Elf32_Word p_filesz; /* Size of contents in file. */
Elf32_Word p_memsz; /* Size of contents in memory. */
Elf32_Word p_flags; /* Access permission flags. */
Elf32_Word p_align; /* Alignment in memory and file. */
} Elf32_Phdr;
/*
* Dynamic structure. The ".dynamic" section contains an array of them.
*/
typedef struct {
Elf32_Sword d_tag; /* Entry type. */
union {
Elf32_Word d_val; /* Integer value. */
Elf32_Addr d_ptr; /* Address value. */
} d_un;
} Elf32_Dyn;
/*
* Relocation entries.
*/
/* Relocations that don't need an addend field. */
typedef struct {
Elf32_Addr r_offset; /* Location to be relocated. */
Elf32_Word r_info; /* Relocation type and symbol index. */
} Elf32_Rel;
/* Relocations that need an addend field. */
typedef struct {
Elf32_Addr r_offset; /* Location to be relocated. */
Elf32_Word r_info; /* Relocation type and symbol index. */
Elf32_Sword r_addend; /* Addend. */
} Elf32_Rela;
/* Macros for accessing the fields of r_info. */
#define ELF32_R_SYM(info) ((info) >> 8)
#define ELF32_R_TYPE(info) ((unsigned char)(info))
/* Macro for constructing r_info from field values. */
#define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type))
/*
* Note entry header
*/
typedef Elf_Note Elf32_Nhdr;
/*
* Move entry
*/
typedef struct {
Elf32_Lword m_value; /* symbol value */
Elf32_Word m_info; /* size + index */
Elf32_Word m_poffset; /* symbol offset */
Elf32_Half m_repeat; /* repeat count */
Elf32_Half m_stride; /* stride info */
} Elf32_Move;
/*
* The macros compose and decompose values for Move.r_info
*
* sym = ELF32_M_SYM(M.m_info)
* size = ELF32_M_SIZE(M.m_info)
* M.m_info = ELF32_M_INFO(sym, size)
*/
#define ELF32_M_SYM(info) ((info)>>8)
#define ELF32_M_SIZE(info) ((unsigned char)(info))
#define ELF32_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size))
/*
* Hardware/Software capabilities entry
*/
typedef struct {
Elf32_Word c_tag; /* how to interpret value */
union {
Elf32_Word c_val;
Elf32_Addr c_ptr;
} c_un;
} Elf32_Cap;
/*
* Symbol table entries.
*/
typedef struct {
Elf32_Word st_name; /* String table index of name. */
Elf32_Addr st_value; /* Symbol value. */
Elf32_Word st_size; /* Size of associated object. */
unsigned char st_info; /* Type and binding information. */
unsigned char st_other; /* Reserved (not used). */
Elf32_Half st_shndx; /* Section index of symbol. */
} Elf32_Sym;
/* Macros for accessing the fields of st_info. */
#define ELF32_ST_BIND(info) ((info) >> 4)
#define ELF32_ST_TYPE(info) ((info) & 0xf)
/* Macro for constructing st_info from field values. */
#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
/* Macro for accessing the fields of st_other. */
#define ELF32_ST_VISIBILITY(oth) ((oth) & 0x3)
/* Structures used by Sun & GNU symbol versioning. */
typedef struct
{
Elf32_Half vd_version;
Elf32_Half vd_flags;
Elf32_Half vd_ndx;
Elf32_Half vd_cnt;
Elf32_Word vd_hash;
Elf32_Word vd_aux;
Elf32_Word vd_next;
} Elf32_Verdef;
typedef struct
{
Elf32_Word vda_name;
Elf32_Word vda_next;
} Elf32_Verdaux;
typedef struct
{
Elf32_Half vn_version;
Elf32_Half vn_cnt;
Elf32_Word vn_file;
Elf32_Word vn_aux;
Elf32_Word vn_next;
} Elf32_Verneed;
typedef struct
{
Elf32_Word vna_hash;
Elf32_Half vna_flags;
Elf32_Half vna_other;
Elf32_Word vna_name;
Elf32_Word vna_next;
} Elf32_Vernaux;
typedef Elf32_Half Elf32_Versym;
typedef struct {
Elf32_Half si_boundto; /* direct bindings - symbol bound to */
Elf32_Half si_flags; /* per symbol flags */
} Elf32_Syminfo;
#endif /* !_SYS_ELF32_H_ */

1321
include/sys/elf_common.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -28,6 +28,11 @@
#include <stdint.h>
#define BASE_KERNEL
#define BASE_MBINFO 0x9800
#define BASE_MBMODS (BASE_MBINFO + sizeof(struct multiboot_info))
#define BASE_CMDLINE
int multiboot_init(char *opts[]);
uint64_t multiboot(void);

View file

@ -34,22 +34,40 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/elf32.h>
#include <xhyve/vmm/vmm_api.h>
#include <xhyve/firmware/multiboot.h>
static struct {
char *kernel;
char *cmdline;
char *modules[16];
} config;
#ifndef ALIGNUP
#define ALIGNUP(x, a) (((x - 1) & ~(a - 1)) + a)
#endif
static
int
#define MAX_MODULES 16
int nmodules; /* number of modules */
static struct mod {
char *path;
char *cmdline;
char *base;
size_t size;
off_t offset;
} memory, kernel, bss, modules[MAX_MODULES];
static int
multiboot_parse_image(const char *image, struct multiboot_header *hdr) {
FILE *f;
uint32_t *head;
size_t bytes;
int i, ret = -1;
int ret = -1;
unsigned i;
f = fopen(image, "r");
if (!f) {
@ -66,7 +84,7 @@ multiboot_parse_image(const char *image, struct multiboot_header *hdr) {
/* Search Multiboot magic */
for (i = 0; i < bytes / sizeof(*head); i++) {
if (head[i] == MULTIBOOT_HEADER_MAGIC) {
memcpy(hdr, &head[i]);
memcpy(hdr, &head[i], sizeof(*hdr));
/* Verify checksum */
ret = (hdr->magic + hdr->flags + hdr->checksum == 0) ? 0 : -3;
@ -81,11 +99,86 @@ multiboot_parse_image(const char *image, struct multiboot_header *hdr) {
return ret;
}
#if 0
static int
multiboot_parse_elf(struct mod *kernel, struct mod *bss, uint32_t *entrypoint) {
FILE *f;
struct elfhdr *ehdr;
struct elf_phdr *phdr;
int i;
ssize_t sz, pos, bytes;
loff_t offset;
f = fopen(kernel->path, "r");
if (!f)
return -1;
buf = malloc(sizeof(*ehdr));
if (buf == NULL)
return -1;
if (fread(ehdr, sizeof(*ehdr), 1, f) != sizeof(*ehdr))
return -1;
/* Check if this is an ELF file */
if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
return -1;
if (ehdr->e_phoff == 0)
return -1;
if (!elf_check_arch(ehdr))
return -1;
for (i=0; i<ehdr->e_phnum; i++) {
phdr = ehdr->e_phoff + i * ehdr->e_phentsize;
if (phdr->p_type == PT_LOAD) {
kernel->offset = phdr->p_offset;
kernel->base =
kernel->size = phdr->p_filesz;
fclose(f);
return 0;
}
}
fclose(f);
return -1; /* Missing PT_LOAD segment */
}
#endif
static int
multiboot_load(struct mod *lowmem, struct mod *module) {
FILE *f;
size_t n;
if (module->base + module->size > lowmem->size)
return -1;
f = fopen(module->path, "r");
if (!f)
return -1;
n = fread(lowmem->base + module->base, module->size, 1, f);
fclose(f);
return n == 1 ? 0 : -1;
}
int
multiboot_init(char *opts[]) {
config.kernel = opts[0];
config.cmdline = opts[1];
config.modules = &opts[2];
kernel.path = opts[0];
kernel.cmdline = opts[1];
/* TODO: parse cmdline: "kernel=kernel_cmdline,module1=module1_cmdline,..." */
for (nmodules = 0; nmodules < MAX_MODULES && opts[2 + nmodules]; nmodules++)
modules[nmodules].path = opts[2 + nmodules];
return 0;
}
@ -95,27 +188,91 @@ multiboot(void)
{
int ret, i;
char *module;
struct multiboot_header hdr;
struct multiboot_header mbhdr;
struct multiboot_info *mbinfo;
struct stat st;
uint32_t entrypoint;
uintptr_t addr;
/* Map low memory for VM */
memory.base = (uintptr_t) xh_vm_map_gpa(0, xh_vm_get_lowmem_size());
memory.size = xh_vm_get_lowmem_size();
/* Check integrity and load header of multiboot image */
ret = multiboot_parse_image(config.kernel, &hdr);
ret = multiboot_parse_image(config.kernel, &mbhdr);
if (ret)
abort();
/* Get address to load kernel */
if (mbhdr.flags & MULTIBOOT_AOUT_KLUDGE) {
kernel.base = mbhdr.load_addr;
kernel.size = mbhdr.load_end_addr - kernel.base;
bss.base = kernel.base + kernel.size;
bss.size = mbhdr.bss_end_addr - bss.base;
entrypoint = mbhdr.entry_addr;
}
/* use ELF header instead */
else {
ret = multiboot_parse_elf(&kernel, &bss, &entrypoint);
if (ret)
abort();
}
/* Load OS image (kernel) */
ret = multiboot_load_kernel(config.kernel, hdr.load_addr, hdr.load_end_addr);
ret = multiboot_load_kernel(config.kernel, kernel.addr, );
if (ret)
abort();
/* Initialize bss section */
memset(memory.base + bss.base, 0. bss.size);
mbinfo->flags = 0;
mbinfo = memory.base + BASE_MBINFO;
/* Load modules (initrd..) */
for (i = 0; i < 16 && module = config.modules[i]; i++) {
FILE *f = fopen(module, "r");
if (f) {
multiboot_load_module(module);
}
else {
fprintf(stderr, "Failed to open module: %s\n", module);
}
/* Available memory range */
mbinfo->flags |= MULTIBOOT_INFO_MEMORY;
mbinfo->mem_lower = memory.base;
mbinfo->mem_upper = memory.base + memory.size;
/* Kernel cmdline */
mbinfo->flags |= MULTIBOOT_INFO_CMDLINE;
mbinfo->cmdline = BASE_CMDLINE;
strcpy(memory.base + BASE_CMDLINE, kernel.cmdline);
/* Multiboot modules */
mbinfo->flags |= MULTIBOOT_INFO_MODS;
mbinfo->mods_count = 0;
mbinfo->mods_addr = BASE_MBMODS;
/* Load modules (initrd..) after bss section */
addr = bss.base + bss.size;
for (i = 0; i < nmodules; i++) {
struct multiboot_mod_list *mbmod = memory.base + BASE_MBMODS + i * sizeof(*mbmods);
struct mod *module = &modules[i];
ret = stat(module->path, &st);
if (ret)
abort();
module->offset = 0;
module->size = st.st_size;
module->base = mbinfo->flags & MULTIBOOT_PAGE_ALIGN
? ALIGNUP(addr, MULTIBOOT_MOD_ALIGN)
: addr;
ret = multiboot_load(module);
if (ret)
abort();
mbmod->mod_start = (multiboot_uint32_t) module->base;
mbmod->mod_end = (multiboot_uint32_t) module->base + module->size;
mbmod->cmdline = module->cmdline;
mbinfo->mods_count++;
}
/* Prepare machine state. See: Section 3.2 of Multiboot spec 0.6 */
@ -137,10 +294,10 @@ multiboot(void)
xh_vm_set_register(0, VM_REG_GUEST_RBP, 0);
xh_vm_set_register(0, VM_REG_GUEST_RDI, 0);
xh_vm_set_register(0, VM_REG_GUEST_RBX, /* MB info struct */);
xh_vm_set_register(0, VM_REG_GUEST_RSI, );
xh_vm_set_register(0, VM_REG_GUEST_RIP, );
xh_vm_set_register(0, VM_REG_GUEST_RAX, 0x2BADB002);
xh_vm_set_register(0, VM_REG_GUEST_RBX, BASE_MBINFO);
xh_vm_set_register(0, VM_REG_GUEST_RSI, 0);
xh_vm_set_register(0, VM_REG_GUEST_RIP, entrypoint);
xh_vm_set_register(0, VM_REG_GUEST_RAX, MULTIBOOT_BOOTLOADER_MAGIG);
return kernel.base;
}