Skip to contentSkip to conversation

32bit gateway for the Bochs BIOS

PATCH In review Stefan Berger 1 participant 1 message Jan 3, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 32bit gateway for the Bochs BIOS

This patch adds a 32bit gateway to the Bochs BIOS. The 32 bit code is compiled with gcc and linked into the hvmloader as a byte-array. Hvmloader allocates memory (rounded up to next 64kb) from the e820 table below 4GB, copies and relocates the 32bit code in the allocated area and copies a jumptable (located in a section '.biosjumptable') pointing to the 'exported' functions into the Bochs BIOS's memory area to link the two code sections. The memory area has been reserved and can be identified with the signature '___JMPT'.

In the Bochs BIOS only stub functions are provided. These load the index of a particular function in the jump table, switch to protected mode and call the function in the high memory area. The stack is prepared such that functions compiled by gcc can just pick the parameters from the stack as usual - this means that the 16bit real-mode return address is taken off the stack. The stub functions should have the same signature as those in 32bit space. For ABI compatibility reasons parameters inside the Bochs BIOS stubs should all be 32bit wide.

This patch includes a test function that calls three gcc-compiled functions in the high memory area and displays their success status. Simple tests are done doing multiplication and addition of 32-bit numbers and reading and modification of a static variable. These functions test the interface and the relocation code. The test code is removed in patch part 3.

