Skip to contentSkip to conversation

riscv: Add HAVE_IOREMAP_PROT support

PATCH In review [email protected] 2 participants 3 messages Jul 16, 2023 – Jul 22, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Add HAVE_IOREMAP_PROT support

From: Guo Ren <[email protected]>

Add pte_pgprot macro, then riscv could have HAVE_IOREMAP_PROT, which will enable generic_access_phys() code, it is useful for debug, eg, gdb.

Because generic_access_phys() would call ioremap_prot()-> pgprot_nx() to disable excutable attribute, add definition of pgprot_nx() for riscv.

---
 .../features/vm/ioremap_prot/arch-support.txt     |  2 +-
 arch/riscv/Kconfig                                |  1 +
 arch/riscv/include/asm/pgtable.h                  | 15 +++++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
Documentation/features/vm/ioremap_prot/arch-support.txt +1 -1
index a24149e59d73..ea8c8a361455 100644
@@ -21,7 +21,7 @@
     |    openrisc: | TODO |
     |      parisc: | TODO |
     |     powerpc: |  ok  |
-    |       riscv: | TODO |
+    |       riscv: |  ok  |
     |        s390: |  ok  |
     |          sh: |  ok  |
     |       sparc: | TODO |
arch/riscv/Kconfig +1 -0
index 4c07b9189c86..15900fa20797 100644
@@ -117,6 +117,7 @@ config RISCV
 	select HAVE_FUNCTION_ERROR_INJECTION
 	select HAVE_GCC_PLUGINS
 	select HAVE_GENERIC_VDSO if MMU && 64BIT
+	select HAVE_IOREMAP_PROT
 	select HAVE_IRQ_TIME_ACCOUNTING
 	select HAVE_KPROBES if !XIP_KERNEL
 	select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL
arch/riscv/include/asm/pgtable.h +15 -0
index 75970ee2bda2..c9552a161f90 100644
@@ -415,6 +415,11 @@ static inline pte_t pte_mkhuge(pte_t pte)
 	return pte;
 }
 
