[PATCH v9 19/51] powerpc: Handle exceptions caused by pkey violation

From: Ram Pai
Date: Mon Nov 06 2017 - 03:59:22 EST


Handle Data and Instruction exceptions caused by memory
protection-key.

The CPU will detect the key fault if the HPTE is already
programmed with the key.

However if the HPTE is not hashed, a key fault will not
be detected by the hardware. The software will detect
pkey violation in such a case.

Signed-off-by: Ram Pai <linuxram@xxxxxxxxxx>
---
arch/powerpc/mm/fault.c | 32 +++++++++++++++++++++++++++-----
1 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c
index 4797d08..dfcd0e4 100644
--- a/arch/powerpc/mm/fault.c
+++ b/arch/powerpc/mm/fault.c
@@ -145,6 +145,24 @@ static noinline int bad_area(struct pt_regs *regs, unsigned long address)
return __bad_area(regs, address, SEGV_MAPERR);
}

+static int bad_page_fault_exception(struct pt_regs *regs, unsigned long address,
+ int si_code)
+{
+ int sig = SIGBUS;
+ int code = BUS_OBJERR;
+
+#ifdef CONFIG_PPC_MEM_KEYS
+ if (si_code & DSISR_KEYFAULT) {
+ perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
+ sig = SIGSEGV;
+ code = SEGV_PKUERR;
+ }
+#endif /* CONFIG_PPC_MEM_KEYS */
+
+ _exception(sig, regs, code, address);
+ return 0;
+}
+
static int do_sigbus(struct pt_regs *regs, unsigned long address,
unsigned int fault)
{
@@ -391,11 +409,9 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,
return 0;

if (unlikely(page_fault_is_bad(error_code))) {
- if (is_user) {
- _exception(SIGBUS, regs, BUS_OBJERR, address);
- return 0;
- }
- return SIGBUS;
+ if (!is_user)
+ return SIGBUS;
+ return bad_page_fault_exception(regs, address, error_code);
}

/* Additional sanity check(s) */
@@ -498,6 +514,12 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,
* the fault.
*/
fault = handle_mm_fault(vma, address, flags);
+
+#ifdef CONFIG_PPC_MEM_KEYS
+ if (unlikely(fault & VM_FAULT_SIGSEGV))
+ return __bad_area(regs, address, SEGV_PKUERR);
+#endif /* CONFIG_PPC_MEM_KEYS */
+
major |= fault & VM_FAULT_MAJOR;

/*
--
1.7.1