linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* Memory Mapping a char array in User Space
@ 2010-07-26 17:28 Ravi Gupta
  2010-07-26 19:42 ` David Howells
  0 siblings, 1 reply; 3+ messages in thread
From: Ravi Gupta @ 2010-07-26 17:28 UTC (permalink / raw)
  To: linuxppc-dev

[-- Attachment #1: Type: text/plain, Size: 1303 bytes --]

Hi,

I am new to linux device driver development and I'm trying to learn the
memory mapping. Currently I have written a simple device driver(major number
251 and minor number 0) and in its mmap(struct file *file, struct
vm_area_struct *vma) function, I am trying to memory map a global character
array (defined in driver) to user space memory.This is my current
implementation

char map[25];

static int test_mmap(struct file *filp, struct vm_area_struct *vma)
{
            strcpy(map, "Hello World!!");
	if (remap_pfn_range(vma, vma->vm_start, page_to_pfn(virt_to_page(map)),
	                    vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
                return -EAGAIN;
	}
	return 0;
}*
*

Now after compiling the driver successfully, I created a character device
file /dev/test0 using mknod command (mknod /dev/test c 251 0). And in my C
program I tried to memory map the /dev/test file.

Now what I want is that whenever I map /dev/test, internally that global
char array gets memory mapped to the user space? Also what should I pass as
the length parameter in the mmap() function? Currently I am passing 25(size
of the array). My device gets memory map successfully but when I tried to
read from it I get garbage value. Is there something that I am missing?

Thanks in advance
Ravi Gupta

[-- Attachment #2: Type: text/html, Size: 2531 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Memory Mapping a char array in User Space
  2010-07-26 17:28 Memory Mapping a char array in User Space Ravi Gupta
@ 2010-07-26 19:42 ` David Howells
  2010-07-27  7:04   ` Ravi Gupta
  0 siblings, 1 reply; 3+ messages in thread
From: David Howells @ 2010-07-26 19:42 UTC (permalink / raw)
  To: Ravi Gupta; +Cc: linuxppc-dev

Ravi Gupta <dceravigupta@gmail.com> wrote:

> My device gets memory map successfully but when I tried to read from it
> I get garbage value. Is there something that I am missing?

For starters, you really should allocate a page for your buffer rather than
using kernel static data.  mmap() allows access to page-aligned data through
multiple-of-page-sized holes only[*].  Not only that, your kernel static map
buffer may not necessarily have a struct page covering it, in which case
virt_to_page() may not give you anything useful.

Furthermore, I don't think *you* should be calling remap_pfn_range().
I think you should be leaving the mapping to the core VM routines.

David

[*] unless your kernel is CONFIG_MMU=n

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Memory Mapping a char array in User Space
  2010-07-26 19:42 ` David Howells
@ 2010-07-27  7:04   ` Ravi Gupta
  0 siblings, 0 replies; 3+ messages in thread
From: Ravi Gupta @ 2010-07-27  7:04 UTC (permalink / raw)
  To: David Howells; +Cc: linuxppc-dev

[-- Attachment #1: Type: text/plain, Size: 488 bytes --]

Hi David,

Thanks for the quick reply. One more thing, in the end I have to memory map
a DMA buffer allocated using pci_alloc_consisten() function to user space.

*> I think you should be leaving the mapping to the core VM routines.*
*> Furthermore, I don't think *you* should be calling remap_pfn_range().*

Let say I have allocated a page using __get_free_pages(). But how would the
core VM routines knows that which page I want to memory map? If possible,
please explain with example.

[-- Attachment #2: Type: text/html, Size: 573 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-07-27  7:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-26 17:28 Memory Mapping a char array in User Space Ravi Gupta
2010-07-26 19:42 ` David Howells
2010-07-27  7:04   ` Ravi Gupta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).