最近用ioremap 和 phys_to_virt 做物理地址于虚拟地址的转换发现
addr = (unsigned int volatile *)ioremap(0x56000088,12);
printk(KERN_ALERT"%x\n",addr);
addr = (unsigned int volatile *) phys_to_virt(0x56000088);
printk(KERN_ALERT"%x\n",addr);
两个很函数返回的addr值不一样看了一下(arm2410)内核的源码发现
在内核中phys_to_virt只是给地址减去一个固定的偏移 :
#ifndef __virt_to_phys
#define __virt_to_phys(x) ((x) - PAGE_OFFSET + PHYS_OFFSET)
#define __phys_to_virt(x) ((x) - PHYS_OFFSET + PAGE_OFFSET)
#endif
而ioremap()的原则就是内核会根据指定的物理地址新建映射页表,物理地址和虚拟地址的关系就由这些页表来搭建!:
void __iomem *
__ioremap(unsigned long phys_addr, size_t size, unsigned long flags,
unsigned long align)
{
void * addr;
struct vm_struct * area;
unsigned long offset, last_addr;
/* Don't allow wraparound or zero size */
last_addr = phys_addr + size - 1;
if (!size || last_addr < phys_addr)
return NULL;
/*
* Mappings have to be page-aligned
*/
offset = phys_addr & ~PAGE_MASK;
phys_addr &= PAGE_MASK;
size = PAGE_ALIGN(last_addr + 1) - phys_addr;
/*
* Ok, go for it..
*/
area = get_vm_area(size, VM_IOREMAP);
if (!area)
return NULL;
addr = area->addr;
if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
vfree(addr);
return NULL;
}
return (void __iomem *) (offset + (char *)addr);
阅读(4199) | 评论(0) | 转发(0) |