Skip to contentSkip to conversation

Add multi-boot kernel loading support

PATCH In review Rene Rebe 7 participants 19 messages Feb 2, 2009 – Feb 4, 2009

lore
Share & tools
lore

Rene Rebe

author
#0 lore raw

PATCH 1/1 Add multi-boot kernel loading support

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

elf_ops.h +4 -0
Index: elf_ops.h
===================================================================
@@ -195,6 +195,10 @@
     }
 
     if (ELF_MACHINE != ehdr.e_machine)
+#if (ELF_MACHINE == EM_X86_64) && !defined(CONFIG_USER_ONLY)
+      /* x86_64 systems can run i386 code as well */
+      if(ehdr.e_machine != EM_386)
+#endif
         goto fail;
 
     if (pentry)
pc.c +443 -5

485 lines not on this page · raw message

Signature · 5 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  Geschäftsführer: Susanne Klaus, René Rebe
  Sitz: Berlin, Amtsgericht Charlottenburg HRB 105 123 B
  USt-IdNr.: DE251602478
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#1 lore raw

pc.c line 589

Quoted 8 lines · Rene Rebe
pc.c +8 -0
+    /* 660f011528000000            lgdt [0x28] */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x0f;                /* LGDT [0x128] */
+    *p++ = 0x01;
+    *p++ = 0x15;
+    pgdt=p; /* we calculate the gdt position later */
+    p+=4;

The comments do not appear to agree with the code here. (The operands for lgdt are different, at the very least.)

-Nathan

#2 lore raw
Quoted 12 lines · Nathan Froyd

pc.c line 589

Quoted 9 lines · Rene Rebe
pc.c +8 -0
+    /* 660f011528000000            lgdt [0x28] */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x0f;                /* LGDT [0x128] */
+    *p++ = 0x01;
+    *p++ = 0x15;
+    pgdt=p; /* we calculate the gdt position later */
+    p+=4;
Quoted 5 lines

The comments do not appear to agree with the code here. (The operands for lgdt are different, at the very least.)

-Nathan

Right, because the operands are actually calculated dynamically. Any suggestions on how to improve that? Maybe LGDT [GDT]?

Alex

#3 lore raw
Quoted 12 lines · Alexander Graf
Quoted 11 lines · Nathan Froyd
Quoted 9 lines · Rene Rebe
pc.c +8 -0
+    /* 660f011528000000            lgdt [0x28] */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x0f;                /* LGDT [0x128] */
+    *p++ = 0x01;
+    *p++ = 0x15;
+    pgdt=p; /* we calculate the gdt position later */
+    p+=4;
Quoted 5 lines
Quoted 2 lines

The comments do not appear to agree with the code here. (The operands for lgdt are different, at the very least.)

Right, because the operands are actually calculated dynamically. Any suggestions on how to improve that? Maybe LGDT [GDT]?

Yes, I think that would be an improvement. Editing the bytestring to reflect that would be good, as well.

-Nathan

#4 lore raw
In reply to Rene Rebe
Quoted 17 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

Index: elf_ops.h
===================================================================

elf_ops.h (working copy) line 204

Quoted 10 lines
elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +4 -0
@@ -195,6 +195,10 @@
     }
 
     if (ELF_MACHINE != ehdr.e_machine)
+#if (ELF_MACHINE == EM_X86_64) && !defined(CONFIG_USER_ONLY)
+      /* x86_64 systems can run i386 code as well */
+      if(ehdr.e_machine != EM_386)
+#endif
         goto fail;
 
     if (pentry)

Seems like an unrelated fix.

Quoted 40 lines

Index: hw/pc.c
===================================================================

pc.c (working copy) renamed from pc.c (revision 6501) +31 -0
@@ -533,6 +533,438 @@
     return size;
 }
 
+/* Generate an initial boot sector which sets state and jump to
+   a specified vector */
+static void generate_bootsect_multiboot(uint8_t *option_rom,
+                                        uint32_t mh_entry_addr, 
 uint32_t bootinfo)
