Bitmap or stack based memory manager
-
Whatever5k
Re:Bitmap or stack based memory manager
Tim:
I see what you mean, but I always thought that you cannot just map a physical address to *any* (virtual) address. How do you that (how would MemMap() look like)? Because I thought the virtual address is built depending on which page table, which page directory, etc. I use.
Or am I wrong?
I see what you mean, but I always thought that you cannot just map a physical address to *any* (virtual) address. How do you that (how would MemMap() look like)? Because I thought the virtual address is built depending on which page table, which page directory, etc. I use.
Or am I wrong?
-
pskyboy
Re:Bitmap or stack based memory manager
You essentially just pop the next page frame of the stack and stick its address into the page table entry. You then have mapped that address to whatever physical address.
Peter
Peter
-
Whatever5k
Re:Bitmap or stack based memory manager
pskyboy:
I think you didn't understand me well. I want to map a physical address to *any* virtual address, not the other way round. Besides, I'm not using a stack to get physical page frames, but a bitmap...
By the way, which address would you then return (which virtual address)? How do you build the virtual address?
I think you didn't understand me well. I want to map a physical address to *any* virtual address, not the other way round. Besides, I'm not using a stack to get physical page frames, but a bitmap...
By the way, which address would you then return (which virtual address)? How do you build the virtual address?
-
pskyboy
Re:Bitmap or stack based memory manager
In that case you just take the address of the page frame you want to map and stick that in the page table entry that maps to your vritual address.
So lets say you have virtual address 16mb - 16Mb + 1Page. This would be at the bottom of the 5th Page Table as each page table (which are pointed to by the page directory) are 4mb in size. You would then take the physical address you want to use and place this in the page table entry at the bottom of teh 5th page table.
You have then just assigned that physical address to the virtual address 16Mb - 16Mb + 1 Page.
I hope this helps if not ill try to explain it a different way. I must admit it took me a while to realise how you could map any virtual address to physical address.
Peter
So lets say you have virtual address 16mb - 16Mb + 1Page. This would be at the bottom of the 5th Page Table as each page table (which are pointed to by the page directory) are 4mb in size. You would then take the physical address you want to use and place this in the page table entry at the bottom of teh 5th page table.
You have then just assigned that physical address to the virtual address 16Mb - 16Mb + 1 Page.
I hope this helps if not ill try to explain it a different way. I must admit it took me a while to realise how you could map any virtual address to physical address.
Peter
-
Whatever5k
Re:Bitmap or stack based memory manager
Yes, that's what I thought, too. But Tim took just an address (in this case: start) and returned this address to the user although he didn't do any calculation or something like this. How can he just return an address - he should have calculated the virtual address by taking the page table entries and page directory entries...
-
pskyboy
Re:Bitmap or stack based memory manager
As i understand it the function he posted is making physical memory addresses to virtual addresses directly so the OS is in a certain bit of physical memory. So 4Mb virtual address is 4Mb physical address
Peter
Peter
-
Tim
Re:Bitmap or stack based memory manager
Here's my MemMap function:
What it does is:
1. Checks whether there is already a page table spanning the given virtual address. If there isn't, it allocates a new page table and puts its address into the page directory.
2. Puts the physical address into the page table.
Because each page directory has the page directory mapped in place of the top page table, the ADDR_TO_PDE and ADDR_TO_PTE macros let you manipulate page directory and page table entries without accessing physical memory directly. I think I mentioned this technique in one of my memory tutorials.
Code: Select all
#define PAGE_SIZE 4096
#define PAGE_DIRENT(X) (((X) >> 22) & 0x3FF)
#define PAGE_ALIGN(addr) ((addr) & -PAGE_SIZE)
#define PAGETABLE_MAP (0xffc00000)
#define PAGEDIRECTORY_MAP (0xffc00000 + (PAGETABLE_MAP / (1024)))
#define ADDR_TO_PDE(v) (addr_t*)(PAGEDIRECTORY_MAP + \
(((addr_t) (v) / (1024 * 1024))&(~0x3)))
#define ADDR_TO_PTE(v) (addr_t*)(PAGETABLE_MAP + ((((addr_t) (v) / 1024))&(~0x3)))
bool MemMap(addr_t virt, addr_t phys, addr_t virt_end, uint16_t priv)
{
addr_t *pde;
virt = PAGE_ALIGN(virt);
phys = PAGE_ALIGN(phys);
virt_end = PAGE_ALIGN(virt_end);
for (; virt < virt_end; virt += PAGE_SIZE, phys += PAGE_SIZE)
{
pde = ADDR_TO_PDE(virt);
if (*pde == 0)
{
addr_t pt;
pt = MemAlloc();
if (pt == NULL)
return false;
if (virt >= 0x80000000)
{
pt |= PAGE_GLOBAL;
kernel_pagedir[PAGE_DIRENT(virt)] = pt
| PRIV_WR | PRIV_USER | PRIV_PRES;
}
*pde = pt | PRIV_WR | PRIV_USER | PRIV_PRES;
}
*ADDR_TO_PTE(virt) = phys | priv;
__asm__("invlpg %0"
:
: "m" (virt));
}
return true;
}1. Checks whether there is already a page table spanning the given virtual address. If there isn't, it allocates a new page table and puts its address into the page directory.
2. Puts the physical address into the page table.
Because each page directory has the page directory mapped in place of the top page table, the ADDR_TO_PDE and ADDR_TO_PTE macros let you manipulate page directory and page table entries without accessing physical memory directly. I think I mentioned this technique in one of my memory tutorials.
-
Whatever5k
Re:Bitmap or stack based memory manager
I see...so you are looking for the appropriate (right) page table and page directory (to ensure that the virtual address(virt) will be correctly mapped to phys), right? Because you cannot choose any page table/page directory since we want to map the physical address to a certain virtual address. I've heard that the virtual address is build with this formula:
(pde_index * PD_SIZE + pte_index) * PAGE_SIZE
Is that right? If yes, does your code (Tim) gets the page directory and page table with this formula (just the other way round). If so, I think I've understood everything
Nevertheless, I'll have a look at the memory tutorials again (because of the selfmapping - don't understand it exactly)...
(pde_index * PD_SIZE + pte_index) * PAGE_SIZE
Is that right? If yes, does your code (Tim) gets the page directory and page table with this formula (just the other way round). If so, I think I've understood everything
Nevertheless, I'll have a look at the memory tutorials again (because of the selfmapping - don't understand it exactly)...
-
Tim
Re:Bitmap or stack based memory manager
Exactly. The virtual address determines which part of the page table and page directory are used.
You don't need to use the self-mapped page directory trick if you're happy poking bytes at physical addresses. I only use that because I don't want all of physical memory mapped into virtual memory.
You don't need to use the self-mapped page directory trick if you're happy poking bytes at physical addresses. I only use that because I don't want all of physical memory mapped into virtual memory.
-
Whatever5k
Re:Bitmap or stack based memory manager
Oh yes, I've got it. Thank you very much, Tim... .
I will probably use a public malloc() implementation and provide a morecore() function for that... .
So, thank you
I will probably use a public malloc() implementation and provide a morecore() function for that... .
So, thank you
-
Whatever5k
Re:Bitmap or stack based memory manager
Oh, I got a problem. I want to use a pointer to the heap, and increment it on every morecore(), as you do, Tim (well, you don't use a pointer, but that doesn't matter). I use a linker script, so I have the physical address of the heap (that is the end of the bss section -> ebss). So should I use a pointer to ebss?
Because I was wondering if I should have the virtual address of the heap?! If so, how can I get the virtual address?
So to clarify my question: Does the variable kernel_sbrk hold the virtual or the physical address of the heap?
Because I was wondering if I should have the virtual address of the heap?! If so, how can I get the virtual address?
So to clarify my question: Does the variable kernel_sbrk hold the virtual or the physical address of the heap?
-
Tim
Re:Bitmap or stack based memory manager
Virtual. The physical addresses of the pages that make up the heap from from MemAlloc.
-
Whatever5k
Re:Bitmap or stack based memory manager
I cannot really understand your second sentence. So the kernel_sbrk contains the virtual address of the heap - ok, that's what I thought, too.Virtual. The physical addresses of the pages that make up the heap from from MemAlloc.
But how do I get the virtual address of the heap? I use a linker script, so I have the end of the bss (that is the heap), but that is only physical, not virtual, right?
best regards and happy new year,
A. Blessing
-
Tim
Re:Bitmap or stack based memory manager
You can choose whatever virtual address you want.
Are you sure you're 100% confident of how virtual memory works? I could explain from the beginning, but you're better off reading the tutorials I wrote.
Are you sure you're 100% confident of how virtual memory works? I could explain from the beginning, but you're better off reading the tutorials I wrote.
-
Whatever5k
Re:Bitmap or stack based memory manager
Oh, that's right... . I didn't think of that. However, if I determine the virtual address of the heap as I want, I will have to make sure that it is correctly mapped (right PDE and PTE).You can choose whatever virtual address you want.
Which virtual address did you choose?