Skip to contentSkip to conversation

Add optionrom compatible with fw_cfg DMA version

PATCH Applied Richard W.M. Jones 5 participants 14 messages Apr 25, 2016 – May 10, 2016

v6 of 9 Applied Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 Add optionrom compatible with fw_cfg DMA version

v5 -> v6:

- Changed the xen_load_linux assertion as suggested by Stefan.

- I renamed the variables in get_e801_addr function, since the registers were really (16 bit 8086-style) AX, not EAX etc. Also I changed the GCC asm to make it a little bit more efficient. I verified by disassembling the function that GCC is still generating the right code after this change.

- Re-tested with small (342K) libguestfs initramfs and with large (20M) Fedora initramfs, and works fine in both cases.

Rich.

#1 lore raw

PATCH v6 1/1 Add optionrom compatible with fw_cfg DMA version

From: Marc Marí <[email protected]>

This optionrom is based on linuxboot.S.

---
 .gitignore                        |   4 +
 hw/i386/pc.c                      |  10 +-
 hw/nvram/fw_cfg.c                 |   2 +-
 include/hw/nvram/fw_cfg.h         |   1 +
 pc-bios/optionrom/Makefile        |  13 +-
 pc-bios/optionrom/linuxboot_dma.c | 302 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 327 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 +8 -2
index 99437e0..098d571 100644
@@ -999,8 +999,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++;
 }
 
@@ -1263,6 +1268,7 @@ void xen_load_linux(PCMachineState *pcms)
     load_linux(pcms, fw_cfg);
     for (i = 0; i < nb_option_roms; i++) {
         assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
+               !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
                !strcmp(option_rom[i].name, "multiboot.bin"));
         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
     }
hw/nvram/fw_cfg.c +1 -1
index 999f480..114aea8 100644
@@ -551,7 +551,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 d008112..5c27a1f 100644
@@ -182,5 +182,6 @@ 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
pc-bios/optionrom/Makefile +11 -2
index ce4852a..d44a62a 100644
@@ -13,15 +13,24 @@ CFLAGS := -Wall -Wstrict-prototypes -Werror -fomit-frame-pointer -fno-builtin
 CFLAGS += -I$(SRC_PATH)
 CFLAGS += $(call cc-option, $(CFLAGS), -fno-stack-protector)
 CFLAGS += $(CFLAGS_NOPIE)
+CFLAGS += -m32
 QEMU_CFLAGS = $(CFLAGS)
 
-build-all: multiboot.bin linuxboot.bin kvmvapic.bin
+ASFLAGS += -32
+
+build-all: multiboot.bin linuxboot.bin linuxboot_dma.bin kvmvapic.bin
 
 # suppress auto-removal of intermediate files
 .SECONDARY:
 
+ifdef CONFIG_WIN32
+LD_EMULATION = i386pe
+else
+LD_EMULATION = elf_i386
+endif
+
 %.img: %.o
-	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -Ttext 0 -e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m $(LD_EMULATION) -Ttext 0 -e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")
 
 %.raw: %.img
 	$(call quiet-command,$(OBJCOPY) -O binary -j .text $< $@,"  Building $(TARGET_DIR)$@")
pc-bios/optionrom/linuxboot_dma.c new file +302 -0

302 lines not on this page · raw message

Signature · 1 line
2.7.4
Reviewed-by on v6 1/1
#3 lore raw
In reply to Richard W.M. Jones
Quoted 10 lines · Richard W.M. Jones

v5 -> v6:

- Changed the xen_load_linux assertion as suggested by Stefan.

- I renamed the variables in get_e801_addr function, since the registers were really (16 bit 8086-style) AX, not EAX etc. Also I changed the GCC asm to make it a little bit more efficient. I verified by disassembling the function that GCC is still generating the right code after this change.

Quoted 6 lines

- Re-tested with small (342K) libguestfs initramfs and with large (20M) Fedora initramfs, and works fine in both cases.

Rich.

Reviewed-by on v6 1/1
#2 lore raw
In reply to Richard W.M. Jones
Quoted 13 lines · On Mon, 25 Apr 2016 17:04:41 +0100 "Richard W.M. Jones" <[email protected]>…

From: Marc Marí <[email protected]>

This optionrom is based on linuxboot.S.

