aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/include/gpxe/uaccess.h
blob: 33aaed18eee9b3f7b82ce2db83d19476ce0443cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef _GPXE_UACCESS_H
#define _GPXE_UACCESS_H

/**
 * @file
 *
 * Access to external ("user") memory
 *
 * gPXE often needs to transfer data between internal and external
 * buffers.  On i386, the external buffers may require access via a
 * different segment, and the buffer address cannot be encoded into a
 * simple void * pointer.  The @c userptr_t type encapsulates the
 * information needed to identify an external buffer, and the
 * copy_to_user() and copy_from_user() functions provide methods for
 * transferring data between internal and external buffers.
 *
 * Note that userptr_t is an opaque type; in particular, performing
 * arithmetic upon a userptr_t is not allowed.
 *
 */

#include <stdint.h>
#include <string.h>
#include <gpxe/api.h>
#include <config/ioapi.h>

/**
 * A pointer to a user buffer
 *
 */
typedef unsigned long userptr_t;

/** Equivalent of NULL for user pointers */
#define UNULL ( ( userptr_t ) 0 )

/**
 * @defgroup uaccess_trivial Trivial user access API implementations
 *
 * User access API implementations that can be used by environments in
 * which virtual addresses allow access to all of memory.
 *
 * @{
 *
 */

/**
 * Convert virtual address to user pointer
 *
 * @v addr		Virtual address
 * @ret userptr		User pointer
 */
static inline __always_inline userptr_t
trivial_virt_to_user ( volatile const void *addr ) {
	return ( ( userptr_t ) addr );
}

/**
 * Convert user pointer to virtual address
 *
 * @v userptr		User pointer
 * @v offset		Offset from user pointer
 * @ret addr		Virtual address
 *
 * This operation is not available under all memory models.
 */
static inline __always_inline void *
trivial_user_to_virt ( userptr_t userptr, off_t offset ) {
	return ( ( void * ) userptr + offset );
}

/**
 * Add offset to user pointer
 *
 * @v userptr		User pointer
 * @v offset		Offset
 * @ret userptr		New pointer value
 */
static inline __always_inline userptr_t
trivial_userptr_add ( userptr_t userptr, off_t offset ) {
	return ( userptr + offset );
}

/**
 * Copy data between user buffers
 *
 * @v dest		Destination
 * @v dest_off		Destination offset
 * @v src		Source
 * @v src_off		Source offset
 * @v len		Length
 */
static inline __always_inline void
trivial_memcpy_user ( userptr_t dest, off_t dest_off,
		      userptr_t src, off_t src_off, size_t len ) {
	memcpy ( ( ( void * ) dest + dest_off ),
		 ( ( void * ) src + src_off ), len );
}

/**
 * Copy data between user buffers, allowing for overlap
 *
 * @v dest		Destination
 * @v dest_off		Destination offset
 * @v src		Source
 * @v src_off		Source offset
 * @v len		Length
 */
static inline __always_inline void
trivial_memmove_user ( userptr_t dest, off_t dest_off,
		       userptr_t src, off_t src_off, size_t len ) {
	memmove ( ( ( void * ) dest + dest_off ),
		  ( ( void * ) src + src_off ), len );
}

/**
 * Fill user buffer with a constant byte
 *
 * @v buffer		User buffer
 * @v offset		Offset within buffer
 * @v c			Constant byte with which to fill
 * @v len		Length
 */
static inline __always_inline void
trivial_memset_user ( userptr_t buffer, off_t offset, int c, size_t len ) {
	memset ( ( ( void * ) buffer + offset ), c, len );
}

/**
 * Find length of NUL-terminated string in user buffer
 *
 * @v buffer		User buffer
 * @v offset		Offset within buffer
 * @ret len		Length of string (excluding NUL)
 */
static inline __always_inline size_t
trivial_strlen_user ( userptr_t buffer, off_t offset ) {
	return strlen ( ( void * ) buffer + offset );
}

/**
 * Find character in user buffer
 *
 * @v buffer		User buffer
 * @v offset		Starting offset within buffer
 * @v c			Character to search for
 * @v len		Length of user buffer
 * @ret offset		Offset of character, or <0 if not found
 */
static inline __always_inline off_t
trivial_memchr_user ( userptr_t buffer, off_t offset, int c, size_t len ) {
	void *found;

	found = memchr ( ( ( void * ) buffer + offset ), c, len );
	return ( found ? ( found - ( void * ) buffer ) : -1 );
}

/** @} */

/**
 * Calculate static inline user access API function name
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 * @ret _subsys_func	Subsystem API function
 */
