Skip to contentSkip to conversation

Add optionrom compatible with fw_cfg DMA version

PATCH Superseded Marc Marí 2 participants 4 messages Jan 8, 2016

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

PATCH 1/1 Add optionrom compatible with fw_cfg DMA version

This optionrom is based on linuxboot.S.

---
 .gitignore                        |   4 +
 hw/i386/pc.c                      |   9 +-
 hw/nvram/fw_cfg.c                 |   2 +-
 include/hw/nvram/fw_cfg.h         |   1 +
 pc-bios/optionrom/Makefile        |   8 +-
 pc-bios/optionrom/linuxboot_dma.c | 338 ++++++++++++++++++++++++++++++++++++++
 pc-bios/optionrom/optionrom.h     |   4 +-
 7 files changed, 361 insertions(+), 5 deletions(-)
 create mode 100644 pc-bios/optionrom/linuxboot_dma.c
.gitignore +4 -0
index 88a80ff..101d1e0 100644
@@ -94,6 +94,10 @@
 /pc-bios/optionrom/linuxboot.bin
 /pc-bios/optionrom/linuxboot.raw
 /pc-bios/optionrom/linuxboot.img
+/pc-bios/optionrom/linuxboot_dma.asm
+/pc-bios/optionrom/linuxboot_dma.bin
+/pc-bios/optionrom/linuxboot_dma.raw
+/pc-bios/optionrom/linuxboot_dma.img
 /pc-bios/optionrom/multiboot.asm
 /pc-bios/optionrom/multiboot.bin
 /pc-bios/optionrom/multiboot.raw
hw/i386/pc.c +7 -2
index 459260b..00339fa 100644
@@ -1007,8 +1007,13 @@ static void load_linux(PCMachineState *pcms,
     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
     fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
 
-    option_rom[nb_option_roms].name = "linuxboot.bin";
-    option_rom[nb_option_roms].bootindex = 0;
+    if (fw_cfg_dma_enabled(fw_cfg)) {
+        option_rom[nb_option_roms].name = "linuxboot_dma.bin";
+        option_rom[nb_option_roms].bootindex = 0;
+    } else {
+        option_rom[nb_option_roms].name = "linuxboot.bin";
+        option_rom[nb_option_roms].bootindex = 0;
+    }
     nb_option_roms++;
 }
 
hw/nvram/fw_cfg.c +1 -1
index a1d650d..d0a5753 100644
@@ -546,7 +546,7 @@ static bool is_version_1(void *opaque, int version_id)
     return version_id == 1;
 }
 
-static bool fw_cfg_dma_enabled(void *opaque)
+bool fw_cfg_dma_enabled(void *opaque)
 {
     FWCfgState *s = opaque;
 
include/hw/nvram/fw_cfg.h +1 -0
index 664eaf6..953e58d 100644
@@ -219,6 +219,7 @@ FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
                                  hwaddr dma_addr, AddressSpace *dma_as);
 
 FWCfgState *fw_cfg_find(void);
+bool fw_cfg_dma_enabled(void *opaque);
 
 #endif /* NO_QEMU_PROTOS */
 
pc-bios/optionrom/Makefile +7 -1
index ce4852a..076f351 100644
@@ -2,6 +2,7 @@ all: build-all
 # Dummy command so that make thinks it has done something
 	@true
 
+BULD_DIR=$(CURDIR)
 include ../../config-host.mak
 include $(SRC_PATH)/rules.mak
 
@@ -11,15 +12,20 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/optionrom)
 
 CFLAGS := -Wall -Wstrict-prototypes -Werror -fomit-frame-pointer -fno-builtin
 CFLAGS += -I$(SRC_PATH)
+CFLAGS += -I$(SRC_PATH)/include
 CFLAGS += $(call cc-option, $(CFLAGS), -fno-stack-protector)
 CFLAGS += $(CFLAGS_NOPIE)
+CFLAGS += -m32
 QEMU_CFLAGS = $(CFLAGS)
 
-build-all: multiboot.bin linuxboot.bin kvmvapic.bin
+build-all: multiboot.bin linuxboot.bin linuxboot_dma.bin kvmvapic.bin
 
 # suppress auto-removal of intermediate files
 .SECONDARY:
 
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m elf_i386 -static -Ttext 0 -e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")
+
 %.img: %.o
 	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -Ttext 0 -e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")
 
pc-bios/optionrom/linuxboot_dma.c new file +338 -0

338 lines not on this page · raw message

pc-bios/optionrom/optionrom.h +3 -1
index f1a9021..25c765f 100644
@@ -29,7 +29,8 @@
 #define DEBUG_HERE \
 	jmp		1f;				\
 	1:
