I am new to stackoverflow and also to this xv6. I was going through the code of xv6 x86 version. I started from main.c Let me tell what idea I have till now.
*pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;
why are we using V2P, virtual to physical of pgtab. This is my doubt.Just in case you need the code for walkpgdir it is here,
static pte_t *
walkpgdir(pde_t *pgdir, const void *va, int alloc)
{
pde_t *pde;
pte_t *pgtab;
pde = &pgdir[PDX(va)];
if(*pde & PTE_P){
pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
} else {
if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
return 0;
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PGSIZE);
// The permissions here are overly generous, but they can
// be further restricted by the permissions in the page table
// entries, if necessary.
*pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;
}
return &pgtab[PTX(va)];
}
Hey, forgive me if I made a mistake in explaining stuff, I just told what I understood, I was just changing through various files and told what I understood. Please correct me if I am wrong.
I am new to stackoverflow and also to this xv6. I was going through the code of xv6 x86 version. I started from main.c Let me tell what idea I have till now.
*pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;
why are we using V2P, virtual to physical of pgtab. This is my doubt.Just in case you need the code for walkpgdir it is here,
static pte_t *
walkpgdir(pde_t *pgdir, const void *va, int alloc)
{
pde_t *pde;
pte_t *pgtab;
pde = &pgdir[PDX(va)];
if(*pde & PTE_P){
pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
} else {
if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
return 0;
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PGSIZE);
// The permissions here are overly generous, but they can
// be further restricted by the permissions in the page table
// entries, if necessary.
*pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;
}
return &pgtab[PTX(va)];
}
Hey, forgive me if I made a mistake in explaining stuff, I just told what I understood, I was just changing through various files and told what I understood. Please correct me if I am wrong.
kalloc
andkfree
allocate or free a physical page. They both return pointers to the virtual address of the page in the kernel’s memory.
Your confusion comes from the assumption that pgtab
is already a physical memory address. While kalloc()
does return a page from physical memory, the address returned is mapped in the kernel's virtual address space. So, pgtab
is a virtual address that points to physical memory.
A virtual address typically is translated to a physical address by the MMU when you access data at the address. What the virtual address translates to depends on the memory map specified by the kernel.
Read about kalloc and kfree here