1 #include "threads/init.h"
10 #include "devices/kbd.h"
11 #include "devices/serial.h"
12 #include "devices/timer.h"
13 #include "devices/vga.h"
14 #include "threads/interrupt.h"
15 #include "threads/io.h"
16 #include "threads/loader.h"
17 #include "threads/malloc.h"
18 #include "threads/mmu.h"
19 #include "threads/paging.h"
20 #include "threads/palloc.h"
21 #include "threads/thread.h"
23 #include "userprog/exception.h"
24 #include "userprog/gdt.h"
25 #include "userprog/syscall.h"
26 #include "userprog/tss.h"
29 #include "devices/disk.h"
30 #include "filesys/filesys.h"
31 #include "filesys/fsutil.h"
34 /* Amount of physical memory, in 4 kB pages. */
38 /* Format the filesystem? */
39 static bool format_filesys;
43 /* Initial program to run. */
44 static char *initial_program;
47 static void ram_init (void);
48 static void argv_init (void);
50 int main (void) NO_RETURN;
55 /* Needed by printf(), so initialize them very early. */
61 printf ("Booting cnachos86 with %'d kB RAM...\n", ram_pages * 4);
63 /* Parse command line. */
66 /* Initialize memory system, segments, paging. */
76 /* Set random seed if not already done. */
79 /* Initialize interrupt handlers. */
88 /* Start thread scheduler and enable interrupts. */
92 /* Initialize filesystem. */
94 filesys_init (format_filesys);
98 printf ("Boot complete.\n");
101 /* Run a user program. */
102 if (initial_program != NULL)
104 printf ("\nExecuting '%s':\n", initial_program);
105 thread_execute (initial_program);
109 /* Terminate this thread. */
113 /* Clear BSS and obtain RAM size from loader. */
117 /* The "BSS" is a segment that should be initialized to zeros.
118 It isn't actually stored on disk or zeroed by the kernel
119 loader, so we have to zero it ourselves.
121 The start and end of the BSS segment is recorded by the
122 linker as _start_bss and _end_bss. See kernel.lds. */
123 extern char _start_bss, _end_bss;
124 memset (&_start_bss, 0, &_end_bss - &_start_bss);
126 /* Get RAM size from loader. See loader.S. */
127 ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES);
130 /* Parses the command line. */
134 char *cmd_line, *pos;
135 char *argv[LOADER_CMD_LINE_LEN / 2 + 1];
139 /* The command line is made up of null terminated strings
140 followed by an empty string. Break it up into words. */
141 cmd_line = pos = ptov (LOADER_CMD_LINE);
142 while (pos < cmd_line + LOADER_CMD_LINE_LEN)
144 ASSERT (argc < LOADER_CMD_LINE_LEN / 2);
148 pos = strchr (pos, '\0') + 1;
152 /* Parse the words. */
153 for (i = 0; i < argc; i++)
154 if (!strcmp (argv[i], "-rs"))
155 random_init (atoi (argv[++i]));
156 else if (!strcmp (argv[i], "-d"))
157 debug_enable (argv[++i]);
159 else if (!strcmp (argv[i], "-ex"))
160 initial_program = argv[++i];
163 else if (!strcmp (argv[i], "-f"))
164 format_filesys = true;
165 else if (!strcmp (argv[i], "-cp"))
166 fsutil_copy_arg = argv[++i];
167 else if (!strcmp (argv[i], "-p"))
168 fsutil_print_file = argv[++i];
169 else if (!strcmp (argv[i], "-r"))
170 fsutil_remove_file = argv[++i];
171 else if (!strcmp (argv[i], "-ls"))
172 fsutil_list_files = true;
173 else if (!strcmp (argv[i], "-D"))
174 fsutil_dump_filesys = true;
176 else if (!strcmp (argv[i], "-u"))
180 " -rs SEED Seed random seed to SEED.\n"
181 " -d CLASS[,...] Enable the given classes of debug messages.\n"
183 " -ex 'PROG [ARG...]' Run PROG, passing the optional arguments.\n"
186 " -f Format the filesystem disk (hdb or hd0:1).\n"
187 " -cp FILENAME:SIZE Copy SIZE bytes from the scratch disk (hdc\n"
188 " or hd1:0) into the filesystem as FILENAME\n"
189 " -p FILENAME Print the contents of FILENAME\n"
190 " -r FILENAME Delete FILENAME\n"
191 " -ls List the files in the filesystem\n"
192 " -D Dump complete filesystem contents\n"
197 PANIC ("unknown option `%s'", argv[i]);