---
 .gitignore                        |   4 +
 hw/i386/pc.c                      |  10 +-
 hw/nvram/fw_cfg.c                 |   2 +-
 include/hw/nvram/fw_cfg.h         |   1 +
 pc-bios/optionrom/Makefile        |  13 +-
 pc-bios/optionrom/linuxboot_dma.c | 302
Quoted 3 lines

++++++++++++++++++++++++++++++++++++++ 6 files changed, 327 insertions(+), 5 deletions(-) create mode 100644 pc-bios/optionrom/linuxboot_dma.c

Not sure if I should send my reviewed-by if it's already signed-off by me, but here it goes:

#4 lore raw
In reply to Richard W.M. Jones
Quoted 4 lines · Richard W.M. Jones

v5 -> v6:

- Changed the xen_load_linux assertion as suggested by Stefan.

Quoted 8 lines

- I renamed the variables in get_e801_addr function, since the registers were really (16 bit 8086-style) AX, not EAX etc. Also I changed the GCC asm to make it a little bit more efficient. I verified by disassembling the function that GCC is still generating the right code after this change.

- Re-tested with small (342K) libguestfs initramfs and with large (20M) Fedora initramfs, and works fine in both cases.

No one has picked this up, so I have (for QEMU 2.7).

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan

#5 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Richard W.M. Jones

v5 -> v6:

- Changed the xen_load_linux assertion as suggested by Stefan.

- I renamed the variables in get_e801_addr function, since the registers were really (16 bit 8086-style) AX, not EAX etc. Also I changed the GCC asm to make it a little bit more efficient. I verified by disassembling the function that GCC is still generating the right code after this change.

Quoted 7 lines
Quoted 2 lines

- Re-tested with small (342K) libguestfs initramfs and with large (20M) Fedora initramfs, and works fine in both cases.

No one has picked this up, so I have (for QEMU 2.7).

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

The bad news is this patch breaks the build under clang:

CC optionrom/linuxboot_dma.o
<inline asm>:29:1: error: unexpected directive .code16gcc .code16gcc
^
<inline asm>:29:11: error: .code16gcc not supported yet .code16gcc
^
linuxboot_dma.c:104:9: error: unexpected token in argument list asm("addr32 movw %%es:(%1), %0" : "=r"(val) : "r"((uint32_t)offset)); ^
<inline asm>:1:17: note: instantiated into assembly here addr32 movw %es:(%eax), %cx
^
linuxboot_dma.c:120:9: error: invalid instruction mnemonic 'addr32' asm("addr32 movl %0, %%es:(%1)" : : "r"(val), "r"((uint32_t)offset)); ^
<inline asm>:1:2: note: instantiated into assembly here addr32 movl %eax, %es:(%edx)
^~~~~~
linuxboot_dma.c:112:9: error: unexpected token in argument list asm("addr32 movl %%es:(%1), %0" : "=r"(val) : "r"((uint32_t)offset)); ^
<inline asm>:1:17: note: instantiated into assembly here addr32 movl %es:(%eax), %eax
^

The .code16gcc issue can probably be resolved with -m16 (which gcc 4.9+ also supports!). I haven't looked into the other issues though.

Dropped from my block tree.

Stefan

#6 lore raw
Quoted 21 lines · Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Richard W.M. Jones

v5 -> v6:

- Changed the xen_load_linux assertion as suggested by Stefan.

- I renamed the variables in get_e801_addr function, since the registers were really (16 bit 8086-style) AX, not EAX etc. Also I changed the GCC asm to make it a little bit more efficient. I verified by disassembling the function that GCC is still generating the right code after this change.

Quoted 7 lines
Quoted 2 lines

- Re-tested with small (342K) libguestfs initramfs and with large (20M) Fedora initramfs, and works fine in both cases.

No one has picked this up, so I have (for QEMU 2.7).

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

The bad news is this patch breaks the build under clang:

[...]

Quoted 2 lines

The .code16gcc issue can probably be resolved with -m16 (which gcc 4.9+ also supports!). I haven't looked into the other issues though.

It looks as if the easiest way to get around this will be to use gas instead of clang's integrated assembler, ie:

CFLAGS += -m16 -fno-integrated-as

However I have no idea if there are platforms we support which use clang and require the integrated assembler instead of binutils. Opinions on this?

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
#7 lore raw
In reply to Stefan Hajnoczi

Actually there's a rather more fundamental problem. In the current linuxboot_dma.c we use asm statements at the top and bottom of the file (outside any function). The asm statements define the header and what I assume is the footer of the file. At any rate, they encode the size of the file (the calculation `.byte (_end - _start) / 512').

