Skip to contentSkip to conversation

Add optionrom compatible with fw_cfg DMA version

PATCH Superseded Marc Marí 5 participants 13 messages Jan 25, 2016 – Jan 28, 2016

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

PATCH v3 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        |   6 +-
 pc-bios/optionrom/linuxboot_dma.c | 262 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 280 insertions(+), 4 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 +5 -1
index ce4852a..8c769ef 100644
@@ -13,13 +13,17 @@ 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
+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 -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 +262 -0

262 lines not on this page · raw message

Signature · 1 line
2.4.3
Reviewed-by on v3 1/1
#1 lore raw

pc-bios/optionrom/Makefile CFLAGS := -Wall -Wstrict-prototypes -Werror -fomit-frame-pointer -fno-builtin · line 28

Quoted 5 lines · Marc Marí
pc-bios/optionrom/Makefile +3 -0
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m elf_i386 -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)$@")

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

Otherwise:

author
#2 lore raw
Quoted 10 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 6 lines · Marc Marí
(quoted diff) +2 -1
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m elf_i386
-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)$@")

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Marc

#3 lore raw
Quoted 14 lines · Marc Marí
Quoted 10 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 6 lines · Marc Marí
(quoted diff) +2 -1
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m elf_i386
-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)$@")

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

Quoted 6 lines

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Any chance the linker needs -m32 too?

cheers,
Gerd

#4 lore raw
Quoted 15 lines · Gerd Hoffmann
Quoted 14 lines · Marc Marí
Quoted 10 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 6 lines · Marc Marí
(quoted diff) +2 -1
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m elf_i386
-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)$@")

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

Quoted 8 lines
Quoted 6 lines

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Any chance the linker needs -m32 too?

I wonder why this isn't a problem for the existing firmware code. Are we really building x86_64 ELF files for our firmware?

Stefan

author
#5 lore raw
Quoted 19 lines · On Wed, 27 Jan 2016 16:43:29 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 18 lines · Gerd Hoffmann
Quoted 17 lines · Marc Marí
Quoted 10 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:

Review

Quoted 6 lines · Marc Marí
(quoted diff) +2 -0
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m

elf_i386 -Ttext 0 -e _start -s -o $@ $<," Building $(TARGET_DIR)$@") + %.img: %.o

(quoted diff) +0 -1
 	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -Ttext 0
-e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")    

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Any chance the linker needs -m32 too?

I wonder why this isn't a problem for the existing firmware code. Are we really building x86_64 ELF files for our firmware?

Yes they are x86_64:

pc-bios/optionrom/linuxboot.img: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

But as they are written directly in assembly, it does not give any problem. Whereas when mixing C and ASM, it does give problems.

Marc

#6 lore raw
Quoted 31 lines · Marc Marí
Quoted 19 lines · On Wed, 27 Jan 2016 16:43:29 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 18 lines · Gerd Hoffmann
Quoted 17 lines · Marc Marí
Quoted 10 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:

Review

Quoted 6 lines · Marc Marí
(quoted diff) +2 -0
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m

elf_i386 -Ttext 0 -e _start -s -o $@ $<," Building $(TARGET_DIR)$@") + %.img: %.o

(quoted diff) +0 -1
 	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -Ttext 0
-e _start -s -o $@ $<,"  Building $(TARGET_DIR)$@")    

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Any chance the linker needs -m32 too?

I wonder why this isn't a problem for the existing firmware code. Are we really building x86_64 ELF files for our firmware?

Quoted 7 lines

Yes they are x86_64:

pc-bios/optionrom/linuxboot.img: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

But as they are written directly in assembly, it does not give any problem. Whereas when mixing C and ASM, it does give problems.

Would it hurt anything to add "-m elf_i386" to the generic assembler rule and then use that for both targets? (Just to keep the makefile a little simpler.)

-Kevin

author
#7 lore raw
Quoted 39 lines · On Wed, 27 Jan 2016 12:17:27 -0500 "Kevin O'Connor" <[email protected]> wrote:
Quoted 38 lines · Marc Marí
Quoted 21 lines · On Wed, 27 Jan 2016 16:43:29 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 20 lines · Gerd Hoffmann
Quoted 19 lines · Marc Marí
Quoted 12 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 7 lines · Marc Marí
(quoted diff) +2 -0
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m

