annotate cos/kernel/kernel.c @ 23:5dd47d6eebac

Added ubersimple malloc algorithm
author windel
date Thu, 01 Dec 2011 21:42:59 +0100
parents 69bc6d477b38
children d8627924d40d
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
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
3 static int shiftstate = 0;
17
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
4 static volatile uint8_t charAvail = 0;
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
5 static volatile char kbdchar = ' ';
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
6
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
7 static char keymap[128] = {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
8 '?','?','1','2', '3', '4', '5','6', '7', '8','9', '0', '-','=', 0xe, '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
9 'q','w','e','r', 't', 'y', 'u','i', 'o', 'p','[', ']', '\n','?', 'a', 's',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
10 'd','f','g','h', 'j', 'k', 'l',';', '\'', '?','?', '?', 'z','x', 'c', 'v',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
11 'b','n','m',',', '.', '/', '?','?', '?', ' ','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
12
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
13 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
14 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
15 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
16 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?'
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
17 };
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
18
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
19 static char keymapUPPER[128] = {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
20 '?','?','!','@', '#', '$', '%','^', '&', '*','(', ')', '_','+', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
21 'Q','W','E','R', 'T', 'Y', 'U','I', 'O', 'P','{', '}', '|','?', 'A', 'S',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
22 'D','F','G','H', 'J', 'K', 'L',':', '"', '?','?', '?', 'Z','X', 'C', 'V',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
23 'B','N','M','<', '>', '?', '?','?', '?', ' ','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
24
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
25 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
26 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
27 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
28 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?'
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
29 };
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
30
17
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
31 // IO port helpers:
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
32 void outb(uint16_t port, uint8_t value)
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
33 {
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
34 asm volatile ("outb %1, %0" : : "dN" (port), "a" (value));
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
35 }
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
36
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
37 uint8_t inb(uint16_t port)
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
38 {
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
39 uint8_t ret;
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
40 asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
41 return ret;
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
42 }
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
43
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
44 uint16_t inw(uint16_t port)
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
45 {
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
46 uint16_t ret;
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
47 asm volatile ("inw %1, %0" : "=a" (ret) : "dN" (port));
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
48 return ret;
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
49 }
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
50
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
51
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
52 static uint64_t ticks = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
53 void timerDriverUpdate()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
54 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
55 ticks++;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
56 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
57
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
58 uint64_t getTimeMS()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
59 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
60 return 55*ticks;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
61 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
62
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
63 int strncmp(const char* s1, const char* s2, int size) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
64 int i;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
65 for (i=0; i<size; i++) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
66 if (s1[i] != s2[i]) return 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
67 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
68 return 1;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
69 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
70
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
71 void keyboardDriverUpdate(unsigned char scancode)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
72 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
73 switch(scancode) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
74 case 0x2a:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
75 shiftstate = 1;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
76 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
77 case 0xaa:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
78 shiftstate = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
79 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
80 default:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
81 if (scancode < 128) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
82 if (charAvail == 0) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
83 if (shiftstate == 0) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
84 kbdchar = keymap[scancode];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
85 } else {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
86 kbdchar = keymapUPPER[scancode];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
87 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
88
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
89 charAvail = 1;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
90 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
91 } else {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
92 // Key release
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
93 //printf("Unhandled scancode: 0x%x\n", scancode);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
94 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
95 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
96 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
97 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
98
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
99 char getChar() {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
100 while (charAvail == 0);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
101 char c = kbdchar;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
102 charAvail = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
103 return c;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
104 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
105
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
106 void getline(char *buffer, int len) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
107 char c;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
108 int i = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
109 while (i < len-1) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
110 c = getChar();
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
111 if (c == '\n') {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
112 // Enter
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
113 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
114 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
115 if (c == 0x0e) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
116 if (i>0) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
117 printf(" ");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
118 i--;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
119 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
120 continue;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
121 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
122 buffer[i] = c;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
123 printf("%c", c);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
124 i++;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
125 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
126 buffer[i] = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
127 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
128
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
129 #define HEAP_MAGIC 0xc0ffee
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
130 #define HEAP_START 0x400000
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
131 #define HEAP_SIZE 0x200000
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
132 #define HEAP_INUSE 1
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
133 #define HEAP_FREE 0
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
134
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
135 typedef struct {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
136 uint64_t magic;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
137 uint64_t state;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
138 uint64_t size;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
139 } heap_t;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
140
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
141 /*
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
142 malloc and free divide the chunks of memory present at the heap
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
143 of the kernel into smaller parts.
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
144 The heap is located at: 0x
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
145 */
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
146 static heap_t* kernel_heap = (heap_t*) 0x400000; // 4 MB - 6 MB is heap
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
147 /* Allocates 'size' bytes and returns the pointer if succesfull.
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
148 Kernelpanic in case of failure..
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
149 */
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
150
20
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
151 void* kmalloc(uint64_t size) {
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
152 // printf("Malloc %d bytes\n", size);
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
153
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
154 // Start at the beginning of our heap and search a free block:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
155 heap_t *current = kernel_heap;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
156 while (current->magic == HEAP_MAGIC)
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
157 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
158 if ((current->state == HEAP_FREE) && (current->size >= size))
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
159 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
160 // Mark block as used:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
161 current->state = HEAP_INUSE;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
162
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
163 // Insert a heap header if required:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
164 if (current->size > size + sizeof(heap_t) + 1)
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
165 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
166 // Calculate location of the inserted header:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
167 heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t));
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
168
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
169 // Set the new header fields:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
170 newheader->size = current->size - size - sizeof(heap_t);
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
171 newheader->state = HEAP_FREE;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
172 newheader->magic = HEAP_MAGIC;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
173
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
174 // Set the size of this block
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
175 current->size = size;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
176 }
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
177 else
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
178 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
179 // We allocate this whole block
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
180 }
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
181 // Calculate the size of the block:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
182 char *address = ((char*)current)+sizeof(heap_t);
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
183 return address;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
184
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
185 }
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
186 // Goto next heap block:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
187 current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t));
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
188 }
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
189 return 0x0;
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
190 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
191
20
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
192 void kfree(void* ptr) {
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
193 printf("Free address %x\n", ptr);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
194 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
195
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
196 void init_heap(void)
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
197 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
198 // Initialize the kernel heap:
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
199 kernel_heap->magic = HEAP_MAGIC;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
200 kernel_heap->state = HEAP_FREE;
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
201 kernel_heap->size = HEAP_SIZE - sizeof(heap_t);
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
202 }
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
203
19
f454e3c592dd Cleanup of bochs config file
windel
parents: 18
diff changeset
204 void startPython()
f454e3c592dd Cleanup of bochs config file
windel
parents: 18
diff changeset
205 {
f454e3c592dd Cleanup of bochs config file
windel
parents: 18
diff changeset
206 // TODO: connect to Py_Main
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
207 //PyRun_SimpleString("print('hello world')");
19
f454e3c592dd Cleanup of bochs config file
windel
parents: 18
diff changeset
208
f454e3c592dd Cleanup of bochs config file
windel
parents: 18
diff changeset
209 }
f454e3c592dd Cleanup of bochs config file
windel
parents: 18
diff changeset
210
20
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
211 void testMalloc()
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
212 {
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
213 char *a, *b;
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
214
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
215 printf("Testing malloc\n");
20
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
216 a = kmalloc(100);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
217 printf("Got a at %x\n", a);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
218 a[0] = 'A';
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
219 b = kmalloc(22);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
220 printf("Got b at %x\n", b);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
221 b[0] = 'B';
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
222 kfree(a);
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
223 }
b1fed2171e1a Now working with 2 MB pages
windel
parents: 19
diff changeset
224
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
225 void kmain()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
226 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
227 init_screen();
22
69bc6d477b38 Added python run function
windel
parents: 20
diff changeset
228 setupIDT();
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
229 init_heap();
14
a58904747019 Added asm interrupt handler things, not yet working
windel
parents: 9
diff changeset
230
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
231 printf("Welcome!\n");
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
232
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
233 while (1==1)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
234 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
235 char buffer[70];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
236 printf(">>>");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
237 getline(buffer, 70);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
238 // TODO: interpret this line with python :)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
239 printf("\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
240 printf("Got line: '%s'\n", buffer);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
241 if (buffer[0] == 'x')
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
242 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
243 printf("System time in ms: %d\n", getTimeMS());
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
244 }
23
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
245 if (buffer[0] == 't')
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
246 {
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
247 testMalloc();
5dd47d6eebac Added ubersimple malloc algorithm
windel
parents: 22
diff changeset
248 }
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
249 if ( strncmp(buffer, "help", 4))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
250 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
251 printf("Help\n Try one of these commands:\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
252 printf(" x: print system time in ms\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
253 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
254 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
255 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
256
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
257