Clang just rearranges everything in the file, so the _start and _end asm snippets appear together at the beginning of the input to the assembler, and nothing works after that.

So that pretty much screws up the whole project of trying to write an option ROM in C.

Of course we're well outside any standards here. Can we tell clang users to use the GCC/pre-compiled option ROMs :-? Any other ideas? I don't think I've missed a flag (GCC has -fno-toplevel-reorder, but clang 3.8 doesn't ...)

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
#8 lore raw
Quoted 11 lines · Richard W.M. Jones

Actually there's a rather more fundamental problem. In the current linuxboot_dma.c we use asm statements at the top and bottom of the file (outside any function). The asm statements define the header and what I assume is the footer of the file. At any rate, they encode the size of the file (the calculation `.byte (_end - _start) / 512').

Clang just rearranges everything in the file, so the _start and _end asm snippets appear together at the beginning of the input to the assembler, and nothing works after that.

Quoted 7 lines

So that pretty much screws up the whole project of trying to write an option ROM in C.

Of course we're well outside any standards here. Can we tell clang users to use the GCC/pre-compiled option ROMs :-? Any other ideas? I don't think I've missed a flag (GCC has -fno-toplevel-reorder, but clang 3.8 doesn't ...)

IIRC for a while we used to ship pre-compiled ACPI byte-code, for people who didn't have "iasl" installed.

We could make this C source file gcc-only, and provide a pre-compiled assembly source file for those platforms that only have clang.

Laszlo

#9 lore raw

FWIW the response from the LLVM developers:

https://llvm.org/bugs/show_bug.cgi?id=27688

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
#11 lore raw
In reply to Richard W.M. Jones
Quoted 5 lines · Richard W.M. Jones

Of course we're well outside any standards here. Can we tell clang users to use the GCC/pre-compiled option ROMs :-? Any other ideas? I don't think I've missed a flag (GCC has -fno-toplevel-reorder, but clang 3.8 doesn't ...)

I guess the checksumming script (scripts/signrom.py) could take care of padding the file to a multiple of 512 bytes, and fill in the size in the third byte. Then "_end" would not be necessary anymore and -m16 could replace the .code16 directive.

Paolo

#12 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Richard W.M. Jones

Of course we're well outside any standards here. Can we tell clang users to use the GCC/pre-compiled option ROMs :-? Any other ideas? I don't think I've missed a flag (GCC has -fno-toplevel-reorder, but clang 3.8 doesn't ...)

Quoted 4 lines

I guess the checksumming script (scripts/signrom.py) could take care of padding the file to a multiple of 512 bytes, and fill in the size in the third byte. Then "_end" would not be necessary anymore and -m16 could replace the .code16 directive.

In my rather limited testing on gcc, gcc -m16 broke booting. However I've not investigated this further. I'll do so shortly.

However I have a question: is there a formal standard or documentation for the option ROM format? Are we sticking to the (ancient) "BIOS Boot Specification" or is there something newer? (My copy is from 1996).

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
#13 lore raw
Quoted 15 lines · Richard W.M. Jones
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Richard W.M. Jones

Of course we're well outside any standards here. Can we tell clang users to use the GCC/pre-compiled option ROMs :-? Any other ideas? I don't think I've missed a flag (GCC has -fno-toplevel-reorder, but clang 3.8 doesn't ...)

Quoted 4 lines

I guess the checksumming script (scripts/signrom.py) could take care of padding the file to a multiple of 512 bytes, and fill in the size in the third byte. Then "_end" would not be necessary anymore and -m16 could replace the .code16 directive.

Quoted 6 lines

In my rather limited testing on gcc, gcc -m16 broke booting. However I've not investigated this further. I'll do so shortly.

However I have a question: is there a formal standard or documentation for the option ROM format? Are we sticking to the (ancient) "BIOS Boot Specification" or is there something newer? (My copy is from 1996).

To my knowledge, the most recent specification that describes PCI expansion ROMs is:

PCI Firmware Specification
Revision 3.2
January 26, 2015

See Chapter 5, "PCI Expansion ROMs".

(Maybe a newer release has been made, not sure.)

Of course, this spec is not public (you have to be a PCI SIG member to get it, or some such, which costs $$$), but since Red Hat is a member, I'll send you a link off-list (and you can't share the PDF outside of RH, of course).

Regarding general (device independent) oproms: I'm not sure.

Thanks
Laszlo