xen-unstable.hg/tools/firmware/hvmloader/32bitbios_support.c new file +169 -0
Index: root/xen-unstable.hg/tools/firmware/hvmloader/32bitbios_support.c
===================================================================
@@ -0,0 +1,169 @@
+/*
+ * 32bitbios_support.c - relocation of 32bit BIOS implementation
+ *
+ * Stefan Berger, [email protected]
+ * Copyright (c) 2006, International Business Machines Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include <elf.h>
+#include <xen/hvm/e820.h>
+#include "util.h"
+#include "config.h"
+
+#include "../rombios/32bit/32bitbios_flat.h"
+#include "../rombios/32bit/jumptable.h"
+
+
+/*
+ * relocate ELF file of type ET_REL
+ */
+static int relocate_elf(unsigned char *elfarray) {
+    Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfarray;
+    Elf32_Shdr *shdr = (Elf32_Shdr *)&elfarray[ehdr->e_shoff];
+    int i;
+
+    if (ehdr->e_type != ET_REL) {
+        printf("Not a relocatabel BIOS object file. Has type %d, need %d\n",
+               ehdr->e_type, ET_REL);
+        return -1;
+    }
+
+    for (i = 0; i < ehdr->e_shnum; i++) {
+        if (!(shdr[i]).sh_flags & SHF_ALLOC) {
+            shdr[i].sh_addr = 0;
+            continue;
+        }
+        shdr[i].sh_addr = (Elf32_Addr)&elfarray[shdr[i].sh_offset];
+    }
+
+    for (i = 0; i < ehdr->e_shnum; i++) {
+        if (shdr[i].sh_type == SHT_REL && shdr[i].sh_addr != 0) {
+            Elf32_Shdr *targetsec = (Elf32_Shdr *)&(shdr[shdr[i].sh_info]);
+            Elf32_Shdr *symtabsec = (Elf32_Shdr *)&(shdr[shdr[i].sh_link]);
+            Elf32_Sym  *syms      = (Elf32_Sym *)symtabsec->sh_addr;
+            Elf32_Rel  *rels      = (Elf32_Rel *)shdr[i].sh_addr;
+            unsigned char *code   = (unsigned char *)targetsec->sh_addr;
+            int j;
+
+            for (j = 0; j < shdr[i].sh_size / sizeof(Elf32_Rel); j++) {
+                int idx           = ELF32_R_SYM(rels[j].r_info);
+                Elf32_Sym *symbol = &syms[idx];
+                uint32_t *loc     = (uint32_t *)&code[rels[j].r_offset];
+                uint32_t fix      = shdr[symbol->st_shndx].sh_addr +
+                                    symbol->st_value;
+
+                switch (ELF32_R_TYPE(rels[j].r_info)) {
+                    case R_386_PC32:
+                        *loc += (fix - (uint32_t)loc);
+                    break;
+
+                    case R_386_32:
+                        *loc += fix;
+                    break;
+                }
+            }
+        } else if (shdr[i].sh_type == SHT_RELA) {
+            return -2;
+        }
+    }
+    return 0;
+}
+
+/* scan the rombios for the destination of the jumptable */
+static char* get_jump_table_start(void)
+{
+    char *bios_mem;
+
+    for ( bios_mem = (char *)ROMBIOS_BEGIN;
+          bios_mem != (char *)ROMBIOS_END;
+          bios_mem++ ) {
+        if (strncmp(bios_mem, "___JMPT", 7) == 0)
+            return bios_mem;
+    }
+
+    return NULL;
+}
+
+/* copy relocated jumptable into the rombios */
+static int copy_jumptable(unsigned char *elfarray)
+{
+    int rc = 0;
+    Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfarray;
+    Elf32_Shdr *shdr = (Elf32_Shdr *)&elfarray[ehdr->e_shoff];
+    Elf32_Shdr *shdr_strings = (Elf32_Shdr *)&shdr[ehdr->e_shstrndx];
+    char *secstrings = (char *)&elfarray[shdr_strings->sh_offset];
+    uint32_t *rombiosjumptable = (uint32_t *)get_jump_table_start();
+    uint32_t *biosjumptable    = NULL;
+    int i;
+
+    if (rombiosjumptable == NULL) {
+        return -3;
+    }
+
+     /* find the section with the jump table  and copy to lower BIOS memory */
+    for (i = 0; i < ehdr->e_shnum; i++) {
+        if (!strcmp(JUMPTABLE_SECTION_NAME, secstrings + shdr[i].sh_name)) {
+            uint32_t biosjumptableentries;
+            biosjumptable        = (uint32_t *)shdr[i].sh_addr;
+            biosjumptableentries = shdr[i].sh_size / 4;
+            for (int j = 0; j < biosjumptableentries; j++) {
+                rombiosjumptable[j] = biosjumptable[j];
+                if (biosjumptable[j] == 0 &&
+                    j < (biosjumptableentries - 1)) {
+                    printf("WARNING: jumptable entry %d is NULL!\n",j);
+                }
+            }
+            break;
+        }
+    }
+
+    if (biosjumptable == NULL) {
+        printf("Could not find " JUMPTABLE_SECTION_NAME " section in file.\n");
+        rc = -4;
+    }
+
+    return 0;
+}
+
+static int relocate_32bitbios(unsigned char *elfarray, uint32_t elfarraysize)
+{
+    int rc = 0;
+    uint32_t mask = (64 * 1024) - 1;
+    uint32_t to_malloc = (elfarraysize + mask) & ~mask; /* round to 64kb */
+    unsigned char *highbiosarea;
+
+    highbiosarea = (unsigned char *)(long)
+                           e820_malloc((uint64_t)to_malloc,
+                                       E820_RESERVED,
+                                       (uint64_t)0xffffffff);
+
+    if (highbiosarea != 0) {
+        memcpy(highbiosarea, elfarray, elfarraysize);
+        rc = relocate_elf(highbiosarea);
+        if (rc == 0) {
+            rc = copy_jumptable(highbiosarea);
+        }
+    } else {
+        rc = -5;
+    }
+
+    return rc;
+}
+
+int highbios_setup(void)
+{
+    return relocate_32bitbios((unsigned char *)highbios_array,
+                              sizeof(highbios_array));
+}
xen-unstable.hg/tools/firmware/hvmloader/Makefile +1 -1
Index: root/xen-unstable.hg/tools/firmware/hvmloader/Makefile
===================================================================
@@ -35,7 +35,7 @@ OBJCOPY  = objcopy
 CFLAGS  += -fno-builtin -O2 -msoft-float
 LDFLAGS  = -nostdlib -Wl,-N -Wl,-Ttext -Wl,$(LOADADDR)
 
-SRCS = hvmloader.c mp_tables.c util.c smbios.c
+SRCS = hvmloader.c mp_tables.c util.c smbios.c 32bitbios_support.c
 OBJS = $(patsubst %.c,%.o,$(SRCS))
 
 .PHONY: all
