-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
141 lines (119 loc) · 2.63 KB
/
main.c
File metadata and controls
141 lines (119 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// main.c -- Defines the C-code kernel entry point, calls initialisation routines.
// Made for JamesM's tutorials <www.jamesmolloy.co.uk>
#include "multiboot.h"
#include "common.h"
#include "monitor.h"
#include "gdt.h"
#include "idt.h"
#include "timer.h"
#include "elf.h"
#include "pmm.h"
#include "vmm.h"
#include "heap.h"
#include "thread.h"
#include "lock.h"
spinlock_t lock = SPINLOCK_UNLOCKED;
int fn(void *arg)
{
for(;;) {
int i;
spinlock_lock(&lock);
for(i = 0; i < 80; i++)
printk("a");
spinlock_unlock(&lock);
}
return 6;
}
int kernel_main(multiboot_t *mboot_ptr)
{
#if CHAPTER >= 3
monitor_clear();
#endif
#if CHAPTER == 3 || CHAPTER == 4
monitor_write("Hello, world!\n");
#endif
#if CHAPTER >= 4
init_gdt ();
init_idt ();
#endif
#if CHAPTER == 4
asm volatile("int $0x3");
#endif
#if CHAPTER >= 5
init_timer (20);
#endif
#if CHAPTER >= 7
init_pmm (mboot_ptr->mem_upper);
init_vmm ();
#endif
#if CHAPTER >= 8
init_heap ();
#endif
#if CHAPTER >= 7
// Find all the usable areas of memory and inform the physical memory manager about them.
uint32_t i = mboot_ptr->mmap_addr;
while (i < mboot_ptr->mmap_addr + mboot_ptr->mmap_length)
{
mmap_entry_t *me = (mmap_entry_t*) i;
// Does this entry specify usable RAM?
if (me->type == 1)
{
uint32_t j;
// For every page in this entry, add to the free page stack.
for (j = me->base_addr_low; j < me->base_addr_low+me->length_low; j += 0x1000)
{
pmm_free_page (j);
}
}
// The multiboot specification is strange in this respect - the size member does not include "size" itself in its calculations,
// so we must add sizeof (uint32_t).
i += me->size + sizeof (uint32_t);
}
#endif
#if CHAPTER >= 6
kernel_elf = elf_from_multiboot (mboot_ptr);
#endif
#if CHAPTER >= 9
asm volatile ("sti");
init_scheduler (init_threading ());
#endif
#if CHAPTER == 9
uint32_t *stack = kmalloc (0x400) + 0x3F0;
thread_t *t = create_thread(&fn, (void*)0x567, stack);
for(;;) {
printk("b");
}
#endif
#if CHAPTER == 10
uint32_t *stack = kmalloc (0x400) + 0x3F0;
thread_t *t = create_thread(&fn, (void*)0x567, stack);
for(;;) {
int i;
spinlock_lock(&lock);
for(i = 0; i < 80; i++)
printk("b");
spinlock_unlock(&lock);
}
#endif
#if CHAPTER == 5
asm volatile("sti");
#endif
#if CHAPTER == 6
panic ("Testing panic mechanism");
#endif
#if CHAPTER >= 11
init_keyboard_driver();
#if CHAPTER == 11
for(;;)
{
char c = keyboard_getchar();
if (c)
monitor_put(c);
}
#endif
#endif
#if CHAPTER >= 3
for (;;);
#endif
return 0xdeadbeef;
}