+{
+    uint8_t rom[512], *p, *reloc, *pgdt, *pmmaploop;
+    uint8_t sum;
+    uint32_t ip;
+    int i;
+    int mmaploop;
+
+    memset(rom, 0, sizeof(rom));
+
+    p = rom;
+    /* Make sure we have an option rom signature */
+    *p++ = 0x55;
+    *p++ = 0xaa;
+
+    /* ROM size in sectors*/
+    *p++ = 1;
+
+    /* Hook int19 */
+
+    *p++ = 0x50;                /* push ax */
+    *p++ = 0x1e;                /* push ds */
+    *p++ = 0x31; *p++ = 0xc0;   /* xor ax, ax */
+    *p++ = 0x8e; *p++ = 0xd8;   /* mov ax, ds */
+
+    *p++ = 0xc7; *p++ = 0x06;   /* movvw _start,0x64 */
+    *p++ = 0x64; *p++ = 0x00;
+    reloc = p;

pc.c (working copy) line 577

Quoted 10 lines
pc.c (working copy) renamed from pc.c (revision 6501) +10 -0
+    *p++ = 0x00; *p++ = 0x00;
+
+    *p++ = 0x8c; *p++ = 0x0e;   /* mov cs,0x66 */
+    *p++ = 0x66; *p++ = 0x00;
+
+    *p++ = 0x1f;                /* pop ds */
+    *p++ = 0x58;                /* pop ax */
+    *p++ = 0xcb;                /* lret */
+    /* Actual code */
+    *reloc = (p - rom);

The above could be refactored to common code.

Quoted 211 lines
pc.c (working copy) renamed from pc.c (revision 6501) +191 -0
+    *p++ = 0xfa;                /* CLI */
+    *p++ = 0xfc;                /* CLD */
+
+    /* 660f011528000000            lgdt [0x28] */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x0f;                /* LGDT [0x128] */
+    *p++ = 0x01;
+    *p++ = 0x15;
+    pgdt=p; /* we calculate the gdt position later */
+    p+=4;
+
+    /* Initialize multiboot mmap structs using the 0x15(e820) */
+    *p++ = 0x31;                /* XOR BX,BX */
+    *p++ = 0xdb;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xbf;                /* MOV EDI,0x9004 */
+    *p++ = 0x04;
+    *p++ = 0x90;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    pmmaploop = p;
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xb8;                /* MOV EAX,0x20 */
+    *p++ = 0x20;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x89;                /* MOV -4(EDI),EAX */
+    *p++ = 0x47;
+    *p++ = 0xfc;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xb8;                /* MOV EAX,0x0000e820 */
+    *p++ = 0x20;
+    *p++ = 0xe8;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xba;                /* MOV EDX,0x534d4150 */
+    *p++ = 0x50;
+    *p++ = 0x41;
+    *p++ = 0x4d;
+    *p++ = 0x53;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xb9;                /* MOV ECX,0x20 */
+    *p++ = 0x20;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    *p++ = 0xcd;                /* INT 0x15 */
+    *p++ = 0x15;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xb8;                /* MOV EAX, 0x24 */
+    *p++ = 0x24;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    *p++ = 0xf7;                /* MUL AX, BX */
+    *p++ = 0xe3;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x21;                /* AND EBX, EBX */
+    *p++ = 0xdb;
+
+    /* don't store if bx = 0 */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x0f;                /* JZ next instruction */
+    *p++ = 0x84;
+    *p++ = 0x07;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    /* store the amount of blocks in the bootinfo struct */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0xa3;                /* MOV [bootinfo+0x2c], EAX */
+    *p++ = (bootinfo+0x2c);
+    *p++ = (bootinfo+0x2c) >> 8;
+    *p++ = (bootinfo+0x2c) >> 16;
+    *p++ = (bootinfo+0x2c) >> 24;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x05;                /* ADD EAX, 0x9004 */
+    *p++ = 0x04;
+    *p++ = 0x90;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    *p++ = 0x89;                /* MOV DI, AX */
+    *p++ = 0xc7;
+
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x21;                /* AND EBX, EBX */
+    *p++ = 0xdb;
+
+    /* process next entry */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0x67;                /* 32-bit addr size */
+    *p++ = 0x0f;                /* JNZ mmaploop */
+    *p++ = 0x85;
+    mmaploop = (int)((long)pmmaploop) - ((long)p) - 4;
+    *p++ = mmaploop;
+    *p++ = mmaploop >> 8;
+    *p++ = mmaploop >> 16;
+    *p++ = mmaploop >> 24;
+
+    /* get us to protected mode now */
+
+    *p++ = 0x66;
+    *p++ = 0xb8;                /* MOV EAX,0x01 */
+    *p++ = 0x01;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    *p++ = 0x00;
+
+    *p++ = 0x0f;                /* MOV CR0,EAX */
+    *p++ = 0x22;
+    *p++ = 0xc0;
+
+    /* the JMP sets CS for us and gets us to 32-bit */
+    ip = 0x000d0000 + (p - rom) + 8; /* set i to the IP after the JMP */
+    *p++ = 0x66;                /* 32-bit operand size */
+    *p++ = 0xea;                /* JMP */
+    *p++ = ip;        /* IP */
+    *p++ = ip >> 8;
+    *p++ = ip >> 16;
+    *p++ = ip >> 24;
+    *p++ = 0x08;
+    *p++ = 0x00;
+
+    /* initialize all other segments */
+    *p++ = 0xb8;                /* MOV EAX,0x10 */
+    *p++ = 0x10;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    *p++ = 0x00;
+    for (i = 0; i < 6; i++) {
+        if (i == 1)                /* Skip CS */
+            continue;
+
+        *p++ = 0x8e;                /* MOV <seg>,EAX */
+        *p++ = 0xc0 + (i << 3);
+    }
+
+    /* EBX contains a pointer to the bootinfo struct */
+    *p++ = 0xbb;                /* MOV EBX,imm32 */
+    *p++ = bootinfo;
+    *p++ = bootinfo >> 8;
+    *p++ = bootinfo >> 16;
+    *p++ = bootinfo >> 24;
+
+    /* EAX has to contain the following magic */
+    *p++ = 0xb8;                /* MOV EAX,0x2badb002 */
+    *p++ = 0x02;
+    *p++ = 0xb0;
+    *p++ = 0xad;
+    *p++ = 0x2b;
+
+    /* Jump off to the kernel */
+    *p++ = 0xea;                /* JMP */
+    *p++ = mh_entry_addr;        /* IP */
+    *p++ = mh_entry_addr >> 8;
+    *p++ = mh_entry_addr >> 16;
+    *p++ = mh_entry_addr >> 24;
+    *p++ = 0x08;
+    *p++ = 0x00;
+
+    { /* GDT loading */
+        uint32_t gdt_base = 0x000d0000 + (p - rom); /* 0x00007c00 is 

the first IP */

pc.c (working copy) renamed from pc.c (revision 6501) +5 -0
+        uint32_t gdtr = gdt_base + 0x28;
+        uint8_t gdt[] = { /* GDT base: 0x00000100 */
+            /* 0x00 */
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            /* 0x08: code segment (base=0, limit=0xfffff, type=32bit 

code exec/read, DPL=0, 4k) */

pc.c (working copy) renamed from pc.c (revision 6501) +2 -0
+            0xff, 0xff, 0x00, 0x00, 0x00, 0x9a, 0xcf, 0x00,
+            /* 0x10: data segment (base=0, limit=0xfffff, type=32bit 

data read/write, DPL=0, 4k) */

pc.c (working copy) renamed from pc.c (revision 6501) +2 -0
+            0xff, 0xff, 0x00, 0x00, 0x00, 0x92, 0xcf, 0x00,
+            /* 0x18: code segment (base=0, limit=0x0ffff, type=16bit 

code exec/read/conf, DPL=0, 1b) */

pc.c (working copy) renamed from pc.c (revision 6501) +2 -0
+            0xff, 0xff, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x00,
+            /* 0x20: data segment (base=0, limit=0x0ffff, type=16bit 

data read/write, DPL=0, 1b) */

pc.c (working copy) renamed from pc.c (revision 6501) +3 -0
+            0xff, 0xff, 0x00, 0x00, 0x00, 0x92, 0x00, 0x00,
+            /* 0x28: gdtdesc */
+            0x27, 0x00, gdt_base, gdt_base >> 8, gdt_base >> 16, 

gdt_base >> 24

pc.c (working copy)

Quoted 9 lines
pc.c (working copy) renamed from pc.c (revision 6501) +9 -0
+        };
+
+        memcpy(p, gdt, sizeof(gdt));
+        p+=sizeof(gdt);
+        *pgdt++ = gdtr;
+        *pgdt++ = gdtr >> 8;
+        *pgdt++ = gdtr >> 16;
+        *pgdt++ = gdtr >> 24;
+    }

It was already pushing the limit to embed the linux loader. This is a bit too much. Perhaps we should split it to a separate .S file? I guess we could apply it as is but I'd like to see this code (along with the Linux code) moved to another file at least.

pc.c (working copy)

Quoted 10 lines

+ fprintf(stderr, "qemu: multiboot loader code is %d bytes
long.\n", (int)(p-rom));

pc.c (working copy) renamed from pc.c (revision 6501) +8 -0
+
+    /* sign rom */
+    sum = 0;
+    for (i = 0; i < (sizeof(rom) - 1); i++)
+        sum += rom[i];
+    rom[sizeof(rom) - 1] = -sum;
+
+    memcpy(option_rom, rom, sizeof(rom));

This too could be common code.

Quoted 54 lines
pc.c (working copy) renamed from pc.c (revision 6501) +20 -0
+}
+
+static int load_multiboot(uint8_t *option_rom,
+                          FILE *f,
+                          const char *kernel_filename,
+                          const char *initrd_filename,
+                          const char *kernel_cmdline,
+                          uint8_t *header)
+{
+    int i, is_multiboot = 0;
+    uint32_t flags = 0;
+    uint32_t mh_entry_addr;
+    uint32_t mh_load_addr;
+    uint32_t mb_kernel_size;
+    uint32_t mb_bootinfo = 0x90000;
+    uint32_t tmp_size;
+
+    /* Ok, let's see if it is a multiboot image */
+    for(i = 0; i < 8144; i += 4) { /* the header is 12x32bit long, so */
+                                   /* the latest entry may be 8192 - 

48 */

pc.c (working copy) renamed from pc.c (revision 6501) +23 -0
+        if(ldl_p(header+i) == 0x1BADB002) {
+            uint32_t checksum = ldl_p(header+i+8);
+            flags = ldl_p(header+i+4);
+            checksum += flags;
+            checksum += (uint32_t)0x1BADB002;
+            if(!checksum) {
+                is_multiboot = 1;
+                break;
+            }
+        }
+    }
+
+    if(!is_multiboot) return 0; /* no multiboot */
+    fprintf(stderr, "qemu: I believe we found a multiboot image!\n");
+
+    if(flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
+        fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
+    }
+    if(!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
+        uint64_t elf_entry;
+        int kernel_size;
+        fclose(f);
+        kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, 

NULL);

pc.c (working copy) renamed from pc.c (revision 6501) +8 -0
+        if(kernel_size < 0) {
+            fprintf(stderr, "Error while loading elf kernel\n");
+            exit(1);
+        }
+        mh_load_addr = mh_entry_addr = elf_entry;
+        mb_kernel_size = kernel_size;
+
+        fprintf(stderr, "qemu: loading multiboot-elf kernel (%#x 

bytes) with entry %#zx\n",

pc.c (working copy)

Quoted 5 lines
pc.c (working copy) renamed from pc.c (revision 6501) +5 -0
+                mb_kernel_size, (size_t)mh_entry_addr);
+    } else {
+        /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
+        uint32_t mh_header_addr = ldl_p(header+i+12);
+        mh_load_addr = ldl_p(header+i+16);

Mixing variable definitions and code.

Quoted 16 lines
pc.c (working copy) renamed from pc.c (revision 6501) +4 -0
+        uint32_t mh_load_end_addr = ldl_p(header+i+20);
+        uint32_t mh_bss_end_addr = ldl_p(header+i+24);
+        uint8_t *mb_kernel_addr = phys_ram_base + (mh_load_addr);
+        uint32_t mb_kernel_text_offset = i - (mh_header_addr - 

mh_load_addr);

pc.c (working copy) renamed from pc.c (revision 6501) +11 -0
+
+        mh_entry_addr = ldl_p(header+i+28);
+        mb_kernel_size = get_file_size(f) - mb_kernel_text_offset;
+
+        /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
+        uint32_t mh_mode_type = ldl_p(header+i+32);
+        uint32_t mh_width = ldl_p(header+i+36);
+        uint32_t mh_height = ldl_p(header+i+40);
+        uint32_t mh_depth = ldl_p(header+i+44); */
+
+        fprintf(stderr, "multiboot: mh_header_addr = %#x\n", 
Quoted 7 lines

mh_header_addr);
+ fprintf(stderr, "multiboot: mh_load_addr = %#x\n",
mh_load_addr);
+ fprintf(stderr, "multiboot: mh_load_end_addr = %#x\n",
mh_load_end_addr);
+ fprintf(stderr, "multiboot: mh_bss_end_addr = %#x\n",
mh_bss_end_addr);

It's a bit chatty by default.

Quoted 37 lines
pc.c (working copy) renamed from pc.c (revision 6501) +3 -0
+        fseek(f, mb_kernel_text_offset, SEEK_SET);
+
+        fprintf(stderr, "qemu: loading multiboot kernel (%#x bytes) 

at %#zx\n",

pc.c (working copy) renamed from pc.c (revision 6501) +3 -0
+                mb_kernel_size, mb_kernel_addr - phys_ram_base);
+
+        if ((tmp_size=fread(mb_kernel_addr, 1, mb_kernel_size, f)) != 

mb_kernel_size) {
+ fprintf(stderr, "qemu: read error on multiboot kernel
'%s' (%#x != %#x)\n", kernel_filename, tmp_size, mb_kernel_size);

pc.c (working copy) renamed from pc.c (revision 6501) +23 -0
+            exit(1);
+        }
+        fclose(f);
+    }
+
+
+    /* load modules */
+
+    stl_p(phys_ram_base + mb_bootinfo + 20, 0x0); /* mods_count */
+    if(initrd_filename) {
+        uint32_t mb_mod_info = mb_bootinfo + 0x100;
+        uint32_t mb_mod_cmdline = mb_bootinfo+ 0x300;
+        uint32_t mb_mod_start = mh_load_addr;
+        uint32_t mb_mod_length = mb_kernel_size;
+        char *next_initrd;
+        char *next_space;
+        int mb_mod_count = 0;
+
+        do {
+            next_initrd = strchr(initrd_filename, ',');
+            if(next_initrd)
+                *next_initrd = '\0';
+            /* if a space comes after the module filename, treat 

everything after that as parameters */

pc.c (working copy) renamed from pc.c (revision 6501) +2 -0
+            strcpy(phys_ram_base + mb_mod_cmdline, initrd_filename);
+            stl_p(phys_ram_base + mb_mod_info + 8, mb_mod_cmdline); 

/* string */

pc.c (working copy)

Quoted 4 lines
pc.c (working copy) renamed from pc.c (revision 6501) +4 -0
+            mb_mod_cmdline += strlen(initrd_filename) + 1;
+            if(next_space = strchr(initrd_filename, ' '))
+                *next_space = '\0';
+printf("multiboot loading module: %s\n", initrd_filename);

Weird indent.

What all uses multiboot at this point? I know Xen does.

Some additional documentation would be nice too.

Regards,

Anthony Liguori

author
#5 lore raw

Anthony Liguori wrote:
...

Quoted 9 lines

pc.c line 792

Quoted 7 lines
pc.c +7 -0
+        memcpy(p, gdt, sizeof(gdt));
+        p+=sizeof(gdt);
+        *pgdt++ = gdtr;
+        *pgdt++ = gdtr >> 8;
+        *pgdt++ = gdtr >> 16;
+        *pgdt++ = gdtr >> 24;
+    }

It was already pushing the limit to embed the linux loader. This is a bit too much. Perhaps we should split it to a separate .S file? I guess we could apply it as is but I'd like to see this code (along with the Linux code) moved to another file at least.

How do you want to assemble the .S file on non x86 builds, or just ship a pre-built object like the BIOS?

Quoted 4 lines
Quoted 4 lines

+ fprintf(stderr, "qemu: multiboot loader code is %d bytes
long.\n", (int)(p-rom));

pc.c +2 -0
+
+    /* sign rom */
Quoted 8 lines

pc.c line 802

Quoted 6 lines
pc.c +6 -0
+    sum = 0;
+    for (i = 0; i < (sizeof(rom) - 1); i++)
+        sum += rom[i];
+    rom[sizeof(rom) - 1] = -sum;
+
+    memcpy(option_rom, rom, sizeof(rom));

This too could be common code.

Ok.
+ } else {

Quoted 5 lines

pc.c line 858

Quoted 3 lines
pc.c +3 -0
+        /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
+        uint32_t mh_header_addr = ldl_p(header+i+12);
+        mh_load_addr = ldl_p(header+i+16);

Mixing variable definitions and code.

Yuck.

Quoted 10 lines
Quoted 8 lines

+ fprintf(stderr, "multiboot: mh_header_addr = %#x\n",
mh_header_addr);
+ fprintf(stderr, "multiboot: mh_load_addr = %#x\n",
mh_load_addr);
+ fprintf(stderr, "multiboot: mh_load_end_addr = %#x\n",
mh_load_end_addr);
+ fprintf(stderr, "multiboot: mh_bss_end_addr = %#x\n",
mh_bss_end_addr);

It's a bit chatty by default.

Yes, indeed - though quite handy for a yet new feature. I could just drop those (maybe after testing booting some other multi-boot kernel as well, ...).

Quoted 3 lines

Weird indent.

What all uses multiboot at this point? I know Xen does.

Maybe other homebrew desktop kernels. I remeber there also went support into NetBSD some time ago.

Quoted 1 line

Some additional documentation would be nice too.

Greetings,
Rene

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#6 lore raw
Quoted 1 line · Rene Rebe

Anthony Liguori wrote:

[...]

Quoted 6 lines
Quoted 3 lines

Weird indent.
What all uses multiboot at this point? I know Xen does.

Maybe other homebrew desktop kernels. I remeber there also went support into NetBSD some time ago.

I know Kevin developed a kernel that was based on multiboot. The original purpose was to load the darwin bootloader, modified to run with multiboot. Hurd does multiboot too IIRC.

Alex

#8 lore raw
In reply to Anthony Liguori
Quoted 27 lines · Anthony Liguori
Quoted 26 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

Index: elf_ops.h
===================================================================

elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +4 -0
@@ -195,6 +195,10 @@
    }
 
    if (ELF_MACHINE != ehdr.e_machine)
+#if (ELF_MACHINE == EM_X86_64) && !defined(CONFIG_USER_ONLY)
+      /* x86_64 systems can run i386 code as well */
+      if(ehdr.e_machine != EM_386)
+#endif
        goto fail;
 
Quoted 3 lines

elf_ops.h (working copy) line 204

Quoted 1 line
elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +0 -0
    if (pentry)

Seems like an unrelated fix.

Actually it's not. If you want to load a 32-bit elf (multiboot) executable on an x86_64 system, you need this patch.

Alex

author
#7 lore raw
In reply to Alexander Graf
Quoted 8 lines · Alexander Graf
Quoted 1 line · Rene Rebe

Anthony Liguori wrote:

[...]

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Weird indent.
What all uses multiboot at this point? I know Xen does.

Maybe other homebrew desktop kernels. I remeber there also went support into NetBSD some time ago.

I know Kevin developed a kernel that was based on multiboot. The original purpose was to load the darwin bootloader, modified to run with multiboot. Hurd does multiboot too IIRC.

Indeed, how could we overlook to list it? For Hurd the multiboot SPEC might even been defined initially?

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
author
#9 lore raw
In reply to Alexander Graf
Quoted 30 lines · Alexander Graf
Quoted 27 lines · Anthony Liguori
Quoted 26 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

Index: elf_ops.h
===================================================================

elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +4 -0
@@ -195,6 +195,10 @@
    }
 
    if (ELF_MACHINE != ehdr.e_machine)
+#if (ELF_MACHINE == EM_X86_64) && !defined(CONFIG_USER_ONLY)
+      /* x86_64 systems can run i386 code as well */
+      if(ehdr.e_machine != EM_386)
+#endif
        goto fail;
 
Quoted 5 lines
Quoted 3 lines

elf_ops.h (working copy) line 204

Quoted 1 line
elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +0 -0
    if (pentry)

Seems like an unrelated fix.

Actually it's not. If you want to load a 32-bit elf (multiboot) executable on an x86_64 system, you need this patch.

Thanks, I was just about to re-reply to the other mail with this question lost while answering.

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#10 lore raw
In reply to Alexander Graf
Quoted 12 lines · Alexander Graf
Quoted 12 lines
Quoted 12 lines
elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +4 -0
@@ -195,6 +195,10 @@
   }
 
   if (ELF_MACHINE != ehdr.e_machine)
+#if (ELF_MACHINE == EM_X86_64) && !defined(CONFIG_USER_ONLY)
+      /* x86_64 systems can run i386 code as well */
+      if(ehdr.e_machine != EM_386)
+#endif
       goto fail;
 
Quoted 6 lines
Quoted 3 lines

elf_ops.h (working copy) line 204

Quoted 1 line
elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +0 -0
   if (pentry)

Seems like an unrelated fix.

Actually it's not. If you want to load a 32-bit elf (multiboot) executable on an x86_64 system, you need this patch.

Why CONFIG_USER_ONLY?

Does it mean qemu-x86_64 (the user one) will behave differently given a 32-bit ELF file if you built qemu-system-x86_64 at the same time?

-- Jamie

#11 lore raw
Quoted 17 lines · Jamie Lokier
Quoted 16 lines · Alexander Graf
Quoted 15 lines

elf_ops.h (working copy) line 204

Quoted 13 lines
elf_ops.h (working copy) renamed from elf_ops.h (revision 6501) +4 -0
@@ -195,6 +195,10 @@
  }
 
  if (ELF_MACHINE != ehdr.e_machine)