xen-unstable.hg/tools/firmware/hvmloader/hvmloader.c +1 -0
Index: root/xen-unstable.hg/tools/firmware/hvmloader/hvmloader.c
===================================================================
@@ -314,6 +314,7 @@ int main(void)
 
     printf("Loading ROMBIOS ...\n");
     memcpy((void *)ROMBIOS_PHYSICAL_ADDRESS, rombios, sizeof(rombios));
+    highbios_setup();
 
     apic_setup();
     pci_setup();
xen-unstable.hg/tools/firmware/hvmloader/util.h +5 -0
Index: root/xen-unstable.hg/tools/firmware/hvmloader/util.h
===================================================================
@@ -53,6 +53,7 @@ int get_apic_mode(void);
 
 /* String and memory functions */
 int strcmp(const char *cs, const char *ct);
+int strncmp(const char *s1, const char *s2, uint32_t n);
 char *strcpy(char *dest, const char *src);
 char *strncpy(char *dest, const char *src, unsigned n);
 unsigned strlen(const char *s);
@@ -77,6 +78,10 @@ int vprintf(const char *fmt, va_list ap)
 /* Allocate region of specified type in the e820 table. */
 uint64_t e820_malloc(uint64_t size, uint32_t type, uint64_t mask);
 
+/* Prepare the 32bit BIOS */
+int highbios_setup(void);
+
+
 #define isdigit(c) ((c) >= '0' && (c) <= '9')
 
 #endif /* __HVMLOADER_UTIL_H__ */
xen-unstable.hg/tools/firmware/Makefile +1 -1
Index: root/xen-unstable.hg/tools/firmware/Makefile
===================================================================
@@ -7,7 +7,7 @@ TARGET      := hvmloader/hvmloader
 INST_DIR := $(DESTDIR)/usr/lib/xen/boot
 
 SUBDIRS :=
-SUBDIRS += rombios
+SUBDIRS += rombios rombios/32bit
 SUBDIRS += vgabios
 SUBDIRS += vmxassist
 SUBDIRS += hvmloader
xen-unstable.hg/tools/firmware/rombios/32bit/Makefile new file +26 -0
Index: root/xen-unstable.hg/tools/firmware/rombios/32bit/Makefile
===================================================================
@@ -0,0 +1,26 @@
+TARGET = 32bitbios_flat.h
+
+CFLAGS = -Wall -Werror -Wunused -Werror-implicit-function-declaration
+CFLAGS += -fomit-frame-pointer -fno-builtin
+CFLAGS += -I../
+
+MODULES = 32bitbios.o
+
+.PHONY: all
+
+all : $(TARGET)
+
+clean ::
+	rm -rf *.o $(TARGET)
+
+$(TARGET) : 32bitbios_all.o $(SOURCES)
+	unref=`nm -u 32bitbios_all.o`
+	if [ "$$unref" != "" ]; then \
+		echo "There are unresolved symbols in the BIOS.";	\
+		echo $$unref ;						\
+	else								\
+		bash mkhex highbios_array 32bitbios_all.o > $(TARGET); \
+	fi
+
+32bitbios_all.o : 32bitbios.o $(MODULES)
+	ld -r $(MODULES) -o 32bitbios_all.o
xen-unstable.hg/tools/firmware/rombios/32bit/mkhex new file +26 -0
Index: root/xen-unstable.hg/tools/firmware/rombios/32bit/mkhex
===================================================================
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+#
+# mkhex: Generate C embeddable hexdumps
+#
+# Leendert van Doorn, [email protected]
+# Copyright (c) 2005, International Business Machines Corporation.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms and conditions of the GNU General Public License,
+# version 2, as published by the Free Software Foundation.
+#
+# This program is distributed in the hope 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., 59 Temple
+# Place - Suite 330, Boston, MA 02111-1307 USA.
+#
+
+echo "unsigned $1[] = {"
+od -v -t x $2 | sed 's/^[0-9]* /0x/' | sed 's/ /, 0x/g' | sed 's/$/,/'
+echo "};"
+
xen-unstable.hg/tools/firmware/rombios/32bit/32bitbios.c new file +60 -0

60 lines not on this page · raw message

