Re: Questions about mmap

From: Nick Piggin
Date: Tue Oct 07 2008 - 12:02:30 EST


On Wednesday 08 October 2008 00:40, Thiago Lacerda wrote:
> Thank you Stefan.... by I'd something more concrete.
>
> I'm trying to do like this:
>
> //Code of the char device
> unsigned int **test;
> static int device_mmap(struct file *filp, struct vm_area_struct *vma) {
> printk(KERN_INFO"Calling mmap\n");
> vma->vm_flags |= VM_LOCKED;
> if(remap_pfn_range(vma, vma->vm_start,
> virt_to_phys((void*)((unsigned long)test)) >> PAGE_SHIFT, vma->vm_end
> - vma->vm_start, PAGE_SHARED))

I guess it is more usual to use vma->vm_page_prot for the last argument.
Unless you really specifically want to override it.

> return -EAGAIN;
> printk(KERN_INFO"mmap returned\n");
> return 0;
> }
>
> static int __init testeInit(void) {
> /* after creating char device and registering it*/
> ...
> ....
> test = (unsigned int**) kmalloc(sizeof(unsigned int*)*1024, GFP_KERNEL);
> int i;
> for(i = 0; i < 1024; i++)
> test[i] = NULL;
>
> unsigned int* temp1 = (unsigned int*) kmalloc(sizeof(unsigned int),
> GFP_KERNEL); (*temp1) = 9;
> test[0] = temp1;
> return 0;
> }

The idea seems strange because the userspace program will never be able
to dereference these pointers.


> and at user program:
>
> int main() {
> int fd;
>
> unsigned int **mptr;
> size_t size = 1024*sizeof(unsigned int*);
> fd = open("/dev/myDev", O_RDWR);
> if( fd == -1) {
> printf("open error...\n");
> exit(0);
> }
>
> mptr = mmap(0, sizeof(unsigned int*)*1024, PROT_READ | PROT_WRITE,
> MAP_FILE | MAP_SHARED, fd, 0);
> if(mptr == MAP_FAILED) {
> printf("mmap() failed\n");
> exit(1);
> }
> printf("teste[0]: %u\n", *mptr[0]);
> munmap(mptr, size);
> close(fd);
> return 0;
> }
>
> It's not working, could you tell me what am I doing wrong? It is for
> my undergraduate thesis ans it is really driving me mad.
>
> I hope that you can help me.

What is not working? What is failing or going wrong for you?
Can't really help without knowing that.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/