Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5368613imm; Sun, 26 Aug 2018 18:10:46 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZYGYrvK1YtGbXcVuJnfBlcodJvu3NQmQwe0nrMCrjLC8C3BJFJELskW6if3kc5Nsu545Px X-Received: by 2002:a63:cd02:: with SMTP id i2-v6mr9818677pgg.93.1535332246913; Sun, 26 Aug 2018 18:10:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535332246; cv=none; d=google.com; s=arc-20160816; b=qwoVpL8deUNTLFMbOfvnFX5ddILnQ5usBwF+zXA7WN8hsMDKzuI2k8GK9o8ovFWD+O 6soJcENUcfNL8Gd3eVTTF0Ec15woUO35AA7G+jtabpCtf4NhOaoDq9vNtFhLL9bfdWoO k7KWkh+j3/tNMGA1A5HMhv4zmUeV3zSNabt8oU2fTh3GdG6BnRW+PY2zUgu6F6J/e3os DUXZZDqOJvw+3yDK4/vVLn7Qr5LN61sCc2PF19pNgZfJ38Jj1VvSWgmABx63n8kHaWpI 3ogd/IO8oL9zUqM2LsCUOQKpae5WwWyJohnqVLZnwOx1xsGVUfEUNAu3+ttzFwJtpOfe Db4w== 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=BaGFNbX90cvpysVX2u4A4Y2LLs2Y38suLdfBNlj9EUY=; b=H8qoqPHBzK8i/+QwgEILG/gzj3QrzhtMyQ3/F79xaw7pNozAxCAtQjF357fQZ40xMB CEwTkanl+M7iDxBAWi0Ben4rsHkOJ2pjnt9mAgutm1fYG8e0TZiyqOPmP/6yxaOFH+Sz k7cQ4adqWqgi/36G8mCXKJYvrW+5kouSULIgL9LIA61BhdjvwrnvUHLuzVX+gfBhEl2v ABlJ5MT8fkRka4B8W2r47XoVf6O2pYLy/JdMKaH55+03KQ/w1kV7SjVJ93Zr+muc/TZD aEQ9RJ+AQVtMApyJlznOkU7iIgKwwB/mWfB0z8sofbsTI89XWZd8uWbIdlRuTODn2AOr LOwA== 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 j21-v6si12696492pgl.8.2018.08.26.18.10.31; Sun, 26 Aug 2018 18:10: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 S1727193AbeH0Ew4 (ORCPT + 99 others); Mon, 27 Aug 2018 00:52:56 -0400 Received: from exmail.andestech.com ([59.124.169.137]:60599 "EHLO ATCSQR.andestech.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726825AbeH0Ew4 (ORCPT ); Mon, 27 Aug 2018 00:52:56 -0400 Received: from mail.andestech.com (atcpcs16.andestech.com [10.0.1.222]) by ATCSQR.andestech.com with ESMTP id w7R1641T025415; Mon, 27 Aug 2018 09:06:04 +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; Mon, 27 Aug 2018 09:08:24 +0800 From: Alan Kao To: , , "Palmer Dabbelt" , Albert Ou , Christoph Hellwig , Andrew Waterman , Arnd Bergmann , Darius Rad CC: , , , , Alan Kao Subject: [PATCH 5/5] Auto-detect whether a FPU exists Date: Mon, 27 Aug 2018 09:07:55 +0800 Message-ID: <1535332075-5727-6-git-send-email-alankao@andestech.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1535332075-5727-1-git-send-email-alankao@andestech.com> References: <1535332075-5727-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 w7R1641T025415 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, has_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/switch_to.h | 8 ++++---- arch/riscv/kernel/cpufeature.c | 8 ++++++++ arch/riscv/kernel/process.c | 4 +++- arch/riscv/kernel/signal.c | 6 ++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 093050b03543..733559083f24 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -56,13 +56,12 @@ static inline void __switch_to_aux(struct task_struct *prev, fstate_restore(next, task_pt_regs(next)); } -#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_INITIAL) - +extern bool has_fpu; #else +#define has_fpu false #define fstate_save(task, regs) do { } while (0) #define fstate_restore(task, regs) do { } while (0) #define __switch_to_aux(__prev, __next) do { } while (0) -#define DEFAULT_SSTATUS (SR_SPIE | SR_FS_OFF) #endif extern struct task_struct *__switch_to(struct task_struct *, @@ -72,7 +71,8 @@ extern struct task_struct *__switch_to(struct task_struct *, do { \ struct task_struct *__prev = (prev); \ struct task_struct *__next = (next); \ - __switch_to_aux(__prev, __next); \ + if (has_fpu) \ + __switch_to_aux(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 17011a870044..46942e635266 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 has_fpu __read_mostly; +#endif void riscv_fill_hwcap(void) { @@ -58,4 +61,9 @@ void riscv_fill_hwcap(void) elf_hwcap |= isa2hwcap[(unsigned char)(isa[i])]; pr_info("elf_hwcap is 0x%lx", elf_hwcap); + +#ifdef CONFIG_FPU + if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)) + has_fpu = true; +#endif } diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 07d515655aa9..bef19993ea92 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -76,7 +76,9 @@ void show_regs(struct pt_regs *regs) void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) { - regs->sstatus = DEFAULT_SSTATUS; + regs->sstatus = SR_SPIE; + if (has_fpu) + regs->sstatus |= SR_FS_INITIAL; regs->sepc = pc; regs->sp = sp; set_fs(USER_DS); diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 2450b824d799..f9b5e7e352ef 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -98,7 +98,8 @@ static long restore_sigcontext(struct pt_regs *regs, /* sc_regs is structured the same as the start of pt_regs */ err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); /* Restore the floating-point state. */ - err |= restore_fp_state(regs, &sc->sc_fpregs); + if (has_fpu) + err |= restore_fp_state(regs, &sc->sc_fpregs); return err; } @@ -150,7 +151,8 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, /* sc_regs is structured the same as the start of pt_regs */ err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); /* Save the floating-point state. */ - err |= save_fp_state(regs, &sc->sc_fpregs); + if (has_fpu) + err |= save_fp_state(regs, &sc->sc_fpregs); return err; } -- 2.18.0