1 #include "threads/init.h"
11 #include "devices/kbd.h"
12 #include "devices/input.h"
13 #include "devices/serial.h"
14 #include "devices/shutdown.h"
15 #include "devices/timer.h"
16 #include "devices/vga.h"
17 #include "devices/rtc.h"
18 #include "threads/interrupt.h"
19 #include "threads/io.h"
20 #include "threads/loader.h"
21 #include "threads/malloc.h"
22 #include "threads/palloc.h"
23 #include "threads/pte.h"
24 #include "threads/thread.h"
26 #include "userprog/process.h"
27 #include "userprog/exception.h"
28 #include "userprog/gdt.h"
29 #include "userprog/syscall.h"
30 #include "userprog/tss.h"
32 #include "tests/threads/tests.h"
35 #include "devices/disk.h"
36 #include "filesys/filesys.h"
37 #include "filesys/fsutil.h"
40 /* Amount of physical memory, in 4 kB pages. */
43 /* Page directory with kernel mappings only. */
44 uint32_t *init_page_dir;
47 /* -f: Format the file system? */
48 static bool format_filesys;
51 /* -q: Power off after kernel tasks complete? */
52 bool power_off_when_done;
54 /* -r: Reboot after kernel tasks complete? */
55 static bool reboot_when_done;
57 /* -ul: Maximum number of pages to put into palloc's user pool. */
58 static size_t user_page_limit = SIZE_MAX;
60 static void ram_init (void);
61 static void paging_init (void);
63 static char **read_command_line (void);
64 static char **parse_options (char **argv);
65 static void run_actions (char **argv);
66 static void usage (void);
68 int main (void) NO_RETURN;
70 /* Pintos main program. */
76 /* Clear BSS and get machine's RAM size. */
79 /* Break command line into arguments and parse options. */
80 argv = read_command_line ();
81 argv = parse_options (argv);
83 /* Initialize ourselves as a thread so we can use locks,
84 then enable console locking. */
89 printf ("Pintos booting with %'zu kB RAM...\n", ram_pages * PGSIZE / 1024);
91 /* Initialize memory system. */
92 palloc_init (user_page_limit);
102 /* Initialize interrupt handlers. */
112 /* Start thread scheduler and enable interrupts. */
114 serial_init_queue ();
118 /* Initialize file system. */
120 filesys_init (format_filesys);
123 printf ("Boot complete.\n");
125 /* Run actions specified on kernel command line. */
129 if (reboot_when_done)
132 if (power_off_when_done)
133 shutdown_power_off ();
137 /* Clear BSS and obtain RAM size from loader. */
141 /* The "BSS" is a segment that should be initialized to zeros.
142 It isn't actually stored on disk or zeroed by the kernel
143 loader, so we have to zero it ourselves.
145 The start and end of the BSS segment is recorded by the
146 linker as _start_bss and _end_bss. See kernel.lds. */
147 extern char _start_bss, _end_bss;
148 memset (&_start_bss, 0, &_end_bss - &_start_bss);
150 /* Get RAM size from loader. See loader.S. */
151 ram_pages = *(uint32_t *) ptov (LOADER_RAM_PGS);
154 /* Populates the base page directory and page table with the
155 kernel virtual mapping, and then sets up the CPU to use the
156 new page directory. Points init_page_dir to the page
157 directory it creates.
159 At the time this function is called, the active page table
160 (set up by loader.S) only maps the first 4 MB of RAM, so we
161 should not try to use extravagant amounts of memory.
162 Fortunately, there is no need to do so. */
168 extern char _start, _end_kernel_text;
170 pd = init_page_dir = palloc_get_page (PAL_ASSERT | PAL_ZERO);
172 for (page = 0; page < ram_pages; page++)
174 uintptr_t paddr = page * PGSIZE;
175 char *vaddr = ptov (paddr);
176 size_t pde_idx = pd_no (vaddr);
177 size_t pte_idx = pt_no (vaddr);
178 bool in_kernel_text = &_start <= vaddr && vaddr < &_end_kernel_text;
180 if (pd[pde_idx] == 0)
182 pt = palloc_get_page (PAL_ASSERT | PAL_ZERO);
183 pd[pde_idx] = pde_create (pt);
186 pt[pte_idx] = pte_create_kernel (vaddr, !in_kernel_text);
189 /* Store the physical address of the page directory into CR3
190 aka PDBR (page directory base register). This activates our
191 new page tables immediately. See [IA32-v2a] "MOV--Move
192 to/from Control Registers" and [IA32-v3a] 3.7.5 "Base Address
193 of the Page Directory". */
194 asm volatile ("movl %0, %%cr3" : : "r" (vtop (init_page_dir)));
197 /* Breaks the kernel command line into words and returns them as
198 an argv-like array. */
200 read_command_line (void)
202 static char *argv[LOADER_ARGS_LEN / 2 + 1];
207 argc = *(uint32_t *) ptov (LOADER_ARG_CNT);
208 p = ptov (LOADER_ARGS);
209 end = p + LOADER_ARGS_LEN;
210 for (i = 0; i < argc; i++)
213 PANIC ("command line arguments overflow");
216 p += strnlen (p, end - p) + 1;
220 /* Print kernel command line. */
221 printf ("Kernel command line:");
222 for (i = 0; i < argc; i++)
223 if (strchr (argv[i], ' ') == NULL)
224 printf (" %s", argv[i]);
226 printf (" '%s'", argv[i]);
232 /* Parses options in ARGV[]
233 and returns the first non-option argument. */
235 parse_options (char **argv)
237 for (; *argv != NULL && **argv == '-'; argv++)
240 char *name = strtok_r (*argv, "=", &save_ptr);
241 char *value = strtok_r (NULL, "", &save_ptr);
243 if (!strcmp (name, "-h"))
245 else if (!strcmp (name, "-q"))
246 power_off_when_done = true;
247 else if (!strcmp (name, "-r"))
248 reboot_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 PANIC ("unknown option `%s' (use -h for help)", name);
265 /* Initialize the random number generator based on the system
266 time. This has no effect if an "-rs" option was specified.
268 When running under Bochs, this is not enough by itself to
269 get a good seed value, because the pintos script sets the
270 initial time to a predictable value, not to the local time,
271 for reproducibility. To fix this, give the "-r" option to
272 the pintos script to request real-time execution. */
273 random_init (rtc_get_time ());
278 /* Runs the task specified in ARGV[1]. */
280 run_task (char **argv)
282 const char *task = argv[1];
284 printf ("Executing '%s':\n", task);
286 process_wait (process_execute (task));
290 printf ("Execution of '%s' complete.\n", task);
293 /* Executes all of the actions specified in ARGV[]
294 up to the null pointer sentinel. */
296 run_actions (char **argv)
301 char *name; /* Action name. */
302 int argc; /* # of args, including action name. */
303 void (*function) (char **argv); /* Function to execute action. */
306 /* Table of supported actions. */
307 static const struct action actions[] =
309 {"run", 2, run_task},
311 {"ls", 1, fsutil_ls},
312 {"cat", 2, fsutil_cat},
313 {"rm", 2, fsutil_rm},
314 {"extract", 1, fsutil_extract},
315 {"append", 2, fsutil_append},
320 while (*argv != NULL)
322 const struct action *a;
325 /* Find action name. */
326 for (a = actions; ; a++)
328 PANIC ("unknown action `%s' (use -h for help)", *argv);
329 else if (!strcmp (*argv, a->name))
332 /* Check for required arguments. */
333 for (i = 1; i < a->argc; i++)
335 PANIC ("action `%s' requires %d argument(s)", *argv, a->argc - 1);
337 /* Invoke action and advance. */
344 /* Prints a kernel command line help message and powers off the
349 printf ("\nCommand line syntax: [OPTION...] [ACTION...]\n"
350 "Options must precede actions.\n"
351 "Actions are executed in the order specified.\n"
352 "\nAvailable actions:\n"
354 " run 'PROG [ARG...]' Run PROG and wait for it to complete.\n"
356 " run TEST Run TEST.\n"
359 " ls List files in the root directory.\n"
360 " cat FILE Print FILE to the console.\n"
361 " rm FILE Delete FILE.\n"
362 "Use these actions indirectly via `pintos' -g and -p options:\n"
363 " extract Untar from scratch disk into file system.\n"
364 " append FILE Append FILE to tar file on scratch disk.\n"
367 " -h Print this help message and power off.\n"
368 " -q Power off VM after actions or on panic.\n"
369 " -r Reboot after actions.\n"
370 " -f Format file system disk during startup.\n"
371 " -rs=SEED Set random number seed to SEED.\n"
372 " -mlfqs Use multi-level feedback queue scheduler.\n"
374 " -ul=COUNT Limit user memory to COUNT pages.\n"
377 shutdown_power_off ();