mirror of
https://github.com/ventoy/Ventoy.git
synced 2025-08-29 00:41:15 +00:00
1. change some directory structure for the build script
2. add build script and document see DOC/BuildVentoyFromSource.txt for detail
This commit is contained in:
295
IPXE/ipxe_mod_code/ipxe-3fe683e/src/arch/x86/core/runtime.c
Normal file
295
IPXE/ipxe_mod_code/ipxe-3fe683e/src/arch/x86/core/runtime.c
Normal file
@@ -0,0 +1,295 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*
|
||||
* You can also choose to distribute this program under the terms of
|
||||
* the Unmodified Binary Distribution Licence (as given in the file
|
||||
* COPYING.UBDL), provided that you have satisfied its requirements.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Command line and initrd passed to iPXE at runtime
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <ipxe/init.h>
|
||||
#include <ipxe/image.h>
|
||||
#include <ipxe/script.h>
|
||||
#include <ipxe/umalloc.h>
|
||||
#include <realmode.h>
|
||||
#include <ventoy.h>
|
||||
|
||||
/** Command line physical address
|
||||
*
|
||||
* This can be set by the prefix.
|
||||
*/
|
||||
uint32_t __bss16 ( cmdline_phys );
|
||||
#define cmdline_phys __use_data16 ( cmdline_phys )
|
||||
|
||||
/** initrd physical address
|
||||
*
|
||||
* This can be set by the prefix.
|
||||
*/
|
||||
uint32_t __bss16 ( initrd_phys );
|
||||
#define initrd_phys __use_data16 ( initrd_phys )
|
||||
|
||||
/** initrd length
|
||||
*
|
||||
* This can be set by the prefix.
|
||||
*/
|
||||
uint32_t __bss16 ( initrd_len );
|
||||
#define initrd_len __use_data16 ( initrd_len )
|
||||
|
||||
/** Internal copy of the command line */
|
||||
static char *cmdline_copy;
|
||||
|
||||
/** Free command line image */
|
||||
static void cmdline_image_free ( struct refcnt *refcnt ) {
|
||||
struct image *image = container_of ( refcnt, struct image, refcnt );
|
||||
|
||||
DBGC ( image, "RUNTIME freeing command line\n" );
|
||||
free ( cmdline_copy );
|
||||
}
|
||||
|
||||
/** Embedded script representing the command line */
|
||||
static struct image cmdline_image = {
|
||||
.refcnt = REF_INIT ( cmdline_image_free ),
|
||||
.name = "<CMDLINE>",
|
||||
.type = &script_image_type,
|
||||
};
|
||||
|
||||
/** Colour for debug messages */
|
||||
#define colour &cmdline_image
|
||||
|
||||
/**
|
||||
* Strip unwanted cruft from command line
|
||||
*
|
||||
* @v cmdline Command line
|
||||
* @v cruft Initial substring of cruft to strip
|
||||
*/
|
||||
static void cmdline_strip ( char *cmdline, const char *cruft ) {
|
||||
char *strip;
|
||||
char *strip_end;
|
||||
|
||||
/* Find unwanted cruft, if present */
|
||||
if ( ! ( strip = strstr ( cmdline, cruft ) ) )
|
||||
return;
|
||||
|
||||
/* Strip unwanted cruft */
|
||||
strip_end = strchr ( strip, ' ' );
|
||||
if ( strip_end ) {
|
||||
*strip_end = '\0';
|
||||
DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
|
||||
strcpy ( strip, ( strip_end + 1 ) );
|
||||
} else {
|
||||
DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
|
||||
*strip = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise command line
|
||||
*
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int cmdline_init ( void ) {
|
||||
userptr_t cmdline_user;
|
||||
userptr_t chainaddr;
|
||||
char *pos1;
|
||||
char *pos2;
|
||||
int chainlen;
|
||||
char *cmdline;
|
||||
size_t len;
|
||||
int rc;
|
||||
|
||||
/* Do nothing if no command line was specified */
|
||||
if ( ! cmdline_phys ) {
|
||||
DBGC ( colour, "RUNTIME found no command line\n" );
|
||||
return 0;
|
||||
}
|
||||
cmdline_user = phys_to_user ( cmdline_phys );
|
||||
len = ( strlen_user ( cmdline_user, 0 ) + 1 /* NUL */ );
|
||||
|
||||
pos1 = strstr((char *)cmdline_user, "mem:");
|
||||
if (pos1)
|
||||
{
|
||||
pos2 = strstr(pos1, ":size:");
|
||||
if (pos2)
|
||||
{
|
||||
*pos2 = 0;
|
||||
chainaddr = phys_to_user(strtoul(pos1 + 4 + 2, NULL, 16)); // skip 0x prefix in hex number
|
||||
chainlen = (int)strtoul(pos2 + 6, NULL, 10);
|
||||
*pos2 = ':';
|
||||
|
||||
g_initrd_addr = (void *)umalloc(chainlen);
|
||||
g_initrd_len = chainlen;
|
||||
memcpy_user((userptr_t)g_initrd_addr, 0, chainaddr, 0, chainlen);
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate and copy command line */
|
||||
cmdline_copy = malloc ( len );
|
||||
if ( ! cmdline_copy ) {
|
||||
DBGC ( colour, "RUNTIME could not allocate %zd bytes for "
|
||||
"command line\n", len );
|
||||
rc = -ENOMEM;
|
||||
goto err_alloc_cmdline_copy;
|
||||
}
|
||||
|
||||
g_cmdline_copy = cmdline_copy;
|
||||
|
||||
cmdline = cmdline_copy;
|
||||
copy_from_user ( cmdline, cmdline_user, 0, len );
|
||||
DBGC ( colour, "RUNTIME found command line \"%s\" at %08x\n",
|
||||
cmdline, cmdline_phys );
|
||||
|
||||
/* Mark command line as consumed */
|
||||
cmdline_phys = 0;
|
||||
|
||||
/* Strip unwanted cruft from the command line */
|
||||
cmdline_strip ( cmdline, "BOOT_IMAGE=" );
|
||||
cmdline_strip ( cmdline, "initrd=" );
|
||||
while ( isspace ( *cmdline ) )
|
||||
cmdline++;
|
||||
DBGC ( colour, "RUNTIME using command line \"%s\"\n", cmdline );
|
||||
|
||||
/* Prepare and register image */
|
||||
cmdline_image.data = virt_to_user ( cmdline );
|
||||
cmdline_image.len = strlen ( cmdline );
|
||||
if ( cmdline_image.len ) {
|
||||
if ( ( rc = register_image ( &cmdline_image ) ) != 0 ) {
|
||||
DBGC ( colour, "RUNTIME could not register command "
|
||||
"line: %s\n", strerror ( rc ) );
|
||||
goto err_register_image;
|
||||
}
|
||||
}
|
||||
|
||||
/* Drop our reference to the image */
|
||||
image_put ( &cmdline_image );
|
||||
|
||||
return 0;
|
||||
|
||||
err_register_image:
|
||||
image_put ( &cmdline_image );
|
||||
err_alloc_cmdline_copy:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise initrd
|
||||
*
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int initrd_init ( void ) {
|
||||
struct image *image;
|
||||
int rc;
|
||||
|
||||
/* Do nothing if no initrd was specified */
|
||||
if ( ! initrd_phys ) {
|
||||
DBGC ( colour, "RUNTIME found no initrd\n" );
|
||||
return 0;
|
||||
}
|
||||
if ( ! initrd_len ) {
|
||||
DBGC ( colour, "RUNTIME found empty initrd\n" );
|
||||
return 0;
|
||||
}
|
||||
DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n",
|
||||
initrd_phys, ( initrd_phys + initrd_len ) );
|
||||
|
||||
/* Allocate image */
|
||||
image = alloc_image ( NULL );
|
||||
if ( ! image ) {
|
||||
DBGC ( colour, "RUNTIME could not allocate image for "
|
||||
"initrd\n" );
|
||||
rc = -ENOMEM;
|
||||
goto err_alloc_image;
|
||||
}
|
||||
if ( ( rc = image_set_name ( image, "<INITRD>" ) ) != 0 ) {
|
||||
DBGC ( colour, "RUNTIME could not set image name: %s\n",
|
||||
strerror ( rc ) );
|
||||
goto err_set_name;
|
||||
}
|
||||
|
||||
/* Allocate and copy initrd content */
|
||||
image->data = umalloc ( initrd_len );
|
||||
if ( ! image->data ) {
|
||||
DBGC ( colour, "RUNTIME could not allocate %d bytes for "
|
||||
"initrd\n", initrd_len );
|
||||
rc = -ENOMEM;
|
||||
goto err_umalloc;
|
||||
}
|
||||
image->len = initrd_len;
|
||||
memcpy_user ( image->data, 0, phys_to_user ( initrd_phys ), 0,
|
||||
initrd_len );
|
||||
|
||||
/* Mark initrd as consumed */
|
||||
initrd_phys = 0;
|
||||
|
||||
/* Register image */
|
||||
if ( ( rc = register_image ( image ) ) != 0 ) {
|
||||
DBGC ( colour, "RUNTIME could not register initrd: %s\n",
|
||||
strerror ( rc ) );
|
||||
goto err_register_image;
|
||||
}
|
||||
|
||||
/* Drop our reference to the image */
|
||||
image_put ( image );
|
||||
|
||||
return 0;
|
||||
|
||||
err_register_image:
|
||||
err_umalloc:
|
||||
err_set_name:
|
||||
image_put ( image );
|
||||
err_alloc_image:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise command line and initrd
|
||||
*
|
||||
*/
|
||||
static void runtime_init ( void ) {
|
||||
int rc;
|
||||
|
||||
/* Initialise command line */
|
||||
if ( ( rc = cmdline_init() ) != 0 ) {
|
||||
/* No way to report failure */
|
||||
return;
|
||||
}
|
||||
|
||||
/* Initialise initrd */
|
||||
if ( ( rc = initrd_init() ) != 0 ) {
|
||||
/* No way to report failure */
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** Command line and initrd initialisation function */
|
||||
struct startup_fn runtime_startup_fn __startup_fn ( STARTUP_NORMAL ) = {
|
||||
.name = "runtime",
|
||||
.startup = runtime_init,
|
||||
};
|
532
IPXE/ipxe_mod_code/ipxe-3fe683e/src/arch/x86/core/ventoy_vdisk.c
Normal file
532
IPXE/ipxe_mod_code/ipxe-3fe683e/src/arch/x86/core/ventoy_vdisk.c
Normal file
@@ -0,0 +1,532 @@
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <byteswap.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <ipxe/blockdev.h>
|
||||
#include <ipxe/io.h>
|
||||
#include <ipxe/acpi.h>
|
||||
#include <ipxe/sanboot.h>
|
||||
#include <ipxe/device.h>
|
||||
#include <ipxe/pci.h>
|
||||
#include <ipxe/eltorito.h>
|
||||
#include <ipxe/timer.h>
|
||||
#include <ipxe/umalloc.h>
|
||||
#include <realmode.h>
|
||||
#include <bios.h>
|
||||
#include <biosint.h>
|
||||
#include <bootsector.h>
|
||||
#include <int13.h>
|
||||
#include <ventoy.h>
|
||||
|
||||
int g_debug = 0;
|
||||
char *g_cmdline_copy;
|
||||
void *g_initrd_addr;
|
||||
size_t g_initrd_len;
|
||||
ventoy_chain_head *g_chain;
|
||||
ventoy_img_chunk *g_chunk;
|
||||
uint32_t g_img_chunk_num;
|
||||
ventoy_img_chunk *g_cur_chunk;
|
||||
uint32_t g_disk_sector_size;
|
||||
|
||||
ventoy_override_chunk *g_override_chunk;
|
||||
uint32_t g_override_chunk_num;
|
||||
|
||||
ventoy_virt_chunk *g_virt_chunk;
|
||||
uint32_t g_virt_chunk_num;
|
||||
|
||||
ventoy_sector_flag g_sector_flag[128];
|
||||
|
||||
static struct int13_disk_address __bss16 ( ventoy_address );
|
||||
#define ventoy_address __use_data16 ( ventoy_address )
|
||||
|
||||
static uint64_t ventoy_remap_lba(uint64_t lba, uint32_t *count)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t max_sectors;
|
||||
ventoy_img_chunk *cur;
|
||||
|
||||
if ((NULL == g_cur_chunk) || ((lba) < g_cur_chunk->img_start_sector) || ((lba) > g_cur_chunk->img_end_sector))
|
||||
{
|
||||
g_cur_chunk = NULL;
|
||||
for (i = 0; i < g_img_chunk_num; i++)
|
||||
{
|
||||
cur = g_chunk + i;
|
||||
if (lba >= cur->img_start_sector && lba <= cur->img_end_sector)
|
||||
{
|
||||
g_cur_chunk = cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_cur_chunk)
|
||||
{
|
||||
max_sectors = g_cur_chunk->img_end_sector - lba + 1;
|
||||
if (*count > max_sectors)
|
||||
{
|
||||
*count = max_sectors;
|
||||
}
|
||||
|
||||
if (512 == g_disk_sector_size)
|
||||
{
|
||||
return g_cur_chunk->disk_start_sector + ((lba - g_cur_chunk->img_start_sector) << 2);
|
||||
}
|
||||
return g_cur_chunk->disk_start_sector + (lba - g_cur_chunk->img_start_sector) * 2048 / g_disk_sector_size;
|
||||
}
|
||||
return lba;
|
||||
}
|
||||
|
||||
static int ventoy_vdisk_read_real(uint64_t lba, unsigned int count, unsigned long buffer)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
uint32_t left = 0;
|
||||
uint32_t readcount = 0;
|
||||
uint32_t tmpcount = 0;
|
||||
uint16_t status = 0;
|
||||
uint64_t curlba = 0;
|
||||
uint64_t maplba = 0;
|
||||
uint64_t start = 0;
|
||||
uint64_t end = 0;
|
||||
uint64_t override_start = 0;
|
||||
uint64_t override_end = 0;
|
||||
unsigned long phyaddr;
|
||||
unsigned long databuffer = buffer;
|
||||
uint8_t *override_data;
|
||||
|
||||
curlba = lba;
|
||||
left = count;
|
||||
|
||||
while (left > 0)
|
||||
{
|
||||
readcount = left;
|
||||
maplba = ventoy_remap_lba(curlba, &readcount);
|
||||
|
||||
if (g_disk_sector_size == 512)
|
||||
{
|
||||
tmpcount = (readcount << 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpcount = (readcount * 2048) / g_disk_sector_size;
|
||||
}
|
||||
|
||||
phyaddr = user_to_phys(buffer, 0);
|
||||
|
||||
while (tmpcount > 0)
|
||||
{
|
||||
/* Use INT 13, 42 to read the data from real disk */
|
||||
ventoy_address.lba = maplba;
|
||||
ventoy_address.buffer.segment = (uint16_t)(phyaddr >> 4);
|
||||
ventoy_address.buffer.offset = (uint16_t)(phyaddr & 0x0F);
|
||||
|
||||
if (tmpcount >= 64) /* max sectors per transmit */
|
||||
{
|
||||
ventoy_address.count = 64;
|
||||
tmpcount -= 64;
|
||||
maplba += 64;
|
||||
phyaddr += 32768;
|
||||
}
|
||||
else
|
||||
{
|
||||
ventoy_address.count = tmpcount;
|
||||
tmpcount = 0;
|
||||
}
|
||||
|
||||
__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
|
||||
"sti\n\t"
|
||||
"int $0x13\n\t"
|
||||
"sti\n\t" /* BIOS bugs */
|
||||
"jc 1f\n\t"
|
||||
"xorw %%ax, %%ax\n\t"
|
||||
"\n1:\n\t" )
|
||||
: "=a" ( status )
|
||||
: "a" ( 0x4200 ), "d" ( VENTOY_BIOS_FAKE_DRIVE ),
|
||||
"S" ( __from_data16 ( &ventoy_address ) ) );
|
||||
}
|
||||
|
||||
curlba += readcount;
|
||||
left -= readcount;
|
||||
buffer += (readcount * 2048);
|
||||
}
|
||||
|
||||
start = lba * 2048;
|
||||
if (start > g_chain->real_img_size_in_bytes)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
end = start + count * 2048;
|
||||
for (i = 0; i < g_override_chunk_num; i++)
|
||||
{
|
||||
override_data = g_override_chunk[i].override_data;
|
||||
override_start = g_override_chunk[i].img_offset;
|
||||
override_end = override_start + g_override_chunk[i].override_size;
|
||||
|
||||
if (end <= override_start || start >= override_end)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (start <= override_start)
|
||||
{
|
||||
if (end <= override_end)
|
||||
{
|
||||
memcpy((char *)databuffer + override_start - start, override_data, end - override_start);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy((char *)databuffer + override_start - start, override_data, override_end - override_start);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (end <= override_end)
|
||||
{
|
||||
memcpy((char *)databuffer, override_data + start - override_start, end - start);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy((char *)databuffer, override_data + start - override_start, override_end - start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ventoy_vdisk_read(struct san_device *sandev, uint64_t lba, unsigned int count, unsigned long buffer)
|
||||
{
|
||||
uint32_t i, j;
|
||||
uint64_t curlba;
|
||||
uint64_t lastlba = 0;
|
||||
uint32_t lbacount = 0;
|
||||
unsigned long lastbuffer;
|
||||
uint64_t readend;
|
||||
ventoy_virt_chunk *node;
|
||||
ventoy_sector_flag *cur_flag;
|
||||
ventoy_sector_flag *sector_flag = g_sector_flag;
|
||||
struct i386_all_regs *ix86;
|
||||
|
||||
if (INT13_EXTENDED_READ != sandev->int13_command)
|
||||
{
|
||||
DBGC(sandev, "invalid cmd %u\n", sandev->int13_command);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ix86 = (struct i386_all_regs *)sandev->x86_regptr;
|
||||
|
||||
readend = (lba + count) * 2048;
|
||||
if (readend < g_chain->real_img_size_in_bytes)
|
||||
{
|
||||
ventoy_vdisk_read_real(lba, count, buffer);
|
||||
ix86->regs.dl = sandev->drive;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count > sizeof(g_sector_flag))
|
||||
{
|
||||
sector_flag = (ventoy_sector_flag *)malloc(count * sizeof(ventoy_sector_flag));
|
||||
}
|
||||
|
||||
for (curlba = lba, cur_flag = sector_flag, j = 0; j < count; j++, curlba++, cur_flag++)
|
||||
{
|
||||
cur_flag->flag = 0;
|
||||
for (node = g_virt_chunk, i = 0; i < g_virt_chunk_num; i++, node++)
|
||||
{
|
||||
if (curlba >= node->mem_sector_start && curlba < node->mem_sector_end)
|
||||
{
|
||||
memcpy((void *)(buffer + j * 2048),
|
||||
(char *)g_virt_chunk + node->mem_sector_offset + (curlba - node->mem_sector_start) * 2048,
|
||||
2048);
|
||||
cur_flag->flag = 1;
|
||||
break;
|
||||
}
|
||||
else if (curlba >= node->remap_sector_start && curlba < node->remap_sector_end)
|
||||
{
|
||||
cur_flag->remap_lba = node->org_sector_start + curlba - node->remap_sector_start;
|
||||
cur_flag->flag = 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (curlba = lba, cur_flag = sector_flag, j = 0; j < count; j++, curlba++, cur_flag++)
|
||||
{
|
||||
if (cur_flag->flag == 2)
|
||||
{
|
||||
if (lastlba == 0)
|
||||
{
|
||||
lastbuffer = buffer + j * 2048;
|
||||
lastlba = cur_flag->remap_lba;
|
||||
lbacount = 1;
|
||||
}
|
||||
else if (lastlba + lbacount == cur_flag->remap_lba)
|
||||
{
|
||||
lbacount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ventoy_vdisk_read_real(lastlba, lbacount, lastbuffer);
|
||||
lastbuffer = buffer + j * 2048;
|
||||
lastlba = cur_flag->remap_lba;
|
||||
lbacount = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lbacount > 0)
|
||||
{
|
||||
ventoy_vdisk_read_real(lastlba, lbacount, lastbuffer);
|
||||
}
|
||||
|
||||
if (sector_flag != g_sector_flag)
|
||||
{
|
||||
free(sector_flag);
|
||||
}
|
||||
|
||||
ix86->regs.dl = sandev->drive;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ventoy_dump_img_chunk(ventoy_chain_head *chain)
|
||||
{
|
||||
uint32_t i;
|
||||
ventoy_img_chunk *chunk;
|
||||
|
||||
chunk = (ventoy_img_chunk *)((char *)chain + chain->img_chunk_offset);
|
||||
|
||||
printf("##################### ventoy_dump_img_chunk #######################\n");
|
||||
|
||||
for (i = 0; i < chain->img_chunk_num; i++)
|
||||
{
|
||||
printf("%2u: [ %u - %u ] <==> [ %llu - %llu ]\n",
|
||||
i, chunk[i].img_start_sector, chunk[i].img_end_sector,
|
||||
chunk[i].disk_start_sector, chunk[i].disk_end_sector);
|
||||
}
|
||||
|
||||
ventoy_debug_pause();
|
||||
}
|
||||
|
||||
static void ventoy_dump_override_chunk(ventoy_chain_head *chain)
|
||||
{
|
||||
uint32_t i;
|
||||
ventoy_override_chunk *chunk;
|
||||
|
||||
chunk = (ventoy_override_chunk *)((char *)chain + chain->override_chunk_offset);
|
||||
|
||||
printf("##################### ventoy_dump_override_chunk #######################\n");
|
||||
|
||||
for (i = 0; i < g_override_chunk_num; i++)
|
||||
{
|
||||
printf("%2u: [ %llu, %u ]\n", i, chunk[i].img_offset, chunk[i].override_size);
|
||||
}
|
||||
|
||||
ventoy_debug_pause();
|
||||
}
|
||||
|
||||
static void ventoy_dump_virt_chunk(ventoy_chain_head *chain)
|
||||
{
|
||||
uint32_t i;
|
||||
ventoy_virt_chunk *node;
|
||||
|
||||
printf("##################### ventoy_dump_virt_chunk #######################\n");
|
||||
printf("virt_chunk_offset=%u\n", chain->virt_chunk_offset);
|
||||
printf("virt_chunk_num=%u\n", chain->virt_chunk_num);
|
||||
|
||||
node = (ventoy_virt_chunk *)((char *)chain + chain->virt_chunk_offset);
|
||||
for (i = 0; i < chain->virt_chunk_num; i++, node++)
|
||||
{
|
||||
printf("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]\n", i,
|
||||
node->mem_sector_start,
|
||||
node->mem_sector_end,
|
||||
node->mem_sector_offset,
|
||||
node->remap_sector_start,
|
||||
node->remap_sector_end,
|
||||
node->org_sector_start);
|
||||
}
|
||||
|
||||
ventoy_debug_pause();
|
||||
}
|
||||
|
||||
static void ventoy_dump_chain(ventoy_chain_head *chain)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
uint8_t chksum = 0;
|
||||
uint8_t *guid;
|
||||
uint8_t *vtoy_reserve;
|
||||
|
||||
guid = chain->os_param.vtoy_disk_guid;
|
||||
for (i = 0; i < sizeof(ventoy_os_param); i++)
|
||||
{
|
||||
chksum += *((uint8_t *)(&(chain->os_param)) + i);
|
||||
}
|
||||
|
||||
vtoy_reserve = (uint8_t *)(chain->os_param.vtoy_reserved);
|
||||
|
||||
printf("##################### ventoy_dump_chain #######################\n");
|
||||
|
||||
printf("os_param will be save at %p\n", ventoy_get_runtime_addr());
|
||||
|
||||
printf("os_param->chksum=0x%x (%s)\n", chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
|
||||
printf("os_param->vtoy_disk_guid=%02x%02x%02x%02x\n", guid[0], guid[1], guid[2], guid[3]);
|
||||
printf("os_param->vtoy_disk_size=%llu\n", chain->os_param.vtoy_disk_size);
|
||||
printf("os_param->vtoy_disk_part_id=%u\n", chain->os_param.vtoy_disk_part_id);
|
||||
printf("os_param->vtoy_disk_part_type=%u\n", chain->os_param.vtoy_disk_part_type);
|
||||
printf("os_param->vtoy_img_path=<%s>\n", chain->os_param.vtoy_img_path);
|
||||
printf("os_param->vtoy_img_size=<%llu>\n", chain->os_param.vtoy_img_size);
|
||||
printf("os_param->vtoy_reserve[0]=<%u>\n", vtoy_reserve[0]);
|
||||
printf("os_param->vtoy_reserve[1]=<%u>\n", vtoy_reserve[1]);
|
||||
printf("os_param->vtoy_img_location_addr=<0x%llx>\n", chain->os_param.vtoy_img_location_addr);
|
||||
printf("os_param->vtoy_img_location_len=<%u>\n", chain->os_param.vtoy_img_location_len);
|
||||
ventoy_debug_pause();
|
||||
|
||||
printf("chain->disk_drive=0x%x\n", chain->disk_drive);
|
||||
printf("chain->drive_map=0x%x\n", chain->drive_map);
|
||||
printf("chain->disk_sector_size=%u\n", chain->disk_sector_size);
|
||||
printf("chain->real_img_size_in_bytes=%llu\n", chain->real_img_size_in_bytes);
|
||||
printf("chain->virt_img_size_in_bytes=%llu\n", chain->virt_img_size_in_bytes);
|
||||
printf("chain->boot_catalog=%u\n", chain->boot_catalog);
|
||||
printf("chain->img_chunk_offset=%u\n", chain->img_chunk_offset);
|
||||
printf("chain->img_chunk_num=%u\n", chain->img_chunk_num);
|
||||
printf("chain->override_chunk_offset=%u\n", chain->override_chunk_offset);
|
||||
printf("chain->override_chunk_num=%u\n", chain->override_chunk_num);
|
||||
printf("chain->virt_chunk_offset=%u\n", chain->virt_chunk_offset);
|
||||
printf("chain->virt_chunk_num=%u\n", chain->virt_chunk_num);
|
||||
ventoy_debug_pause();
|
||||
|
||||
ventoy_dump_img_chunk(chain);
|
||||
ventoy_dump_override_chunk(chain);
|
||||
ventoy_dump_virt_chunk(chain);
|
||||
}
|
||||
|
||||
static int ventoy_update_image_location(ventoy_os_param *param)
|
||||
{
|
||||
uint8_t chksum = 0;
|
||||
unsigned int i;
|
||||
unsigned int length;
|
||||
userptr_t address = 0;
|
||||
ventoy_image_location *location = NULL;
|
||||
ventoy_image_disk_region *region = NULL;
|
||||
ventoy_img_chunk *chunk = g_chunk;
|
||||
|
||||
length = sizeof(ventoy_image_location) + (g_img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
|
||||
|
||||
address = umalloc(length + 4096 * 2);
|
||||
if (!address)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (address % 4096)
|
||||
{
|
||||
address += 4096 - (address % 4096);
|
||||
}
|
||||
|
||||
param->chksum = 0;
|
||||
param->vtoy_img_location_addr = user_to_phys(address, 0);
|
||||
param->vtoy_img_location_len = length;
|
||||
|
||||
/* update check sum */
|
||||
for (i = 0; i < sizeof(ventoy_os_param); i++)
|
||||
{
|
||||
chksum += *((uint8_t *)param + i);
|
||||
}
|
||||
param->chksum = (chksum == 0) ? 0 : (uint8_t)(0x100 - chksum);
|
||||
|
||||
location = (ventoy_image_location *)(unsigned long)(address);
|
||||
if (NULL == location)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(&location->guid, ¶m->guid, sizeof(ventoy_guid));
|
||||
location->image_sector_size = 2048;
|
||||
location->disk_sector_size = g_chain->disk_sector_size;
|
||||
location->region_count = g_img_chunk_num;
|
||||
|
||||
region = location->regions;
|
||||
|
||||
for (i = 0; i < g_img_chunk_num; i++)
|
||||
{
|
||||
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
|
||||
region->image_start_sector = chunk->img_start_sector;
|
||||
region->disk_start_sector = chunk->disk_start_sector;
|
||||
region++;
|
||||
chunk++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ventoy_boot_vdisk(void *data)
|
||||
{
|
||||
uint8_t chksum = 0;
|
||||
unsigned int i;
|
||||
unsigned int drive;
|
||||
|
||||
(void)data;
|
||||
|
||||
ventoy_address.bufsize = offsetof ( typeof ( ventoy_address ), buffer_phys );
|
||||
|
||||
if (strstr(g_cmdline_copy, "debug"))
|
||||
{
|
||||
g_debug = 1;
|
||||
printf("### ventoy chain boot begin... ###\n");
|
||||
ventoy_debug_pause();
|
||||
}
|
||||
|
||||
g_chain = (ventoy_chain_head *)g_initrd_addr;
|
||||
g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
|
||||
g_img_chunk_num = g_chain->img_chunk_num;
|
||||
g_disk_sector_size = g_chain->disk_sector_size;
|
||||
g_cur_chunk = g_chunk;
|
||||
|
||||
g_override_chunk = (ventoy_override_chunk *)((char *)g_chain + g_chain->override_chunk_offset);
|
||||
g_override_chunk_num = g_chain->override_chunk_num;
|
||||
|
||||
g_virt_chunk = (ventoy_virt_chunk *)((char *)g_chain + g_chain->virt_chunk_offset);
|
||||
g_virt_chunk_num = g_chain->virt_chunk_num;
|
||||
|
||||
if (g_debug)
|
||||
{
|
||||
for (i = 0; i < sizeof(ventoy_os_param); i++)
|
||||
{
|
||||
chksum += *((uint8_t *)(&(g_chain->os_param)) + i);
|
||||
}
|
||||
printf("os param checksum: 0x%x %s\n", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
|
||||
}
|
||||
|
||||
ventoy_update_image_location(&(g_chain->os_param));
|
||||
|
||||
if (g_debug)
|
||||
{
|
||||
ventoy_dump_chain(g_chain);
|
||||
}
|
||||
|
||||
drive = ventoy_int13_hook(g_chain);
|
||||
|
||||
if (g_debug)
|
||||
{
|
||||
printf("### ventoy chain boot before boot image ... ###\n");
|
||||
ventoy_debug_pause();
|
||||
}
|
||||
|
||||
ventoy_int13_boot(drive, &(g_chain->os_param), g_cmdline_copy);
|
||||
|
||||
if (g_debug)
|
||||
{
|
||||
printf("!!!!!!!!!! ventoy boot failed !!!!!!!!!!\n");
|
||||
ventoy_debug_pause();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -0,0 +1,820 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*
|
||||
* You can also choose to distribute this program under the terms of
|
||||
* the Unmodified Binary Distribution Licence (as given in the file
|
||||
* COPYING.UBDL), provided that you have satisfied its requirements.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Hyper-V driver
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <byteswap.h>
|
||||
#include <pic8259.h>
|
||||
#include <ipxe/malloc.h>
|
||||
#include <ipxe/device.h>
|
||||
#include <ipxe/timer.h>
|
||||
#include <ipxe/quiesce.h>
|
||||
#include <ipxe/cpuid.h>
|
||||
#include <ipxe/msr.h>
|
||||
#include <ipxe/hyperv.h>
|
||||
#include <ipxe/vmbus.h>
|
||||
#include "hyperv.h"
|
||||
|
||||
/** Maximum time to wait for a message response
|
||||
*
|
||||
* This is a policy decision.
|
||||
*/
|
||||
#define HV_MESSAGE_MAX_WAIT_MS 1000
|
||||
|
||||
/** Hyper-V timer frequency (fixed 10Mhz) */
|
||||
#define HV_TIMER_HZ 10000000
|
||||
|
||||
/** Hyper-V timer scale factor (used to avoid 64-bit division) */
|
||||
#define HV_TIMER_SHIFT 18
|
||||
|
||||
/**
|
||||
* Convert a Hyper-V status code to an iPXE status code
|
||||
*
|
||||
* @v status Hyper-V status code
|
||||
* @ret rc iPXE status code (before negation)
|
||||
*/
|
||||
#define EHV( status ) EPLATFORM ( EINFO_EPLATFORM, (status) )
|
||||
|
||||
/**
|
||||
* Allocate zeroed pages
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v ... Page addresses to fill in, terminated by NULL
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
__attribute__ (( sentinel )) int
|
||||
hv_alloc_pages ( struct hv_hypervisor *hv, ... ) {
|
||||
va_list args;
|
||||
void **page;
|
||||
int i;
|
||||
|
||||
/* Allocate and zero pages */
|
||||
va_start ( args, hv );
|
||||
for ( i = 0 ; ( ( page = va_arg ( args, void ** ) ) != NULL ); i++ ) {
|
||||
*page = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
|
||||
if ( ! *page )
|
||||
goto err_alloc;
|
||||
memset ( *page, 0, PAGE_SIZE );
|
||||
}
|
||||
va_end ( args );
|
||||
|
||||
return 0;
|
||||
|
||||
err_alloc:
|
||||
va_end ( args );
|
||||
va_start ( args, hv );
|
||||
for ( ; i >= 0 ; i-- ) {
|
||||
page = va_arg ( args, void ** );
|
||||
free_dma ( *page, PAGE_SIZE );
|
||||
}
|
||||
va_end ( args );
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free pages
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v ... Page addresses, terminated by NULL
|
||||
*/
|
||||
__attribute__ (( sentinel )) void
|
||||
hv_free_pages ( struct hv_hypervisor *hv, ... ) {
|
||||
va_list args;
|
||||
void *page;
|
||||
|
||||
va_start ( args, hv );
|
||||
while ( ( page = va_arg ( args, void * ) ) != NULL )
|
||||
free_dma ( page, PAGE_SIZE );
|
||||
va_end ( args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate message buffer
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hv_alloc_message ( struct hv_hypervisor *hv ) {
|
||||
|
||||
/* Allocate buffer. Must be aligned to at least 8 bytes and
|
||||
* must not cross a page boundary, so align on its own size.
|
||||
*/
|
||||
hv->message = malloc_dma ( sizeof ( *hv->message ),
|
||||
sizeof ( *hv->message ) );
|
||||
if ( ! hv->message )
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free message buffer
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
*/
|
||||
static void hv_free_message ( struct hv_hypervisor *hv ) {
|
||||
|
||||
/* Free buffer */
|
||||
free_dma ( hv->message, sizeof ( *hv->message ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether or not we are running in Hyper-V
|
||||
*
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hv_check_hv ( void ) {
|
||||
struct x86_features features;
|
||||
uint32_t interface_id;
|
||||
uint32_t discard_ebx;
|
||||
uint32_t discard_ecx;
|
||||
uint32_t discard_edx;
|
||||
|
||||
/* Check for presence of a hypervisor (not necessarily Hyper-V) */
|
||||
x86_features ( &features );
|
||||
if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_HYPERVISOR ) ) {
|
||||
DBGC ( HV_INTERFACE_ID, "HV not running in a hypervisor\n" );
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Check that hypervisor is Hyper-V */
|
||||
cpuid ( HV_CPUID_INTERFACE_ID, 0, &interface_id, &discard_ebx,
|
||||
&discard_ecx, &discard_edx );
|
||||
if ( interface_id != HV_INTERFACE_ID ) {
|
||||
DBGC ( HV_INTERFACE_ID, "HV not running in Hyper-V (interface "
|
||||
"ID %#08x)\n", interface_id );
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check required features
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hv_check_features ( struct hv_hypervisor *hv ) {
|
||||
uint32_t available;
|
||||
uint32_t permissions;
|
||||
uint32_t discard_ecx;
|
||||
uint32_t discard_edx;
|
||||
|
||||
/* Check that required features and privileges are available */
|
||||
cpuid ( HV_CPUID_FEATURES, 0, &available, &permissions, &discard_ecx,
|
||||
&discard_edx );
|
||||
if ( ! ( available & HV_FEATURES_AVAIL_HYPERCALL_MSR ) ) {
|
||||
DBGC ( hv, "HV %p has no hypercall MSRs (features %08x:%08x)\n",
|
||||
hv, available, permissions );
|
||||
return -ENODEV;
|
||||
}
|
||||
if ( ! ( available & HV_FEATURES_AVAIL_SYNIC_MSR ) ) {
|
||||
DBGC ( hv, "HV %p has no SynIC MSRs (features %08x:%08x)\n",
|
||||
hv, available, permissions );
|
||||
return -ENODEV;
|
||||
}
|
||||
if ( ! ( permissions & HV_FEATURES_PERM_POST_MESSAGES ) ) {
|
||||
DBGC ( hv, "HV %p cannot post messages (features %08x:%08x)\n",
|
||||
hv, available, permissions );
|
||||
return -EACCES;
|
||||
}
|
||||
if ( ! ( permissions & HV_FEATURES_PERM_SIGNAL_EVENTS ) ) {
|
||||
DBGC ( hv, "HV %p cannot signal events (features %08x:%08x)",
|
||||
hv, available, permissions );
|
||||
return -EACCES;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that Gen 2 UEFI firmware is not running
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @ret rc Return status code
|
||||
*
|
||||
* We must not steal ownership from the Gen 2 UEFI firmware, since
|
||||
* doing so will cause an immediate crash. Avoid this by checking for
|
||||
* the guest OS identity known to be used by the Gen 2 UEFI firmware.
|
||||
*/
|
||||
static int hv_check_uefi ( struct hv_hypervisor *hv ) {
|
||||
uint64_t guest_os_id;
|
||||
|
||||
/* Check for UEFI firmware's guest OS identity */
|
||||
guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
|
||||
if ( guest_os_id == HV_GUEST_OS_ID_UEFI ) {
|
||||
DBGC ( hv, "HV %p is owned by UEFI firmware\n", hv );
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map hypercall page
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
*/
|
||||
static void hv_map_hypercall ( struct hv_hypervisor *hv ) {
|
||||
union {
|
||||
struct {
|
||||
uint32_t ebx;
|
||||
uint32_t ecx;
|
||||
uint32_t edx;
|
||||
} __attribute__ (( packed ));
|
||||
char text[ 13 /* "bbbbccccdddd" + NUL */ ];
|
||||
} vendor_id;
|
||||
uint32_t build;
|
||||
uint32_t version;
|
||||
uint32_t discard_eax;
|
||||
uint32_t discard_ecx;
|
||||
uint32_t discard_edx;
|
||||
uint64_t guest_os_id;
|
||||
uint64_t hypercall;
|
||||
|
||||
/* Report guest OS identity */
|
||||
guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
|
||||
if ( guest_os_id != 0 ) {
|
||||
DBGC ( hv, "HV %p guest OS ID MSR was %#08llx\n",
|
||||
hv, guest_os_id );
|
||||
}
|
||||
guest_os_id = HV_GUEST_OS_ID_IPXE;
|
||||
DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
|
||||
wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
|
||||
|
||||
/* Get hypervisor system identity (for debugging) */
|
||||
cpuid ( HV_CPUID_VENDOR_ID, 0, &discard_eax, &vendor_id.ebx,
|
||||
&vendor_id.ecx, &vendor_id.edx );
|
||||
vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
|
||||
cpuid ( HV_CPUID_HYPERVISOR_ID, 0, &build, &version, &discard_ecx,
|
||||
&discard_edx );
|
||||
DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
|
||||
vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
|
||||
|
||||
/* Map hypercall page */
|
||||
hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
|
||||
hypercall &= ( PAGE_SIZE - 1 );
|
||||
hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
|
||||
DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
|
||||
wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap hypercall page
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
*/
|
||||
static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
|
||||
uint64_t hypercall;
|
||||
uint64_t guest_os_id;
|
||||
|
||||
/* Unmap the hypercall page */
|
||||
hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
|
||||
hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
|
||||
DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
|
||||
wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
|
||||
|
||||
/* Reset the guest OS identity */
|
||||
guest_os_id = 0;
|
||||
DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
|
||||
wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Map synthetic interrupt controller
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
*/
|
||||
static void hv_map_synic ( struct hv_hypervisor *hv ) {
|
||||
uint64_t simp;
|
||||
uint64_t siefp;
|
||||
uint64_t scontrol;
|
||||
|
||||
/* Zero SynIC message and event pages */
|
||||
memset ( hv->synic.message, 0, PAGE_SIZE );
|
||||
memset ( hv->synic.event, 0, PAGE_SIZE );
|
||||
|
||||
/* Map SynIC message page */
|
||||
simp = rdmsr ( HV_X64_MSR_SIMP );
|
||||
simp &= ( PAGE_SIZE - 1 );
|
||||
simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
|
||||
DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
|
||||
wrmsr ( HV_X64_MSR_SIMP, simp );
|
||||
|
||||
/* Map SynIC event page */
|
||||
siefp = rdmsr ( HV_X64_MSR_SIEFP );
|
||||
siefp &= ( PAGE_SIZE - 1 );
|
||||
siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
|
||||
DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
|
||||
wrmsr ( HV_X64_MSR_SIEFP, siefp );
|
||||
|
||||
/* Enable SynIC */
|
||||
scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
|
||||
scontrol |= HV_SCONTROL_ENABLE;
|
||||
DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
|
||||
wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap synthetic interrupt controller, leaving SCONTROL untouched
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
*/
|
||||
static void hv_unmap_synic_no_scontrol ( struct hv_hypervisor *hv ) {
|
||||
uint64_t siefp;
|
||||
uint64_t simp;
|
||||
|
||||
/* Unmap SynIC event page */
|
||||
siefp = rdmsr ( HV_X64_MSR_SIEFP );
|
||||
siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
|
||||
DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
|
||||
wrmsr ( HV_X64_MSR_SIEFP, siefp );
|
||||
|
||||
/* Unmap SynIC message page */
|
||||
simp = rdmsr ( HV_X64_MSR_SIMP );
|
||||
simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
|
||||
DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
|
||||
wrmsr ( HV_X64_MSR_SIMP, simp );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap synthetic interrupt controller
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
*/
|
||||
static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
|
||||
uint64_t scontrol;
|
||||
|
||||
/* Disable SynIC */
|
||||
scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
|
||||
scontrol &= ~HV_SCONTROL_ENABLE;
|
||||
DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
|
||||
wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
|
||||
|
||||
/* Unmap SynIC event and message pages */
|
||||
hv_unmap_synic_no_scontrol ( hv );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable synthetic interrupt
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v sintx Synthetic interrupt number
|
||||
*/
|
||||
void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
|
||||
unsigned long msr = HV_X64_MSR_SINT ( sintx );
|
||||
uint64_t sint;
|
||||
|
||||
/* Enable synthetic interrupt
|
||||
*
|
||||
* We have to enable the interrupt, otherwise messages will
|
||||
* not be delivered (even though the documentation implies
|
||||
* that polling for messages is possible). We enable AutoEOI
|
||||
* and hook the interrupt to the obsolete IRQ13 (FPU
|
||||
* exception) vector, which will be implemented as a no-op.
|
||||
*/
|
||||
sint = rdmsr ( msr );
|
||||
sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
|
||||
sint |= ( HV_SINT_AUTO_EOI |
|
||||
HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
|
||||
DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
|
||||
wrmsr ( msr, sint );
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable synthetic interrupt
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v sintx Synthetic interrupt number
|
||||
*/
|
||||
void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
|
||||
unsigned long msr = HV_X64_MSR_SINT ( sintx );
|
||||
uint64_t sint;
|
||||
|
||||
/* Do nothing if interrupt is already disabled */
|
||||
sint = rdmsr ( msr );
|
||||
if ( sint & HV_SINT_MASKED )
|
||||
return;
|
||||
|
||||
/* Disable synthetic interrupt */
|
||||
sint &= ~HV_SINT_AUTO_EOI;
|
||||
sint |= HV_SINT_MASKED;
|
||||
DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
|
||||
wrmsr ( msr, sint );
|
||||
}
|
||||
|
||||
/**
|
||||
* Post message
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v id Connection ID
|
||||
* @v type Message type
|
||||
* @v data Message
|
||||
* @v len Length of message
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
|
||||
unsigned int type, const void *data, size_t len ) {
|
||||
struct hv_post_message *msg = &hv->message->posted;
|
||||
int status;
|
||||
int rc;
|
||||
|
||||
/* Sanity check */
|
||||
assert ( len <= sizeof ( msg->data ) );
|
||||
|
||||
/* Construct message */
|
||||
memset ( msg, 0, sizeof ( *msg ) );
|
||||
msg->id = cpu_to_le32 ( id );
|
||||
msg->type = cpu_to_le32 ( type );
|
||||
msg->len = cpu_to_le32 ( len );
|
||||
memcpy ( msg->data, data, len );
|
||||
DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
|
||||
hv, id, type );
|
||||
DBGC2_HDA ( hv, 0, msg->data, len );
|
||||
|
||||
/* Post message */
|
||||
if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
|
||||
rc = -EHV ( status );
|
||||
DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
|
||||
hv, id, strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for received message
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v sintx Synthetic interrupt number
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
|
||||
struct hv_message *msg = &hv->message->received;
|
||||
struct hv_message *src = &hv->synic.message[sintx];
|
||||
unsigned int retries;
|
||||
size_t len;
|
||||
|
||||
/* Wait for message to arrive */
|
||||
for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
|
||||
|
||||
/* Check for message */
|
||||
if ( src->type ) {
|
||||
|
||||
/* Copy message */
|
||||
memset ( msg, 0, sizeof ( *msg ) );
|
||||
len = src->len;
|
||||
assert ( len <= sizeof ( *msg ) );
|
||||
memcpy ( msg, src,
|
||||
( offsetof ( typeof ( *msg ), data ) + len ) );
|
||||
DBGC2 ( hv, "HV %p SINT%d received message type "
|
||||
"%#08x:\n", hv, sintx,
|
||||
le32_to_cpu ( msg->type ) );
|
||||
DBGC2_HDA ( hv, 0, msg->data, len );
|
||||
|
||||
/* Consume message */
|
||||
src->type = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Trigger message delivery */
|
||||
wrmsr ( HV_X64_MSR_EOM, 0 );
|
||||
|
||||
/* Delay */
|
||||
mdelay ( 1 );
|
||||
}
|
||||
|
||||
DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
|
||||
hv, sintx );
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signal event
|
||||
*
|
||||
* @v hv Hyper-V hypervisor
|
||||
* @v id Connection ID
|
||||
* @v flag Flag number
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
|
||||
unsigned int flag ) {
|
||||
struct hv_signal_event *event = &hv->message->signalled;
|
||||
int status;
|
||||
int rc;
|
||||
|
||||
/* Construct event */
|
||||
memset ( event, 0, sizeof ( *event ) );
|
||||
event->id = cpu_to_le32 ( id );
|
||||
event->flag = cpu_to_le16 ( flag );
|
||||
|
||||
/* Signal event */
|
||||
if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
|
||||
rc = -EHV ( status );
|
||||
DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
|
||||
hv, id, strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Probe root device
|
||||
*
|
||||
* @v rootdev Root device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hv_probe ( struct root_device *rootdev ) {
|
||||
struct hv_hypervisor *hv;
|
||||
int rc;
|
||||
|
||||
/* Check we are running in Hyper-V */
|
||||
if ( ( rc = hv_check_hv() ) != 0 )
|
||||
goto err_check_hv;
|
||||
|
||||
/* Allocate and initialise structure */
|
||||
hv = zalloc ( sizeof ( *hv ) );
|
||||
if ( ! hv ) {
|
||||
rc = -ENOMEM;
|
||||
goto err_alloc;
|
||||
}
|
||||
|
||||
/* Check features */
|
||||
if ( ( rc = hv_check_features ( hv ) ) != 0 )
|
||||
goto err_check_features;
|
||||
|
||||
/* Check that Gen 2 UEFI firmware is not running */
|
||||
if ( ( rc = hv_check_uefi ( hv ) ) != 0 )
|
||||
goto err_check_uefi;
|
||||
|
||||
/* Allocate pages */
|
||||
if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
|
||||
&hv->synic.event, NULL ) ) != 0 )
|
||||
goto err_alloc_pages;
|
||||
|
||||
/* Allocate message buffer */
|
||||
if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
|
||||
goto err_alloc_message;
|
||||
|
||||
/* Map hypercall page */
|
||||
hv_map_hypercall ( hv );
|
||||
|
||||
/* Map synthetic interrupt controller */
|
||||
hv_map_synic ( hv );
|
||||
|
||||
/* Probe Hyper-V devices */
|
||||
if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
|
||||
goto err_vmbus_probe;
|
||||
|
||||
rootdev_set_drvdata ( rootdev, hv );
|
||||
return 0;
|
||||
|
||||
vmbus_remove ( hv, &rootdev->dev );
|
||||
err_vmbus_probe:
|
||||
hv_unmap_synic ( hv );
|
||||
hv_unmap_hypercall ( hv );
|
||||
hv_free_message ( hv );
|
||||
err_alloc_message:
|
||||
hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
|
||||
NULL );
|
||||
err_alloc_pages:
|
||||
err_check_uefi:
|
||||
err_check_features:
|
||||
free ( hv );
|
||||
err_alloc:
|
||||
err_check_hv:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove root device
|
||||
*
|
||||
* @v rootdev Root device
|
||||
*/
|
||||
static void hv_remove ( struct root_device *rootdev ) {
|
||||
struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
|
||||
|
||||
vmbus_remove ( hv, &rootdev->dev );
|
||||
hv_unmap_synic ( hv );
|
||||
hv_unmap_hypercall ( hv );
|
||||
hv_free_message ( hv );
|
||||
hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
|
||||
NULL );
|
||||
free ( hv );
|
||||
rootdev_set_drvdata ( rootdev, NULL );
|
||||
}
|
||||
|
||||
/** Hyper-V root device driver */
|
||||
static struct root_driver hv_root_driver = {
|
||||
.probe = hv_probe,
|
||||
.remove = hv_remove,
|
||||
};
|
||||
|
||||
/** Hyper-V root device */
|
||||
struct root_device hv_root_device __root_device = {
|
||||
.dev = { .name = "Hyper-V" },
|
||||
.driver = &hv_root_driver,
|
||||
};
|
||||
|
||||
/**
|
||||
* Quiesce system
|
||||
*
|
||||
*/
|
||||
static void hv_quiesce ( void ) {
|
||||
struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
|
||||
unsigned int i;
|
||||
|
||||
/* Do nothing if we are not running in Hyper-V */
|
||||
if ( ! hv )
|
||||
return;
|
||||
|
||||
/* The "enlightened" portions of the Windows Server 2016 boot
|
||||
* process will not cleanly take ownership of an active
|
||||
* Hyper-V connection. Experimentation shows that the minimum
|
||||
* requirement is that we disable the SynIC message page
|
||||
* (i.e. zero the SIMP MSR).
|
||||
*
|
||||
* We cannot perform a full shutdown of the Hyper-V
|
||||
* connection. Experimentation shows that if we disable the
|
||||
* SynIC (i.e. zero the SCONTROL MSR) then Windows Server 2016
|
||||
* will enter an indefinite wait loop.
|
||||
*
|
||||
* Attempt to create a safe handover environment by resetting
|
||||
* all MSRs except for SCONTROL.
|
||||
*
|
||||
* Note that we do not shut down our VMBus devices, since we
|
||||
* may need to unquiesce the system and continue operation.
|
||||
*/
|
||||
|
||||
/* Disable all synthetic interrupts */
|
||||
for ( i = 0 ; i <= HV_SINT_MAX ; i++ )
|
||||
hv_disable_sint ( hv, i );
|
||||
|
||||
/* Unmap synthetic interrupt controller, leaving SCONTROL
|
||||
* enabled (see above).
|
||||
*/
|
||||
hv_unmap_synic_no_scontrol ( hv );
|
||||
|
||||
/* Unmap hypercall page */
|
||||
hv_unmap_hypercall ( hv );
|
||||
|
||||
DBGC ( hv, "HV %p quiesced\n", hv );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unquiesce system
|
||||
*
|
||||
*/
|
||||
static void hv_unquiesce ( void ) {
|
||||
struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
|
||||
uint64_t simp;
|
||||
int rc;
|
||||
|
||||
/* Do nothing if we are not running in Hyper-V */
|
||||
if ( ! hv )
|
||||
return;
|
||||
|
||||
/* Experimentation shows that the "enlightened" portions of
|
||||
* Windows Server 2016 will break our Hyper-V connection at
|
||||
* some point during a SAN boot. Surprisingly it does not
|
||||
* change the guest OS ID MSR, but it does leave the SynIC
|
||||
* message page disabled.
|
||||
*
|
||||
* Our own explicit quiescing procedure will also disable the
|
||||
* SynIC message page. We can therefore use the SynIC message
|
||||
* page enable bit as a heuristic to determine when we need to
|
||||
* reestablish our Hyper-V connection.
|
||||
*/
|
||||
simp = rdmsr ( HV_X64_MSR_SIMP );
|
||||
if ( simp & HV_SIMP_ENABLE )
|
||||
return;
|
||||
|
||||
/* Remap hypercall page */
|
||||
hv_map_hypercall ( hv );
|
||||
|
||||
/* Remap synthetic interrupt controller */
|
||||
hv_map_synic ( hv );
|
||||
|
||||
/* Reset Hyper-V devices */
|
||||
if ( ( rc = vmbus_reset ( hv, &hv_root_device.dev ) ) != 0 ) {
|
||||
DBGC ( hv, "HV %p could not unquiesce: %s\n",
|
||||
hv, strerror ( rc ) );
|
||||
/* Nothing we can do */
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** Hyper-V quiescer */
|
||||
struct quiescer hv_quiescer __quiescer = {
|
||||
.quiesce = hv_quiesce,
|
||||
.unquiesce = hv_unquiesce,
|
||||
};
|
||||
|
||||
/**
|
||||
* Probe timer
|
||||
*
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hv_timer_probe ( void ) {
|
||||
uint32_t available;
|
||||
uint32_t discard_ebx;
|
||||
uint32_t discard_ecx;
|
||||
uint32_t discard_edx;
|
||||
int rc;
|
||||
|
||||
/* Check we are running in Hyper-V */
|
||||
if ( ( rc = hv_check_hv() ) != 0 )
|
||||
return rc;
|
||||
|
||||
/* Check for available reference counter */
|
||||
cpuid ( HV_CPUID_FEATURES, 0, &available, &discard_ebx, &discard_ecx,
|
||||
&discard_edx );
|
||||
if ( ! ( available & HV_FEATURES_AVAIL_TIME_REF_COUNT_MSR ) ) {
|
||||
DBGC ( HV_INTERFACE_ID, "HV has no time reference counter\n" );
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current system time in ticks
|
||||
*
|
||||
* @ret ticks Current time, in ticks
|
||||
*/
|
||||
static unsigned long hv_currticks ( void ) {
|
||||
|
||||
/* Calculate time using a combination of bit shifts and
|
||||
* multiplication (to avoid a 64-bit division).
|
||||
*/
|
||||
return ( ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) >> HV_TIMER_SHIFT ) *
|
||||
( TICKS_PER_SEC / ( HV_TIMER_HZ >> HV_TIMER_SHIFT ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delay for a fixed number of microseconds
|
||||
*
|
||||
* @v usecs Number of microseconds for which to delay
|
||||
*/
|
||||
static void hv_udelay ( unsigned long usecs ) {
|
||||
uint32_t start;
|
||||
uint32_t elapsed;
|
||||
uint32_t threshold;
|
||||
|
||||
/* Spin until specified number of 10MHz ticks have elapsed */
|
||||
start = rdmsr ( HV_X64_MSR_TIME_REF_COUNT );
|
||||
threshold = ( usecs * ( HV_TIMER_HZ / 1000000 ) );
|
||||
do {
|
||||
elapsed = ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) - start );
|
||||
} while ( elapsed < threshold );
|
||||
}
|
||||
|
||||
/** Hyper-V timer */
|
||||
struct timer hv_timer __timer ( TIMER_PREFERRED ) = {
|
||||
.name = "Hyper-V",
|
||||
.probe = hv_timer_probe,
|
||||
.currticks = hv_currticks,
|
||||
.udelay = hv_udelay,
|
||||
};
|
||||
|
||||
/* Drag in objects via hv_root_device */
|
||||
REQUIRING_SYMBOL ( hv_root_device );
|
||||
|
||||
/* Drag in netvsc driver */
|
||||
//REQUIRE_OBJECT ( netvsc );
|
503
IPXE/ipxe_mod_code/ipxe-3fe683e/src/arch/x86/drivers/xen/hvm.c
Normal file
503
IPXE/ipxe_mod_code/ipxe-3fe683e/src/arch/x86/drivers/xen/hvm.c
Normal file
@@ -0,0 +1,503 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*
|
||||
* You can also choose to distribute this program under the terms of
|
||||
* the Unmodified Binary Distribution Licence (as given in the file
|
||||
* COPYING.UBDL), provided that you have satisfied its requirements.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <ipxe/malloc.h>
|
||||
#include <ipxe/pci.h>
|
||||
#include <ipxe/cpuid.h>
|
||||
#include <ipxe/msr.h>
|
||||
#include <ipxe/xen.h>
|
||||
#include <ipxe/xenver.h>
|
||||
#include <ipxe/xenmem.h>
|
||||
#include <ipxe/xenstore.h>
|
||||
#include <ipxe/xenbus.h>
|
||||
#include <ipxe/xengrant.h>
|
||||
#include "hvm.h"
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Xen HVM driver
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get CPUID base
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hvm_cpuid_base ( struct hvm_device *hvm ) {
|
||||
struct {
|
||||
uint32_t ebx;
|
||||
uint32_t ecx;
|
||||
uint32_t edx;
|
||||
} __attribute__ (( packed )) signature;
|
||||
uint32_t base;
|
||||
uint32_t version;
|
||||
uint32_t discard_eax;
|
||||
uint32_t discard_ebx;
|
||||
uint32_t discard_ecx;
|
||||
uint32_t discard_edx;
|
||||
|
||||
/* Scan for magic signature */
|
||||
for ( base = HVM_CPUID_MIN ; base <= HVM_CPUID_MAX ;
|
||||
base += HVM_CPUID_STEP ) {
|
||||
cpuid ( base, 0, &discard_eax, &signature.ebx, &signature.ecx,
|
||||
&signature.edx );
|
||||
if ( memcmp ( &signature, HVM_CPUID_MAGIC,
|
||||
sizeof ( signature ) ) == 0 ) {
|
||||
hvm->cpuid_base = base;
|
||||
cpuid ( ( base + HVM_CPUID_VERSION ), 0, &version,
|
||||
&discard_ebx, &discard_ecx, &discard_edx );
|
||||
DBGC2 ( hvm, "HVM using CPUID base %#08x (v%d.%d)\n",
|
||||
base, ( version >> 16 ), ( version & 0xffff ) );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
DBGC ( hvm, "HVM could not find hypervisor\n" );
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map hypercall page(s)
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hvm_map_hypercall ( struct hvm_device *hvm ) {
|
||||
uint32_t pages;
|
||||
uint32_t msr;
|
||||
uint32_t discard_ecx;
|
||||
uint32_t discard_edx;
|
||||
physaddr_t hypercall_phys;
|
||||
uint32_t version;
|
||||
static xen_extraversion_t extraversion;
|
||||
int xenrc;
|
||||
int rc;
|
||||
|
||||
/* Get number of hypercall pages and MSR to use */
|
||||
cpuid ( ( hvm->cpuid_base + HVM_CPUID_PAGES ), 0, &pages, &msr,
|
||||
&discard_ecx, &discard_edx );
|
||||
|
||||
/* Allocate pages */
|
||||
hvm->hypercall_len = ( pages * PAGE_SIZE );
|
||||
hvm->xen.hypercall = malloc_dma ( hvm->hypercall_len, PAGE_SIZE );
|
||||
if ( ! hvm->xen.hypercall ) {
|
||||
DBGC ( hvm, "HVM could not allocate %d hypercall page(s)\n",
|
||||
pages );
|
||||
return -ENOMEM;
|
||||
}
|
||||
hypercall_phys = virt_to_phys ( hvm->xen.hypercall );
|
||||
DBGC2 ( hvm, "HVM hypercall page(s) at [%#08lx,%#08lx) via MSR %#08x\n",
|
||||
hypercall_phys, ( hypercall_phys + hvm->hypercall_len ), msr );
|
||||
|
||||
/* Write to MSR */
|
||||
wrmsr ( msr, hypercall_phys );
|
||||
|
||||
/* Check that hypercall mechanism is working */
|
||||
version = xenver_version ( &hvm->xen );
|
||||
if ( ( xenrc = xenver_extraversion ( &hvm->xen, &extraversion ) ) != 0){
|
||||
rc = -EXEN ( xenrc );
|
||||
DBGC ( hvm, "HVM could not get extraversion: %s\n",
|
||||
strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
DBGC2 ( hvm, "HVM found Xen version %d.%d%s\n",
|
||||
( version >> 16 ), ( version & 0xffff ) , extraversion );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap hypercall page(s)
|
||||
*
|
||||
* @v hvm HVM device
|
||||
*/
|
||||
static void hvm_unmap_hypercall ( struct hvm_device *hvm ) {
|
||||
|
||||
/* Free pages */
|
||||
free_dma ( hvm->xen.hypercall, hvm->hypercall_len );
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate and map MMIO space
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @v space Source mapping space
|
||||
* @v len Length (must be a multiple of PAGE_SIZE)
|
||||
* @ret mmio MMIO space address, or NULL on error
|
||||
*/
|
||||
static void * hvm_ioremap ( struct hvm_device *hvm, unsigned int space,
|
||||
size_t len ) {
|
||||
struct xen_add_to_physmap add;
|
||||
struct xen_remove_from_physmap remove;
|
||||
unsigned int pages = ( len / PAGE_SIZE );
|
||||
physaddr_t mmio_phys;
|
||||
unsigned int i;
|
||||
void *mmio;
|
||||
int xenrc;
|
||||
int rc;
|
||||
|
||||
/* Sanity check */
|
||||
assert ( ( len % PAGE_SIZE ) == 0 );
|
||||
|
||||
/* Check for available space */
|
||||
if ( ( hvm->mmio_offset + len ) > hvm->mmio_len ) {
|
||||
DBGC ( hvm, "HVM could not allocate %zd bytes of MMIO space "
|
||||
"(%zd of %zd remaining)\n", len,
|
||||
( hvm->mmio_len - hvm->mmio_offset ), hvm->mmio_len );
|
||||
goto err_no_space;
|
||||
}
|
||||
|
||||
/* Map this space */
|
||||
mmio = ioremap ( ( hvm->mmio + hvm->mmio_offset ), len );
|
||||
if ( ! mmio ) {
|
||||
DBGC ( hvm, "HVM could not map MMIO space [%08lx,%08lx)\n",
|
||||
( hvm->mmio + hvm->mmio_offset ),
|
||||
( hvm->mmio + hvm->mmio_offset + len ) );
|
||||
goto err_ioremap;
|
||||
}
|
||||
mmio_phys = virt_to_phys ( mmio );
|
||||
|
||||
/* Add to physical address space */
|
||||
for ( i = 0 ; i < pages ; i++ ) {
|
||||
add.domid = DOMID_SELF;
|
||||
add.idx = i;
|
||||
add.space = space;
|
||||
add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
|
||||
if ( ( xenrc = xenmem_add_to_physmap ( &hvm->xen, &add ) ) !=0){
|
||||
rc = -EXEN ( xenrc );
|
||||
DBGC ( hvm, "HVM could not add space %d idx %d at "
|
||||
"[%08lx,%08lx): %s\n", space, i,
|
||||
( mmio_phys + ( i * PAGE_SIZE ) ),
|
||||
( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
|
||||
strerror ( rc ) );
|
||||
goto err_add_to_physmap;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update offset */
|
||||
hvm->mmio_offset += len;
|
||||
|
||||
return mmio;
|
||||
|
||||
i = pages;
|
||||
err_add_to_physmap:
|
||||
for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
|
||||
remove.domid = DOMID_SELF;
|
||||
add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
|
||||
xenmem_remove_from_physmap ( &hvm->xen, &remove );
|
||||
}
|
||||
iounmap ( mmio );
|
||||
err_ioremap:
|
||||
err_no_space:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap MMIO space
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @v mmio MMIO space address
|
||||
* @v len Length (must be a multiple of PAGE_SIZE)
|
||||
*/
|
||||
static void hvm_iounmap ( struct hvm_device *hvm, void *mmio, size_t len ) {
|
||||
struct xen_remove_from_physmap remove;
|
||||
physaddr_t mmio_phys = virt_to_phys ( mmio );
|
||||
unsigned int pages = ( len / PAGE_SIZE );
|
||||
unsigned int i;
|
||||
int xenrc;
|
||||
int rc;
|
||||
|
||||
/* Unmap this space */
|
||||
iounmap ( mmio );
|
||||
|
||||
/* Remove from physical address space */
|
||||
for ( i = 0 ; i < pages ; i++ ) {
|
||||
remove.domid = DOMID_SELF;
|
||||
remove.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
|
||||
if ( ( xenrc = xenmem_remove_from_physmap ( &hvm->xen,
|
||||
&remove ) ) != 0 ) {
|
||||
rc = -EXEN ( xenrc );
|
||||
DBGC ( hvm, "HVM could not remove space [%08lx,%08lx): "
|
||||
"%s\n", ( mmio_phys + ( i * PAGE_SIZE ) ),
|
||||
( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
|
||||
strerror ( rc ) );
|
||||
/* Nothing we can do about this */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map shared info page
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hvm_map_shared_info ( struct hvm_device *hvm ) {
|
||||
physaddr_t shared_info_phys;
|
||||
int rc;
|
||||
|
||||
/* Map shared info page */
|
||||
hvm->xen.shared = hvm_ioremap ( hvm, XENMAPSPACE_shared_info,
|
||||
PAGE_SIZE );
|
||||
if ( ! hvm->xen.shared ) {
|
||||
rc = -ENOMEM;
|
||||
goto err_alloc;
|
||||
}
|
||||
shared_info_phys = virt_to_phys ( hvm->xen.shared );
|
||||
DBGC2 ( hvm, "HVM shared info page at [%#08lx,%#08lx)\n",
|
||||
shared_info_phys, ( shared_info_phys + PAGE_SIZE ) );
|
||||
|
||||
/* Sanity check */
|
||||
DBGC2 ( hvm, "HVM wallclock time is %d\n",
|
||||
readl ( &hvm->xen.shared->wc_sec ) );
|
||||
|
||||
return 0;
|
||||
|
||||
hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
|
||||
err_alloc:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap shared info page
|
||||
*
|
||||
* @v hvm HVM device
|
||||
*/
|
||||
static void hvm_unmap_shared_info ( struct hvm_device *hvm ) {
|
||||
|
||||
/* Unmap shared info page */
|
||||
hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
|
||||
}
|
||||
|
||||
/**
|
||||
* Map grant table
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hvm_map_grant ( struct hvm_device *hvm ) {
|
||||
physaddr_t grant_phys;
|
||||
int rc;
|
||||
|
||||
/* Initialise grant table */
|
||||
if ( ( rc = xengrant_init ( &hvm->xen ) ) != 0 ) {
|
||||
DBGC ( hvm, "HVM could not initialise grant table: %s\n",
|
||||
strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Map grant table */
|
||||
hvm->xen.grant.table = hvm_ioremap ( hvm, XENMAPSPACE_grant_table,
|
||||
hvm->xen.grant.len );
|
||||
if ( ! hvm->xen.grant.table )
|
||||
return -ENODEV;
|
||||
|
||||
grant_phys = virt_to_phys ( hvm->xen.grant.table );
|
||||
DBGC2 ( hvm, "HVM mapped grant table at [%08lx,%08lx)\n",
|
||||
grant_phys, ( grant_phys + hvm->xen.grant.len ) );
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap grant table
|
||||
*
|
||||
* @v hvm HVM device
|
||||
*/
|
||||
static void hvm_unmap_grant ( struct hvm_device *hvm ) {
|
||||
|
||||
/* Unmap grant table */
|
||||
hvm_iounmap ( hvm, hvm->xen.grant.table, hvm->xen.grant.len );
|
||||
}
|
||||
|
||||
/**
|
||||
* Map XenStore
|
||||
*
|
||||
* @v hvm HVM device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hvm_map_xenstore ( struct hvm_device *hvm ) {
|
||||
uint64_t xenstore_evtchn;
|
||||
uint64_t xenstore_pfn;
|
||||
physaddr_t xenstore_phys;
|
||||
char *name;
|
||||
int xenrc;
|
||||
int rc;
|
||||
|
||||
/* Get XenStore event channel */
|
||||
if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_EVTCHN,
|
||||
&xenstore_evtchn ) ) != 0 ) {
|
||||
rc = -EXEN ( xenrc );
|
||||
DBGC ( hvm, "HVM could not get XenStore event channel: %s\n",
|
||||
strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
hvm->xen.store.port = xenstore_evtchn;
|
||||
|
||||
/* Get XenStore PFN */
|
||||
if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_PFN,
|
||||
&xenstore_pfn ) ) != 0 ) {
|
||||
rc = -EXEN ( xenrc );
|
||||
DBGC ( hvm, "HVM could not get XenStore PFN: %s\n",
|
||||
strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
xenstore_phys = ( xenstore_pfn * PAGE_SIZE );
|
||||
|
||||
/* Map XenStore */
|
||||
hvm->xen.store.intf = ioremap ( xenstore_phys, PAGE_SIZE );
|
||||
if ( ! hvm->xen.store.intf ) {
|
||||
DBGC ( hvm, "HVM could not map XenStore at [%08lx,%08lx)\n",
|
||||
xenstore_phys, ( xenstore_phys + PAGE_SIZE ) );
|
||||
return -ENODEV;
|
||||
}
|
||||
DBGC2 ( hvm, "HVM mapped XenStore at [%08lx,%08lx) with event port "
|
||||
"%d\n", xenstore_phys, ( xenstore_phys + PAGE_SIZE ),
|
||||
hvm->xen.store.port );
|
||||
|
||||
/* Check that XenStore is working */
|
||||
if ( ( rc = xenstore_read ( &hvm->xen, &name, "name", NULL ) ) != 0 ) {
|
||||
DBGC ( hvm, "HVM could not read domain name: %s\n",
|
||||
strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
DBGC2 ( hvm, "HVM running in domain \"%s\"\n", name );
|
||||
free ( name );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmap XenStore
|
||||
*
|
||||
* @v hvm HVM device
|
||||
*/
|
||||
static void hvm_unmap_xenstore ( struct hvm_device *hvm ) {
|
||||
|
||||
/* Unmap XenStore */
|
||||
iounmap ( hvm->xen.store.intf );
|
||||
}
|
||||
|
||||
/**
|
||||
* Probe PCI device
|
||||
*
|
||||
* @v pci PCI device
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int hvm_probe ( struct pci_device *pci ) {
|
||||
struct hvm_device *hvm;
|
||||
int rc;
|
||||
|
||||
/* Allocate and initialise structure */
|
||||
hvm = zalloc ( sizeof ( *hvm ) );
|
||||
if ( ! hvm ) {
|
||||
rc = -ENOMEM;
|
||||
goto err_alloc;
|
||||
}
|
||||
hvm->mmio = pci_bar_start ( pci, HVM_MMIO_BAR );
|
||||
hvm->mmio_len = pci_bar_size ( pci, HVM_MMIO_BAR );
|
||||
DBGC2 ( hvm, "HVM has MMIO space [%08lx,%08lx)\n",
|
||||
hvm->mmio, ( hvm->mmio + hvm->mmio_len ) );
|
||||
|
||||
/* Fix up PCI device */
|
||||
adjust_pci_device ( pci );
|
||||
|
||||
/* Attach to hypervisor */
|
||||
if ( ( rc = hvm_cpuid_base ( hvm ) ) != 0 )
|
||||
goto err_cpuid_base;
|
||||
if ( ( rc = hvm_map_hypercall ( hvm ) ) != 0 )
|
||||
goto err_map_hypercall;
|
||||
if ( ( rc = hvm_map_shared_info ( hvm ) ) != 0 )
|
||||
goto err_map_shared_info;
|
||||
if ( ( rc = hvm_map_grant ( hvm ) ) != 0 )
|
||||
goto err_map_grant;
|
||||
if ( ( rc = hvm_map_xenstore ( hvm ) ) != 0 )
|
||||
goto err_map_xenstore;
|
||||
|
||||
/* Probe Xen devices */
|
||||
if ( ( rc = xenbus_probe ( &hvm->xen, &pci->dev ) ) != 0 ) {
|
||||
DBGC ( hvm, "HVM could not probe Xen bus: %s\n",
|
||||
strerror ( rc ) );
|
||||
goto err_xenbus_probe;
|
||||
}
|
||||
|
||||
pci_set_drvdata ( pci, hvm );
|
||||
return 0;
|
||||
|
||||
xenbus_remove ( &hvm->xen, &pci->dev );
|
||||
err_xenbus_probe:
|
||||
hvm_unmap_xenstore ( hvm );
|
||||
err_map_xenstore:
|
||||
hvm_unmap_grant ( hvm );
|
||||
err_map_grant:
|
||||
hvm_unmap_shared_info ( hvm );
|
||||
err_map_shared_info:
|
||||
hvm_unmap_hypercall ( hvm );
|
||||
err_map_hypercall:
|
||||
err_cpuid_base:
|
||||
free ( hvm );
|
||||
err_alloc:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove PCI device
|
||||
*
|
||||
* @v pci PCI device
|
||||
*/
|
||||
static void hvm_remove ( struct pci_device *pci ) {
|
||||
struct hvm_device *hvm = pci_get_drvdata ( pci );
|
||||
|
||||
xenbus_remove ( &hvm->xen, &pci->dev );
|
||||
hvm_unmap_xenstore ( hvm );
|
||||
hvm_unmap_grant ( hvm );
|
||||
hvm_unmap_shared_info ( hvm );
|
||||
hvm_unmap_hypercall ( hvm );
|
||||
free ( hvm );
|
||||
}
|
||||
|
||||
/** PCI device IDs */
|
||||
static struct pci_device_id hvm_ids[] = {
|
||||
PCI_ROM ( 0x5853, 0x0001, "hvm", "hvm", 0 ),
|
||||
PCI_ROM ( 0x5853, 0x0002, "hvm2", "hvm2", 0 ),
|
||||
};
|
||||
|
||||
/** PCI driver */
|
||||
struct pci_driver hvm_driver __pci_driver = {
|
||||
.ids = hvm_ids,
|
||||
.id_count = ( sizeof ( hvm_ids ) / sizeof ( hvm_ids[0] ) ),
|
||||
.probe = hvm_probe,
|
||||
.remove = hvm_remove,
|
||||
};
|
||||
|
||||
/* Drag in objects via hvm_driver */
|
||||
REQUIRING_SYMBOL ( hvm_driver );
|
||||
|
||||
/* Drag in netfront driver */
|
||||
//REQUIRE_OBJECT ( netfront );
|
@@ -0,0 +1,235 @@
|
||||
/* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*
|
||||
* You can also choose to distribute this program under the terms of
|
||||
* the Unmodified Binary Distribution Licence (as given in the file
|
||||
* COPYING.UBDL), provided that you have satisfied its requirements.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <assert.h>
|
||||
#include <realmode.h>
|
||||
#include <biosint.h>
|
||||
#include <basemem.h>
|
||||
#include <fakee820.h>
|
||||
#include <ipxe/init.h>
|
||||
#include <ipxe/io.h>
|
||||
#include <ipxe/hidemem.h>
|
||||
|
||||
/** Set to true if you want to test a fake E820 map */
|
||||
#define FAKE_E820 0
|
||||
|
||||
/** Alignment for hidden memory regions */
|
||||
#define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
|
||||
|
||||
/**
|
||||
* A hidden region of iPXE
|
||||
*
|
||||
* This represents a region that will be edited out of the system's
|
||||
* memory map.
|
||||
*
|
||||
* This structure is accessed by assembly code, so must not be
|
||||
* changed.
|
||||
*/
|
||||
struct hidden_region {
|
||||
/** Physical start address */
|
||||
uint64_t start;
|
||||
/** Physical end address */
|
||||
uint64_t end;
|
||||
};
|
||||
|
||||
/** Hidden base memory */
|
||||
extern struct hidden_region __data16 ( hidemem_base );
|
||||
#define hidemem_base __use_data16 ( hidemem_base )
|
||||
|
||||
/** Hidden umalloc memory */
|
||||
extern struct hidden_region __data16 ( hidemem_umalloc );
|
||||
#define hidemem_umalloc __use_data16 ( hidemem_umalloc )
|
||||
|
||||
/** Hidden text memory */
|
||||
extern struct hidden_region __data16 ( hidemem_textdata );
|
||||
#define hidemem_textdata __use_data16 ( hidemem_textdata )
|
||||
|
||||
/** Assembly routine in e820mangler.S */
|
||||
extern void int15();
|
||||
|
||||
/** Vector for storing original INT 15 handler */
|
||||
extern struct segoff __text16 ( int15_vector );
|
||||
#define int15_vector __use_text16 ( int15_vector )
|
||||
|
||||
/* The linker defines these symbols for us */
|
||||
extern char _textdata[];
|
||||
extern char _etextdata[];
|
||||
extern char _text16_memsz[];
|
||||
#define _text16_memsz ( ( size_t ) _text16_memsz )
|
||||
extern char _data16_memsz[];
|
||||
#define _data16_memsz ( ( size_t ) _data16_memsz )
|
||||
|
||||
/**
|
||||
* Hide region of memory from system memory map
|
||||
*
|
||||
* @v region Hidden memory region
|
||||
* @v start Start of region
|
||||
* @v end End of region
|
||||
*/
|
||||
static void hide_region ( struct hidden_region *region,
|
||||
physaddr_t start, physaddr_t end ) {
|
||||
|
||||
/* Some operating systems get a nasty shock if a region of the
|
||||
* E820 map seems to start on a non-page boundary. Make life
|
||||
* safer by rounding out our edited region.
|
||||
*/
|
||||
region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
|
||||
region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
|
||||
|
||||
DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide used base memory
|
||||
*
|
||||
*/
|
||||
void hide_basemem ( void ) {
|
||||
/* Hide from the top of free base memory to 640kB. Don't use
|
||||
* hide_region(), because we don't want this rounded to the
|
||||
* nearest page boundary.
|
||||
*/
|
||||
hidemem_base.start = ( get_fbms() * 1024 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide umalloc() region
|
||||
*
|
||||
*/
|
||||
void hide_umalloc ( physaddr_t start, physaddr_t end ) {
|
||||
assert ( end <= virt_to_phys ( _textdata ) );
|
||||
hide_region ( &hidemem_umalloc, start, end );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide .text and .data
|
||||
*
|
||||
*/
|
||||
void hide_textdata ( void ) {
|
||||
hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
|
||||
virt_to_phys ( _etextdata ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide Etherboot
|
||||
*
|
||||
* Installs an INT 15 handler to edit Etherboot out of the memory map
|
||||
* returned by the BIOS.
|
||||
*/
|
||||
static void hide_etherboot ( void ) {
|
||||
struct memory_map memmap;
|
||||
unsigned int rm_ds_top;
|
||||
unsigned int rm_cs_top;
|
||||
unsigned int fbms;
|
||||
|
||||
/* Dump memory map before mangling */
|
||||
DBG ( "Hiding iPXE from system memory map\n" );
|
||||
get_memmap ( &memmap );
|
||||
|
||||
/* Hook in fake E820 map, if we're testing one */
|
||||
if ( FAKE_E820 ) {
|
||||
DBG ( "Hooking in fake E820 map\n" );
|
||||
fake_e820();
|
||||
get_memmap ( &memmap );
|
||||
}
|
||||
|
||||
/* Initialise the hidden regions */
|
||||
hide_basemem();
|
||||
hide_umalloc ( virt_to_phys ( _textdata ), virt_to_phys ( _textdata ) );
|
||||
hide_textdata();
|
||||
|
||||
/* Some really moronic BIOSes bring up the PXE stack via the
|
||||
* UNDI loader entry point and then don't bother to unload it
|
||||
* before overwriting the code and data segments. If this
|
||||
* happens, we really don't want to leave INT 15 hooked,
|
||||
* because that will cause any loaded OS to die horribly as
|
||||
* soon as it attempts to fetch the system memory map.
|
||||
*
|
||||
* We use a heuristic to guess whether or not we are being
|
||||
* loaded sensibly.
|
||||
*/
|
||||
rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
|
||||
rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
|
||||
fbms = get_fbms();
|
||||
if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
|
||||
DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
|
||||
"DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
|
||||
DBG ( "Disabling INT 15 memory hiding\n" );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Hook INT 15 */
|
||||
hook_bios_interrupt ( 0x15, ( intptr_t ) int15, &int15_vector );
|
||||
|
||||
/* Dump memory map after mangling */
|
||||
DBG ( "Hidden iPXE from system memory map\n" );
|
||||
get_memmap ( &memmap );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unhide Etherboot
|
||||
*
|
||||
* Uninstalls the INT 15 handler installed by hide_etherboot(), if
|
||||
* possible.
|
||||
*/
|
||||
static void unhide_etherboot ( int flags __unused ) {
|
||||
struct memory_map memmap;
|
||||
int rc;
|
||||
|
||||
/* If we have more than one hooked interrupt at this point, it
|
||||
* means that some other vector is still hooked, in which case
|
||||
* we can't safely unhook INT 15 because we need to keep our
|
||||
* memory protected. (We expect there to be at least one
|
||||
* hooked interrupt, because INT 15 itself is still hooked).
|
||||
*/
|
||||
if ( hooked_bios_interrupts > 1 ) {
|
||||
DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
|
||||
hooked_bios_interrupts );
|
||||
return;
|
||||
}
|
||||
|
||||
/* Try to unhook INT 15 */
|
||||
if ( ( rc = unhook_bios_interrupt ( 0x15, ( intptr_t ) int15,
|
||||
&int15_vector ) ) != 0 ) {
|
||||
DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
|
||||
/* Leave it hooked; there's nothing else we can do,
|
||||
* and it should be intrinsically safe (though
|
||||
* wasteful of RAM).
|
||||
*/
|
||||
}
|
||||
|
||||
/* Unhook fake E820 map, if used */
|
||||
if ( FAKE_E820 )
|
||||
unfake_e820();
|
||||
|
||||
/* Dump memory map after unhiding */
|
||||
DBG ( "Unhidden iPXE from system memory map\n" );
|
||||
get_memmap ( &memmap );
|
||||
}
|
||||
|
||||
/** Hide Etherboot startup function */
|
||||
struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
|
||||
.name = "hidemem",
|
||||
.startup = hide_etherboot,
|
||||
.shutdown = unhide_etherboot,
|
||||
};
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*
|
||||
* You can also choose to distribute this program under the terms of
|
||||
* the Unmodified Binary Distribution Licence (as given in the file
|
||||
* COPYING.UBDL), provided that you have satisfied its requirements.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <errno.h>
|
||||
#include <ipxe/sanboot.h>
|
||||
|
||||
static int null_san_hook ( unsigned int drive __unused,
|
||||
struct uri **uris __unused,
|
||||
unsigned int count __unused,
|
||||
unsigned int flags __unused ) {
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
static void null_san_unhook ( unsigned int drive __unused ) {
|
||||
/* Do nothing */
|
||||
}
|
||||
|
||||
static int null_san_boot ( unsigned int drive __unused,
|
||||
const char *filename __unused ) {
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
static int null_san_describe ( void ) {
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
PROVIDE_SANBOOT ( pcbios, san_hook, null_san_hook );
|
||||
PROVIDE_SANBOOT ( pcbios, san_unhook, null_san_unhook );
|
||||
PROVIDE_SANBOOT ( pcbios, san_boot, null_san_boot );
|
||||
PROVIDE_SANBOOT ( pcbios, san_describe, null_san_describe );
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,48 @@
|
||||
|
||||
#ifndef __VENTOY_INT13_H__
|
||||
#define __VENTOY_INT13_H__
|
||||
|
||||
#undef for_each_sandev
|
||||
#define for_each_sandev( sandev ) sandev = g_sandev; if (sandev)
|
||||
|
||||
int ventoy_vdisk_read(struct san_device*sandev, uint64_t lba, unsigned int count, unsigned long buffer);
|
||||
|
||||
static inline int ventoy_sandev_write ( struct san_device *sandev, uint64_t lba, unsigned int count, unsigned long buffer )
|
||||
{
|
||||
(void)sandev;
|
||||
(void)lba;
|
||||
(void)count;
|
||||
(void)buffer;
|
||||
DBGC(sandev, "ventoy_sandev_write\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int ventoy_sandev_reset (void *sandev)
|
||||
{
|
||||
(void)sandev;
|
||||
DBGC(sandev, "ventoy_sandev_reset\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define sandev_reset ventoy_sandev_reset
|
||||
#define sandev_read ventoy_vdisk_read
|
||||
#define sandev_write ventoy_sandev_write
|
||||
|
||||
#undef ECANCELED
|
||||
#define ECANCELED 0x0b
|
||||
#undef ENODEV
|
||||
#define ENODEV 0x2c
|
||||
#undef ENOTSUP
|
||||
#define ENOTSUP 0x3c
|
||||
#undef ENOMEM
|
||||
#define ENOMEM 0x31
|
||||
#undef EIO
|
||||
#define EIO 0x1d
|
||||
#undef ENOEXEC
|
||||
#define ENOEXEC 0x2e
|
||||
#undef ENOSPC
|
||||
#define ENOSPC 0x34
|
||||
|
||||
|
||||
#endif /* __VENTOY_INT13_H__ */
|
||||
|
Reference in New Issue
Block a user