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