On Mon, May 16, 2016 at 12:53:54AM +0200, Mark Kettenis wrote:
> So Theo's SROP mitigation diff made my armv7 machine completely
> unstable. Filesystem-related panics, random core dumps, files with
> blocks of zeroes on them, etc.
>
> Looking at the changes in question, there is no way they're
> responsible for this behavior. This smells like cache-related bug
> triggered by changes in the memory layout in the kernel. Since I'm
> running with root on an SD card hanging off imxesdhc(4), I started
> looking for missing bus_dmamap_sync() calls in that driver. Found
> one, but notone that actually mattered. Then I turned to the actual
> bus_dmamap_sync(9) implementation. It has a nice comment explaining
> when cache maintenance is needed. It mentions that the D-Cache needs
> to be invalidated for a POSTREAD operation. But it doesn't actually
> do that.
>
> The NetBSD code (from which our code is derived) does implement this,
> and has an additional comment:
>
> /*
> * Cortex CPUs can do speculative loads so we need to clean the cache
> * after a DMA read to deal with any speculatively loaded cache lines.
> * Since these can't be dirty, we can just invalidate them and don't
> * have to worry about having to write back their contents.
> */
>
> Adding the relevant bit of code seems to fix the issue.
>
> So here's the diff. It seems that this code isn't needed on the
> pre-ARMv7 hardware. So I could sprinkle in an #ifdef CPU_ARMv7 if
> people think that is worth it. But that might make us forget this
> code for future ARM processor generations.
>
> ok?
membar_sync() only exists for CPU_ARMv7 currently, I'm not sure
exactly when it was introduced (some v5 and all v6?) but we might be
able to use CP15DMB for armish/zaurus?
Index: atomic.h
===================================================================
RCS file: /cvs/src/sys/arch/arm/include/atomic.h,v
retrieving revision 1.14
diff -u -p -r1.14 atomic.h
--- atomic.h 25 Apr 2016 08:00:43 -0000 1.14
+++ atomic.h 16 May 2016 02:47:03 -0000
@@ -465,8 +465,19 @@ atomic_clearbits_int(volatile unsigned i
: "memory", "cc"
);
}
+#endif /* CPU_ARMv7 */
#define __membar(_f) do { __asm __volatile(_f ::: "memory"); } while (0)
+
+#if !defined(CPU_ARMv7)
+ /* use CP15DMB */
+#define membar_enter() __membar("mcr p15, 0, r0, c7, c10, 5")
+#define membar_exit() __membar("mcr p15, 0, r0, c7, c10, 5")
+#define membar_producer() __membar("mcr p15, 0, r0, c7, c10, 5")
+#define membar_consumer() __membar("mcr p15, 0, r0, c7, c10, 5")
+#define membar_sync() __membar("mcr p15, 0, r0, c7, c10, 5")
+
+#else /* !CPU_ARMv7 */
#define membar_enter() __membar("dmb sy")
#define membar_exit() __membar("dmb sy")