1 /*
2  * Copyright (c) 2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Common fault handler for ARCv2
10  *
11  * Common fault handler for ARCv2 processors.
12  */
13 
14 #include <zephyr/toolchain.h>
15 #include <zephyr/linker/sections.h>
16 #include <inttypes.h>
17 
18 #include <zephyr/kernel.h>
19 #include <kernel_internal.h>
20 #include <zephyr/kernel_structs.h>
21 #include <zephyr/arch/common/exc_handle.h>
22 #include <zephyr/logging/log.h>
23 
24 LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
25 
26 #ifdef CONFIG_USERSPACE
27 Z_EXC_DECLARE(z_arc_user_string_nlen);
28 
29 static const struct z_exc_handle exceptions[] = {
30 	Z_EXC_HANDLE(z_arc_user_string_nlen)
31 };
32 #endif
33 
34 #if defined(CONFIG_MPU_STACK_GUARD)
35 /**
36  * @brief Assess occurrence of current thread's stack corruption
37  *
38  * This function performs an assessment whether a memory fault (on a given
39  * memory address) is the result of a stack overflow of the current thread.
40  *
41  * When called, we know at this point that we received an ARC
42  * protection violation, with any cause code, with the protection access
43  * error either "MPU" or "Secure MPU". In other words, an MPU fault of
44  * some kind. Need to determine whether this is a general MPU access
45  * exception or the specific case of a stack overflow.
46  *
47  * @param fault_addr memory address on which memory access violation
48  *                   has been reported.
49  * @param sp stack pointer when exception comes out
50  * @retval True if this appears to be a stack overflow
51  * @retval False if this does not appear to be a stack overflow
52  */
z_check_thread_stack_fail(const uint32_t fault_addr,uint32_t sp)53 static bool z_check_thread_stack_fail(const uint32_t fault_addr, uint32_t sp)
54 {
55 #if defined(CONFIG_MULTITHREADING)
56 	uint32_t guard_end, guard_start;
57 	const struct k_thread *thread = _current;
58 
59 	if (!thread) {
60 		/* TODO: Under what circumstances could we get here ? */
61 		return false;
62 	}
63 
64 #ifdef CONFIG_USERSPACE
65 	if ((thread->base.user_options & K_USER) != 0) {
66 		if ((z_arc_v2_aux_reg_read(_ARC_V2_ERSTATUS) &
67 		     _ARC_V2_STATUS32_U) != 0) {
68 			/* Normal user mode context. There is no specific
69 			 * "guard" installed in this case, instead what's
70 			 * happening is that the stack pointer is crashing
71 			 * into the privilege mode stack buffer which
72 			 * immediately precedes it.
73 			 */
74 			guard_end = thread->stack_info.start;
75 			guard_start = (uint32_t)thread->stack_obj;
76 		} else {
77 			/* Special case: handling a syscall on privilege stack.
78 			 * There is guard memory reserved immediately before
79 			 * it.
80 			 */
81 			guard_end = thread->arch.priv_stack_start;
82 			guard_start = guard_end - Z_ARC_STACK_GUARD_SIZE;
83 		}
84 	} else
85 #endif /* CONFIG_USERSPACE */
86 	{
87 		/* Supervisor thread */
88 		guard_end = thread->stack_info.start;
89 		guard_start = guard_end - Z_ARC_STACK_GUARD_SIZE;
90 	}
91 
92 	 /* treat any MPU exceptions within the guard region as a stack
93 	  * overflow.As some instrustions
94 	  * (like enter_s {r13-r26, fp, blink}) push a collection of
95 	  * registers on to the stack. In this situation, the fault_addr
96 	  * will less than guard_end, but sp will greater than guard_end.
97 	  */
98 	if (fault_addr < guard_end && fault_addr >= guard_start) {
99 		return true;
100 	}
101 #endif /* CONFIG_MULTITHREADING */
102 
103 	return false;
104 }
105 #endif
106 
107 #ifdef CONFIG_EXCEPTION_DEBUG
108 /* For EV_ProtV, the numbering/semantics of the parameter are consistent across
109  * several codes, although not all combination will be reported.
110  *
111  * These codes and parameters do not have associated* names in
112  * the technical manual, just switch on the values in Table 6-5
113  */
get_protv_access_err(uint32_t parameter)114 static const char *get_protv_access_err(uint32_t parameter)
115 {
116 	switch (parameter) {
117 	case 0x1:
118 		return "code protection scheme";
119 	case 0x2:
120 		return "stack checking scheme";
121 	case 0x4:
122 		return "MPU";
123 	case 0x8:
124 		return "MMU";
125 	case 0x10:
126 		return "NVM";
127 	case 0x24:
128 		return "Secure MPU";
129 	case 0x44:
130 		return "Secure MPU with SID mismatch";
131 	default:
132 		return "unknown";
133 	}
134 }
135 
dump_protv_exception(uint32_t cause,uint32_t parameter)136 static void dump_protv_exception(uint32_t cause, uint32_t parameter)
137 {
138 	switch (cause) {
139 	case 0x0:
140 		EXCEPTION_DUMP("Instruction fetch violation (%s)",
141 			get_protv_access_err(parameter));
142 		break;
143 	case 0x1:
144 		EXCEPTION_DUMP("Memory read protection violation (%s)",
145 			get_protv_access_err(parameter));
146 		break;
147 	case 0x2:
148 		EXCEPTION_DUMP("Memory write protection violation (%s)",
149 			get_protv_access_err(parameter));
150 		break;
151 	case 0x3:
152 		EXCEPTION_DUMP("Memory read-modify-write violation (%s)",
153 			get_protv_access_err(parameter));
154 		break;
155 	case 0x10:
156 		EXCEPTION_DUMP("Normal vector table in secure memory");
157 		break;
158 	case 0x11:
159 		EXCEPTION_DUMP("NS handler code located in S memory");
160 		break;
161 	case 0x12:
162 		EXCEPTION_DUMP("NSC Table Range Violation");
163 		break;
164 	default:
165 		EXCEPTION_DUMP("unknown");
166 		break;
167 	}
168 }
169 
dump_machine_check_exception(uint32_t cause,uint32_t parameter)170 static void dump_machine_check_exception(uint32_t cause, uint32_t parameter)
171 {
172 	switch (cause) {
173 	case 0x0:
174 		EXCEPTION_DUMP("double fault");
175 		break;
176 	case 0x1:
177 		EXCEPTION_DUMP("overlapping TLB entries");
178 		break;
179 	case 0x2:
180 		EXCEPTION_DUMP("fatal TLB error");
181 		break;
182 	case 0x3:
183 		EXCEPTION_DUMP("fatal cache error");
184 		break;
185 	case 0x4:
186 		EXCEPTION_DUMP("internal memory error on instruction fetch");
187 		break;
188 	case 0x5:
189 		EXCEPTION_DUMP("internal memory error on data fetch");
190 		break;
191 	case 0x6:
192 		EXCEPTION_DUMP("illegal overlapping MPU entries");
193 		if (parameter == 0x1) {
194 			EXCEPTION_DUMP(" - jump and branch target");
195 		}
196 		break;
197 	case 0x10:
198 		EXCEPTION_DUMP("secure vector table not located in secure memory");
199 		break;
200 	case 0x11:
201 		EXCEPTION_DUMP("NSC jump table not located in secure memory");
202 		break;
203 	case 0x12:
204 		EXCEPTION_DUMP("secure handler code not located in secure memory");
205 		break;
206 	case 0x13:
207 		EXCEPTION_DUMP("NSC target address not located in secure memory");
208 		break;
209 	case 0x80:
210 		EXCEPTION_DUMP("uncorrectable ECC or parity error in vector memory");
211 		break;
212 	default:
213 		EXCEPTION_DUMP("unknown");
214 		break;
215 	}
216 }
217 
dump_privilege_exception(uint32_t cause,uint32_t parameter)218 static void dump_privilege_exception(uint32_t cause, uint32_t parameter)
219 {
220 	switch (cause) {
221 	case 0x0:
222 		EXCEPTION_DUMP("Privilege violation");
223 		break;
224 	case 0x1:
225 		EXCEPTION_DUMP("disabled extension");
226 		break;
227 	case 0x2:
228 		EXCEPTION_DUMP("action point hit");
229 		break;
230 	case 0x10:
231 		switch (parameter) {
232 		case 0x1:
233 			EXCEPTION_DUMP("N to S return using incorrect return mechanism");
234 			break;
235 		case 0x2:
236 			EXCEPTION_DUMP("N to S return with incorrect operating mode");
237 			break;
238 		case 0x3:
239 			EXCEPTION_DUMP("IRQ/exception return fetch from wrong mode");
240 			break;
241 		case 0x4:
242 			EXCEPTION_DUMP("attempt to halt secure processor in NS mode");
243 			break;
244 		case 0x20:
245 			EXCEPTION_DUMP("attempt to access secure resource from normal mode");
246 			break;
247 		case 0x40:
248 			EXCEPTION_DUMP("SID violation on resource access (APEX/UAUX/key NVM)");
249 			break;
250 		default:
251 			EXCEPTION_DUMP("unknown");
252 			break;
253 		}
254 		break;
255 	case 0x13:
256 		switch (parameter) {
257 		case 0x20:
258 			EXCEPTION_DUMP("attempt to access secure APEX feature from NS mode");
259 			break;
260 		case 0x40:
261 			EXCEPTION_DUMP("SID violation on access to APEX feature");
262 			break;
263 		default:
264 			EXCEPTION_DUMP("unknown");
265 			break;
266 		}
267 		break;
268 	default:
269 		EXCEPTION_DUMP("unknown");
270 		break;
271 	}
272 }
273 
dump_exception_info(uint32_t vector,uint32_t cause,uint32_t parameter)274 static void dump_exception_info(uint32_t vector, uint32_t cause, uint32_t parameter)
275 {
276 	if (vector >= 0x10 && vector <= 0xFF) {
277 		EXCEPTION_DUMP("interrupt %u", vector);
278 		return;
279 	}
280 
281 	/* Names are exactly as they appear in Designware ARCv2 ISA
282 	 * Programmer's reference manual for easy searching
283 	 */
284 	switch (vector) {
285 	case ARC_EV_RESET:
286 		EXCEPTION_DUMP("Reset");
287 		break;
288 	case ARC_EV_MEM_ERROR:
289 		EXCEPTION_DUMP("Memory Error");
290 		break;
291 	case ARC_EV_INS_ERROR:
292 		EXCEPTION_DUMP("Instruction Error");
293 		break;
294 	case ARC_EV_MACHINE_CHECK:
295 		EXCEPTION_DUMP("EV_MachineCheck");
296 		dump_machine_check_exception(cause, parameter);
297 		break;
298 	case ARC_EV_TLB_MISS_I:
299 		EXCEPTION_DUMP("EV_TLBMissI");
300 		break;
301 	case ARC_EV_TLB_MISS_D:
302 		EXCEPTION_DUMP("EV_TLBMissD");
303 		break;
304 	case ARC_EV_PROT_V:
305 		EXCEPTION_DUMP("EV_ProtV");
306 		dump_protv_exception(cause, parameter);
307 		break;
308 	case ARC_EV_PRIVILEGE_V:
309 		EXCEPTION_DUMP("EV_PrivilegeV");
310 		dump_privilege_exception(cause, parameter);
311 		break;
312 	case ARC_EV_SWI:
313 		EXCEPTION_DUMP("EV_SWI");
314 		break;
315 	case ARC_EV_TRAP:
316 		EXCEPTION_DUMP("EV_Trap");
317 		break;
318 	case ARC_EV_EXTENSION:
319 		EXCEPTION_DUMP("EV_Extension");
320 		break;
321 	case ARC_EV_DIV_ZERO:
322 		EXCEPTION_DUMP("EV_DivZero");
323 		break;
324 	case ARC_EV_DC_ERROR:
325 		EXCEPTION_DUMP("EV_DCError");
326 		break;
327 	case ARC_EV_MISALIGNED:
328 		EXCEPTION_DUMP("EV_Misaligned");
329 		break;
330 	case ARC_EV_VEC_UNIT:
331 		EXCEPTION_DUMP("EV_VecUnit");
332 		break;
333 	default:
334 		EXCEPTION_DUMP("unknown");
335 		break;
336 	}
337 }
338 #endif /* CONFIG_EXCEPTION_DEBUG */
339 
340 /*
341  * @brief Fault handler
342  *
343  * This routine is called when fatal error conditions are detected by hardware
344  * and is responsible only for reporting the error. Once reported, it then
345  * invokes the user provided routine k_sys_fatal_error_handler() which is
346  * responsible for implementing the error handling policy.
347  */
z_arc_fault(struct arch_esf * esf,uint32_t old_sp)348 void z_arc_fault(struct arch_esf *esf, uint32_t old_sp)
349 {
350 	uint32_t vector, cause, parameter;
351 	uint32_t exc_addr = z_arc_v2_aux_reg_read(_ARC_V2_EFA);
352 	uint32_t ecr = z_arc_v2_aux_reg_read(_ARC_V2_ECR);
353 
354 #ifdef CONFIG_USERSPACE
355 	for (int i = 0; i < ARRAY_SIZE(exceptions); i++) {
356 		uint32_t start = (uint32_t)exceptions[i].start;
357 		uint32_t end = (uint32_t)exceptions[i].end;
358 
359 		if (esf->pc >= start && esf->pc < end) {
360 			esf->pc = (uint32_t)(exceptions[i].fixup);
361 			return;
362 		}
363 	}
364 #endif
365 
366 	vector = Z_ARC_V2_ECR_VECTOR(ecr);
367 	cause =  Z_ARC_V2_ECR_CODE(ecr);
368 	parameter = Z_ARC_V2_ECR_PARAMETER(ecr);
369 
370 	/* exception raised by kernel */
371 	if (vector == ARC_EV_TRAP && parameter == _TRAP_S_CALL_RUNTIME_EXCEPT) {
372 		/*
373 		 * in user mode software-triggered system fatal exceptions only allow
374 		 * K_ERR_KERNEL_OOPS and K_ERR_STACK_CHK_FAIL
375 		 */
376 #ifdef CONFIG_USERSPACE
377 		if ((esf->status32 & _ARC_V2_STATUS32_U) &&
378 			esf->r0 != K_ERR_STACK_CHK_FAIL) {
379 			esf->r0 = K_ERR_KERNEL_OOPS;
380 		}
381 #endif
382 
383 		z_arc_fatal_error(esf->r0, esf);
384 		return;
385 	}
386 
387 #ifdef CONFIG_EXCEPTION_DEBUG
388 	EXCEPTION_DUMP("***** Exception vector: 0x%x, cause code: 0x%x, parameter 0x%x",
389 		vector, cause, parameter);
390 	EXCEPTION_DUMP("Address 0x%x", exc_addr);
391 
392 	dump_exception_info(vector, cause, parameter);
393 #endif
394 
395 #ifdef CONFIG_ARC_STACK_CHECKING
396 	/* Vector 6 = EV_ProV. Regardless of cause, parameter 2 means stack
397 	 * check violation
398 	 * stack check and mpu violation can come out together, then
399 	 * parameter = 0x2 | [0x4 | 0x8 | 0x1]
400 	 */
401 	if (vector == ARC_EV_PROT_V && parameter & 0x2) {
402 		z_arc_fatal_error(K_ERR_STACK_CHK_FAIL, esf);
403 		return;
404 	}
405 #endif
406 
407 #ifdef CONFIG_MPU_STACK_GUARD
408 	if (vector == ARC_EV_PROT_V && ((parameter == 0x4) ||
409 					(parameter == 0x24))) {
410 		if (z_check_thread_stack_fail(exc_addr, old_sp)) {
411 			z_arc_fatal_error(K_ERR_STACK_CHK_FAIL, esf);
412 			return;
413 		}
414 	}
415 #endif
416 	z_arc_fatal_error(K_ERR_CPU_EXCEPTION, esf);
417 }
418