forked from ReKernel/ReKernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
executable file
·144 lines (123 loc) · 4.85 KB
/
kernel.c
File metadata and controls
executable file
·144 lines (123 loc) · 4.85 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
#include "kernel.h"
#include "gdt.h"
#include "vga.h"
#include "idt.h"
#include "pic.h"
#include "keyboard.h"
#include "memory.h"
/* ── Simple shell ────────────────────────────────────────────────────────────
Supports: help, clear, mem, echo <text>, malloc <n>
───────────────────────────────────────────────────────────────────────────── */
static int kstrcmp(const char *a, const char *b) {
while (*a && *a == *b) { a++; b++; }
return *a - *b;
}
static int kstrncmp(const char *a, const char *b, size_t n) {
while (n-- && *a && *a == *b) { a++; b++; }
return n == (size_t)-1 ? 0 : *a - *b;
}
static size_t katoi(const char *s) {
size_t n = 0;
while (*s >= '0' && *s <= '9') n = n * 10 + (size_t)(*s++ - '0');
return n;
}
#define CMD_BUF 128
static char cmd[CMD_BUF];
static int cmd_len = 0;
static void shell_prompt(void) {
vga_set_colour(VGA_LIGHT_GREEN, VGA_BLACK);
vga_puts("\nkernel> ");
vga_set_colour(VGA_WHITE, VGA_BLACK);
}
static void run_command(void) {
if (cmd_len == 0) return;
if (kstrcmp(cmd, "help") == 0) {
vga_set_colour(VGA_YELLOW, VGA_BLACK);
vga_puts("\nAvailable commands:\n");
vga_set_colour(VGA_LIGHT_GREY, VGA_BLACK);
vga_puts(" help — show this message\n");
vga_puts(" clear — clear the screen\n");
vga_puts(" mem — show heap stats\n");
vga_puts(" echo <text> — print text\n");
vga_puts(" malloc <n> — allocate n bytes and show the address\n");
} else if (kstrcmp(cmd, "clear") == 0) {
vga_clear();
} else if (kstrcmp(cmd, "mem") == 0) {
vga_set_colour(VGA_CYAN, VGA_BLACK);
vga_puts("\nMemory stats:\n");
vga_set_colour(VGA_LIGHT_GREY, VGA_BLACK);
memory_dump_stats();
} else if (kstrncmp(cmd, "echo ", 5) == 0) {
vga_putchar('\n');
vga_puts(cmd + 5);
} else if (kstrncmp(cmd, "malloc ", 7) == 0) {
size_t n = katoi(cmd + 7);
void *p = kmalloc(n);
if (p) {
vga_printf("\nAllocated %u bytes at 0x%x", n, (uint32_t)p);
kfree(p);
vga_puts(" (freed)");
}
} else {
vga_set_colour(VGA_LIGHT_RED, VGA_BLACK);
vga_printf("\nUnknown command: '%s' (try 'help')", cmd);
vga_set_colour(VGA_WHITE, VGA_BLACK);
}
}
static void shell_loop(void) {
shell_prompt();
while (1) {
char c = keyboard_getchar();
if (c == '\n') {
cmd[cmd_len] = '\0';
run_command();
cmd_len = 0;
shell_prompt();
} else if (c == '\b') {
if (cmd_len > 0) {
cmd_len--;
vga_putchar('\b');
}
} else if (cmd_len < CMD_BUF - 1) {
cmd[cmd_len++] = c;
vga_putchar(c);
}
}
}
/* ── Entry point ─────────────────────────────────────────────────────────────
Called from boot.asm after the stack is set up.
───────────────────────────────────────────────────────────────────────────── */
void kernel_main(void) {
gdt_init(); /* MUST be first — installs our own GDT before any ISR fires */
vga_init();
/* Banner */
vga_set_colour(VGA_YELLOW, VGA_BLACK);
vga_puts("===========================================\n");
vga_puts(" MyKernel v0.4 — x86 32-bit \n");
vga_puts("===========================================\n");
vga_set_colour(VGA_CYAN, VGA_BLACK);
vga_puts("[INIT] GDT... ");
vga_set_colour(VGA_LIGHT_GREEN, VGA_BLACK);
vga_puts("OK\n");
/* Subsystem initialisation */
vga_set_colour(VGA_CYAN, VGA_BLACK);
vga_puts("[INIT] Memory manager... ");
memory_init();
vga_set_colour(VGA_LIGHT_GREEN, VGA_BLACK);
vga_puts("OK\n");
vga_set_colour(VGA_CYAN, VGA_BLACK);
vga_puts("[INIT] IDT & PIC... ");
pic_remap();
idt_init();
vga_set_colour(VGA_LIGHT_GREEN, VGA_BLACK);
vga_puts("OK\n");
vga_set_colour(VGA_CYAN, VGA_BLACK);
vga_puts("[INIT] Keyboard driver.. ");
keyboard_init();
__asm__ volatile ("sti"); /* enable interrupts */
vga_set_colour(VGA_LIGHT_GREEN, VGA_BLACK);
vga_puts("OK\n");
vga_set_colour(VGA_LIGHT_GREY, VGA_BLACK);
vga_puts("\nType 'help' to see available commands.\n");
shell_loop(); /* never returns */
}