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. */
41 size_t init_ram_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 /* -ul: Maximum number of pages to put into palloc's user pool. */
52 static size_t user_page_limit = SIZE_MAX;
54 static void ram_init (void);
55 static void paging_init (void);
57 static char **read_command_line (void);
58 static char **parse_options (char **argv);
59 static void run_actions (char **argv);
60 static void usage (void);
62 int main (void) NO_RETURN;
64 /* Pintos main program. */
70 /* Clear BSS and get machine's RAM size. */
73 /* Break command line into arguments and parse options. */
74 argv = read_command_line ();
75 argv = parse_options (argv);
77 /* Initialize ourselves as a thread so we can use locks,
78 then enable console locking. */
83 printf ("Pintos booting with %'zu kB RAM...\n",
84 init_ram_pages * PGSIZE / 1024);
86 /* Initialize memory system. */
87 palloc_init (user_page_limit);
97 /* Initialize interrupt handlers. */
107 /* Start thread scheduler and enable interrupts. */
109 serial_init_queue ();
113 /* Initialize file system. */
115 filesys_init (format_filesys);
118 printf ("Boot complete.\n");
120 /* Run actions specified on kernel command line. */
128 /* Clear BSS and obtain RAM size from loader. */
132 /* The "BSS" is a segment that should be initialized to zeros.
133 It isn't actually stored on disk or zeroed by the kernel
134 loader, so we have to zero it ourselves.
136 The start and end of the BSS segment is recorded by the
137 linker as _start_bss and _end_bss. See kernel.lds. */
138 extern char _start_bss, _end_bss;
139 memset (&_start_bss, 0, &_end_bss - &_start_bss);
141 /* Get RAM size from loader. See loader.S. */
142 init_ram_pages = *(uint32_t *) ptov (LOADER_RAM_PGS);
145 /* Populates the base page directory and page table with the
146 kernel virtual mapping, and then sets up the CPU to use the
147 new page directory. Points init_page_dir to the page
148 directory it creates.
150 At the time this function is called, the active page table
151 (set up by loader.S) only maps the first 4 MB of RAM, so we
152 should not try to use extravagant amounts of memory.
153 Fortunately, there is no need to do so. */
159 extern char _start, _end_kernel_text;
161 pd = init_page_dir = palloc_get_page (PAL_ASSERT | PAL_ZERO);
163 for (page = 0; page < init_ram_pages; page++)
165 uintptr_t paddr = page * PGSIZE;
166 char *vaddr = ptov (paddr);
167 size_t pde_idx = pd_no (vaddr);
168 size_t pte_idx = pt_no (vaddr);
169 bool in_kernel_text = &_start <= vaddr && vaddr < &_end_kernel_text;
171 if (pd[pde_idx] == 0)
173 pt = palloc_get_page (PAL_ASSERT | PAL_ZERO);
174 pd[pde_idx] = pde_create (pt);
177 pt[pte_idx] = pte_create_kernel (vaddr, !in_kernel_text);
180 /* Store the physical address of the page directory into CR3
181 aka PDBR (page directory base register). This activates our
182 new page tables immediately. See [IA32-v2a] "MOV--Move
183 to/from Control Registers" and [IA32-v3a] 3.7.5 "Base Address
184 of the Page Directory". */
185 asm volatile ("movl %0, %%cr3" : : "r" (vtop (init_page_dir)));
188 /* Breaks the kernel command line into words and returns them as
189 an argv-like array. */
191 read_command_line (void)
193 static char *argv[LOADER_ARGS_LEN / 2 + 1];
198 argc = *(uint32_t *) ptov (LOADER_ARG_CNT);
199 p = ptov (LOADER_ARGS);
200 end = p + LOADER_ARGS_LEN;
201 for (i = 0; i < argc; i++)
204 PANIC ("command line arguments overflow");
207 p += strnlen (p, end - p) + 1;
211 /* Print kernel command line. */
212 printf ("Kernel command line:");
213 for (i = 0; i < argc; i++)
214 if (strchr (argv[i], ' ') == NULL)
215 printf (" %s", argv[i]);
217 printf (" '%s'", argv[i]);
223 /* Parses options in ARGV[]
224 and returns the first non-option argument. */
226 parse_options (char **argv)
228 for (; *argv != NULL && **argv == '-'; argv++)
231 char *name = strtok_r (*argv, "=", &save_ptr);
232 char *value = strtok_r (NULL, "", &save_ptr);
234 if (!strcmp (name, "-h"))
236 else if (!strcmp (name, "-q"))
237 shutdown_configure (SHUTDOWN_POWER_OFF);
238 else if (!strcmp (name, "-r"))
239 shutdown_configure (SHUTDOWN_REBOOT);
241 else if (!strcmp (name, "-f"))
242 format_filesys = true;
244 else if (!strcmp (name, "-rs"))
245 random_init (atoi (value));
246 else if (!strcmp (name, "-mlfqs"))
249 else if (!strcmp (name, "-ul"))
250 user_page_limit = atoi (value);
253 PANIC ("unknown option `%s' (use -h for help)", name);
256 /* Initialize the random number generator based on the system
257 time. This has no effect if an "-rs" option was specified.
259 When running under Bochs, this is not enough by itself to
260 get a good seed value, because the pintos script sets the
261 initial time to a predictable value, not to the local time,
262 for reproducibility. To fix this, give the "-r" option to
263 the pintos script to request real-time execution. */
264 random_init (rtc_get_time ());
269 /* Runs the task specified in ARGV[1]. */
271 run_task (char **argv)
273 const char *task = argv[1];
275 printf ("Executing '%s':\n", task);
277 process_wait (process_execute (task));
281 printf ("Execution of '%s' complete.\n", task);
284 /* Executes all of the actions specified in ARGV[]
285 up to the null pointer sentinel. */
287 run_actions (char **argv)
292 char *name; /* Action name. */
293 int argc; /* # of args, including action name. */
294 void (*function) (char **argv); /* Function to execute action. */
297 /* Table of supported actions. */
298 static const struct action actions[] =
300 {"run", 2, run_task},
302 {"ls", 1, fsutil_ls},
303 {"cat", 2, fsutil_cat},
304 {"rm", 2, fsutil_rm},
305 {"extract", 1, fsutil_extract},
306 {"append", 2, fsutil_append},
311 while (*argv != NULL)
313 const struct action *a;
316 /* Find action name. */
317 for (a = actions; ; a++)
319 PANIC ("unknown action `%s' (use -h for help)", *argv);
320 else if (!strcmp (*argv, a->name))
323 /* Check for required arguments. */
324 for (i = 1; i < a->argc; i++)
326 PANIC ("action `%s' requires %d argument(s)", *argv, a->argc - 1);
328 /* Invoke action and advance. */
335 /* Prints a kernel command line help message and powers off the
340 printf ("\nCommand line syntax: [OPTION...] [ACTION...]\n"
341 "Options must precede actions.\n"
342 "Actions are executed in the order specified.\n"
343 "\nAvailable actions:\n"
345 " run 'PROG [ARG...]' Run PROG and wait for it to complete.\n"
347 " run TEST Run TEST.\n"
350 " ls List files in the root directory.\n"
351 " cat FILE Print FILE to the console.\n"
352 " rm FILE Delete FILE.\n"
353 "Use these actions indirectly via `pintos' -g and -p options:\n"
354 " extract Untar from scratch disk into file system.\n"
355 " append FILE Append FILE to tar file on scratch disk.\n"
358 " -h Print this help message and power off.\n"
359 " -q Power off VM after actions or on panic.\n"
360 " -r Reboot after actions.\n"
361 " -f Format file system disk during startup.\n"
362 " -rs=SEED Set random number seed to SEED.\n"
363 " -mlfqs Use multi-level feedback queue scheduler.\n"
365 " -ul=COUNT Limit user memory to COUNT pages.\n"
368 shutdown_power_off ();