mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nick Piggin <nickpiggin@yahoo.com.au>
To: "Thiago Lacerda" <thiagotbl@gmail.com>
Cc: linux-kernel@vger.kernel.org,
	"Stefan Richter" <stefanr@s5r6.in-berlin.de>
Subject: Re: Questions about mmap
Date: Wed, 8 Oct 2008 03:02:11 +1100	[thread overview]
Message-ID: <200810080302.11618.nickpiggin@yahoo.com.au> (raw)
In-Reply-To: <3fedcc3b0810070640p52852433v8ecf631a9b3dcd8f@mail.gmail.com>

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.

  reply	other threads:[~2008-10-07 16:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <3fedcc3b0810050621y3f44417ag605ee75dd0389100@mail.gmail.com>
2008-10-05 13:24 ` Thiago Lacerda
2008-10-05 14:01   ` Stefan Richter
     [not found]     ` <3fedcc3b0810070638u60681b6do8a7e94c4919f4a40@mail.gmail.com>
2008-10-07 13:40       ` Thiago Lacerda
2008-10-07 16:02         ` Nick Piggin [this message]
2008-10-07 16:06         ` Stefan Richter
2008-10-07 17:09           ` Thiago Lacerda
2008-10-08  4:45             ` Nick Piggin
2008-10-07 20:43         ` linux-os (Dick Johnson)
2008-10-07 20:56           ` Chris Friesen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200810080302.11618.nickpiggin@yahoo.com.au \
    --to=nickpiggin@yahoo.com.au \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stefanr@s5r6.in-berlin.de \
    --cc=thiagotbl@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome