Skip to contentSkip to conversation

arm: Set TTB XN bit in case DCACHE_OFF for LPAE mode

PATCH New Keerthy 3 participants 8 messages Oct 28, 2016 – Oct 29, 2016 Arm (32-bit)

lore
Share & tools
lore

Keerthy

author
#0 lore raw

PATCH 1/2 arm: Set TTB XN bit in case DCACHE_OFF for LPAE mode

While we setup the mmu initially we mark set_section_dcache with DCACHE_OFF flag. In case of non-LPAE mode the DCACHE_OFF macro is rightly defined with TTB_SECT_XN_MASK set so as to mark all the 4GB XN. In case of LPAE mode XN(Execute-never) bit is not set with DCACHE_OFF. Hence XN bit is not set by default for DCACHE_OFF which keeps all the regions execute okay and this leads to random speculative fetches in random memory regions which was eventually caught by kernel omap-l3-noc driver.

Fix this to mark the regions as XN by default.

---
 arch/arm/include/asm/system.h | 2 +-
 arch/arm/lib/cache-cp15.c     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
arch/arm/include/asm/system.h +1 -1
index b928bd8..2f430ad 100644
@@ -329,7 +329,7 @@ static inline void set_dacr(unsigned int val)
 
 /* options available for data cache on each page */
 enum dcache_option {
-	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0),
+	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0) | TTB_SECT_XN_MASK,
 	DCACHE_WRITETHROUGH = TTB_SECT | TTB_SECT_MAIR(1),
 	DCACHE_WRITEBACK = TTB_SECT | TTB_SECT_MAIR(2),
 	DCACHE_WRITEALLOC = TTB_SECT | TTB_SECT_MAIR(3),
arch/arm/lib/cache-cp15.c +5 -0
index 70e94f0..4d9903e 100644
@@ -71,8 +71,13 @@ void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
 
 	end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
 	start = start >> MMU_SECTION_SHIFT;
+#ifdef CONFIG_ARMV7_LPAE
+	debug("%s: start=%pa, size=%zu, option=%llu\n", __func__, &start, size,
+	      option);
+#else
 	debug("%s: start=%pa, size=%zu, option=%d\n", __func__, &start, size,
 	      option);
+#endif
 	for (upto = start; upto < end; upto++)
 		set_section_dcache(upto, option);
 
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 2/2 arm: cpu: armv7: omap-common: Clear XN bit in the ARMV7_DCACHE_POLICY

Clear the XN bit in the ARMV7_DCACHE_POLICY so as to mark the regions as execute okay.

---
 arch/arm/cpu/armv7/omap-common/omap-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/cpu/armv7/omap-common/omap-cache.c +1 -1
index b37163a..a71aa0d 100644
@@ -34,7 +34,7 @@ DECLARE_GLOBAL_DATA_PTR;
  */
 
 #ifdef CONFIG_ARMV7_LPAE
-#define ARMV7_DCACHE_POLICY	DCACHE_WRITEALLOC
+#define ARMV7_DCACHE_POLICY	DCACHE_WRITEALLOC & ~TTB_SECT_XN_MASK
 #else
 #define ARMV7_DCACHE_POLICY	DCACHE_WRITEBACK & ~TTB_SECT_XN_MASK
 #endif
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/2 arm: cpu: armv7: omap-common: Clear XN bit in the ARMV7_DCACHE_POLICY

Quoted 4 lines · Keerthy

Clear the XN bit in the ARMV7_DCACHE_POLICY so as to mark the regions as execute okay.

How did you ever get the bit set in DCACHE_WRITEALLOC or WRITEBACK in the first place? Both are RAM mapping flags which shouldn't have the XN mask set.

Alex

Quoted 18 lines
---
 arch/arm/cpu/armv7/omap-common/omap-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/cpu/armv7/omap-common/omap-cache.c +1 -1
index b37163a..a71aa0d 100644
@@ -34,7 +34,7 @@ DECLARE_GLOBAL_DATA_PTR;
  */
 
 #ifdef CONFIG_ARMV7_LPAE
-#define ARMV7_DCACHE_POLICY	DCACHE_WRITEALLOC
+#define ARMV7_DCACHE_POLICY	DCACHE_WRITEALLOC & ~TTB_SECT_XN_MASK
 #else
 #define ARMV7_DCACHE_POLICY	DCACHE_WRITEBACK & ~TTB_SECT_XN_MASK
 #endif
