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