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> |
|
12 |
#include <sys/types.h> |
|
13 |
#include <sys/time.h> |
|
14 |
#include <sys/stat.h> |
|
15 |
||
16 |
#include <pcilib.h> |
|
17 |
#include "pcilib/kmem.h" |
|
18 |
||
19 |
#define DEVICE "/dev/fpga0"
|
|
20 |
||
21 |
#define WR(addr, value) { *(uint32_t*)(bar + addr) = value; }
|
|
22 |
#define RD(addr, value) { value = *(uint32_t*)(bar + addr); }
|
|
23 |
||
24 |
int main(int argc, char *argv[]) { |
|
25 |
uint32_t i, j; |
|
26 |
pcilib_t *pci; |
|
27 |
pcilib_kmem_handle_t *kbuf; |
|
28 |
||
29 |
void* volatile bar; |
|
30 |
||
31 |
pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT); |
|
32 |
if (!pci) { |
|
33 |
printf("pcilib_open\n"); |
|
34 |
exit(1); |
|
35 |
}
|
|
36 |
||
37 |
kbuf = pcilib_alloc_kernel_memory(pci, PCILIB_KMEM_TYPE_PAGE, 1, 4096, 4096, 0, 0); |
|
38 |
volatile uint32_t *ua = pcilib_kmem_get_ua(pci, kbuf); |
|
39 |
uintptr_t pa = pcilib_kmem_get_pa(pci, kbuf); |
|
40 |
||
41 |
printf("User: %p\n", ua); |
|
42 |
printf("HW : 0x%lx\n", pa); |
|
43 |
||
44 |
int fd = open("/sys/class/test/test0/test_request", O_RDWR); |
|
45 |
if (fd >= 0) { |
|
46 |
size_t bytes; |
|
47 |
char res[64]; |
|
48 |
||
49 |
sprintf(res, "%p", ua); |
|
50 |
write(fd, res, strlen(res)); |
|
51 |
lseek(fd, SEEK_SET, 0); |
|
52 |
bytes = read(fd, res, sizeof(res) - 1); |
|
53 |
||
54 |
printf("Res : "); |
|
55 |
if (bytes < 0) { |
|
56 |
puts("Error"); |
|
57 |
} else { |
|
58 |
res[bytes] = 0; |
|
59 |
puts(res); |
|
60 |
}
|
|
61 |
||
62 |
close(fd); |
|
63 |
}
|
|
64 |
||
65 |
pcilib_free_kernel_memory(pci, kbuf, 0); |
|
66 |
||
67 |
pcilib_close(pci); |
|
68 |
}
|