comparison cos/kernel/kernel.c @ 37:5c20bd53cccd

Cleanup
author windel
date Mon, 16 Jan 2012 21:38:55 +0100
parents 91f91ff07ea8
children 35cc54e078dd
comparison
equal deleted inserted replaced
36:91f91ff07ea8 37:5c20bd53cccd
1 #include "kernel.h" 1 #include "kernel.h"
2
3 static void testMalloc()
4 {
5 char *a, *b;
6
7 printf("Testing malloc\n");
8 a = kmalloc(100);
9 printf("Got a at %x\n", a);
10 a[0] = 'A';
11 b = kmalloc(22);
12 printf("Got b at %x\n", b);
13 b[0] = 'B';
14 kfree(a);
15 }
16
17 multiboot_info_t *multiboot_info = 0; // Set by startup code.
18 2
19 /* This routine initializes the kernel. 3 /* This routine initializes the kernel.
20 * We are left here in 64-bit long mode with the first 6 MB identity mapped. 4 * We are left here in 64-bit long mode with the first 6 MB identity mapped.
21 * */ 5 * */
22 void kmain() 6 void kmain()
23 { 7 {
24 // No kmalloc required here yet: 8 // No kmalloc required here yet:
25 init_screen(); 9 init_screen();
26 setupIDT(); 10 setupIDT();
27 11 read_multiboot_info(); // Parse the GRUB multiboot header.
28 /* Retrieve memory information from multiboot header */ 12 init_memory(available_memory); // Setup a new paging scheme and memory manager
29 uint64_t available_memory = 0; 13 // From here kmalloc can be used.
30 if ((multiboot_info->flags & (1 << 6)) == (1 << 6))
31 {
32 multiboot_memory_map_t *mmap;
33 for (mmap = (multiboot_memory_map_t*)(uint64_t)multiboot_info->mmap_addr;
34 (uint64_t)mmap < multiboot_info->mmap_addr + multiboot_info->mmap_length;
35 mmap = (multiboot_memory_map_t*) ( (uint64_t)mmap + mmap->size + sizeof(mmap->size)) )
36 {
37 /*
38 printf("size: %d, start: 0x%x, length=0x%x, type=%d\n", mmap->size,
39 mmap->base, mmap->length, mmap->type);
40 */
41
42 if ( (mmap->type == 1) && (mmap->base == 0x100000) )
43 {
44 available_memory = mmap->length;
45 }
46 }
47 }
48
49 if (available_memory == 0)
50 {
51 panic("Found no usable memory in grub's memory map\n");
52 }
53
54 printf("Running with %d MB ram\n", available_memory / 1000000);
55
56 init_memory(available_memory); // Setup paging and memory manager
57
58 keyboard_init(); 14 keyboard_init();
59 timer_init(); 15 timer_init();
16 printf("Ramdisk location: %p\n", ramdisk_location);
60 17
61 /* 18 /*
62 fs_node_t *fs_root = 0; 19 fs_node_t *fs_root = 0;
63 if ( (multiboot_info->flags & (1<<3)) == (1<<3))
64 {
65 printf("Mod count: %d\n", multiboot_info->mods_count);
66 uint64_t i;
67 multiboot_module_t *mod;
68 for (i=0, mod=(multiboot_module_t*)(uint64_t)multiboot_info->mods_addr; i<multiboot_info->mods_count; i++, mod++)
69 {
70 printf("Mod start: %x, end: %x\n", mod->mod_start, mod->mod_end);
71 if (i == 0)
72 {
73 uint64_t ramdisk_location = ((uint32_t*)((uint64_t)multiboot_info->mods_addr))[0];
74 fs_root = initialize_initrd(ramdisk_location);
75 }
76 // TODO: Make sure that placement malloc does not overwrite the ramdisk.
77 printf("Mod size: %d", mod->mod_end - mod->mod_start);
78 }
79 }
80 20
21 fs_root = initialize_initrd(ramdisk_location);
22
81 if (fs_root != 0) 23 if (fs_root != 0)
82 { 24 {
83 fs_dirent_t *node = 0; 25 fs_dirent_t *node = 0;
84 int i = 0; 26 int i = 0;
85 while ( (node = readdir_fs(fs_root, i)) != 0) 27 while ( (node = readdir_fs(fs_root, i)) != 0)
87 29
88 } 30 }
89 } 31 }
90 */ 32 */
91 33
92 34 // TODO: make shell a user space program!
93 // TODO: make below a user space program! 35 shell(); // Start user shell
94 printf("Welcome!\n");
95
96 while (1)
97 {
98 char buffer[70];
99 printf(">");
100 getline(buffer, 70);
101 // TODO: interpret this line with python :)
102 printf("\n");
103 if (buffer[0] == 'x')
104 {
105 printf("System time in ms: %d\n", getTimeMS());
106 }
107 if (buffer[0] == 't')
108 {
109 testMalloc();
110 }
111 if ( strncmp(buffer, "help", 4))
112 {
113 printf("Help\n Try one of these commands:\n");
114 printf(" x: print system time in ms\n");
115 printf(" r: reboot\n");
116 printf(" t: test\n");
117 printf(" b: break\n");
118 }
119 if (strncmp(buffer, "r", 1))
120 {
121 reboot();
122 }
123 if (strncmp(buffer, "b", 1))
124 {
125 magicBochsBreak();
126 }
127 if (strncmp(buffer, "pf", 2))
128 {
129 /* Test general protection exception */
130 uint64_t *x;
131 x = (uint64_t*)0x4000000; // Address that is not mapped
132 *x = 0x2; // trigger paging exception
133 }
134 }
135 } 36 }
136 37
137