* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab1 / kern / init.c
blob49648b0cc2d3134000e75a18c02af6dfdb6b6217
1 /* See COPYRIGHT for copyright information. */
3 #include <inc/stdio.h>
4 #include <inc/string.h>
5 #include <inc/assert.h>
7 #include <kern/monitor.h>
8 #include <kern/console.h>
10 // Test the stack backtrace function (lab 1 only)
11 void
12 test_backtrace(int x)
14 cprintf("entering test_backtrace %d\n", x);
15 if (x > 0)
16 test_backtrace(x-1);
17 else
18 mon_backtrace(0, 0, 0);
19 cprintf("leaving test_backtrace %d\n", x);
22 asmlinkage void
23 i386_init(void)
25 extern char edata[], end[];
26 extern const uintptr_t sctors[], ectors[];
27 const uintptr_t *ctorva;
29 // Initialize the console.
30 // Can't call cprintf until after we do this!
31 cons_init();
33 // Then call any global constructors (e.g., defined by C++).
34 // This relies on linker script magic to define the 'sctors' and
35 // 'ectors' symbols; see kern/kernel.ld.
36 // Call after cons_init() so we can cprintf() if necessary.
37 for (ctorva = ectors; ctorva > sctors; )
38 ((void(*)()) *--ctorva)();
40 cprintf("6828 decimal is %o octal!\n", 6828);
48 // Test the stack backtrace function (lab 1 only)
49 test_backtrace(5);
51 // Drop into the kernel monitor.
52 while (1)
53 monitor(NULL);
58 * Variable panicstr contains argument to first call to panic; used as flag
59 * to indicate that the kernel has already called panic.
61 static const char *panicstr;
64 * Panic is called on unresolvable fatal errors.
65 * It prints "panic: mesg", and then enters the kernel monitor.
67 void
68 _panic(const char *file, int line, const char *fmt, ...)
70 va_list ap;
72 if (panicstr)
73 goto dead;
74 panicstr = fmt;
76 // Be extra sure that the machine is in as reasonable state
77 __asm __volatile("cli; cld");
79 va_start(ap, fmt);
80 cprintf("kernel panic at %s:%d: ", file, line);
81 vcprintf(fmt, ap);
82 cprintf("\n");
83 va_end(ap);
85 dead:
86 /* break into the kernel monitor */
87 while (1)
88 monitor(NULL);
91 /* like panic, but don't */
92 void
93 _warn(const char *file, int line, const char *fmt, ...)
95 va_list ap;
97 va_start(ap, fmt);
98 cprintf("kernel warning at %s:%d: ", file, line);
99 vcprintf(fmt, ap);
100 cprintf("\n");
101 va_end(ap);