Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qboot changes for PVH boot #17

Merged
merged 4 commits into from Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion benchmark.h
Expand Up @@ -9,5 +9,6 @@
#define FW_START 1
#define LINUX_START_FWCFG 2
#define LINUX_START_BOOT 3
#define LINUX_START_PVHBOOT 4

#endif
#endif /* BENCHMARK_H */
77 changes: 76 additions & 1 deletion fw_cfg.c
Expand Up @@ -6,8 +6,12 @@
#include "fw_cfg.h"
#include "bswap.h"
#include "linuxboot.h"
#include "memaccess.h"
#include "multiboot.h"
#include "benchmark.h"
#include "start_info.h"

extern struct hvm_start_info start_info;

struct fw_cfg_file {
uint32_t size;
Expand Down Expand Up @@ -184,6 +188,72 @@ static void boot_multiboot_from_fw_cfg(void)
panic();
}

static void pvh_e820_setup()
{
struct hvm_memmap_table_entry *pvh_e820p;
int i, pvh_e820_sz;

pvh_e820_sz = sizeof(struct hvm_memmap_table_entry) * e820->nr_map;

pvh_e820p = malloc(pvh_e820_sz);
memset(pvh_e820p, 0, pvh_e820_sz);

for (i = 0; i < e820->nr_map; i++) {
pvh_e820p[i].addr = e820->map[i].addr;
pvh_e820p[i].size = e820->map[i].size;
pvh_e820p[i].type = e820->map[i].type;
}
start_info.memmap_paddr = (uintptr_t)pvh_e820p;
start_info.memmap_entries = e820->nr_map;
}

static void boot_pvh_from_fw_cfg(void)
{
void *kernel_entry;
uint32_t sz;
struct linuxboot_args args;
struct hvm_modlist_entry ramdisk_mod;

start_info.magic = XEN_HVM_START_MAGIC_VALUE;
start_info.version = 1;
start_info.flags = 0;
start_info.nr_modules = 1;
start_info.reserved = 0;

fw_cfg_select(FW_CFG_CMDLINE_SIZE);
args.cmdline_size = fw_cfg_readl_le();
args.cmdline_addr = malloc(args.cmdline_size);
fw_cfg_read_entry(FW_CFG_CMDLINE_DATA, args.cmdline_addr,
args.cmdline_size);
start_info.cmdline_paddr = (uintptr_t)args.cmdline_addr;

/* Use this field for pvhboot. Not used by pvhboot otherwise */
fw_cfg_read_entry(FW_CFG_KERNEL_DATA, &ramdisk_mod,
sizeof(ramdisk_mod));
ramdisk_mod.cmdline_paddr = (uintptr_t)&ramdisk_mod;
start_info.modlist_paddr = (uintptr_t)&ramdisk_mod;

pvh_e820_setup();

fw_cfg_select(FW_CFG_KERNEL_SIZE);
sz = fw_cfg_readl_le();
if (!sz)
panic();

fw_cfg_select(FW_CFG_KERNEL_ENTRY);
kernel_entry = (void *) fw_cfg_readl_le();

#ifdef BENCHMARK_HACK
/* Exit just before jumping to vmlinux, so that it is easy
* to time/profile the firmware.
*/
outb(LINUX_EXIT_PORT, LINUX_START_PVHBOOT);
#endif
asm volatile("jmp *%2" : : "a" (0x2badb002),
"b"(&start_info), "c"(kernel_entry));
panic();
}