#define UACCESS_INLINE( _subsys, _api_func ) \
	SINGLE_API_INLINE ( UACCESS_PREFIX_ ## _subsys, _api_func )

/**
 * Provide an user access API implementation
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 * @v _func		Implementing function
 */
#define PROVIDE_UACCESS( _subsys, _api_func, _func ) \
	PROVIDE_SINGLE_API ( UACCESS_PREFIX_ ## _subsys, _api_func, _func )

/**
 * Provide a static inline user access API implementation
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 */
#define PROVIDE_UACCESS_INLINE( _subsys, _api_func ) \
	PROVIDE_SINGLE_API_INLINE ( UACCESS_PREFIX_ ## _subsys, _api_func )

/* Include all architecture-independent user access API headers */
#include <gpxe/efi/efi_uaccess.h>

/* Include all architecture-dependent user access API headers */
#include <bits/uaccess.h>

/**
 * Convert physical address to user pointer
 *
 * @v phys_addr		Physical address
 * @ret userptr		User pointer
 */
userptr_t phys_to_user ( unsigned long phys_addr );

/**
 * Convert user pointer to physical address
 *
 * @v userptr		User pointer
 * @v offset		Offset from user pointer
 * @ret phys_addr	Physical address
 */
unsigned long user_to_phys ( userptr_t userptr, off_t offset );

/**
 * Convert virtual address to user pointer
 *
 * @v addr		Virtual address
 * @ret userptr		User pointer
 */
userptr_t virt_to_user ( volatile const void *addr );

/**
 * Convert user pointer to virtual address
 *
 * @v userptr		User pointer
 * @v offset		Offset from user pointer
 * @ret addr		Virtual address
 *
 * This operation is not available under all memory models.
 */
void * user_to_virt ( userptr_t userptr, off_t offset );

/**
 * Add offset to user pointer
 *
 * @v userptr		User pointer
 * @v offset		Offset
 * @ret userptr		New pointer value
 */
userptr_t userptr_add ( userptr_t userptr, off_t offset );

/**
 * Convert virtual address to a physical address
 *
 * @v addr		Virtual address
 * @ret phys_addr	Physical address
 */
static inline __always_inline unsigned long
virt_to_phys ( volatile const void *addr ) {
	return user_to_phys ( virt_to_user ( addr ), 0 );
}

/**
 * Convert physical address to a virtual address
 *
 * @v addr		Virtual address
 * @ret phys_addr	Physical address
 *
 * This operation is not available under all memory models.
 */
static inline __always_inline void * phys_to_virt ( unsigned long phys_addr ) {
	return user_to_virt ( phys_to_user ( phys_addr ), 0 );
}

/**
 * Copy data between user buffers
 *
 * @v dest		Destination
 * @v dest_off		Destination offset
 * @v src		Source
 * @v src_off		Source offset
 * @v len		Length
 */
void memcpy_user ( userptr_t dest, off_t dest_off,
		   userptr_t src, off_t src_off, size_t len );

/**
 * Copy data to user buffer
 *
 * @v dest		Destination
 * @v dest_off		Destination offset
 * @v src		Source
 * @v len		Length
 */
static inline __always_inline void
copy_to_user ( userptr_t dest, off_t dest_off, const void *src, size_t len ) {
	memcpy_user ( dest, dest_off, virt_to_user ( src ), 0, len );
}

/**
 * Copy data from user buffer
 *
 * @v dest		Destination
 * @v src		Source
 * @v src_off		Source offset
 * @v len		Length
 */
static inline __always_inline void
copy_from_user ( void *dest, userptr_t src, off_t src_off, size_t len ) {
	memcpy_user ( virt_to_user ( dest ), 0, src, src_off, len );
}

/**
 * Copy data between user buffers, allowing for overlap
 *
 * @v dest		Destination
 * @v dest_off		Destination offset
 * @v src		Source
 * @v src_off		Source offset
 * @v len		Length
 */
void memmove_user ( userptr_t dest, off_t dest_off,
		    userptr_t src, off_t src_off, size_t len );

/**
 * Fill user buffer with a constant byte
 *
 * @v userptr		User buffer
 * @v offset		Offset within buffer
 * @v c			Constant byte with which to fill
 * @v len		Length
 */
void memset_user ( userptr_t userptr, off_t offset, int c, size_t len );

/**
 * Find length of NUL-terminated string in user buffer
 *
 * @v userptr		User buffer
 * @v offset		Offset within buffer
 * @ret len		Length of string (excluding NUL)
 */
size_t strlen_user ( userptr_t userptr, off_t offset );

/**
 * Find character in user buffer
 *
 * @v userptr		User buffer
 * @v offset		Starting offset within buffer
 * @v c			Character to search for
 * @v len		Length of user buffer
 * @ret offset		Offset of character, or <0 if not found
 */
off_t memchr_user ( userptr_t userptr, off_t offset, int c, size_t len );

#endif /* _GPXE_UACCESS_H */