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 while (pos < cmd_line + LOADER_CMD_LINE_LEN)
157 ASSERT (argc < LOADER_CMD_LINE_LEN / 2);
161 pos = strchr (pos, '\0') + 1;
166 /* Parse the words. */
167 for (i = 0; i < argc; i++)
168 if (!strcmp (argv[i], "-rs"))
169 random_init (atoi (argv[++i]));
170 else if (!strcmp (argv[i], "-d"))
171 debug_enable (argv[++i]);
172 else if (!strcmp (argv[i], "-q"))
175 else if (!strcmp (argv[i], "-ex"))
176 initial_program = argv[++i];
179 else if (!strcmp (argv[i], "-f"))
180 format_filesys = true;
181 else if (!strcmp (argv[i], "-ci"))
183 fsutil_copyin_file = argv[++i];
184 fsutil_copyin_size = atoi (argv[++i]);
186 else if (!strcmp (argv[i], "-co"))
187 fsutil_copyout_file = argv[++i];
188 else if (!strcmp (argv[i], "-p"))
189 fsutil_print_file = argv[++i];
190 else if (!strcmp (argv[i], "-r"))
191 fsutil_remove_file = argv[++i];
192 else if (!strcmp (argv[i], "-ls"))
193 fsutil_list_files = true;
194 else if (!strcmp (argv[i], "-D"))
195 fsutil_dump_filesys = true;
197 else if (!strcmp (argv[i], "-u"))
201 " -rs SEED Seed random seed to SEED.\n"
202 " -d CLASS[,...] Enable the given classes of debug messages.\n"
204 " -ex 'PROG [ARG...]' Run PROG, passing the optional arguments.\n"
207 " -f Format the filesystem disk (hdb or hd0:1).\n"
208 " -ci FILENAME SIZE Copy SIZE bytes from the scratch disk (hdc\n"
209 " or hd1:0) into the filesystem as FILENAME\n"
210 " -co FILENAME Copy FILENAME to the scratch disk, with\n"
211 " size at start of sector 0 and data afterward\n"
212 " -p FILENAME Print the contents of FILENAME\n"
213 " -r FILENAME Delete FILENAME\n"
214 " -ls List the files in the filesystem\n"
215 " -D Dump complete filesystem contents\n"
217 " -q Power off after doing requested actions.\n"
221 PANIC ("unknown option `%s'", argv[i]);
227 const char s[] = "Shutdown";
234 printf ("Powering off...\n");
235 for (p = s; *p != '\0'; p++)