Page 1 of 1

multiboot2 problems

Posted: Wed Sep 19, 2018 6:09 pm
by MakaAlbarn001
I've been having problems with my multiboot2 info request. I'm pretty sure I have my header right:

Code: Select all

# Define the contnts of the Multiboot Header
.set MAGIC,		0xE85250D6
.set ARCH,		0
.set LENGTH,	(multiboot_header_end - multiboot_header)
.set CHECKSUM,	-(MAGIC + ARCH + LENGTH)

# Store the contents of the Multiboot header at the beginning of the file.
.section .multiboot
.align 8								# Align the section on the 64-bit boundary.
multiboot_header:
.long MAGIC
.long ARCH
.long LENGTH
.long CHECKSUM
multiboot_info_req:
.short 1
.short 0
.long (multiboot_info_req_end - multiboot_info_req)
.long 4
.long 6
multiboot_info_req_end:
.short 0
.short 0
.long 8
multiboot_header_end:
Can anyone tell me if GRUB loads the multiboot info in the same order I requested it, and how to go about reading it in main()? Here is what I have for main():

Code: Select all

#include <sys/sys.h>
#include <sys/multiboot2.h>
#include <kernel/tty.h>
#include <libc/stdio.h>

#include <stdint.h>

struct multiboot2_data {
    multiboot_uint32_t totalSize;
    multiboot_uint32_t reserved;
    struct multiboot_tag_basic_meminfo memInfo;
    struct multiboot_tag_mmap mmapInfo;
};

extern "C" void _main(struct multiboot2_data* mbd) {
    gdt_install();
    idt_install();
    terminal_initialize();
    terminal_writestring("Hello World! \n");
    struct multiboot_tag_basic_meminfo memInfo = mbd->memInfo;
    printf("Lower Memory: %x\n", memInfo.mem_lower);
    printf("Version: 0.0.0.1d\n");
    for(;;);
}
I know I'm having problems because when I test it, it tells me that I have 38GB of lower memory. And no, that is NOT a typo.

edit: And yes, I did remember to push %ebx.

Re: multiboot2 problems

Posted: Wed Sep 19, 2018 6:31 pm
by kzinti
MakaAlbarn001 wrote: I know I'm having problems because when I test it, it tells me that I have 38GB of lower memory. And no, that is NOT a typo.
Something might be wrong with your printf() function... multiboot_uint32_t is a 32 bits unsigned integer. The biggest number that can be represented with a 32 bits unsigned integer is 4 GB.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 10:30 am
by MakaAlbarn001
You forgot that mem_lower is in Kilobytes.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 12:17 pm
by kzinti
Can you show us your boot code? Because without it, we can't look at it. Basically you should be pushing "ebx" on the stack before calling your main function.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 2:24 pm
by MichaelPetch
kzinti wrote:Can you show us your boot code? Because without it, we can't look at it. Basically you should be pushing "ebx" on the stack before calling your main function.
In one of the OPs early edits they placed this at the bottom:
edit: And yes, I did remember to push %ebx.
. I agree though that seeing all the code he's using would help identify the issue.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 4:02 pm
by kzinti
Well without seeing more code, there is no bug to be found here.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 4:59 pm
by MakaAlbarn001
You can take a look at my most recent commit. I have it set to print out the lower memory in decimal format.

git clone https://[email protected]/ ... ststep.git

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 5:33 pm
by kzinti
I know it's supposed to be preserved, but I wonder if EBX is getting trashed when you call "_init".

https://bitbucket.org/BringerOfShadows/ ... #boot.s-43

It might be worth pushing / poping it around the call to make sure its valid when you call "_main".

Have you tried tracing this with a debugger? It's probably easier than trying to guess what is happening.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 6:52 pm
by MakaAlbarn001
Okay, I need to update my glasses prescription. I forgot that I removed the "push %ebx" about 4-5 commits ago.
Now, when I try to run it. I get a lower memory of 5376KB.

edit: printing both lower and upper memory gives an upper memory of zero and a lower memory of 4Mil+ KB.

do you know what order GRUB will store the multiboot info?

edit2: Okay, I fixed the problem with my printf() function. Turns out that there was a buffer overrun issue when I was reversing the decimal string. Now I get a lower memory of 5376KB and an upper memory of 1510KB. So... still an issue in my multiboot structure.

edit3: Also, when I print other lines to screen, the contents of the structure get corrupted.

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 10:29 pm
by kzinti
MakaAlbarn001 wrote:do you know what order GRUB will store the multiboot info?
I don't understand what you mean by that. The muiltiboot info is a structure. What is there to "order"?

Re: multiboot2 problems

Posted: Thu Sep 20, 2018 10:36 pm
by MakaAlbarn001
That's multiboot 1. In multiboot2, there is a series of different structures that follow a pattern. Look up the multiboot2 manual.

Re: multiboot2 problems

Posted: Fri Sep 21, 2018 12:05 am
by kzinti
I see. You can look at the source code of grub to answer your question. But I don't understand why this would matter at all. Your code should not expect any specific ordering.

You should simply walk all the entries in a loop and handle the tags you care about.

Example:

https://github.com/kiznit/rainbow-os/bl ... #L302-L363