author
#3 lore raw

PATCH 2/2 arm: cpu: armv7: omap-common: Clear XN bit in the ARMV7_DCACHE_POLICY

Quoted 11 lines · Alexander Graf
Quoted 4 lines · Keerthy

Clear the XN bit in the ARMV7_DCACHE_POLICY so as to mark the regions as execute okay.

How did you ever get the bit set in DCACHE_WRITEALLOC or WRITEBACK in the first place? Both are RAM mapping flags which shouldn't have the XN mask set.

Just searched through none of them have the bit sit so that is redundant. I added to be doubly sure and can be removed. This patch is not needed.

Quoted 22 lines

Alex

Quoted 18 lines
---
 arch/arm/cpu/armv7/omap-common/omap-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/cpu/armv7/omap-common/omap-cache.c +1 -1
index b37163a..a71aa0d 100644
@@ -34,7 +34,7 @@ DECLARE_GLOBAL_DATA_PTR;
  */
 
 #ifdef CONFIG_ARMV7_LPAE
-#define ARMV7_DCACHE_POLICY	DCACHE_WRITEALLOC
+#define ARMV7_DCACHE_POLICY	DCACHE_WRITEALLOC & ~TTB_SECT_XN_MASK
 #else
 #define ARMV7_DCACHE_POLICY	DCACHE_WRITEBACK & ~TTB_SECT_XN_MASK
 #endif
#4 lore raw

PATCH 2/2 arm: cpu: armv7: omap-common: Clear XN bit in the ARMV7_DCACHE_POLICY

In reply to Keerthy
Quoted 4 lines · Keerthy

Clear the XN bit in the ARMV7_DCACHE_POLICY so as to mark the regions as execute okay.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20161028/a3b1f3b6/attachment.sig>
#5 lore raw

PATCH 1/2 arm: Set TTB XN bit in case DCACHE_OFF for LPAE mode

In reply to Keerthy
Quoted 9 lines · Keerthy

While we setup the mmu initially we mark set_section_dcache with DCACHE_OFF flag. In case of non-LPAE mode the DCACHE_OFF macro is rightly defined with TTB_SECT_XN_MASK set so as to mark all the 4GB XN. In case of LPAE mode XN(Execute-never) bit is not set with DCACHE_OFF. Hence XN bit is not set by default for DCACHE_OFF which keeps all the regions execute okay and this leads to random speculative fetches in random memory regions which was eventually caught by kernel omap-l3-noc driver.

Quoted 3 lines

Fix this to mark the regions as XN by default.

I guess in theory we could have the same problem on armv8, but we don't right now because we only map known populated memory regions. And cross our fingers that speculative instruction fetches don't both MMIO regions. Hmm.

Alex

Quoted 37 lines
---
 arch/arm/include/asm/system.h | 2 +-
 arch/arm/lib/cache-cp15.c     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
arch/arm/include/asm/system.h +1 -1
index b928bd8..2f430ad 100644
@@ -329,7 +329,7 @@ static inline void set_dacr(unsigned int val)
 
 /* options available for data cache on each page */
 enum dcache_option {
-	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0),
+	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0) | TTB_SECT_XN_MASK,
 	DCACHE_WRITETHROUGH = TTB_SECT | TTB_SECT_MAIR(1),
 	DCACHE_WRITEBACK = TTB_SECT | TTB_SECT_MAIR(2),
 	DCACHE_WRITEALLOC = TTB_SECT | TTB_SECT_MAIR(3),
arch/arm/lib/cache-cp15.c +5 -0
index 70e94f0..4d9903e 100644
@@ -71,8 +71,13 @@ void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
 
 	end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
 	start = start >> MMU_SECTION_SHIFT;
+#ifdef CONFIG_ARMV7_LPAE
+	debug("%s: start=%pa, size=%zu, option=%llu\n", __func__, &start, size,
+	      option);
+#else
 	debug("%s: start=%pa, size=%zu, option=%d\n", __func__, &start, size,
 	      option);
+#endif
 	for (upto = start; upto < end; upto++)
 		set_section_dcache(upto, option);
 
#6 lore raw

PATCH 1/2 arm: Set TTB XN bit in case DCACHE_OFF for LPAE mode

In reply to Keerthy
Quoted 35 lines · Keerthy

