Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp342464imm; Tue, 7 Aug 2018 20:27:46 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwWNNRdcKr4yV2ffHVz6mogq3G1lQsBSRIW6ajhQpg67JPoxyL32lhPkaWWHvIkXNWiwHav X-Received: by 2002:a17:902:7884:: with SMTP id q4-v6mr899573pll.174.1533698866774; Tue, 07 Aug 2018 20:27:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533698866; cv=none; d=google.com; s=arc-20160816; b=DKi/F7THTg5qjbaeDcB2LCrXhjXQVdFBkXj7YvpSEWC2yCZSmGDckedzLFW/gxVuzu FRascEQ6Rm6d53HCS52g1oVgSpmTPVnnKaR0A6mRsJBvptQjmRSWs1i9cVvMzUl0ECKF O0MO6fHFYX6BY3t7zvOaf5Oubo9IUjQA+HMf0tEJwBAGU6bgpzalpjHWZCQ9XceUn1Td VomJeo7bTJlRFO0AaMWI+swz7qvA2S9k377ebjOBbqMBixEZA0zdqsObATD11MqFSemg RJ631xpf49ti7qF7VXFagFinn3ZtlAE7GfMjetrcJnW8D7uG6eCmm4csLFLauTfsrvEE yxGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=bPCGEnqS4DhdytJMEtlQP5+XSfJQEcCzoLY9oZLo/f8=; b=f0y3F/P4N/gu4meF9HgqDfCj1fhNsu6geJGWK1hrztYG/HiM281dIFDWygO5OXf9sm z6yElrsvGo1gbE4UMTDmWxD6LSwdJrV7XnIfFwHWqWsWoZDalUETHS++Ms22TsKggU6m J6MQfay7/Yru9IGJiEMfp0/iCeRWszIYxyY/poATL9TVWGaXRY/Saiy0aIV+x7T5qEkj WKE1UHR9GgYO5yNx3KKqrDCcOkpuZAJIT1/aUnaK/33goINSia1VOCRxtlB2EIqOrat3 PEde6MORqsEwoV10w19d6R8aCfOSU5rdK+CFGMUNsQkJNERhQwXhJUhhAbr2APjYmZoG T/6g== ARC-Authentication-Results: i=1; mx.google.com; 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 n8-v6si2413487plk.255.2018.08.07.20.27.32; Tue, 07 Aug 2018 20:27:46 -0700 (PDT) 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; 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 S1727139AbeHHFm6 (ORCPT + 99 others); Wed, 8 Aug 2018 01:42:58 -0400 Received: from exmail.andestech.com ([59.124.169.137]:21899 "EHLO ATCSQR.andestech.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726258AbeHHFm6 (ORCPT ); Wed, 8 Aug 2018 01:42:58 -0400 Received: from mail.andestech.com (atcpcs16.andestech.com [10.0.1.222]) by ATCSQR.andestech.com with ESMTP id w783OTNV083368; Wed, 8 Aug 2018 11:24:29 +0800 (GMT-8) (envelope-from alankao@andestech.com) Received: from atcsqa06.andestech.com (10.0.1.85) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.123.3; Wed, 8 Aug 2018 11:25:19 +0800 From: Alan Kao To: , , "Palmer Dabbelt" , Albert Ou , Christoph Hellwig , Andrew Waterman , Arnd Bergmann , Darius Rad CC: Alan Kao , Greentime Hu , Vincent Chen , Zong Li , Nick Hu Subject: [PATCH v4 5/5] Auto-detect whether a FPU exists Date: Wed, 8 Aug 2018 11:24:45 +0800 Message-ID: <1533698685-18022-6-git-send-email-alankao@andestech.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1533698685-18022-1-git-send-email-alankao@andestech.com> References: <1533698685-18022-1-git-send-email-alankao@andestech.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.0.1.85] X-DNSRBL: X-MAIL: ATCSQR.andestech.com w783OTNV083368 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We expect that a kernel with CONFIG_FPU=y can still support no-FPU machines. To do so, the kernel should first examine the existence of a FPU, then do nothing if a FPU does exist; otherwise, it should disable/bypass all FPU-related functions. In this patch, a new global variable, no_fpu, is created and determined when parsing the hardware capability from device tree during booting. This variable is used in those FPU-related functions. Signed-off-by: Alan Kao Cc: Greentime Hu Cc: Vincent Chen Cc: Zong Li Cc: Nick Hu --- arch/riscv/include/asm/hwcap.h | 3 +++ arch/riscv/include/asm/switch_to.h | 13 ++++++++++++- arch/riscv/kernel/cpufeature.c | 11 +++++++++++ arch/riscv/kernel/signal.c | 6 ++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 8a4ed7bbcbea..1b870086a869 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -33,5 +33,8 @@ enum { }; extern unsigned long elf_hwcap; +#ifdef CONFIG_FPU +extern bool no_fpu; +#endif #endif #endif diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 093050b03543..7278e3eb7a70 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef CONFIG_FPU extern void __fstate_save(struct task_struct *save_to); @@ -30,6 +31,9 @@ static inline void __fstate_clean(struct pt_regs *regs) static inline void fstate_save(struct task_struct *task, struct pt_regs *regs) { + if (unlikely(no_fpu)) + return; + if ((regs->sstatus & SR_FS) == SR_FS_DIRTY) { __fstate_save(task); __fstate_clean(regs); @@ -39,6 +43,9 @@ static inline void fstate_save(struct task_struct *task, static inline void fstate_restore(struct task_struct *task, struct pt_regs *regs) { + if (unlikely(no_fpu)) + return; + if ((regs->sstatus & SR_FS) != SR_FS_OFF) { __fstate_restore(task); __fstate_clean(regs); @@ -50,13 +57,17 @@ static inline void __switch_to_aux(struct task_struct *prev, { struct pt_regs *regs; + if (unlikely(no_fpu)) + return; + regs = task_pt_regs(prev); if (unlikely(regs->sstatus & SR_SD)) fstate_save(prev, regs); fstate_restore(next, task_pt_regs(next)); } -#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_INITIAL) +#define DEFAULT_SSTATUS \ + ((unlikely(no_fpu)) ? (SR_SPIE | SR_FS_OFF) : (SR_SPIE | SR_FS_INITIAL)) #else #define fstate_save(task, regs) do { } while (0) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 17011a870044..bc269c1e0b1a 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -22,6 +22,9 @@ #include unsigned long elf_hwcap __read_mostly; +#ifdef CONFIG_FPU +bool no_fpu __read_mostly; +#endif void riscv_fill_hwcap(void) { @@ -58,4 +61,12 @@ void riscv_fill_hwcap(void) elf_hwcap |= isa2hwcap[(unsigned char)(isa[i])]; pr_info("elf_hwcap is 0x%lx", elf_hwcap); + +#ifdef CONFIG_FPU + no_fpu = 0; + if (!(elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))) { + pr_info("Bypass FPU code."); + no_fpu = 1; + } +#endif } diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 2450b824d799..9714e4fccb69 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -45,6 +45,9 @@ static long restore_fp_state(struct pt_regs *regs, struct __riscv_d_ext_state __user *state = &sc_fpregs->d; size_t i; + if (unlikely(no_fpu)) + return 0; + err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); if (unlikely(err)) return err; @@ -72,6 +75,9 @@ static long save_fp_state(struct pt_regs *regs, struct __riscv_d_ext_state __user *state = &sc_fpregs->d; size_t i; + if (unlikely(no_fpu)) + return 0; + fstate_save(current, regs); err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); if (unlikely(err)) -- 2.18.0