+static inline pgprot_t pte_pgprot(pte_t pte)
+{
+	return __pgprot(pte_val(pte) & ~_PAGE_PFN_MASK);
+}
+
 #ifdef CONFIG_NUMA_BALANCING
 /*
  * See the comment in include/asm-generic/pgtable.h
@@ -573,6 +578,16 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
 	return ptep_test_and_clear_young(vma, address, ptep);
 }
 
+#define pgprot_nx pgprot_nx
+static inline pgprot_t pgprot_nx(pgprot_t _prot)
+{
+	unsigned long prot = pgprot_val(_prot);
+
+	prot &= ~_PAGE_EXEC;
+
+	return __pgprot(prot);
+}
+
 #define pgprot_noncached pgprot_noncached
 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
 {
Signature · 1 line
2.36.1
#1 lore raw

Hi Guo,

Quoted 5 lines · On 16/07/2023 17:20, [email protected] wrote:

From: Guo Ren <[email protected]>

Add pte_pgprot macro, then riscv could have HAVE_IOREMAP_PROT, which will enable generic_access_phys() code, it is useful for debug, eg, gdb.

I don't understand, we already have the generic ioremap_prot() implementation since we select GENERIC_IOREMAP: shouldn't HAVE_IOREMAP_PROT imply that we have our own implementation?

Thanks,

Alex

Quoted 70 lines

Because generic_access_phys() would call ioremap_prot()-> pgprot_nx() to disable excutable attribute, add definition of pgprot_nx() for riscv.

---
  .../features/vm/ioremap_prot/arch-support.txt     |  2 +-
  arch/riscv/Kconfig                                |  1 +
  arch/riscv/include/asm/pgtable.h                  | 15 +++++++++++++++
  3 files changed, 17 insertions(+), 1 deletion(-)
Documentation/features/vm/ioremap_prot/arch-support.txt +1 -1
index a24149e59d73..ea8c8a361455 100644
@@ -21,7 +21,7 @@
      |    openrisc: | TODO |
      |      parisc: | TODO |
      |     powerpc: |  ok  |
-    |       riscv: | TODO |
+    |       riscv: |  ok  |
      |        s390: |  ok  |
      |          sh: |  ok  |
      |       sparc: | TODO |
arch/riscv/Kconfig +1 -0
index 4c07b9189c86..15900fa20797 100644
@@ -117,6 +117,7 @@ config RISCV
  	select HAVE_FUNCTION_ERROR_INJECTION
  	select HAVE_GCC_PLUGINS
  	select HAVE_GENERIC_VDSO if MMU && 64BIT
+	select HAVE_IOREMAP_PROT
  	select HAVE_IRQ_TIME_ACCOUNTING
  	select HAVE_KPROBES if !XIP_KERNEL
  	select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL
arch/riscv/include/asm/pgtable.h +15 -0
index 75970ee2bda2..c9552a161f90 100644
@@ -415,6 +415,11 @@ static inline pte_t pte_mkhuge(pte_t pte)
  	return pte;
  }
 
+static inline pgprot_t pte_pgprot(pte_t pte)
+{
+	return __pgprot(pte_val(pte) & ~_PAGE_PFN_MASK);
+}
+
  #ifdef CONFIG_NUMA_BALANCING
  /*
   * See the comment in include/asm-generic/pgtable.h
@@ -573,6 +578,16 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  	return ptep_test_and_clear_young(vma, address, ptep);
  }
 
+#define pgprot_nx pgprot_nx
+static inline pgprot_t pgprot_nx(pgprot_t _prot)
+{
+	unsigned long prot = pgprot_val(_prot);
+
+	prot &= ~_PAGE_EXEC;
+
+	return __pgprot(prot);
+}
+
  #define pgprot_noncached pgprot_noncached
  static inline pgprot_t pgprot_noncached(pgprot_t _prot)
  {
author
#2 lore raw
Quoted 6 lines · Alexandre Ghiti

Hi Guo,

Quoted 2 lines · On 16/07/2023 17:20, [email protected] wrote:

From: Guo Ren <[email protected]>

Quoted 8 lines
Quoted 3 lines

Add pte_pgprot macro, then riscv could have HAVE_IOREMAP_PROT, which will enable generic_access_phys() code, it is useful for debug, eg, gdb.

I don't understand, we already have the generic ioremap_prot() implementation since we select GENERIC_IOREMAP: shouldn't HAVE_IOREMAP_PROT imply that we have our own implementation?

They are different! See arch/arm64/Kconfig, which selects all of them.

HAVE_IOREMAP_PROT would enable your drivers/char/mem.c generic_access_phys of mmap_mem_ops.

It's a small function enabling.

Quoted 76 lines

Thanks,

Alex

Quoted 70 lines

Because generic_access_phys() would call ioremap_prot()-> pgprot_nx() to disable excutable attribute, add definition of pgprot_nx() for riscv.

---
  .../features/vm/ioremap_prot/arch-support.txt     |  2 +-
  arch/riscv/Kconfig                                |  1 +
  arch/riscv/include/asm/pgtable.h                  | 15 +++++++++++++++
  3 files changed, 17 insertions(+), 1 deletion(-)
Documentation/features/vm/ioremap_prot/arch-support.txt +1 -1
index a24149e59d73..ea8c8a361455 100644
@@ -21,7 +21,7 @@
      |    openrisc: | TODO |
      |      parisc: | TODO |
      |     powerpc: |  ok  |
-    |       riscv: | TODO |
+    |       riscv: |  ok  |
      |        s390: |  ok  |
      |          sh: |  ok  |
      |       sparc: | TODO |
arch/riscv/Kconfig +1 -0
index 4c07b9189c86..15900fa20797 100644
@@ -117,6 +117,7 @@ config RISCV
      select HAVE_FUNCTION_ERROR_INJECTION
      select HAVE_GCC_PLUGINS
      select HAVE_GENERIC_VDSO if MMU && 64BIT
+     select HAVE_IOREMAP_PROT
      select HAVE_IRQ_TIME_ACCOUNTING
      select HAVE_KPROBES if !XIP_KERNEL
      select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL
arch/riscv/include/asm/pgtable.h +15 -0
index 75970ee2bda2..c9552a161f90 100644
@@ -415,6 +415,11 @@ static inline pte_t pte_mkhuge(pte_t pte)
      return pte;
  }
 
+static inline pgprot_t pte_pgprot(pte_t pte)
+{
+     return __pgprot(pte_val(pte) & ~_PAGE_PFN_MASK);
+}
+
  #ifdef CONFIG_NUMA_BALANCING
  /*
   * See the comment in include/asm-generic/pgtable.h
@@ -573,6 +578,16 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
      return ptep_test_and_clear_young(vma, address, ptep);
  }
 
+#define pgprot_nx pgprot_nx
+static inline pgprot_t pgprot_nx(pgprot_t _prot)
+{
+     unsigned long prot = pgprot_val(_prot);
+
+     prot &= ~_PAGE_EXEC;
+
+     return __pgprot(prot);
+}
+
  #define pgprot_noncached pgprot_noncached
  static inline pgprot_t pgprot_noncached(pgprot_t _prot)
  {
Signature · 2 lines
Best Regards
 Guo Ren