annotate cos/kernel/kernel.c @ 17:f3e3e0e9c4bc

First attempt IDT loader 64 bits. INT13 occurs
author windel
date Sat, 19 Nov 2011 20:01:28 +0100
parents a58904747019
children 6129643f5c34
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
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
129 /*
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
130 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
131 of the kernel into smaller parts.
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
132 The heap is located at: 0x
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
133 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
134 static void* kernel_heap = (void*) 0xD0000000;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
135 /* Allocates 'size' bytes and returns the pointer if succesfull.
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
136 Kernelpanic in case of failure..
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
137 */
17
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
138 void* malloc(uint64_t size) {
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
139 printf("Malloc %d bytes\n", size);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
140 return kernel_heap;
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
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
143 void free(void* ptr) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
144 printf("Free address %x\n", ptr);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
145 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
146
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
147 void kmain()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
148 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
149 init_screen();
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
150 clear_screen();
17
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
151 printf("Welcome! .. ");
14
a58904747019 Added asm interrupt handler things, not yet working
windel
parents: 9
diff changeset
152
17
f3e3e0e9c4bc First attempt IDT loader 64 bits. INT13 occurs
windel
parents: 14
diff changeset
153 printf("Enabling interrupts .. ");
14
a58904747019 Added asm interrupt handler things, not yet working
windel
parents: 9
diff changeset
154 setupIDT();
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
155 printf("Entering mainloop!\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
156
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
157 while (1==1)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
158 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
159 char buffer[70];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
160 printf(">>>");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
161 getline(buffer, 70);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
162 // TODO: interpret this line with python :)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
163 printf("\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
164 printf("Got line: '%s'\n", buffer);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
165 if (buffer[0] == 'x')
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
166 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
167 printf("System time in ms: %d\n", getTimeMS());
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
168 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
169 if ( strncmp(buffer, "help", 4))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
170 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
171 printf("Help\n Try one of these commands:\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
172 printf(" x: print system time in ms\n");
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
173 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
174 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
175 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
176
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
177