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
32 #include "los_exchook.h"
33 #include "los_context.h"
34
35 #ifndef LOSCFG_BASE_EXC_HOOK_LIMIT
36 #define LOSCFG_BASE_EXC_HOOK_LIMIT 16
37 #endif
38
39 struct Node {
40 ExcHookFn excHookFn;
41 struct Node *next;
42 };
43
44 STATIC struct Node g_excNodes[LOSCFG_BASE_EXC_HOOK_LIMIT];
45 STATIC struct Node *g_excHeads[EXC_TYPE_END + 1]; /* EXC_TYPE_END is used for the free list. */
46
DoExcHookInRegOrder(EXC_TYPE excType,struct Node * node)47 STATIC VOID DoExcHookInRegOrder(EXC_TYPE excType, struct Node *node)
48 {
49 if (node != NULL) {
50 DoExcHookInRegOrder(excType, node->next);
51 node->excHookFn(excType);
52 }
53 }
54
DoExcHook(EXC_TYPE excType)55 STATIC VOID DoExcHook(EXC_TYPE excType)
56 {
57 UINT32 intSave;
58 if (excType >= EXC_TYPE_END) {
59 return;
60 }
61 intSave = LOS_IntLock();
62 DoExcHookInRegOrder(excType, g_excHeads[excType]);
63 LOS_IntRestore(intSave);
64 }
65
GetFreeNode(VOID)66 STATIC struct Node *GetFreeNode(VOID)
67 {
68 struct Node *node = NULL;
69 int i;
70 if (g_excHeads[EXC_TYPE_END] == NULL) {
71 if (g_excNodes[0].excHookFn != NULL) {
72 /* no free node now */
73 return NULL;
74 } else {
75 /* Initialize the free list */
76 for (i = 0; i < LOSCFG_BASE_EXC_HOOK_LIMIT; ++i) {
77 g_excNodes[i].next = g_excHeads[EXC_TYPE_END];
78 g_excHeads[EXC_TYPE_END] = &g_excNodes[i];
79 }
80 OsExcHookRegister(DoExcHook);
81 }
82 }
83
84 node = g_excHeads[EXC_TYPE_END];
85 g_excHeads[EXC_TYPE_END] = node->next;
86 return node;
87 }
88
LOS_RegExcHook(EXC_TYPE excType,ExcHookFn excHookFn)89 UINT32 LOS_RegExcHook(EXC_TYPE excType, ExcHookFn excHookFn)
90 {
91 UINT32 intSave;
92 struct Node *node = NULL;
93 if (excType >= EXC_TYPE_END || excHookFn == NULL) {
94 return LOS_ERRNO_SYS_PTR_NULL;
95 }
96
97 intSave = LOS_IntLock();
98 node = GetFreeNode();
99 if (node == NULL) {
100 LOS_IntRestore(intSave);
101 return LOS_ERRNO_SYS_HOOK_IS_FULL;
102 }
103
104 node->excHookFn = excHookFn;
105 node->next = g_excHeads[excType];
106 g_excHeads[excType] = node;
107 LOS_IntRestore(intSave);
108 return LOS_OK;
109 }
110
LOS_UnRegExcHook(EXC_TYPE excType,ExcHookFn excHookFn)111 UINT32 LOS_UnRegExcHook(EXC_TYPE excType, ExcHookFn excHookFn)
112 {
113 UINT32 intSave;
114 struct Node *node = NULL;
115 struct Node *preNode = NULL;
116 if (excType >= EXC_TYPE_END || excHookFn == NULL) {
117 return LOS_ERRNO_SYS_PTR_NULL;
118 }
119
120 intSave = LOS_IntLock();
121 for (node = g_excHeads[excType]; node != NULL; node = node->next) {
122 if (node->excHookFn == excHookFn) {
123 if (preNode) {
124 preNode->next = node->next;
125 } else {
126 g_excHeads[excType] = node->next;
127 }
128 node->excHookFn = NULL;
129 node->next = g_excHeads[EXC_TYPE_END];
130 g_excHeads[EXC_TYPE_END] = node;
131 }
132 preNode = node;
133 }
134 LOS_IntRestore(intSave);
135 return LOS_OK;
136 }
137