Espresso 0.0.2a

This commit is contained in:
2025-10-20 21:57:30 -05:00
parent 102d517097
commit ff6cba1164
59 changed files with 29272 additions and 773 deletions

View File

@ -1,16 +1,66 @@
#include <stdint.h>
#include <string.h>
#include <cpuid.h>
#include <stdlib.h>
#include <kernel/boot.h>
uint8_t parse_boot_data(const char* data)
extern uint8_t __bss_start;
extern uint8_t __bss_end;
void clear_bss(void)
{
if (strlen(data) == 0)
{
return 0;
}
/*memset(&__bss_start, 0, &__bss_end - &__bss_start);*/
return 0;
for (int i = 0; i < ((&__bss_end) - (&__bss_start)); i++)
{
(&(__bss_start))[i] = 0x0;
}
}
char* get_cpu_vendor_string(void)
{
unsigned int eax, ebx, ecx, edx;
char vendor[13];
if (!__get_cpuid(0, &eax, &ebx, &ecx, &edx))
{
return strdup("Unknown");
}
memcpy(&vendor[0], &ebx, 4);
memcpy(&vendor[4], &edx, 4);
memcpy(&vendor[8], &ecx, 4);
vendor[12] = '\0';
return strdup(vendor);
}
char* get_cpu_brand_string(void)
{
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
int k = __get_cpuid_max(0x80000000, NULL);
if (k >= (int) 0x80000005)
{
return strdup("Unknown");
}
char brand[49];
unsigned int* brand_ptr = (unsigned int*) brand;
for (unsigned int i = 0; i < 3; ++i)
{
__get_cpuid(0x80000002 + i, &eax, &ebx, &ecx, &edx);
brand_ptr[i * 4 + 0] = eax;
brand_ptr[i * 4 + 1] = ebx;
brand_ptr[i * 4 + 2] = ecx;
brand_ptr[i * 4 + 3] = edx;
}
brand[48] = '\0';
return strdup(brand);
}

View File