void boot_from_fwcfg(void)
{
struct linuxboot_args args;
Expand All @@ -208,8 +278,13 @@ void boot_from_fwcfg(void)
fw_cfg_select(FW_CFG_SETUP_DATA);
fw_cfg_read(args.header, sizeof(args.header));

if (!parse_bzimage(&args))
if (!parse_bzimage(&args)) {
uint8_t *header = args.header;

if (ldl_p(header) == 0x464c457f) /* ELF magic */
boot_pvh_from_fw_cfg();
boot_multiboot_from_fw_cfg();
}

/* SETUP_DATA already selected */
if (args.setup_size > sizeof(args.header))
Expand Down
30 changes: 30 additions & 0 deletions include/memaccess.h
@@ -0,0 +1,30 @@
#ifndef MEMACCESS_H_
#define MEMACCESS_H_

#include "string.h"

static inline uint16_t lduw_p(void *p)
{
uint16_t val;
memcpy(&val, p, 2);
return val;
}

static inline uint32_t ldl_p(void *p)
{
uint32_t val;
memcpy(&val, p, 4);
return val;
}

static inline void stw_p(void *p, uint16_t val)
{
memcpy(p, &val, 2);
}

static inline void stl_p(void *p, uint32_t val)
{
memcpy(p, &val, 4);
}

#endif /* MEMACCESS_H_ */
146 changes: 146 additions & 0 deletions include/start_info.h
@@ -0,0 +1,146 @@
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Copyright (c) 2016, Citrix Systems, Inc.
*/

#ifndef __XEN_PUBLIC_ARCH_X86_HVM_START_INFO_H__
#define __XEN_PUBLIC_ARCH_X86_HVM_START_INFO_H__

/*
* Start of day structure passed to PVH guests and to HVM guests in %ebx.
*
* NOTE: nothing will be loaded at physical address 0, so a 0 value in any
* of the address fields should be treated as not present.
*
* 0 +----------------+
* | magic | Contains the magic value XEN_HVM_START_MAGIC_VALUE
* | | ("xEn3" with the 0x80 bit of the "E" set).
* 4 +----------------+
* | version | Version of this structure. Current version is 1. New
* | | versions are guaranteed to be backwards-compatible.
* 8 +----------------+
* | flags | SIF_xxx flags.
* 12 +----------------+
* | nr_modules | Number of modules passed to the kernel.
* 16 +----------------+
* | modlist_paddr | Physical address of an array of modules
* | | (layout of the structure below).
* 24 +----------------+
* | cmdline_paddr | Physical address of the command line,
* | | a zero-terminated ASCII string.
* 32 +----------------+
* | rsdp_paddr | Physical address of the RSDP ACPI data structure.
* 40 +----------------+
* | memmap_paddr | Physical address of the (optional) memory map. Only
* | | present in version 1 and newer of the structure.
* 48 +----------------+
* | memmap_entries | Number of entries in the memory map table. Only
* | | present in version 1 and newer of the structure.
* | | Zero if there is no memory map being provided.
* 52 +----------------+
* | reserved | Version 1 and newer only.
* 56 +----------------+
*
* The layout of each entry in the module structure is the following:
*
* 0 +----------------+
* | paddr | Physical address of the module.
* 8 +----------------+
* | size | Size of the module in bytes.
* 16 +----------------+
* | cmdline_paddr | Physical address of the command line,
* | | a zero-terminated ASCII string.
* 24 +----------------+
* | reserved |
* 32 +----------------+
*
* The layout of each entry in the memory map table is as follows:
*
* 0 +----------------+
* | addr | Base address
* 8 +----------------+
* | size | Size of mapping in bytes
* 16 +----------------+
* | type | Type of mapping as defined between the hypervisor
* | | and guest it's starting. E820_TYPE_xxx, for example.
* 20 +----------------|
* | reserved |
* 24 +----------------+
*
* The address and sizes are always a 64bit little endian unsigned integer.
*
* NB: Xen on x86 will always try to place all the data below the 4GiB
* boundary.
*
* Version numbers of the hvm_start_info structure have evolved like this:
*
* Version 0:
*
* Version 1: Added the memmap_paddr/memmap_entries fields (plus 4 bytes of
* padding) to the end of the hvm_start_info struct. These new
* fields can be used to pass a memory map to the guest. The
* memory map is optional and so guests that understand version 1
* of the structure must check that memmap_entries is non-zero
* before trying to read the memory map.
*/
#define XEN_HVM_START_MAGIC_VALUE 0x336ec578

/*
* C representation of the x86/HVM start info layout.
*
* The canonical definition of this layout is above, this is just a way to
* represent the layout described there using C types.
*/
struct hvm_start_info {
uint32_t magic; /* Contains the magic value 0x336ec578 */
/* ("xEn3" with the 0x80 bit of the "E" set).*/
uint32_t version; /* Version of this structure. */
uint32_t flags; /* SIF_xxx flags. */
uint32_t nr_modules; /* Number of modules passed to the kernel. */
uint64_t modlist_paddr; /* Physical address of an array of */
/* hvm_modlist_entry. */
uint64_t cmdline_paddr; /* Physical address of the command line. */
uint64_t rsdp_paddr; /* Physical address of the RSDP ACPI data */
/* structure. */
uint64_t memmap_paddr; /* Physical address of an array of */
/* hvm_memmap_table_entry. Only present in */
/* version 1 and newer of the structure */
uint32_t memmap_entries; /* Number of entries in the memmap table. */
/* Only present in version 1 and newer of */
/* the structure. Value will be zero if */
/* there is no memory map being provided. */
uint32_t reserved;
};

struct hvm_modlist_entry {
uint64_t paddr; /* Physical address of the module. */
uint64_t size; /* Size of the module in bytes. */
uint64_t cmdline_paddr; /* Physical address of the command line. */
uint64_t reserved;
};

struct hvm_memmap_table_entry {
uint64_t addr; /* Base address of the memory region */
uint64_t size; /* Size of the memory region in bytes */
uint32_t type; /* Mapping type */
uint32_t reserved;
};

#endif /* __XEN_PUBLIC_ARCH_X86_HVM_START_INFO_H__ */
26 changes: 3 additions & 23 deletions linuxboot.c
@@ -1,33 +1,13 @@
#include "bios.h"
#include "linuxboot.h"
#include "memaccess.h"
#include "ioport.h"
#include "start_info.h"
#include "string.h"
#include "stdio.h"
#include "benchmark.h"

static inline uint16_t lduw_p(void *p)
{
uint16_t val;
memcpy(&val, p, 2);
return val;
}

static inline uint32_t ldl_p(void *p)
{
uint32_t val;
memcpy(&val, p, 4);
return val;
}

static inline void stw_p(void *p, uint16_t val)
{
memcpy(p, &val, 2);
}

static inline void stl_p(void *p, uint32_t val)
{
memcpy(p, &val, 4);
}
struct hvm_start_info start_info = {0};

bool parse_bzimage(struct linuxboot_args *args)
{
Expand Down
10 changes: 10 additions & 0 deletions tables.c
Expand Up @@ -2,6 +2,9 @@
#include "stdio.h"
#include "fw_cfg.h"
#include "string.h"
#include "start_info.h"

extern struct hvm_start_info start_info;

struct loader_cmd {
uint32_t cmd;
Expand Down Expand Up @@ -67,6 +70,13 @@ static void do_alloc(char *file, uint32_t align, uint8_t zone)

set_file_addr(id, p);
fw_cfg_read_file(id, p, n);

/* For PVH boot, save the PA where the RSDP is stored */
if (zone == ALLOC_FSEG) {
if (!memcmp(p, "RSD PTR ", 8)) {
start_info.rsdp_paddr = (uintptr_t)id_to_addr(id);
}
}
}

static void do_ptr(char *dest, char *src, uint32_t offset, uint8_t size)
Expand Down