libmetal
sys.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of Xilinx nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * @file linux/sys.h
33  * @brief Linux system primitives for libmetal.
34  */
35 
36 #ifndef __METAL_SYS__H__
37 #error "Include metal/sys.h instead of metal/linux/sys.h"
38 #endif
39 
40 #ifndef __METAL_LINUX_SYS__H__
41 #define __METAL_LINUX_SYS__H__
42 
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <libgen.h>
46 #include <limits.h>
47 #include <pthread.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdarg.h>
51 #include <syslog.h>
52 #include <sys/file.h>
53 #include <sys/mman.h>
54 #include <sys/stat.h>
55 #include <sys/syscall.h>
56 #include <sys/types.h>
57 #include <sys/wait.h>
58 #include <unistd.h>
59 
60 #include <linux/futex.h>
61 #include <sysfs/libsysfs.h>
62 #ifdef HAVE_HUGETLBFS_H
63 #include <hugetlbfs.h>
64 #endif
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 #define METAL_INVALID_VADDR NULL
71 #define MAX_PAGE_SIZES 32
72 
76  size_t page_size;
77 
79  unsigned long page_shift;
80 
82  char path[PATH_MAX];
83 
86 };
87 
89 struct metal_state {
90 
93 
95  int data_fd;
96 
98  unsigned long page_size;
99 
101  unsigned long page_shift;
102 
104  const char *sysfs_path;
105 
107  const char *tmp_path;
108 
111 
114 
117 };
118 
119 #ifdef METAL_INTERNAL
120 extern int metal_linux_bus_init(void);
121 extern void metal_linux_bus_finish(void);
122 
123 extern int metal_open(const char *path, int shm);
124 extern int metal_open_unlinked(const char *path, int shm);
125 extern int metal_mktemp(char *template, int fifo);
126 extern int metal_mktemp_unlinked(char *template);
127 
128 extern int metal_map(int fd, off_t offset, size_t size, int expand,
129  int flags, void **result);
130 extern int metal_unmap(void *mem, size_t size);
131 extern int metal_mlock(void *mem, size_t size);
132 
133 extern void metal_randomize_string(char *template);
134 extern void metal_mktemp_template(char template[PATH_MAX],
135  const char *name);
136 extern int metal_virt2phys(void *addr, unsigned long *phys);
137 
138 #define metal_for_each_page_size_up(ps) \
139  for ((ps) = &_metal.page_sizes[0]; \
140  (ps) <= &_metal.page_sizes[_metal.num_page_sizes - 1]; \
141  (ps)++)
142 
143 #define metal_for_each_page_size_down(ps) \
144  for ((ps) = &_metal.page_sizes[_metal.num_page_sizes - 1]; \
145  (ps) >= &_metal.page_sizes[0]; \
146  (ps)--)
147 
148 #endif
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif /* __METAL_LINUX_SYS__H__ */
void metal_linux_bus_finish(void)
Definition: device.c:641
int metal_mktemp_unlinked(char *template)
Open (or create) and unlink a temporary file.
Definition: utilities.c:195
int metal_linux_bus_init(void)
Definition: device.c:630
int pagemap_fd
Definition: sys.h:116
Definition: sys.h:92
#define MAX_PAGE_SIZES
Definition: sys.h:71
int data_fd
Definition: sys.h:95
size_t page_size
Definition: sys.h:76
const char * tmp_path
Definition: sys.h:107
int metal_open_unlinked(const char *path, int shm)
Open (or create) and unlink a file.
Definition: utilities.c:75
Definition: sys.h:54
int num_page_sizes
Definition: sys.h:113
int metal_map(int fd, off_t offset, size_t size, int expand, int flags, void **result)
Map a segment of a file/device.
Definition: utilities.c:227
unsigned long page_shift
Definition: sys.h:101
int metal_mlock(void *mem, size_t size)
Lock in a region of the process address space.
Definition: utilities.c:281
int metal_mktemp(char *template, int fifo)
Create a temporary file or fifo.
Definition: utilities.c:133
void metal_randomize_string(char *template)
Randomize a string.
Definition: utilities.c:98
char path[PATH_MAX]
Definition: sys.h:82
unsigned long page_size
Definition: sys.h:98
unsigned long page_shift
Definition: sys.h:79
int mmap_flags
Definition: sys.h:85
Definition: sys.h:74
int metal_unmap(void *mem, size_t size)
Unmap a segment of the process address space.
Definition: utilities.c:269
struct metal_page_size page_sizes[MAX_PAGE_SIZES]
Definition: sys.h:110
void metal_mktemp_template(char template[PATH_MAX], const char *name)
Create a file name template suitable for use with metal_mktemp.
Definition: utilities.c:115
const char * sysfs_path
Definition: sys.h:104
int metal_open(const char *path, int shm)
Open (or create) a file.
Definition: utilities.c:49
int metal_virt2phys(void *addr, unsigned long *phys)
Definition: utilities.c:286
struct metal_common_state common
Definition: sys.h:57