Skip to contentSkip to conversation

multiboot: Fix memory information

PATCH Applied Kevin Wolf 3 participants 8 messages Jun 23, 2013 – Jul 1, 2013

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover multiboot: Fix memory information

Kevin Wolf (3):
multiboot: Don't forget last mmap entry
multiboot: Calculate upper_mem in the ROM
multiboot: Updated ROM binary

 hw/i386/multiboot.c           |    2 -
 pc-bios/multiboot.bin         |  Bin 1024 -> 1024 bytes
 pc-bios/optionrom/multiboot.S |   75 +++++++++++++++++++++++++++++++----------
 3 files changed, 57 insertions(+), 20 deletions(-)
 mode change 100644 => 100755 pc-bios/multiboot.bin
Signature · 1 line
1.7.7
author
#1 lore raw

PATCH 1/3 multiboot: Don't forget last mmap entry

When the BIOS returns ebx = 0, the current entry is still valid and needs to be included in the Multiboot memory map.

Fixing this meant that using bx as the entry index doesn't work any more because it's 0 on the last entry (and it was SeaBIOS-specific anyway), so the whole loop had to change a bit and should be more generic as a result (ebx can be an arbitrary continuation number now, and the entry size returned by the BIOS is used instead of hard-coding 20 bytes).

---
 pc-bios/optionrom/multiboot.S |   35 +++++++++++++++++------------------
 1 files changed, 17 insertions(+), 18 deletions(-)
pc-bios/optionrom/multiboot.S +17 -18
index 003bcfb..a0f3602 100644
@@ -89,17 +89,14 @@ run_multiboot:
 
 	/* Initialize multiboot mmap structs using int 0x15(e820) */
 	xor		%ebx, %ebx
-	/* mmap start after first size */
-	movl		$4, %edi
+	/* Start storing mmap data at %es:0 */
+	xor		%edi, %edi
 
 mmap_loop:
+	/* The multiboot entry size has offset -4, so leave some space */
+	add		$4, %di
 	/* entry size (mmap struct) & max buffer size (int15) */
 	movl		$20, %ecx
-	/* store entry size */
-	/* old as(1) doesn't like this insn so emit the bytes instead:
-	movl		%ecx, %es:-4(%edi)
-	*/
-	.dc.b		0x26,0x67,0x66,0x89,0x4f,0xfc
 	/* e820 */
 	movl		$0x0000e820, %eax
 	/* 'SMAP' magic */
@@ -107,21 +104,23 @@ mmap_loop:
 	int		$0x15
 
 mmap_check_entry:
-	/* last entry? then we're done */
+	/* Error or last entry already done? */
 	jb		mmap_done
-	and		%bx, %bx
-	jz		mmap_done
-	/* valid entry, so let's loop on */
 
 mmap_store_entry:
-	/* %ax = entry_number * 24 */
-	mov		$24, %ax
-	mul		%bx
-	mov		%ax, %di
+	/* store entry size */
+	/* old as(1) doesn't like this insn so emit the bytes instead:
+	movl		%ecx, %es:-4(%edi)
+	*/
+	.dc.b		0x26,0x67,0x66,0x89,0x4f,0xfc
+
+	/* %edi += entry_size, store as mbs_mmap_length */
+	add		%ecx, %edi
 	movw		%di, %fs:0x2c
-	/* %di = 4 + (entry_number * 24) */
-	add		$4, %di
-	jmp		mmap_loop
+
+	/* Continuation value 0 means last entry */
+	test		%ebx, %ebx
+	jnz		mmap_loop
 
 mmap_done:
 real_to_prot:
Signature · 1 line
1.7.7
author
#2 lore raw

PATCH 2/3 multiboot: Calculate upper_mem in the ROM

In reply to Kevin Wolf

The upper_mem field of the Multiboot information struct doesn't really contain the RAM size - 1 MB like we used to calculate it, but only the memory from 1 MB up to the first (upper) memory hole.

In order to correctly retrieve this information, the multiboot ROM now looks at the mmap it creates anyway and tries to find the size of contiguous usable memory from 1 MB.

Drop the multiboot.c definition of lower_mem and upper_mem because both are queried at runtime now.

---
 hw/i386/multiboot.c           |    2 --
 pc-bios/optionrom/multiboot.S |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)
hw/i386/multiboot.c +0 -2
index 09211e0..985ca1e 100644
@@ -315,8 +315,6 @@ int load_multiboot(FWCfgState *fw_cfg,
                                 | MULTIBOOT_FLAGS_CMDLINE
                                 | MULTIBOOT_FLAGS_MODULES
                                 | MULTIBOOT_FLAGS_MMAP);
-    stl_p(bootinfo + MBI_MEM_LOWER,   640);
-    stl_p(bootinfo + MBI_MEM_UPPER,   (ram_size / 1024) - 1024);
     stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
     stl_p(bootinfo + MBI_MMAP_ADDR,   ADDR_E820_MAP);
 
pc-bios/optionrom/multiboot.S +40 -0
index a0f3602..b7efe4d 100644
@@ -123,6 +123,46 @@ mmap_store_entry:
 	jnz		mmap_loop
 
 mmap_done:
