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