KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 arch/dreamcast/include/arch/stack.h
4 (c)2002 Megan Potter
5 (c)2025 Eric Fradella
6
7*/
8
9/** \file arch/stack.h
10 \brief Stack functions
11 \ingroup debugging_stacktrace
12
13 This file contains arch-specific stack implementations, including defining
14 stack sizes and alignments, as well as functions for stack tracing and
15 debugging. On Dreamcast, the stack tracing functions only work if frame
16 pointers have been enabled at compile time (-DFRAME_POINTERS and no
17 -fomit-frame-pointer flag).
18
19 \author Megan Potter
20 \author Eric Fradella
21*/
22
23#ifndef __ARCH_STACK_H
24#define __ARCH_STACK_H
25
26#include <kos/cdefs.h>
27__BEGIN_DECLS
28
29#include <stdint.h>
30#include <kos/thread.h>
31
32/** \defgroup debugging_stacktrace Stack Traces
33 \brief API for managing stack backtracing
34 \ingroup debugging
35
36 @{
37*/
38
39#ifndef THD_STACK_ALIGNMENT
40/** \brief Required alignment for stack. */
41#define THD_STACK_ALIGNMENT 8
42#endif
43
44#ifndef THD_STACK_SIZE
45/** \brief Default thread stack size. */
46#define THD_STACK_SIZE 32768
47#endif
48
49#ifndef THD_KERNEL_STACK_SIZE
50/** \brief Main/kernel thread's stack size. */
51#define THD_KERNEL_STACK_SIZE (64 * 1024)
52#endif
53
54/** \brief DC specific "function" to get the return address from the current
55 function.
56
57 \return The return address of the current function.
58*/
59static __always_inline uintptr_t arch_get_ret_addr(void) {
60 uintptr_t pr;
61
62 __asm__ __volatile__("sts pr,%0\n" : "=r"(pr));
63
64 return pr;
65}
66
67/* Please note that all of the following frame pointer macros are ONLY
68 valid if you have compiled your code WITHOUT -fomit-frame-pointer. These
69 are mainly useful for getting a stack trace from an error. */
70
71/** \brief DC specific "function" to get the frame pointer from the current
72 function.
73
74 \return The frame pointer from the current function.
75 \note This only works if you don't disable frame pointers.
76*/
77static __always_inline uintptr_t arch_get_fptr(void) {
78 register uintptr_t fp __asm__("r14");
79
80 return fp;
81}
82
83/** \brief Pass in a frame pointer value to get the return address for the
84 given frame.
85
86 \param fptr The frame pointer to look at.
87 \return The return address of the pointer.
88*/
89static inline uintptr_t arch_fptr_ret_addr(uintptr_t fptr) {
90 return *(uintptr_t *)fptr;
91}
92
93/** \brief Pass in a frame pointer value to get the previous frame pointer for
94 the given frame.
95
96 \param fptr The frame pointer to look at.
97 \return The previous frame pointer.
98*/
99static inline uintptr_t arch_fptr_next(uintptr_t fptr) {
100 return arch_fptr_ret_addr(fptr + 4);
101}
102
103/** \brief Set up new stack before running.
104
105 This function does nothing as it is unnecessary on Dreamcast.
106
107 \param nt A pointer to the new thread for which a stack
108 is to be set up.
109*/
111
112/** \brief Do a stack trace from the current function.
113
114 This function does a stack trace from the current function, printing the
115 results to stdout. This is used, for instance, when an assertion fails in
116 assert().
117
118 \param n The number of frames to leave off. Each frame is a
119 jump to subroutine or branch to subroutine. assert()
120 leaves off 2 frames, for reference.
121*/
122void arch_stk_trace(int n);
123
124/** \brief Do a stack trace from the current function.
125
126 This function does a stack trace from the the specified frame pointer,
127 printing the results to stdout. This could be used for doing something like
128 stack tracing a main thread from inside an IRQ handler.
129
130 \param fp The frame pointer to start from.
131 \param n The number of frames to leave off.
132*/
133void arch_stk_trace_at(uint32_t fp, size_t n);
134
135/** @} */
136
137__END_DECLS
138
139#endif /* __ARCH_EXEC_H */
140
Various common macros used throughout the codebase.
void arch_stk_trace_at(uint32_t fp, size_t n)
Do a stack trace from the current function.
static __always_inline uintptr_t arch_get_fptr(void)
DC specific "function" to get the frame pointer from the current function.
Definition stack.h:77
static uintptr_t arch_fptr_ret_addr(uintptr_t fptr)
Pass in a frame pointer value to get the return address for the given frame.
Definition stack.h:89
static __always_inline uintptr_t arch_get_ret_addr(void)
DC specific "function" to get the return address from the current function.
Definition stack.h:59
void arch_stk_trace(int n)
Do a stack trace from the current function.
static uintptr_t arch_fptr_next(uintptr_t fptr)
Pass in a frame pointer value to get the previous frame pointer for the given frame.
Definition stack.h:99
void arch_stk_setup(kthread_t *nt)
Set up new stack before running.
static uint32_t("Please see purupuru_effect_t for modern equivalent.") PURUPURU_EFFECT2_UINTENSITY(uint8_t x)
Definition purupuru.h:96
Structure describing one running thread.
Definition thread.h:164
Threading support.