Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c - using memory mapped location to resource0 of Network wireless pci device causing segment fault

I am trying to create a simple program I mapped a memory to resource0 of wireless Network pci device. the problem is I am getting 0xffffffff address returned of mmap(..) call. it does not look correctly mapped since 0xffffffff make no sense. and errno is 22 on this page it says it got to do with offset android mmap failed: invalid argument (errno 22)

this is my code

            void main(){
                void *map_base, *virt_addr;
                off_t target, target_base;
                int fd;
                if((fd = open("/sys/bus/pci/devices/0000:02:00.0/resource0", O_RDWR | O_SYNC)) == -1) {
                   printf("open error
");
                }

                target=0xc0700000;//register location of resource. But not sure what to have here
                target_base = target & ~(sysconf(_SC_PAGE_SIZE)-1);
                int map_size = 4096UL;
                struct ethhdr *eth;
                map_base = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target_base);
                printf("%x
",map_base);
                printf("hello
");
                for (int i = 0; i < 4; i++) {
                    virt_addr = map_base + target + i*8 - target_base;
                    printf("hello
");
                    eth=((struct ethhdr *) virt_addr);
                    char a=eth->h_dest[0];
                    printf("hello
");
                    //printf( "%.2X-%.2X-%.2X-%.2X-%.2X-%.2X 
", eth->h_dest[0] , eth->h_dest[1] , eth->h_dest[2] , eth->h_dest[3] , eth->h_dest[4] , eth->h_dest[5] );

                }
                return;

            }

Then I am trying to read the mapped location and try to extract ethernet header from the mapped area in loop (it may be wrong but with wrong mapping it is useless). I dont think its the right way of doing this thing any ways considering the code on github was using different archetecture. So I like to ask what might the reason of me getting weird address retured from mmap when I mapped to IO resource. I will get into the datasheet part then I can extract headers from mapped location correctly. But my main question is about wrong mapping. How can I map it correctly. Then what should I using (in entire for loop) so I have some useful print after correct IO mapping (not asking for intel device architecture and register info. Just data structure that required so easy to getting Ethernet header/ip header/and tcp header). I dont know if it make any sense but I have system virtualization enabled. What I know is maybe its necessary for having devices mapped to Memory location area as in Memory Mapped I/O in intel x86-64 which is I am using.

Any ways there is a similar techniue on this page. I got idea idea from this page https://github.com/billfarrow/pcimem/blob/master/pcimem.c

question from:https://stackoverflow.com/questions/65626075/using-memory-mapped-location-to-resource0-of-network-wireless-pci-device-causing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...