[lkml]   [2002]   [Sep]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRE: Calculating kernel logical address ..

-----Original Message-----
From: Daniel Phillips []
Sent: Monday, September 09, 2002 11:27 AM
Cc: 'David S. Miller';
Subject: Re: Calculating kernel logical address ..

On Monday 09 September 2002 20:12, Imran Badr wrote:
> But my question here still begging an answer: What would be the portable
> to calculate kernel logical address of that user buffer?

>Could you please post your code for doing the kmalloc and mmap?

Sure, in mmap():

size = vma->vm_end - vma->vm_start;
if(size % PAGE_SIZE)
printk(KERN_CRIT "mmap: size (%ld) not multiple of PAGE_SIZE.\n", size);
return -ENXIO;

offset = vma->vm_pgoff<<PAGE_SHIFT;
if(offset & ~PAGE_MASK)
printk(KERN_CRIT "mmap: offset (%ld) not aligned.\n", offset);
return -ENXIO;

kmalloc_ptr = (Uint8 *)kmalloc(size+(2*PAGE_SIZE), GFP_KERNEL);
if(kmalloc_ptr == NULL)
printk(KERN_CRIT "mmap: not enough memory.\n");
return -ENOMEM;

/* align it to page boundary */
kmalloc_area = (Uint8 *)(((Uint32)kmalloc_ptr + PAGE_SIZE -1) & PAGE_MASK);

/* reserve all pages */
for(virt_addr = (Uint32)kmalloc_area; virt_addr < (Uint32)kmalloc_area +
size; virt_addr +=PAGE_SIZE)

/* lock the area*/
vma->vm_flags |=VM_LOCKED;

virt_to_phys((void *)(Uint32)kmalloc_area),
printk(KERN_CRIT "mmap: remap page range failed.\n");
return -ENXIO;

vma->vm_ops = &pkp_vma_ops;
vma->vm_private_data = kmalloc_ptr;
return 0;

This works just fine on my i386 platform (SMP ,non-SMP). Now in my ioctl()
entry I get the kernel logical address by using the following code:

adr = user_address;
pgd_offset(current->mm, adr);
if (!pgd_none(*pgd)) {
pmd = pmd_offset(pgd, adr);
if (!pmd_none(*pmd)) {
ptep = pte_offset(pmd, adr);
pte = *ptep;
if(pte_present(pte)) {
kaddr = (unsigned long) page_address(pte_page(pte));
kaddr |= (adr & (PAGE_SIZE - 1));

Now for DMA, I get bus address by using virt_to_bus(kaddr). So, is there any
portablility issue in this scheme?


To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:28    [W:0.058 / U:0.196 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site