From 00efcd33082192bc33d0b1d0c12ad9b47e7ecd0a Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 18 Mar 2022 12:20:15 +0800 Subject: [PATCH] arch/risc-v: Merge riscv_getnewintctx into common And also mask the bits which should be preserved (from ISA spec) Signed-off-by: Huang Qi --- arch/risc-v/include/csr.h | 8 +++ arch/risc-v/src/bl602/Make.defs | 2 +- arch/risc-v/src/bl602/bl602_irq.c | 23 ------- arch/risc-v/src/c906/Make.defs | 2 +- arch/risc-v/src/c906/c906_irq.c | 27 -------- arch/risc-v/src/common/riscv_getnewintctx.c | 71 +++++++++++++++++++++ arch/risc-v/src/esp32c3/Make.defs | 2 +- arch/risc-v/src/esp32c3/esp32c3_irq.c | 19 ------ arch/risc-v/src/fe310/Make.defs | 2 +- arch/risc-v/src/fe310/fe310_irq.c | 19 ------ arch/risc-v/src/k210/Make.defs | 2 +- arch/risc-v/src/k210/k210_irq.c | 23 ------- arch/risc-v/src/litex/Make.defs | 2 +- arch/risc-v/src/litex/litex_irq.c | 19 ------ arch/risc-v/src/mpfs/Make.defs | 2 +- arch/risc-v/src/mpfs/mpfs_irq.c | 27 -------- arch/risc-v/src/qemu-rv/Make.defs | 2 +- arch/risc-v/src/qemu-rv/qemu_rv_irq.c | 24 ------- arch/risc-v/src/rv32m1/Make.defs | 2 +- arch/risc-v/src/rv32m1/rv32m1_irq.c | 19 ------ 20 files changed, 88 insertions(+), 209 deletions(-) create mode 100644 arch/risc-v/src/common/riscv_getnewintctx.c diff --git a/arch/risc-v/include/csr.h b/arch/risc-v/include/csr.h index 16c3d9b8817..a5cb948b0c8 100644 --- a/arch/risc-v/include/csr.h +++ b/arch/risc-v/include/csr.h @@ -307,6 +307,14 @@ #define MSTATUS_FS_CLEAN (0x2 << 13) #define MSTATUS_FS_DIRTY (0x3 << 13) +/* Mask of preserved bits for mstatus */ + +#ifdef CONFIG_ARCH_RV32 +#define MSTATUS_WPRI (0xff << 23 | 0x15) +#else +#define MSTATUS_WPRI (UINT64_C(0x1ffffff) << 38 | UINT64_C(0x1ff) << 23 | 0x15) +#endif + /* In mie (machine interrupt enable) register */ #define MIE_MSIE (0x1 << 3) /* Machine Software Interrupt Enable */ diff --git a/arch/risc-v/src/bl602/Make.defs b/arch/risc-v/src/bl602/Make.defs index ece57c53b7e..251f1980d5c 100644 --- a/arch/risc-v/src/bl602/Make.defs +++ b/arch/risc-v/src/bl602/Make.defs @@ -33,7 +33,7 @@ CMN_CSRCS += riscv_interruptcontext.c riscv_modifyreg32.c riscv_puts.c riscv_mde CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c riscv_copyfullstate.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_udelay.c riscv_unblocktask.c riscv_usestack.c -CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c +CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c riscv_getnewintctx.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) CMN_CSRCS += riscv_backtrace.c diff --git a/arch/risc-v/src/bl602/bl602_irq.c b/arch/risc-v/src/bl602/bl602_irq.c index 0a0a7e334dc..9540f793bde 100644 --- a/arch/risc-v/src/bl602/bl602_irq.c +++ b/arch/risc-v/src/bl602/bl602_irq.c @@ -163,29 +163,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. - * Also set machine previous interrupt enable - */ - - uintptr_t mstatus = READ_CSR(mstatus); - -#ifdef CONFIG_ARCH_FPU - return (mstatus | MSTATUS_FS_INIT | MSTATUS_MPPM | MSTATUS_MPIE); -#else - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -#endif -} - /**************************************************************************** * Name: riscv_ack_irq * diff --git a/arch/risc-v/src/c906/Make.defs b/arch/risc-v/src/c906/Make.defs index 53447d88ef1..75c6cddd7dc 100644 --- a/arch/risc-v/src/c906/Make.defs +++ b/arch/risc-v/src/c906/Make.defs @@ -34,7 +34,7 @@ CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_unblocktask.c riscv_usestack.c CMN_CSRCS += riscv_mdelay.c riscv_copyfullstate.c riscv_idle.c -CMN_CSRCS += riscv_tcbinfo.c +CMN_CSRCS += riscv_tcbinfo.c riscv_getnewintctx.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) CMN_CSRCS += riscv_backtrace.c diff --git a/arch/risc-v/src/c906/c906_irq.c b/arch/risc-v/src/c906/c906_irq.c index feb91687556..e315b7456df 100644 --- a/arch/risc-v/src/c906/c906_irq.c +++ b/arch/risc-v/src/c906/c906_irq.c @@ -194,33 +194,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. Reegardless of - * how NuttX is configured and of what kind of thread is being started. - * That is because all threads, even user-mode threads will start in - * kernel trampoline at nxtask_start() or pthread_start(). - * The thread's privileges will be dropped before transitioning to - * user code. Also set machine previous interrupt enable. - */ - - uintptr_t mstatus = READ_CSR(mstatus); - -#ifdef CONFIG_ARCH_FPU - return (mstatus | MSTATUS_FS_INIT | MSTATUS_MPPM | MSTATUS_MPIE); -#else - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -#endif -} - /**************************************************************************** * Name: riscv_ack_irq * diff --git a/arch/risc-v/src/common/riscv_getnewintctx.c b/arch/risc-v/src/common/riscv_getnewintctx.c new file mode 100644 index 00000000000..04180f7abb7 --- /dev/null +++ b/arch/risc-v/src/common/riscv_getnewintctx.c @@ -0,0 +1,71 @@ +/**************************************************************************** + * arch/risc-v/src/common/riscv_getnewintctx.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include "riscv_internal.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: riscv_get_newintctx + * + * Description: + * Return initial mstatus when a task is created. + * + ****************************************************************************/ + +uintptr_t riscv_get_newintctx(void) +{ + /* Set machine previous privilege mode to machine mode. Reegardless of + * how NuttX is configured and of what kind of thread is being started. + * That is because all threads, even user-mode threads will start in + * kernel trampoline at nxtask_start() or pthread_start(). + * The thread's privileges will be dropped before transitioning to + * user code. Also set machine previous interrupt enable. + * + * Mask the bits which should be preserved (from ISA spec) + */ + + uintptr_t mstatus = READ_CSR(mstatus); + + mstatus &= MSTATUS_WPRI; + + return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE +#ifdef CONFIG_ARCH_FPU + | MSTATUS_FS_INIT +#endif + ); +} diff --git a/arch/risc-v/src/esp32c3/Make.defs b/arch/risc-v/src/esp32c3/Make.defs index 318ed8008d7..bfa54a7df77 100644 --- a/arch/risc-v/src/esp32c3/Make.defs +++ b/arch/risc-v/src/esp32c3/Make.defs @@ -36,7 +36,7 @@ CMN_CSRCS += riscv_interruptcontext.c riscv_modifyreg32.c riscv_puts.c riscv_mde CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c riscv_copyfullstate.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_udelay.c riscv_unblocktask.c riscv_usestack.c -CMN_CSRCS += riscv_tcbinfo.c +CMN_CSRCS += riscv_tcbinfo.c riscv_getnewintctx.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) CMN_CSRCS += riscv_backtrace.c diff --git a/arch/risc-v/src/esp32c3/esp32c3_irq.c b/arch/risc-v/src/esp32c3/esp32c3_irq.c index 27a8e56d8f3..ad247d0e8a7 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_irq.c +++ b/arch/risc-v/src/esp32c3/esp32c3_irq.c @@ -134,25 +134,6 @@ void up_irqinitialize(void) #endif } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. - * Also set machine previous interrupt enable - */ - - uintptr_t mstatus = READ_CSR(mstatus); - - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -} - /**************************************************************************** * Name: up_enable_irq * diff --git a/arch/risc-v/src/fe310/Make.defs b/arch/risc-v/src/fe310/Make.defs index e8e6252bab2..dcb63384f50 100644 --- a/arch/risc-v/src/fe310/Make.defs +++ b/arch/risc-v/src/fe310/Make.defs @@ -33,7 +33,7 @@ CMN_CSRCS += riscv_interruptcontext.c riscv_modifyreg32.c riscv_puts.c riscv_mde CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c riscv_copyfullstate.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_udelay.c riscv_unblocktask.c riscv_usestack.c -CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c +CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c riscv_getnewintctx.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) CMN_CSRCS += riscv_backtrace.c diff --git a/arch/risc-v/src/fe310/fe310_irq.c b/arch/risc-v/src/fe310/fe310_irq.c index 7397a576dbe..9e0e17a0e40 100644 --- a/arch/risc-v/src/fe310/fe310_irq.c +++ b/arch/risc-v/src/fe310/fe310_irq.c @@ -165,25 +165,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. - * Also set machine previous interrupt enable - */ - - uintptr_t mstatus = READ_CSR(mstatus); - - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -} - /**************************************************************************** * Name: riscv_ack_irq * diff --git a/arch/risc-v/src/k210/Make.defs b/arch/risc-v/src/k210/Make.defs index 20e8d43cdf8..38ac64f6786 100644 --- a/arch/risc-v/src/k210/Make.defs +++ b/arch/risc-v/src/k210/Make.defs @@ -34,7 +34,7 @@ CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_unblocktask.c riscv_usestack.c CMN_CSRCS += riscv_mdelay.c riscv_copyfullstate.c riscv_idle.c -CMN_CSRCS += riscv_tcbinfo.c riscv_cpuidlestack.c +CMN_CSRCS += riscv_tcbinfo.c riscv_cpuidlestack.c riscv_getnewintctx.c ifeq ($(CONFIG_SMP), y) CMN_CSRCS += riscv_cpuindex.c riscv_cpupause.c riscv_cpustart.c diff --git a/arch/risc-v/src/k210/k210_irq.c b/arch/risc-v/src/k210/k210_irq.c index b6143e52594..983c6c86116 100644 --- a/arch/risc-v/src/k210/k210_irq.c +++ b/arch/risc-v/src/k210/k210_irq.c @@ -210,29 +210,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. Reegardless of - * how NuttX is configured and of what kind of thread is being started. - * That is because all threads, even user-mode threads will start in - * kernel trampoline at nxtask_start() or pthread_start(). - * The thread's privileges will be dropped before transitioning to - * user code. Also set machine previous interrupt enable. - */ - - uintptr_t mstatus = READ_CSR(mstatus); - - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -} - /**************************************************************************** * Name: riscv_ack_irq * diff --git a/arch/risc-v/src/litex/Make.defs b/arch/risc-v/src/litex/Make.defs index d33fd8fe16b..f992c6bddba 100644 --- a/arch/risc-v/src/litex/Make.defs +++ b/arch/risc-v/src/litex/Make.defs @@ -33,7 +33,7 @@ CMN_CSRCS += riscv_interruptcontext.c riscv_modifyreg32.c riscv_puts.c riscv_mde CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c riscv_copyfullstate.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_udelay.c riscv_unblocktask.c riscv_usestack.c -CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c +CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c riscv_getnewintctx.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) CMN_CSRCS += riscv_backtrace.c diff --git a/arch/risc-v/src/litex/litex_irq.c b/arch/risc-v/src/litex/litex_irq.c index fa12ac0055e..e1df1d37866 100644 --- a/arch/risc-v/src/litex/litex_irq.c +++ b/arch/risc-v/src/litex/litex_irq.c @@ -171,25 +171,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. - * Also set machine previous interrupt enable - */ - - uintptr_t mstatus = READ_CSR(mstatus); - - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -} - /**************************************************************************** * Name: riscv_ack_irq * diff --git a/arch/risc-v/src/mpfs/Make.defs b/arch/risc-v/src/mpfs/Make.defs index 460d5c7d62b..e46bf5e10ab 100755 --- a/arch/risc-v/src/mpfs/Make.defs +++ b/arch/risc-v/src/mpfs/Make.defs @@ -31,7 +31,7 @@ CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_unblocktask.c riscv_usestack.c CMN_CSRCS += riscv_mdelay.c riscv_udelay.c riscv_copyfullstate.c -CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c +CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c riscv_getnewintctx.c CMN_CSRCS += riscv_cpuindex.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) diff --git a/arch/risc-v/src/mpfs/mpfs_irq.c b/arch/risc-v/src/mpfs/mpfs_irq.c index f7bf10a6819..c99574a77a2 100755 --- a/arch/risc-v/src/mpfs/mpfs_irq.c +++ b/arch/risc-v/src/mpfs/mpfs_irq.c @@ -205,33 +205,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. Reegardless of - * how NuttX is configured and of what kind of thread is being started. - * That is because all threads, even user-mode threads will start in - * kernel trampoline at nxtask_start() or pthread_start(). - * The thread's privileges will be dropped before transitioning to - * user code. Also set machine previous interrupt enable. - */ - - uintptr_t mstatus = READ_CSR(mstatus); - -#ifdef CONFIG_ARCH_FPU - return (mstatus | MSTATUS_FS_INIT | MSTATUS_MPPM | MSTATUS_MPIE); -#else - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -#endif -} - /**************************************************************************** * Name: riscv_ack_irq * diff --git a/arch/risc-v/src/qemu-rv/Make.defs b/arch/risc-v/src/qemu-rv/Make.defs index f45d036960d..a86700252c3 100644 --- a/arch/risc-v/src/qemu-rv/Make.defs +++ b/arch/risc-v/src/qemu-rv/Make.defs @@ -34,7 +34,7 @@ CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c riscv_copyfullstate. CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_unblocktask.c riscv_usestack.c CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c riscv_cpuidlestack.c -CMN_CSRCS += riscv_fault.c +CMN_CSRCS += riscv_fault.c riscv_getnewintctx.c ifeq ($(CONFIG_SMP), y) CMN_CSRCS += riscv_cpuindex.c riscv_cpupause.c riscv_cpustart.c diff --git a/arch/risc-v/src/qemu-rv/qemu_rv_irq.c b/arch/risc-v/src/qemu-rv/qemu_rv_irq.c index 55cdfc1ef69..a309cf81097 100644 --- a/arch/risc-v/src/qemu-rv/qemu_rv_irq.c +++ b/arch/risc-v/src/qemu-rv/qemu_rv_irq.c @@ -205,27 +205,3 @@ irqstate_t up_irq_enable(void) oldstat = READ_AND_SET_CSR(mstatus, MSTATUS_MIE); return oldstat; } - -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. - * Also set machine previous interrupt enable - * Note: In qemu, FPU is always exist even if don't use F|D ISA extension - */ - - uintptr_t mstatus = READ_CSR(mstatus); - -#ifdef CONFIG_ARCH_FPU - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE | MSTATUS_FS_INIT); -#else - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -#endif -} diff --git a/arch/risc-v/src/rv32m1/Make.defs b/arch/risc-v/src/rv32m1/Make.defs index e360cf23a7e..f44ea46cf08 100644 --- a/arch/risc-v/src/rv32m1/Make.defs +++ b/arch/risc-v/src/rv32m1/Make.defs @@ -33,7 +33,7 @@ CMN_CSRCS += riscv_interruptcontext.c riscv_modifyreg32.c riscv_puts.c CMN_CSRCS += riscv_releasepending.c riscv_reprioritizertr.c riscv_copyfullstate.c CMN_CSRCS += riscv_releasestack.c riscv_stackframe.c riscv_schedulesigaction.c CMN_CSRCS += riscv_sigdeliver.c riscv_unblocktask.c riscv_usestack.c -CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c +CMN_CSRCS += riscv_idle.c riscv_tcbinfo.c riscv_getnewintctx.c ifeq ($(CONFIG_SCHED_BACKTRACE),y) CMN_CSRCS += riscv_backtrace.c diff --git a/arch/risc-v/src/rv32m1/rv32m1_irq.c b/arch/risc-v/src/rv32m1/rv32m1_irq.c index 2b410d75af5..7692a0b2987 100644 --- a/arch/risc-v/src/rv32m1/rv32m1_irq.c +++ b/arch/risc-v/src/rv32m1/rv32m1_irq.c @@ -213,25 +213,6 @@ void up_enable_irq(int irq) } } -/**************************************************************************** - * Name: riscv_get_newintctx - * - * Description: - * Return initial mstatus when a task is created. - * - ****************************************************************************/ - -uintptr_t riscv_get_newintctx(void) -{ - /* Set machine previous privilege mode to machine mode. - * Also set machine previous interrupt enable - */ - - uintptr_t mstatus = READ_CSR(mstatus); - - return (mstatus | MSTATUS_MPPM | MSTATUS_MPIE); -} - /**************************************************************************** * Name: riscv_ack_irq *