From patchwork Mon Apr 15 14:38:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10900933 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 19CEB922 for ; Mon, 15 Apr 2019 14:38:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E6DAE28639 for ; Mon, 15 Apr 2019 14:38:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E4C2B286F6; Mon, 15 Apr 2019 14:38:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4A31C28644 for ; Mon, 15 Apr 2019 14:38:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:To:From :Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=IouLCqDFNg9ryBNA8tB+vAW4vpJs0gOxYzmTo9gg2PQ=; b=K1A/h4aYFBwitY BhuPFnJZlrHFdTHCIPQITy9xiV2Ubd/Mwl4FuQGoABIAVgPjc4KiDLXrQ7ikKBYMPIIBKCvELoFQl 2WdjH9lrVrI26SFAnP/ih6g2I8Z5iLseh4tAYInAK9lssYQ6roLaIuFy0B31ysKjlUR1IZq24JXrs M7f6o4y148zhKfeIT926dnav17lMOCR9cwlSq5gvcD9LlkVXwEoi9qwoRkFT24LHlB6GieSbbinPp yVfSlhUJUsg3eY9HortFS6iLzFfL+qAX4uN7RCxv7F95nZHgtNPBE7bZJW2VEJBMVOOXkmVINoIvc U3691wFDlwtTuDsdI2zQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hG2kf-0003Zx-Kv; Mon, 15 Apr 2019 14:38:29 +0000 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hG2kb-0003T1-KV for linux-arm-kernel@lists.infradead.org; Mon, 15 Apr 2019 14:38:27 +0000 Received: by mail-pg1-x544.google.com with SMTP id v12so8692568pgq.1 for ; Mon, 15 Apr 2019 07:38:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=AIqQfazSNqgaJokAcdDro2dFfhikFIqjWxYw4YigiZI=; b=SuH47miCIm32LQ4+GVx4b0RiF3W5u3IsWgCSZFT/vLpwrxdUSzD7Z8sXmj3u8eoZkV C/N1vWmbFJ2652FjmouJCx3L8vVnAaFCgpUXvlBF5Vpmj0Xg+F6XcVZ7cSDfzY3Nim1N K+T/cXrHWKGh7zfMTszsZnctfciUvAIxz25Do= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=AIqQfazSNqgaJokAcdDro2dFfhikFIqjWxYw4YigiZI=; b=FN0dKX9UjwrTRcgSHfpNNo5tT17BtI3P4u+Zuec7J0AKUsCLl9qc+033XNfsNMvLBZ 7zObIRF+TUu6ZsylJZEDsYiPPJHMlPqCsV4Ck4QPxI0Kkvl/hKswg2HgjLo6cyeovVmX 7dppsDcX0qC8gMzITxfOnbH5zk2vndNVq+8xC2T2NoiWiaEzV7JL7JtAfry83GkVKW7f K0wj78S6sv6x8OQkiClKsCATTVMOHvviJV+CwQzfbHghhzmRXPEyhBmj5vPBIrCpgg/n K/M/Ot1vZpD8g8MZ3LNDFEH47sBS0I4+u9GRxcd7pVKIKSnSFjJSE+noh21ngZmbm+Ig UWzg== X-Gm-Message-State: APjAAAWxZkiX0bPmeD4Vxkt9BLlvF9XmzCtOk/A5W9p7t9PmYBzmMJy8 XB3e1ARIZ+hiS2XClI5fT7ogNQ== X-Google-Smtp-Source: APXvYqzo7u9m2mzQ8NhV5uxXsUmlvdosDxd6nNMGpGnObpCOhMT2HWcSTK+0JYqLJ0/1DnuGEKDeXA== X-Received: by 2002:a62:e215:: with SMTP id a21mr74546155pfi.30.1555339104341; Mon, 15 Apr 2019 07:38:24 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id q128sm79883144pga.60.2019.04.15.07.38.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Apr 2019 07:38:23 -0700 (PDT) Date: Mon, 15 Apr 2019 07:38:21 -0700 From: Kees Cook To: Catalin Marinas Subject: [PATCH v3] arm64: sysreg: make mrs_s and msr_s macros work with Clang and LTO Message-ID: <20190415143821.GA46880@beast> MIME-Version: 1.0 Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190415_073825_699254_2B0473D1 X-CRM114-Status: GOOD ( 15.00 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel , Will Deacon , Nick Desaulniers , linux-kernel@vger.kernel.org, Yury Norov , Sami Tolvanen , Alex Matveev , Matthias Kaehlcke , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alex Matveev Clang's integrated assembler does not allow assembly macros defined in one inline asm block using the .macro directive to be used across separate asm blocks. LLVM developers consider this a feature and not a bug, recommending code refactoring: https://bugs.llvm.org/show_bug.cgi?id=19749 As binutils doesn't allow macros to be redefined, this change uses UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros in-place and workaround gcc and clang limitations on redefining macros across different assembler blocks. Specifically, the current state after preprocessing looks like this: asm volatile(".macro mXX_s ... .endm"); void f() { asm volatile("mXX_s a, b"); } With GCC, it gives macro redefinition error because sysreg.h is included in multiple source files, and assembler code for all of them is later combined for LTO (I've seen an intermediate file with hundreds of identical definitions). With clang, it gives macro undefined error because clang doesn't allow sharing macros between inline asm statements. I also seem to remember catching another sort of undefined error with GCC due to reordering of macro definition asm statement and generated asm code for function that uses the macro. The solution with defining and undefining for each use, while certainly not elegant, satisfies both GCC and clang, LTO and non-LTO. Signed-off-by: Alex Matveev Signed-off-by: Yury Norov Signed-off-by: Sami Tolvanen Signed-off-by: Kees Cook --- v3: split out patch as stand-alone, added more uses in irqflags, updated commit log, based on discussion in https://lore.kernel.org/patchwork/patch/851580/ --- arch/arm64/include/asm/irqflags.h | 12 +++++-- arch/arm64/include/asm/kvm_hyp.h | 8 +++-- arch/arm64/include/asm/sysreg.h | 55 +++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/arch/arm64/include/asm/irqflags.h b/arch/arm64/include/asm/irqflags.h index 43d8366c1e87..06d3987d1546 100644 --- a/arch/arm64/include/asm/irqflags.h +++ b/arch/arm64/include/asm/irqflags.h @@ -43,7 +43,9 @@ static inline void arch_local_irq_enable(void) asm volatile(ALTERNATIVE( "msr daifclr, #2 // arch_local_irq_enable\n" "nop", + DEFINE_MSR_S "msr_s " __stringify(SYS_ICC_PMR_EL1) ",%0\n" + UNDEFINE_MSR_S "dsb sy", ARM64_HAS_IRQ_PRIO_MASKING) : @@ -55,7 +57,9 @@ static inline void arch_local_irq_disable(void) { asm volatile(ALTERNATIVE( "msr daifset, #2 // arch_local_irq_disable", - "msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0", + DEFINE_MSR_S + "msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0\n" + UNDEFINE_MSR_S, ARM64_HAS_IRQ_PRIO_MASKING) : : "r" ((unsigned long) GIC_PRIO_IRQOFF) @@ -85,8 +89,10 @@ static inline unsigned long arch_local_save_flags(void) asm volatile(ALTERNATIVE( "mov %0, %1\n" "nop\n" - "nop", + "nop\n", + DEFINE_MRS_S "mrs_s %0, " __stringify(SYS_ICC_PMR_EL1) "\n" + UNDEFINE_MRS_S "ands %1, %1, " __stringify(PSR_I_BIT) "\n" "csel %0, %0, %2, eq", ARM64_HAS_IRQ_PRIO_MASKING) @@ -116,7 +122,9 @@ static inline void arch_local_irq_restore(unsigned long flags) asm volatile(ALTERNATIVE( "msr daif, %0\n" "nop", + DEFINE_MSR_S "msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0\n" + UNDEFINE_MSR_S "dsb sy", ARM64_HAS_IRQ_PRIO_MASKING) : "+r" (flags) diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h index 4da765f2cca5..9d0e7f546240 100644 --- a/arch/arm64/include/asm/kvm_hyp.h +++ b/arch/arm64/include/asm/kvm_hyp.h @@ -30,7 +30,9 @@ ({ \ u64 reg; \ asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\ - "mrs_s %0, " __stringify(r##vh),\ + DEFINE_MRS_S \ + "mrs_s %0, " __stringify(r##vh) "\n"\ + UNDEFINE_MRS_S, \ ARM64_HAS_VIRT_HOST_EXTN) \ : "=r" (reg)); \ reg; \ @@ -40,7 +42,9 @@ do { \ u64 __val = (u64)(v); \ asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\ - "msr_s " __stringify(r##vh) ", %x0",\ + DEFINE_MSR_S \ + "msr_s " __stringify(r##vh) ", %x0\n"\ + UNDEFINE_MSR_S, \ ARM64_HAS_VIRT_HOST_EXTN) \ : : "rZ" (__val)); \ } while (0) diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h index 5b267dec6194..e9f64c237a7c 100644 --- a/arch/arm64/include/asm/sysreg.h +++ b/arch/arm64/include/asm/sysreg.h @@ -746,20 +746,39 @@ #include #include -asm( -" .irp num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n" -" .equ .L__reg_num_x\\num, \\num\n" -" .endr\n" +#define __DEFINE_MRS_MSR_S_REGNUM \ +" .irp num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n" \ +" .equ .L__reg_num_x\\num, \\num\n" \ +" .endr\n" \ " .equ .L__reg_num_xzr, 31\n" -"\n" -" .macro mrs_s, rt, sreg\n" - __emit_inst(0xd5200000|(\\sreg)|(.L__reg_num_\\rt)) + +#define DEFINE_MRS_S \ + __DEFINE_MRS_MSR_S_REGNUM \ +" .macro mrs_s, rt, sreg\n" \ + __emit_inst(0xd5200000|(\\sreg)|(.L__reg_num_\\rt)) \ " .endm\n" -"\n" -" .macro msr_s, sreg, rt\n" - __emit_inst(0xd5000000|(\\sreg)|(.L__reg_num_\\rt)) + +#define DEFINE_MSR_S \ + __DEFINE_MRS_MSR_S_REGNUM \ +" .macro msr_s, sreg, rt\n" \ + __emit_inst(0xd5000000|(\\sreg)|(.L__reg_num_\\rt)) \ " .endm\n" -); + +#define UNDEFINE_MRS_S \ +" .purgem mrs_s\n" + +#define UNDEFINE_MSR_S \ +" .purgem msr_s\n" + +#define __mrs_s(r, v) \ + DEFINE_MRS_S \ +" mrs_s %0, " __stringify(r) "\n" \ + UNDEFINE_MRS_S : "=r" (v) + +#define __msr_s(r, v) \ + DEFINE_MSR_S \ +" msr_s " __stringify(r) ", %x0\n" \ + UNDEFINE_MSR_S : : "rZ" (v) /* * Unlike read_cpuid, calls to read_sysreg are never expected to be @@ -785,15 +804,15 @@ asm( * For registers without architectural names, or simply unsupported by * GAS. */ -#define read_sysreg_s(r) ({ \ - u64 __val; \ - asm volatile("mrs_s %0, " __stringify(r) : "=r" (__val)); \ - __val; \ +#define read_sysreg_s(r) ({ \ + u64 __val; \ + asm volatile(__mrs_s(r, __val)); \ + __val; \ }) -#define write_sysreg_s(v, r) do { \ - u64 __val = (u64)(v); \ - asm volatile("msr_s " __stringify(r) ", %x0" : : "rZ" (__val)); \ +#define write_sysreg_s(v, r) do { \ + u64 __val = (u64)(v); \ + asm volatile(__msr_s(r, __val)); \ } while (0) /*