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. */
50 If false (default), use LRU page replacement policy.
51 If true, use random page replacement policy. */
52 bool enable_random_paging;
56 /* -f: Format the filesystem? */
57 static bool format_filesys;
60 /* -q: Power off after kernel tasks complete? */
61 bool power_off_when_done;
63 static void ram_init (void);
64 static void paging_init (void);
66 static char **read_command_line (void);
67 static char **parse_options (char **argv);
68 static void run_actions (char **argv);
69 static void usage (void);
71 static void print_stats (void);
74 int main (void) NO_RETURN;
76 /* Pintos main program. */
82 /* Clear BSS and get machine's RAM size. */
85 /* Initialize ourselves as a thread so we can use locks. */
88 /* Initialize the console so we can use printf(). */
94 printf ("Pintos booting with %'zu kB RAM...\n", ram_pages * PGSIZE / 1024);
96 /* Break command line into arguments and parse options. */
97 argv = read_command_line ();
98 argv = parse_options (argv);
100 /* Initialize memory system. */
111 /* Set random seed if parse_options() didn't. */
114 /* Initialize interrupt handlers. */
123 /* Start thread scheduler and enable interrupts. */
125 serial_init_queue ();
129 /* Initialize filesystem. */
131 filesys_init (format_filesys);
134 printf ("Boot complete.\n");
136 /* Run actions specified on kernel command line. */
140 if (power_off_when_done)
145 /* Clear BSS and obtain RAM size from loader. */
149 /* The "BSS" is a segment that should be initialized to zeros.
150 It isn't actually stored on disk or zeroed by the kernel
151 loader, so we have to zero it ourselves.
153 The start and end of the BSS segment is recorded by the
154 linker as _start_bss and _end_bss. See kernel.lds. */
155 extern char _start_bss, _end_bss;
156 memset (&_start_bss, 0, &_end_bss - &_start_bss);
158 /* Get RAM size from loader. See loader.S. */
159 ram_pages = *(uint32_t *) ptov (LOADER_RAM_PGS);
162 /* Populates the base page directory and page table with the
163 kernel virtual mapping, and then sets up the CPU to use the
164 new page directory. Points base_page_dir to the page
165 directory it creates.
167 At the time this function is called, the active page table
168 (set up by loader.S) only maps the first 4 MB of RAM, so we
169 should not try to use extravagant amounts of memory.
170 Fortunately, there is no need to do so. */
177 pd = base_page_dir = palloc_get_page (PAL_ASSERT | PAL_ZERO);
179 for (page = 0; page < ram_pages; page++)
181 uintptr_t paddr = page * PGSIZE;
182 void *vaddr = ptov (paddr);
183 size_t pde_idx = pd_no (vaddr);
184 size_t pte_idx = pt_no (vaddr);
186 if (pd[pde_idx] == 0)
188 pt = palloc_get_page (PAL_ASSERT | PAL_ZERO);
189 pd[pde_idx] = pde_create (pt);
192 pt[pte_idx] = pte_create_kernel (vaddr, true);
195 /* Store the physical address of the page directory into CR3
196 aka PDBR (page directory base register). This activates our
197 new page tables immediately. See [IA32-v2a] "MOV--Move
198 to/from Control Registers" and [IA32-v3] 3.7.5. */
199 asm volatile ("movl %0, %%cr3" :: "r" (vtop (base_page_dir)));
202 /* Breaks the kernel command line into words and returns them as
203 an argv-like array. */
205 read_command_line (void)
207 static char *argv[LOADER_ARGS_LEN / 2 + 1];
212 argc = *(uint32_t *) ptov (LOADER_ARG_CNT);
213 p = ptov (LOADER_ARGS);
214 end = p + LOADER_ARGS_LEN;
215 for (i = 0; i < argc; i++)
218 PANIC ("command line arguments overflow");
221 p += strnlen (p, end - p) + 1;
225 /* Print kernel command line. */
226 printf ("Kernel command line:");
227 for (i = 0; i < argc; i++)
228 printf (" %s", argv[i]);
234 /* Parses options in ARGV[]
235 and returns the first non-option argument. */
237 parse_options (char **argv)
239 for (; *argv != NULL && **argv == '-'; argv++)
242 char *name = strtok_r (*argv, "=", &save_ptr);
243 char *value = strtok_r (NULL, "", &save_ptr);
245 if (!strcmp (name, "-h"))
247 else if (!strcmp (name, "-q"))
248 power_off_when_done = true;
250 else if (!strcmp (name, "-f"))
251 format_filesys = true;
253 else if (!strcmp (name, "-rs"))
254 random_init (atoi (value));
255 else if (!strcmp (name, "-mlfqs"))
258 else if (!strcmp (name, "-ul"))
259 user_page_limit = atoi (value);
262 else if (!strcmp (name, "-rndpg"))
263 enable_random_paging = true;
266 PANIC ("unknown option `%s' (use -h for help)", name);
273 /* Runs the task specified in ARGV[1]. */
275 run_task (char **argv)
277 const char *task = argv[1];
279 printf ("Executing '%s':\n", task);
281 process_wait (process_execute (task));
285 printf ("Execution of '%s' complete.\n", task);
288 /* Executes all of the actions specified in ARGV[]
289 up to the null pointer sentinel. */
291 run_actions (char **argv)
296 char *name; /* Action name. */
297 int argc; /* # of args, including action name. */
298 void (*function) (char **argv); /* Function to execute action. */
301 /* Table of supported actions. */
302 static const struct action actions[] =
304 {"run", 2, run_task},
306 {"ls", 1, fsutil_ls},
307 {"cat", 2, fsutil_cat},
308 {"rm", 2, fsutil_rm},
309 {"put", 2, fsutil_put},
310 {"get", 2, fsutil_get},
315 while (*argv != NULL)
317 const struct action *a;
320 /* Find action name. */
321 for (a = actions; ; a++)
323 PANIC ("unknown action `%s' (use -h for help)", *argv);
324 else if (!strcmp (*argv, a->name))
327 /* Check for required arguments. */
328 for (i = 1; i < a->argc; i++)
330 PANIC ("action `%s' requires %d argument(s)", *argv, a->argc - 1);
332 /* Invoke action and advance. */
339 /* Prints a kernel command line help message and powers off the
344 printf ("\nCommand line syntax: [OPTION...] [ACTION...]\n"
345 "Options must precede actions.\n"
346 "Actions are executed in the order specified.\n"
347 "\nAvailable actions:\n"
349 " run 'PROG [ARG...]' Run PROG and wait for it to complete.\n"
351 " run TEST Run TEST.\n"
354 " ls List files in the root directory.\n"
355 " cat FILE Print FILE to the console.\n"
356 " rm FILE Delete FILE.\n"
357 "Use these actions indirectly via `pintos' -g and -p options:\n"
358 " put FILE Put FILE into file system from scratch disk.\n"
359 " get FILE Get FILE from file system into scratch disk.\n"
362 " -h Print this help message and power off.\n"
363 " -q Power off VM after actions or on panic.\n"
364 " -f Format file system disk during startup.\n"
365 " -rs=SEED Set random number seed to SEED.\n"
366 " -mlfqs Use multi-level feedback queue scheduler.\n"
368 " -ul=COUNT Limit user memory to COUNT pages.\n"
371 " -rndpg Use random page replacement policy.\n"
378 /* Powers down the machine we're running on,
379 as long as we're running on Bochs or qemu. */
383 const char s[] = "Shutdown";
392 printf ("Powering off...\n");
395 for (p = s; *p != '\0'; p++)
400 /* Print statistics about Pintos execution. */
404 timer_print_stats ();
405 thread_print_stats ();
409 console_print_stats ();
412 exception_print_stats ();