Page 1 of 1

[Closed]My kernel just unexpectedly stops when setting up paging on a real laptop computer

Posted: Mon Jan 13, 2025 9:05 pm
by stskyblade
Before enabling paging in kernel, I need to set up memory paging first. In my design, I need to map about hundreds of MB. It's really a time-consuming task, especially on a real laptop computer. My kernel unexpectedly stops. This bug doesn't happen on Qemu. I have no idea how to solve it.

My laptop is quite old. I bought it in 2013. It has "Intel i5-3230M (4) @ 3.200GHz" CPU and 8GB RAM.

Code: Select all

    trace("step3.1");
    PTE &entry = page_table[entry_offset];
    trace("step3.2");
    entry.p = 1;
    entry.rw = 1;
    trace("step3.3");
    entry.user_or_supervisor = user_level;
    trace("step3.4");
    entry.reserved1 = 0;
    trace("step3.4.1");
    entry.access = 0;
    trace("step3.4.2");
    entry.dirty = 0;
    trace("step3.4.3");
    entry.reserved2 = 0;
    trace("step3.4.4");
    entry.avail = 0;
    trace("step3.5");
    entry.address =
        reinterpret_cast<uint32_t>(physical_address) >> 12; // save high 20bit
    trace("step4");
}
This block of code will execute hundreds of thousands times. There will be lots of outputs on screen. At first, output messages are flushed very fast. Just before the bug, output messages are flushed slowly, line by line. Then the bug comes, there is a message "step3.4.4". And nothing happens. "step3.5" is never printed to screen. The laptop doesn't restart. It just stops.

I have read the assembly code, can't find anything weird.

Code: Select all

400069fe:       8b 45 d8                mov    -0x28(%ebp),%eax   
40006a01:       0f b6 50 01             movzbl 0x1(%eax),%edx       
40006a05:       83 e2 f1                and    $0xfffffff1,%edx                                                                         
40006a08:       88 50 01                mov    %dl,0x1(%eax)
My code:
https://github.com/stskyblade/StarOS/tr ... ocess-soft
https://github.com/stskyblade/StarOS/bl ... g.cpp#L120

How to build:
git clone -b dev-process-soft https://github.com/stskyblade/StarOS.git
cd StarOS
mkdir build
cd build
cmake -DUSB_DEVICE:STRING=/dev/sda .. # be very careful. /dev/sda is the device where my code will burn the disk image into
cmake --build . --target burn # Burning image happends here

Then restart computer from the hard disk above in IDE mode. My kernel only supports IDE mode of SATA disk.

Re: My kernel just unexpectedly stops when setting up paging on a real laptop computer

Posted: Mon Jan 13, 2025 10:02 pm
by Octocontrabass
Check the memory map. Are you trying to store your page tables outside of usable memory?

Re: My kernel just unexpectedly stops when setting up paging on a real laptop computer

Posted: Wed Jan 15, 2025 1:48 am
by stskyblade
You are right. This bug is caused by accessing invalid memory.

I used to think the content in memory after boot is all zero. Based on this assumption, I wrote my kernel. But the memory is not blank after boot on a real computer. The unexpected value in memory leads me to access invalid memory.
Octocontrabass wrote: Mon Jan 13, 2025 10:02 pm Check the memory map. Are you trying to store your page tables outside of usable memory?
Thank you! :D