Add: cpuid, meminfo, system info; + minor fixes

This commit is contained in:
xamidev
2024-08-09 12:55:09 +02:00
parent f3b30bbb9a
commit e5d3b460b3
11 changed files with 100 additions and 12 deletions

BIN
com1.out

Binary file not shown.

Binary file not shown.

View File

@@ -14,8 +14,11 @@ char* ascii_title =
" o888ooo888 o888o 88ooo88 8o o888o o888o o888o o888o 88ooo88 o88oooo888\n\n" " o888ooo888 o888o 88ooo88 8o o888o o888o o888o o888o 88ooo88 o88oooo888\n\n"
" --------------------------------- v0.3.45 --------------------------------\n\n"; " --------------------------------- v0.3.45 --------------------------------\n\n";
int kmain(int retvalue) unsigned int g_multiboot_info_address;
void kmain(unsigned int multiboot_info_address)
{ {
g_multiboot_info_address = multiboot_info_address;
init_serial(); init_serial();
log("serial connection established", 3); log("serial connection established", 3);
@@ -29,18 +32,14 @@ int kmain(int retvalue)
__asm__ __volatile__("sti"); __asm__ __volatile__("sti");
log("initialized IRQs", 2), log("initialized IRQs", 2),
log("kernel started", 2);
clear(); clear();
colorputs(ascii_title, 10); colorputs(ascii_title, 10);
colorputs(" by @xamidev - star the repo for a cookie!\n\n", 14); colorputs(" by @xamidev - star the repo for a cookie!\n\n", 14);
// TODO: Grub modules to load programs
timer_install(); timer_install();
log("initialized timer handler", 2);
keyboard_install(); keyboard_install();
log("initialized keyboard driver", 2);
shell_install(); shell_install();
log("started system shell", 2);
return retvalue;
} }

View File

@@ -19,7 +19,8 @@ extern kmain
loader: loader:
cli cli
; mov eax, 0xCAFEBABE ; mov eax, 0xCAFEBABE
push dword 42 ; push dword 42
push ebx
call kmain call kmain
.loop: .loop:

View File

@@ -64,6 +64,7 @@ void shell_install()
register_command("bf", program_bf); register_command("bf", program_bf);
register_command("uptime", program_uptime); register_command("uptime", program_uptime);
register_command("echo", program_echo); register_command("echo", program_echo);
register_command("sysinfo", program_sysinfo);
for (;;) for (;;)
{ {

11
src/kernel/sysinfo.c Normal file
View File

@@ -0,0 +1,11 @@
#include "../libc/stdio.h"
#include "../libc/string.h"
void cpuid(int code, unsigned int* a, unsigned int* d)
{
asm volatile("cpuid"
: "=a"(*a), "=d"(*d)
: "a"(code)
: "ecx", "ebx");
}

6
src/kernel/sysinfo.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef SYSINFO_H
#define SYSINFO_H
void cpuid(int code, unsigned int* a, unsigned int* d);
#endif

View File

@@ -26,5 +26,7 @@ int uptime();
extern volatile unsigned long global_ticks; extern volatile unsigned long global_ticks;
extern unsigned int g_multiboot_info_address;
#endif #endif

View File

@@ -45,7 +45,7 @@ void program_uptime()
void program_help() void program_help()
{ {
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\tuptime\techo\n"); printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\n");
} }
// Panic // Panic

View File

@@ -5,6 +5,10 @@ void program_words();
void program_primes(); void program_primes();
void program_math(); void program_math();
void program_bf(); void program_bf();
void program_sysinfo();
void get_cpuid();
void get_meminfo(unsigned int multiboot_info_address);
// Misc // Misc
void program_rainbow(); void program_rainbow();

64
src/programs/sysinfo.c Normal file
View File

@@ -0,0 +1,64 @@
#include "../kernel/sysinfo.h"
#include "../libc/stdio.h"
#include "../kernel/system.h"
extern unsigned int multiboot_info_address;
typedef struct multiboot_memory_map
{
unsigned int size;
unsigned int base_addr_low;
unsigned int base_addr_high;
unsigned int length_low;
unsigned int length_high;
unsigned int type;
} multiboot_memory_map_t;
void get_cpuid()
{
// CPUid
unsigned int eax, edx;
char vendor[13];
unsigned int* v = (unsigned int*)vendor;
asm volatile("cpuid"
: "=b"(v[0]), "=d"(v[1]), "=c"(v[2])
: "a"(0));
vendor[12] = '\0';
cpuid(1, &eax, &edx);
unsigned int model = (eax >> 4) & 0xF;
unsigned int family = (eax >> 8) & 0xF;
printf("CPU information\n\tvendor: %s\n\tfamily: %u\n\tmodel: %u\n\tfeatures: 0x%x\n", vendor, family, model, edx);
}
void get_meminfo(unsigned int multiboot_info_address)
{
// RAM
unsigned int mem_lower = *((unsigned int*)(multiboot_info_address + 4));
unsigned int mem_upper = *((unsigned int*)(multiboot_info_address + 8));
printf("RAM information\n\tLower memory: %u KB\n\tUpper memory: %u KB\n", mem_lower, mem_upper);
multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)*((unsigned int*)(multiboot_info_address + 44));
printf("\tMemory map:\n");
while ((unsigned int)mmap < multiboot_info_address + *((unsigned int*)(multiboot_info_address + 40)))
{
printf("\t\tBase addr: 0x%x%x\n\t\tLength: 0x%x%x\n\t\tType: %u\n",
mmap->base_addr_high, mmap->base_addr_low,
mmap->length_high, mmap->length_low,
mmap->type);
mmap = (multiboot_memory_map_t*)((unsigned int)mmap + mmap->size + sizeof(unsigned int));
}
}
void program_sysinfo()
{
get_cpuid();
get_meminfo(g_multiboot_info_address);
}