annotate cos/kernel/kernel.c @ 35:bcb3b68c8147

Added bss end address and load end address to multiboot header
author windel
date Mon, 16 Jan 2012 17:38:00 +0100
parents 8012221dd740
children 91f91ff07ea8
rev   line source
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
1 #include "kernel.h"
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
2
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
3 static void testMalloc()
20
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
4 {
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
5 char *a, *b;
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
6
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
7 printf("Testing malloc\n");
20
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
8 a = kmalloc(100);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
9 printf("Got a at %x\n", a);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
10 a[0] = 'A';
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
11 b = kmalloc(22);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
12 printf("Got b at %x\n", b);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
13 b[0] = 'B';
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
14 kfree(a);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
15 }
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
16
35
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
17 uint64_t testvar = 1234; // Ends up in data section
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
18 uint64_t testvar2 = 0; // Ends up in bss section
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
19
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
20 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
21
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
22 /* This routine initializes the kernel.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
23 * We are left here in 64-bit long mode with the first 6 MB identity mapped.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
24 * */
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
25 void kmain()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
26 {
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
27 // No kmalloc required here yet:
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
28 init_screen();
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
29 setupIDT();
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
30 keyboard_init();
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
31 timer_init();
33
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
32
35
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
33 testvar++;
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
34 testvar2++;
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
35 printf("Test variable = %d, testvar2 = %d\n", testvar, testvar2);
bcb3b68c8147 Added bss end address and load end address to multiboot header
windel
parents: 34
diff changeset
36
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
37 /* Retrieve memory information from multiboot header */
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
38 uint64_t available_memory = 0;
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
39 if ((multiboot_info->flags & (1<<6)) == (1<<6))
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
40 {
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
41 multiboot_memory_map_t *mmap;
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
42 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
43 (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
44 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
45 {
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
46 /*
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
47 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
48 mmap->base, mmap->length, mmap->type);
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
49 */
32
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 ( (mmap->type == 1) && (mmap->base == 0x100000) )
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 available_memory = mmap->length;
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 }
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
56 }
33
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
57 else
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
58 {
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
59 panic("Found no GRUB memory map\n");
33
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
60 }
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
61
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
62 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
63
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
64 /*
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
65 fs_node_t *fs_root = 0;
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
66 if ( (multiboot_info->flags & (1<<3)) == (1<<3))
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
67 {
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
68 printf("Mod count: %d\n", multiboot_info->mods_count);
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
69 uint64_t i;
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
70 multiboot_module_t *mod;
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
71 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
72 {
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
73 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
74 if (i == 0)
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 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
77 fs_root = initialize_initrd(ramdisk_location);
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
78 }
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
79 // 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
80 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
81 }
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 if (fs_root != 0)
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
85 {
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
86 fs_dirent_t *node = 0;
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
87 int i = 0;
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
88 while ( (node = readdir_fs(fs_root, i)) != 0)
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 }
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
92 }
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
93 */
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
94
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
95 // Assume first 16MB:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
96 // TODO: get size from grub
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
97 init_memory(0x1000000);
14
a58904747019 Added asm interrupt handler things, not yet working
windel
parents: 9
diff changeset
98
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
99 // TODO: make below a user space program!
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
100 printf("Welcome!\n");
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 31
diff changeset
101
34
8012221dd740 Fixes for uninitialized data. This causes problems on real machines
windel
parents: 33
diff changeset
102 while (1)
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
103 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
104 char buffer[70];
27
7f74363f4c82 Added some files for the python port
windel
parents: 26
diff changeset
105 printf(">");
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
106 getline(buffer, 70);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
107 // TODO: interpret this line with python :)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
108 printf("\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
109 if (buffer[0] == 'x')
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
110 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
111 printf("System time in ms: %d\n", getTimeMS());
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
112 }
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
113 if (buffer[0] == 't')
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
114 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
115 testMalloc();
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
116 }
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
117 if ( strncmp(buffer, "help", 4))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
118 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
119 printf("Help\n Try one of these commands:\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
120 printf(" x: print system time in ms\n");
28
47b7df514243 Moved Makefiles
windel
parents: 27
diff changeset
121 printf(" r: reboot\n");
47b7df514243 Moved Makefiles
windel
parents: 27
diff changeset
122 printf(" t: test\n");
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
123 printf(" b: break\n");
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
124 }
24
d8627924d40d Split up in more files and reboot command
windel
parents: 23
diff changeset
125 if (strncmp(buffer, "r", 1))
d8627924d40d Split up in more files and reboot command
windel
parents: 23
diff changeset
126 {
d8627924d40d Split up in more files and reboot command
windel
parents: 23
diff changeset
127 reboot();
d8627924d40d Split up in more files and reboot command
windel
parents: 23
diff changeset
128 }
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
129 if (strncmp(buffer, "b", 1))
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
130 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
131 magicBochsBreak();
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
132 }
33
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
133 if (strncmp(buffer, "pf", 2))
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
134 {
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
135 /* Test general protection exception */
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
136 uint64_t *x;
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
137 x = (uint64_t*)0x4000000; // Address that is not mapped
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
138 *x = 0x2; // trigger paging exception
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
139 }
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
140
d8185ddb6c7b Added more interrupt handlers
windel
parents: 32
diff changeset
141 }
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
142 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
143
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
144