While we setup the mmu initially we mark set_section_dcache with DCACHE_OFF flag. In case of non-LPAE mode the DCACHE_OFF macro is rightly defined with TTB_SECT_XN_MASK set so as to mark all the 4GB XN. In case of LPAE mode XN(Execute-never) bit is not set with DCACHE_OFF. Hence XN bit is not set by default for DCACHE_OFF which keeps all the regions execute okay and this leads to random speculative fetches in random memory regions which was eventually caught by kernel omap-l3-noc driver.

Fix this to mark the regions as XN by default.

---
 arch/arm/include/asm/system.h | 2 +-
 arch/arm/lib/cache-cp15.c     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
arch/arm/include/asm/system.h +1 -1
index b928bd8..2f430ad 100644
@@ -329,7 +329,7 @@ static inline void set_dacr(unsigned int val)
 
 /* options available for data cache on each page */
 enum dcache_option {
-	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0),
+	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0) | TTB_SECT_XN_MASK,
 	DCACHE_WRITETHROUGH = TTB_SECT | TTB_SECT_MAIR(1),
 	DCACHE_WRITEBACK = TTB_SECT | TTB_SECT_MAIR(2),
 	DCACHE_WRITEALLOC = TTB_SECT | TTB_SECT_MAIR(3),
arch/arm/lib/cache-cp15.c +0 -0
index 70e94f0..4d9903e 100644

arch/arm/lib/cache-cp15.c void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size, · line 80

Quoted 10 lines
arch/arm/lib/cache-cp15.c +5 -0
@@ -71,8 +71,13 @@ void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
 
 	end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
 	start = start >> MMU_SECTION_SHIFT;
+#ifdef CONFIG_ARMV7_LPAE
+	debug("%s: start=%pa, size=%zu, option=%llu\n", __func__, &start, size,
+	      option);
+#else
 	debug("%s: start=%pa, size=%zu, option=%d\n", __func__, &start, size,
 	      option);
+#endif

Does it really make sense to be printing option in decimal rather than hex here?

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20161028/fcdc21e6/attachment.sig>
author
#7 lore raw

PATCH 1/2 arm: Set TTB XN bit in case DCACHE_OFF for LPAE mode

Quoted 37 lines · Tom Rini
Quoted 36 lines · Keerthy

While we setup the mmu initially we mark set_section_dcache with DCACHE_OFF flag. In case of non-LPAE mode the DCACHE_OFF macro is rightly defined with TTB_SECT_XN_MASK set so as to mark all the 4GB XN. In case of LPAE mode XN(Execute-never) bit is not set with DCACHE_OFF. Hence XN bit is not set by default for DCACHE_OFF which keeps all the regions execute okay and this leads to random speculative fetches in random memory regions which was eventually caught by kernel omap-l3-noc driver.

Fix this to mark the regions as XN by default.

---
 arch/arm/include/asm/system.h | 2 +-
 arch/arm/lib/cache-cp15.c     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
arch/arm/include/asm/system.h +1 -1
index b928bd8..2f430ad 100644
@@ -329,7 +329,7 @@ static inline void set_dacr(unsigned int val)
 
 /* options available for data cache on each page */
 enum dcache_option {
-	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0),
+	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0) | TTB_SECT_XN_MASK,
 	DCACHE_WRITETHROUGH = TTB_SECT | TTB_SECT_MAIR(1),
 	DCACHE_WRITEBACK = TTB_SECT | TTB_SECT_MAIR(2),
 	DCACHE_WRITEALLOC = TTB_SECT | TTB_SECT_MAIR(3),
arch/arm/lib/cache-cp15.c +0 -0
index 70e94f0..4d9903e 100644
@@ -71,8 +71,13 @@ void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
 
Quoted 12 lines

arch/arm/lib/cache-cp15.c void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size, · line 80

Quoted 9 lines
arch/arm/lib/cache-cp15.c +5 -0
 	end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
 	start = start >> MMU_SECTION_SHIFT;
+#ifdef CONFIG_ARMV7_LPAE
+	debug("%s: start=%pa, size=%zu, option=%llu\n", __func__, &start, size,
+	      option);
+#else
 	debug("%s: start=%pa, size=%zu, option=%d\n", __func__, &start, size,
 	      option);
+#endif

Does it really make sense to be printing option in decimal rather than hex here?

Okay. I will change that to hex and re-post.

Quoted 1 line