libpynq (release 5EWC0-2023 version 0.2.2 of 2023-09-24 22:22)
Loading...
Searching...
No Matches
arm_shared_memory_system.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30#include <unistd.h>
31
32void *arm_shared_init(arm_shared *handle, const uint32_t address,
33 const uint32_t length) {
34 if (handle == NULL) {
35 fprintf(stderr, "You need to pass a valid handle to %s\n", __FUNCTION__);
36 exit(EXIT_FAILURE);
37 }
38
39 handle->address = address;
40 handle->length = length;
41 handle->file_descriptor = open("/dev/mem", O_RDWR | O_SYNC);
42 if (handle->file_descriptor < 0) {
43 fprintf(stderr,
44 "FAILED open memory: %s, please run with sufficient permissions "
45 "(sudo).\n",
46 strerror(errno));
47 exit(EXIT_FAILURE);
48 }
49
50 long page_size = sysconf(_SC_PAGE_SIZE);
51
52 uint32_t start_address = handle->address;
53 uint32_t page_offset = start_address % page_size;
54 start_address -= page_offset;
55 handle->length += page_offset;
56
57 handle->mmaped_region =
58 mmap(NULL, handle->length, PROT_READ | PROT_WRITE, MAP_SHARED,
59 handle->file_descriptor, start_address);
60
61 if (handle->mmaped_region == MAP_FAILED) {
62 fprintf(stderr, "FAILED to memory map requested region: %s\n",
63 strerror(errno));
64 close(handle->file_descriptor);
65 exit(EXIT_FAILURE);
66 }
67 return (void *)(((uint32_t)(handle->mmaped_region)) + page_offset);
68}
69
71 if (handle == NULL) {
72 fprintf(stderr, "You need to pass a valid handle to %s\n", __FUNCTION__);
73 exit(EXIT_FAILURE);
74 }
75 if (handle->mmaped_region != MAP_FAILED) {
76 munmap(handle->mmaped_region, handle->length);
77 }
78 if (handle->file_descriptor >= 0) {
79 close(handle->file_descriptor);
80 }
81}
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)