xen-unstable.hg/tools/firmware/rombios/rombios.c +19 -1
Index: root/xen-unstable.hg/tools/firmware/rombios/rombios.c
===================================================================
@@ -724,6 +724,8 @@ typedef struct {
     } cdemu_t;
 #endif // BX_ELTORITO_BOOT
   
+#include "32bitgateway.h"
+
   // for access to EBDA area
   //     The EBDA structure should conform to 
   //     http://www.cybertrails.com/~fys/rombios.htm document
@@ -745,6 +747,7 @@ typedef struct {
     cdemu_t cdemu;
 #endif // BX_ELTORITO_BOOT
 
+    upcall_t upcall;
     } ebda_data_t;
   
   #define EbdaData ((ebda_data_t *) 0)
@@ -1845,6 +1848,7 @@ print_bios_banner()
   printf(BX_APPNAME" BIOS, %d cpu%s, ", BX_SMP_PROCESSORS, BX_SMP_PROCESSORS>1?"s":"");
   printf("%s %s\n", bios_cvs_version_string, bios_date_string);
   printf("\n");
+  test_gateway();
 }
 
 //--------------------------------------------------------------------------
@@ -8766,6 +8770,10 @@ use16 386
 
 #endif
 
+ASM_END
+#include "32bitgateway.c"
+ASM_START
+
 ;--------------------
 #if BX_PCIBIOS
 use32 386
@@ -10553,13 +10561,23 @@ static Bit8u vgafont8[128*8]=
 };
 
 #ifdef HVMASSIST
+ASM_START
+
+// space for addresses in 32bit BIOS area; currently 256/4 entries
+// are allocated
+.org 0xcb00
+jmptable:
+db 0x5F, 0x5F, 0x5F, 0x4A, 0x4D, 0x50, 0x54 ;; ___JMPT
+dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;;  64 bytes
+dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;; 128 bytes
+dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;; 192 bytes
+
 //
 // MP Tables
 // just carve out some blank space for HVMLOADER to write the MP tables to
 //
 // NOTE: There should be enough space for a 32 processor entry MP table
 //
-ASM_START
 .org 0xcc00
 db 0x5F, 0x5F, 0x5F, 0x48, 0x56, 0x4D, 0x4D, 0x50 ;; ___HVMMP
 dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;;  64 bytes
xen-unstable.hg/tools/firmware/rombios/32bit/rombios_compat.h new file +92 -0

92 lines not on this page · raw message

xen-unstable.hg/tools/firmware/rombios/32bitprotos.h new file +22 -0
Index: root/xen-unstable.hg/tools/firmware/rombios/32bitprotos.h
===================================================================
@@ -0,0 +1,22 @@
+#ifndef PROTOS_HIGHBIOS
+#define PROTOS_HIGHBIOS
+
+/* bcc does not like 'enum' */
+#define IDX_MULTIPLY   0
+#define IDX_ADD        1
+#define IDX_SET_STATIC 2
+#define IDX_LAST       3 /* keep last! */
+
+
+#ifdef GCC_PROTOS
+  #define PARMS(x...) x
+#else
+  /* bcc doesn't want any parameter types in prototypes */
+  #define PARMS(x...)
+#endif
+
+Bit32u multiply( PARMS(Bit32u a, Bit32u b) );
+Bit32u add( PARMS(Bit32u a, Bit32u b) );
+Bit32u set_static( PARMS(Bit32u) );
+
+#endif
xen-unstable.hg/tools/firmware/rombios/32bitgateway.c new file +486 -0

486 lines not on this page · raw message

xen-unstable.hg/tools/firmware/rombios/32bitgateway.h new file +18 -0
Index: root/xen-unstable.hg/tools/firmware/rombios/32bitgateway.h
===================================================================
@@ -0,0 +1,18 @@
+#ifndef GATEWAY
+#define GATEWAY
+
+#include "32bitprotos.h"
+
+void test_gateway();
+
+/* extension for the EBDA */
+typedef struct {
+  Bit16u reg_ss;
+  Bit16u reg_cs;
+  Bit16u reg_ds;
+  Bit16u reg_es;
+  Bit16u esp_hi;
+  Bit16u retaddr;
+} upcall_t;
+
+#endif
xen-unstable.hg/tools/firmware/hvmloader/mp_tables.c +1 -10
Index: root/xen-unstable.hg/tools/firmware/hvmloader/mp_tables.c
===================================================================
@@ -45,12 +45,6 @@ typedef unsigned long uint64_t;
 typedef   signed long int64_t;
 #endif
 
