* more re-work
[mascara-docs.git] / i386 / junos / ucla / src / lab2 / kern / init.c
blob4486f6c06c4cb04de40fb5289e90313fb9987f4a
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>
9 #include <kern/pmap.h>
10 #include <kern/kclock.h>
11 #include <kern/trap.h>
13 // Test the stack backtrace function (used in lab 1 only)
14 void
15 test_backtrace(int x)
17 cprintf("entering test_backtrace %d\n", x);
18 if (x > 0)
19 test_backtrace(x-1);
20 else
21 mon_backtrace(0, 0, 0);
22 cprintf("leaving test_backtrace %d\n", x);
25 // Test kernel breakpoint functionality (used in lab 2 only)
26 static void test_kernel_breakpoint(void) __attribute__((noinline));
27 static void
28 test_kernel_breakpoint(void)
30 __asm__ __volatile__("int3");
31 // Then the breakpoint should return (after the user types 'exit')
32 cprintf("Breakpoint succeeded!\n");
35 asmlinkage void
36 i386_init(void)
38 extern char edata[], end[];
39 extern const uintptr_t sctors[], ectors[];
40 const uintptr_t *ctorva;
42 // Initialize the console.
43 // Can't call cprintf until after we do this!
44 cons_init();
46 // Then call any global constructors (e.g., defined by C++).
47 // This relies on linker script magic to define the 'sctors' and
48 // 'ectors' symbols; see kern/kernel.ld.
49 // Call after cons_init() so we can cprintf() if necessary.
50 for (ctorva = ectors; ctorva > sctors; )
51 ((void(*)()) *--ctorva)();
53 cprintf("6828 decimal is %o octal!\n", 6828);
55 // Lab 2 memory management initialization functions
56 mem_init();
58 // Lab 2 interrupt and gate descriptor initialization functions
59 idt_init();
64 // Test IDT (lab 2 only)
65 test_kernel_breakpoint();
67 // Test the stack backtrace function (lab 1 only)
68 //test_backtrace(5);
70 // Drop into the kernel monitor.
71 while (1)
72 monitor(NULL);
77 * Variable panicstr contains argument to first call to panic; used as flag
78 * to indicate that the kernel has already called panic.
80 static const char *panicstr;
83 * Panic is called on unresolvable fatal errors.
84 * It prints "panic: mesg", and then enters the kernel monitor.
86 void
87 _panic(const char *file, int line, const char *fmt, ...)
89 va_list ap;
91 if (panicstr)
92 goto dead;
93 panicstr = fmt;
95 // Be extra sure that the machine is in as reasonable state
96 __asm __volatile("cli; cld");
98 va_start(ap, fmt);
99 cprintf("kernel panic at %s:%d: ", file, line);
100 vcprintf(fmt, ap);
101 cprintf("\n");
102 va_end(ap);
104 dead:
105 /* break into the kernel monitor */
106 while (1)
107 monitor(NULL);
110 /* like panic, but don't */
111 void
112 _warn(const char *file, int line, const char *fmt, ...)
114 va_list ap;
116 va_start(ap, fmt);
117 cprintf("kernel warning at %s:%d: ", file, line);
118 vcprintf(fmt, ap);
119 cprintf("\n");
120 va_end(ap);