linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* mmap question
@ 2002-09-24  7:26 Der Herr Hofrat
  2002-09-24  8:54 ` Der Herr Hofrat
  2002-09-24  9:37 ` Zhonghua Dai
  0 siblings, 2 replies; 3+ messages in thread
From: Der Herr Hofrat @ 2002-09-24  7:26 UTC (permalink / raw)
  To: linux-kernel


Hi !

 trying to write up a simple mmap for a pseudo device that accesses a 
 kmalloc'ed area.

 The driver is a character driver that only has mmap implemented - the kmalloc
 is done in init module and the pointer to the buffer is in global context.
 I expected to be able to write to the mmap'ed area from user-space but it
 never shows up in kernel space (the printk in driver_mmap always shows the
 init_msg passed in init_module). 

 the basic framework I'm using is below - can anybody point me to an obvious
 error or to some docs that would explain how to share an kmalloc'ed area
 with user-space via mmap ? 

thx !
hofrat
 
---driver---
char *kmalloc_area;
...
static int
driver_mmap(struct file *file,
	struct vm_area_struct *vma)
{
	vma->vm_flags |= VM_LOCKED|VM_SHARED;

	printk("message buffer: %s\",kmalloc_area); 
	remap_page_range(vma->vm_start,
		virt_to_phys(kmalloc_area),
		LEN,
		PAGE_SHARED);
	return 0;
}
	
static struct file_operations simple_fops={
    mmap:	driver_mmap,
};

int
init_module(void){
	...
	kmalloc_area=kmalloc(LEN,GFP_USER);
	strncpy(kmalloc_area,init_msg,sizeof(init_msg));
	...
}

---user-app---

int main(void)
{
	int fd;
	char msg[]="some message - should appear in kernel space";
	unsigned int *addr;

	if((fd=open("/dev/simple-device", O_RDWR))<0)
	addr = mmap(0, LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	memset(addr,0,LEN); 
	strncpy(addr,msg,sizeof(msg));
	return 0;
}

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

* Re: mmap question
  2002-09-24  7:26 mmap question Der Herr Hofrat
@ 2002-09-24  8:54 ` Der Herr Hofrat
  2002-09-24  9:37 ` Zhonghua Dai
  1 sibling, 0 replies; 3+ messages in thread
From: Der Herr Hofrat @ 2002-09-24  8:54 UTC (permalink / raw)
  Cc: linux-kernel

> 
> int
> init_module(void){
> 	...
> 	kmalloc_area=kmalloc(LEN,GFP_USER);
> 	strncpy(kmalloc_area,init_msg,sizeof(init_msg));
> 	...
> }

found the problem (naturally after posting....) - forgot to mark 
the page as reserved...

	struct page *page;
	kmalloc_area=kmalloc(LEN,GFP_USER);
	page = virt_to_page(kmalloc_area); 
	mem_map_reserve(page);
	memcpy(kmalloc_area,msg,sizeof(msg));

hofrat

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

* Re: mmap question
  2002-09-24  7:26 mmap question Der Herr Hofrat
  2002-09-24  8:54 ` Der Herr Hofrat
@ 2002-09-24  9:37 ` Zhonghua Dai
  1 sibling, 0 replies; 3+ messages in thread
From: Zhonghua Dai @ 2002-09-24  9:37 UTC (permalink / raw)
  To: Der Herr Hofrat, linux-kernel

On Tuesday 24 September 2002 15:26, Der Herr Hofrat wrote:
> Hi !
>
>  trying to write up a simple mmap for a pseudo device that accesses a
>  kmalloc'ed area.
>
>  The driver is a character driver that only has mmap implemented - the
> kmalloc is done in init module and the pointer to the buffer is in global
> context. I expected to be able to write to the mmap'ed area from user-space
> but it never shows up in kernel space (the printk in driver_mmap always
> shows the init_msg passed in init_module).
>
>  the basic framework I'm using is below - can anybody point me to an
> obvious error or to some docs that would explain how to share an kmalloc'ed
> area with user-space via mmap ?
>
> thx !
> hofrat
>
> ---driver---
> char *kmalloc_area;
> ...
> static int
> driver_mmap(struct file *file,
> 	struct vm_area_struct *vma)
> {
> 	vma->vm_flags |= VM_LOCKED|VM_SHARED;
>
> 	printk("message buffer: %s\",kmalloc_area);
> 	remap_page_range(vma->vm_start,
> 		virt_to_phys(kmalloc_area),
> 		LEN,
> 		PAGE_SHARED);

Before memory can be exported into userspace, the reserved bit must be set. 
Call mem_map_reserve()  prior to remap_page_range().

good luck.


> 	return 0;
> }
>
> static struct file_operations simple_fops={
>     mmap:	driver_mmap,
> };
>
> int
> init_module(void){
> 	...
> 	kmalloc_area=kmalloc(LEN,GFP_USER);
> 	strncpy(kmalloc_area,init_msg,sizeof(init_msg));
> 	...
> }
>
> ---user-app---
>
> int main(void)
> {
> 	int fd;
> 	char msg[]="some message - should appear in kernel space";
> 	unsigned int *addr;
>
> 	if((fd=open("/dev/simple-device", O_RDWR))<0)
> 	addr = mmap(0, LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> 	memset(addr,0,LEN);
> 	strncpy(addr,msg,sizeof(msg));
> 	return 0;
> }
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/


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

end of thread, other threads:[~2002-11-12  7:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-24  7:26 mmap question Der Herr Hofrat
2002-09-24  8:54 ` Der Herr Hofrat
2002-09-24  9:37 ` Zhonghua Dai

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).