WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: mmap of /dev/mem failing.  (Read 7022 times)

Offline jacobpjose

  • Newbie
  • *
  • Posts: 2
mmap of /dev/mem failing.
« on: August 21, 2009, 02:51:30 AM »
In my code I try to call mmap the /dev/mem in Tiny Core Linux.

I able to open the file handle to /dev/mem using open.

However the mmap fails with errno 11. strerror would give the message "Resouce temporarily unavailable." In the dmesg the following entry was found "map pfn expected mapping type uncached-minus for e0000-f0000, got write-back.

I tried the same in micro core too. There also the same error came.

Please do advise if there is some thing I am dong wrong. The code is given below.

////////////////////THE CODE/////////////////////////

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

int main(void)
{
   int mem_fd;
   off_t offset = (off_t)0xE0000 & -4096;
   size_t len = (((off_t)0xE0000 + 0x10000 + 4096 - 1) & -4096) - offset;
   char *mappedAddress = NULL;

   if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
      printf("cannot open /dev/mem\n");
      exit(1);
   }

   mappedAddress = (char *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, mem_fd, offset);
   if (mappedAddress == MAP_FAILED) {
      perror("mmap failed");
      exit(1);
   }
   printf("mapped address 0x%.16x\n", (unsigned long)mappedAddress);

   return 0;
}

Offline ^thehatsrule^

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 1726
Re: mmap of /dev/mem failing.
« Reply #1 on: August 21, 2009, 04:48:09 PM »
From the uncached error, I think this looks like a kernel problem

http://bugzilla.kernel.org/show_bug.cgi?id=13877
http://kerneltrap.org/mailarchive/git-commits-head/2008/8/22/3043954

Try booting with "nopat"

Offline jacobpjose

  • Newbie
  • *
  • Posts: 2
Re: mmap of /dev/mem failing.
« Reply #2 on: August 24, 2009, 04:29:41 AM »
Thanks. It worked perfectly.