1 /*
2  * Copyright (c) 2013 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 /*
25  * Global init hook mechanism. Allows code anywhere in the system to define
26  * a init hook that is called at increasing init levels as the system is
27  * initialized.
28  */
29 #include <arch/ops.h>
30 #include <lk/init.h>
31 
32 #include <assert.h>
33 #include <compiler.h>
34 #include <debug.h>
35 #include <trace.h>
36 
37 #define LOCAL_TRACE 0
38 #define TRACE_INIT 0
39 #define TRACE_INIT_FLAGS (LK_INIT_FLAG_ALL_CPUS)
40 #ifndef EARLIEST_TRACE_LEVEL
41 #define EARLIEST_TRACE_LEVEL LK_INIT_LEVEL_TARGET_EARLY
42 #endif
43 
44 extern const struct lk_init_struct __lk_init[];
45 extern const struct lk_init_struct __lk_init_end[];
46 
lk_init_level(enum lk_init_flags required_flag,uint start_level,uint stop_level)47 void lk_init_level(enum lk_init_flags required_flag, uint start_level, uint stop_level)
48 {
49     LTRACEF("flags %#x, start_level %#x, stop_level %#x\n",
50             required_flag, start_level, stop_level);
51 
52     ASSERT(start_level > 0);
53     uint last_called_level = start_level - 1;
54     const struct lk_init_struct *last = NULL;
55     for (;;) {
56         /* search for the lowest uncalled hook to call */
57         LTRACEF("last %p, last_called_level %#x\n", last, last_called_level);
58 
59         const struct lk_init_struct *found = NULL;
60         bool seen_last = false;
61         for (const struct lk_init_struct *ptr = __lk_init; ptr != __lk_init_end; ptr++) {
62             LTRACEF("looking at %p (%s) level %#x, flags %#x, seen_last %d\n", ptr, ptr->name, ptr->level, ptr->flags, seen_last);
63 
64             if (ptr == last)
65                 seen_last = true;
66 
67             /* reject the easy ones */
68             if (!(ptr->flags & required_flag))
69                 continue;
70             if (ptr->level > stop_level)
71                 continue;
72             if (ptr->level < last_called_level)
73                 continue;
74             if (found && found->level <= ptr->level)
75                 continue;
76 
77             /* keep the lowest one we haven't called yet */
78             if (ptr->level >= start_level && ptr->level > last_called_level) {
79                 found = ptr;
80                 continue;
81             }
82 
83             /* if we're at the same level as the last one we called and we've
84              * already passed over it this time around, we can mark this one
85              * and early terminate the loop.
86              */
87             if (ptr->level == last_called_level && ptr != last && seen_last) {
88                 found = ptr;
89                 break;
90             }
91         }
92 
93         if (!found)
94             break;
95 
96 #if TRACE_INIT
97         if (found->level >= EARLIEST_TRACE_LEVEL && (required_flag & TRACE_INIT_FLAGS)) {
98             printf("INIT: cpu %d, calling hook %p (%s) at level %#x, flags %#x\n",
99                    arch_curr_cpu_num(), found->hook, found->name, found->level, found->flags);
100         }
101 #endif
102         found->hook(found->level);
103         last_called_level = found->level;
104         last = found;
105     }
106 }
107 
108 #if 0
109 void test_hook(uint level)
110 {
111     LTRACEF("level %#x\n", level);
112 }
113 void test_hook1(uint level)
114 {
115     LTRACEF("level %#x\n", level);
116 }
117 void test_hook1a(uint level)
118 {
119     LTRACEF("level %#x\n", level);
120 }
121 void test_hook1b(uint level)
122 {
123     LTRACEF("level %#x\n", level);
124 }
125 void test_hook2(uint level)
126 {
127     LTRACEF("level %#x\n", level);
128 }
129 
130 LK_INIT_HOOK(test, test_hook, 1);
131 LK_INIT_HOOK(test1, test_hook1, 1);
132 LK_INIT_HOOK(test2, test_hook2, 2);
133 LK_INIT_HOOK(test1a, test_hook1a, 1);
134 LK_INIT_HOOK(test1b, test_hook1b, 1);
135 #endif
136