aboutsummaryrefslogtreecommitdiff
path: root/test8.c
blob: 5c5d8c9e0c390b1e2e87ef00750da53087b9c82f (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Test MPU
 */
#include "armv7m.h"

char __end_rom, __after_all_load;

char offlimits[1024] __attribute__((aligned(1024)));

char privonly[1024] __attribute__((aligned(1024)));

static
void try(volatile char *p)
{
	uint32_t tval = 0;
	puts("Try ");
	puthex((uint32_t)p);
	putc('\n');
	__asm__ ("mov %r0, #'X'; ldr %r0, [%r1]" : "+r"(tval) : "r"(p) :);
	puts("Got ");
	putc(tval);
	putc('\n');
}

static volatile
unsigned expect_fault = 1;

static
void checkfault(unsigned v)
{
	if(v!=expect_fault) {
		puts("expect_fault ");
		puthex(expect_fault);
		puts(" != ");
		puthex(v);
		putc('\n');
		abort();
	}
}

void hard(uint32_t *sp)
{
	if(expect_fault==10) {
		sp = get_src_stack(sp);
		inst_skip(sp);
		puts("HardFault access offlimits\n");
		try(offlimits);
		expect_fault = 11;
		return;
	}
	puts("Unexpected HardFault!!\n");
	abort();
}

static __attribute__((naked))
void hard_entry(void)
{
	asm("mov r0, sp");
	asm("b hard");
}

void mem(uint32_t *sp)
{
	sp = get_src_stack(sp);
	inst_skip(sp);
	puts("In MemFault\n Addr ");
	puthex(in32((void*)0xe000ed34));
	puts(" From ");
	puthex(sp[6]);
	putc('\n');

	switch(expect_fault) {
	case 2: /* expected */
		expect_fault = 3;
		break;
	case 1:
		puts("Unexpected MemFault!!\n");
		abort();
	default:
		puts("Unexpected state ");
		puthex(expect_fault);
		putc('\n');
		abort();
	}
}

static __attribute__((naked))
void mem_entry(void)
{
	asm("mov r0, sp");
	asm("b mem");
}

static inline
void drop_priv(void)
{
	uint32_t val;
	__asm__ ("mrs %0, CONTROL" : "=r"(val) ::);
	val |= 1;
	__asm__ ("msr CONTROL, %0" :: "r"(val) :);
}

void svc(void *sp)
{
	int num;
	sp = get_src_stack(sp);
	num = get_svc(sp);
	puts("In SVC ");
	if(num<0) {
		puts("BAD\n");
		abort();
	} else {
		puthex(num);
		putc('\n');
	}
	switch(num) {
	case 1: /* restore privlage */
	{
		uint32_t val;
		__asm__ ("mrs %0, CONTROL" : "=r"(val) ::);
		val &= ~1;
		__asm__ ("msr CONTROL, %0" :: "r"(val) :);
	}
		break;
	case 2:
		abort();
	default:
		puts("Unknown\n");
		abort();
	}
}

static __attribute__((naked))
void svc_entry(void)
{
	asm("mov r0, sp");
	asm("b svc");
}

void main(void)
{
	run_table.hard = &hard_entry;
	run_table.svc = &svc_entry;
    run_table.mem = &mem_entry;

	asm("cpsid if");
    out32(SCB(0xd24), 1<<16); // enable MemFault with SHCSR

	puts("1. In Main\n");
	expect_fault = 1;

	/* priority of entries is highest to lowest (0 checked last) */

    set_mpu(0, 0x00000000, (uint32_t)&__end_rom, MPU_NORMAL|MPU_RORO);
    set_mpu(1, 0x20000000, 0x00080000, MPU_NORMAL|MPU_RWRW|MPU_XN);
    set_mpu(2, 0x4000c000, 0x00001000, MPU_DEVICE|MPU_RWRW|MPU_XN);
    set_mpu(3, 0xe000e000, 0x00001000, MPU_DEVICE|MPU_RWRW|MPU_XN);

	/* disable all access to offlimits[] */
	set_mpu(4, (uint32_t)offlimits, sizeof(offlimits),
			MPU_XN|MPU_NORMAL|MPU_NANA);
	/* disable unpriv access to privonly[] */
	set_mpu(5, (uint32_t)privonly, sizeof(privonly),
			MPU_XN|MPU_NORMAL|MPU_RONA);

	puts("2. Access offlimits\n");
	try(offlimits);
	checkfault(1);

	puts("3. Enable MPU\n");
    enable_mpu(1,1,0);

	asm("cpsie if");

	expect_fault = 2;
	puts("4. Access offlimits\n");
	try(offlimits);
	checkfault(3);

	puts("6. In Main\n");
	expect_fault = 1;

	puts("7. Access privonly\n");
	try(privonly);
	checkfault(1);

	puts("8. drop_priv\n");
	expect_fault = 2;
	drop_priv();
	puts("9. Access privonly\n");
	try(privonly);

	puts("11. In Main\n");
	asm("svc 1");
	checkfault(3);
	expect_fault = 1;

	puts("12. Access privonly\n");
	try(privonly);
	checkfault(1);

	puts("13. In Main\n");
	asm("cpsid i");


	puts("14. fault to hardfault\n");
	expect_fault = 10;
	try(offlimits);
	checkfault(11);

	puts("15. Enable MPU w/ HFNMIENA\n");
    enable_mpu(1,1,1);
	puts("16. fault to hardfault (will escalate to unrecoverable)\n");
	expect_fault = 10;
	try(offlimits);

	puts("Shouldn't be here!\n");
}