LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 01-09-2009, 01:16 PM   #1
btklee
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
mmap function returns 0 instead of the address location


Hi all,

I am writing a mmap function for a new socket family. The function looks like the following. The memory that's being mapped is a contiguous block of memory that's allocated by a device driver which is around 2M in size.

/**************************************************************/
int new_socket_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
{
int fd = (int)sock->sk;
NEWSOCK *pNEWSOCK = (NEWSOCK *)fd;

unsigned long start = (unsigned long)vma->vm_start;
unsigned long size = (unsigned long)(vma->vm_end - vma->vm_start);
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
int error;

if (offset != 0)
{
return -ENXIO;
}

/* map the whole physically contiguous area in one piece
* hostBuffer is the physical address of the memory that
* needs to be mapped
*/
if ( ( error = remap_pfn_range( vma,
start,
pNEWSOCK->hostBuffer >> PAGE_SHIFT ),
size,
vma->vm_page_prot ) ) < 0 )
{
return error;
}

return 0;
}
/**************************************************************/

Everything seems to work except that when I call the mmap function from a user application, it returns 0 instead of the start address that was mapped. I called the mmap function as below.

if ( pBuffer = (unsigned long *)mmap( 0,
0x00080000,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED,
fd,
0 ) < 0 )
{
perror( "mmapping" );
exit( 1 );
}
else
{
printf( "0x%X\n", pBuffer );
/* and pBuffer turns out to be 0 */
}

However, if I do a printk at for the "start" variable in the mmap function and hardcode that into the user application (ie. pBuffer = 0xXXXXXXXX), I am able to see the right data that's in the memory (That means the mapping is actually working). So the question is, if the mapping works, how come it returns a 0 instead of the mapped address. Can someone please help me out on this one?

Thanks a lot,
Ben
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mmap a physical address fardad Programming 3 10-30-2008 11:30 AM
Fixed Location for mmap EmbeddedSteve Programming 1 02-09-2008 10:18 PM
function returns address of local variable (void) rubadub Programming 11 10-28-2007 12:59 PM
mmap /dev/mem returns 0 stz Programming 2 11-27-2006 12:11 AM
mmap function professional1983 Programming 1 06-14-2005 04:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 10:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration