Files
ThreadX4TGFS/port/threadx_smp/src/tx_thread_stack_build.S
T
alex fa665fcc2e corrects saving of floating point registers
The previous implementation always made space for XLEN sized registers, in RV32D this leads to overlaps
2026-09-16 08:01:11 +02:00

146 lines
7.8 KiB
ArmAsm

/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** Thread */
/** */
/**************************************************************************/
/**************************************************************************/
#include "tx_port.h"
.section .text
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_thread_stack_build RISC-V64/GNU */
/* 6.2.1 */
/* AUTHOR */
/* */
/* Scott Larson, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function builds a stack frame on the supplied thread's stack. */
/* The stack frame results in a fake interrupt return to the supplied */
/* function pointer. */
/* */
/* INPUT */
/* */
/* thread_ptr Pointer to thread control blk */
/* function_ptr Pointer to return function */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* _tx_thread_create Create thread service */
/* */
/* RELEASE HISTORY */
/* */
/* DATE NAME DESCRIPTION */
/* */
/* 03-08-2023 Scott Larson Initial Version 6.2.1 */
/* */
/**************************************************************************/
/* VOID _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID))
{ */
.global _tx_thread_stack_build
_tx_thread_stack_build:
/* Build a fake interrupt frame using this port's inc/tx_context_layout.h,
included through tx_port.h. Integer slot indices below are multiplied
by REGBYTES to obtain byte offsets from the saved stack pointer.
Reg Integer slot
Stack Top: 1 0 Interrupt stack frame type
x27 1 Initial s11
x26 2 Initial s10
x25 3 Initial s9
x24 4 Initial s8
x23 5 Initial s7
x22 6 Initial s6
x21 7 Initial s5
x20 8 Initial s4
x19 9 Initial s3
x18 10 Initial s2
x9 11 Initial s1
x8 12 Initial s0
x31 13 Initial t6
x30 14 Initial t5
x29 15 Initial t4
x28 16 Initial t3
x7 17 Initial t2
x6 18 Initial t1
x5 19 Initial t0
x17 20 Initial a7
x16 21 Initial a6
x15 22 Initial a5
x14 23 Initial a4
x13 24 Initial a3
x12 25 Initial a2
x11 26 Initial a1
x10 27 Initial a0
x1 28 Initial ra
-- 29 reserved
mepc 30 Initial mepc
-- 31 Alignment padding
With FP support, f0..f31 follow at TX_INTERRUPT_FP_BASE. Each register
occupies TX_FP_BYTES bytes; fN is at TX_INTERRUPT_FP_OFFSET(N). The
fcsr slot is at TX_INTERRUPT_FCSR_OFFSET and occupies REGBYTES bytes.
Trailing padding rounds the frame size up to a multiple of 16 bytes.
Without FP support, the frame ends after integer slot 31.
Stack Bottom: saved stack pointer + TX_INTERRUPT_FRAME_SIZE
(higher memory address) */
LOAD t0, 4*REGBYTES(a0) // Pickup end of stack area
andi t0, t0, -16 // Ensure 16-byte alignment
/* Actually build the stack frame. */
addi t0, t0, -TX_INTERRUPT_FRAME_SIZE
/* Clear the whole frame, including both halves of RV32D FP slots. */
mv t1, t0
addi t2, t0, TX_INTERRUPT_FRAME_SIZE
.Lclear_initial_context:
STORE x0, 0(t1)
addi t1, t1, REGBYTES
bltu t1, t2, .Lclear_initial_context
li t1, 1 // Build stack type
STORE t1, 0*REGBYTES(t0) // Place stack type on the top
STORE a1, 30*REGBYTES(t0) // Initial mepc
#if TX_CONTEXT_FP_ENABLED
csrr a1, fcsr
STORE a1, TX_INTERRUPT_FCSR_OFFSET(t0) // Initial fcsr
#endif
/* Setup stack pointer. */
/* thread_ptr -> tx_thread_stack_ptr = t0; */
STORE t0, 2*REGBYTES(a0) // Save stack pointer in thread's
addi t1, x0, 1 // Build ready flag
STORE t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET(a0) // Set ready flag
ret // control block and return
/* } */