| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
#ifndef __VDSO_UNALIGNED_H |
| 3 |
#define __VDSO_UNALIGNED_H |
| 4 |
|
| 5 |
#include <linux/compiler_types.h> |
| 6 |
|
| 7 |
/** |
| 8 |
* __get_unaligned_t - read an unaligned value from memory. |
| 9 |
* @type: the type to load from the pointer. |
| 10 |
* @ptr: the pointer to load from. |
| 11 |
* |
| 12 |
* Use memcpy to affect an unaligned type sized load avoiding undefined behavior |
| 13 |
* from approaches like type punning that require -fno-strict-aliasing in order |
| 14 |
* to be correct. As type may be const, use __unqual_scalar_typeof to map to a |
| 15 |
* non-const type - you can't memcpy into a const type. The |
| 16 |
* __get_unaligned_ctrl_type gives __unqual_scalar_typeof its required |
| 17 |
* expression rather than type, a pointer is used to avoid warnings about mixing |
| 18 |
* the use of 0 and NULL. The void* cast silences ubsan warnings. |
| 19 |
*/ |
| 20 |
#define __get_unaligned_t(type, ptr) ({ \ |
| 21 |
type *__get_unaligned_ctrl_type __always_unused = NULL; \ |
| 22 |
__unqual_scalar_typeof(*__get_unaligned_ctrl_type) __get_unaligned_val; \ |
| 23 |
__builtin_memcpy(&__get_unaligned_val, (void *)(ptr), \ |
| 24 |
sizeof(__get_unaligned_val)); \ |
| 25 |
__get_unaligned_val; \ |
| 26 |
}) |
| 27 |
|
| 28 |
/** |
| 29 |
* __put_unaligned_t - write an unaligned value to memory. |
| 30 |
* @type: the type of the value to store. |
| 31 |
* @val: the value to store. |
| 32 |
* @ptr: the pointer to store to. |
| 33 |
* |
| 34 |
* Use memcpy to affect an unaligned type sized store avoiding undefined |
| 35 |
* behavior from approaches like type punning that require -fno-strict-aliasing |
| 36 |
* in order to be correct. The void* cast silences ubsan warnings. |
| 37 |
*/ |
| 38 |
#define __put_unaligned_t(type, val, ptr) do { \ |
| 39 |
type __put_unaligned_val = (val); \ |
| 40 |
__builtin_memcpy((void *)(ptr), &__put_unaligned_val, \ |
| 41 |
sizeof(__put_unaligned_val)); \ |
| 42 |
} while (0) |
| 43 |
|
| 44 |
#endif /* __VDSO_UNALIGNED_H */ |