Virtual address of heap

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
Whatever5k

Virtual address of heap

Post by Whatever5k »

Currently, I'm implementing my morecore() function. Therefore I need to chose an address for my heap...what would be the best?
Should I do virt_heap = phys_heap? Up to now, I have the following construction:

virtual 0x0 -> physical 0x100000
and I map 1024 pages (4mb). So that would mean:

virt_heap = phys_heap - 0x10000;

That's ok, but I need to make sure that the page tables containing the physical addresses of the heap won't be changed...I could do it like this:

Code: Select all

void page_fault(addr_t address)
{
if (addr <= virtual_kernel_end)
   panic("page fault within kernel space");
else
   handle_it();
}
But this doesn't look very nice to me...it would mean I couldn't handle page faults within kernel space...not good.
So what do you think? Which constellation should I take?

best regards,
A. Blessing
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Virtual address of heap

Post by Pype.Clicker »

i'm afraid i don't understand what is troubling you ... what's the problem with having on-demand paging for the kernel ?
DarylD

Re:Virtual address of heap

Post by DarylD »

Same here, I think you are getting yourself confused over this.

Take a deep breath, step back and look again at your problem, I think you will realise there isn't a problem at all!!

Daryl.
Whatever5k

Re:Virtual address of heap

Post by Whatever5k »

Problem:

To which physical address should I map the virtual address of the heap?
My OS is booted by GRUB, so the kernel is loaded at 1MB...I can also locate the physical address of the kernel (end of BSS)...
DarylD

Re:Virtual address of heap

Post by DarylD »

You can choose whatever physical address you want, I personally would figure out where the kernel finishes and use memory beyond that.

If you put a _end in the linker script, you can use this to calculate how many pages of memory the kernel uses, add this to 0x100000 and bingo, there is physical pages to start using for your heap.

To make it even easier, you could start at say 0x400000 (4MB mark) and allocate from there.

Remember to remove pages used from your free page stack/bitmap in the process otherwise you will "lose" memory.

Daryl.
Whatever5k

Re:Virtual address of heap

Post by Whatever5k »

Ok, you say I should allocate a memory region beyond kernel end...what do the others think?

Let's say I map the heap to 4 MB physical. If so, I have to make sure that the virtual address of the heap *always stays* 4 MB. I mean I have to make sure that the page tables concerning the virtual heap/4 MB won't get changed...do you get me?
How should I do this? The only place where page tables can be changed is the page fault handler...Could I do it like this? :

Code: Select all

void page_fault(addr_t phys_addr)
{
   if (phys_addr >= FOUR_MB && phys_addr <= (FOUR_MB + HEAP_SIZE))
         panic("page fault within heap space ");
   ...
}
What do you think of this, or do you even have a better idea?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Virtual address of heap

Post by Pype.Clicker »

first of all, a page fault would give you is a *logical* address (as most #PF come from a non-present page, it would be pretty silly to require the physical address, imho)

If i understood what you wish, your so-called "kernel heap" uses fixed paging (statically allocated at system initialization), right ?

So, if these pages are valid, why would they raise a page fault ? Because of a user access ? well, i guess you can solve this with "memory regions" handling:

Code: Select all

class pageFault {
handler() {
region r=find_region_with_vma(pagefault.vma);
boolean handled=false;
if (region!=null)
  handled=region.handle(pagefault.vma, pagefault.usermode);
if (!handled) {
  log.print("unresolved page fault"+pagefault);
  current.process().kill();
}
}
}

class kernelHeapRegion extends Region {
   handle(vma v, boolean user) {
      if (user) return false;
      log.print("unexpected #PF in kernel mode at kernel heap"+v);
   }
}

class kernelVirtualRegion extends Region {
  handle(vma v, boolean user) {
     if (user) return false;
     pager.pte(v).frame=pager.alloc_new_frame();
     pager.pte(v).present=true;
  }
}
Hope it helps figuring out...
Whatever5k

Re:Virtual address of heap

Post by Whatever5k »

[quote[ if these pages are valid, why would they raise a page fault
What if a page fault occurs (because of a non-present page, not in the heap region but s.w. else)? I would have to swap out some page frames, but I have to make sure that the physical heap region won't be swapped out...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Virtual address of heap

Post by Pype.Clicker »

just inform the pager class that the physical range you reserved for your heap is locked and unavailable for swapping.

For instance,

Code: Select all


class Pager {
    
    static {
        int all=MemInfo.getMaxMemory();
        int hp=KernelHeap.getBase();
        int hpe=KernelHeap.getSize();
        
        Pager.empty();
        Pager.addPages(low=hp+hpe, hi=all);
    }
}
makes sense ?
Whatever5k

Re:Virtual address of heap

Post by Whatever5k »

Yes, I will probably do that (I'm using a bitmap but that's similar to implement)...
Hm, I just thought of choosing ebss as the physical address of the heap...is that OK? If yes, what virtual address would be ok?
Whatever5k

Re:Virtual address of heap

Post by Whatever5k »

Uh, no more replies?
So, let's summarize things:
1. I will probably put the physical heap at the end of the BSS section. Question: is this OK?
2. I will ensure that this memory region won't be swapped out by cleaning this part of region out of the bitmap - that *is* OK ;)
3. I will have to decide which virtual address to choose...so:

Which virtual address should I choose? I know I can choose any but what is a good one, what would be logic?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Virtual address of heap

Post by Pype.Clicker »

the physical placement seems okay ... maybe you could just make sure it will leave enough "buffer-class" memory for ISA DMA buffers (<16MB)

Putting the virtual address of the heap after the virtual address of the kernel's BSS seems fine too ... Maybe you can just enforce a blank page before and after it so that you can catch trivial buffer overflows ...

If virtual addresses allocation troubles you (i.e. you don't want to force a specific virtual address), just use a top-down or bottom-up allocation scheme for placing the objects in the kernel address space.
Whatever5k

Re:Virtual address of heap

Post by Whatever5k »

Putting the virtual address of the heap after the virtual address of the kernel's BSS seems fine too
Well, I don't have a virtual address of the BSS either, yet. Actually, how would that be?:

Code: Select all

void init_mm()
{
addr_t heap;
...
mm_map(0, 0x100000, 1024);
heap = phys_ebss - 0x100000;
...
}
The call to mm_map() would map 1024 pages, starting to map virtual 0 to physical 1 MB...
Consequently, the heap would be phys_ebss - 1 MB...

Is this OK or do you have a better idea?
Post Reply