mirror of
https://github.com/gnif/LookingGlass.git
synced 2024-11-22 13:37:22 +00:00
328f9078ee
This commit makes the test program try the following cases: * mmaping 0-offset dmabuf with 0 offset * mmaping 0-offset dmabuf with 1 page offset * mmaping page-offset dmabuf with 0 offset * mmaping page-offset dmabuf with 1 page offset * mmaping device with 0 offset * mmaping device with 1 page offset
119 lines
2.9 KiB
C
119 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <inttypes.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/syscall.h>
|
|
#include <sys/mman.h>
|
|
|
|
#include "kvmfr.h"
|
|
|
|
int main(void)
|
|
{
|
|
int page_size = getpagesize();
|
|
|
|
int fd = open("/dev/kvmfr0", O_RDWR);
|
|
if (fd < 0)
|
|
{
|
|
perror("open");
|
|
return -1;
|
|
}
|
|
|
|
unsigned long size = ioctl(fd, KVMFR_DMABUF_GETSIZE , 0);
|
|
printf("Size: %lu MiB\n", size / 1024 / 1024);
|
|
|
|
// mmaping 0-offset dmabuf with 0 offset
|
|
struct kvmfr_dmabuf_create create =
|
|
{
|
|
.flags = KVMFR_DMABUF_FLAG_CLOEXEC,
|
|
.offset = 0x0,
|
|
.size = size,
|
|
};
|
|
int dmaFd = ioctl(fd, KVMFR_DMABUF_CREATE, &create);
|
|
if (dmaFd < 0)
|
|
{
|
|
perror("ioctl");
|
|
return -1;
|
|
}
|
|
|
|
void * mem = mmap(NULL, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, dmaFd, 0);
|
|
if (mem == MAP_FAILED)
|
|
{
|
|
perror("mmap on dmabuf with no offset");
|
|
return -1;
|
|
}
|
|
memset(mem, 0xAA, create.size);
|
|
strcpy(mem + page_size, "Hello, world!");
|
|
munmap(mem, create.size);
|
|
|
|
// mmaping 0-offset dmabuf with 1 page offset
|
|
mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, dmaFd, page_size);
|
|
if (mem == MAP_FAILED)
|
|
{
|
|
perror("mmap on dmabuf with offset");
|
|
return -1;
|
|
}
|
|
printf("Read string: %s\n", (char *) mem);
|
|
munmap(mem, page_size);
|
|
|
|
close(dmaFd);
|
|
|
|
// mmaping page-offset dmabuf with 0 offset
|
|
create.offset = page_size;
|
|
create.size = 2 * page_size;
|
|
dmaFd = ioctl(fd, KVMFR_DMABUF_CREATE, &create);
|
|
mem = mmap(NULL, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, dmaFd, 0);
|
|
if (mem == MAP_FAILED)
|
|
{
|
|
perror("mmap on offset dmabuf with no offset");
|
|
return -1;
|
|
}
|
|
printf("Read string: %s\n", (char *) mem);
|
|
munmap(mem, create.size);
|
|
|
|
// mmaping page-offset dmabuf with 1 page offset
|
|
char *bytes = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, dmaFd, page_size);
|
|
if (bytes == MAP_FAILED)
|
|
{
|
|
perror("mmap on offset dmabuf with offset");
|
|
return -1;
|
|
}
|
|
for (int i = 0; i < page_size; i++)
|
|
if (bytes[i] != (char) 0xAA)
|
|
printf("Index: %d: 0x%02x\n", i, (unsigned) bytes[i]);
|
|
munmap(mem, page_size);
|
|
|
|
close(dmaFd);
|
|
|
|
// mmaping device with 0 offset
|
|
mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, page_size);
|
|
if (mem == MAP_FAILED)
|
|
{
|
|
perror("mmap on file with offset");
|
|
return -1;
|
|
}
|
|
printf("Read string: %s\n", (char *) mem);
|
|
munmap(mem, page_size);
|
|
|
|
// mmaping device with 0 offset
|
|
uint32_t *data = mmap(NULL, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
if (data == MAP_FAILED)
|
|
{
|
|
perror("mmap on file with no offset");
|
|
return -1;
|
|
}
|
|
for (size_t i = 0; i < create.size / sizeof(uint32_t); i++)
|
|
{
|
|
if (data[i] != 0xAAAAAAAA)
|
|
printf("Index %lu: 0x%08" PRIx32 "\n", i, data[i]);
|
|
}
|
|
munmap(data, create.size);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|