Search code examples
c++assemblykernelosdev

How to get more kernel memory


I'm making a custom operating system, and I'm running into some memory issues. Recently I've had issues such as this: enter image description here

I've attributed characters not appearing on screen to there not being enough kernel memory, as this runs from kernel. I'm very new to asm, c++, and OS development as a whole so there could be a lot of things I've done to cause this issue. One reason I believe this is a memory-related issue is because when I shortened my kernel.cpp file, suddenly characters appeared (though if I add too many, some disappear). Here is a link to my GitHub repository with my code: https://github.com/Subwaey/KaiOS


Solution

  • Your boot loader only loads 2 sectors (1024 bytes) of the kernel into memory.

    You could increase this a little (temporarily) by changing the mov dh, 2 to a larger value at line 15 of boot.asm.

    This has limits (e.g. limited to 255 sectors, and possibly limited by the number of sectors per track on the disk). To break that limit you have to break it up into multiple reads (e.g. if there's 18 sectors per track, then loading a 123 KiB kernel as 7 reads of not more than 18 sectors per read).

    The next limit is caused by real mode only being able to access 640 KiB of RAM (where some of that is BIOS data, etc). For this reason (and because most kernels grow to be much larger than 640 KiB) you want a loop that loads the next piece of the kernel's file into a buffer, then switches to protected mode and copies that piece elsewhere (e.g. starting at 0x0100000 where there's likely to be a lot more contiguous RAM you can use), then switches back to real mode; until the whole kernel is loaded and copied elsewhere.

    Of course hard-coding the "number of sectors in kernel's file" directly into the boot loader is fragile. A better approach would be to have some kind of header at/near the start of the kernel's file that contains the size of the kernel; where boot loader loads the header and uses it to determine how many sectors it needs to load.

    Also; I suspect you don't actually have a separate file for the kernel - it's just a single file containing "boot loader + kernel". This is likely to be a major problem when you try to get the kernel to boot in different scenarios (from partitioned hard disks where boot loader has to care about which partition it was installed in, from CD-ROM where it's all completely different, from network where it's all completely different, etc).