LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 11-22-2005, 10:29 AM   #1
justin8086
LQ Newbie
 
Registered: Nov 2005
Posts: 1

Rep: Reputation: 0
Newbie question about usage of mmap


Hi there,

I have a PCI device and I tried to write a short code to access it in user space with mmap.

first I "cat /proc/bus/pci/devices" and get the device's address: 0x00001800.

then I write the following code:

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

int main(void)
{
void *map;
int fd;
int src = 0;
ulong base;
ulong length;

base = 0x00001800;
length = 0xFF;

fd = open("/dev/mem", O_RDWR);
printf("%d\n",fd);
map = mmap(NULL, (size_t)length, (PROT_READ | PROT_WRITE), MAP_SHARED, fd,(off_t)base);
if (map == MAP_FAILED)
{
printf("map failed: %s\n",strerror(errno));
exit(1);
}

memcpy(map+0x85,&src, 1);

return 0;
}

I run the code and got the error says that "Invalid Argument", I guess that is because of some misuse of mmap, but where is the error?

TIA

justin
 
Old 11-22-2005, 01:10 PM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
The offset (the last arg to mmap) must be multiple of the page size (4K on x86, 8K on 64-bit archs). Try just using an offset of zero and taking the 0x1800 into account when accessing the memory.
 
  


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
RAM usage question. 99%? TheVrolok Linux - Newbie 2 11-28-2005 08:49 PM
Question about memory usage stefaandk Linux - Newbie 3 08-03-2005 02:22 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
memory usage question bmicielli Linux - Hardware 3 12-27-2003 07:15 PM
CPU usage question infamous41md Linux - Newbie 2 04-01-2003 05:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 10:39 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