mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ryan Lortie <desrt@desrt.ca>
To: linux-kernel@vger.kernel.org
Subject: Strange /dev/mem behaviour on IA32, PPC64, (others?)
Date: Sat, 12 Feb 2005 22:46:54 -0500	[thread overview]
Message-ID: <1108266414.8479.12.camel@moonpix.desrt.ca> (raw)

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

There are a couple of problems that I have encountered with /dev/mem on
the 2.6.10 kernel.

The first problem is that lseek()/read() results in different data than
mmap() and reading from the mapped memory area.  I've written a small
program to demonstrate the problem.  I am fairly sure it is correct (a
few others haved looked over it).  The program produces unexpected
output when I run it on both IA32 and PPC64.  You need to have at least
1GB of RAM to run it, but it is easy to modify if you have less.

In short, lseek()/read() returns what I believe to be the correct
values.  Reading from the mmap() region gives the correct values for the
first few reads but after that it starts giving zeros.

In testing this program on IA32 I ran into an additional problem.  If
you lseek() to the (very suspicious) 896MB mark and read() then read
gives EFAULT (Bad address).

I have high memory support enabled and the 'free' command reports:

             total
Mem:       1036172

so seeking should be OK all the way up to 1GB.

Any information about these problems (including "you're wrong
because....") is appreciated.  I'm not on the list, so please Cc:
replies.

Cheers.

[-- Attachment #2: fetch.c --]
[-- Type: text/x-csrc, Size: 920 bytes --]

#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main( void )
{
  char *ptr;
  int fd;
  long i;
  char z;

  fd = open( "/dev/mem", O_RDONLY, 0666 );

  if( fd < 0 )
  {
    perror( "open /dev/mem" );
    exit( 1 );
  }

  /* map in 1G worth of physical RAM */
  ptr = mmap( NULL, 0x40000000L, PROT_READ, MAP_SHARED, fd, 0 );

  if( ptr == (void *) -1 )
  {
    perror( "mmap" );
    exit( 1 );
  }

  /* print out memory at 8 meg intervals.  8M * 128 = 1024M */
  for( i = 0; i < 128; i++ )
  {
    /* -> for mmap */
    printf( "%02lx -> %x\n", i, ptr[8 * 1024 * 1024 * i] );

    /* => for lseek/read */
    if( lseek( fd, 8 * 1024 * 1024 * i, SEEK_SET ) < 0 )
    {
      perror( "lseek" );
      exit( 1 );
    }

    if( read( fd, &z, 1 ) != 1 )
    {
      perror( "read" );
      exit( 1 );
    }

    printf( "%02lx => %x\n", i, z );
  }

  return 0;
}


                 reply	other threads:[~2005-02-13  3:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1108266414.8479.12.camel@moonpix.desrt.ca \
    --to=desrt@desrt.ca \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®