OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 12:33 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 7:05 am 
Offline
Member
Member

Joined: Fri Jan 26, 2018 11:43 am
Posts: 64
When my kernel is loaded by grub, it begins reading the multiboot info. First, the memory limits, and then the memory map. My problem is that the reported address of the memory map is 0x100a4, which happens to be in the middle of my kernel code.

What I assume is happening is that grub is creating the table, then loading my kernel source right over it, but why is it doing this? Surely it knows how big my kernel is, and should know not to do this. What possible fixes are there? Can I tell grub to load the kernel elsewhere?


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 8:07 am 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
Grub Legacy or Grub2?

Greetings
Peter


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 8:16 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5143
j4cobgarby wrote:
What I assume is happening is that grub is creating the table, then loading my kernel source right over it, but why is it doing this?

GRUB is designed to load the operating system above 1MB. I wouldn't be surprised if it assumes you will never request a load address below 1MB and doesn't validate the request.

j4cobgarby wrote:
What possible fixes are there?

Tell GRUB to load your OS at 1MB or higher.

j4cobgarby wrote:
Can I tell grub to load the kernel elsewhere?

Yes. :P

Without seeing your code, I can't point you at exactly what you need to change, but it'll probably be one of the addresses in your linker script.


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 8:47 am 
Offline
Member
Member

Joined: Fri Jan 26, 2018 11:43 am
Posts: 64
Octocontrabass wrote:
j4cobgarby wrote:
What I assume is happening is that grub is creating the table, then loading my kernel source right over it, but why is it doing this?

GRUB is designed to load the operating system above 1MB. I wouldn't be surprised if it assumes you will never request a load address below 1MB and doesn't validate the request.

j4cobgarby wrote:
What possible fixes are there?

Tell GRUB to load your OS at 1MB or higher.

j4cobgarby wrote:
Can I tell grub to load the kernel elsewhere?

Yes. :P

Without seeing your code, I can't point you at exactly what you need to change, but it'll probably be one of the addresses in your linker script.


It seems that my kernel is being loaded at 1MB. Here's my linker script:

Code:
ENTRY(_start)

SECTIONS
{
    /* start putting code at 1MB, apparently this is a
     * normal place for kernel code to be loaded from*/
    . = 0x100000;

    /* multiboot header has to be early on in the image so
     * that grub finds it */
    .text BLOCK(4K) : ALIGN(4K)
    {
        *(.multiboot)   /* first the multiboot header */
        *(.text)        /* now the actual entry code */
    }

    /* now the uninitialised data */
    .bss BLOCK(4K) : ALIGN(4K)
    {
        *(.bss) /* just the stacks */
    }
}


Is it the linker script that informs grub of where to load the kernel?


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 8:55 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5143
The linker script determines what values the linker will put in the ELF headers, and GRUB can read the ELF headers to determine where to load your kernel. The multiboot header can also contain a load address, though, and I can't recall off the top of my head which one takes priority when both are present.

According to your linker script, your kernel will be loaded at 0x100000. Is 0x100a4 really in the middle of your kernel code?


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 9:34 am 
Offline
Member
Member

Joined: Fri Jan 26, 2018 11:43 am
Posts: 64
Octocontrabass wrote:
The linker script determines what values the linker will put in the ELF headers, and GRUB can read the ELF headers to determine where to load your kernel. The multiboot header can also contain a load address, though, and I can't recall off the top of my head which one takes priority when both are present.

According to your linker script, your kernel will be loaded at 0x100000. Is 0x100a4 really in the middle of your kernel code?


You know, it's possible that I misread some address. In hindsight I'm not sure if it was in the middle of my code...
At least I've learnt that I can move my kernel around by using the linker script, I was never really that solid on how the linker script worked, so thanks!


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 9:43 am 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
Octocontrabass wrote:
The linker script determines what values the linker will put in the ELF headers, and GRUB can read the ELF headers to determine where to load your kernel. The multiboot header can also contain a load address, though, and I can't recall off the top of my head which one takes priority when both are present.

According to your linker script, your kernel will be loaded at 0x100000. Is 0x100a4 really in the middle of your kernel code?

You sure are the expert, but maybe the content of "multiboot" is the reason?

Greetings
Peter


Top
 Profile  
 
 Post subject: Re: My kernel code is overwriting grub's memory map table.
PostPosted: Wed Jun 03, 2020 11:57 am 
Offline
Member
Member

Joined: Fri Jan 26, 2018 11:43 am
Posts: 64
PeterX wrote:
Octocontrabass wrote:
The linker script determines what values the linker will put in the ELF headers, and GRUB can read the ELF headers to determine where to load your kernel. The multiboot header can also contain a load address, though, and I can't recall off the top of my head which one takes priority when both are present.

According to your linker script, your kernel will be loaded at 0x100000. Is 0x100a4 really in the middle of your kernel code?

You sure are the expert, but maybe the content of "multiboot" is the reason?

Greetings
Peter

I haven't specified a load address in the header, so that's probably not it here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot], Google [Bot] and 124 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group