Heap/Paging Questions

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.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Heap/Paging Questions

Post by Creature »

I modified the 'check-for-page-alignment', still no success however. On the SVN repository, the code is up-to-date, but the floppy doesn't always update. Here's an image with the kernel binary on revision 75 (the one I committed a few minutes ago after posting, the file is called 'Floppy (r75).zip'). I will try the magic breakpoints soon.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Heap/Paging Questions

Post by Creature »

berkus wrote:You may want to throw out JamesM example code, as it is indeed very buggy and intended only as an example, and write your own from scratch, or try pondering at my copy (also based off of JamesM tutorial) here.

Drawing pictures of what is happening with the memory also helps, at least me.
I guess it's time to revise the entire thing then. I think I'll best start with revising the paging code, since the heap works fairly fine except for the paging issues, which I think is not the heap causing it. The heap is also self-written, but simply inspired by JamesM.

Thanks for all your help, other suggestions are still welcome too
Creature
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Heap/Paging Questions

Post by Creature »

Sorry for double post, but I seem to have found the problem! In the GetPage function (based on JamesM's), the function AllocAlignGetPhys (or kmalloc_ap) function gets called. However, this redirects to the AllocEx (kmalloc_int) function, which checks if the heap exists (and it does) and then calls Heap::Alloc all over again! The heap will then try to expand AGAIN, resulting in an infinite loop. I solved this by removing the

Code: Select all

else if(make)
    {
        u32int tmp;
        dir->tables[table_idx] = (page_table_t*)kmalloc_ap(sizeof(page_table_t), &tmp);
        memset(dir->tables[table_idx], 0, 0x1000);
        dir->tablesPhysical[table_idx] = tmp | 0x7; // PRESENT, RW, US.
        return &dir->tables[table_idx]->pages[address%1024];
    }
Code from the GetPage function and instead, created all the page tables up front, before the placement address is frozen and identity mapped (as it should). This gave me the following code:

Code: Select all

unsigned PhysAddr;
	unsigned i;

	for(i = 0; i < 1024; ++i)
	{
		KernelDir->PageTables[i] = ((PageTable *) AllocAlignGetPhys(sizeof(PageTable), &PhysAddr));
		memset(KernelDir->PageTables[i], 0, PAGE_SIZE);
		KernelDir->TablesPhys[i] = PhysAddr | 0x7; /* The physical address with the 'Present', 'Read-Write' and 'UserMode' set. */
	}
Which goes above

Code: Select all

/* Map kernel heap pages, creating them if necessary. */
	for(i = HEAP_START; i < HEAP_START + HEAP_START_SIZE; i += PAGE_SIZE)
		GetPage(i, true, KernelDir);
And fixed my issue (the paging initialization time is significantly increased, though, but I have little choice). The Heap::Alloc function was going berserk after looping so many times and that was also why the "#0" string kept getting printed.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Heap/Paging Questions

Post by frank »

I would start by separating the allocators into three very separate levels.
  • A physical memory allocator- allocates physical pages or frames as they are also called
  • A virtual memory allocator- maps physical addresses to virtual ones accesses physical memory allocator for pages
  • Heap- manages malloc type allocations, accesses virtual memory allocator when it runs out of pages
Then don't let a lower level access a higher level. That might eliminate some of the circular dependencies you are having.
Post Reply