1 /* See COPYRIGHT for copyright information. */
4 #include <inc/string.h>
5 #include <inc/assert.h>
7 #include <kern/monitor.h>
8 #include <kern/console.h>
10 #include <kern/kclock.h>
11 #include <kern/trap.h>
13 #include <kern/sched.h>
14 #include <kern/picirq.h>
16 // Test the stack backtrace function (used in lab 1 only)
20 cprintf("entering test_backtrace %d\n", x
);
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
));
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");
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!
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
61 // Lab 2 interrupt and gate descriptor initialization functions
64 // Lab 3 user environment initialization functions
67 // Lab 4 multitasking initialization functions
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
;
83 e
->env_next
= env_free_list
;
88 ENV_CREATE(fs_bufcache
);
91 // Don't touch -- used by grading script!
92 ENV_CREATE2(TEST
, TESTSIZE
);
94 // Touch all you want.
95 // ENV_CREATE(user_writemotd);
96 // ENV_CREATE(user_testfile);
97 // ENV_CREATE(user_icode);
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)
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.
124 _panic(const char *file
, int line
, const char *fmt
, ...)
132 // Be extra sure that the machine is in as reasonable state
133 __asm
__volatile("cli; cld");
136 cprintf("kernel panic at %s:%d: ", file
, line
);
142 /* break into the kernel monitor */
147 /* like panic, but don't */
149 _warn(const char *file
, int line
, const char *fmt
, ...)
154 cprintf("kernel warning at %s:%d: ", file
, line
);