bzr branch
http://darksoft.org/webbzr/alps/bar_resolve
1
by Suren A. Chilingaryan
Test application for resolving user-space BAR addresses |
1 |
#define _BSD_SOURCE
|
2 |
#define _DEFAULT_SOURCE
|
|
3 |
#define _POSIX_C_SOURCE 199309L
|
|
4 |
#include <stdio.h> |
|
5 |
#include <stdlib.h> |
|
6 |
#include <string.h> |
|
7 |
#include <unistd.h> |
|
8 |
#include <stdarg.h> |
|
9 |
#include <time.h> |
|
10 |
#include <sched.h> |
|
11 |
#include <fcntl.h> |
|
2
by Suren A. Chilingaryan
Finalyze the sample |
12 |
#include <errno.h> |
1
by Suren A. Chilingaryan
Test application for resolving user-space BAR addresses |
13 |
#include <sys/types.h> |
14 |
#include <sys/time.h> |
|
15 |
#include <sys/stat.h> |
|
16 |
||
17 |
#include <pcilib.h> |
|
2
by Suren A. Chilingaryan
Finalyze the sample |
18 |
#include <pcilib/bar.h> |
19 |
#include <pcilib/kmem.h> |
|
1
by Suren A. Chilingaryan
Test application for resolving user-space BAR addresses |
20 |
|
21 |
#define DEVICE "/dev/fpga0"
|
|
2
by Suren A. Chilingaryan
Finalyze the sample |
22 |
#define BAR PCILIB_BAR0
|
23 |
||
1
by Suren A. Chilingaryan
Test application for resolving user-space BAR addresses |
24 |
|
25 |
#define WR(addr, value) { *(uint32_t*)(bar + addr) = value; }
|
|
26 |
#define RD(addr, value) { value = *(uint32_t*)(bar + addr); }
|
|
27 |
||
2
by Suren A. Chilingaryan
Finalyze the sample |
28 |
void request(void* volatile ua) { |
1
by Suren A. Chilingaryan
Test application for resolving user-space BAR addresses |
29 |
int fd = open("/sys/class/test/test0/test_request", O_RDWR); |
30 |
if (fd >= 0) { |
|
31 |
size_t bytes; |
|
32 |
char res[64]; |
|
33 |
||
34 |
sprintf(res, "%p", ua); |
|
35 |
write(fd, res, strlen(res)); |
|
36 |
lseek(fd, SEEK_SET, 0); |
|
37 |
bytes = read(fd, res, sizeof(res) - 1); |
|
38 |
||
39 |
printf("Res : "); |
|
40 |
if (bytes < 0) { |
|
41 |
puts("Error"); |
|
42 |
} else { |
|
43 |
res[bytes] = 0; |
|
44 |
puts(res); |
|
45 |
}
|
|
46 |
||
47 |
close(fd); |
|
2
by Suren A. Chilingaryan
Finalyze the sample |
48 |
} else { |
49 |
printf("Error openning /sys/class/test/test0/test_request, errno %i\n", errno); |
|
50 |
}
|
|
51 |
}
|
|
52 |
||
53 |
int main(int argc, char *argv[]) { |
|
54 |
uint32_t i, j; |
|
55 |
pcilib_t *pci; |
|
56 |
pcilib_kmem_handle_t *kbuf; |
|
57 |
||
58 |
void* volatile bar; |
|
59 |
const pcilib_bar_info_t *bar_info; |
|
60 |
||
61 |
pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT); |
|
62 |
if (!pci) { |
|
63 |
printf("pcilib_open\n"); |
|
64 |
exit(1); |
|
65 |
}
|
|
66 |
||
67 |
kbuf = pcilib_alloc_kernel_memory(pci, PCILIB_KMEM_TYPE_PAGE, 1, 4096, 4096, 0, 0); |
|
68 |
void* volatile ua = pcilib_kmem_get_ua(pci, kbuf); |
|
69 |
uintptr_t pa = pcilib_kmem_get_pa(pci, kbuf); |
|
70 |
||
71 |
printf("Kmem\n-----\n"); |
|
72 |
printf("User: %p\n", ua); |
|
73 |
printf("HW : 0x%lx\n", pa); |
|
74 |
request(ua); |
|
75 |
printf("\n"); |
|
76 |
||
77 |
bar = pcilib_resolve_bar_address(pci, BAR, 0); |
|
78 |
if (!bar) { |
|
79 |
pcilib_close(pci); |
|
80 |
printf("map bar\n"); |
|
81 |
exit(1); |
|
82 |
}
|
|
83 |
bar_info = pcilib_get_bar_info(pci, BAR); |
|
84 |
||
85 |
printf("Bar\n-----\n"); |
|
86 |
printf("User: %p\n", bar_info[BAR].virt_addr); |
|
87 |
printf("HW : %p\n", bar_info[BAR].phys_addr); |
|
88 |
request(bar_info[BAR].virt_addr); |
|
89 |
printf("\n"); |
|
1
by Suren A. Chilingaryan
Test application for resolving user-space BAR addresses |
90 |
|
91 |
pcilib_free_kernel_memory(pci, kbuf, 0); |
|
92 |
pcilib_close(pci); |
|
93 |
}
|