From d9f0e30bdd647174f16c715b31dc1c7c0e332c27 Mon Sep 17 00:00:00 2001 From: hujun5 Date: Tue, 6 Aug 2024 11:24:26 +0800 Subject: [PATCH] arch: inline up_switch_context,in arm arm64 reason: when a context switch occurs, up_switch_context is executed. In order to reduce the time taken for context switching, we inline the up_switch_context function. Signed-off-by: hujun5 --- arch/arm/include/irq.h | 12 ++++ arch/arm/include/tlsr82/irq.h | 8 +++ arch/arm/src/common/CMakeLists.txt | 1 - arch/arm/src/common/Make.defs | 2 +- arch/arm/src/common/arm_internal.h | 8 --- arch/arm/src/common/arm_switchcontext.c | 74 --------------------- arch/arm/src/tlsr82/Make.defs | 1 - arch/arm64/include/irq.h | 10 +++ arch/arm64/src/common/CMakeLists.txt | 2 +- arch/arm64/src/common/Make.defs | 2 +- arch/arm64/src/common/arm64_internal.h | 3 - arch/arm64/src/common/arm64_switchcontext.c | 74 --------------------- include/nuttx/arch.h | 2 + 13 files changed, 35 insertions(+), 164 deletions(-) delete mode 100644 arch/arm/src/common/arm_switchcontext.c delete mode 100644 arch/arm64/src/common/arm64_switchcontext.c diff --git a/arch/arm/include/irq.h b/arch/arm/include/irq.h index e859604c6b18a..b88b2159cf2e3 100644 --- a/arch/arm/include/irq.h +++ b/arch/arm/include/irq.h @@ -32,6 +32,7 @@ #include #ifndef __ASSEMBLY__ # include +# include #endif /**************************************************************************** @@ -76,6 +77,17 @@ #ifndef __ASSEMBLY__ +#ifndef up_switch_context +#define up_switch_context(tcb, rtcb) \ + do { \ + if (!up_interrupt_context()) \ + { \ + sys_call2(SYS_switch_context, (uintptr_t)&rtcb->xcp.regs, \ + (uintptr_t)tcb->xcp.regs); \ + } \ + } while (0) +#endif + #ifdef __cplusplus #define EXTERN extern "C" extern "C" diff --git a/arch/arm/include/tlsr82/irq.h b/arch/arm/include/tlsr82/irq.h index 72cb08c9bfcf9..7cf890854278c 100644 --- a/arch/arm/include/tlsr82/irq.h +++ b/arch/arm/include/tlsr82/irq.h @@ -312,6 +312,14 @@ static inline_function bool up_interrupt_context(void) return ret; } +#define up_switch_context(tcb, rtcb) \ + do { \ + if (!up_interrupt_context()) \ + { \ + tc32_switchcontext(&rtcb->xcp.regs, tcb->xcp.regs); \ + } \ + } while (0) + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/arch/arm/src/common/CMakeLists.txt b/arch/arm/src/common/CMakeLists.txt index ec0fccfdfba0e..6e507fca82c78 100644 --- a/arch/arm/src/common/CMakeLists.txt +++ b/arch/arm/src/common/CMakeLists.txt @@ -38,7 +38,6 @@ set(SRCS arm_releasestack.c arm_registerdump.c arm_stackframe.c - arm_switchcontext.c arm_usestack.c arm_fork.c ${ARCH_TOOLCHAIN_PATH}/fork.S) diff --git a/arch/arm/src/common/Make.defs b/arch/arm/src/common/Make.defs index aeb4ab6443b81..e7552337811b9 100644 --- a/arch/arm/src/common/Make.defs +++ b/arch/arm/src/common/Make.defs @@ -24,7 +24,7 @@ CMN_CSRCS += arm_allocateheap.c arm_createstack.c arm_exit.c CMN_CSRCS += arm_getintstack.c arm_initialize.c arm_lowputs.c CMN_CSRCS += arm_modifyreg8.c arm_modifyreg16.c arm_modifyreg32.c CMN_CSRCS += arm_nputs.c arm_releasestack.c arm_registerdump.c -CMN_CSRCS += arm_stackframe.c arm_switchcontext.c +CMN_CSRCS += arm_stackframe.c CMN_CSRCS += arm_usestack.c arm_fork.c ifneq ($(CONFIG_ALARM_ARCH),y) diff --git a/arch/arm/src/common/arm_internal.h b/arch/arm/src/common/arm_internal.h index e7ace0d52ed84..44daabd67afca 100644 --- a/arch/arm/src/common/arm_internal.h +++ b/arch/arm/src/common/arm_internal.h @@ -153,14 +153,6 @@ extern void arm_fullcontextrestore(uint32_t *restoreregs); #endif -#ifndef arm_switchcontext -# define arm_switchcontext(saveregs, restoreregs) \ - sys_call2(SYS_switch_context, (uintptr_t)saveregs, (uintptr_t)restoreregs); -#else -extern void arm_switchcontext(uint32_t **saveregs, - uint32_t *restoreregs); -#endif - /* Redefine the linker symbols as armlink style */ #ifdef CONFIG_ARM_TOOLCHAIN_ARMCLANG diff --git a/arch/arm/src/common/arm_switchcontext.c b/arch/arm/src/common/arm_switchcontext.c deleted file mode 100644 index 946e6441c81b2..0000000000000 --- a/arch/arm/src/common/arm_switchcontext.c +++ /dev/null @@ -1,74 +0,0 @@ -/**************************************************************************** - * arch/arm/src/common/arm_switchcontext.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 "sched/sched.h" -#include "group/group.h" -#include "clock/clock.h" -#include "arm_internal.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: up_switch_context - * - * Description: - * A task is currently in the ready-to-run list but has been prepped - * to execute. Restore its context, and start execution. - * - * Input Parameters: - * tcb: Refers to the head task of the ready-to-run list - * which will be executed. - * rtcb: Refers to the running task which will be blocked. - * - ****************************************************************************/ - -void up_switch_context(struct tcb_s *tcb, struct tcb_s *rtcb) -{ - /* Are we in an interrupt handler? */ - - if (!up_interrupt_context()) - { - /* Switch context to the context of the task at the head of the - * ready to run list. - */ - - arm_switchcontext(&rtcb->xcp.regs, tcb->xcp.regs); - - /* arm_switchcontext forces a context switch to the task at the - * head of the ready-to-run list. It does not 'return' in the - * normal sense. When it does return, it is because the blocked - * task is again ready to run and has execution priority. - */ - } -} diff --git a/arch/arm/src/tlsr82/Make.defs b/arch/arm/src/tlsr82/Make.defs index e5007f532a96a..f1fe854475fea 100644 --- a/arch/arm/src/tlsr82/Make.defs +++ b/arch/arm/src/tlsr82/Make.defs @@ -66,4 +66,3 @@ ifeq ($(CONFIG_TLSR82_SOFT_FPU),y) endif CFLAGS += -Darm_fullcontextrestore=tc32_fullcontextrestore -CFLAGS += -Darm_switchcontext=tc32_switchcontext diff --git a/arch/arm64/include/irq.h b/arch/arm64/include/irq.h index 19ed567ad18e6..1f8b45a7b7549 100644 --- a/arch/arm64/include/irq.h +++ b/arch/arm64/include/irq.h @@ -34,6 +34,7 @@ #ifndef __ASSEMBLY__ # include +# include #endif /* Include NuttX-specific IRQ definitions */ @@ -423,6 +424,15 @@ static inline_function void up_set_current_regs(uint64_t *regs) __asm__ volatile ("msr " "tpidr_el1" ", %0" : : "r" (regs)); } +#define up_switch_context(tcb, rtcb) \ + do { \ + if (!up_interrupt_context()) \ + { \ + sys_call2(SYS_switch_context, (uintptr_t)&rtcb->xcp.regs, \ + (uintptr_t)tcb->xcp.regs); \ + } \ + } while (0) + /**************************************************************************** * Name: up_interrupt_context * diff --git a/arch/arm64/src/common/CMakeLists.txt b/arch/arm64/src/common/CMakeLists.txt index 7f9e95042a30b..f8c74617c9f41 100644 --- a/arch/arm64/src/common/CMakeLists.txt +++ b/arch/arm64/src/common/CMakeLists.txt @@ -29,7 +29,7 @@ list(APPEND SRCS arm64_fork_func.S) list(APPEND SRCS arm64_initialize.c arm64_initialstate.c arm64_boot.c) list(APPEND SRCS arm64_nputs.c arm64_copystate.c arm64_createstack.c) list(APPEND SRCS arm64_releasestack.c arm64_stackframe.c arm64_usestack.c) -list(APPEND SRCS arm64_exit.c arm64_fork.c arm64_switchcontext.c) +list(APPEND SRCS arm64_exit.c arm64_fork.c) list(APPEND SRCS arm64_schedulesigaction.c arm64_sigdeliver.c) list(APPEND SRCS arm64_getintstack.c arm64_registerdump.c) list(APPEND SRCS arm64_perf.c arm64_tcbinfo.c) diff --git a/arch/arm64/src/common/Make.defs b/arch/arm64/src/common/Make.defs index 51540dd31abc9..a2e3ac3857f12 100644 --- a/arch/arm64/src/common/Make.defs +++ b/arch/arm64/src/common/Make.defs @@ -41,7 +41,7 @@ CMN_ASRCS += arm64_fork_func.S CMN_CSRCS = arm64_initialize.c arm64_initialstate.c arm64_boot.c CMN_CSRCS += arm64_nputs.c arm64_copystate.c arm64_createstack.c CMN_CSRCS += arm64_releasestack.c arm64_stackframe.c arm64_usestack.c -CMN_CSRCS += arm64_exit.c arm64_fork.c arm64_switchcontext.c +CMN_CSRCS += arm64_exit.c arm64_fork.c CMN_CSRCS += arm64_schedulesigaction.c arm64_sigdeliver.c CMN_CSRCS += arm64_getintstack.c arm64_registerdump.c CMN_CSRCS += arm64_perf.c arm64_tcbinfo.c diff --git a/arch/arm64/src/common/arm64_internal.h b/arch/arm64/src/common/arm64_internal.h index 6f4ab45b2c93b..c8ce7d757df34 100644 --- a/arch/arm64/src/common/arm64_internal.h +++ b/arch/arm64/src/common/arm64_internal.h @@ -119,9 +119,6 @@ } \ while (1) -#define arm64_switchcontext(saveregs, restoreregs) \ - sys_call2(SYS_switch_context, (uintptr_t)saveregs, (uintptr_t)restoreregs) - /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/arch/arm64/src/common/arm64_switchcontext.c b/arch/arm64/src/common/arm64_switchcontext.c deleted file mode 100644 index 51210b228cc64..0000000000000 --- a/arch/arm64/src/common/arm64_switchcontext.c +++ /dev/null @@ -1,74 +0,0 @@ -/**************************************************************************** - * arch/arm64/src/common/arm64_switchcontext.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 "sched/sched.h" -#include "group/group.h" -#include "clock/clock.h" -#include "arm64_internal.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: up_switch_context - * - * Description: - * A task is currently in the ready-to-run list but has been prepped - * to execute. Restore its context, and start execution. - * - * Input Parameters: - * tcb: Refers to the head task of the ready-to-run list - * which will be executed. - * rtcb: Refers to the running task which will be blocked. - * - ****************************************************************************/ - -void up_switch_context(struct tcb_s *tcb, struct tcb_s *rtcb) -{ - /* Are we in an interrupt handler? */ - - if (!up_interrupt_context()) - { - /* Switch context to the context of the task at the head of the - * ready to run list. - */ - - arm64_switchcontext(&rtcb->xcp.regs, tcb->xcp.regs); - - /* arm_switchcontext forces a context switch to the task at the - * head of the ready-to-run list. It does not 'return' in the - * normal sense. When it does return, it is because the blocked - * task is again ready to run and has execution priority. - */ - } -} diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index 5536975c1875b..d6b8a0ef3b609 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -439,7 +439,9 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype); * ****************************************************************************/ +#ifndef up_switch_context void up_switch_context(FAR struct tcb_s *tcb, FAR struct tcb_s *rtcb); +#endif /**************************************************************************** * Name: up_exit