aboutsummaryrefslogtreecommitdiff
path: root/test5.c
blob: 6881d371ad96bf5080c694e2a3aa3419ca795d63 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Test Mode/Stack changes
 */
#include "armv7m.h"
#include "testme.h"

static
unsigned testseq;

#define SEQ() __atomic_add_fetch(&testseq, 1, __ATOMIC_RELAXED)

#define CHECK_SEQ(N) testEqI(N, SEQ(), "SEQ " #N)

static inline
void test_equal(const char *m, uint32_t expect, uint32_t actual)
{
    testEqI(expect, actual, "%s", m);
}

extern char _main_stack_top, _proc_stack_top;
extern char _main_stack_bot, _proc_stack_bot;

static
void show_control(unsigned ectrl, unsigned evect)
{
    uint32_t actrl, avect;
    unsigned instack;
    char *sp;
    __asm__ ("mov %0,sp" : "=r"(sp) ::);
    __asm__ ("mrs %0,IPSR" : "=r"(avect) ::);
    __asm__ ("mrs %0,CONTROL" : "=r"(actrl) ::);
    testDiag("SP %p", sp);

    test_equal("CONTROL", ectrl, actrl);
    test_equal("IPSR", evect, avect);

    if(sp>&_main_stack_bot && sp<=&_main_stack_top)
        instack = 0;
    else if(sp>&_proc_stack_bot && sp<=&_proc_stack_top)
        instack = 2;
    else {
        testFail("fail - Corrupt SP %p", sp);
        return;
    }
    testOk((avect>0 && instack==0) || (avect==0 && (actrl&2)==instack),
           "stack%c, avect=%x instack=%x actrl=%x", instack?'2':'0',
           (unsigned)avect, (unsigned)instack, (unsigned)actrl);
}

static
void show_masks(unsigned expect)
{
    uint32_t val, temp;
    __asm__ volatile ("mrs %0,PRIMASK" : "=r"(val)::);
    __asm__ volatile ("mrs %0,FAULTMASK" : "=r"(temp)::);
    val |= temp<<1;
    __asm__ volatile ("mrs %0,BASEPRI" : "=r"(temp)::);
    val |= temp<<2;
    test_equal("masks", expect, val);
}


static
void svc(void)
{
    unsigned test = SEQ();
    printk("# in SVC SEQ %u\n", test);
    switch(test) {
    case 1:
        show_control(0, 11);
        break;
    case 3:
        show_control(0, 11);
        break;
    case 5:
        show_control(1, 11);
        break;
    case 7:
        show_masks(0);
        __asm__ volatile ("cpsid i" :::);
        show_masks(1);
        break;
    default:
        testFail("Fail SVC\n");
        abort();
    }
}

static
void hard(void)
{
    unsigned test = SEQ();
    testDiag("HARDFAULT %u", test);
    if(test==9) {
        testDiag("Set CONTROL=0");
        uint32_t val = 0;
        __asm__ volatile ("msr CONTROL, %0" :: "r"(val):);
    } else {
        testFail("Unexpected HardFault SEQ %u", test);
        abort();
    }
}

void main(void)
{
    run_table.svc = svc;
    run_table.hard = hard;

    testInit(45);
    
    {
        uint32_t temp = (uint32_t)&_proc_stack_top;
        __asm__ volatile ("msr PSP,%0" :: "r"(temp) :);
    }

    testDiag("MSP=%p PSP=%p", &_main_stack_top, &_proc_stack_top);

    show_control(0, 0);

    testDiag("Start, trigger SVC");
    CPSIE(if);
    SVC(42);

    testDiag("Back in main");
    CHECK_SEQ(2);
    show_control(0, 0);

    testDiag("Priv w/ proc stack");
    {
        uint32_t val = 2;
        __asm__ volatile ("msr CONTROL,%0" :: "r"(val):);
    }
    show_control(2, 0);

    testDiag("trigger SVC");
    SVC(42);

    testDiag("Back in main");
    CHECK_SEQ(4);
    show_control(2, 0);

    testDiag("Drop privlage");
    {
        uint32_t val = 3;
        __asm__ volatile ("msr CONTROL,%0" :: "r"(val):);
    }
    show_control(3, 0);

    testDiag("trigger SVC");
    SVC(42);

    testDiag("Back in main");
    CHECK_SEQ(6);
    show_control(3, 0);

    testDiag("Try to restore privlage and switch stack (should be noop)");
    {
        uint32_t val = 0;
        __asm__ volatile ("msr CONTROL,%0" :: "r"(val):);
    }
    show_control(3, 0);

    testDiag("Try to set masks");
    CPSID(if);
    show_masks(0); /* doesn't work */

    testDiag("trigger SVC");
    SVC(42);

    testDiag("Back in main");
    CHECK_SEQ(8);
    show_masks(0); /* unprivlaged doesn't see mask */

    testDiag("trigger HardFault (restores priv)");
    SVC(42);
    testDiag("Back in main");
    CHECK_SEQ(10);
    show_control(2, 0);
    testDiag("restore MSP");
    {
        uint32_t val = 0;
        __asm__ volatile ("msr CONTROL,%0" :: "r"(val):);
    }
    show_control(0, 0);

    /* If we didn't manage to get back into privileged mode
     * for some reason then we're still running on the PSP stack,
     * and trying to return from this function will crash.
     * To avoid that, abort if we're still in user mode.
     */
    {
        uint32_t actrl;
        __asm__ ("mrs %0,CONTROL" : "=r"(actrl) ::);

        if (actrl != 0) {
            testDiag("failed to return to privileged stack: aborting");
            abort();
        }
    }

    testDiag("Done.");
}