Do you have a question? Post it now! No Registration Necessary
Subject
- Posted on
August 21, 2006, 10:48 am

Hi
How can I get physics address from a virtual address in kernel space?
For low mem I can simply use virt_to_phys macro, and for those memory
been mapped
by ioremap, I try to walk through the pagetable by following code on arm
platform:
static unsigned long himem_virt_to_phys(unsigned long virt)
{
pmd_t * pmd;
pte_t * pte;
unsigned long phys_addr;
pmd = ( pmd_t * ) (pgd_offset_k(virt));
pte = pte_offset_kernel(pmd, virt & ~PGDIR_MASK);
phys_addr = ( pte_val(*pte) & PAGE_MASK ) | ( virt & ~PAGE_MASK);
printk("--- pmd addr = %lx, pte addr = %lx \n",pmd,pte);
printk("--- pte content = %lx\n",pte_val(*pte));
printk("------ virt = %lx, phys= %lx \n",virt, phys_addr);
return phys_addr;
}
it did work, but when I try to get the physical address of a local var
say i,
it won't work! Why ? Is there some thing I missing?
the code i test the func is as following:
//---------------------
int i;
unsigned long virt;
virt = (unsigned long)ioremap(0x10018000,0x10000);
himem_virt_to_phys(virt);
i = 28;
printk("&i = %x\n", &i);
himem_virt_to_phys((unsigned long)&i);
//---------------------
and the outcome:
--- pmd addr = c0007120, pte addr = c3fdba9c
--- pte content = 100180a3
------ virt = c48a7000, phys= 10018000
&i = c3d99f64
--- pmd addr = c00070f0, pte addr = c3c00e64
--- pte content = e51b5084
------ virt = c3d99f64, phys= e51b5f64
this is not right , since pte's last bit is not 1 ,
and actually my sdram mem is on 0x1xxxxxxx
not 0xExxxxxxx
Any ideas ? Thx
How can I get physics address from a virtual address in kernel space?
For low mem I can simply use virt_to_phys macro, and for those memory
been mapped
by ioremap, I try to walk through the pagetable by following code on arm
platform:
static unsigned long himem_virt_to_phys(unsigned long virt)
{
pmd_t * pmd;
pte_t * pte;
unsigned long phys_addr;
pmd = ( pmd_t * ) (pgd_offset_k(virt));
pte = pte_offset_kernel(pmd, virt & ~PGDIR_MASK);
phys_addr = ( pte_val(*pte) & PAGE_MASK ) | ( virt & ~PAGE_MASK);
printk("--- pmd addr = %lx, pte addr = %lx \n",pmd,pte);
printk("--- pte content = %lx\n",pte_val(*pte));
printk("------ virt = %lx, phys= %lx \n",virt, phys_addr);
return phys_addr;
}
it did work, but when I try to get the physical address of a local var
say i,
it won't work! Why ? Is there some thing I missing?
the code i test the func is as following:
//---------------------
int i;
unsigned long virt;
virt = (unsigned long)ioremap(0x10018000,0x10000);
himem_virt_to_phys(virt);
i = 28;
printk("&i = %x\n", &i);
himem_virt_to_phys((unsigned long)&i);
//---------------------
and the outcome:
--- pmd addr = c0007120, pte addr = c3fdba9c
--- pte content = 100180a3
------ virt = c48a7000, phys= 10018000
&i = c3d99f64
--- pmd addr = c00070f0, pte addr = c3c00e64
--- pte content = e51b5084
------ virt = c3d99f64, phys= e51b5f64
this is not right , since pte's last bit is not 1 ,
and actually my sdram mem is on 0x1xxxxxxx
not 0xExxxxxxx
Any ideas ? Thx

Re: Question on Virtual Address to Physcial Address, need help

Hallo

pmd = pgd_offset(current->mm, virt);

pte = pte_offset_kernel(pmd, virt);

For local variables pgd is in current->mm (I hope) and for vmalloc (ioremap)
is in init_mm at the begining and after page fault on others directories,
see gorman book chapter 7.
Staszek Gruszka
Site Timeline
- » Experienced Embedded (or kernel) Software Engineer
- — Next thread in » Embedded Linux
-
- » [Q] Porting asterisk to Linksys WRTP54G router
- — Previous thread in » Embedded Linux
-
- » Crosscompiling for ARM: reloc type R_ARM_ABS32 is not supported for PIC - ...
- — Newest thread in » Embedded Linux
-
- » Der beste Editor (was: kann man in dem Wirrwar nicht finden)
- — The site's Newest Thread. Posted in » Electronics (German)
-