Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp3585333imb; Tue, 5 Mar 2019 13:21:27 -0800 (PST) X-Google-Smtp-Source: APXvYqxvBEtICSd9FBsKzn4yS2hhMuuXkQGVr/5d5dXN1HDHvzt/B3Mp3uIVYRZpq+lVJW/vngzE X-Received: by 2002:a17:902:f096:: with SMTP id go22mr3191040plb.172.1551820887531; Tue, 05 Mar 2019 13:21:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551820887; cv=none; d=google.com; s=arc-20160816; b=xyjcA3mL2Qv4dlr+jC3SSjIExWBMD0OdwaG3dmZWvpeyGb3jOSZLKHCJf5otdy6VUk duJXxrKi4UvSaPKn8fVLC5JqdiJq2s/9dr38C3bj5ux0KCT6S+xsJ2B/JCR9AoolX6hs TX9HAsInSILhvKdPIwRfYJQXzKbIoSE3m8fAUiaQ6fgbayLRo0Gz3PpAUJXY/WzsACWX f/9btCHPUSVovOru5X+TV402Jrn3T2HbeueyTFpe24qJYVRoudTtTj9N6XcY3VWlLwyl CXg+J+ppNmhw25dLVi21N774eO+Baj1LLGCJIpQh5jJvNoQxIZt80wc9+grGg/Qw7/p5 tmlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id:dkim-signature; bh=Mktwf7OhG+phM8DeCSqyt4UzKMJEmTkYVGjGez7vSAw=; b=nX26p8dM44ndKs1Yia5xKhSOis7E6gL/bf02Ue3r8aNjeNlAMsQ0MTFW/AnuB/4jjk XatgazlJi08/WiWqafS3AnKY2+n65oMvHE51GMmU5n88d2vbN3E78b5hLr5RCZz7KJlQ dff1wdLNk5Ar3RjHYNNBf2LNtGz8M8EbaVOCP6Cx3iNSFolN4tnJsnaVEIIz6TY3vwam s59+EHXY59tuhtjf5Cn9j7qA0YZ4+jAONo5UQwW7Z7zFXlnyeIt1bag5Gb1vb6KNVnru f3WDr/86Fj9IqPopVzNXvfvYztp3zCq1pnfu94S9seWCe75juU/sZIn6w6NIYfdtrFii rD8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=Z7M0kvGW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r203si8649609pgr.517.2019.03.05.13.21.12; Tue, 05 Mar 2019 13:21:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=Z7M0kvGW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728823AbfCEVSW (ORCPT + 99 others); Tue, 5 Mar 2019 16:18:22 -0500 Received: from pegase1.c-s.fr ([93.17.236.30]:27363 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728789AbfCEVSU (ORCPT ); Tue, 5 Mar 2019 16:18:20 -0500 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 44DV9k2wBRz9v6HM; Tue, 5 Mar 2019 22:18:18 +0100 (CET) Authentication-Results: localhost; dkim=pass reason="1024-bit key; insecure key" header.d=c-s.fr header.i=@c-s.fr header.b=Z7M0kvGW; dkim-adsp=pass; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id N0LAE-lh2hb5; Tue, 5 Mar 2019 22:18:18 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 44DV9k1qyrz9v6LR; Tue, 5 Mar 2019 22:18:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1551820698; bh=Mktwf7OhG+phM8DeCSqyt4UzKMJEmTkYVGjGez7vSAw=; h=In-Reply-To:References:From:Subject:To:Cc:Date:From; b=Z7M0kvGWWw2m5PXfE0QYSLmr7hRiom1kNKveHCJ9AXQut+7DdRS4n5pCLukgLqj3O /8Ys3hDAI1b8ATGovjj8KI53U/fEDaOj5NWX/VGutpDLGn9w+lGVeto5+uo5rnTj1/ Ew3LsKFJPn8KU6vrYpDzGAWOp7KHY7McoV0UUcds= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 68C958B860; Tue, 5 Mar 2019 22:18:18 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id HV8J2urAZzvE; Tue, 5 Mar 2019 22:18:18 +0100 (CET) Received: from po16846vm.idsi0.si.c-s.fr (unknown [192.168.4.90]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 247CA8B83A; Tue, 5 Mar 2019 22:18:18 +0100 (CET) Received: by localhost.localdomain (Postfix, from userid 0) id D81436F8CD; Tue, 5 Mar 2019 21:18:17 +0000 (UTC) Message-Id: In-Reply-To: References: From: Christophe Leroy Subject: [PATCH 4/7] powerpc/32: Prepare for Kernel Userspace Access Protection To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , ruscur@russell.cc Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Date: Tue, 5 Mar 2019 21:18:17 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds ASM macros for saving, restoring and checking the KUAP state, and modifies setup_32 to call them on exceptions from kernel. The macros are defined as empty by default for when CONFIG_PPC_KUAP is not selected and/or for platforms which don't handle (yet) KUAP. Signed-off-by: Christophe Leroy --- arch/powerpc/include/asm/kup.h | 15 ++++++++++++++- arch/powerpc/kernel/entry_32.S | 16 ++++++++++++---- arch/powerpc/platforms/Kconfig.cputype | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h index 082f50ad41a7..51a55be70cbc 100644 --- a/arch/powerpc/include/asm/kup.h +++ b/arch/powerpc/include/asm/kup.h @@ -6,7 +6,20 @@ #include #endif -#ifndef __ASSEMBLY__ +#ifdef __ASSEMBLY__ +#ifndef CONFIG_PPC_KUAP +.macro kuap_save_and_lock sp, thread, gpr1, gpr2, gpr3 +.endm + +.macro kuap_restore sp, current, gpr1, gpr2, gpr3 +.endm + +.macro kuap_check current, gpr +.endm + +#endif + +#else /* !__ASSEMBLY__ */ #include diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S index b61cfd29c76f..7b7752d6ab2f 100644 --- a/arch/powerpc/kernel/entry_32.S +++ b/arch/powerpc/kernel/entry_32.S @@ -36,6 +36,7 @@ #include #include #include +#include /* * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE. @@ -150,8 +151,8 @@ transfer_to_handler: stw r12,_CTR(r11) stw r2,_XER(r11) mfspr r12,SPRN_SPRG_THREAD - addi r2,r12,-THREAD beq 2f /* if from user, fix up THREAD.regs */ + addi r2, r12, -THREAD addi r11,r1,STACK_FRAME_OVERHEAD stw r11,PT_REGS(r12) #if defined(CONFIG_40x) || defined(CONFIG_BOOKE) @@ -186,6 +187,8 @@ transfer_to_handler: 2: /* if from kernel, check interrupted DOZE/NAP mode and * check for stack overflow */ + kuap_save_and_lock r11, r12, r9, r2, r0 + addi r2, r12, -THREAD lwz r9,KSP_LIMIT(r12) cmplw r1,r9 /* if r1 <= ksp_limit */ ble- stack_ovf /* then the kernel stack overflowed */ @@ -272,6 +275,7 @@ reenable_mmu: /* re-enable mmu so we can */ lwz r9,_MSR(r11) /* if sleeping, clear MSR.EE */ rlwinm r9,r9,0,~MSR_EE lwz r12,_LINK(r11) /* and return to address in LR */ + kuap_restore r11, r2, r3, r4, r5 b fast_exception_return #endif @@ -428,6 +432,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX) ACCOUNT_CPU_USER_EXIT(r2, r5, r7) 3: #endif + kuap_check r2, r4 lwz r4,_LINK(r1) lwz r5,_CCR(r1) mtlr r4 @@ -678,6 +683,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_SPE) stw r10,_CCR(r1) stw r1,KSP(r3) /* Set old stack pointer */ + kuap_check r2, r4 #ifdef CONFIG_SMP /* We need a sync somewhere here to make sure that if the * previous task gets rescheduled on another CPU, it sees all @@ -866,12 +872,12 @@ resume_kernel: /* check current_thread_info->preempt_count */ lwz r0,TI_PREEMPT(r2) cmpwi 0,r0,0 /* if non-zero, just restore regs and return */ - bne restore + bne restore_kuap andi. r8,r8,_TIF_NEED_RESCHED - beq+ restore + beq+ restore_kuap lwz r3,_MSR(r1) andi. r0,r3,MSR_EE /* interrupts off? */ - beq restore /* don't schedule if so */ + beq restore_kuap /* don't schedule if so */ #ifdef CONFIG_TRACE_IRQFLAGS /* Lockdep thinks irqs are enabled, we need to call * preempt_schedule_irq with IRQs off, so we inform lockdep @@ -890,6 +896,8 @@ resume_kernel: bl trace_hardirqs_on #endif #endif /* CONFIG_PREEMPT */ +restore_kuap: + kuap_restore r1, r2, r9, r10, r0 /* interrupts are hard-disabled at this point */ restore: diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype index 08966e3127bb..00fa0d110dcb 100644 --- a/arch/powerpc/platforms/Kconfig.cputype +++ b/arch/powerpc/platforms/Kconfig.cputype @@ -374,7 +374,7 @@ config PPC_KUAP config PPC_KUAP_DEBUG bool "Extra debugging for Kernel Userspace Access Protection" - depends on PPC_HAVE_KUAP && PPC_RADIX_MMU + depends on PPC_HAVE_KUAP && (PPC_RADIX_MMU || PPC_32) help Add extra debugging for Kernel Userspace Access Protection (KUAP) If you're unsure, say N. -- 2.13.3