@ -1,19 +1,30 @@
#include <string.h>
#include <tty.h>
#include <stdio.h>
#include <port_io.h>
#include <stdlib.h>
#include <drivers/ps2_keyboard.h>
#include <kernel/intro.h>
void delay_us(uint32_t microseconds, uint32_t cpu_mhz)
{
uint32_t count = cpu_mhz * microseconds;
while (count--)
{
asm volatile ("nop" ::: "memory");
}
}
void ack_char(char c);
void start_kernel_shell(void);
char char_entered = 0x00;
void intro_begin(void)
{
extern char* kernel_version;
char* fin = (char*) malloc(strlen(kernel_version) + 6);
memset(fin, 0, (strlen(kernel_version) + 5));
strcpy(fin, kernel_version);
#ifdef _DEBUG
strcat(fin, " DEBUG");
#endif
begin_anim(fin);
}
int16_t begin_anim(const char* version)
{
@ -31,11 +42,18 @@ int16_t begin_anim(const char* version)
int16_t b = 0;
int32_t sh_pos = VGA_WIDTH / 2;
int32_t sv_pos = VGA_HEIGHT / 2;
setup_hook((ps2_hook_t) ack_char);
while (true)
{
terminal_clear();
if (char_entered != 0)
{
break;
}
for (int32_t i = 0; n[i]; ++i)
{
terminal_putentryat(n[i], terminal_getcolor(), sh_pos, sv_pos);
@ -64,11 +82,35 @@ int16_t begin_anim(const char* version)
b = 0;
delay_us(50000, 5000);
sleep(1000);
}
if (char_entered != 0)
{
start_kernel_shell();
}
else
{
terminal_clear();
}
terminal_clear();
return 0;
}
void ack_char(char c)
{
char_entered = c;
return;
}
void start_kernel_shell(void)
{
/* terminal_clear() already called by begin_anim() */
extern int kshell_start(int argc, char** argv);
kshell_start(0, NULL);
return;
}

25
kernel/kdebug.c Normal file
View File

@ -0,0 +1,25 @@
#include <kdebug.h>
static bool __debug = false;
void set_debug(void)
{
__debug = true;
}
void clear_debug(void)
{
__debug = false;
}
void toggle_debug(void)
{
__debug = !__debug;
}
bool get_debug(void)
{
return __debug;
}

View File

@ -11,8 +11,13 @@
#include <types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <kernel/boot.h>
#include <kernel/kshell.h>
#include <kdebug.h>
#include <gdt.h>
#include <drivers/idt.h>
@ -20,7 +25,6 @@
#include <multiboot.h>
#include <stdio.h>
#include <drivers/pci.h>
#include <drivers/ps2_keyboard.h>
#include <drivers/pit.h>
@ -38,6 +42,8 @@
#include <builtin_games/miner.h>
#include <fs/ssfs.h>
#define DEBUG
@ -53,18 +59,24 @@ char* espresso_str = ""
"# # # # # # # # # # # # #\n"
"####### ##### # # # ####### ##### ##### #######\n";
char* kernel_version = "0.0.2a";
void kernel_main(multiboot_info_t* mbd, uint32_t magic)
{
/* --- BEGIN INITIALIZATION SECTION --- */
const char* espresso_kernel_version = "0.0.1e";
/* We need to initialize the terminal so that any error/debugging messages show. */
terminal_initialize();
printf("Loading Espresso %s...\n", espresso_kernel_version);
printf("Loading Espresso %s... ", kernel_version);
#ifdef _DEBUG
printf("[ DEBUG BUILD ]");
#endif
printf("\n");
terminal_setcolor(VGA_COLOR_RED);
@ -85,24 +97,20 @@ void kernel_main(multiboot_info_t* mbd, uint32_t magic)
gdt_install(false);
pic_remap();
idt_init();
_sti_asm();
irq_init(); /* MUST be done after pic_remap() and idt_init() */
terminal_setcolor(VGA_COLOR_GREEN);
printd("Initializing physical memory manager...\n");
pmm_init(mbd);
printd("Physical memory manager initialized\n");
printd("Initializing paging...\n");
paging_init();
printd("Paging initialized\n");
printd("Initializing heap allocator...\n");
heap_init();
printd("Heap allocator initialized\n");
heap_init(0xC2000000, 0x80000);
printd("Testing SSE...\n");
int32_t sse_test_result = test_sse();
@ -115,31 +123,13 @@ void kernel_main(multiboot_info_t* mbd, uint32_t magic)
printd("SSE test passed\n");
}
printd("Initializing the PIT...\n");
pit_init();
printd("PIT initialized\n");
printd("Initializing the PS/2 keyboard...\n");
keyboard_init();
printd("PS/2 Keyboard initialized\n");
/*
printd("Initalizing AHCI...\n");
ahci_init();
printd("AHCI initialized\n");
*/
printd("Initializing IDE system...\n");
ide_initialize();
printd("IDE initialized\n");
/*printd("Initializing DuckFS...\n");
duckfs_init();
printd("DuckFS initialized\n");*/
printd("Initializing PCI...\n");
pci_enumerate();
printd("PCI initialized\n");
pci_init();
/* --- END INITIALIZATION SECTION --- */
@ -148,22 +138,19 @@ void kernel_main(multiboot_info_t* mbd, uint32_t magic)
/*pit_sleep(4000);
begin_anim(espresso_kernel_version);*/
begin_anim(kernel_version);*/
printf("Guten tag and welcome to Espresso %s\n", espresso_kernel_version);
printf("%s\n", espresso_str);
char buffer[512] = { 0 };
printf("Guten tag and welcome to Espresso %s\n", kernel_version);
int32_t i = sfs_read_file("test.txt", buffer, -1);
sleep(1000);
printf("%i\n", i);
intro_begin();
/*extern void terminal_clear(void);
if (i == 0)
{
printf("%s\n", buffer);
}
terminal_clear();
kshell_start(1, NULL);*/
while (true)
{

54
kernel/ksh_debug.c Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <types.h>
#include <kernel/ksh_debug.h>
void print_all_regs(void)
{
uint32_t eax, ebx, ecx, edx, esi, edi, esp, ebp, eip;
uint16_t cs, ds, es, ss;
asm volatile ("mov %%eax, %0" : "=r"(eax));
asm volatile ("mov %%ebx, %0" : "=r"(ebx));
asm volatile ("mov %%ecx, %0" : "=r"(ecx));
asm volatile ("mov %%edx, %0" : "=r"(edx));
asm volatile ("mov %%esi, %0" : "=r"(esi));
asm volatile ("mov %%edi, %0" : "=r"(edi));
asm volatile ("mov %%esp, %0" : "=r"(esp));
asm volatile ("mov %%ebp, %0" : "=r"(ebp));
asm volatile (
"call 1f\n"
"1: pop %0\n"
: "=r"(eip)
);
asm volatile ("mov %%cs, %0" : "=r"(cs));
asm volatile ("mov %%ds, %0" : "=r"(ds));
asm volatile ("mov %%es, %0" : "=r"(es));
asm volatile ("mov %%ss, %0" : "=r"(ss));
printf("EAX -> 0x%x EBX -> 0x%x ECX -> 0x%x EDX -> 0x%x\n", eax, ebx, ecx, edx);
printf("ESI -> 0x%x EDI -> 0x%x ESP -> 0x%x EBP -> 0x%x\n", esi, edi, esp, ebp);
printf("EIP (maybe) -> 0x%x\n", eip);
printf("CS -> 0x%04x DS -> 0x%04x ES -> 0x%04x SS -> 0x%04x\n", cs, ds, es, ss);
}
void print_gprs(void)
{
uint32_t eax, ebx, ecx, edx, esi, edi, esp, ebp;
asm volatile ("mov %%eax, %0" : "=r"(eax));
asm volatile ("mov %%ebx, %0" : "=r"(ebx));
asm volatile ("mov %%ecx, %0" : "=r"(ecx));
asm volatile ("mov %%edx, %0" : "=r"(edx));
asm volatile ("mov %%esi, %0" : "=r"(esi));
asm volatile ("mov %%edi, %0" : "=r"(edi));
asm volatile ("mov %%esp, %0" : "=r"(esp));
asm volatile ("mov %%ebp, %0" : "=r"(ebp));
printf("EAX -> 0x%x EBX -> 0x%x ECX -> 0x%x EDX -> 0x%x\n", eax, ebx, ecx, edx);
printf("ESI -> 0x%x EDI -> 0x%x ESP -> 0x%x EBP -> 0x%x\n", esi, edi, esp, ebp);
}

287
kernel/kshell.c Normal file
View File

@ -0,0 +1,287 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <drivers/ps2_keyboard.h>
#include <tty.h>
#include <kernel/ksh_debug.h>
#include <kernel/kshell.h>
const char* shell_version = "0.0.2";
char* prompt = NULL;
int command = -1;
bool _debug = false;
int execute(void);
static void print_intro(void)
{
printf("Espresso kshell, ver %s on Espresso %s", shell_version, KERNEL_VERSION);
#ifdef _DEBUG
printf(" DEBUG BUILD");
#endif
printf("\nSSE level: ");
command = 0;
execute();
extern char* get_cpu_vendor_string(void);
extern char* get_cpu_brand_string(void);
char* temp_ = get_cpu_vendor_string();
char* _temp = get_cpu_brand_string();
printf("CPU: %s\n", _temp == NULL ? "No info" : _temp);
printf("CPU vendor: %s\n", temp_ == NULL ? "No info" : temp_);
if (temp_)
{
free(temp_);
}
if (_temp)
{
free(_temp);
}
printf("\nCopyright 2025 David J Goeke\n");
}
static void parse_opts(int argc, char** argv)
{
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "--color") == 0)
{
}
}
}
int kshell_start(int argc, char** argv)
{
(void)argc;
(void)argv;
prompt = strdup(">");
char* i = NULL;
terminal_setcolor(VGA_COLOR_LIGHT_BLUE);
print_intro();
command = -1;
do
{
printf("%s ", prompt);
i = gets();
command = -1;
if (strcmp(i, "") == 0)
{
continue;
}
else if (strcmp(i, "exit") == 0 || strcmp(i, "quit") == 0)
{
break;
}
else if (strcmp(i, "sseinfo") == 0)
{
command = 0;
}
else if (strcmp(i, "kinfo") == 0)
{
command = 1;
}
else if (strcmp(i, "enable_debug") == 0)
{
_debug = true;
}
else if (strcmp(i, "disable_debug") == 0)
{
_debug = false;
}
else if (strcmp(i, "toggle_debug") == 0)
{
_debug = !_debug;
}
else if (strcmp(i, "get_debug") == 0)
{
printf("Debugging: %s\n", _debug == true ? "On" : "Off");
}
else if (strcmp(i, "dumpregs") == 0)
{
command = 2;
}
else if (strcmp(i, "dumpgprs") == 0)
{
command = 3;
}
else if (strcmp(i, "kernelmem") == 0)
{
command = 4;
}
else if (strcmp(i, "sectionmem") == 0)
{
command = 5;
}
else if (strcmp(i, "testascii") == 0)
{
command = 6;
}
else if (strcmp(i, "asciitype") == 0)
{
command = 7;
}
else
{
printf("Unknown command %s len %i\n", i, strlen(i));
}
if (_debug)
{
printf(" ASCII -> ");
size_t len = strlen(i) + 1;
for (size_t n = 0; n < len; n++)
{
printf("%02X ", (int) i[n]);
}
printf("\n");
}
execute();
} while (1);
free(i);
printf("Goodbye\n");
return 0;
}
int execute(void)
{
switch (command)
{
case 0: {
extern int sse_initialized;
switch (sse_initialized)
{
case 0:
printf("No SSE support");
break;
case 1:
printf("SSE1");
break;
case 2:
printf("SSE2");
break;
case 3:
printf("SSE3");
break;
case 4:
printf("SSSE3");
break;
case 5:
printf("SSE4.1");
break;
case 6:
printf("SSE4.2");
break;
default:
printf("WARNING: sse_initialized is in a invalid state!");
break;
}
printf("\n");
break;
}
case 1: {
printf("Espresso %s\n", KERNEL_VERSION);
break;
}
case 2: {
print_all_regs();
break;
}
case 3: {
print_gprs();
break;
}
case 4: {
extern uint8_t __kernel_start;
extern uint8_t __kernel_end;
size_t kernel_size = (size_t)&__kernel_end - (size_t)&__kernel_start;
printf("Kernel start: 0x%x\n", (&__kernel_start));
printf("Kernel end: 0x%x\n", (&__kernel_end));
printf("Kernel size (bytes): %llu\n", (uint64_t) kernel_size);
printf("Kernel size (kbytes): %llu\n", (((uint64_t) kernel_size) / 1000));
printf("Kernel size (mbytes): %f\n", (((uint64_t) kernel_size) / 1000.0) / 1000.0);
break;
}
case 5: {
extern uint8_t __kernel_text_start;
extern uint8_t __kernel_text_end;
extern uint8_t __kernel_rodata_start;
extern uint8_t __kernel_rodata_end;
extern uint8_t __kernel_data_start;
extern uint8_t __kernel_data_end;
extern uint8_t __bss_start;
extern uint8_t __bss_end;
printf(".text: %i\n.data: %i\n.rodata: %i\n.bss: %i\n", ((&__kernel_text_end) - (&__kernel_text_start)), ((&__kernel_data_end) - (&__kernel_data_start)),
((&__kernel_rodata_end) - (&__kernel_rodata_start)), ((&__bss_end) - (&__bss_start)));
break;
}
case 6: {
/*extern void terminal_clear(void);*/
terminal_clear();
printf("Printing ASCII chars 0x00 through 0xFE, hit enter to exit\n");
sleep(2500);
terminal_clear();
for (int i = 0x00; i < 0xFF; i++)
{
printf("%c", (char) i);
}
printf("\nCool ASCII art\n%c%c\n%c%c\n", 0xDA, 0xBF, 0xC0, 0xD9);
gets();
break;
}
case 7: {
break;
}
}
return 0;
}

16
kernel/vars.c Normal file
View File

@ -0,0 +1,16 @@
#include <types.h>
#include <string.h>
#include <stdlib.h>
#include <kernel/vars.h>
static uint8_t* vars_start = NULL;
static int num_bytes_used = 0;
void init_vars(void)
{
vars_start = (uint8_t*) malloc(8 * 1024); /* 1 KiB */
memclr(vars_start, (size_t) 1024);
}