summaryrefslogtreecommitdiff
path: root/src/boot/libu64/stackcheck.c
blob: 8b07188cdc49800afa25018cfca6f701c32e64e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "libu64/stackcheck.h"

#include "macros.h"
#include "stdbool.h"
#include "stdint.h"

#include "versions.h"

#if MM_VERSION < N64_US || DEBUG_FEATURES
#define STACKCHECK_PRINTF osSyncPrintf
#elif IDO_PRINTF_WORKAROUND
#define STACKCHECK_PRINTF(args) (void)0
#else
#define STACKCHECK_PRINTF(format, ...) (void)0
#endif

StackEntry* sStackInfoListStart = NULL;
StackEntry* sStackInfoListEnd = NULL;

void StackCheck_Init(StackEntry* entry, void* stackBottom, void* stackTop, u32 initValue, s32 minSpace,
                     const char* name) {
    if (entry == NULL) {
        sStackInfoListStart = NULL;
    } else {
        StackEntry* iter;

        entry->head = stackBottom;
        entry->tail = stackTop;
        entry->initValue = initValue;
        entry->minSpace = minSpace;
        entry->name = name;

        for (iter = sStackInfoListStart; iter != NULL; iter = iter->next) {
            if (iter == entry) {
                STACKCHECK_PRINTF(
                    T("stackcheck_init: %08x は既にリスト中にある\n", "stackcheck_init: %08x is already in the list\n"),
                    entry);
                return;
            }
        }

        entry->prev = sStackInfoListEnd;
        entry->next = NULL;

        if (sStackInfoListEnd) {
            sStackInfoListEnd->next = entry;
        }

        sStackInfoListEnd = entry;
        if (sStackInfoListStart == NULL) {
            sStackInfoListStart = entry;
        }

        if (entry->minSpace != -1) {
            u32* addr = entry->head;

            while (addr < (u32*)entry->tail) {
                *addr++ = entry->initValue;
            }
        }
    }
}

void StackCheck_Cleanup(StackEntry* entry) {
    u32 inconsistency = false;

    if (entry->prev == NULL) {
        if (entry == sStackInfoListStart) {
            sStackInfoListStart = entry->next;
        } else {
            inconsistency = true;
        }
    } else {
        entry->prev->next = entry->next;
    }

    if (!entry->next) {
        if (entry == sStackInfoListEnd) {
            sStackInfoListEnd = entry->prev;
        } else {
            inconsistency = true;
        }
    }

    if (inconsistency) {
        STACKCHECK_PRINTF(
            T("stackcheck_cleanup: %08x リスト不整合です\n", "stackcheck_cleanup: %08x list inconsistency\n"), entry);
    }
}

StackStatus StackCheck_GetState(StackEntry* entry) {
    u32* last;
    size_t used;
    size_t free;
    StackStatus status;

    for (last = entry->head; last < (u32*)entry->tail; last++) {
        if (entry->initValue != *last) {
            break;
        }
    }

    used = (uintptr_t)entry->tail - (uintptr_t)last;
    free = (uintptr_t)last - (uintptr_t)entry->head;

    if (free == 0) {
        status = STACK_STATUS_OVERFLOW;
    } else if ((free < (size_t)entry->minSpace) && (entry->minSpace != -1)) {
        status = STACK_STATUS_WARNING;
    } else {
        status = STACK_STATUS_OK;
    }

    STACKCHECK_PRINTF("head=%08x tail=%08x last=%08x used=%08x free=%08x [%s]\n", entry->head, entry->tail, last, used,
                      free, (entry->name != NULL) ? entry->name : "(null)");

#if MM_VERSION >= N64_US
    (void)"(null)";
#endif

    return status;
}

u32 StackCheck_CheckAll(void) {
    u32 ret = 0;
    StackEntry* iter = sStackInfoListStart;

    while (iter != NULL) {
        StackStatus state = StackCheck_GetState(iter);

        if (state != STACK_STATUS_OK) {
            ret = 1;
        }
        iter = iter->next;
    }

    return ret;
}

u32 StackCheck_Check(StackEntry* entry) {
    if (entry == NULL) {
        return StackCheck_CheckAll();
    } else {
        return StackCheck_GetState(entry);
    }
}