elf_i386 -Ttext 0 -e _start -s -o $@ $<," Building $(TARGET_DIR)$@") + %.img: %.o

(quoted diff) +0 -1
 	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE)
-Ttext 0 -e _start -s -o $@ $<,"  Building

$(TARGET_DIR)$@")

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Any chance the linker needs -m32 too?

I wonder why this isn't a problem for the existing firmware code. Are we really building x86_64 ELF files for our firmware?

Yes they are x86_64:

pc-bios/optionrom/linuxboot.img: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

Quoted 6 lines
Quoted 2 lines

But as they are written directly in assembly, it does not give any problem. Whereas when mixing C and ASM, it does give problems.

Would it hurt anything to add "-m elf_i386" to the generic assembler rule and then use that for both targets? (Just to keep the makefile a little simpler.)

It doesn't seem to hurt, and it is simpler.

Marc

#8 lore raw
Quoted 42 lines · Marc Marí
Quoted 39 lines · On Wed, 27 Jan 2016 12:17:27 -0500 "Kevin O'Connor" <[email protected]> wrote:
Quoted 38 lines · Marc Marí
Quoted 21 lines · On Wed, 27 Jan 2016 16:43:29 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 20 lines · Gerd Hoffmann
Quoted 19 lines · Marc Marí
Quoted 12 lines · On Tue, 26 Jan 2016 11:11:54 +0000 Stefan Hajnoczi <[email protected]> wrote:
Quoted 7 lines · Marc Marí
(quoted diff) +2 -0
+linuxboot_dma.img: linuxboot_dma.o
+	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE) -m

elf_i386 -Ttext 0 -e _start -s -o $@ $<," Building $(TARGET_DIR)$@") + %.img: %.o

