-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
executable file
·298 lines (240 loc) · 6.01 KB
/
kernel.c
File metadata and controls
executable file
·298 lines (240 loc) · 6.01 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
void printki(long num);
void printkid(long num);
void printk(char* s);
extern void outb(unsigned short port, unsigned char val);
extern unsigned char inb(unsigned short port);
extern void pci_thing();
extern void some_ps2_stuff();
extern void some_serial_stuff();
extern void setup_apic();
extern void setup_apic_notreally();
extern void setup_pic();
extern void setup_ata_pio();
extern void setup_bga();
extern void lua_stuff();
extern void serial_printk(char* s);
// imagine dragons - radioactive
void* kmstart;
void* kmlimit;
// end symbol - from linker script
extern const void end;
void _start()
{
int i; int j;
printk("I apparently have ");
printkid(*((short*)0x5020));
printk(" MB of rams\n");
printk("sizeof int (should be 4): ");
printkid(sizeof(int));
printk(" sizeof long (should be 8): ");
printkid(sizeof(long));
printk("\nsizeof long long (should be 8, for Lua 5.3): ");
printkid(sizeof(long long));
printk("\n");
printk("E820 System Memory Map:\n");
long starts[16];
long lengths[16];
int goodCount = 0;
long* wat = (long*)0x4000;
for (i = 0; i < 16; i++) {
/*long* startAddr = (long*)((long)0x4000 + 24 * i);
long* length = (long*)((long)0x4000 + 24 * i + 1);
int* memType = (int*)((long)0x4000 + 24 * i + 16);
int* acpi = (int*)((long)0x4000 + 24 * i + 20);
printk(" start ");
printki(*startAddr);
printk(" len ");
printki(*length);
printk(" memType ");
printkid(*memType);
printk(" acpi ");
printkid(*acpi);
printk("\n");*/
long startAddr = *wat;
wat++;
long length = *wat;
wat++;
int type = (int)(*wat >> 32);
int acpi = (int)(*wat);
wat++;
// usable high memory?
if (type == 1 && startAddr >= 0x100000) {
printki(startAddr);
printk(" ");
printki(length);
printk(" ");
printkid(type);
printk(" ");
printkid(acpi);
printk("\n");
starts[goodCount] = startAddr;
lengths[goodCount] = length;
goodCount++;
if (goodCount == 16)
break;
}
/*for (j = 0; j < 2; j++) {
printki(*wat);
printk(" ");
wat++;
}
j = (int)(*wat >> 32);
printkid(j);
printk(" ");
j = (int)(*wat);
printkid(j);
printk("\n");*/
}
int biggest = 0;
if (goodCount == 0) {
printk("I didn't find any suitable high memory. Email leaf@leaf.sx!\n");
while(1) {}
} else {
printk("I found at least one suitable high memory area.\n");
// biggest mem area?
for (j = 0; j < goodCount; j++) {
if (lengths[j] > biggest)
biggest = j;
}
printk("Biggest contiguous memory area is ");
printkid(lengths[biggest] / (1024*1024));
printk(" MiB.\n");
if (lengths[biggest] < (256*1024*1024)) {
printk("THIS IS SMALLER THAN 256 MiB! OS may fail. Let's try, though.\n");
}
}
kmstart = (void*) starts[biggest];
kmlimit = (void*) (starts[biggest] + lengths[biggest]);
if (kmstart < &end || (long)kmstart < 0x500000) {
printk("kmstart is ");
printki((long)kmstart);
printk(" which is less than end of kernel, ");
printki((long)&end);
printk(" -> changing kmstart\n");
//kmstart = &end;
kmstart = (void*)0x500000;
}
pci_thing();
//setup_apic();
setup_apic_notreally();
setup_pic();
some_ps2_stuff();
some_serial_stuff();
setup_ata_pio();
//setup_bga();
lua_stuff();
while (1) { }
}
// Simple memory allocation.
// 'Real' memory allocation is performed after setup is done by taking
// control of a huge chunk of memory, and another malloc func does caretaking.
void* kmalloc(long bytes) {
void* m = kmstart;
kmstart += bytes;
if (kmstart >= kmlimit) {
printk("OUT OF MEMORY!\n");
while(1) {}
}
return m;
}
// backbuffer for vid screen
char vidfriendback[2000];
char* vidfriendfront = (char*)0xb8000;
int vidfriendpos = 0;
void printkid(long num) {
char kibi[64];
int i;
long num2;
for (i = 0; i < 64; i++) {
kibi[i] = 0;
}
kibi[0] = '0';
num2 = num;
i = -1;
for (; num2 > 0; num2 /= 10) {
i++;
}
for (; num > 0; num /= 10) {
kibi[i] = (num % 10) + '0';
i--;
}
printk(kibi);
}
void printki(long num) {
char kibi[64];
int i;
long num2;
for (i = 0; i < 64; i++) {
kibi[i] = 0;
}
kibi[0] = '0';
kibi[1] = 'x';
kibi[2] = '0';
num2 = num;
i = 1;
for (; num2 > 0; num2 /= 16) {
i++;
}
for (; num > 0; num /= 16) {
if (num % 16 > 9) {
kibi[i] = ((num % 16) - 10) + 'a';
} else {
kibi[i] = (num % 16) + '0';
}
i--;
}
printk(kibi);
}
void printk(char* s) {
char* t = s;
char* vf = (char*)(vidfriendfront + 2 * vidfriendpos);
char* vfb = (char*)(vidfriendback + vidfriendpos);
int i;
for (; *s != 0; s++) {
if (*s == '\n') {
// insert spaces
for (; (vidfriendpos % 80) != 0; ) {
*vf = ' ';
*vfb = ' ';
vf++; vfb++;
*vf = 7;
vf++;
vidfriendpos++;
}
continue;
}
if (vidfriendpos >= 25*80) {
for (i = 0; i < 24*80; i++) {
vidfriendback[i] = vidfriendback[i + 80];
}
for (i = 24*80; i < 25*80; i++) {
vidfriendback[i] = ' ';
}
// now blit backbuffer to front
vf = (char*)vidfriendfront;
vfb = (char*)vidfriendback;
for (i = 0; i < 25*80; i++) {
*vf = *vfb;
vf++;
*vf = 7;
vf++;
vfb++;
}
// fix pos
vidfriendpos = 24*80;
vf = (char*)(vidfriendfront + 2 * vidfriendpos);
vfb = (char*)(vidfriendback + vidfriendpos);
}
*vf = *s;
vf++;
*vf = 7;
vf++;
*vfb = *s;
vfb++;
vidfriendpos++;
}
// give to serial, but don't do it for message 'COM1 IRQ'
if (t[0] != 'C' || t[3] != '1' || t[5] != 'I') {
serial_printk(t);
}
}