-	
+
+#ifndef C_CODE
 /*
  * Read a variable from the fw_cfg device.
  * Clobbers:	%edx
@@ -49,6 +50,7 @@
 	inb		(%dx), %al
 	bswap		%eax
 .endm
+#endif
 
 #define read_fw_blob_pre(var)				\
 	read_fw		var ## _SIZE;			\
Signature · 1 line
2.4.3
#1 lore raw

pc-bios/optionrom/linuxboot_dma.c line 129

Quoted 8 lines · Marc Marí
pc-bios/optionrom/linuxboot_dma.c new file +7 -0
+static inline uint16_t readw_addr32(const void *addr) {
+    uint16_t val;
+    asm("addr32 movw %1, %0" : "=r"(val) : "g"(addr));
+    barrier();
+    return val;
+}
+

Does SeaBIOS ensure that DS base is zero here?

Quoted 12 lines
pc-bios/optionrom/linuxboot_dma.c new file +12 -0
+static void transition32(void)
+{
+    extern void *gdt;
+    uint32_t data_segment;
+    struct length_addr rombios_gdt;
+
+    data_segment = read_ds();
+    rombios_gdt.addr = (uint32_t)((data_segment << 4) + (uint32_t)(&gdt));
+    rombios_gdt.length = (3 * 8) - 1;
+
+    /* Load GDT */
+    asm("data32 lgdt %0" : : "m"(rombios_gdt): "memory");

pc-bios/optionrom/linuxboot_dma.c line 240

Quoted 10 lines
pc-bios/optionrom/linuxboot_dma.c new file +10 -0
+
+	/* Get us to protected mode and set ES to a 32 bit segment */
+    asm("mov $1, %%eax\n"
+        "mov %%eax, %%cr0\n"
+        "mov $0x10, %%eax\n"
+        "mov %%eax, %%es\n"
+        : : : "eax");
+
+    /* We're now running in 16-bit CS, but 32-bit ES! */
+}

Do you even need to go to 32-bit mode? The only reason to do so in the original ROM was to do a "rep insb" above 1 GB, but here fw_cfg can do DMA to high addresses for you.

Paolo

author
#2 lore raw
Quoted 4 lines · On Fri, 8 Jan 2016 16:54:07 +0100 Paolo Bonzini <[email protected]> wrote:
Quoted 1 line · Marc Marí
Quoted 9 lines

pc-bios/optionrom/linuxboot_dma.c line 129

Quoted 7 lines
pc-bios/optionrom/linuxboot_dma.c new file +7 -0
+static inline uint16_t readw_addr32(const void *addr) {
+    uint16_t val;
+    asm("addr32 movw %1, %0" : "=r"(val) : "g"(addr));
+    barrier();
+    return val;
+}
+

Does SeaBIOS ensure that DS base is zero here?

DS = CS:

" movw %cs, %ax\n"
" movw %ax, %ds\n"

At the beginning of the ROM.

Quoted 20 lines
Quoted 20 lines
pc-bios/optionrom/linuxboot_dma.c new file +8 -0
+static void transition32(void)
+{
+    extern void *gdt;
+    uint32_t data_segment;
+    struct length_addr rombios_gdt;
+
+    data_segment = read_ds();
+    rombios_gdt.addr = (uint32_t)((data_segment << 4) +

(uint32_t)(&gdt));

pc-bios/optionrom/linuxboot_dma.c new file +6 -0
+    rombios_gdt.length = (3 * 8) - 1;
+
+    /* Load GDT */
+    asm("data32 lgdt %0" : : "m"(rombios_gdt): "memory");
+
+	/* Get us to protected mode and set ES to a 32 bit segment

*/

pc-bios/optionrom/linuxboot_dma.c new file +4 -0
+    asm("mov $1, %%eax\n"
+        "mov %%eax, %%cr0\n"
+        "mov $0x10, %%eax\n"
+        "mov %%eax, %%es\n"
Quoted 8 lines

pc-bios/optionrom/linuxboot_dma.c line 240

Quoted 4 lines
pc-bios/optionrom/linuxboot_dma.c new file +4 -0
+        : : : "eax");
+
+    /* We're now running in 16-bit CS, but 32-bit ES! */
+}

Do you even need to go to 32-bit mode? The only reason to do so in the original ROM was to do a "rep insb" above 1 GB, but here fw_cfg can do DMA to high addresses for you.

There's no need of course. I was looking at the original ROM and I did not think this through.

Thanks for your comments
Marc

#3 lore raw
Quoted 5 lines · Marc Marí
Quoted 3 lines
Quoted 3 lines

Do you even need to go to 32-bit mode? The only reason to do so in the original ROM was to do a "rep insb" above 1 GB, but here fw_cfg can do DMA to high addresses for you.

There's no need of course. I was looking at the original ROM and I did not think this through.

Nice, this should make the code much simpler!

Paolo