-#define ROMBIOS_SEG            0xF000
-#define ROMBIOS_BEGIN          0x000F0000
-#define ROMBIOS_SIZE           0x00010000 
-#define ROMBIOS_MAXOFFSET      0x0000FFFF
-#define ROMBIOS_END            (ROMBIOS_BEGIN + ROMBIOS_SIZE)
-
 /* number of non-processor MP table entries */
 #define NR_NONPROC_ENTRIES     18
 
@@ -311,10 +305,7 @@ void* get_mp_table_start(void)
           bios_mem != (char *)ROMBIOS_END; 
           bios_mem++ )
     {
-        if ( bios_mem[0] == '_' && bios_mem[1] == '_' &&
-             bios_mem[2] == '_' && bios_mem[3] == 'H' &&
-             bios_mem[4] == 'V' && bios_mem[5] == 'M' &&
-             bios_mem[6] == 'M' && bios_mem[7] == 'P' )
+        if ( strncmp(bios_mem, "___HVMMP", 8) == 0)
             return bios_mem;
     }
 
xen-unstable.hg/tools/firmware/hvmloader/config.h +7 -0
Index: root/xen-unstable.hg/tools/firmware/hvmloader/config.h
===================================================================
@@ -11,4 +11,11 @@
 #define PCI_ISA_DEVFN       0x08    /* dev 1, fn 0 */
 #define PCI_ISA_IRQ_MASK    0x0c60U /* ISA IRQs 5,6,10,11 are PCI connected */
 
+#define ROMBIOS_SEG            0xF000
+#define ROMBIOS_BEGIN          0x000F0000
+#define ROMBIOS_SIZE           0x00010000
+#define ROMBIOS_MAXOFFSET      0x0000FFFF
+#define ROMBIOS_END            (ROMBIOS_BEGIN + ROMBIOS_SIZE)
+
+
 #endif /* __HVMLOADER_CONFIG_H__ */
xen-unstable.hg/tools/firmware/rombios/Makefile +1 -1
Index: root/xen-unstable.hg/tools/firmware/rombios/Makefile
===================================================================
@@ -12,7 +12,7 @@ clean:
 	rm -f  rombios*.txt rombios*.sym usage biossums
 	rm -f  BIOS-bochs-*
 
-BIOS-bochs-latest: rombios.c biossums
+BIOS-bochs-latest: rombios.c biossums 32bitgateway.c
 	gcc -DBX_SMP_PROCESSORS=1 -E -P $< > _rombios_.c
 	bcc -o rombios.s -C-c -D__i86__ -0 -S _rombios_.c
 	sed -e 's/^\.text//' -e 's/^\.data//' rombios.s > _rombios_.s
xen-unstable.hg/tools/firmware/rombios/32bit/jumptable.h new file +11 -0
Index: root/xen-unstable.hg/tools/firmware/rombios/32bit/jumptable.h
===================================================================
@@ -0,0 +1,11 @@
+#ifndef JUMPTABLE_H
+#define JUMPTABLE_H
+
+/*
+   name of the section the 32bit BIOS must have and where the array of
+   function poiners is built; hvmloader looks for this section and copies
+   it into the lower BIOS in the 0xf000 segment
+ */
+#define JUMPTABLE_SECTION_NAME ".biosjumptable"
+
+#endif
xen-unstable.hg/tools/firmware/hvmloader/util.c +9 -0
Index: root/xen-unstable.hg/tools/firmware/hvmloader/util.c
===================================================================
@@ -91,6 +91,15 @@ int strcmp(const char *cs, const char *c
     return res;
 }
 
+int strncmp(const char *s1, const char *s2, uint32_t n)
+{
+	uint32_t ctr;
+	for (ctr = 0; ctr < n; ctr++)
+		if (s1[ctr] != s2[ctr])
+			return (int)(s1[ctr] - s2[ctr]);
+	return 0;
+}
+
 void *memcpy(void *dest, const void *src, unsigned n)
 {
     int t0, t1, t2;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel