linux mmap从用户空间应用程序访问PCI内存区域
作为PCI驱动程序的第一级测试,我希望能够通过/sys/bus/PCI/devices/0000:01:00.0/resource0访问PCI_iomap区域 来自我的用户应用程序的文件。mmap的手册页、我找到的示例程序和其他帖子似乎表明用户进程访问应该可以工作。但是一些文章似乎指出mmap调用需要通过ioctl访问器从内核内部完成 我的问题是PCI sysfs资源文件的mmap()是否应该从应用程序空间工作? 当我运行我的代码时,mmap返回看起来像是有效地址的内容,但当我尝试访问虚拟地址时,会出现总线错误。 我相信我的终端设备FPGA上的PCI-to-Xilinx AXI网桥运行正常,因为我可以通过windows PCIe实用程序(Win驱动程序)对其进行R/W 我使用的是NXP LS1021A ARM7处理器,Linux版本为3.12.37 谢谢 账单 并不是说我想让任何人调试我的代码,而是我正在做的事情可能最好由代码来解释,所以我也包括了它。如果粘贴的代码显示不正确,我深表歉意。希望是这样 我运行下面的代码并获得 root@ls1021aiot:~#pcimem/sys/bus/pci/devices/0000:01:00.0/resource0 w /sys/bus/pci/devices/0000:01:00.0/resource0已打开。 目标偏移量为0x0,页面大小为4096,映射掩码为0xFFF mmap(0,4096,0x3,0x1,3,0x0) mmap(0,4096,0x3,0x1,3,0x0) PCI内存将4096字节区域映射到映射基0x76fb5000。 PCI内存映射访问0x 76FB5000。 总线错误linux mmap从用户空间应用程序访问PCI内存区域,linux,pci-e,Linux,Pci E,作为PCI驱动程序的第一级测试,我希望能够通过/sys/bus/PCI/devices/0000:01:00.0/resource0访问PCI_iomap区域 来自我的用户应用程序的文件。mmap的手册页、我找到的示例程序和其他帖子似乎表明用户进程访问应该可以工作。但是一些文章似乎指出mmap调用需要通过ioctl访问器从内核内部完成 我的问题是PCI sysfs资源文件的mmap()是否应该从应用程序空间工作? 当我运行我的代码时,mmap返回看起来像是有效地址的内容,但当我尝试访问虚拟地址时
/*
* pcimem.c: Simple program to read/write from/to a pci device from userspace.
*
* Copyright (C) 2010, Bill Farrow (bfarrow@beyondelectronics.us)
*
* Based on the devmem2.c code
* Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <linux/pci.h>
#define PRINT_ERROR \
do { \
fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); \
} while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
uint32_t read_result, writeval;
char *filename;
off_t target;
int access_type = 'w';
if(argc < 3) {
// pcimem /sys/bus/pci/devices/0001\:00\:07.0/resource0 0x100 w 0x00
// argv[0] [1] [2] [3] [4]
fprintf(stderr, "\nUsage:\t%s { sys file } { offset } [ type [ data ] ]\n"
"\tsys file: sysfs file for the pci resource to act on\n"
"\toffset : offset into pci memory region to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
"\tdata : data to be written\n\n",
argv[0]);
exit(1);
}
filename = argv[1];
target = strtoul(argv[2], 0, 0);
if(argc > 3)
access_type = tolower(argv[3][0]);
if((fd = open(filename, O_RDWR | O_SYNC)) == -1){
PRINT_ERROR;
}
printf("%s opened.\n", filename);
printf("Target offset is 0x%x, page size is %ld map mask is 0x%lX\n", (int) target, sysconf(_SC_PAGE_SIZE), MAP_MASK);
fflush(stdout);
/* Map one page */
#if 0
//map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) (target & ~MAP_MASK));
//map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
#endif
printf("mmap(%d, %ld, 0x%x, 0x%x, %d, 0x%x)\n", 0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (int) (target & ~MAP_MASK));
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (target & ~MAP_MASK));
if(map_base == (void *) -1){
printf("PCI Memory mapped ERROR.\n");
PRINT_ERROR;
close(fd);
return 1;
}
printf("mmap(%d, %ld, 0x%x, 0x%x, %d, 0x%x)\n", 0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (int) (target & ~MAP_MASK));
printf("PCI Memory mapped %ld byte region to map_base 0x%08lx.\n", MAP_SIZE, (unsigned long) map_base);
fflush(stdout);
virt_addr = map_base + (target & MAP_MASK);
printf("PCI Memory mapped access 0x %08X.\n", (uint32_t ) virt_addr);
switch(access_type) {
case 'b':
read_result = *((uint8_t *) virt_addr);
break;
case 'h':
read_result = *((uint16_t *) virt_addr);
break;
case 'w':
read_result = *((uint32_t *) virt_addr);
printf("READ Value at offset 0x%X (%p): 0x%X\n", (int) target, virt_addr, read_result);
break;
default:
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
exit(2);
}
fflush(stdout);
if(argc > 4) {
writeval = strtoul(argv[4], 0, 0);
switch(access_type) {
case 'b':
*((uint8_t *) virt_addr) = writeval;
read_result = *((uint8_t *) virt_addr);
break;
case 'h':
*((uint16_t *) virt_addr) = writeval;
read_result = *((uint16_t *) virt_addr);
break;
case 'w':
*((uint32_t *) virt_addr) = writeval;
read_result = *((uint32_t *) virt_addr);
break;
}
printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
fflush(stdout);
}
if(munmap(map_base, MAP_SIZE) == -1) { PRINT_ERROR;}
close(fd);
return 0;
}
/*
*c:从用户空间读/写pci设备的简单程序。
*
*版权所有(C)2010,比尔·法罗(bfarrow@beyondelectronics.us)
*
*基于devmem2.c代码
*版权所有(C)2000,Jan Derk Bakker(J.D。Bakker@its.tudelft.nl)
*
*这个程序是自由软件;您可以重新分发和/或修改它
*它是根据GNU通用公共许可证的条款发布的
*自由软件基金会;许可证的第2版,或
*(由您选择)任何更高版本。
*
*这个节目的发布是希望它会有用,
*但无任何保证;甚至没有任何关于
*适销性或适合某一特定目的。见
*有关更多详细信息,请参阅GNU通用公共许可证。
*
*您应该已经收到GNU通用公共许可证的副本
*与此同时,;如果没有,请写信给自由软件
*基金会,59寺庙广场,套房330,波士顿,MA 02111-1307美国
*
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义打印错误\
做{\
fprintf(stderr,“第%d行的错误,文件%s(%d)[%s]\n”\
__行_uu,_u文件uuu,errno,strerror(errno));退出(1)\
}而(0)
#定义地图尺寸4096UL
#定义映射掩码(映射大小-1)
int main(int argc,字符**argv){
int-fd;
void*map\u base,*virt\u addr;
uint32读取结果,写入评估;
字符*文件名;
偏离目标;
int access_type='w';
如果(argc<3){
//pcimem/sys/bus/pci/devices/0001 \:00 \:07.0/resource0 0x100 w 0x00
//argv[0][1][2][3][4]
fprintf(stderr,“\n用法:\t%s{sys file}{offset}[键入[数据]]\n”
“\tsys文件:pci资源要操作的sysfs文件\n”
“\toffset:要操作的pci内存区域的偏移量\n”
\t类型:访问操作类型:[b]yte,[h]alfword,[w]ord\n
“\t数据:要写入的数据\n\n”,
argv[0]);
出口(1);
}
filename=argv[1];
目标=strtoul(argv[2],0,0);
如果(argc>3)
访问类型=tolower(argv[3][0]);
如果((fd=open(文件名,O|RDWR | O|u SYNC))=-1){
打印错误;
}
printf(“%s已打开。\n”,文件名);
printf(“目标偏移量为0x%x,页面大小为%ld,映射掩码为0x%lX\n”,(int)目标,sysconf(\u SC\u页面大小),映射掩码);
fflush(stdout);
/*映射一页*/
#如果0
//map_base=mmap(0,map_大小,PROT_读取,PROT_写入,map_共享,fd,(off_t)(目标和映射掩码));
//map_base=mmap(0,map_大小,PROT_读写,map_共享,fd,目标&~map_掩码);
#恩迪夫
printf(“mmap(%d,%ld,0x%x,0x%x,%d,0x%x)\n”,0,映射大小,保护读写,映射共享,fd,(int)(目标和映射掩码));
map_base=mmap(0,map_SIZE,PROT_READ,PROT_WRITE,map_SHARED,fd,(target&~map_MASK));
if(map_base==(void*)-1){
printf(“PCI内存映射错误。\n”);
打印错误;
关闭(fd);
返回1;
}
printf(“mmap(%d,%ld,0x%x,0x%x,%d,0x%x)\n”,0,映射大小,保护读写,映射共享,fd,(int)(目标和映射掩码));
printf(“PCI内存将%ld字节区域映射到映射库0x%08lx。\n”,映射库大小,(无符号长)映射库);
fflush(stdout);
virt_addr=map_base+(目标和映射掩码);
printf(“PCI内存映射访问0x%08X.\n”,(uint32\u t)virt\u addr);
交换机(接入型){
案例“b”:
读取结果=*((uint8\u t*)虚拟地址);
打破
案例“h”:
读取结果=*((uint16)虚拟地址);
打破
案例“w”:
读取结果=*((uint32\u t*)虚拟地址);
printf(“偏移量0x%X(%p):0x%X\n)处的读取值,(int)目标,虚拟地址,读取结果);
打破
违约:
fprintf(stderr,“非法数据类型“%c”。\n”,访问类型);
出口(2);
}
fflush(stdout);
如果(argc>4){
writeval=strtoul(argv[4],0,0);
交换机(接入型){
案例“b”:
*((uint8_t*)virt_addr)=书面评估;
读取结果=*((uint8\u t*)虚拟地址);
打破
案例“h”:
*((uint16_t*)病毒_