mmap and mem_offset

In the following code:

#define IMAGE_SIZE 0xf0000

// — Allocate a physically contiguous buffer

addr = mmap( 0, IMAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON |
MAP_SHARED | MAP_PHYS,NOFD,0 );
if ( addr == (void*)MAP_FAILED ) {
clean();
return NULL; }
}

// — Obtain physical address

if ( mem_offset(addr, NOFD, IMAGE_SIZE, &offset, &size)
== -1) {
clean();
return NULL;
}

printf(“memaddr %u\n”, size );

I was expecting size to be equal to IMAGE_SIZE but instead it’s equal to
4096?

  • Mario

Ping!

“Mario Charest” <goto@nothingness.com> wrote in message
news:aba2am$ifk$1@inn.qnx.com

In the following code:

#define IMAGE_SIZE 0xf0000

// — Allocate a physically contiguous buffer

addr = mmap( 0, IMAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON
|
MAP_SHARED | MAP_PHYS,NOFD,0 );
if ( addr == (void*)MAP_FAILED ) {
clean();
return NULL; }
}

// — Obtain physical address

if ( mem_offset(addr, NOFD, IMAGE_SIZE, &offset, &size)
== -1) {
clean();
return NULL;
}

printf(“memaddr %u\n”, size );

I was expecting size to be equal to IMAGE_SIZE but instead it’s equal to
4096?

  • Mario
    \

I vaguely recall Brian Stecher saying something about the current
implmentation of the memory manager means that mem_offset
always giving you pagesize as the maximum contiguous size. Hopefully
someone will correct me if I’m wrong.

\

cburgess@qnx.com

Colin Burgess <cburgess@qnx.com> wrote:

I vaguely recall Brian Stecher saying something about the current
implmentation of the memory manager means that mem_offset
always giving you pagesize as the maximum contiguous size. Hopefully
someone will correct me if I’m wrong.

Nope, you are right. You have to manually loop over and call mem_offset().
I think he (Brian) called it “braindead” or something like that and that it
would be fixed in the future.

chris

\

Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Mario Charest <goto@nothingness.com> wrote:

mem_offset() is broken in how it returns the contiguous size.
Don’t believe it.

-David

In the following code:

#define IMAGE_SIZE 0xf0000

// — Allocate a physically contiguous buffer

addr = mmap( 0, IMAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON |
MAP_SHARED | MAP_PHYS,NOFD,0 );
if ( addr == (void*)MAP_FAILED ) {
clean();
return NULL; }
}

// — Obtain physical address

if ( mem_offset(addr, NOFD, IMAGE_SIZE, &offset, &size)
== -1) {
clean();
return NULL;
}

printf(“memaddr %u\n”, size );

I was expecting size to be equal to IMAGE_SIZE but instead it’s equal to
4096?

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:abetog$8uq$2@nntp.qnx.com

Mario Charest <> goto@nothingness.com> > wrote:

mem_offset() is broken in how it returns the contiguous size.
Don’t believe it.

Ok, are doc people reading this ?

-David

In the following code:

#define IMAGE_SIZE 0xf0000

// — Allocate a physically contiguous buffer

addr = mmap( 0, IMAGE_SIZE, PROT_READ|PROT_WRITE,
MAP_ANON |
MAP_SHARED | MAP_PHYS,NOFD,0 );
if ( addr == (void*)MAP_FAILED ) {
clean();
return NULL; }
}

// — Obtain physical address

if ( mem_offset(addr, NOFD, IMAGE_SIZE, &offset, &size)
== -1) {
clean();
return NULL;
}

printf(“memaddr %u\n”, size );

I was expecting size to be equal to IMAGE_SIZE but instead it’s equal to
4096?

Mario Charest <goto@nothingness.com> wrote:

: “David Gibbs” <dagibbs@qnx.com> wrote in message
: news:abetog$8uq$2@nntp.qnx.com
:> Mario Charest <goto@nothingness.com> wrote:
:>
:> mem_offset() is broken in how it returns the contiguous size.
:> Don’t believe it.

: Ok, are doc people reading this ?

Yup. Since it sounds like something that’s going to be fixed, I’ll mention
it in the release notes.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

Chris McKillop <cdm@qnx.com> wrote:
: Colin Burgess <cburgess@qnx.com> wrote:
:> I vaguely recall Brian Stecher saying something about the current
:> implmentation of the memory manager means that mem_offset
:> always giving you pagesize as the maximum contiguous size. Hopefully
:> someone will correct me if I’m wrong.
:>

: Nope, you are right. You have to manually loop over and call mem_offset().
: I think he (Brian) called it “braindead” or something like that and that it
: would be fixed in the future.

Is there a workaround?


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

“Steve Reid” <stever@qnx.com> wrote:

Chris McKillop <> cdm@qnx.com> > wrote:
: You have to manually loop over and call mem_offset().

Is there a workaround?

Here is the workaround I use:

/***********************************************************************
*

  • void *osMemVirt2Phys(void *vAddr, size_t size);
  • Returns the physical address corresponding to the virtual
  • address ‘vAddr’.
  • The argument ‘size’ is used to verify if the block of virtual memory
  • occupies a contiguous block of physical memory.

*/
void * osMemVirt2Phys
(
void vAddr, / Virtual address to convert /
size_t size /
Length of the block pointed to by vAddr. */
)
{
off_t offset;
size_t contigLen = 0;
size_t remainLen;
off_t dummyOffset1;
off_t dummyOffset2;
size_t contigLen2 = 0;
void *dummyAddr;

/* Validate parameter */
if ((size == 0) || (vAddr == NULL)) return (NULL);

/* Obtain the physical address (offset) of the first segment. */
if (mem_offset(vAddr, NOFD, size, &offset, &contigLen) == -1)
{
return (NULL);
}

/* Make sure memory is contiguous */
dummyAddr = (void *)((int)vAddr + contigLen);
if (contigLen > size)
{
remainLen = 0;
}
else
{
remainLen = size - contigLen;
}

/* Loop thru all segments of physical memory /
/
to ensure they’re contiguous. /
dummyOffset1 = offset;
while(remainLen > 0)
{
/
Obtain the physical address of the next segment. */
if (mem_offset(dummyAddr, NOFD, remainLen,
&dummyOffset2, &contigLen2) == -1)
{
return NULL;
}

/* Make sure this segment is located /
/
immediately after the previous one. */
if (dummyOffset2 - dummyOffset1 != contigLen)
{
return NULL;
}

dummyAddr = (void *)((int)dummyAddr + contigLen2);
if (contigLen2 > remainLen)
{
remainLen = 0;
}
else
{
remainLen -= contigLen2;
}

dummyOffset1 = dummyOffset2;
contigLen = contigLen2;
}

/* Fine… the memory block is contiguous. */
return ((void *)offset);

} /* end of osMemVirt2Phys */