comparison cos/kernel/kernel.c @ 24:d8627924d40d

Split up in more files and reboot command
author windel
date Fri, 02 Dec 2011 14:00:02 +0100
parents 5dd47d6eebac
children dcce92b1efbc
comparison
equal deleted inserted replaced
23:5dd47d6eebac 24:d8627924d40d
1 #include "kernel.h" 1 #include "kernel.h"
2
3 static int shiftstate = 0;
4 static volatile uint8_t charAvail = 0;
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
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
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
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
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 */
146 static heap_t* kernel_heap = (heap_t*) 0x400000; // 4 MB - 6 MB is heap
147 /* Allocates 'size' bytes and returns the pointer if succesfull.
148 Kernelpanic in case of failure..
149 */
150
151 void* kmalloc(uint64_t size) {
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;
190 }
191
192 void kfree(void* ptr) {
193 printf("Free address %x\n", ptr);
194 }
195
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 2
204 void startPython() 3 void startPython()
205 { 4 {
206 // TODO: connect to Py_Main 5 // TODO: connect to Py_Main
207 //PyRun_SimpleString("print('hello world')"); 6 //PyRun_SimpleString("print('hello world')");
208
209 } 7 }
210 8
211 void testMalloc() 9 void testMalloc()
212 { 10 {
213 char *a, *b; 11 char *a, *b;
220 printf("Got b at %x\n", b); 18 printf("Got b at %x\n", b);
221 b[0] = 'B'; 19 b[0] = 'B';
222 kfree(a); 20 kfree(a);
223 } 21 }
224 22
23 void reboot()
24 {
25 while ( (inb(0x64) & 0x02) == 0x02)
26 {
27 ;
28 }
29 outb(0x64, 0xFE);
30 }
31
225 void kmain() 32 void kmain()
226 { 33 {
227 init_screen(); 34 init_screen();
228 setupIDT(); 35 setupIDT(); // This causes error on real hardware
229 init_heap(); 36 init_heap();
230 37
231 printf("Welcome!\n"); 38 printf("Welcome!\n");
232 39
233 while (1==1) 40 while (1==1)
235 char buffer[70]; 42 char buffer[70];
236 printf(">>>"); 43 printf(">>>");
237 getline(buffer, 70); 44 getline(buffer, 70);
238 // TODO: interpret this line with python :) 45 // TODO: interpret this line with python :)
239 printf("\n"); 46 printf("\n");
240 printf("Got line: '%s'\n", buffer);
241 if (buffer[0] == 'x') 47 if (buffer[0] == 'x')
242 { 48 {
243 printf("System time in ms: %d\n", getTimeMS()); 49 printf("System time in ms: %d\n", getTimeMS());
244 } 50 }
245 if (buffer[0] == 't') 51 if (buffer[0] == 't')
249 if ( strncmp(buffer, "help", 4)) 55 if ( strncmp(buffer, "help", 4))
250 { 56 {
251 printf("Help\n Try one of these commands:\n"); 57 printf("Help\n Try one of these commands:\n");
252 printf(" x: print system time in ms\n"); 58 printf(" x: print system time in ms\n");
253 } 59 }
60 if (strncmp(buffer, "r", 1))
61 {
62 reboot();
63 }
254 } 64 }
255 } 65 }
256 66
257 67