-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cpp
91 lines (80 loc) · 2.13 KB
/
kernel.cpp
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
#include "./types.h"
#include "gdt.h"
#include "port.h"
#include "print.h"
#include "interrupts.h"
#include "keyboard.h"
#include "syscall.h"
#include "jfs.h"
#include "multiboot.h"
#include "allocator.h"
extern "C" JumpFS* testfsmeta;
extern "C" void __cxa_pure_virtual() {asm("hlt");}
uint8_t set_video_mode(uint8_t mode) {
uint8_t result = 0;
asm volatile("movl $0x0, %%eax\n"
"movb %0, %%al\n"
"int $0x10" : "=ra"(result) : "r"(mode));
return result;
}
extern "C" void kmain(struct multiboot_info* multiboot_s, uint32_t magic) {
if ((multiboot_s->flags & 0x1) == 0) {
asm("hlt"); // error can't get size of memory
}
MemManager mm((void*)0x1000000, multiboot_s->mem_upper);
JumpFS testfs(testfsmeta);
GlobalDescriptorTable gdt;
InterruptManager im(&gdt);
// init hardware
KeyboardDriver kd(&im);
SyscallHandler sh(&im, &testfs);
im.activate();
set_video_mode(0x3);// text 80x25
cls();
printf("previous video mode ");
printh(multiboot_s->vbe_mode);
printf("\n");
printf("Welcome to MOS!\n");
#define LINE_LEN (4 + JFS_NAME_SZ + 1)
// com file\0
char line[LINE_LEN];
char* name = &line[4];
File* f = 0;
while (true) {
printf("COMMAND $ ");
get_line(line, LINE_LEN);
line[3] = '\0';
if (strn_eq(line, "lis", 3)) {
JFile* list = testfs.list();
for (int i = 0; i < testfs.get_count(); i++) {
printf((char*)list[i].get_name());
printf("\n");
}
continue;
}
f = testfs.open(name);
if (f == 0) {
printf("no such file\n");
}
if (strn_eq(line, "cat", 3)) {
uint8_t buf[11];
uint8_t read = 0;
for (int offset = 0; offset < f->get_size(); offset += 10) {
if ((read = f->read(offset, buf, 10)) < 0) {
printf("error catting file\n");
}
buf[read] = '\0';
printf((char*)buf);
}
} else if (strn_eq(line, "run", 3)) {
// most dangerous thing in the history of operating systems
f->exec((uint8_t*)"this was passed to program by arguments\n", 40);
} else {
printf("No command chose cat, run or lis\n");
}
}
while(true) {
// sleep until next interupt
asm volatile("hlt");
}
}