Page 1 of 1

Making all installed memory available causes a triple fault

Posted: Wed Nov 29, 2017 6:22 pm
by sofferjacob
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: Select all

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: Select all

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

Re: Making all installed memory available causes a triple fa

Posted: Thu Nov 30, 2017 9:42 am
by MichaelPetch
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?

Re: Making all installed memory available causes a triple fa

Posted: Thu Nov 30, 2017 9:49 am
by sofferjacob
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.

Re: Making all installed memory available causes a triple fa

Posted: Thu Nov 30, 2017 10:09 am
by Brendan
Hi,
sofferjacob wrote:

Code: Select all

        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: Select all

        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

Re: Making all installed memory available causes a triple fa

Posted: Thu Nov 30, 2017 11:36 am
by MichaelPetch
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.