1 #include "threads/init.h"
11 #include "devices/kbd.h"
12 #include "devices/serial.h"
13 #include "devices/timer.h"
14 #include "devices/vga.h"
15 #include "threads/interrupt.h"
16 #include "threads/io.h"
17 #include "threads/loader.h"
18 #include "threads/malloc.h"
19 #include "threads/mmu.h"
20 #include "threads/paging.h"
21 #include "threads/palloc.h"
22 #include "threads/test.h"
23 #include "threads/thread.h"
25 #include "userprog/exception.h"
26 #include "userprog/gdt.h"
27 #include "userprog/syscall.h"
28 #include "userprog/tss.h"
31 #include "devices/disk.h"
32 #include "filesys/filesys.h"
33 #include "filesys/fsutil.h"
36 /* Amount of physical memory, in 4 kB pages. */
40 /* Format the filesystem? */
41 static bool format_filesys;
45 /* Initial program to run. */
46 static char *initial_program;
49 /* Power off after running requested actions? */
50 static bool power_off;
52 static void ram_init (void);
53 static void argv_init (void);
54 static void do_power_off (void);
56 int main (void) NO_RETURN;
61 /* Initialize everything needed for printf() first. */
69 printf ("Pintos booting with %'zd kB RAM...\n", ram_pages * (PGSIZE / 1024));
71 /* Parse command line. */
74 /* Initialize memory system, segments, paging. */
83 /* Set random seed if argv_init() didn't. */
86 /* Initialize interrupt handlers. */
95 /* Start thread scheduler and enable interrupts. */
100 /* Initialize filesystem. */
102 filesys_init (format_filesys);
106 printf ("Boot complete.\n");
109 /* Run a user program. */
110 if (initial_program != NULL)
112 printf ("\nExecuting '%s':\n", initial_program);
113 thread_execute (initial_program);
122 /* Terminate this thread. */
126 /* Clear BSS and obtain RAM size from loader. */
130 /* The "BSS" is a segment that should be initialized to zeros.
131 It isn't actually stored on disk or zeroed by the kernel
132 loader, so we have to zero it ourselves.
134 The start and end of the BSS segment is recorded by the
135 linker as _start_bss and _end_bss. See kernel.lds. */
136 extern char _start_bss, _end_bss;
137 memset (&_start_bss, 0, &_end_bss - &_start_bss);
139 /* Get RAM size from loader. See loader.S. */
140 ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES);
143 /* Parses the command line. */
147 char *cmd_line, *pos;
148 char *argv[LOADER_CMD_LINE_LEN / 2 + 2];
152 /* The command line is made up of null terminated strings
153 followed by an empty string. Break it up into words. */
154 cmd_line = pos = ptov (LOADER_CMD_LINE);
155 printf ("Kernel command line:");
156 while (pos < cmd_line + LOADER_CMD_LINE_LEN)
158 ASSERT (argc < LOADER_CMD_LINE_LEN / 2);
163 pos = strchr (pos, '\0') + 1;
169 /* Parse the words. */
170 for (i = 0; i < argc; i++)
171 if (!strcmp (argv[i], "-rs"))
172 random_init (atoi (argv[++i]));
173 else if (!strcmp (argv[i], "-d"))
174 debug_enable (argv[++i]);
175 else if (!strcmp (argv[i], "-q"))
178 else if (!strcmp (argv[i], "-ex"))
179 initial_program = argv[++i];
182 else if (!strcmp (argv[i], "-f"))
183 format_filesys = true;
184 else if (!strcmp (argv[i], "-ci"))
186 fsutil_copyin_file = argv[++i];
187 fsutil_copyin_size = atoi (argv[++i]);
189 else if (!strcmp (argv[i], "-co"))
190 fsutil_copyout_file = argv[++i];
191 else if (!strcmp (argv[i], "-p"))
192 fsutil_print_file = argv[++i];
193 else if (!strcmp (argv[i], "-r"))
194 fsutil_remove_file = argv[++i];
195 else if (!strcmp (argv[i], "-ls"))
196 fsutil_list_files = true;
197 else if (!strcmp (argv[i], "-D"))
198 fsutil_dump_filesys = true;
200 else if (!strcmp (argv[i], "-u"))
204 " -rs SEED Seed random seed to SEED.\n"
205 " -d CLASS[,...] Enable the given classes of debug messages.\n"
207 " -ex 'PROG [ARG...]' Run PROG, passing the optional arguments.\n"
210 " -f Format the filesystem disk (hdb or hd0:1).\n"
211 " -ci FILENAME SIZE Copy SIZE bytes from the scratch disk (hdc\n"
212 " or hd1:0) into the filesystem as FILENAME\n"
213 " -co FILENAME Copy FILENAME to the scratch disk, with\n"
214 " size at start of sector 0 and data afterward\n"
215 " -p FILENAME Print the contents of FILENAME\n"
216 " -r FILENAME Delete FILENAME\n"
217 " -ls List the files in the filesystem\n"
218 " -D Dump complete filesystem contents\n"
220 " -q Power off after doing requested actions.\n"
221 " -u Print this help message and power off.\n"
226 PANIC ("unknown option `%s' (use -u for help)", argv[i]);
232 const char s[] = "Shutdown";
239 printf ("Powering off...\n");
240 for (p = s; *p != '\0'; p++)