From e43dd799df553a1fac521ea3d305fb8ce3d70f56 Mon Sep 17 00:00:00 2001 From: rubberhead Date: Sun, 28 Jan 2024 08:01:59 +0000 Subject: [PATCH] Updated reader userspace program --- src/aarch64-linux-flush-dcache/my_shmem.c | 7 +- .../userspace/reader.c | 92 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/aarch64-linux-flush-dcache/userspace/reader.c diff --git a/src/aarch64-linux-flush-dcache/my_shmem.c b/src/aarch64-linux-flush-dcache/my_shmem.c index 240d6a1..4e00aa6 100644 --- a/src/aarch64-linux-flush-dcache/my_shmem.c +++ b/src/aarch64-linux-flush-dcache/my_shmem.c @@ -263,7 +263,12 @@ static int my_shmem_fops_release(struct inode *inode, struct file *filp) return 0; } -// static int +static int my_shmem_fops_fsync( + struct file *filp, loff_t bgn_off, loff_t end_off, int datasync) +{ + pr_info("[%s] Entered.\n", __func__); + +} static const struct file_operations my_shmem_fops = { .owner = THIS_MODULE, diff --git a/src/aarch64-linux-flush-dcache/userspace/reader.c b/src/aarch64-linux-flush-dcache/userspace/reader.c new file mode 100644 index 0000000..c8715d8 --- /dev/null +++ b/src/aarch64-linux-flush-dcache/userspace/reader.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include // sysconf +#include // printf +#include // exit +#include // O_READ +#include // mmap +#include // strlen +#include // errno + +#define eprintf(args...) fprintf(stderr, ##args) + +const char *DEVICE_PATH = "/dev/my_shmem"; +const struct timespec WAIT_DURATION = { + .tv_nsec = 100000000, // 0.1s + .tv_sec = 0, +}; + +static atomic_bool timeout; + +void *pthread_fn(void *); + +int main(int argc, char *argv[]) +{ + const long PAGE_SIZE = sysconf(_SC_PAGESIZE); + if (PAGE_SIZE == -1) { + eprintf("Error retrieving page size: %d.\n", errno); + exit(EXIT_FAILURE); + } + + // open device file w/ RW perms + FILE *fp = fopen(DEVICE_PATH, "r+"); + if (!fp) { + eprintf("Error opening device %s: %d.\n", DEVICE_PATH, errno); + exit(EXIT_FAILURE); + } + int fd = fileno(fp); + if (fd == -1) { + eprintf("Error retrieving file descriptor: %d.\n", errno); + exit(EXIT_FAILURE); + } + + // mmap device + void *buf = mmap(NULL, PAGE_SIZE * 2, PROT_READ, + MAP_SHARED, fd, 0); + if (!buf) { + eprintf("Error mmap-ing: %d.\n", errno); + exit(EXIT_FAILURE); + } + + // Read from mmap-ed device + pthread_t thread_handle; + int ret_pth_create = pthread_create( + &thread_handle, NULL, &pthread_fn, buf); + if (ret_pth_create) { + eprintf("Error pthread_create: %d.\n", ret_pth_create); + } + + // Sleep for some time, then inform exit. + nanosleep(&WAIT_DURATION, NULL); + atomic_store(&timeout, true); + + // Join thread + pthread_join(thread_handle, NULL); + + // Unmap device + munmap(buf, PAGE_SIZE * 2); + + // Close device + fclose(fp); + + printf("Finished!\n"); + exit(EXIT_SUCCESS); +} + +void *pthread_fn(void *mmap_buf) { + bool time_to_exit; + char readbuf[5]; + + for (;;) { + time_to_exit = atomic_load(&timeout); + if (time_to_exit) break; + + memcpy(readbuf, mmap_buf, 4); + printf("Read 0x%x 0x%x 0x%x 0x%x.\n", + readbuf[0], readbuf[1], readbuf[2], readbuf[3]); + } + + return NULL; +} \ No newline at end of file