OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 3:22 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Physical Memory Manager Initialization Error
PostPosted: Fri Jun 16, 2017 8:56 am 
Offline
User avatar

Joined: Fri Jun 16, 2017 8:36 am
Posts: 2
Location: United States
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:
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:
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:
File comment: Source Code
DarkOS.zip [52.55 KiB]
Downloaded 17 times
Top
 Profile  
 
 Post subject: Re: Physical Memory Manager Initialization Error
PostPosted: Fri Jun 16, 2017 3:40 pm 
Offline
User avatar

Joined: Fri Jun 16, 2017 8:36 am
Posts: 2
Location: United States
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 171 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group