1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "los_interrupt.h"
32 #include <stdarg.h>
33 #include "securec.h"
34 #include "los_context.h"
35 #include "los_arch_interrupt.h"
36 #include "los_debug.h"
37 #include "los_hook.h"
38 #include "los_task.h"
39 #include "los_sched.h"
40 #include "los_memory.h"
41 #include "los_membox.h"
42 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
43 #include "los_cpup.h"
44 #endif
45
46 UINT32 g_intCount = 0;
47
48 /* *
49 * @ingroup los_hwi
50 * Hardware interrupt form mapping handling function array.
51 */
52 STATIC HWI_PROC_FUNC __attribute__((aligned(LOSCFG_ARCH_HWI_VECTOR_ALIGN))) g_hwiForm[OS_VECTOR_CNT] = {0};
53
54 #if (LOSCFG_DEBUG_TOOLS == 1)
55 STATIC UINT32 g_hwiFormCnt[OS_HWI_MAX_NUM] = {0};
56 STATIC CHAR *g_hwiFormName[OS_HWI_MAX_NUM] = {0};
57
OsGetHwiFormCnt(UINT32 index)58 UINT32 OsGetHwiFormCnt(UINT32 index)
59 {
60 return g_hwiFormCnt[index];
61 }
62
OsGetHwiFormName(UINT32 index)63 CHAR *OsGetHwiFormName(UINT32 index)
64 {
65 return g_hwiFormName[index];
66 }
67
OsGetHwiCreated(UINT32 index)68 BOOL OsGetHwiCreated(UINT32 index)
69 {
70 if (g_hwiForm[index] != (HWI_PROC_FUNC)HalHwiDefaultHandler) {
71 return TRUE;
72 }
73
74 return FALSE;
75 }
76 #endif
77
78 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
79
80 typedef struct {
81 HWI_PROC_FUNC pfnHandler;
82 VOID *pParm;
83 } HWI_HANDLER_FUNC;
84
85 /* *
86 * @ingroup los_hwi
87 * Hardware interrupt handler form mapping handling function array.
88 */
89 STATIC HWI_HANDLER_FUNC g_hwiHandlerForm[OS_VECTOR_CNT] = {{ (HWI_PROC_FUNC)0, (HWI_ARG_T)0 }};
90
91 /* *
92 * @ingroup los_hwi
93 * Set interrupt vector table.
94 */
OsSetVector(UINT32 num,HWI_PROC_FUNC vector,VOID * arg)95 VOID OsSetVector(UINT32 num, HWI_PROC_FUNC vector, VOID *arg)
96 {
97 if ((num + OS_SYS_VECTOR_CNT) < OS_VECTOR_CNT) {
98 g_hwiForm[num + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalInterrupt;
99 g_hwiHandlerForm[num + OS_SYS_VECTOR_CNT].pfnHandler = vector;
100 g_hwiHandlerForm[num + OS_SYS_VECTOR_CNT].pParm = arg;
101 }
102 }
103
104 #else
105 /* *
106 * @ingroup los_hwi
107 * hardware interrupt handler form mapping handling function array.
108 */
109 STATIC HWI_PROC_FUNC g_hwiHandlerForm[OS_VECTOR_CNT] = {0};
110
111 /* *
112 * @ingroup los_hwi
113 * Set interrupt vector table.
114 */
OsSetVector(UINT32 num,HWI_PROC_FUNC vector)115 VOID OsSetVector(UINT32 num, HWI_PROC_FUNC vector)
116 {
117 if ((num + OS_SYS_VECTOR_CNT) < OS_VECTOR_CNT) {
118 g_hwiForm[num + OS_SYS_VECTOR_CNT] = HalInterrupt;
119 g_hwiHandlerForm[num + OS_SYS_VECTOR_CNT] = vector;
120 }
121 }
122 #endif
123
SysTick_Handler(VOID)124 WEAK VOID SysTick_Handler(VOID)
125 {
126 return;
127 }
128
129 /* ****************************************************************************
130 Function : HwiNumGet
131 Description : Get an interrupt number
132 Input : None
133 Output : None
134 Return : Interrupt Indexes number
135 **************************************************************************** */
HwiNumGet(VOID)136 STATIC UINT32 HwiNumGet(VOID)
137 {
138 return __get_IPSR();
139 }
140
HwiUnmask(HWI_HANDLE_T hwiNum)141 STATIC UINT32 HwiUnmask(HWI_HANDLE_T hwiNum)
142 {
143 if (hwiNum >= OS_HWI_MAX_NUM) {
144 return OS_ERRNO_HWI_NUM_INVALID;
145 }
146
147 NVIC_EnableIRQ((IRQn_Type)hwiNum);
148
149 return LOS_OK;
150 }
151
HwiMask(HWI_HANDLE_T hwiNum)152 STATIC UINT32 HwiMask(HWI_HANDLE_T hwiNum)
153 {
154 if (hwiNum >= OS_HWI_MAX_NUM) {
155 return OS_ERRNO_HWI_NUM_INVALID;
156 }
157
158 NVIC_DisableIRQ((IRQn_Type)hwiNum);
159
160 return LOS_OK;
161 }
162
HwiSetPriority(HWI_HANDLE_T hwiNum,UINT8 priority)163 STATIC UINT32 HwiSetPriority(HWI_HANDLE_T hwiNum, UINT8 priority)
164 {
165 if (hwiNum >= OS_HWI_MAX_NUM) {
166 return OS_ERRNO_HWI_NUM_INVALID;
167 }
168
169 if (priority > OS_HWI_PRIO_LOWEST) {
170 return OS_ERRNO_HWI_PRIO_INVALID;
171 }
172
173 NVIC_SetPriority((IRQn_Type)hwiNum, priority);
174
175 return LOS_OK;
176 }
177
HwiPending(HWI_HANDLE_T hwiNum)178 STATIC UINT32 HwiPending(HWI_HANDLE_T hwiNum)
179 {
180 if (hwiNum >= OS_HWI_MAX_NUM) {
181 return OS_ERRNO_HWI_NUM_INVALID;
182 }
183
184 NVIC_SetPendingIRQ((IRQn_Type)hwiNum);
185
186 return LOS_OK;
187 }
188
HwiClear(HWI_HANDLE_T hwiNum)189 STATIC UINT32 HwiClear(HWI_HANDLE_T hwiNum)
190 {
191 if (hwiNum >= OS_HWI_MAX_NUM) {
192 return OS_ERRNO_HWI_NUM_INVALID;
193 }
194
195 NVIC_ClearPendingIRQ((IRQn_Type)hwiNum);
196
197 return LOS_OK;
198 }
199
200 HwiControllerOps g_archHwiOps = {
201 .enableIrq = HwiUnmask,
202 .disableIrq = HwiMask,
203 .setIrqPriority = HwiSetPriority,
204 .getCurIrqNum = HwiNumGet,
205 .triggerIrq = HwiPending,
206 .clearIrq = HwiClear,
207 };
208
ArchIsIntActive(VOID)209 inline UINT32 ArchIsIntActive(VOID)
210 {
211 return (g_intCount > 0);
212 }
213 /* ****************************************************************************
214 Function : HalHwiDefaultHandler
215 Description : default handler of the hardware interrupt
216 Input : None
217 Output : None
218 Return : None
219 **************************************************************************** */
HalHwiDefaultHandler(VOID)220 LITE_OS_SEC_TEXT_MINOR VOID HalHwiDefaultHandler(VOID)
221 {
222 PRINT_ERR("%s irqnum:%u\n", __FUNCTION__, HwiNumGet());
223 while (1) {}
224 }
225
HalPreInterruptHandler(UINT32 arg)226 WEAK VOID HalPreInterruptHandler(UINT32 arg)
227 {
228 (VOID)arg;
229 return;
230 }
231
HalAftInterruptHandler(UINT32 arg)232 WEAK VOID HalAftInterruptHandler(UINT32 arg)
233 {
234 (VOID)arg;
235 return;
236 }
237
238 /* ****************************************************************************
239 Function : HalInterrupt
240 Description : Hardware interrupt entry function
241 Input : None
242 Output : None
243 Return : None
244 **************************************************************************** */
HalInterrupt(VOID)245 LITE_OS_SEC_TEXT VOID HalInterrupt(VOID)
246 {
247 UINT32 hwiIndex;
248 UINT32 intSave;
249
250 #if (LOSCFG_KERNEL_RUNSTOP == 1)
251 SCB->SCR &= (UINT32) ~((UINT32)SCB_SCR_SLEEPDEEP_Msk);
252 #endif
253
254 intSave = LOS_IntLock();
255 g_intCount++;
256 LOS_IntRestore(intSave);
257
258 hwiIndex = HwiNumGet();
259
260 OsHookCall(LOS_HOOK_TYPE_ISR_ENTER, hwiIndex);
261 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
262 OsCpupIrqStart(hwiIndex);
263 #endif
264
265 HalPreInterruptHandler(hwiIndex);
266
267 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
268 if (g_hwiHandlerForm[hwiIndex].pfnHandler != 0) {
269 g_hwiHandlerForm[hwiIndex].pfnHandler((VOID *)g_hwiHandlerForm[hwiIndex].pParm);
270 }
271 #else
272 if (g_hwiHandlerForm[hwiIndex] != 0) {
273 g_hwiHandlerForm[hwiIndex]();
274 }
275 #endif
276
277 #if (LOSCFG_DEBUG_TOOLS == 1)
278 ++g_hwiFormCnt[hwiIndex];
279 #endif
280
281 HalAftInterruptHandler(hwiIndex);
282
283 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
284 OsCpupIrqEnd(hwiIndex);
285 #endif
286
287 OsHookCall(LOS_HOOK_TYPE_ISR_EXIT, hwiIndex);
288
289 intSave = LOS_IntLock();
290 g_intCount--;
291 LOS_IntRestore(intSave);
292 }
293
294 /* ****************************************************************************
295 Function : ArchHwiCreate
296 Description : create hardware interrupt
297 Input : hwiNum --- hwi num to create
298 hwiPrio --- priority of the hwi
299 hwiMode --- unused
300 hwiHandler --- hwi handler
301 irqParam --- param of the hwi handler
302 Output : None
303 Return : LOS_OK on success or error code on failure
304 **************************************************************************** */
ArchHwiCreate(HWI_HANDLE_T hwiNum,HWI_PRIOR_T hwiPrio,HWI_MODE_T hwiMode,HWI_PROC_FUNC hwiHandler,HwiIrqParam * irqParam)305 LITE_OS_SEC_TEXT_INIT UINT32 ArchHwiCreate(HWI_HANDLE_T hwiNum,
306 HWI_PRIOR_T hwiPrio,
307 HWI_MODE_T hwiMode,
308 HWI_PROC_FUNC hwiHandler,
309 HwiIrqParam *irqParam)
310 {
311 (VOID)hwiMode;
312 UINT32 intSave;
313
314 if (hwiHandler == NULL) {
315 return OS_ERRNO_HWI_PROC_FUNC_NULL;
316 }
317
318 if (hwiNum >= OS_HWI_MAX_NUM) {
319 return OS_ERRNO_HWI_NUM_INVALID;
320 }
321
322 if (g_hwiForm[hwiNum + OS_SYS_VECTOR_CNT] != (HWI_PROC_FUNC)HalHwiDefaultHandler) {
323 return OS_ERRNO_HWI_ALREADY_CREATED;
324 }
325
326 if (hwiPrio > OS_HWI_PRIO_LOWEST) {
327 return OS_ERRNO_HWI_PRIO_INVALID;
328 }
329
330 intSave = LOS_IntLock();
331 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
332 if (irqParam != NULL) {
333 OsSetVector(hwiNum, hwiHandler, irqParam->pDevId);
334 } else {
335 OsSetVector(hwiNum, hwiHandler, NULL);
336 }
337 #else
338 (VOID)irqParam;
339 OsSetVector(hwiNum, hwiHandler);
340 #endif
341
342 #if (LOSCFG_DEBUG_TOOLS == 1)
343 if ((irqParam != NULL) && (irqParam->pName != NULL)) {
344 g_hwiFormName[hwiNum + OS_SYS_VECTOR_CNT] = (CHAR *)irqParam->pName;
345 }
346 g_hwiFormCnt[hwiNum + OS_SYS_VECTOR_CNT] = 0;
347 #endif
348
349 HwiUnmask((IRQn_Type)hwiNum);
350 HwiSetPriority((IRQn_Type)hwiNum, hwiPrio);
351
352 LOS_IntRestore(intSave);
353
354 return LOS_OK;
355 }
356
357 /* ****************************************************************************
358 Function : ArchHwiDelete
359 Description : Delete hardware interrupt
360 Input : hwiNum --- hwi num to delete
361 irqParam --- param of the hwi handler
362 Output : None
363 Return : LOS_OK on success or error code on failure
364 **************************************************************************** */
ArchHwiDelete(HWI_HANDLE_T hwiNum,HwiIrqParam * irqParam)365 LITE_OS_SEC_TEXT_INIT UINT32 ArchHwiDelete(HWI_HANDLE_T hwiNum, HwiIrqParam *irqParam)
366 {
367 (VOID)irqParam;
368 UINT32 intSave;
369
370 if (hwiNum >= OS_HWI_MAX_NUM) {
371 return OS_ERRNO_HWI_NUM_INVALID;
372 }
373
374 HwiMask((IRQn_Type)hwiNum);
375
376 intSave = LOS_IntLock();
377
378 g_hwiForm[hwiNum + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalHwiDefaultHandler;
379
380 LOS_IntRestore(intSave);
381
382 return LOS_OK;
383 }
384
385 #define FAULT_STATUS_REG_BIT 32
386 #define USGFAULT (1 << 18)
387 #define BUSFAULT (1 << 17)
388 #define MEMFAULT (1 << 16)
389 #define DIV0FAULT (1 << 4)
390 #define UNALIGNFAULT (1 << 3)
391 #define HARDFAULT_IRQN (-13)
392
393 ExcInfo g_excInfo = {0};
394
395 UINT8 g_uwExcTbl[FAULT_STATUS_REG_BIT] = {
396 0, 0, 0, 0, 0, 0, OS_EXC_UF_DIVBYZERO, OS_EXC_UF_UNALIGNED,
397 0, 0, 0, 0, OS_EXC_UF_NOCP, OS_EXC_UF_INVPC, OS_EXC_UF_INVSTATE, OS_EXC_UF_UNDEFINSTR,
398 0, 0, 0, OS_EXC_BF_STKERR, OS_EXC_BF_UNSTKERR, OS_EXC_BF_IMPRECISERR, OS_EXC_BF_PRECISERR, OS_EXC_BF_IBUSERR,
399 0, 0, 0, OS_EXC_MF_MSTKERR, OS_EXC_MF_MUNSTKERR, 0, OS_EXC_MF_DACCVIOL, OS_EXC_MF_IACCVIOL
400 };
401
402 #if (LOSCFG_KERNEL_PRINTF != 0)
OsExcNvicDump(VOID)403 STATIC VOID OsExcNvicDump(VOID)
404 {
405 #define OS_NR_NVIC_EXC_DUMP_TYPES 7
406 UINT32 *base = NULL;
407 UINT32 len, i, j;
408 UINT32 rgNvicBases[OS_NR_NVIC_EXC_DUMP_TYPES] = {
409 OS_NVIC_SETENA_BASE, OS_NVIC_SETPEND_BASE, OS_NVIC_INT_ACT_BASE,
410 OS_NVIC_PRI_BASE, OS_NVIC_EXCPRI_BASE, OS_NVIC_SHCSR, OS_NVIC_INT_CTRL
411 };
412 UINT32 rgNvicLens[OS_NR_NVIC_EXC_DUMP_TYPES] = {
413 OS_NVIC_INT_ENABLE_SIZE, OS_NVIC_INT_PEND_SIZE, OS_NVIC_INT_ACT_SIZE,
414 OS_NVIC_INT_PRI_SIZE, OS_NVIC_EXCPRI_SIZE, OS_NVIC_SHCSR_SIZE,
415 OS_NVIC_INT_CTRL_SIZE
416 };
417 CHAR strRgEnable[] = "enable";
418 CHAR strRgPending[] = "pending";
419 CHAR strRgActive[] = "active";
420 CHAR strRgPriority[] = "priority";
421 CHAR strRgException[] = "exception";
422 CHAR strRgShcsr[] = "shcsr";
423 CHAR strRgIntCtrl[] = "control";
424 CHAR *strRgs[] = {
425 strRgEnable, strRgPending, strRgActive, strRgPriority,
426 strRgException, strRgShcsr, strRgIntCtrl
427 };
428
429 PRINTK("\r\nOS exception NVIC dump:\n");
430 for (i = 0; i < OS_NR_NVIC_EXC_DUMP_TYPES; i++) {
431 base = (UINT32 *)rgNvicBases[i];
432 len = rgNvicLens[i];
433 PRINTK("interrupt %s register, base address: %p, size: 0x%x\n", strRgs[i], base, len);
434 len = (len >> 2); /* 2: Gets the next register offset */
435 for (j = 0; j < len; j++) {
436 PRINTK("0x%x ", *(base + j));
437 if ((j != 0) && ((j % 16) == 0)) { /* 16: print wrap line */
438 PRINTK("\n");
439 }
440 }
441 PRINTK("\n");
442 }
443 }
444
OsExcTypeInfo(const ExcInfo * excInfo)445 STATIC VOID OsExcTypeInfo(const ExcInfo *excInfo)
446 {
447 CHAR *phaseStr[] = {"exc in init", "exc in task", "exc in hwi"};
448
449 PRINTK("Type = %d\n", excInfo->type);
450 PRINTK("ThrdPid = %d\n", excInfo->thrdPid);
451 PRINTK("Phase = %s\n", phaseStr[excInfo->phase]);
452 PRINTK("FaultAddr = 0x%x\n", excInfo->faultAddr);
453 }
454
OsExcCurTaskInfo(const ExcInfo * excInfo)455 STATIC VOID OsExcCurTaskInfo(const ExcInfo *excInfo)
456 {
457 PRINTK("Current task info:\n");
458 if (excInfo->phase == OS_EXC_IN_TASK) {
459 LosTaskCB *taskCB = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
460 PRINTK("Task name = %s\n", taskCB->taskName);
461 PRINTK("Task ID = %d\n", taskCB->taskID);
462 PRINTK("Task SP = %p\n", taskCB->stackPointer);
463 PRINTK("Task ST = 0x%x\n", taskCB->topOfStack);
464 PRINTK("Task SS = 0x%x\n", taskCB->stackSize);
465 } else if (excInfo->phase == OS_EXC_IN_HWI) {
466 PRINTK("Exception occur in interrupt phase!\n");
467 } else {
468 PRINTK("Exception occur in system init phase!\n");
469 }
470 }
471
OsExcRegInfo(const ExcInfo * excInfo)472 STATIC VOID OsExcRegInfo(const ExcInfo *excInfo)
473 {
474 PRINTK("Exception reg dump:\n");
475 PRINTK("PC = 0x%x\n", excInfo->context->uwPC);
476 PRINTK("LR = 0x%x\n", excInfo->context->uwLR);
477 PRINTK("SP = 0x%x\n", excInfo->context->uwSP);
478 PRINTK("R0 = 0x%x\n", excInfo->context->uwR0);
479 PRINTK("R1 = 0x%x\n", excInfo->context->uwR1);
480 PRINTK("R2 = 0x%x\n", excInfo->context->uwR2);
481 PRINTK("R3 = 0x%x\n", excInfo->context->uwR3);
482 PRINTK("R4 = 0x%x\n", excInfo->context->uwR4);
483 PRINTK("R5 = 0x%x\n", excInfo->context->uwR5);
484 PRINTK("R6 = 0x%x\n", excInfo->context->uwR6);
485 PRINTK("R7 = 0x%x\n", excInfo->context->uwR7);
486 PRINTK("R8 = 0x%x\n", excInfo->context->uwR8);
487 PRINTK("R9 = 0x%x\n", excInfo->context->uwR9);
488 PRINTK("R10 = 0x%x\n", excInfo->context->uwR10);
489 PRINTK("R11 = 0x%x\n", excInfo->context->uwR11);
490 PRINTK("R12 = 0x%x\n", excInfo->context->uwR12);
491 PRINTK("PriMask = 0x%x\n", excInfo->context->uwPriMask);
492 PRINTK("xPSR = 0x%x\n", excInfo->context->uwxPSR);
493 }
494
495 #if (LOSCFG_KERNEL_BACKTRACE == 1)
OsExcBackTraceInfo(const ExcInfo * excInfo)496 STATIC VOID OsExcBackTraceInfo(const ExcInfo *excInfo)
497 {
498 UINTPTR LR[LOSCFG_BACKTRACE_DEPTH] = {0};
499 UINT32 index;
500
501 OsBackTraceHookCall(LR, LOSCFG_BACKTRACE_DEPTH, 0, excInfo->context->uwSP);
502
503 PRINTK("----- backtrace start -----\n");
504 for (index = 0; index < LOSCFG_BACKTRACE_DEPTH; index++) {
505 if (LR[index] == 0) {
506 break;
507 }
508 PRINTK("backtrace %d -- lr = 0x%x\n", index, LR[index]);
509 }
510 PRINTK("----- backtrace end -----\n");
511 }
512 #endif
513
OsExcMemPoolCheckInfo(VOID)514 STATIC VOID OsExcMemPoolCheckInfo(VOID)
515 {
516 PRINTK("\r\nmemory pools check:\n");
517 #if (LOSCFG_PLATFORM_EXC == 1)
518 MemInfoCB memExcInfo[OS_SYS_MEM_NUM];
519 UINT32 errCnt;
520 UINT32 i;
521
522 (VOID)memset_s(memExcInfo, sizeof(memExcInfo), 0, sizeof(memExcInfo));
523
524 errCnt = OsMemExcInfoGet(OS_SYS_MEM_NUM, memExcInfo);
525 if (errCnt < OS_SYS_MEM_NUM) {
526 errCnt += OsMemboxExcInfoGet(OS_SYS_MEM_NUM - errCnt, memExcInfo + errCnt);
527 }
528
529 if (errCnt == 0) {
530 PRINTK("all memory pool check passed!\n");
531 return;
532 }
533
534 for (i = 0; i < errCnt; i++) {
535 PRINTK("pool num = %d\n", i);
536 PRINTK("pool type = %d\n", memExcInfo[i].type);
537 PRINTK("pool addr = 0x%x\n", memExcInfo[i].startAddr);
538 PRINTK("pool size = 0x%x\n", memExcInfo[i].size);
539 PRINTK("pool free = 0x%x\n", memExcInfo[i].free);
540 PRINTK("pool blkNum = %d\n", memExcInfo[i].blockSize);
541 PRINTK("pool error node addr = 0x%x\n", memExcInfo[i].errorAddr);
542 PRINTK("pool error node len = 0x%x\n", memExcInfo[i].errorLen);
543 PRINTK("pool error node owner = %d\n", memExcInfo[i].errorOwner);
544 }
545 #endif
546 UINT32 ret = LOS_MemIntegrityCheck(LOSCFG_SYS_HEAP_ADDR);
547 if (ret == LOS_OK) {
548 PRINTK("system heap memcheck over, all passed!\n");
549 }
550
551 PRINTK("memory pool check end!\n");
552 }
553 #endif
554
OsExcInfoDisplay(const ExcInfo * excInfo)555 STATIC VOID OsExcInfoDisplay(const ExcInfo *excInfo)
556 {
557 #if (LOSCFG_KERNEL_PRINTF != 0)
558 PRINTK("*************Exception Information**************\n");
559 OsExcTypeInfo(excInfo);
560 OsExcCurTaskInfo(excInfo);
561 OsExcRegInfo(excInfo);
562 #if (LOSCFG_KERNEL_BACKTRACE == 1)
563 OsExcBackTraceInfo(excInfo);
564 #endif
565 OsGetAllTskInfo();
566 OsExcNvicDump();
567 OsExcMemPoolCheckInfo();
568 #endif
569 }
570
HalExcHandleEntry(UINT32 excType,UINT32 faultAddr,UINT32 pid,EXC_CONTEXT_S * excBufAddr)571 LITE_OS_SEC_TEXT_INIT VOID HalExcHandleEntry(UINT32 excType, UINT32 faultAddr, UINT32 pid, EXC_CONTEXT_S *excBufAddr)
572 {
573 UINT16 tmpFlag = (excType >> 16) & OS_NULL_SHORT; /* 16: Get Exception Type */
574 g_intCount++;
575 g_excInfo.nestCnt++;
576
577 g_excInfo.type = excType & OS_NULL_SHORT;
578
579 if (tmpFlag & OS_EXC_FLAG_FAULTADDR_VALID) {
580 g_excInfo.faultAddr = faultAddr;
581 } else {
582 g_excInfo.faultAddr = OS_EXC_IMPRECISE_ACCESS_ADDR;
583 }
584 if (g_losTask.runTask != NULL) {
585 if (tmpFlag & OS_EXC_FLAG_IN_HWI) {
586 g_excInfo.phase = OS_EXC_IN_HWI;
587 g_excInfo.thrdPid = pid;
588 } else {
589 g_excInfo.phase = OS_EXC_IN_TASK;
590 g_excInfo.thrdPid = g_losTask.runTask->taskID;
591 }
592 } else {
593 g_excInfo.phase = OS_EXC_IN_INIT;
594 g_excInfo.thrdPid = OS_NULL_INT;
595 }
596 if (excType & OS_EXC_FLAG_NO_FLOAT) {
597 g_excInfo.context = (EXC_CONTEXT_S *)((CHAR *)excBufAddr - LOS_OFF_SET_OF(EXC_CONTEXT_S, uwR4));
598 } else {
599 g_excInfo.context = excBufAddr;
600 }
601
602 OsDoExcHook(EXC_INTERRUPT);
603 OsExcInfoDisplay(&g_excInfo);
604 ArchSysExit();
605 }
606
607 /* stack protector */
608 WEAK UINT32 __stack_chk_guard = 0xd00a0dff;
609
__stack_chk_fail(VOID)610 WEAK VOID __stack_chk_fail(VOID)
611 {
612 /* __builtin_return_address is a builtin function, building in gcc */
613 LOS_Panic("stack-protector: Kernel stack is corrupted in: %p\n",
614 __builtin_return_address(0));
615 }
616
617 /* ****************************************************************************
618 Function : HalHwiInit
619 Description : initialization of the hardware interrupt
620 Input : None
621 Output : None
622 Return : None
623 **************************************************************************** */
HalHwiInit(VOID)624 LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID)
625 {
626 #if (LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT == 1)
627 UINT32 index;
628 g_hwiForm[0] = 0; /* [0] Top of Stack */
629 g_hwiForm[1] = (HWI_PROC_FUNC)Reset_Handler; /* [1] reset */
630 for (index = 2; index < OS_VECTOR_CNT; index++) { /* 2: The starting position of the interrupt */
631 g_hwiForm[index] = (HWI_PROC_FUNC)HalHwiDefaultHandler;
632 }
633 /* Exception handler register */
634 g_hwiForm[NonMaskableInt_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcNMI;
635 g_hwiForm[HARDFAULT_IRQN + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcHardFault;
636 g_hwiForm[MemoryManagement_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcMemFault;
637 g_hwiForm[BusFault_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcBusFault;
638 g_hwiForm[UsageFault_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcUsageFault;
639 g_hwiForm[SVCall_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcSvcCall;
640 g_hwiForm[PendSV_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalPendSV;
641 g_hwiForm[SysTick_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)SysTick_Handler;
642
643 /* Interrupt vector table location */
644 SCB->VTOR = (UINT32)(UINTPTR)g_hwiForm;
645 #endif
646 #if (__CORTEX_M >= 0x03U) /* only for Cortex-M3 and above */
647 NVIC_SetPriorityGrouping(OS_NVIC_AIRCR_PRIGROUP);
648 #endif
649
650 /* Enable USGFAULT, BUSFAULT, MEMFAULT */
651 *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT);
652
653 /* Enable DIV 0 and unaligned exception */
654 #ifdef LOSCFG_ARCH_UNALIGNED_EXC
655 *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT);
656 #else
657 *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT);
658 #endif
659
660 return;
661 }
662
663