multiboot2 problems

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MakaAlbarn001
Posts: 20
Joined: Fri Jun 16, 2017 1:51 am
Libera.chat IRC: Maka_Albarn

multiboot2 problems

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: multiboot2 problems

Post 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.
MakaAlbarn001
Posts: 20
Joined: Fri Jun 16, 2017 1:51 am
Libera.chat IRC: Maka_Albarn

Re: multiboot2 problems

Post by MakaAlbarn001 »

You forgot that mem_lower is in Kilobytes.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: multiboot2 problems

Post 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.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: multiboot2 problems

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: multiboot2 problems

Post by kzinti »

Well without seeing more code, there is no bug to be found here.
MakaAlbarn001
Posts: 20
Joined: Fri Jun 16, 2017 1:51 am
Libera.chat IRC: Maka_Albarn

Re: multiboot2 problems

Post 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
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: multiboot2 problems

Post 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.
MakaAlbarn001
Posts: 20
Joined: Fri Jun 16, 2017 1:51 am
Libera.chat IRC: Maka_Albarn

Re: multiboot2 problems

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: multiboot2 problems

Post 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"?
MakaAlbarn001
Posts: 20
Joined: Fri Jun 16, 2017 1:51 am
Libera.chat IRC: Maka_Albarn

Re: multiboot2 problems

Post by MakaAlbarn001 »

That's multiboot 1. In multiboot2, there is a series of different structures that follow a pattern. Look up the multiboot2 manual.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: multiboot2 problems

Post 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
Post Reply