• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
124 /* ****************************************************************************
125  Function    : HwiNumGet
126  Description : Get an interrupt number
127  Input       : None
128  Output      : None
129  Return      : Interrupt Indexes number
130  **************************************************************************** */
HwiNumGet(VOID)131 STATIC UINT32 HwiNumGet(VOID)
132 {
133     return __get_IPSR();
134 }
135 
HwiUnmask(HWI_HANDLE_T hwiNum)136 STATIC UINT32 HwiUnmask(HWI_HANDLE_T hwiNum)
137 {
138     if (hwiNum >= OS_HWI_MAX_NUM) {
139         return OS_ERRNO_HWI_NUM_INVALID;
140     }
141 
142     NVIC_EnableIRQ((IRQn_Type)hwiNum);
143 
144     return LOS_OK;
145 }
146 
HwiMask(HWI_HANDLE_T hwiNum)147 STATIC UINT32 HwiMask(HWI_HANDLE_T hwiNum)
148 {
149     if (hwiNum >= OS_HWI_MAX_NUM) {
150         return OS_ERRNO_HWI_NUM_INVALID;
151     }
152 
153     NVIC_DisableIRQ((IRQn_Type)hwiNum);
154 
155     return LOS_OK;
156 }
157 
HwiSetPriority(HWI_HANDLE_T hwiNum,UINT8 priority)158 STATIC UINT32 HwiSetPriority(HWI_HANDLE_T hwiNum, UINT8 priority)
159 {
160     if (hwiNum >= OS_HWI_MAX_NUM) {
161         return OS_ERRNO_HWI_NUM_INVALID;
162     }
163 
164     if (priority > OS_HWI_PRIO_LOWEST) {
165         return OS_ERRNO_HWI_PRIO_INVALID;
166     }
167 
168     NVIC_SetPriority((IRQn_Type)hwiNum, priority);
169 
170     return LOS_OK;
171 }
172 
HwiPending(HWI_HANDLE_T hwiNum)173 STATIC UINT32 HwiPending(HWI_HANDLE_T hwiNum)
174 {
175     if (hwiNum >= OS_HWI_MAX_NUM) {
176         return OS_ERRNO_HWI_NUM_INVALID;
177     }
178 
179     NVIC_SetPendingIRQ((IRQn_Type)hwiNum);
180 
181     return LOS_OK;
182 }
183 
HwiClear(HWI_HANDLE_T hwiNum)184 STATIC UINT32 HwiClear(HWI_HANDLE_T hwiNum)
185 {
186     if (hwiNum >= OS_HWI_MAX_NUM) {
187         return OS_ERRNO_HWI_NUM_INVALID;
188     }
189 
190     NVIC_ClearPendingIRQ((IRQn_Type)hwiNum);
191 
192     return LOS_OK;
193 }
194 
195 HwiControllerOps g_archHwiOps = {
196     .enableIrq      = HwiUnmask,
197     .disableIrq     = HwiMask,
198     .setIrqPriority = HwiSetPriority,
199     .getCurIrqNum   = HwiNumGet,
200     .triggerIrq     = HwiPending,
201     .clearIrq       = HwiClear,
202 };
203 
ArchIsIntActive(VOID)204 inline UINT32 ArchIsIntActive(VOID)
205 {
206     return (g_intCount > 0);
207 }
208 /* ****************************************************************************
209  Function    : HalHwiDefaultHandler
210  Description : default handler of the hardware interrupt
211  Input       : None
212  Output      : None
213  Return      : None
214  **************************************************************************** */
HalHwiDefaultHandler(VOID)215 LITE_OS_SEC_TEXT_MINOR VOID HalHwiDefaultHandler(VOID)
216 {
217     PRINT_ERR("%s irqnum:%u\n", __FUNCTION__, HwiNumGet());
218     while (1) {}
219 }
220 
HalPreInterruptHandler(UINT32 arg)221 WEAK VOID HalPreInterruptHandler(UINT32 arg)
222 {
223     (VOID)arg;
224     return;
225 }
226 
HalAftInterruptHandler(UINT32 arg)227 WEAK VOID HalAftInterruptHandler(UINT32 arg)
228 {
229     (VOID)arg;
230     return;
231 }
232 
233 /* ****************************************************************************
234  Function    : HalInterrupt
235  Description : Hardware interrupt entry function
236  Input       : None
237  Output      : None
238  Return      : None
239  **************************************************************************** */
HalInterrupt(VOID)240 LITE_OS_SEC_TEXT VOID HalInterrupt(VOID)
241 {
242     UINT32 hwiIndex;
243     UINT32 intSave;
244 
245 #if (LOSCFG_KERNEL_RUNSTOP == 1)
246     SCB->SCR &= (UINT32) ~((UINT32)SCB_SCR_SLEEPDEEP_Msk);
247 #endif
248 
249     intSave = LOS_IntLock();
250     g_intCount++;
251     LOS_IntRestore(intSave);
252 
253     hwiIndex = HwiNumGet();
254 
255     OsHookCall(LOS_HOOK_TYPE_ISR_ENTER, hwiIndex);
256 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
257     OsCpupIrqStart(hwiIndex);
258 #endif
259 
260     HalPreInterruptHandler(hwiIndex);
261 
262 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
263     if (g_hwiHandlerForm[hwiIndex].pfnHandler != 0) {
264         g_hwiHandlerForm[hwiIndex].pfnHandler((VOID *)g_hwiHandlerForm[hwiIndex].pParm);
265     }
266 #else
267     if (g_hwiHandlerForm[hwiIndex] != 0) {
268         g_hwiHandlerForm[hwiIndex]();
269     }
270 #endif
271 
272 #if (LOSCFG_DEBUG_TOOLS == 1)
273     ++g_hwiFormCnt[hwiIndex];
274 #endif
275 
276     HalAftInterruptHandler(hwiIndex);
277 
278 #if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
279     OsCpupIrqEnd(hwiIndex);
280 #endif
281 
282     OsHookCall(LOS_HOOK_TYPE_ISR_EXIT, hwiIndex);
283 
284     intSave = LOS_IntLock();
285     g_intCount--;
286     LOS_IntRestore(intSave);
287 }
288 
289 /* ****************************************************************************
290  Function    : ArchHwiCreate
291  Description : create hardware interrupt
292  Input       : hwiNum   --- hwi num to create
293                hwiPrio  --- priority of the hwi
294                hwiMode  --- unused
295                hwiHandler --- hwi handler
296                irqParam --- param of the hwi handler
297  Output      : None
298  Return      : LOS_OK on success or error code on failure
299  **************************************************************************** */
ArchHwiCreate(HWI_HANDLE_T hwiNum,HWI_PRIOR_T hwiPrio,HWI_MODE_T hwiMode,HWI_PROC_FUNC hwiHandler,HwiIrqParam * irqParam)300 LITE_OS_SEC_TEXT_INIT UINT32 ArchHwiCreate(HWI_HANDLE_T hwiNum,
301                                            HWI_PRIOR_T hwiPrio,
302                                            HWI_MODE_T hwiMode,
303                                            HWI_PROC_FUNC hwiHandler,
304                                            HwiIrqParam *irqParam)
305 {
306     (VOID)hwiMode;
307     UINT32 intSave;
308 
309     if (hwiHandler == NULL) {
310         return OS_ERRNO_HWI_PROC_FUNC_NULL;
311     }
312 
313     if (hwiNum >= OS_HWI_MAX_NUM) {
314         return OS_ERRNO_HWI_NUM_INVALID;
315     }
316 
317     if (g_hwiForm[hwiNum + OS_SYS_VECTOR_CNT] != (HWI_PROC_FUNC)HalHwiDefaultHandler) {
318         return OS_ERRNO_HWI_ALREADY_CREATED;
319     }
320 
321     if (hwiPrio > OS_HWI_PRIO_LOWEST) {
322         return OS_ERRNO_HWI_PRIO_INVALID;
323     }
324 
325     intSave = LOS_IntLock();
326 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
327     if (irqParam != NULL) {
328         OsSetVector(hwiNum, hwiHandler, irqParam->pDevId);
329     } else {
330         OsSetVector(hwiNum, hwiHandler, NULL);
331     }
332 #else
333     (VOID)irqParam;
334     OsSetVector(hwiNum, hwiHandler);
335 #endif
336 
337 #if (LOSCFG_DEBUG_TOOLS == 1)
338     if ((irqParam != NULL) && (irqParam->pName != NULL)) {
339         g_hwiFormName[hwiNum + OS_SYS_VECTOR_CNT] = (CHAR *)irqParam->pName;
340     }
341     g_hwiFormCnt[hwiNum + OS_SYS_VECTOR_CNT] = 0;
342 #endif
343 
344     HwiUnmask((IRQn_Type)hwiNum);
345     HwiSetPriority((IRQn_Type)hwiNum, hwiPrio);
346 
347     LOS_IntRestore(intSave);
348 
349     return LOS_OK;
350 }
351 
352 /* ****************************************************************************
353  Function    : ArchHwiDelete
354  Description : Delete hardware interrupt
355  Input       : hwiNum   --- hwi num to delete
356                irqParam --- param of the hwi handler
357  Output      : None
358  Return      : LOS_OK on success or error code on failure
359  **************************************************************************** */
ArchHwiDelete(HWI_HANDLE_T hwiNum,HwiIrqParam * irqParam)360 LITE_OS_SEC_TEXT_INIT UINT32 ArchHwiDelete(HWI_HANDLE_T hwiNum, HwiIrqParam *irqParam)
361 {
362     (VOID)irqParam;
363     UINT32 intSave;
364 
365     if (hwiNum >= OS_HWI_MAX_NUM) {
366         return OS_ERRNO_HWI_NUM_INVALID;
367     }
368 
369     NVIC_DisableIRQ((IRQn_Type)hwiNum);
370 
371     intSave = LOS_IntLock();
372 
373     g_hwiForm[hwiNum + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalHwiDefaultHandler;
374 
375     LOS_IntRestore(intSave);
376 
377     return LOS_OK;
378 }
379 
380 #define FAULT_STATUS_REG_BIT            32
381 #define USGFAULT                        (1 << 18)
382 #define BUSFAULT                        (1 << 17)
383 #define MEMFAULT                        (1 << 16)
384 #define DIV0FAULT                       (1 << 4)
385 #define UNALIGNFAULT                    (1 << 3)
386 #define HARDFAULT_IRQN                  (-13)
387 
388 ExcInfo g_excInfo = {0};
389 
390 UINT8 g_uwExcTbl[FAULT_STATUS_REG_BIT] = {
391     0, 0, 0, 0, 0, 0, OS_EXC_UF_DIVBYZERO, OS_EXC_UF_UNALIGNED,
392     0, 0, 0, 0, OS_EXC_UF_NOCP, OS_EXC_UF_INVPC, OS_EXC_UF_INVSTATE, OS_EXC_UF_UNDEFINSTR,
393     0, 0, 0, OS_EXC_BF_STKERR, OS_EXC_BF_UNSTKERR, OS_EXC_BF_IMPRECISERR, OS_EXC_BF_PRECISERR, OS_EXC_BF_IBUSERR,
394     0, 0, 0, OS_EXC_MF_MSTKERR, OS_EXC_MF_MUNSTKERR, 0, OS_EXC_MF_DACCVIOL, OS_EXC_MF_IACCVIOL
395 };
396 
397 #if (LOSCFG_KERNEL_PRINTF != 0)
OsExcNvicDump(VOID)398 STATIC VOID OsExcNvicDump(VOID)
399 {
400 #define OS_NR_NVIC_EXC_DUMP_TYPES   7
401     UINT32 *base = NULL;
402     UINT32 len, i, j;
403     UINT32 rgNvicBases[OS_NR_NVIC_EXC_DUMP_TYPES] = {
404         OS_NVIC_SETENA_BASE, OS_NVIC_SETPEND_BASE, OS_NVIC_INT_ACT_BASE,
405         OS_NVIC_PRI_BASE, OS_NVIC_EXCPRI_BASE, OS_NVIC_SHCSR, OS_NVIC_INT_CTRL
406     };
407     UINT32 rgNvicLens[OS_NR_NVIC_EXC_DUMP_TYPES] = {
408         OS_NVIC_INT_ENABLE_SIZE, OS_NVIC_INT_PEND_SIZE, OS_NVIC_INT_ACT_SIZE,
409         OS_NVIC_INT_PRI_SIZE, OS_NVIC_EXCPRI_SIZE, OS_NVIC_SHCSR_SIZE,
410         OS_NVIC_INT_CTRL_SIZE
411     };
412     CHAR strRgEnable[] = "enable";
413     CHAR strRgPending[] = "pending";
414     CHAR strRgActive[] = "active";
415     CHAR strRgPriority[] = "priority";
416     CHAR strRgException[] = "exception";
417     CHAR strRgShcsr[] = "shcsr";
418     CHAR strRgIntCtrl[] = "control";
419     CHAR *strRgs[] = {
420         strRgEnable, strRgPending, strRgActive, strRgPriority,
421         strRgException, strRgShcsr, strRgIntCtrl
422     };
423 
424     PRINTK("\r\nOS exception NVIC dump:\n");
425     for (i = 0; i < OS_NR_NVIC_EXC_DUMP_TYPES; i++) {
426         base = (UINT32 *)rgNvicBases[i];
427         len = rgNvicLens[i];
428         PRINTK("interrupt %s register, base address: %p, size: 0x%x\n", strRgs[i], base, len);
429         len = (len >> 2); /* 2: Gets the next register offset */
430         for (j = 0; j < len; j++) {
431             PRINTK("0x%x ", *(base + j));
432             if ((j != 0) && ((j % 16) == 0)) { /* 16: print wrap line */
433                 PRINTK("\n");
434             }
435         }
436         PRINTK("\n");
437     }
438 }
439 
OsExcTypeInfo(const ExcInfo * excInfo)440 STATIC VOID OsExcTypeInfo(const ExcInfo *excInfo)
441 {
442     CHAR *phaseStr[] = {"exc in init", "exc in task", "exc in hwi"};
443 
444     PRINTK("Type      = %d\n", excInfo->type);
445     PRINTK("ThrdPid   = %d\n", excInfo->thrdPid);
446     PRINTK("Phase     = %s\n", phaseStr[excInfo->phase]);
447     PRINTK("FaultAddr = 0x%x\n", excInfo->faultAddr);
448 }
449 
OsExcCurTaskInfo(const ExcInfo * excInfo)450 STATIC VOID OsExcCurTaskInfo(const ExcInfo *excInfo)
451 {
452     PRINTK("Current task info:\n");
453     if (excInfo->phase == OS_EXC_IN_TASK) {
454         LosTaskCB *taskCB = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
455         PRINTK("Task name = %s\n", taskCB->taskName);
456         PRINTK("Task ID   = %d\n", taskCB->taskID);
457         PRINTK("Task SP   = %p\n", taskCB->stackPointer);
458         PRINTK("Task ST   = 0x%x\n", taskCB->topOfStack);
459         PRINTK("Task SS   = 0x%x\n", taskCB->stackSize);
460     } else if (excInfo->phase == OS_EXC_IN_HWI) {
461         PRINTK("Exception occur in interrupt phase!\n");
462     } else {
463         PRINTK("Exception occur in system init phase!\n");
464     }
465 }
466 
OsExcRegInfo(const ExcInfo * excInfo)467 STATIC VOID OsExcRegInfo(const ExcInfo *excInfo)
468 {
469     PRINTK("Exception reg dump:\n");
470     PRINTK("PC        = 0x%x\n", excInfo->context->uwPC);
471     PRINTK("LR        = 0x%x\n", excInfo->context->uwLR);
472     PRINTK("SP        = 0x%x\n", excInfo->context->uwSP);
473     PRINTK("R0        = 0x%x\n", excInfo->context->uwR0);
474     PRINTK("R1        = 0x%x\n", excInfo->context->uwR1);
475     PRINTK("R2        = 0x%x\n", excInfo->context->uwR2);
476     PRINTK("R3        = 0x%x\n", excInfo->context->uwR3);
477     PRINTK("R4        = 0x%x\n", excInfo->context->uwR4);
478     PRINTK("R5        = 0x%x\n", excInfo->context->uwR5);
479     PRINTK("R6        = 0x%x\n", excInfo->context->uwR6);
480     PRINTK("R7        = 0x%x\n", excInfo->context->uwR7);
481     PRINTK("R8        = 0x%x\n", excInfo->context->uwR8);
482     PRINTK("R9        = 0x%x\n", excInfo->context->uwR9);
483     PRINTK("R10       = 0x%x\n", excInfo->context->uwR10);
484     PRINTK("R11       = 0x%x\n", excInfo->context->uwR11);
485     PRINTK("R12       = 0x%x\n", excInfo->context->uwR12);
486     PRINTK("PriMask   = 0x%x\n", excInfo->context->uwPriMask);
487     PRINTK("xPSR      = 0x%x\n", excInfo->context->uwxPSR);
488 }
489 
490 #if (LOSCFG_KERNEL_BACKTRACE == 1)
OsExcBackTraceInfo(const ExcInfo * excInfo)491 STATIC VOID OsExcBackTraceInfo(const ExcInfo *excInfo)
492 {
493     UINTPTR LR[LOSCFG_BACKTRACE_DEPTH] = {0};
494     UINT32 index;
495 
496     OsBackTraceHookCall(LR, LOSCFG_BACKTRACE_DEPTH, 0, excInfo->context->uwSP);
497 
498     PRINTK("----- backtrace start -----\n");
499     for (index = 0; index < LOSCFG_BACKTRACE_DEPTH; index++) {
500         if (LR[index] == 0) {
501             break;
502         }
503         PRINTK("backtrace %d -- lr = 0x%x\n", index, LR[index]);
504     }
505     PRINTK("----- backtrace end -----\n");
506 }
507 #endif
508 
OsExcMemPoolCheckInfo(VOID)509 STATIC VOID OsExcMemPoolCheckInfo(VOID)
510 {
511     PRINTK("\r\nmemory pools check:\n");
512 #if (LOSCFG_PLATFORM_EXC == 1)
513     MemInfoCB memExcInfo[OS_SYS_MEM_NUM];
514     UINT32 errCnt;
515     UINT32 i;
516 
517     (VOID)memset_s(memExcInfo, sizeof(memExcInfo), 0, sizeof(memExcInfo));
518 
519     errCnt = OsMemExcInfoGet(OS_SYS_MEM_NUM, memExcInfo);
520     if (errCnt < OS_SYS_MEM_NUM) {
521         errCnt += OsMemboxExcInfoGet(OS_SYS_MEM_NUM - errCnt, memExcInfo + errCnt);
522     }
523 
524     if (errCnt == 0) {
525         PRINTK("all memory pool check passed!\n");
526         return;
527     }
528 
529     for (i = 0; i < errCnt; i++) {
530         PRINTK("pool num    = %d\n", i);
531         PRINTK("pool type   = %d\n", memExcInfo[i].type);
532         PRINTK("pool addr   = 0x%x\n", memExcInfo[i].startAddr);
533         PRINTK("pool size   = 0x%x\n", memExcInfo[i].size);
534         PRINTK("pool free   = 0x%x\n", memExcInfo[i].free);
535         PRINTK("pool blkNum = %d\n", memExcInfo[i].blockSize);
536         PRINTK("pool error node addr  = 0x%x\n", memExcInfo[i].errorAddr);
537         PRINTK("pool error node len   = 0x%x\n", memExcInfo[i].errorLen);
538         PRINTK("pool error node owner = %d\n", memExcInfo[i].errorOwner);
539     }
540 #endif
541     UINT32 ret = LOS_MemIntegrityCheck(LOSCFG_SYS_HEAP_ADDR);
542     if (ret == LOS_OK) {
543         PRINTK("system heap memcheck over, all passed!\n");
544     }
545 
546     PRINTK("memory pool check end!\n");
547 }
548 #endif
549 
OsExcInfoDisplay(const ExcInfo * excInfo)550 STATIC VOID OsExcInfoDisplay(const ExcInfo *excInfo)
551 {
552 #if (LOSCFG_KERNEL_PRINTF != 0)
553     PRINTK("*************Exception Information**************\n");
554     OsExcTypeInfo(excInfo);
555     OsExcCurTaskInfo(excInfo);
556     OsExcRegInfo(excInfo);
557 #if (LOSCFG_KERNEL_BACKTRACE == 1)
558     OsExcBackTraceInfo(excInfo);
559 #endif
560     OsGetAllTskInfo();
561     OsExcNvicDump();
562     OsExcMemPoolCheckInfo();
563 #endif
564 }
565 
HalExcHandleEntry(UINT32 excType,UINT32 faultAddr,UINT32 pid,EXC_CONTEXT_S * excBufAddr)566 LITE_OS_SEC_TEXT_INIT VOID HalExcHandleEntry(UINT32 excType, UINT32 faultAddr, UINT32 pid, EXC_CONTEXT_S *excBufAddr)
567 {
568     UINT16 tmpFlag = (excType >> 16) & OS_NULL_SHORT; /* 16: Get Exception Type */
569     g_intCount++;
570     g_excInfo.nestCnt++;
571 
572     g_excInfo.type = excType & OS_NULL_SHORT;
573 
574     if (tmpFlag & OS_EXC_FLAG_FAULTADDR_VALID) {
575         g_excInfo.faultAddr = faultAddr;
576     } else {
577         g_excInfo.faultAddr = OS_EXC_IMPRECISE_ACCESS_ADDR;
578     }
579     if (g_losTask.runTask != NULL) {
580         if (tmpFlag & OS_EXC_FLAG_IN_HWI) {
581             g_excInfo.phase = OS_EXC_IN_HWI;
582             g_excInfo.thrdPid = pid;
583         } else {
584             g_excInfo.phase = OS_EXC_IN_TASK;
585             g_excInfo.thrdPid = g_losTask.runTask->taskID;
586         }
587     } else {
588         g_excInfo.phase = OS_EXC_IN_INIT;
589         g_excInfo.thrdPid = OS_NULL_INT;
590     }
591     if (excType & OS_EXC_FLAG_NO_FLOAT) {
592         g_excInfo.context = (EXC_CONTEXT_S *)((CHAR *)excBufAddr - LOS_OFF_SET_OF(EXC_CONTEXT_S, uwR4));
593     } else {
594         g_excInfo.context = excBufAddr;
595     }
596 
597     OsDoExcHook(EXC_INTERRUPT);
598     OsExcInfoDisplay(&g_excInfo);
599     ArchSysExit();
600 }
601 
602 /* ****************************************************************************
603  Function    : HalHwiInit
604  Description : initialization of the hardware interrupt
605  Input       : None
606  Output      : None
607  Return      : None
608  **************************************************************************** */
HalHwiInit(VOID)609 LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID)
610 {
611 #if (LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT == 1)
612     UINT32 index;
613     g_hwiForm[0] = 0;             /* [0] Top of Stack */
614     g_hwiForm[1] = (HWI_PROC_FUNC)Reset_Handler; /* [1] reset */
615     for (index = 2; index < OS_VECTOR_CNT; index++) { /* 2: The starting position of the interrupt */
616         g_hwiForm[index] = (HWI_PROC_FUNC)HalHwiDefaultHandler;
617     }
618     /* Exception handler register */
619     g_hwiForm[NonMaskableInt_IRQn + OS_SYS_VECTOR_CNT]   = (HWI_PROC_FUNC)HalExcNMI;
620     g_hwiForm[HARDFAULT_IRQN + OS_SYS_VECTOR_CNT]        = (HWI_PROC_FUNC)HalExcHardFault;
621     g_hwiForm[MemoryManagement_IRQn + OS_SYS_VECTOR_CNT] = (HWI_PROC_FUNC)HalExcMemFault;
622     g_hwiForm[BusFault_IRQn + OS_SYS_VECTOR_CNT]         = (HWI_PROC_FUNC)HalExcBusFault;
623     g_hwiForm[UsageFault_IRQn + OS_SYS_VECTOR_CNT]       = (HWI_PROC_FUNC)HalExcUsageFault;
624     g_hwiForm[SVCall_IRQn + OS_SYS_VECTOR_CNT]           = (HWI_PROC_FUNC)HalExcSvcCall;
625     g_hwiForm[PendSV_IRQn + OS_SYS_VECTOR_CNT]           = (HWI_PROC_FUNC)HalPendSV;
626     g_hwiForm[SysTick_IRQn + OS_SYS_VECTOR_CNT]          = (HWI_PROC_FUNC)SysTick_Handler;
627 
628     /* Interrupt vector table location */
629     SCB->VTOR = (UINT32)(UINTPTR)g_hwiForm;
630 #endif
631 #if (__CORTEX_M >= 0x03U) /* only for Cortex-M3 and above */
632     NVIC_SetPriorityGrouping(OS_NVIC_AIRCR_PRIGROUP);
633 #endif
634 
635     /* Enable USGFAULT, BUSFAULT, MEMFAULT */
636     *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT);
637 
638     /* Enable DIV 0 and unaligned exception */
639 #ifdef LOSCFG_ARCH_UNALIGNED_EXC
640     *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT);
641 #else
642     *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT);
643 #endif
644 
645     return;
646 }
647 
648