[SOLVED]bootloader can't write to memory at 0xC0000000 which is 3GB on Qemu

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
stskyblade
Posts: 12
Joined: Sun Jul 28, 2024 9:56 pm

[SOLVED]bootloader can't write to memory at 0xC0000000 which is 3GB on Qemu

Post by stskyblade »

I'm writing a bootloader to load my kernel to memory address 0xC0000000. It is at the 3GB offset.
I've tested my bootloader on both Qemu and my laptop. It works on laptop but failed on Qemu.

I use gdb to find out what's wrong. No matter what did I write to memory larger than 0xC0000000, I got zeros whan I read it back. In GDB, it says can't access memory at 0xC0000000. The last bytes accessable is 0xBFFFFFFF which is 3GB - 1Byte.

Command to run Qemu:
`qemu-system-i386 -m 5G -serial stdio disk.img`

Part of my source code:

Code: Select all

    uint8_t *buffer = (uint8_t *)(1024U * 1024 * 1024 * 3);
    memset(buffer, 0, kernel_size);

    for (uint32_t i = 0; i < 32; i++) {
        buffer[i] = i;   // write test value to 0xC0000000
    }

    printf("memory at 0xc0000000:\n");
    print_memory(buffer, 32);   // all are zeros

My bootloader is working in 32bit protected mode, without page enabled. I use a flat memory model for all 4GB memory address space in GDT.
Last edited by stskyblade on Thu Aug 29, 2024 6:31 am, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: bootloader can't write to memory at 0xC0000000 which is 3GB on Qemu

Post by iansjack »

You really need to look at the memory map before assuming that you can use a particular region. Some BIOSs reserve 0xC0000000 - 0xFFFFFFFF (or parts thereof) for memory mapped devices. It looks as if the qemu BIOS does but your laptop doesn't.

On other hardware the allocation may well fail.

https://github.com/open-mpi/hwloc/wiki/ ... -with-Qemu
nullplan
Member
Member
Posts: 1788
Joined: Wed Aug 30, 2017 8:24 am

Re: bootloader can't write to memory at 0xC0000000 which is 3GB on Qemu

Post by nullplan »

stskyblade wrote: Wed Aug 28, 2024 6:42 am I'm writing a bootloader to load my kernel to memory address 0xC0000000. It is at the 3GB offset.
But why? If you want to write a higher-half kernel, you generally load the kernel as low as possible in physical memory, then map it higher with paging. As iansjack pointed out, you cannot, in general assume there's RAM at that address. You can generally assume there's RAM at the 1MB line, and an ample amount of it, at least 14MB before something else comes along. And if your kernel is larger than 14MB you probably need to fix that before anything else.
Carpe diem!
stskyblade
Posts: 12
Joined: Sun Jul 28, 2024 9:56 pm

Re: bootloader can't write to memory at 0xC0000000 which is 3GB on Qemu

Post by stskyblade »

Thank you for your help. This is the cause of this "bug".

According to link below, I detected memory on Qemu.
https://wiki.osdev.org/Detecting_Memory ... per_Memory

Here is the result:
Screenshot_20240829_202622_qemu_memory_layout.png
Memory region starts at address 0xC0000000 is not listed.
Treat unlisted regions as Type 2 -- reserved.
So I think it shouldn't be used as normal RAM.
Post Reply