Mercurial > lcfOS
annotate cos/kernel/kernel.c @ 32:3a6a9b929db0
Added initial ramdisk and some virtual file system functions
author | windel |
---|---|
date | Fri, 13 Jan 2012 18:18:17 +0100 |
parents | 88590c42320f |
children | d8185ddb6c7b |
rev | line source |
---|---|
31 | 1 |
9 | 2 #include "kernel.h" |
3 | |
29 | 4 static void testMalloc() |
20 | 5 { |
6 char *a, *b; | |
23 | 7 |
8 printf("Testing malloc\n"); | |
20 | 9 a = kmalloc(100); |
10 printf("Got a at %x\n", a); | |
11 a[0] = 'A'; | |
12 b = kmalloc(22); | |
13 printf("Got b at %x\n", b); | |
14 b[0] = 'B'; | |
15 kfree(a); | |
16 } | |
17 | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
18 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
|
19 |
29 | 20 /* This routine initializes the kernel. |
21 * We are left here in 64-bit long mode with the first 6 MB identity mapped. | |
22 * */ | |
9 | 23 void kmain() |
24 { | |
29 | 25 init_screen(); |
26 setupIDT(); | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
27 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
28 uint64_t available_memory = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
29 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
30 /* Display memory information from multiboot header */ |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
31 if ((multiboot_info->flags & (1<<6)) == (1<<6)) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
32 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
33 multiboot_memory_map_t *mmap; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
34 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
|
35 (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
|
36 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
|
37 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
38 printf("size: %d, start: 0x%x, length=0x%x, type=%d\n", mmap->size, mmap->base, mmap->length, mmap->type); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
39 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
40 if ( (mmap->type == 1) && (mmap->base == 0x100000) ) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
41 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
42 available_memory = mmap->length; |
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 } |
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 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
47 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
|
48 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
49 fs_node_t *fs_root = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
50 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
51 if ( (multiboot_info->flags & (1<<3)) == (1<<3)) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
52 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
53 printf("Mod count: %d\n", multiboot_info->mods_count); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
54 uint64_t i; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
55 multiboot_module_t *mod; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
56 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
|
57 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
58 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
|
59 if (i == 0) |
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 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
|
62 fs_root = initialize_initrd(ramdisk_location); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
63 } |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
64 } |
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 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
67 if (fs_root != 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 fs_dirent_t *node = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
70 int i = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
71 while ( (node = readdir_fs(fs_root, i)) != 0) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
72 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
73 |
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 // TODO: Make sure that placement malloc does not overwrite the ramdisk. |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
78 printf("Mod size: "); |
29 | 79 |
80 // Assume first 16MB: | |
81 // TODO: get size from grub | |
82 init_memory(0x1000000); | |
14 | 83 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
84 // TODO: make below a user space program! |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
85 printf("Welcome!\n"); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
86 |
9 | 87 |
88 while (1==1) | |
89 { | |
90 char buffer[70]; | |
27 | 91 printf(">"); |
9 | 92 getline(buffer, 70); |
93 // TODO: interpret this line with python :) | |
94 printf("\n"); | |
95 if (buffer[0] == 'x') | |
96 { | |
97 printf("System time in ms: %d\n", getTimeMS()); | |
98 } | |
23 | 99 if (buffer[0] == 't') |
100 { | |
101 testMalloc(); | |
102 } | |
9 | 103 if ( strncmp(buffer, "help", 4)) |
104 { | |
105 printf("Help\n Try one of these commands:\n"); | |
106 printf(" x: print system time in ms\n"); | |
28 | 107 printf(" r: reboot\n"); |
108 printf(" t: test\n"); | |
29 | 109 printf(" b: break\n"); |
9 | 110 } |
24 | 111 if (strncmp(buffer, "r", 1)) |
112 { | |
113 reboot(); | |
114 } | |
29 | 115 if (strncmp(buffer, "b", 1)) |
116 { | |
117 magicBochsBreak(); | |
118 } | |
9 | 119 } |
120 } | |
121 | |
122 |