+	/* Calculate upper_mem field: The amount of memory between 1 MB and
+	   the first upper memory hole. Get it from the mmap. */
+	xor		%di, %di
+	mov		$0x100000, %edx
+upper_mem_entry:
+	cmp		%fs:0x2c, %di
+	je		upper_mem_done
+	add		$4, %di
+
+	/* Skip if type != 1 */
+	cmpl		$1, %es:16(%di)
+	jne		upper_mem_next
+
+	/* Skip if > 4 GB */
+	movl		%es:4(%di), %eax
+	test		%eax, %eax
+	jnz		upper_mem_next
+
+	/* Check for contiguous extension (base <= %edx < base + length) */
+	movl		%es:(%di), %eax
+	cmp		%eax, %edx
+	jb		upper_mem_next
+	addl		%es:8(%di), %eax
+	cmp		%eax, %edx
+	jae		upper_mem_next
+
+	/* If so, update %edx, and restart the search (mmap isn't ordered) */
+	mov		%eax, %edx
+	xor		%di, %di
+	jmp		upper_mem_entry
+
+upper_mem_next:
+	addl		%es:-4(%di), %edi
+	jmp		upper_mem_entry
+
+upper_mem_done:
+	sub		$0x100000, %edx
+	shr		$10, %edx
+	mov		%edx, %fs:0x8
+
 real_to_prot:
 	/* Load the GDT before going into protected mode */
 lgdt:
Signature · 1 line
1.7.7
author
#3 lore raw

PATCH 3/3 multiboot: Updated ROM binary

In reply to Kevin Wolf
---
 pc-bios/multiboot.bin |  Bin 1024 -> 1024 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 pc-bios/multiboot.bin
pc-bios/multiboot.bin binary +0 -0
old mode 100644
new mode 100755
index 7b3c1745a430ea5e0e15b9aa817d1cbbaa40db14..e772713c95749bee82c20002b50ec6d05b2d4987
GIT binary patch
Signature · 1 line
1.7.7
Reviewed-by on the v1 series
#4 lore raw
In reply to Kevin Wolf
Quoted 4 lines · Kevin Wolf

Kevin Wolf (3):
multiboot: Don't forget last mmap entry
multiboot: Calculate upper_mem in the ROM
multiboot: Updated ROM binary

Do you have a test case that triggered this that you can share?

I'll apply this after a day or so when others have had a chance to review.

Regards,

Anthony Liguori

Quoted 9 lines
 hw/i386/multiboot.c           |    2 -
 pc-bios/multiboot.bin         |  Bin 1024 -> 1024 bytes
 pc-bios/optionrom/multiboot.S |   75 +++++++++++++++++++++++++++++++----------
 3 files changed, 57 insertions(+), 20 deletions(-)
 mode change 100644 => 100755 pc-bios/multiboot.bin
Signature · 1 line
1.7.7
#5 lore raw

Am 23.06.2013 um 23:39 hat Anthony Liguori geschrieben:

Quoted 10 lines
Quoted 4 lines · Kevin Wolf

Kevin Wolf (3):
multiboot: Don't forget last mmap entry
multiboot: Calculate upper_mem in the ROM
multiboot: Updated ROM binary

Do you have a test case that triggered this that you can share?

I haven't seen an actual kernel failure myself, this was reported by someone else. But it's easy enough to check with a simple Multiboot kernel that just outputs lower_mem/upper_mem and the mmap.

For debugging and fixing I added some throw-away debug code to an existing kernel and compared the fixed version with the output when loaded by GRUB and it matches now.

If we can have a real test case for this somewhere, I can write a small kernel to do the check. Not sure where it would fit though - probably kvm-unittests is the closest, even though the test wouldn't really have anything to do with KVM.

Quoted 1 line

I'll apply this after a day or so when others have had a chance to review.

Sounds good, thanks.

Kevin

#6 lore raw
Quoted 16 lines · Kevin Wolf

Am 23.06.2013 um 23:39 hat Anthony Liguori geschrieben:

Quoted 10 lines
Quoted 4 lines · Kevin Wolf

Kevin Wolf (3):
multiboot: Don't forget last mmap entry
multiboot: Calculate upper_mem in the ROM
multiboot: Updated ROM binary

Do you have a test case that triggered this that you can share?

I haven't seen an actual kernel failure myself, this was reported by someone else. But it's easy enough to check with a simple Multiboot kernel that just outputs lower_mem/upper_mem and the mmap.

Quoted 8 lines

For debugging and fixing I added some throw-away debug code to an existing kernel and compared the fixed version with the output when loaded by GRUB and it matches now.

If we can have a real test case for this somewhere, I can write a small kernel to do the check. Not sure where it would fit though - probably kvm-unittests is the closest, even though the test wouldn't really have anything to do with KVM.

We could put it in pc-bios even with just a README for now. Would be nice to figure out ways to test -kernel as we do find issues often enough.

It's up to you. If you had something handy, I was going to add it to my local test setup.

Regards,

Anthony Liguori

Quoted 6 lines
Quoted 2 lines

I'll apply this after a day or so when others have had a chance to review.

Sounds good, thanks.

Kevin