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