GCC may emit 4x 1 Byte reads in case of our regular mmio_read32 accessor, when used in combination with -Os. Yes, I've seen it.
Define safe handlers to overcome this issue. Signed-off-by: Ralf Ramsauer <[email protected]> --- hypervisor/arch/riscv/include/asm/mmio.h | 67 ++++++++++++++++++++++++ hypervisor/include/jailhouse/mmio.h | 4 ++ 2 files changed, 71 insertions(+) diff --git a/hypervisor/arch/riscv/include/asm/mmio.h b/hypervisor/arch/riscv/include/asm/mmio.h index e69de29b..e19d0cec 100644 --- a/hypervisor/arch/riscv/include/asm/mmio.h +++ b/hypervisor/arch/riscv/include/asm/mmio.h @@ -0,0 +1,67 @@ +/* + * Jailhouse, a Linux-based partitioning hypervisor + * + * Copyright (c) OTH Regensburg, 2022 + * + * Authors: + * Ralf Ramsauer <[email protected]> + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +/* + * We need those definitions, as I saw the compiler emitting 4x 1B loads in + * case of mmio_read32. + */ + +#define DEFINE_MMIO_READ +#define DEFINE_MMIO_WRITE + +static inline void mmio_write8(volatile void *addr, u8 val) +{ + asm volatile("sb %0, 0(%1)" : : "r" (val), "r" (addr)); +} + +static inline void mmio_write16(volatile void *addr, u16 val) +{ + asm volatile("sh %0, 0(%1)" : : "r" (val), "r" (addr)); +} + +static inline void mmio_write32(volatile void *addr, u32 val) +{ + asm volatile("sw %0, 0(%1)" : : "r" (val), "r" (addr)); +} + +static inline void mmio_write64(volatile void *addr, u64 val) +{ + asm volatile("sd %0, 0(%1)" : : "r" (val), "r" (addr)); +} + +static inline u8 mmio_read8(const volatile void *addr) +{ + u8 val; + asm volatile("lb %0, 0(%1)" : "=r" (val) : "r" (addr)); + return val; +} + +static inline u16 mmio_read16(const volatile void *addr) +{ + u16 val; + asm volatile("lh %0, 0(%1)" : "=r" (val) : "r" (addr)); + return val; +} + +static inline u32 mmio_read32(const volatile void *addr) +{ + u32 val; + asm volatile("lw %0, 0(%1)" : "=r" (val) : "r" (addr)); + return val; +} + +static inline u64 mmio_read64(const volatile void *addr) +{ + u64 val; + asm volatile("ld %0, 0(%1)" : "=r" (val) : "r" (addr)); + return val; +} diff --git a/hypervisor/include/jailhouse/mmio.h b/hypervisor/include/jailhouse/mmio.h index aa85a089..f5ace9e9 100644 --- a/hypervisor/include/jailhouse/mmio.h +++ b/hypervisor/include/jailhouse/mmio.h @@ -32,6 +32,7 @@ struct cell; * Define MMIO read accessor. * @param size Access size. */ +#ifndef DEFINE_MMIO_READ #define DEFINE_MMIO_READ(size) \ static inline u##size mmio_read##size(void *address) \ { \ @@ -49,12 +50,14 @@ DEFINE_MMIO_READ(8) DEFINE_MMIO_READ(16) DEFINE_MMIO_READ(32) DEFINE_MMIO_READ(64) +#endif /** @} */ /** * Define MMIO write accessor. * @param size Access size. */ +#ifndef DEFINE_MMIO_WRITE #define DEFINE_MMIO_WRITE(size) \ static inline void mmio_write##size(void *address, u##size value) \ { \ @@ -71,6 +74,7 @@ DEFINE_MMIO_WRITE(8) DEFINE_MMIO_WRITE(16) DEFINE_MMIO_WRITE(32) DEFINE_MMIO_WRITE(64) +#endif /** @} */ /** -- 2.36.1 -- You received this message because you are subscribed to the Google Groups "Jailhouse" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jailhouse-dev/20220627132905.4338-4-ralf.ramsauer%40oth-regensburg.de.
