Mercurial > lcfOS
annotate cos/kernel/kernel.c @ 33:d8185ddb6c7b
Added more interrupt handlers
author | windel |
---|---|
date | Sun, 15 Jan 2012 13:39:49 +0100 |
parents | 3a6a9b929db0 |
children | 8012221dd740 |
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 { | |
29 | 24 init_screen(); |
25 setupIDT(); | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
26 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
27 uint64_t available_memory = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
28 |
33 | 29 printf("Running with %d MB ram\n", available_memory / 1000000); |
30 | |
31 printf("Detecting amount of memory\n"); | |
32 | |
33 printf("Grub multiboot header location: %x\n", multiboot_info); | |
34 // On real hardware, the retrieval of the flags fails into general protection fault?? | |
35 | |
36 printf("Grub lower mem: %d\n", multiboot_info->mem_lower); | |
37 printf("Grub multiboot header flags: %x\n", multiboot_info->flags); | |
38 | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
39 /* Display memory information from multiboot header */ |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
40 if ((multiboot_info->flags & (1<<6)) == (1<<6)) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
41 { |
33 | 42 printf("Found GRUB memory map\n"); |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
43 multiboot_memory_map_t *mmap; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
44 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
|
45 (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
|
46 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
|
47 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
48 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
|
49 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
50 if ( (mmap->type == 1) && (mmap->base == 0x100000) ) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
51 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
52 available_memory = mmap->length; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
53 } |
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 } |
33 | 56 else |
57 { | |
58 printf("Found no GRUB map\n"); | |
59 } | |
32
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("Running with %d MB ram\n", available_memory / 1000000); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
62 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
63 fs_node_t *fs_root = 0; |
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 if ( (multiboot_info->flags & (1<<3)) == (1<<3)) |
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 printf("Mod count: %d\n", multiboot_info->mods_count); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
68 uint64_t i; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
69 multiboot_module_t *mod; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
70 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
|
71 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
72 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
|
73 if (i == 0) |
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 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
|
76 fs_root = initialize_initrd(ramdisk_location); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
77 } |
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 } |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
80 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
81 if (fs_root != 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 fs_dirent_t *node = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
84 int i = 0; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
85 while ( (node = readdir_fs(fs_root, i)) != 0) |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
86 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
87 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
88 } |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
89 } |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
90 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
91 // 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
|
92 printf("Mod size: "); |
29 | 93 |
94 // Assume first 16MB: | |
95 // TODO: get size from grub | |
96 init_memory(0x1000000); | |
14 | 97 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
98 // TODO: make below a user space program! |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
99 printf("Welcome!\n"); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
31
diff
changeset
|
100 |
9 | 101 |
102 while (1==1) | |
103 { | |
104 char buffer[70]; | |
27 | 105 printf(">"); |
9 | 106 getline(buffer, 70); |
107 // TODO: interpret this line with python :) | |
108 printf("\n"); | |
109 if (buffer[0] == 'x') | |
110 { | |
111 printf("System time in ms: %d\n", getTimeMS()); | |
112 } | |
23 | 113 if (buffer[0] == 't') |
114 { | |
115 testMalloc(); | |
116 } | |
9 | 117 if ( strncmp(buffer, "help", 4)) |
118 { | |
119 printf("Help\n Try one of these commands:\n"); | |
120 printf(" x: print system time in ms\n"); | |
28 | 121 printf(" r: reboot\n"); |
122 printf(" t: test\n"); | |
29 | 123 printf(" b: break\n"); |
9 | 124 } |
24 | 125 if (strncmp(buffer, "r", 1)) |
126 { | |
127 reboot(); | |
128 } | |
29 | 129 if (strncmp(buffer, "b", 1)) |
130 { | |
131 magicBochsBreak(); | |
132 } | |
33 | 133 if (strncmp(buffer, "pf", 2)) |
134 { | |
135 /* Test general protection exception */ | |
136 uint64_t *x; | |
137 x = (uint64_t*)0x4000000; // Address that is not mapped | |
138 *x = 0x2; // trigger paging exception | |
139 } | |
140 | |
141 } | |
9 | 142 } |
143 | |
144 |