Basic question about mmap

pramodh mallipatna (pramodh@ittc.ukans.edu)
Mon, 6 Dec 1999 06:41:06 -0600 (EST)


Hello all,

Please forgive me if I sound very silly. I am doing mmap for the first
time. A Slightly long mail, appreciate your patience to read it.

I have a device driver where in I want to map some kernel memory to the
user space. I do the following shown in the code below (similar to what is
done in drivers/char/bttv.c which I took as reference)

When I call mmap from my usercode, the kernel reboots. The control comes
to my_driver_mmap() function, loops in the while loop for sometime and
reboots the machine.

Questions:
1. Is my approach correct
2. Are the mmap() parameters in my user code correct. If not what and how
should they be
3. Is there any tutorial/document/example code for mmap? I could only find
mmap for files in R. Stevens, but that is not what I want. I want to
have mmap implemented for my device driver.

I am not in this mailing list. Please copy the mail to me.

Thanks in Advance,
Pramodh

my_driver.c
-----------
my_driver_open(struct inode *inode, struct file *filp)
{
my_mmap_buffer = (char *)kmalloc(PAGE_SIZE*10, GFP_KERNEL);
}

my_driver_mmap(struct file *file, struct vm_area_struct *vma)
{
unsigned long page, start, size;
void *pos;

start = (unsigned long)((char *)vma->vm_start);
pos = my_mmap_buffer;
size = (unsigned long)(vma->vm_end-vma->vm_start);

while (size > 0) {
page = virt_to_phys(pos);
if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
return -EAGAIN;
start += PAGE_SIZE;
pos += PAGE_SIZE;
size += PAGE_SIZE;
}
return 0;
}

my_usercode.c
-------------
main()
{
fd = open("/dev/my_driver", 0);

rptr = mmap((void *)0, (PAGE_SIZE*10), PROT_READ|PROT_WRITE,
MAP_PRIVATE, fd, 0);
perror("mmap");
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/