[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
init.c File Reference
#include "threads/init.h"
#include <console.h>
#include <debug.h>
#include <limits.h>
#include <random.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "devices/kbd.h"
#include "devices/input.h"
#include "devices/serial.h"
#include "devices/timer.h"
#include "devices/vga.h"
#include "threads/interrupt.h"
#include "threads/io.h"
#include "threads/loader.h"
#include "threads/malloc.h"
#include "threads/mmu.h"
#include "threads/palloc.h"
#include "threads/pte.h"
#include "threads/thread.h"
#include "tests/threads/tests.h"
Include dependency graph for init.c:

Functions

static void bss_init (void)
 
static void paging_init (uint64_t mem_end)
 
static char ** read_command_line (void)
 
static char ** parse_options (char **argv)
 
static void run_actions (char **argv)
 
static void usage (void)
 
static void print_stats (void)
 
int main (void)
 
static void run_task (char **argv)
 
void power_off (void)
 

Variables

uint64_tbase_pml4
 
bool power_off_when_done
 
bool thread_tests
 

Function Documentation

◆ bss_init()

static void bss_init ( void  )
static
132 {
133 /* The "BSS" is a segment that should be initialized to zeros.
134 It isn't actually stored on disk or zeroed by the kernel
135 loader, so we have to zero it ourselves.
136
137 The start and end of the BSS segment is recorded by the
138 linker as _start_bss and _end_bss. See kernel.lds. */
139 extern char _start_bss, _end_bss;
140 memset (&_start_bss, 0, &_end_bss - &_start_bss);
141}
void * memset(void *, int, size_t)
Definition: string.c:258
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void  )
69 {
70 uint64_t mem_end;
71 char **argv;
72
73 /* Clear BSS and get machine's RAM size. */
74 bss_init ();
75
76 /* Break command line into arguments and parse options. */
77 argv = read_command_line ();
78 argv = parse_options (argv);
79
80 /* Initialize ourselves as a thread so we can use locks,
81 then enable console locking. */
82 thread_init ();
83 console_init ();
84
85 /* Initialize memory system. */
86 mem_end = palloc_init ();
87 malloc_init ();
88 paging_init (mem_end);
89
90#ifdef USERPROG
91 tss_init ();
92 gdt_init ();
93#endif
94
95 /* Initialize interrupt handlers. */
96 intr_init ();
97 timer_init ();
98 kbd_init ();
99 input_init ();
100#ifdef USERPROG
102 syscall_init ();
103#endif
104 /* Start thread scheduler and enable interrupts. */
105 thread_start ();
108
109#ifdef FILESYS
110 /* Initialize file system. */
111 disk_init ();
112 filesys_init (format_filesys);
113#endif
114
115#ifdef VM
116 vm_init ();
117#endif
118
119 printf ("Boot complete.\n");
120
121 /* Run actions specified on kernel command line. */
122 run_actions (argv);
123
124 /* Finish up. */
126 power_off ();
127 thread_exit ();
128}
void console_init(void)
Definition: console.c:64
void disk_init(void)
Definition: disk.c:101
void exception_init(void)
Definition: exception.c:31
void filesys_init(bool format)
Definition: filesys.c:19
void gdt_init(void)
Definition: gdt.c:83
static void paging_init(uint64_t mem_end)
Definition: init.c:147
static void run_actions(char **argv)
Definition: init.c:258
void power_off(void)
Definition: init.c:340
static char ** parse_options(char **argv)
Definition: init.c:206
static void bss_init(void)
Definition: init.c:132
bool power_off_when_done
Definition: init.c:50
static char ** read_command_line(void)
Definition: init.c:173
void input_init(void)
Definition: input.c:11
void intr_init(void)
Definition: interrupt.c:164
void kbd_init(void)
Definition: kbd.c:30
void malloc_init(void)
Definition: malloc.c:69
uint64_t palloc_init(void)
Definition: palloc.c:239
void serial_init_queue(void)
Definition: serial.c:81
unsigned long long int uint64_t
Definition: stdint.h:29
int printf(const char *,...) PRINTF_FORMAT(1
void thread_start(void)
Definition: thread.c:140
void thread_init(void)
Definition: thread.c:110
void thread_exit(void) NO_RETURN
Definition: thread.c:325
void timer_init(void)
Definition: timer.c:37
void timer_calibrate(void)
Definition: timer.c:51
void tss_init(void)
Definition: tss.c:53
void syscall_init(void)
Definition: syscall.c:74
void vm_init(void)
Definition: vm.c:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ paging_init()

static void paging_init ( uint64_t  mem_end)
static
147 {
148 uint64_t *pml4, *pte;
149 int perm;
151
152 extern char start, _end_kernel_text;
153 // Maps physical address [0 ~ mem_end] to
154 // [LOADER_KERN_BASE ~ LOADER_KERN_BASE + mem_end].
155 for (uint64_t pa = 0; pa < mem_end; pa += PGSIZE) {
156 uint64_t va = (uint64_t) ptov(pa);
157
158 perm = PTE_P | PTE_W;
159 if ((uint64_t) &start <= va && va < (uint64_t) &_end_kernel_text)
160 perm &= ~PTE_W;
161
162 if ((pte = pml4e_walk (pml4, va, 1)) != NULL)
163 *pte = pa | perm;
164 }
165
166 // reload cr3
167 pml4_activate(0);
168}
uint64_t * base_pml4
Definition: init.c:42
void pml4_activate(uint64_t *pml4)
Definition: mmu.c:205
uint64_t * pml4e_walk(uint64_t *pml4, const uint64_t va, int create)
Definition: mmu.c:65
@ PAL_ZERO
Definition: palloc.h:10
@ PAL_ASSERT
Definition: palloc.h:9
void * palloc_get_page(enum palloc_flags)
Definition: palloc.c:301
#define PTE_W
Definition: pte.h:40
#define PTE_P
Definition: pte.h:39
#define NULL
Definition: stddef.h:4
#define PGSIZE
Definition: vaddr.h:20
#define ptov(paddr)
Definition: vaddr.h:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_options()

static char ** parse_options ( char **  argv)
static
206 {
207 for (; *argv != NULL && **argv == '-'; argv++) {
208 char *save_ptr;
209 char *name = strtok_r (*argv, "=", &save_ptr);
210 char *value = strtok_r (NULL, "", &save_ptr);
211
212 if (!strcmp (name, "-h"))
213 usage ();
214 else if (!strcmp (name, "-q"))
215 power_off_when_done = true;
216#ifdef FILESYS
217 else if (!strcmp (name, "-f"))
218 format_filesys = true;
219#endif
220 else if (!strcmp (name, "-rs"))
221 random_init (atoi (value));
222 else if (!strcmp (name, "-mlfqs"))
223 thread_mlfqs = true;
224#ifdef USERPROG
225 else if (!strcmp (name, "-ul"))
226 user_page_limit = atoi (value);
227 else if (!strcmp (name, "-threads-tests"))
228 thread_tests = true;
229#endif
230 else
231 PANIC ("unknown option `%s' (use -h for help)", name);
232 }
233
234 return argv;
235}
#define PANIC(...)
Definition: debug.h:14
bool thread_tests
Definition: init.c:52
static void usage(void)
Definition: init.c:305
size_t user_page_limit
Definition: palloc.c:40
void random_init(unsigned seed)
Definition: random.c:33
int atoi(const char *)
Definition: stdlib.c:10
int strcmp(const char *, const char *)
Definition: string.c:67
char * strtok_r(char *, const char *, char **)
Definition: string.c:219
bool thread_mlfqs
Definition: thread.c:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ power_off()

void power_off ( void  )
340 {
341#ifdef FILESYS
342 filesys_done ();
343#endif
344
345 print_stats ();
346
347 printf ("Powering off...\n");
348 outw (0x604, 0x2000); /* Poweroff command for qemu */
349 for (;;);
350}
void filesys_done(void)
Definition: filesys.c:47
static void print_stats(void)
Definition: init.c:354
static void outw(uint16_t port, uint16_t data)
Definition: io.h:127
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_stats()

static void print_stats ( void  )
static
354 {
357#ifdef FILESYS
359#endif
362#ifdef USERPROG
364#endif
365}
void console_print_stats(void)
Definition: console.c:79
void disk_print_stats(void)
Definition: disk.c:161
void exception_print_stats(void)
Definition: exception.c:65
void kbd_print_stats(void)
Definition: kbd.c:36
void thread_print_stats(void)
Definition: thread.c:176
void timer_print_stats(void)
Definition: timer.c:128
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_command_line()

static char ** read_command_line ( void  )
static
173 {
174 static char *argv[LOADER_ARGS_LEN / 2 + 1];
175 char *p, *end;
176 int argc;
177 int i;
178
179 argc = *(uint32_t *) ptov (LOADER_ARG_CNT);
180 p = ptov (LOADER_ARGS);
181 end = p + LOADER_ARGS_LEN;
182 for (i = 0; i < argc; i++) {
183 if (p >= end)
184 PANIC ("command line arguments overflow");
185
186 argv[i] = p;
187 p += strnlen (p, end - p) + 1;
188 }
189 argv[argc] = NULL;
190
191 /* Print kernel command line. */
192 printf ("Kernel command line:");
193 for (i = 0; i < argc; i++)
194 if (strchr (argv[i], ' ') == NULL)
195 printf (" %s", argv[i]);
196 else
197 printf (" '%s'", argv[i]);
198 printf ("\n");
199
200 return argv;
201}
#define LOADER_ARGS
Definition: loader.h:25
#define LOADER_ARGS_LEN
Definition: loader.h:30
#define LOADER_ARG_CNT
Definition: loader.h:26
unsigned int uint32_t
Definition: stdint.h:26
char * strchr(const char *, int)
Definition: string.c:104
size_t strnlen(const char *, size_t)
Definition: string.c:284
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_actions()

static void run_actions ( char **  argv)
static
258 {
259 /* An action. */
260 struct action {
261 char *name; /* Action name. */
262 int argc; /* # of args, including action name. */
263 void (*function) (char **argv); /* Function to execute action. */
264 };
265
266 /* Table of supported actions. */
267 static const struct action actions[] = {
268 {"run", 2, run_task},
269#ifdef FILESYS
270 {"ls", 1, fsutil_ls},
271 {"cat", 2, fsutil_cat},
272 {"rm", 2, fsutil_rm},
273 {"put", 2, fsutil_put},
274 {"get", 2, fsutil_get},
275#endif
276 {NULL, 0, NULL},
277 };
278
279 while (*argv != NULL) {
280 const struct action *a;
281 int i;
282
283 /* Find action name. */
284 for (a = actions; ; a++)
285 if (a->name == NULL)
286 PANIC ("unknown action `%s' (use -h for help)", *argv);
287 else if (!strcmp (*argv, a->name))
288 break;
289
290 /* Check for required arguments. */
291 for (i = 1; i < a->argc; i++)
292 if (argv[i] == NULL)
293 PANIC ("action `%s' requires %d argument(s)", *argv, a->argc - 1);
294
295 /* Invoke action and advance. */
296 a->function (argv);
297 argv += a->argc;
298 }
299
300}
void fsutil_cat(char **argv)
Definition: fsutil.c:32
void fsutil_ls(char **argv UNUSED)
Definition: fsutil.c:16
void fsutil_rm(char **argv)
Definition: fsutil.c:57
void fsutil_get(char **argv)
Definition: fsutil.c:141
void fsutil_put(char **argv)
Definition: fsutil.c:78
static void run_task(char **argv)
Definition: init.c:239
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_task()

static void run_task ( char **  argv)
static
239 {
240 const char *task = argv[1];
241
242 printf ("Executing '%s':\n", task);
243#ifdef USERPROG
244 if (thread_tests){
245 run_test (task);
246 } else {
248 }
249#else
250 run_test (task);
251#endif
252 printf ("Execution of '%s' complete.\n", task);
253}
int process_wait(tid_t)
tid_t process_create_initd(const char *file_name)
Definition: process.c:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usage()

static void usage ( void  )
static
305 {
306 printf ("\nCommand line syntax: [OPTION...] [ACTION...]\n"
307 "Options must precede actions.\n"
308 "Actions are executed in the order specified.\n"
309 "\nAvailable actions:\n"
310#ifdef USERPROG
311 " run 'PROG [ARG...]' Run PROG and wait for it to complete.\n"
312#else
313 " run TEST Run TEST.\n"
314#endif
315#ifdef FILESYS
316 " ls List files in the root directory.\n"
317 " cat FILE Print FILE to the console.\n"
318 " rm FILE Delete FILE.\n"
319 "Use these actions indirectly via `pintos' -g and -p options:\n"
320 " put FILE Put FILE into file system from scratch disk.\n"
321 " get FILE Get FILE from file system into scratch disk.\n"
322#endif
323 "\nOptions:\n"
324 " -h Print this help message and power off.\n"
325 " -q Power off VM after actions or on panic.\n"
326 " -f Format file system disk during startup.\n"
327 " -rs=SEED Set random number seed to SEED.\n"
328 " -mlfqs Use multi-level feedback queue scheduler.\n"
329#ifdef USERPROG
330 " -ul=COUNT Limit user memory to COUNT pages.\n"
331#endif
332 );
333 power_off ();
334}
#define USERPROG
Definition: thread.h:12
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ base_pml4

uint64_t* base_pml4

◆ power_off_when_done

bool power_off_when_done

◆ thread_tests

bool thread_tests