1 /*
2  * Copyright (c) 2013 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 /*
10  * Global init hook mechanism. Allows code anywhere in the system to define
11  * a init hook that is called at increasing init levels as the system is
12  * initialized.
13  */
14 #include <lk/init.h>
15 
16 #include <arch/ops.h>
17 #include <assert.h>
18 #include <lk/compiler.h>
19 #include <lk/debug.h>
20 #include <lk/trace.h>
21 
22 #define LOCAL_TRACE 0
23 #define TRACE_INIT (LK_DEBUGLEVEL >= 2)
24 #ifndef EARLIEST_TRACE_LEVEL
25 #define EARLIEST_TRACE_LEVEL LK_INIT_LEVEL_TARGET_EARLY
26 #endif
27 
28 extern const struct lk_init_struct __start_lk_init __WEAK;
29 extern const struct lk_init_struct __stop_lk_init __WEAK;
30 
lk_init_level(enum lk_init_flags required_flag,uint start_level,uint stop_level)31 void lk_init_level(enum lk_init_flags required_flag, uint start_level, uint stop_level) {
32     LTRACEF("flags %#x, start_level %#x, stop_level %#x\n",
33             required_flag, start_level, stop_level);
34 
35     ASSERT(start_level > 0);
36     uint last_called_level = start_level - 1;
37     const struct lk_init_struct *last = NULL;
38     for (;;) {
39         /* search for the lowest uncalled hook to call */
40         LTRACEF("last %p, last_called_level %#x\n", last, last_called_level);
41 
42         const struct lk_init_struct *found = NULL;
43         bool seen_last = false;
44         for (const struct lk_init_struct *ptr = &__start_lk_init; ptr != &__stop_lk_init; ptr++) {
45             LTRACEF("looking at %p (%s) level %#x, flags %#x, seen_last %d\n", ptr, ptr->name, ptr->level, ptr->flags, seen_last);
46 
47             if (ptr == last)
48                 seen_last = true;
49 
50             /* reject the easy ones */
51             if (!(ptr->flags & required_flag))
52                 continue;
53             if (ptr->level > stop_level)
54                 continue;
55             if (ptr->level < last_called_level)
56                 continue;
57             if (found && found->level <= ptr->level)
58                 continue;
59 
60             /* keep the lowest one we haven't called yet */
61             if (ptr->level >= start_level && ptr->level > last_called_level) {
62                 found = ptr;
63                 continue;
64             }
65 
66             /* if we're at the same level as the last one we called and we've
67              * already passed over it this time around, we can mark this one
68              * and early terminate the loop.
69              */
70             if (ptr->level == last_called_level && ptr != last && seen_last) {
71                 found = ptr;
72                 break;
73             }
74         }
75 
76         if (!found)
77             break;
78 
79 #if TRACE_INIT
80         if (found->level >= EARLIEST_TRACE_LEVEL) {
81             printf("INIT: cpu %d, calling hook %p (%s) at level %#x, flags %#x\n",
82                    arch_curr_cpu_num(), found->hook, found->name, found->level, found->flags);
83         }
84 #endif
85         found->hook(found->level);
86         last_called_level = found->level;
87         last = found;
88     }
89 }
90 
91 #if 0
92 void test_hook(uint level) {
93     LTRACEF("level %#x\n", level);
94 }
95 void test_hook1(uint level) {
96     LTRACEF("level %#x\n", level);
97 }
98 void test_hook1a(uint level) {
99     LTRACEF("level %#x\n", level);
100 }
101 void test_hook1b(uint level) {
102     LTRACEF("level %#x\n", level);
103 }
104 void test_hook2(uint level) {
105     LTRACEF("level %#x\n", level);
106 }
107 
108 LK_INIT_HOOK(test, test_hook, 1);
109 LK_INIT_HOOK(test1, test_hook1, 1);
110 LK_INIT_HOOK(test2, test_hook2, 2);
111 LK_INIT_HOOK(test1a, test_hook1a, 1);
112 LK_INIT_HOOK(test1b, test_hook1b, 1);
113 #endif
114