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/palloc.h"
21 #include "threads/thread.h"
23 #include "userprog/process.h"
24 #include "userprog/exception.h"
25 #include "userprog/gdt.h"
26 #include "userprog/syscall.h"
27 #include "userprog/tss.h"
29 #include "tests/threads/tests.h"
32 #include "devices/disk.h"
33 #include "filesys/filesys.h"
34 #include "filesys/fsutil.h"
37 /* Amount of physical memory, in 4 kB pages. */
40 /* Page directory with kernel mappings only. */
41 uint32_t *base_page_dir;
44 If false (default), use round-robin scheduler.
45 If true, use multi-level feedback queue scheduler. */
49 /* -f: Format the filesystem? */
50 static bool format_filesys;
53 /* -q: Power off after kernel tasks complete? */
54 bool power_off_when_done;
56 static void ram_init (void);
57 static void paging_init (void);
59 static char **read_command_line (void);
60 static char **parse_options (char **argv);
61 static void run_actions (char **argv);
62 static void usage (void);
64 static void print_stats (void);
67 int main (void) NO_RETURN;
69 /* Pintos main program. */
75 /* Clear BSS and get machine's RAM size. */
78 /* Initialize ourselves as a thread so we can use locks. */
81 /* Initialize the console so we can use printf(). */
87 printf ("Pintos booting with %'zu kB RAM...\n", ram_pages * PGSIZE / 1024);
89 /* Break command line into arguments and parse options. */
90 argv = read_command_line ();
91 argv = parse_options (argv);
93 /* Initialize memory system. */
104 /* Set random seed if parse_options() didn't. */
107 /* Initialize interrupt handlers. */
116 /* Start thread scheduler and enable interrupts. */
118 serial_init_queue ();
122 /* Initialize filesystem. */
124 filesys_init (format_filesys);
127 printf ("Boot complete.\n");
129 /* Run actions specified on kernel command line. */
133 if (power_off_when_done)
138 /* Clear BSS and obtain RAM size from loader. */
142 /* The "BSS" is a segment that should be initialized to zeros.
143 It isn't actually stored on disk or zeroed by the kernel
144 loader, so we have to zero it ourselves.
146 The start and end of the BSS segment is recorded by the
147 linker as _start_bss and _end_bss. See kernel.lds. */
148 extern char _start_bss, _end_bss;
149 memset (&_start_bss, 0, &_end_bss - &_start_bss);
151 /* Get RAM size from loader. See loader.S. */
152 ram_pages = *(uint32_t *) ptov (LOADER_RAM_PGS);
155 /* Populates the base page directory and page table with the
156 kernel virtual mapping, and then sets up the CPU to use the
157 new page directory. Points base_page_dir to the page
158 directory it creates.
160 At the time this function is called, the active page table
161 (set up by loader.S) only maps the first 4 MB of RAM, so we
162 should not try to use extravagant amounts of memory.
163 Fortunately, there is no need to do so. */
170 pd = base_page_dir = palloc_get_page (PAL_ASSERT | PAL_ZERO);
172 for (page = 0; page < ram_pages; page++)
174 uintptr_t paddr = page * PGSIZE;
175 void *vaddr = ptov (paddr);
176 size_t pde_idx = pd_no (vaddr);
177 size_t pte_idx = pt_no (vaddr);
179 if (pd[pde_idx] == 0)
181 pt = palloc_get_page (PAL_ASSERT | PAL_ZERO);
182 pd[pde_idx] = pde_create (pt);
185 pt[pte_idx] = pte_create_kernel (vaddr, true);
188 /* Store the physical address of the page directory into CR3
189 aka PDBR (page directory base register). This activates our
190 new page tables immediately. See [IA32-v2a] "MOV--Move
191 to/from Control Registers" and [IA32-v3a] 3.7.5 "Base Address
192 of the Page Directory". */
193 asm volatile ("movl %0, %%cr3" :: "r" (vtop (base_page_dir)));
196 /* Breaks the kernel command line into words and returns them as
197 an argv-like array. */
199 read_command_line (void)
201 static char *argv[LOADER_ARGS_LEN / 2 + 1];
206 argc = *(uint32_t *) ptov (LOADER_ARG_CNT);
207 p = ptov (LOADER_ARGS);
208 end = p + LOADER_ARGS_LEN;
209 for (i = 0; i < argc; i++)
212 PANIC ("command line arguments overflow");
215 p += strnlen (p, end - p) + 1;
219 /* Print kernel command line. */
220 printf ("Kernel command line:");
221 for (i = 0; i < argc; i++)
222 printf (" %s", argv[i]);
228 /* Parses options in ARGV[]
229 and returns the first non-option argument. */
231 parse_options (char **argv)
233 for (; *argv != NULL && **argv == '-'; argv++)
236 char *name = strtok_r (*argv, "=", &save_ptr);
237 char *value = strtok_r (NULL, "", &save_ptr);
239 if (!strcmp (name, "-h"))
241 else if (!strcmp (name, "-q"))
242 power_off_when_done = true;
244 else if (!strcmp (name, "-f"))
245 format_filesys = true;
247 else if (!strcmp (name, "-rs"))
248 random_init (atoi (value));
249 else if (!strcmp (name, "-mlfqs"))
252 else if (!strcmp (name, "-ul"))
253 user_page_limit = atoi (value);
256 PANIC ("unknown option `%s' (use -h for help)", name);
262 /* Runs the task specified in ARGV[1]. */
264 run_task (char **argv)
266 const char *task = argv[1];
268 printf ("Executing '%s':\n", task);
270 process_wait (process_execute (task));
274 printf ("Execution of '%s' complete.\n", task);
277 /* Executes all of the actions specified in ARGV[]
278 up to the null pointer sentinel. */
280 run_actions (char **argv)
285 char *name; /* Action name. */
286 int argc; /* # of args, including action name. */
287 void (*function) (char **argv); /* Function to execute action. */
290 /* Table of supported actions. */
291 static const struct action actions[] =
293 {"run", 2, run_task},
295 {"ls", 1, fsutil_ls},
296 {"cat", 2, fsutil_cat},
297 {"rm", 2, fsutil_rm},
298 {"put", 2, fsutil_put},
299 {"get", 2, fsutil_get},
304 while (*argv != NULL)
306 const struct action *a;
309 /* Find action name. */
310 for (a = actions; ; a++)
312 PANIC ("unknown action `%s' (use -h for help)", *argv);
313 else if (!strcmp (*argv, a->name))
316 /* Check for required arguments. */
317 for (i = 1; i < a->argc; i++)
319 PANIC ("action `%s' requires %d argument(s)", *argv, a->argc - 1);
321 /* Invoke action and advance. */
328 /* Prints a kernel command line help message and powers off the
333 printf ("\nCommand line syntax: [OPTION...] [ACTION...]\n"
334 "Options must precede actions.\n"
335 "Actions are executed in the order specified.\n"
336 "\nAvailable actions:\n"
338 " run 'PROG [ARG...]' Run PROG and wait for it to complete.\n"
340 " run TEST Run TEST.\n"
343 " ls List files in the root directory.\n"
344 " cat FILE Print FILE to the console.\n"
345 " rm FILE Delete FILE.\n"
346 "Use these actions indirectly via `pintos' -g and -p options:\n"
347 " put FILE Put FILE into file system from scratch disk.\n"
348 " get FILE Get FILE from file system into scratch disk.\n"
351 " -h Print this help message and power off.\n"
352 " -q Power off VM after actions or on panic.\n"
353 " -f Format file system disk during startup.\n"
354 " -rs=SEED Set random number seed to SEED.\n"
355 " -mlfqs Use multi-level feedback queue scheduler.\n"
357 " -ul=COUNT Limit user memory to COUNT pages.\n"
364 /* Powers down the machine we're running on,
365 as long as we're running on Bochs or qemu. */
369 const char s[] = "Shutdown";
378 printf ("Powering off...\n");
381 for (p = s; *p != '\0'; p++)
386 /* Print statistics about Pintos execution. */
390 timer_print_stats ();
391 thread_print_stats ();
395 console_print_stats ();
398 exception_print_stats ();