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