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