qinwei2004 commented on code in PR #6478: URL: https://github.com/apache/incubator-nuttx/pull/6478#discussion_r902417367
########## arch/arm64/src/common/arm64_fatal.h: ########## @@ -0,0 +1,77 @@ +/**************************************************************************** + * arch/arm64/src/common/arm64_fatal.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM64_SRC_COMMON_ARM64_FATAL_H__ +#define __ARCH_ARM64_SRC_COMMON_ARM64_FATAL_H__ + +/** + * @defgroup fatal_apis Fatal error APIs + * @ingroup kernel_apis + * @{ + */ + +#define K_ERR_CPU_EXCEPTION (0) +#define K_ERR_CPU_MODE32 (1) +#define K_ERR_SPURIOUS_IRQ (2) + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <stdint.h> +#include <debug.h> +#include <assert.h> + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: arm64_fatal_error + * + * Description: + * fatal error handle for arm64 + * Input Parameters: + * reason: error reason + * reg: exception stack reg context + * + * Returned Value: + * + ****************************************************************************/ + +void arm64_fatal_error(unsigned int reason, struct regs_context * reg); +void arm64_dump_fatal(struct regs_context * reg); + +#define __builtin_unreachable() \ + do { \ + sinfo("Unreachable code\n"); \ + PANIC(); \ + } while (true) + +#endif //__ASSEMBLY__ + +#endif /* __ARCH_ARM64_SRC_COMMON_ARM64_FATAL_H__ */ Review Comment: fix at [48faa78](https://github.com/apache/incubator-nuttx/pull/6478/commits/48faa78d9898c978116903e3d2860b1f2593196a) please check ########## arch/arm64/src/common/arm64_fpu.c: ########## @@ -0,0 +1,249 @@ +/*************************************************************************** + * arch/arm64/src/common/arm64_fpu.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 <nuttx/config.h> + +#include <inttypes.h> +#include <stdint.h> +#include <string.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> +#include <nuttx/sched.h> +#include <nuttx/arch.h> +#include <arch/irq.h> + +#include "sched/sched.h" +#include "arm64_arch.h" +#include "arm64_vfork.h" +#include "arm64_internal.h" +#include "arm64_fatal.h" +#include "arm64_fpu.h" + +static struct fpu_reg g_idle_thread_fpu[CONFIG_SMP_NCPUS]; + +struct arm64_cpu_fpu_context +{ + /* owner of current CPU's FPU */ + + struct tcb_s * fpu_owner; + + struct tcb_s * idle_thread; + + /* for statistic propose */ + + int save_count; + int restore_count; + int switch_count; + int exe_depth_count; +}; + +static struct arm64_cpu_fpu_context g_cpu_fpu_ctx[CONFIG_SMP_NCPUS]; + +/*************************************************************************** + * Private Data + ***************************************************************************/ + +/*************************************************************************** + * Public Functions + ***************************************************************************/ + +void arm64_init_fpu(struct tcb_s *tcb) +{ + if (tcb->pid < CONFIG_SMP_NCPUS) + { + memset(&g_cpu_fpu_ctx[this_cpu()], 0, + sizeof(struct arm64_cpu_fpu_context)); + g_cpu_fpu_ctx[this_cpu()].idle_thread = tcb; + + tcb->xcp.fpu_regs = &g_idle_thread_fpu[this_cpu()]; + } + + memset(tcb->xcp.fpu_regs, 0, sizeof(struct fpu_reg)); + tcb->xcp.fpu_regs->fpu_trap = 0; +} + +void arm64_destory_fpu(struct tcb_s * tcb) +{ + struct tcb_s * owner; + + /* save current fpu owner's context */ + + owner = g_cpu_fpu_ctx[this_cpu()].fpu_owner; + + if (owner == tcb) + { + g_cpu_fpu_ctx[this_cpu()].fpu_owner = NULL; + } +} + +/* enable FPU access trap */ + +static void arm64_fpu_access_trap_enable(void) +{ + uint64_t cpacr; + + cpacr = read_sysreg(cpacr_el1); + cpacr &= ~CPACR_EL1_FPEN_NOTRAP; + write_sysreg(cpacr, cpacr_el1); + + __ISB(); +} + +/* disable FPU access trap */ + +static void arm64_fpu_access_trap_disable(void) +{ + uint64_t cpacr; + + cpacr = read_sysreg(cpacr_el1); + + cpacr |= CPACR_EL1_FPEN_NOTRAP; + + write_sysreg(cpacr, cpacr_el1); + + __ISB(); +} + +/*************************************************************************** + * Name: arm64_fpu_enter_exception + * + * Description: + * called at every time get into a exception + * + ***************************************************************************/ + +void arm64_fpu_enter_exception(void) +{ +} + +void arm64_fpu_exit_exception(void) +{ +} + +void arm64_fpu_trap(struct esf_reg * regs) +{ + struct tcb_s * owner; + + /* disable fpu trap access */ + + arm64_fpu_access_trap_disable(); + + /* save current fpu owner's context */ + + owner = g_cpu_fpu_ctx[this_cpu()].fpu_owner; + + if (owner != NULL) + { + arm64_fpu_save(owner->xcp.fpu_regs); + __DSB(); + g_cpu_fpu_ctx[this_cpu()].save_count++; + g_cpu_fpu_ctx[this_cpu()].fpu_owner = NULL; + } + + if (arch_get_exception_depth() > 1) + { + /* if get_exception_depth > 1 + * it means FPU access exception occurred in exception context + * switch FPU owner to idle thread + */ + + owner = g_cpu_fpu_ctx[this_cpu()].idle_thread; + } + else + { + owner = (struct tcb_s *)arch_get_current_tcb(); + } + + /* restore our content */ + + arm64_fpu_restore(owner->xcp.fpu_regs); + g_cpu_fpu_ctx[this_cpu()].restore_count++; + + /* become new owner */ + + g_cpu_fpu_ctx[this_cpu()].fpu_owner = owner; + owner->xcp.fpu_regs->fpu_trap = 1; +} + +void arm64_fpu_context_restore(void) +{ + struct tcb_s *new_tcb = (struct tcb_s *)arch_get_current_tcb(); + + arm64_fpu_access_trap_enable(); + + if (new_tcb->xcp.fpu_regs->fpu_trap == 0) + { + /* FPU trap hasn't happened at this task */ + + arm64_fpu_access_trap_enable(); + } + else + { + /* FPU trap has happened at this task */ + + if (new_tcb == g_cpu_fpu_ctx[this_cpu()].fpu_owner) + { + arm64_fpu_access_trap_disable(); + } + else + { + arm64_fpu_access_trap_enable(); + } + } + + g_cpu_fpu_ctx[this_cpu()].switch_count++; +} + +void arm64_fpu_enable(void) +{ + irqstate_t flags = up_irq_save(); + + arm64_fpu_access_trap_enable(); + up_irq_restore(flags); +} + +void arm64_fpu_disable(void) +{ + irqstate_t flags = up_irq_save(); + + arm64_fpu_access_trap_disable(); + up_irq_restore(flags); +} + +/*************************************************************************** + * Name: up_fpucmp + * + * Description: + * compare FPU areas from thread context + * + ***************************************************************************/ + +bool up_fpucmp(const void *saveregs1, const void *saveregs2) +{ + const uint64_t *regs1 = saveregs1 + XCPTCONTEXT_GP_SIZE; + const uint64_t *regs2 = saveregs2 + XCPTCONTEXT_GP_SIZE; Review Comment: fix at [48faa78](https://github.com/apache/incubator-nuttx/pull/6478/commits/48faa78d9898c978116903e3d2860b1f2593196a) please check -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org