Page 1 of 1

heap management in JamesM tutorial

Posted: Fri Apr 20, 2012 7:46 pm
by vjain20
Hi,

I am not understanding the calculation of physical address in the kmalloc function in JamesM tutorial.
As far as I understand , every address is identity-mapped. So the virtual address of the allocated block (addr) should
equal its physical address. Why is it added to address of the frame ?

Code: Select all

u32int kmalloc_int(u32int sz, int align, u32int *phys)
{
    if (kheap != 0)
    {
        void *addr = alloc(sz, (u8int)align, kheap);
        if (phys != 0)
        {
            pte_t *page = get_pte((u32int)addr, 0, kernel_directory);
            *phys = page->frame*0x1000 + (u32int)addr&0xFFF;
        }
        return (u32int)addr;
    }else {.... }

Re: heap management in JamesM tutorial

Posted: Fri Apr 20, 2012 9:54 pm
by serviper
vjain20 wrote:Hi,

I am not understanding the calculation of physical address in the kmalloc function in JamesM tutorial.
As far as I understand , every address is identity-mapped. So the virtual address of the allocated block (addr) should
equal its physical address. Why is it added to address of the frame ?

Code: Select all

u32int kmalloc_int(u32int sz, int align, u32int *phys)
{
    if (kheap != 0)
    {
        void *addr = alloc(sz, (u8int)align, kheap);
        if (phys != 0)
        {
            pte_t *page = get_pte((u32int)addr, 0, kernel_directory);
            *phys = page->frame*0x1000 + (u32int)addr&0xFFF;
        }
        return (u32int)addr;
    }else {.... }
The kernel heap starts at linear address 0xC0000000 (3GB) - not identity mapped.
Look for KHEAP_START in kheap.h.

Re: heap management in JamesM tutorial

Posted: Sat Apr 21, 2012 2:31 am
by vjain20
Sorry I did not think much. Now I got it.
addr is the virtual address of the allocated block so the last 12 bits will be the offset.
Adding these to the physical address of the frame will give the physical address of the block.
Thanks!