From: Ben Pfaff Date: Tue, 31 Aug 2004 05:34:25 +0000 (+0000) Subject: Clean up a little. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pintos-anon;a=commitdiff_plain;h=bf866bc97d077fa3757b4aceaa7a6b41e7298c73 Clean up a little. --- diff --git a/src/threads/init.c b/src/threads/init.c index 79cd3d0..caacaee 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -22,9 +22,6 @@ #include "disk.h" #endif -/* Size of kernel static code and data, in 4 kB pages. */ -size_t kernel_pages; - /* Amount of physical memory, in 4 kB pages. */ size_t ram_pages; @@ -46,41 +43,31 @@ main_thread (void *aux UNUSED) int main (void) { - struct thread *t; - - /* Initialize components needed by printk() very early. */ + /* Initialize prerequisites for calling printk(). */ ram_init (); vga_init (); serial_init (); - printk ("Booting cnachos86...\n"); - /* Calculate how much RAM the kernel uses, and find out from - the bootloader how much RAM this machine has. */ - printk ("ram: detected %'d kB main memory.\n", ram_pages * 4); + /* Greet user. */ + printk ("Booting cnachos86 with %'d kB RAM...\n", ram_pages * 4); - /* Memory from the end of the kernel through the end of memory - is free. Give it to the page allocator. */ - palloc_init (ptov (LOADER_KERN_BASE + kernel_pages * PGSIZE), - ptov (ram_pages * PGSIZE)); + /* Initialize memory system. */ + palloc_init (); paging_init (); gdt_init (); - malloc_init (); - random_init (); + random_init (); argv_init (); + /* Initialize interrupt handlers. */ intr_init (); timer_init (); kbd_init (); + /* Do everything else in a system thread. */ thread_init (); - - t = thread_create ("main", main_thread, NULL); - thread_start (t); - - printk ("Done!\n"); - return 0; + thread_start (thread_create ("main", main_thread, NULL)); } static uint64_t @@ -162,28 +149,39 @@ gdt_init (void) static void ram_init (void) { - /* Start and end of kernel image, - and start and end of BSS segment. - These are created by kernel.lds. */ - extern char _start, _end; - extern char _start_bss, _end_bss; - /* The "BSS" is a segment that should be initialized to zeros. It isn't actually stored on disk or zeroed by the kernel - loader, so we have to zero it ourselves. */ + loader, so we have to zero it ourselves. + + The start and end of the BSS segment is recorded by the + linker as _start_bss and _end_bss. See kernel.lds. */ + extern char _start_bss, _end_bss; memset (&_start_bss, 0, &_end_bss - &_start_bss); - /* Calculate how much RAM the kernel uses, - and find out from the bootloader how much RAM this machine - has. */ - kernel_pages = (&_end - &_start + 4095) / 4096; - ram_pages = *(uint32_t *) ptov (LOADER_BASE + LOADER_RAM_PAGES); + /* Get RAM size from loader. */ + ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES); } + +/* This should be sufficient because the command line buffer is + only 128 bytes and arguments are space-delimited. */ +#define ARGC_MAX 64 -void +int argc; +char *argv[ARGC_MAX + 1]; + +static void argv_init (void) { - char *cmd_line = ptov (LOADER_BASE + LOADER_CMD_LINE); + char *cmd_line = ptov (LOADER_CMD_LINE); + char *arg, *pos; + + for (arg = strtok_r (cmd_line, " \t\r\n\v", &pos); arg != NULL; + arg = strtok_r (NULL, " \t\r\n\v", &pos)) + { + ASSERT (argc < ARGC_MAX); + argv[argc++] = arg; + } + argv[argc] = NULL; } void diff --git a/src/threads/init.h b/src/threads/init.h index e23269d..8dfb651 100644 --- a/src/threads/init.h +++ b/src/threads/init.h @@ -3,12 +3,14 @@ #include -struct tss *tss; - -extern size_t kernel_pages; -extern size_t ram_pages; +/* Physical memory size, in 4 kB pages. */ +size_t ram_pages; +/* Kernel command line. */ extern int argc; extern char *argv[]; +struct tss *tss; + + #endif /* init.h */ diff --git a/src/threads/palloc.c b/src/threads/palloc.c index 3cf8241..a0196ac 100644 --- a/src/threads/palloc.c +++ b/src/threads/palloc.c @@ -2,6 +2,8 @@ #include #include #include "debug.h" +#include "init.h" +#include "loader.h" #include "lib.h" #include "mmu.h" @@ -15,10 +17,20 @@ static struct page *free_pages; static uint8_t *uninit_start, *uninit_end; void -palloc_init (uint8_t *start, uint8_t *end) +palloc_init (void) { - uninit_start = start; - uninit_end = end; + /* Kernel static code and data, in 4 kB pages. + + We can figure this out because the linker records the start + and end of the kernel as _start and _end. See + kernel.lds. */ + extern char _start, _end; + size_t kernel_pages; + kernel_pages = (&_end - &_start + 4095) / 4096; + + /* Then we know how much is available to allocate. */ + uninit_start = ptov (LOADER_KERN_BASE + kernel_pages * PGSIZE); + uninit_end = ptov (ram_pages * PGSIZE); } void * diff --git a/src/threads/palloc.h b/src/threads/palloc.h index 9bd076f..5b75e55 100644 --- a/src/threads/palloc.h +++ b/src/threads/palloc.h @@ -13,7 +13,7 @@ enum palloc_flags PAL_ZERO = 002 /* Zero page contents. */ }; -void palloc_init (uint8_t *start, uint8_t *end); +void palloc_init (void); void *palloc_get (enum palloc_flags); void palloc_free (void *);