* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab3 / kern / init.c
blob98ffb212ba3f8e43f9fc5019e636008cc6bced2d
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>
12 #include <kern/env.h>
14 // Test the stack backtrace function (used in lab 1 only)
15 void
16 test_backtrace(int x)
18 cprintf("entering test_backtrace %d\n", x);
19 if (x > 0)
20 test_backtrace(x-1);
21 else
22 mon_backtrace(0, 0, 0);
23 cprintf("leaving test_backtrace %d\n", x);
26 // Test kernel breakpoint functionality (used in lab 2 only)
27 static void test_kernel_breakpoint(void) __attribute__((noinline));
28 static void
29 test_kernel_breakpoint(void)
31 __asm__ __volatile__("int3");
32 // Then the breakpoint should return (after the user types 'exit')
33 cprintf("Breakpoint succeeded!\n");
36 asmlinkage void
37 i386_init(void)
39 extern char edata[], end[];
40 extern const uintptr_t sctors[], ectors[];
41 const uintptr_t *ctorva;
43 // Initialize the console.
44 // Can't call cprintf until after we do this!
45 cons_init();
47 // Then call any global constructors (e.g., defined by C++).
48 // This relies on linker script magic to define the 'sctors' and
49 // 'ectors' symbols; see kern/kernel.ld.
50 // Call after cons_init() so we can cprintf() if necessary.
51 for (ctorva = ectors; ctorva > sctors; )
52 ((void(*)()) *--ctorva)();
54 cprintf("6828 decimal is %o octal!\n", 6828);
56 // Lab 2 memory management initialization functions
57 mem_init();
59 // Lab 2 interrupt and gate descriptor initialization functions
60 idt_init();
62 // Lab 3 user environment initialization functions
63 env_init();
65 // Temporary test code specific to LAB 3
66 #if defined(TEST)
67 // Don't touch -- used by grading script!
68 ENV_CREATE2(TEST, TESTSIZE);
69 #else
70 // Touch all you want.
71 ENV_CREATE(user_hello);
72 #endif // TEST*
75 // We only have one user environment for now, so just run it.
76 env_run(&envs[0]);
78 // Test IDT (lab 2 only)
79 //test_kernel_breakpoint();
81 // Test the stack backtrace function (lab 1 only)
82 //test_backtrace(5);
84 // Drop into the kernel monitor (lab 1-2 only; not reached later)
85 while (1)
86 monitor(NULL);
91 * Variable panicstr contains argument to first call to panic; used as flag
92 * to indicate that the kernel has already called panic.
94 static const char *panicstr;
97 * Panic is called on unresolvable fatal errors.
98 * It prints "panic: mesg", and then enters the kernel monitor.
100 void
101 _panic(const char *file, int line, const char *fmt, ...)
103 va_list ap;
105 if (panicstr)
106 goto dead;
107 panicstr = fmt;
109 // Be extra sure that the machine is in as reasonable state
110 __asm __volatile("cli; cld");
112 va_start(ap, fmt);
113 cprintf("kernel panic at %s:%d: ", file, line);
114 vcprintf(fmt, ap);
115 cprintf("\n");
116 va_end(ap);
118 dead:
119 /* break into the kernel monitor */
120 while (1)
121 monitor(NULL);
124 /* like panic, but don't */
125 void
126 _warn(const char *file, int line, const char *fmt, ...)
128 va_list ap;
130 va_start(ap, fmt);
131 cprintf("kernel warning at %s:%d: ", file, line);
132 vcprintf(fmt, ap);
133 cprintf("\n");
134 va_end(ap);