* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab4 / kern / init.c
blob7f1025001b7e0ee2b0ce94477314911b39ffe5d7
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>
13 #include <kern/sched.h>
14 #include <kern/picirq.h>
16 // Test the stack backtrace function (used in lab 1 only)
17 void
18 test_backtrace(int x)
20 cprintf("entering test_backtrace %d\n", x);
21 if (x > 0)
22 test_backtrace(x-1);
23 else
24 mon_backtrace(0, 0, 0);
25 cprintf("leaving test_backtrace %d\n", x);
28 // Test kernel breakpoint functionality (used in lab 2 only)
29 static void test_kernel_breakpoint(void) __attribute__((noinline));
30 static void
31 test_kernel_breakpoint(void)
33 __asm__ __volatile__("int3");
34 // Then the breakpoint should return (after the user types 'exit')
35 cprintf("Breakpoint succeeded!\n");
38 asmlinkage void
39 i386_init(void)
41 extern char edata[], end[];
42 extern const uintptr_t sctors[], ectors[];
43 const uintptr_t *ctorva;
45 // Initialize the console.
46 // Can't call cprintf until after we do this!
47 cons_init();
49 // Then call any global constructors (e.g., defined by C++).
50 // This relies on linker script magic to define the 'sctors' and
51 // 'ectors' symbols; see kern/kernel.ld.
52 // Call after cons_init() so we can cprintf() if necessary.
53 for (ctorva = ectors; ctorva > sctors; )
54 ((void(*)()) *--ctorva)();
56 cprintf("6828 decimal is %o octal!\n", 6828);
58 // Lab 2 memory management initialization functions
59 mem_init();
61 // Lab 2 interrupt and gate descriptor initialization functions
62 idt_init();
64 // Lab 3 user environment initialization functions
65 env_init();
67 // Lab 4 multitasking initialization functions
68 pic_init();
69 kclock_init();
71 // Should always have an idle process as first one.
72 ENV_CREATE(user_idle);
74 #if defined(TEST)
75 // Don't touch -- used by grading script!
76 ENV_CREATE2(TEST, TESTSIZE);
77 #else
78 // Touch all you want.
79 ENV_CREATE(user_primes);
80 #endif // TEST*
83 // Schedule and run the first user environment!
84 sched_yield();
86 // Test IDT (lab 2 only)
87 //test_kernel_breakpoint();
89 // Test the stack backtrace function (lab 1 only)
90 //test_backtrace(5);
92 // Drop into the kernel monitor (lab 1-2 only; not reached later)
93 while (1)
94 monitor(NULL);
99 * Variable panicstr contains argument to first call to panic; used as flag
100 * to indicate that the kernel has already called panic.
102 static const char *panicstr;
105 * Panic is called on unresolvable fatal errors.
106 * It prints "panic: mesg", and then enters the kernel monitor.
108 void
109 _panic(const char *file, int line, const char *fmt, ...)
111 va_list ap;
113 if (panicstr)
114 goto dead;
115 panicstr = fmt;
117 // Be extra sure that the machine is in as reasonable state
118 __asm __volatile("cli; cld");
120 va_start(ap, fmt);
121 cprintf("kernel panic at %s:%d: ", file, line);
122 vcprintf(fmt, ap);
123 cprintf("\n");
124 va_end(ap);
126 dead:
127 /* break into the kernel monitor */
128 while (1)
129 monitor(NULL);
132 /* like panic, but don't */
133 void
134 _warn(const char *file, int line, const char *fmt, ...)
136 va_list ap;
138 va_start(ap, fmt);
139 cprintf("kernel warning at %s:%d: ", file, line);
140 vcprintf(fmt, ap);
141 cprintf("\n");
142 va_end(ap);