* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab5 / kern / init.c
blob6349a59b20d2cfd48874ef8b2811b0531ee0bacb
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 // Start bufcache. Bufcache is always meant to run as environment
75 // 0x1100, so we rearrange the free list to put that environment first.
77 extern struct Env *env_free_list;
78 struct Env **pprev = &env_free_list, *bce = 0;
79 for (bce = 0; *pprev; pprev = &(*pprev)->env_next)
80 if (*pprev == &envs[ENVX(ENVID_BUFCACHE)]) {
81 struct Env *e = *pprev;
82 *pprev = e->env_next;
83 e->env_next = env_free_list;
84 env_free_list = e;
85 break;
88 ENV_CREATE(fs_bufcache);
90 #if defined(TEST)
91 // Don't touch -- used by grading script!
92 ENV_CREATE2(TEST, TESTSIZE);
93 #else
94 // Touch all you want.
95 // ENV_CREATE(user_writemotd);
96 // ENV_CREATE(user_testfile);
97 // ENV_CREATE(user_icode);
98 #endif // TEST*
101 // Schedule and run a user environment!
102 // We want to run the bufcache first.
103 env_run(&envs[ENVX(ENVID_BUFCACHE)]);
105 // Test IDT (lab 2 only)
106 //test_kernel_breakpoint();
108 // Test the stack backtrace function (lab 1 only)
109 //test_backtrace(5);
114 * Variable panicstr contains argument to first call to panic; used as flag
115 * to indicate that the kernel has already called panic.
117 static const char *panicstr;
120 * Panic is called on unresolvable fatal errors.
121 * It prints "panic: mesg", and then enters the kernel monitor.
123 void
124 _panic(const char *file, int line, const char *fmt, ...)
126 va_list ap;
128 if (panicstr)
129 goto dead;
130 panicstr = fmt;
132 // Be extra sure that the machine is in as reasonable state
133 __asm __volatile("cli; cld");
135 va_start(ap, fmt);
136 cprintf("kernel panic at %s:%d: ", file, line);
137 vcprintf(fmt, ap);
138 cprintf("\n");
139 va_end(ap);
141 dead:
142 /* break into the kernel monitor */
143 while (1)
144 monitor(NULL);
147 /* like panic, but don't */
148 void
149 _warn(const char *file, int line, const char *fmt, ...)
151 va_list ap;
153 va_start(ap, fmt);
154 cprintf("kernel warning at %s:%d: ", file, line);
155 vcprintf(fmt, ap);
156 cprintf("\n");
157 va_end(ap);