summaryrefslogtreecommitdiff
path: root/kern/init.c
blob: dd699976877422b72c808807564165b9fae12eef (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright (c) 2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * The main purpose of the init module is to provide a convenient interface
 * to declare initialization operations and their dependency, infer an order
 * of execution based on the resulting graph (which must be acyclic), and
 * run the operations in that order. Topological sorting is achieved using
 * Kahn's algorithm.
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <kern/init.h>
#include <kern/slist.h>
#include <kern/macros.h>
#include <machine/cpu.h>

extern struct init_op _init_ops;
extern struct init_op _init_ops_end;

static_assert(sizeof(struct init_op) == INIT_OP_ALIGN, "invalid init_op size");

/*
 * List of initialization operations.
 *
 * This type is used for the output of the topological sort.
 */
struct init_ops_list {
    struct slist ops;
    size_t size;
};

static void __init
init_ops_list_init(struct init_ops_list *list)
{
    slist_init(&list->ops);
    list->size = 0;
}

static size_t __init
init_ops_list_size(const struct init_ops_list *list)
{
    return list->size;
}

static void __init
init_ops_list_push(struct init_ops_list *list, struct init_op *op)
{
    slist_insert_head(&list->ops, &op->list_node);
    list->size++;
}

static struct init_op * __init
init_ops_list_pop(struct init_ops_list *list)
{
    struct init_op *op;

    if (list->size == 0) {
        return NULL;
    }

    op = slist_first_entry(&list->ops, struct init_op, list_node);
    slist_remove(&list->ops, NULL);
    list->size--;
    return op;
}

/*
 * Stack of initialization operations.
 *
 * This type is used internally by the topological sort algorithm.
 */
struct init_ops_stack {
    struct slist ops;
};

static void __init
init_ops_stack_init(struct init_ops_stack *stack)
{
    slist_init(&stack->ops);
}

static void __init
init_ops_stack_push(struct init_ops_stack *stack, struct init_op *op)
{
    slist_insert_head(&stack->ops, &op->stack_node);
}

static struct init_op * __init
init_ops_stack_pop(struct init_ops_stack *stack)
{
    struct init_op *op;

    if (slist_empty(&stack->ops)) {
        return NULL;
    }

    op = slist_first_entry(&stack->ops, struct init_op, stack_node);
    slist_remove(&stack->ops, NULL);
    return op;
}

static struct init_op_dep * __init
init_op_get_dep(const struct init_op *op, size_t index)
{
    assert(index < op->nr_deps);
    return &op->deps[index];
}

static void __init
init_op_init(struct init_op *op)
{
    struct init_op_dep *dep;

    for (size_t i = 0; i < op->nr_deps; i++) {
        dep = init_op_get_dep(op, i);
        dep->op->nr_parents++;
        assert(dep->op->nr_parents != 0);
    }
}

static bool __init
init_op_orphan(const struct init_op *op)
{
    return (op->nr_parents == 0);
}

static bool __init
init_op_pending(const struct init_op *op)
{
    return op->state == INIT_OP_STATE_PENDING;
}

static void __init
init_op_set_pending(struct init_op *op)
{
    assert(op->state == INIT_OP_STATE_UNLINKED);
    op->state = INIT_OP_STATE_PENDING;
}

static bool __init
init_op_complete(const struct init_op *op)
{
    return op->state == INIT_OP_STATE_COMPLETE;
}

static void __init
init_op_set_complete(struct init_op *op)
{
    assert(init_op_pending(op));
    op->state = INIT_OP_STATE_COMPLETE;
}

static bool __init
init_op_ready(const struct init_op *op)
{
    const struct init_op_dep *dep;
    size_t i;

    for (i = 0; i < op->nr_deps; i++) {
        dep = init_op_get_dep(op, i);

        if (!init_op_complete(dep->op)
            || (dep->required && dep->op->error)) {
            return false;
        }
    }

    return true;
}

static void __init
init_op_run(struct init_op *op)
{
    if (init_op_ready(op)) {
        op->error = op->fn();
    }

    init_op_set_complete(op);
}

#ifdef CONFIG_INIT_DEBUG

#define INIT_DEBUG_LOG_BUFFER_SIZE 8192

struct init_debug_log {
    char buffer[INIT_DEBUG_LOG_BUFFER_SIZE];
    size_t index;
};

/*
 * Buffers used to store an easy-to-dump text representation of init operations.
 *
 * These buffers are meant to be retrieved through a debugging interface such
 * as JTAG.
 */
struct init_debug_logs {
    struct init_debug_log roots;    /* graph roots */
    struct init_debug_log cycles;   /* operations with dependency cycles */
    struct init_debug_log pending;  /* operations successfully sorted */
    struct init_debug_log complete; /* executed operations */
};

static struct init_debug_logs init_debug_logs;

static void __init
init_debug_log_append(struct init_debug_log *log, const char *name)
{
    size_t size;

    if (log->index == sizeof(log->buffer)) {
        return;
    }

    size = sizeof(log->buffer) - log->index;
    log->index += snprintf(log->buffer + log->index, size, "%s ", name);

    if (log->index >= sizeof(log->buffer)) {
        log->index = sizeof(log->buffer);
    }
}

static void __init
init_debug_append_root(const struct init_op *op)
{
    init_debug_log_append(&init_debug_logs.roots, op->name);
}

static void __init
init_debug_append_cycle(const struct init_op *op)
{
    init_debug_log_append(&init_debug_logs.cycles, op->name);
}

static void __init
init_debug_append_pending(const struct init_op *op)
{
    init_debug_log_append(&init_debug_logs.pending, op->name);
}

static void __init
init_debug_append_complete(const struct init_op *op)
{
    init_debug_log_append(&init_debug_logs.complete, op->name);
}

static void __init
init_debug_scan_not_pending(void)
{
    const struct init_op *op;

    for (op = &_init_ops; op < &_init_ops_end; op++) {
        if (!init_op_pending(op)) {
            init_debug_append_cycle(op);
        }
    }
}

#else /* CONFIG_INIT_DEBUG */
#define init_debug_append_root(roots)
#define init_debug_append_pending(op)
#define init_debug_append_complete(op)
#define init_debug_scan_not_pending()
#endif /* CONFIG_INIT_DEBUG */

static void __init
init_add_pending_op(struct init_ops_list *pending_ops, struct init_op *op)
{
    assert(!init_op_pending(op));

    init_op_set_pending(op);
    init_ops_list_push(pending_ops, op);
    init_debug_append_pending(op);
}

static void __init
init_op_visit(struct init_op *op, struct init_ops_stack *stack)
{
    struct init_op_dep *dep;

    for (size_t i = 0; i < op->nr_deps; i++) {
        dep = init_op_get_dep(op, i);
        assert(dep->op->nr_parents != 0);
        dep->op->nr_parents--;

        if (init_op_orphan(dep->op)) {
            init_ops_stack_push(stack, dep->op);
        }
    }
}

static void __init
init_check_ops_alignment(void)
{
    uintptr_t start, end;

    start = (uintptr_t)&_init_ops;
    end = (uintptr_t)&_init_ops_end;

    if (((end - start) % INIT_OP_ALIGN) != 0) {
        cpu_halt();
    }
}

static void __init
init_bootstrap(void)
{
    struct init_op *op;

    init_check_ops_alignment();

    for (op = &_init_ops; op < &_init_ops_end; op++) {
        init_op_init(op);
    }
}

static void __init
init_scan_roots(struct init_ops_stack *stack)
{
    struct init_op *op;

    init_ops_stack_init(stack);

    for (op = &_init_ops; op < &_init_ops_end; op++) {
        if (init_op_orphan(op)) {
            init_ops_stack_push(stack, op);
            init_debug_append_root(op);
        }
    }
}

static void __init
init_scan_ops(struct init_ops_list *pending_ops)
{
    struct init_ops_stack stack;
    struct init_op *op;
    size_t nr_ops;

    init_scan_roots(&stack);

    for (;;) {
        op = init_ops_stack_pop(&stack);

        if (op == NULL) {
            break;
        }

        init_add_pending_op(pending_ops, op);
        init_op_visit(op, &stack);
    }

    init_debug_scan_not_pending();

    nr_ops = &_init_ops_end - &_init_ops;

    if (init_ops_list_size(pending_ops) != nr_ops) {
        cpu_halt();
    }
}

static void __init
init_run_ops(struct init_ops_list *pending_ops)
{
    struct init_op *op;

    for (;;) {
        op = init_ops_list_pop(pending_ops);

        if (op == NULL) {
            break;
        }

        init_op_run(op);
        init_debug_append_complete(op);
    }
}

void __init
init_setup(void)
{
    struct init_ops_list pending_ops;

    init_ops_list_init(&pending_ops);

    init_bootstrap();
    init_scan_ops(&pending_ops);
    init_run_ops(&pending_ops);
}