/****************************************************************************
 * arch/sim/src/sim/sim_initialstate.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 <stdint.h>
#include <string.h>
#ifdef CONFIG_SIM_ASAN
#  include <sanitizer/asan_interface.h>
#  include <sanitizer/common_interface_defs.h>
#endif

#include <nuttx/arch.h>

#include "sched/sched.h"
#include "sim_internal.h"

/****************************************************************************
 * Private Functions
 ****************************************************************************/

static void pre_start(void)
{
  struct tcb_s *tcb = this_task();

  /* Notify ASan that fiber switch has completed */

  sim_asan_finish_switch();

  /* Enable signal delivery */

  up_irq_restore(0);

  /* Then call the real start function */

  tcb->start();
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: up_initial_state
 *
 * Description:
 *   A new thread is being started and a new TCB
 *   has been created. This function is called to initialize
 *   the processor specific portions of the new TCB.
 *
 *   This function must setup the initial architecture registers
 *   and/or  stack so that execution will begin at tcb->start
 *   on the next context switch.
 *
 ****************************************************************************/

void up_initial_state(struct tcb_s *tcb)
{
  if (tcb->pid == IDLE_PROCESS_ID)
    {
      tcb->adj_stack_size  = CONFIG_IDLETHREAD_STACKSIZE +
                             CONFIG_SIM_STACKSIZE_ADJUSTMENT;

      tcb->stack_alloc_ptr = (char *)g_idle_topstack -
                             CONFIG_IDLETHREAD_STACKSIZE -
                             CONFIG_SIM_STACKSIZE_ADJUSTMENT;
      tcb->stack_base_ptr  = tcb->stack_alloc_ptr;

#ifdef CONFIG_STACK_COLORATION
      /* If stack debug is enabled, then fill the stack with a
       * recognizable value that we can use later to test for high
       * water marks.
       */

      sim_stack_color(tcb->stack_alloc_ptr, 0);
#endif /* CONFIG_STACK_COLORATION */
    }
  else
    {
      /* Mark the stack top to zero, to avoid unwind backtrace failed */

      memset((char *)tcb->stack_base_ptr + tcb->adj_stack_size -
           XCPTCONTEXT_SIZE, 0, XCPTCONTEXT_SIZE);
    }

  memset(&tcb->xcp, 0, sizeof(struct xcptcontext));
  tcb->xcp.regs = tcb->xcp.buf;

  /* Note: The amd64 ABI requires 16-bytes alignment _before_ a function
   * call.
   * On the other hand, our way to set up and switch to a new context
   * is basically a JUMP.
   * Thus, we need to emulate the effect of a CALL here, by subtracting
   * sizeof(xcpt_reg_t), which is the amount a CALL would move RSP to store
   * the return address.
   *
   * Note: On ARM64 architectures the return address is passed via LR
   *       register. No extra adjustment for the stack needed.
   */

  tcb->xcp.regs[JB_SP] = (xcpt_reg_t)tcb->stack_base_ptr
#if !defined(CONFIG_HOST_ARM64)
                                   - sizeof(xcpt_reg_t)
#endif
                                   + tcb->adj_stack_size;

  /* Mask the interrupt until switching to the new task */

  memset(&tcb->xcp.regs[JB_FLAG], 0xff, sizeof(uint32_t) * 2);

  tcb->xcp.regs[JB_PC] = (xcpt_reg_t)pre_start;

#if defined(CONFIG_SIM_ASAN) || defined(CONFIG_MM_KASAN_SIM)
  __asan_unpoison_memory_region(tcb->stack_alloc_ptr,
                                tcb->pid == IDLE_PROCESS_ID ?
                                tcb->adj_stack_size :
                                kmm_malloc_size(tcb->stack_alloc_ptr));
#endif
}