+#if (ELF_MACHINE == EM_X86_64) && !defined(CONFIG_USER_ONLY)
+      /* x86_64 systems can run i386 code as well */
+      if(ehdr.e_machine != EM_386)
+#endif
      goto fail;
 
  if (pentry)

Seems like an unrelated fix.

Quoted 7 lines
Quoted 2 lines

Actually it's not. If you want to load a 32-bit elf (multiboot) executable on an x86_64 system, you need this patch.

Why CONFIG_USER_ONLY?

Does it mean qemu-x86_64 (the user one) will behave differently given a 32-bit ELF file if you built qemu-system-x86_64 at the same time?

It's !defined(), to ensure the userspace emulation behavior doesn't change. Right now, loading an i386 binary with qemu-x86_64 does not work - and I don't want to change that for now.

AFAIK CONFIG_USER_ONLY is defined when building qemu-x86_64, but not when building qemu-system-x86_64.

Alex

#12 lore raw
In reply to Rene Rebe
Quoted 9 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

The memory map doesn't seem to work. It's all zeros for me (haven't looked for the reason yet) and that kills my kernel. When I disable the memory map part, the kernel seems to boot correctly and it sees all the modules with the right command lines.

Once these basic things work, another nice feature would be gzip support. It took me some minutes until I realized that I tried to feed it a gzipped kernel which didn't work, of course.

Kevin

#17 lore raw
In reply to Rene Rebe
Quoted 9 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

This is introducing quite a lot of nontrivial target code in a hard to understand and maintain way. This seems like a bad idea.

I suspect it would be better to implement this as an actual boot rom, and have it read arguments from the firmware device. Ideally the same rom would also cover the linux kernel case.

Paul

author
#13 lore raw
In reply to Kevin Wolf

Hi,

Quoted 11 lines · Kevin Wolf
Quoted 9 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

Quoted 8 lines

The memory map doesn't seem to work. It's all zeros for me (haven't looked for the reason yet) and that kills my kernel. When I disable the memory map part, the kernel seems to boot correctly and it sees all the modules with the right command lines.

Once these basic things work, another nice feature would be gzip support. It took me some minutes until I realized that I tried to feed it a gzipped kernel which didn't work, of course.

I think I noticed the same with the GRUB example kernel. I'll take a look at some later hour.

Is your kernel something you can share for testing?

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
author
#18 lore raw
In reply to Paul Brook
Quoted 8 lines · Paul Brook
Quoted 7 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

Quoted 6 lines
Quoted 2 lines

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

This is introducing quite a lot of nontrivial target code in a hard to understand and maintain way. This seems like a bad idea.

I suspect it would be better to implement this as an actual boot rom, and have it read arguments from the firmware device. Ideally the same rom would also cover the linux kernel case.

Hm. As I'm not yet memorized the Qemu code and and build system maybe someone wants to shape the existing option rom code in the way envisioned to base this patch series on?

Another option would also to apply (a cleaned up) version of the patch as it is quite local and intrusive code and someone else further re-factors it into an externally built option rom?

Greetings,

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#14 lore raw
In reply to Rene Rebe
Quoted 19 lines · Rene Rebe

Hi,

Quoted 16 lines · Kevin Wolf
Quoted 9 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

The memory map doesn't seem to work. It's all zeros for me (haven't looked for the reason yet) and that kills my kernel. When I disable the memory map part, the kernel seems to boot correctly and it sees all the modules with the right command lines.

Quoted 8 lines
Quoted 3 lines

Once these basic things work, another nice feature would be gzip support. It took me some minutes until I realized that I tried to feed it a gzipped kernel which didn't work, of course.

I think I noticed the same with the GRUB example kernel. I'll take a look at some later hour.

Is your kernel something you can share for testing?

Sure. I tried it with with several homebrew kernels from
http://lowlevel.brainsware.org/wiki/index.php/Kategorie:Betriebssystem
(page is in German) - to be precise it was tyndur, meinOS, Xantorix and lightOS. Links to the sources and/or binaries for each one are in the respective Wiki articles.

I could also send you a tarball with all of them off-list if you like.

Kevin

author
#15 lore raw

Hi,

Quoted 29 lines · Kevin Wolf
Quoted 19 lines · Rene Rebe

Hi,

Quoted 16 lines · Kevin Wolf
Quoted 9 lines · Rene Rebe

Hi all,

Alexander Graf implemented multi-boot kernel loading during his work to run Darwin inside Qemu/KVM. As the boot loader expects to load the kernel in an EFI environment a custom booter is used to load the kernel using a legacy BIOS.

This is a port of the patch to the new extload / INT 19 machinery (including minor cleanups).

The memory map doesn't seem to work. It's all zeros for me (haven't looked for the reason yet) and that kills my kernel. When I disable the memory map part, the kernel seems to boot correctly and it sees all the modules with the right command lines.

Quoted 8 lines
Quoted 3 lines

Once these basic things work, another nice feature would be gzip support. It took me some minutes until I realized that I tried to feed it a gzipped kernel which didn't work, of course.

I think I noticed the same with the GRUB example kernel. I'll take a look at some later hour.

Is your kernel something you can share for testing?

Quoted 7 lines

Sure. I tried it with with several homebrew kernels from
http://lowlevel.brainsware.org/wiki/index.php/Kategorie:Betriebssystem
(page is in German) - to be precise it was tyndur, meinOS, Xantorix and lightOS. Links to the sources and/or binaries for each one are in the respective Wiki articles.

I could also send you a tarball with all of them off-list if you like.

From reading the last mails I thought you would have your own kernel.

I now have the mmap, memory sizes (kB, not Bytes) fixed and some other comments from the list taken into account (ARRAY_SIZE, etc.).

http://svn.exactcode.de/t2/trunk/package/emulators/kvm/09-qemu-multiboot.patch

As I did not yet feel like extracting all the kernels and their modules from the cited disk / floppy images so it's not yet tested with those "homebrew" kernels.

Further comments welcome,

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#16 lore raw
Quoted 11 lines · Rene Rebe
Quoted 11 lines
Quoted 4 lines

I think I noticed the same with the GRUB example kernel. I'll take a look at some later hour.

Is your kernel something you can share for testing?

Sure. I tried it with with several homebrew kernels from
http://lowlevel.brainsware.org/wiki/index.php/Kategorie:Betriebssystem
(page is in German) - to be precise it was tyndur, meinOS, Xantorix and lightOS. Links to the sources and/or binaries for each one are in the respective Wiki articles.

Quoted 3 lines
Quoted 1 line

I could also send you a tarball with all of them off-list if you like.

From reading the last mails I thought you would have your own kernel.

Well, I didn't say that I'm not involved in one of these. tyndur is the one, even if the broken multiboot patch proved that there is room for improvement in the error handling. (*cough* Triple fault... ;-))

Quoted 10 lines

I now have the mmap, memory sizes (kB, not Bytes) fixed and some other comments from the list taken into account (ARRAY_SIZE, etc.).

http://svn.exactcode.de/t2/trunk/package/emulators/kvm/09-qemu-multiboot.patch

As I did not yet feel like extracting all the kernels and their modules from the cited disk / floppy images so it's not yet tested with those "homebrew" kernels.

Looks much better now. Not all systems complete the boot (I would suspect the OSes to be buggy here though), but no horrible crashes any more.

Well, except that with the lightOS kernel I still managed to get a qemu segfault in the fread in pc.c:939 when loading a module, and other modules failed to load ("read error on multiboot module abc (0 != x)"). Interestingly the same modules are loaded without any problem if I use a different kernel. I guess we'd better fix that before committing.

Kevin