OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:24 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Making all installed memory available causes a triple fault
PostPosted: Wed Nov 29, 2017 6:22 pm 
Offline

Joined: Tue Feb 28, 2017 11:44 am
Posts: 7
Hi,
I am trying to start using all the installed memory on my computer instead of using just 16mb. I have no problems detecting memory, nor parsing the memory map, but when I attempt to deinitialize a used memory region (by making the frame unavailable in my physical memory manager) and to enable paging after this my kernel triple faults. If I remove the code that deinitializes the regions the kernel works fine with all the memory (until the point where it tries to use memory in a used region).

The code that is causing the issue is the following:
Code:
int initRegions(multiboot_info_t* bootinfo) {
    // Check GRUB passed the memmory map
    if (!(bootinfo->flags & MULTIBOOT_INFO_MEM_MAP)) {
        PANIC("Couldn't load memory map");
    }
    memory_map_t* mmap = bootinfo->mmap_addr;
    uint8_t regNum = 0;
    while (mmap < bootinfo->mmap_addr + bootinfo->mmap_length) {
        if (mmap->type > 4) {
          mmap->type = 2;
        }
        kprint("Found section ");kernelPrintDec(regNum);kprint(" of type ");kprint(memTypes[mmap->type]);kprint(". \n");
        kprint("Section base: ");kernelPrintHex(mmap->base_addr_low); kprint("\n");
        if (mmap->type == 2) {
          for (uint64_t i = 0; i < mmap->length_low; i += 0x1000) {
              if (mmap->base_addr_low + i > 0xFFFFFFFF) break;
              set_frame((mmap->base_addr_low + i) /*& 0xFFFFF000*/);
          }
          kprintf("Deinitialized region %i\n", regNum);
        }
        mmap = (memory_map_t*)((unsigned int)mmap + mmap->size + sizeof(mmap->size));
        regNum++;
    }
}


And the code that enables paging is the following:
Code:
void switch_page_directory(page_directory_t *dir) {
    current_directory = dir;
    asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical));
    uint32_t cr0;
    asm volatile("mov %%cr0, %0": "=r"(cr0));
    cr0 |= 0x80000000; // Enable paging!
    asm volatile("mov %0, %%cr0":: "r"(cr0));
}

You can find my kernels sourcecode at: https://github.com/sofferjacob/jslk, the memory related code is located at "hal/x86/memory".
Thanks in advance


Top
 Profile  
 
 Post subject: Re: Making all installed memory available causes a triple fa
PostPosted: Thu Nov 30, 2017 9:42 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
If I take that code out and build it, how do I get it to triple fault?i ran it and didn't have a fault, so there must be something environmental (version of the i386 GCC cross compiler?) altering the behaviour. Either that or the code in Github isn't the version that crashes?


Top
 Profile  
 
 Post subject: Re: Making all installed memory available causes a triple fa
PostPosted: Thu Nov 30, 2017 9:49 am 
Offline

Joined: Tue Feb 28, 2017 11:44 am
Posts: 7
Yes, you are right the code on GitHub wasn't updated, I forgot to push, but it is updated now if you build it and run it you will get a triple fault after switching page directories.

Sorry for that and thank you.


Top
 Profile  
 
 Post subject: Re: Making all installed memory available causes a triple fa
PostPosted: Thu Nov 30, 2017 10:09 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

sofferjacob wrote:
Code:
        if (mmap->type == 2) {
          for (uint64_t i = 0; i < mmap->length_low; i += 0x1000) {
              if (mmap->base_addr_low + i > 0xFFFFFFFF) break;
              set_frame((mmap->base_addr_low + i) /*& 0xFFFFF000*/);
          }


If the memory map has a region of RAM from 0x00000000 to 0x0009F800, this code will assume that there's a page of usable RAM starting at 0x0009F000 even though half of the page is not usable RAM.

If the memory map has a region of RAM from 0x00123800 to 0x00124000, this code will assume that there's a page of usable RAM starting at 0x00123000 even though half of the page is not usable RAM.

Of course assuming things are usable RAM when they're not usable RAM can cause problems later on; and the symptoms will depend on the exact memory map (e.g. for some computers everything might already be aligned on page boundaries so nothing goes wrong).

You want to do something more like:

Code:
        if (mmap->type == 2) {

            // Align start up to next page boundary (and clamp to 32-bit)
            aligned_start = (mmap->base_addr_low + 0x0000000000000FFF) & 0xFFFFFFFFFFFFF000;
            if(aligned_start < 0x00000000FFFFFFFF) {

                // Align end down to page boundary (and clamp to 32-bit)
                aligned_end = (mmap->base_addr_low + mmap->length_low) & 0xFFFFFFFFFFFFF000;
                if(aligned_end > 0x0000000100000000) {
                   aligned_end = 0x0000000100000000;
                }

                // Do each page from "aligned start" to "aligned end"

                for (uint64_t i = aligned_start; i < aligned_end; i += 0x1000) {
                    set_frame(i);
                }
           }



Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Making all installed memory available causes a triple fa
PostPosted: Thu Nov 30, 2017 11:36 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
Appears your identity mapping code has mapped all the pages you want to identity map to physical page at 0x00000000. This of course is incorrect. When you turn on paging you'll end up faulting soon after since you'll be executing data in the base of memory as instructions. Any interrupts will fault since the IDT will now be munged as it will be pointing at the bottom of memory too.

On a side note, since your kernel is using the x87 FPU you should check for the x87 and then initialize it. There is information on the OSDev wiki about this.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 69 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