(quoted diff) +0 -1
 	$(call quiet-command,$(LD) $(LDFLAGS_NOPIE)
-Ttext 0 -e _start -s -o $@ $<,"  Building

$(TARGET_DIR)$@")

Why is -m elf_i386 necessary for linuxboot_dma.img but not for the other *.img files?

I cannot give a precise explanation. But if I don't force an output type, I get this error:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Building optionrom/linuxboot_dma.img
ld: i386 architecture of input file `linuxboot_dma.o' is incompatible with i386:x86-64 output

Any chance the linker needs -m32 too?

I wonder why this isn't a problem for the existing firmware code. Are we really building x86_64 ELF files for our firmware?

Yes they are x86_64:

pc-bios/optionrom/linuxboot.img: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

But as they are written directly in assembly, it does not give any problem. Whereas when mixing C and ASM, it does give problems.

Would it hurt anything to add "-m elf_i386" to the generic assembler rule and then use that for both targets? (Just to keep the makefile a little simpler.)

It doesn't seem to hurt, and it is simpler.

Great, I think this makes sense.

Stefan

#9 lore raw
In reply to Marc Marí
Quoted 1 line · Marc Marí

This optionrom is based on linuxboot.S.

Hi Marc,

Out of curiousity, how does the timing with this option rom compare to the previous SeaBIOS patches that implemented linux dma loading?

When I first tried to compile this (on fc23), I got:

In file included from /usr/include/features.h:389:0,

                 from /usr/include/stdint.h:25,
                 from /usr/lib/gcc/x86_64-redhat-linux/5.3.1/include/stdint.h:9,
                 from linuxboot_dma.c:62:

/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory compilation terminated.

which I fixed by running "dnf install glibc-devel.i686". Is a configure check needed?

See further comments below.

[...]

Quoted 52 lines
pc-bios/optionrom/linuxboot_dma.c new file +49 -0
@@ -0,0 +1,262 @@
+/*
+ * Linux Boot Option ROM for fw_cfg DMA
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2015 Red Hat Inc.
+ *   Authors: Marc Marí <[email protected]>
+ */
+
+asm(
+".text\n"
+".global _start\n"
+"_start:\n"
+"   .short	0xaa55\n"
+"   .byte (_end - _start) / 512\n"
+"   lret\n"
+"   .org 0x18\n"
+"   .short 0\n"
+"   .short _pnph\n"
+"_pnph:\n"
+"   .ascii \"$PnP\"\n"
+"   .byte 0x01\n"
+"   .byte ( _pnph_len / 16 )\n"
+"   .short 0x0000\n"
+"   .byte 0x00\n"
+"   .byte 0x00\n"
+"   .long 0x00000000\n"
+"   .short _manufacturer\n"
+"   .short _product\n"
+"   .long 0x00000000\n"
+"   .short 0x0000\n"
+"   .short 0x0000\n"
+"   .short _bev\n"
+"   .short 0x0000\n"
+"   .short 0x0000\n"
+"   .equ _pnph_len, . - _pnph\n"
+"   .align 4, 0\n"
+"_bev:\n"

pc-bios/optionrom/linuxboot_dma.c line 59

Quoted 10 lines
pc-bios/optionrom/linuxboot_dma.c new file +10 -0
+".code16gcc\n"
+/* DS = CS */
+"   movw %cs, %ax\n"
+"   movw %ax, %ds\n"
+"   movl %esp, %ebp\n"
+"run_linuxboot:\n"
+"   cli\n"
+"   cld\n"
+"   jmp load_kernel\n"
+);

The run_linuxboot label doesn't seem to be used anywhere.

[...]

Quoted 8 lines
pc-bios/optionrom/linuxboot_dma.c new file +8 -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;
+}
+
+static inline uint32_t readl_addr32(const void *addr) {

pc-bios/optionrom/linuxboot_dma.c line 77

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

The above does not look correct to me. Since the code is running in 16bit mode the above memory accesses are relative to the %ds segment. Because %ds=%cs this is going to access a different address than expected.

What I think you want to do is assign %es=setup_addr>>4 and then perform the read at the given offset (eg, 0x206).

[...]

Quoted 8 lines
pc-bios/optionrom/linuxboot_dma.c new file +8 -0
+static void bios_cfg_read_entry(void *buf, uint16_t entry, uint32_t len)
+{
+    FWCfgDmaAccess access;
+    uint32_t control = (entry << 16) | BIOS_CFG_DMA_CTL_SELECT
+                        | BIOS_CFG_DMA_CTL_READ;
+
+    access.address = cpu_to_be64((uint64_t)(uint32_t)buf);
+    access.length = cpu_to_be32(len);

pc-bios/optionrom/linuxboot_dma.c line 95

Quoted 10 lines
pc-bios/optionrom/linuxboot_dma.c new file +10 -0
+    access.control = cpu_to_be32(control);
+
+    barrier();
+
+    outl(cpu_to_be32((uint32_t)&access), BIOS_CFG_DMA_ADDR_LOW);
+
+    while(be32_to_cpu(access.control) & ~BIOS_CFG_DMA_CTL_ERROR) {
+        barrier();
+    }
+}

FYI, I think with a small incremental patch (see below) one could entirely replace the existing linuxboot.rom with your new code.

The one caveat is that this patch requires that kvm support "big real mode" and I know there were quirks with that on some older Intel chips. However, I think the "insb" instruction would trap anyway, so maybe it's not an issue.

-Kevin

pc-bios/optionrom/linuxboot_dma.c +21 -0
@@ -73,6 +73,8 @@ asm(
 #define BIOS_CFG_DMA_CTL_SKIP    0x04
 #define BIOS_CFG_DMA_CTL_SELECT  0x08
 
+#define BIOS_CFG_CTL           0x510
+#define BIOS_CFG_DATA          0x511
 #define BIOS_CFG_DMA_ADDR_HIGH 0x514
 #define BIOS_CFG_DMA_ADDR_LOW  0x518
 
@@ -87,6 +89,16 @@ typedef struct FWCfgDmaAccess {
     uint64_t address;
 } __attribute__((packed)) FWCfgDmaAccess;
 
+static inline void outw(uint16_t value, uint16_t port) {
+    asm("outw %w0, %w1" : : "a"(value), "Nd"(port));
+}
+
+static inline uint32_t inl(uint16_t port) {
+    uint32_t value;
+    __asm__ __volatile__("inl %w1, %0" : "=a"(value) : "Nd"(port));
+    return value;
+}
+
 static inline void outl(uint32_t value, uint16_t port) {
     asm("outl %0, %w1" : : "a"(value), "Nd"(port));
 }
@@ -124,6 +136,15 @@ static inline uint32_t be32_to_cpu(uint32_t x) {
 
 static void bios_cfg_read_entry(void *buf, uint16_t entry, uint32_t len)
 {
+    if (inl(BIOS_CFG_DMA_ADDR_LOW) != 0x47464320) {
+        // Legacy PIO fw_cfg
+        outw(entry, BIOS_CFG_CTL);
+        asm volatile("movw %w0, %%es" :: "r"(0) : "memory");
+        asm volatile("rep insb (%%dx), %%es:(%%edi)"
+                     : "+c"(len), "+D"(buf) : "d"(BIOS_CFG_DATA) : "memory");
+        return;
+    }
+
     FWCfgDmaAccess access;
     uint32_t control = (entry << 16) | BIOS_CFG_DMA_CTL_SELECT
                         | BIOS_CFG_DMA_CTL_READ;
author
#10 lore raw
Quoted 7 lines · On Wed, 27 Jan 2016 19:14:54 -0500 "Kevin O'Connor" <[email protected]> wrote:
Quoted 1 line · Marc Marí

This optionrom is based on linuxboot.S.

Hi Marc,

Out of curiousity, how does the timing with this option rom compare to the previous SeaBIOS patches that implemented linux dma loading?

This patch
QEMU startup time: .092
BIOS startup time: .047
Kernel setup time: .003
Total time: .142

Current master (fw_cfg DMA enabled, but using linuxboot.img) QEMU startup time: .083
BIOS startup time: .047
Kernel setup time: .600
Total time: .730

You can see the time loading the kernel (between SeaBIOS function do_boot and the last instruction in the optionrom) is reduced a lot.

Quoted 77 lines

When I first tried to compile this (on fc23), I got:

In file included from /usr/include/features.h:389:0,

                 from /usr/include/stdint.h:25,
                 from /usr/lib/gcc/x86_64-redhat-linux/5.3.1/include/stdint.h:9,
                 from linuxboot_dma.c:62:

/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory compilation terminated.

which I fixed by running "dnf install glibc-devel.i686". Is a configure check needed?

See further comments below.

[...]

Quoted 61 lines
pc-bios/optionrom/linuxboot_dma.c new file +4 -0
@@ -0,0 +1,262 @@
+/*
+ * Linux Boot Option ROM for fw_cfg DMA
+ *
+ * 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, see
<http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2015 Red Hat Inc.
+ * Authors: Marc Marí <[email protected]>
+ */

pc-bios/optionrom/linuxboot_dma.c new file +34 -0
+
+asm(
+".text\n"
+".global _start\n"
+"_start:\n"
+"   .short	0xaa55\n"
+"   .byte (_end - _start) / 512\n"
+"   lret\n"
+"   .org 0x18\n"
+"   .short 0\n"
+"   .short _pnph\n"
+"_pnph:\n"
+"   .ascii \"$PnP\"\n"
+"   .byte 0x01\n"
+"   .byte ( _pnph_len / 16 )\n"
+"   .short 0x0000\n"
+"   .byte 0x00\n"
+"   .byte 0x00\n"
+"   .long 0x00000000\n"
+"   .short _manufacturer\n"
+"   .short _product\n"
+"   .long 0x00000000\n"
+"   .short 0x0000\n"
+"   .short 0x0000\n"
+"   .short _bev\n"
+"   .short 0x0000\n"
+"   .short 0x0000\n"
+"   .equ _pnph_len, . - _pnph\n"
+"   .align 4, 0\n"
+"_bev:\n"
+".code16gcc\n"
+/* DS = CS */
+"   movw %cs, %ax\n"
+"   movw %ax, %ds\n"
Quoted 8 lines
Quoted 6 lines

+" movl %esp, %ebp\n"
+"run_linuxboot:\n"

+"   cli\n"
+"   cld\n"
+"   jmp load_kernel\n"

+);

The run_linuxboot label doesn't seem to be used anywhere.

No it isn't, I can remove it or leave it as a reference of "code starts here".

Quoted 25 lines

[...]

Quoted 8 lines
pc-bios/optionrom/linuxboot_dma.c new file +8 -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;
+}
+
+static inline uint32_t readl_addr32(const void *addr) {

pc-bios/optionrom/linuxboot_dma.c

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

The above does not look correct to me. Since the code is running in 16bit mode the above memory accesses are relative to the %ds segment. Because %ds=%cs this is going to access a different address than expected.

Quoted 2 lines

What I think you want to do is assign %es=setup_addr>>4 and then perform the read at the given offset (eg, 0x206).

I was wondering why it does work, when it shouldn't:

asm("addr32 movl %1, %0" : "=r"(val) : "g"(addr));

This is copying addr to val, but not reading what is in addr. Stupid mistake!

What do you think of the patch at the end?

Quoted 23 lines

[...]

Quoted 8 lines

+static void bios_cfg_read_entry(void *buf, uint16_t entry, uint32_t len) +{

pc-bios/optionrom/linuxboot_dma.c new file +6 -0
+    FWCfgDmaAccess access;
+    uint32_t control = (entry << 16) | BIOS_CFG_DMA_CTL_SELECT
+                        | BIOS_CFG_DMA_CTL_READ;
+
+    access.address = cpu_to_be64((uint64_t)(uint32_t)buf);
+    access.length = cpu_to_be32(len);

pc-bios/optionrom/linuxboot_dma.c

Quoted 10 lines
pc-bios/optionrom/linuxboot_dma.c new file +10 -0
+    access.control = cpu_to_be32(control);
+
+    barrier();
+
+    outl(cpu_to_be32((uint32_t)&access), BIOS_CFG_DMA_ADDR_LOW);
+
+    while(be32_to_cpu(access.control) & ~BIOS_CFG_DMA_CTL_ERROR) {
+        barrier();
+    }
+}  

FYI, I think with a small incremental patch (see below) one could entirely replace the existing linuxboot.rom with your new code.

Quoted 4 lines

The one caveat is that this patch requires that kvm support "big real mode" and I know there were quirks with that on some older Intel chips. However, I think the "insb" instruction would trap anyway, so maybe it's not an issue.

This has already been mentioned in the previous patch version. There is no problem on doing it (and it is very simple, as you show). The last comments on this topics were:

Gerd Hoffman:

Quoted 3 lines

I'm personally fine with having two roms, but when merging them into one we surely should ditch the fw_cfg asm macros and go with something more maintainable.

Stefan Hajnoczi:

Quoted 3 lines

There is no technical requirement for a unified linuxboot ROM. If there is no disadvantage to having 2 ROMs then let's stick to Marc's approach.

I don't mind joining them either.

Quoted 46 lines
pc-bios/optionrom/linuxboot_dma.c +21 -0
@@ -73,6 +73,8 @@ asm(
 #define BIOS_CFG_DMA_CTL_SKIP    0x04
 #define BIOS_CFG_DMA_CTL_SELECT  0x08
 
+#define BIOS_CFG_CTL           0x510
+#define BIOS_CFG_DATA          0x511
 #define BIOS_CFG_DMA_ADDR_HIGH 0x514
 #define BIOS_CFG_DMA_ADDR_LOW  0x518
 
@@ -87,6 +89,16 @@ typedef struct FWCfgDmaAccess {
     uint64_t address;
 } __attribute__((packed)) FWCfgDmaAccess;
 
+static inline void outw(uint16_t value, uint16_t port) {
+    asm("outw %w0, %w1" : : "a"(value), "Nd"(port));
+}
+
+static inline uint32_t inl(uint16_t port) {
+    uint32_t value;
+    __asm__ __volatile__("inl %w1, %0" : "=a"(value) : "Nd"(port));
+    return value;
+}
+
 static inline void outl(uint32_t value, uint16_t port) {
     asm("outl %0, %w1" : : "a"(value), "Nd"(port));
 }
@@ -124,6 +136,15 @@ static inline uint32_t be32_to_cpu(uint32_t x) {
 
 static void bios_cfg_read_entry(void *buf, uint16_t entry, uint32_t
 len) {
+    if (inl(BIOS_CFG_DMA_ADDR_LOW) != 0x47464320) {
+        // Legacy PIO fw_cfg
+        outw(entry, BIOS_CFG_CTL);
+        asm volatile("movw %w0, %%es" :: "r"(0) : "memory");
+        asm volatile("rep insb (%%dx), %%es:(%%edi)"
+                     : "+c"(len), "+D"(buf) : "d"(BIOS_CFG_DATA) :
 "memory");
+        return;
+    }
+
     FWCfgDmaAccess access;
     uint32_t control = (entry << 16) | BIOS_CFG_DMA_CTL_SELECT
Quoted 1 line
                         | BIOS_CFG_DMA_CTL_READ;

A small cosmetic comment in this patch: is there any practical reason to mix the three ways to put inline assembly (asm(), asm volatile() and __asm__ __volatile__ ()) in this patch?

Thanks for your comments
Marc

Patch for segmentation:

pc-bios/optionrom/linuxboot_dma.c +0 -0
@@ -91,23 +91,28 @@ static inline void outl(uint32_t value, uint16_t
port) {
     asm("outl %0, %w1" : : "a"(value), "Nd"(port));

}

-static inline uint16_t readw_addr32(const void *addr) { +static inline void set_setup_addr(void *addr) {
+ uint32_t seg = (uint32_t)addr >> 4;
+ asm("movl %0, %%es\n" : : "r"(seg));
+}
+

+static inline uint16_t readw_setup(uint16_t offset) {
     uint16_t val;
-    asm("addr32 movw %1, %0" : "=r"(val) : "g"(addr));
+    asm("addr32 movw %%es:(%1), %0" : "=r"(val) :
"r"((uint32_t)offset)); barrier();
     return val;

}

-static inline uint32_t readl_addr32(const void *addr) {

+static inline uint32_t readl_setup(uint16_t offset) {
     uint32_t val;
-    asm("addr32 movl %1, %0" : "=r"(val) : "g"(addr));
+    asm("addr32 movl %%es:(%1), %0" : "=r"(val) :
"r"((uint32_t)offset)); barrier();
     return val;

}

-static inline void writel_addr32(void *addr, uint32_t val) {

+static inline void writel_setup(uint16_t offset, uint32_t val) {
     barrier();

- asm("addr32 movl %0, %1" : : "r"(val), "g"(addr));
+ asm("addr32 movl %0, %%es:(%1)" : : "r"(val),
"r"((uint32_t)offset)); }

static inline uint64_t cpu_to_be64(uint64_t x) {

#11 lore raw
Quoted 3 lines · Marc Marí

A small cosmetic comment in this patch: is there any practical reason to mix the three ways to put inline assembly (asm(), asm volatile() and __asm__ __volatile__ ()) in this patch?

No reason - it was just copy-and-paste crud.

Quoted 9 lines

Thanks for your comments
Marc

Patch for segmentation:

pc-bios/optionrom/linuxboot_dma.c static inline void outl(uint32_t value, uint16_t · line 98

Quoted 9 lines
pc-bios/optionrom/linuxboot_dma.c +4 -1
@@ -91,23 +91,28 @@ static inline void outl(uint32_t value, uint16_t
 port) {
     asm("outl %0, %w1" : : "a"(value), "Nd"(port));
 }
 
-static inline uint16_t readw_addr32(const void *addr) {
+static inline void set_setup_addr(void *addr) {
+    uint32_t seg = (uint32_t)addr >> 4;
+    asm("movl %0, %%es\n" : : "r"(seg));
+}

As a minor comment, I think using "set_es()" and "readw_es()" would be a little more descriptive.

pc-bios/optionrom/linuxboot_dma.c static inline void outl(uint32_t value, uint16_t · line 104

Quoted 7 lines
pc-bios/optionrom/linuxboot_dma.c +2 -1
+static inline uint16_t readw_setup(uint16_t offset) {
     uint16_t val;
-    asm("addr32 movw %1, %0" : "=r"(val) : "g"(addr));
+    asm("addr32 movw %%es:(%1), %0" : "=r"(val) :
 "r"((uint32_t)offset)); barrier();
     return val;
 }

SeaBIOS uses slightly different assembler - see the READx_SEG() macros at the top of farptr.h. That said, the above assembler is probably also fine.

https://github.com/KevinOConnor/seabios/blob/rel-1.9.0/src/farptr.h#L16

-Kevin

author
#12 lore raw
In reply to Kevin O'Connor
Quoted 4 lines

When I first tried to compile this (on fc23), I got:

In file included from /usr/include/features.h:389:0,

Quoted 8 lines
                 from /usr/include/stdint.h:25,
                 from /usr/lib/gcc/x86_64-redhat-linux/5.3.1/include/stdint.h:9,
                 from linuxboot_dma.c:62:

/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory compilation terminated.

which I fixed by running "dnf install glibc-devel.i686". Is a configure check needed?

I forgot to address this point this morning. I now put up a clean setup and I had the same issue. I'll add the configure check, but I don't know if there is a nicer solution.

Thanks
Marc