aboutsummaryrefslogtreecommitdiff
path: root/core/include/io.h
diff options
context:
space:
mode:
authorVolodymyr Babchuk <vlad.babchuk@gmail.com>2018-09-03 20:16:08 +0300
committerJérôme Forissier <jerome.forissier@linaro.org>2018-09-07 14:16:43 +0200
commit81801f833a1f3af4cf098de75211eea5df924c51 (patch)
treea30d60bd61065e4fa1ad328f00fcb94269188067 /core/include/io.h
parentc86451073201c11186b5b6fcaab93eff24ec26a3 (diff)
io.h: add READ_ONCE macro
Compiler can rearrange memory reads and writes if it does not see any dependency on them. This can be troublesome if we deal with memory which is shared with non-secure world. READ_ONCE macro ensures that compiler will read memory only once. It is simple wrapper over __compiler_atomic_load(), but it's name emphasizes it's function. Signed-off-by: Volodymyr Babchuk <vlad.babchuk@gmail.com> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Diffstat (limited to 'core/include/io.h')
-rw-r--r--core/include/io.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/core/include/io.h b/core/include/io.h
index 8e81f429..25eca0e9 100644
--- a/core/include/io.h
+++ b/core/include/io.h
@@ -5,10 +5,20 @@
#ifndef IO_H
#define IO_H
+#include <compiler.h>
#include <stdint.h>
#include <types_ext.h>
#include <utee_defines.h>
+/*
+ * Make sure that compiler reads given variable only once. This is needed
+ * in cases when we have normal shared memory, and this memory can be changed
+ * at any moment. Compiler does not knows about this, so it can optimize memory
+ * access in any way, including repeated read from the same address. This macro
+ * enforces compiler to access memory only once.
+ */
+#define READ_ONCE(p) __compiler_atomic_load(&(p))
+
static inline void write8(uint8_t val, vaddr_t addr)
{
*(volatile uint8_t *)addr = val;