Physical Memory Manager Initialization Error

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
User avatar
jaller200
Posts: 2
Joined: Fri Jun 16, 2017 8:36 am
Libera.chat IRC: jaller200
Location: United States

Physical Memory Manager Initialization Error

Post by jaller200 »

So I have been tinkering around with OS development for months now, constantly writing and rewriting code to help me better understand some of the intricate details of how a kernel and bootloader work. However, I am now running into a strange issue that I've never seen before.

I started writing a new iteration of my kernel from scratch, taking inspiration from multiple locations (including James Molley's Kernel tutorials, a little from Bran's Kernel tutorials, and some from BrokenThorne tutorials). I have managed to construct a simple bootloader that finds and loads the kernel image into memory at 0x10000 after setting up the GDT and enabling the A20 gate. My kernel then flushes the GDT (essentially reinitializing it), sets up the IDT and the IVT, and starts the PIT. I decided to go ahead and start working on memory management based on BrokenThorne's tutorials, as I've done in the past, but this time whenever I initialize the Physical Memory Manager (PMM) it crashes the OS.

Code: Select all

void pmm_init(uint32_t size, uint32_t bitmap) {
	pmm_memory_size	= size;
	pmm_bitmap		= (uint32_t *)bitmap;
	pmm_max_blocks	= (pmm_memory_size * 1024) / PMM_BLOCK_SIZE;
	pmm_used_blocks	= pmm_max_blocks;

	// By default, all memory is in use
	printf("%x | size: %x\n", pmm_bitmap, pmm_max_blocks / PMM_BLOCKS_PER_BYTE);
	memset((void *)pmm_bitmap, 0xF, (pmm_max_blocks / PMM_BLOCKS_PER_BYTE));
}
The above is my initialization function. I have narrowed the problem to the memset function here:

Code: Select all

uint8_t * memset(uint8_t * dest, uint8_t val, size_t len) {
	for (; len != 0; len--) {
		*dest++ = val;
	}	
	return dest;
}
Whenever I comment the function call out from pmm_init the error disappears, which means there is an error writing to memory. I have triple-checked my memset function, examined (in depth) my GDT initialization, and even debugged where the start of the memory being written to was, and the size of it.

If anyone has any ideas on what might be the cause of the issue here, I would love to hear them. I have attached my code below for reference.

Another quick thing that I have noted is that if I keep hardware interrupts disabled (never call "sti" from my kernel) then the issue "disappears", although the next printed line is in a weird location.

Also, if I add 6 sectors to the kernel size when initializing the memory, the issue vanishes, but I don't want to use arbitrary numbers if I have no idea why they would be needed in the first place. All I know is that the OS loads the correct number of sectors (17 sectors since the binary image is 8548 bytes) so the starting location for my PMM Bitmap (0x10000 + (17 * 512)) shouldn't conflict with the kernel, and I know the memory (based on the memory map) between 0x100000 and 0x7EE0000 is available for use.

Thanks in advance!
Attachments
DarkOS.zip
Source Code
(52.55 KiB) Downloaded 33 times
User avatar
jaller200
Posts: 2
Joined: Fri Jun 16, 2017 8:36 am
Libera.chat IRC: jaller200
Location: United States

Re: Physical Memory Manager Initialization Error

Post by jaller200 »

As an update, I figured it out and I am kicking myself because of it :(

I was ignoring the data and bss sections I had put into the linker script, so I just pointed the start of the PMM bitmap to the end of the kernel (via the _end symbol in kernel_entry.ld) and it works.

Thanks anyways!
Post Reply