Page 1 of 1

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

Posted: Wed Aug 28, 2024 6:42 am
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.

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

Posted: Wed Aug 28, 2024 8:00 am
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

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

Posted: Wed Aug 28, 2024 8:42 am
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.

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

Posted: Thu Aug 29, 2024 6:30 am
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.