Redo makefiles.
[pintos-anon] / src / threads / init.c
1 #include "init.h"
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <limits.h>
5 #include "interrupt.h"
6 #include "io.h"
7 #include "loader.h"
8 #include "malloc.h"
9 #include "mmu.h"
10 #include "paging.h"
11 #include "palloc.h"
12 #include "thread.h"
13 #include "devices/kbd.h"
14 #include "devices/serial.h"
15 #include "devices/timer.h"
16 #include "devices/vga.h"
17 #include "lib/debug.h"
18 #include "lib/lib.h"
19 #include "lib/random.h"
20 #ifdef USERPROG
21 #include "userprog/exception.h"
22 #include "userprog/gdt.h"
23 #include "userprog/syscall.h"
24 #include "userprog/tss.h"
25 #endif
26 #ifdef FILESYS
27 #include "devices/disk.h"
28 #include "filesys/filesys.h"
29 #include "filesys/fsutil.h"
30 #endif
31
32 /* Amount of physical memory, in 4 kB pages. */
33 size_t ram_pages;
34
35 #ifdef FILESYS
36 /* Format the filesystem? */
37 static bool format_filesys;
38 #endif
39
40 #ifdef USERPROG
41 /* Initial program to run. */
42 static char *initial_program;
43 #endif
44
45 static void ram_init (void);
46 static void argv_init (void);
47
48 int main (void) NO_RETURN;
49
50 int
51 main (void)
52 {
53   /* Needed by printk(), so initialize them very early. */
54   ram_init ();
55   vga_init ();
56   serial_init ();
57
58   /* Greet user. */
59   printk ("Booting cnachos86 with %'d kB RAM...\n", ram_pages * 4);
60
61   /* Parse command line. */
62   argv_init ();
63
64   /* Initialize memory system, segments, paging. */
65   thread_init ();
66   palloc_init ();
67   paging_init ();
68 #ifdef USERPROG
69   tss_init ();
70   gdt_init ();
71 #endif
72   malloc_init ();
73
74   /* Set random seed if not already done. */
75   random_init (0);
76
77   /* Initialize interrupt handlers. */
78   intr_init ();
79   timer_init ();
80   kbd_init ();
81 #ifdef USERPROG
82   exception_init ();
83   syscall_init ();
84 #endif
85
86   /* Start thread scheduler and enable interrupts. */
87   thread_start ();
88
89 #ifdef FILESYS
90   /* Initialize filesystem. */
91   disk_init ();
92   filesys_init (format_filesys);
93   fsutil_run ();
94 #endif
95
96   printk ("Boot complete.\n");
97
98 #ifdef USERPROG
99   /* Run a user program. */
100   if (initial_program != NULL)
101     {
102       printk ("\nExecuting '%s':\n", initial_program);
103       thread_execute (initial_program); 
104     }
105 #endif
106
107   /* Terminate this thread. */
108   thread_exit ();
109 }
110 \f
111 /* Clear BSS and obtain RAM size from loader. */
112 static void
113 ram_init (void) 
114 {
115   /* The "BSS" is a segment that should be initialized to zeros.
116      It isn't actually stored on disk or zeroed by the kernel
117      loader, so we have to zero it ourselves.
118
119      The start and end of the BSS segment is recorded by the
120      linker as _start_bss and _end_bss.  See kernel.lds. */
121   extern char _start_bss, _end_bss;
122   memset (&_start_bss, 0, &_end_bss - &_start_bss);
123
124   /* Get RAM size from loader.  See loader.S. */
125   ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES);
126 }
127
128 /* Parses the command line. */
129 static void
130 argv_init (void) 
131 {
132   char *cmd_line, *pos;
133   char *argv[LOADER_CMD_LINE_LEN / 2 + 1];
134   int argc = 0;
135   int i;
136
137   /* The command line is made up of null terminated strings
138      followed by an empty string.  Break it up into words. */
139   cmd_line = pos = ptov (LOADER_CMD_LINE);
140   while (pos < cmd_line + LOADER_CMD_LINE_LEN)
141     {
142       ASSERT (argc < LOADER_CMD_LINE_LEN / 2);
143       if (*pos == '\0')
144         break;
145       argv[argc++] = pos;
146       pos = strchr (pos, '\0') + 1;
147     }
148   argv[argc] = "";
149
150   /* Parse the words. */
151   for (i = 0; i < argc; i++)
152     if (!strcmp (argv[i], "-rs")) 
153       random_init (atoi (argv[++i]));
154     else if (!strcmp (argv[i], "-d")) 
155       debug_enable (argv[++i]);
156 #ifdef USERPROG
157     else if (!strcmp (argv[i], "-ex")) 
158       initial_program = argv[++i];
159 #endif
160 #ifdef FILESYS
161   else if (!strcmp (argv[i], "-f"))
162       format_filesys = true;
163     else if (!strcmp (argv[i], "-cp")) 
164       fsutil_copy_arg = argv[++i];
165     else if (!strcmp (argv[i], "-p")) 
166       fsutil_print_file = argv[++i];
167     else if (!strcmp (argv[i], "-r"))
168       fsutil_remove_file = argv[++i];
169     else if (!strcmp (argv[i], "-ls"))
170       fsutil_list_files = true;
171     else if (!strcmp (argv[i], "-D"))
172       fsutil_dump_filesys = true;
173 #endif
174     else if (!strcmp (argv[i], "-u"))
175       {
176         printk (
177           "Kernel options:\n"
178           " -rs SEED            Seed random seed to SEED.\n"
179           " -d CLASS[,...]      Enable the given classes of debug messages.\n"
180 #ifdef USERPROG
181           " -ex 'PROG [ARG...]' Run PROG, passing the optional arguments.\n"
182 #endif
183 #ifdef FILESYS
184           " -f                  Format the filesystem disk (hdb or hd0:1).\n"
185           " -cp FILENAME:SIZE   Copy SIZE bytes from the scratch disk (hdc\n"
186           "                     or hd1:0) into the filesystem as FILENAME\n"
187           " -p FILENAME         Print the contents of FILENAME\n"
188           " -r FILENAME         Delete FILENAME\n"
189           " -ls                 List the files in the filesystem\n"
190           " -D                  Dump complete filesystem contents\n"
191 #endif
192           );
193       }
194     else 
195       PANIC ("unknown option `%s'", argv[i]);
196 }