Page 1 of 1

Trying to get multiboot info(memory map) from grub2 in 64bi.

Posted: Mon Feb 01, 2021 7:06 am
by JustVic
Trying to get multiboot info(memory map) from grub2 in 64-bit long mode but always get strange wrong data.

github link: https://github.com/JustVic/kernel_grub_multiboot_info

in the _start function I copied multiboot2 magic number and info addres to variables:

Code: Select all

_start:
        movl $stack_top, %esp

        movl    %eax, multiboot_magic
        movl    %ebx, multiboot_info
varieables declaration:

Code: Select all

.data
multiboot_magic:
	.long 0
multiboot_info:
	.long 0
after this I setup the 64-bit long mode and call _start64 function according to calling conventions mov magic number and info numbers to rdi and rsi:

Code: Select all

.code64

.global _start64
.type _start64, @function
_start64:
        // Setup segment selectors
        movw $0, %ax
        movw %ax, %ds
        movw %ax, %es
        movw %ax, %fs
        movw %ax, %gs
        movw %ax, %ss

	mov    multiboot_magic, %rsi
	mov    multiboot_info, %rdi

	call cmain
and trying to get this data from c:

Code: Select all

int cmain(unsigned long addr, unsigned long magic)
{
}
and parse it like multiboot2 gnu example.

What I did wrong? And How to send memory map data to c lang after 64-bit long mode setup from grub multiboot info?

Screenshot of the wrong data:
https://pixelfed-prod.nyc3.cdn.digitalo ... w2VOio.png

Re: Trying to get multiboot info(memory map) from grub2 in 6

Posted: Mon Feb 01, 2021 9:41 am
by kzinti

Code: Select all

        movl    %eax, multiboot_magic
        movl    %ebx, multiboot_info

Code: Select all

	mov    multiboot_magic, %rsi
	mov    multiboot_info, %rdi
You are storing these values as 32 bits but reading them back as 64 bits. So this won't work.

I would define both variables as qwords (64 bits) and add an explicit size suffix to your move instructions (i.e. movq).

Re: Trying to get multiboot info(memory map) from grub2 in 6

Posted: Mon Feb 01, 2021 10:55 am
by JustVic
kzinti wrote:

Code: Select all

        movl    %eax, multiboot_magic
        movl    %ebx, multiboot_info

Code: Select all

	mov    multiboot_magic, %rsi
	mov    multiboot_info, %rdi
You are storing these values as 32 bits but reading them back as 64 bits. So this won't work.

I would define both variables as qwords (64 bits) and add an explicit size suffix to your move instructions (i.e. movq).

You right. But if I change code to this:

Code: Select all

.data
multiboot_magic:
   .quad 0
multiboot_info:
   .quad 0
------

Code: Select all

movq    multiboot_magic, %rsi
	movq    multiboot_info, %rdi
I still get the same results.

Re: Trying to get multiboot info(memory map) from grub2 in 6

Posted: Mon Feb 01, 2021 4:49 pm
by kzinti
Keep the variables 32 bits long, but load them as 32 bits value into %esi and %edi. The upper 32 bits of %rsdi and %rdi will be automatically cleared to zero for you.

Re: Trying to get multiboot info(memory map) from grub2 in 6

Posted: Wed Feb 03, 2021 7:49 am
by JustVic
In memory the entire multiboot_info structure right and address passed correctly. I checked it with gdb. All the problem in the printing to screen function and itoa functions. But it is a little strange that all that code print right with my simple 32bit example.

Re: Trying to get multiboot info(memory map) from grub2 in 6

Posted: Wed Feb 03, 2021 5:53 pm
by Octocontrabass
You need to use the stdarg.h macros to read the arguments to your printf function.