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