Files
opencoreC/servers/sh/main.cpp
T

1234 lines
52 KiB
C++

#include "ipc.hpp"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "abi/syscalls.h"
#include "abi/net.h"
#include "fb.h"
#include "rtl8139.h"
#include "pci.h"
#define UART_ENDPOINT 2
#define PROCMGR_ENDPOINT 4
// In-Memory Virtual File System (Ramdisk)
struct VfsFile {
char name[32];
char content[2048];
bool is_dir;
};
static VfsFile vfs_nodes[64] = {
{ "/etc", "", true },
{ "/bin", "", true },
{ "/boot", "", true },
{ "/proc", "", true },
{ "/dev", "", true },
{ "/etc/os-release", "NAME=\"opencoreC\"\nVERSION=\"0.1.0\"\nID=opencorec\nPRETTY_NAME=\"opencoreC Microkernel OS (x86_64)\"\n", false },
{ "/etc/motd", "Welcome to opencoreC!\nHigh-performance microkernel written in Rust, C, and C++20.\n", false },
{ "/etc/hosts", "127.0.0.1 localhost\n", false },
{ "/proc/version", "opencoreC version 0.1.0-release (rustc 1.98.0 / gcc 16.2.1) x86_64 SMP\n", false },
{ "/proc/cpuinfo", "model name: x86_64 Microkernel Virtual CPU\nfeatures: long_mode syscall hhdm smp sse sse2 avx\n", false },
{ "/dev/null", "", false },
{ "/dev/uart0", "16550 Serial COM1 Port\n", false },
{ "/dev/net0", "PCI Fast Ethernet Network Adapter\n", false },
};
static size_t vfs_count = 13;
static void vfs_init_modules() {
char buf[512] = {0};
int len = sys_module_list(buf, sizeof(buf) - 1);
if (len > 0) {
char *p = buf;
while (*p && vfs_count < 60) {
char mod[64] = {0};
int mi = 0;
while (*p && *p != ' ' && mi < 63) {
mod[mi++] = *p++;
}
while (*p == ' ') p++;
if (mi > 0) {
snprintf(vfs_nodes[vfs_count].name, sizeof(vfs_nodes[vfs_count].name), "/bin/%s", mod);
snprintf(vfs_nodes[vfs_count].content, sizeof(vfs_nodes[vfs_count].content), "Executable binary: %s\n", mod);
vfs_nodes[vfs_count].is_dir = false;
vfs_count++;
}
}
}
}
// Shell Environment Variables
struct EnvVar {
char key[32];
char val[64];
};
static EnvVar g_env[16] = {
{ "USER", "root" },
{ "HOME", "/root" },
{ "SHELL", "/bin/osh" },
{ "TERM", "xterm-256color" },
{ "PATH", "/bin:/usr/bin" },
};
static size_t g_env_count = 5;
static void get_cpu_brand(char *brand, size_t max_len) {
memset(brand, 0, max_len);
uint32_t regs[4];
__asm__ volatile("cpuid" : "=a"(regs[0]), "=b"(regs[1]), "=c"(regs[2]), "=d"(regs[3]) : "a"(0x80000000));
if (regs[0] >= 0x80000004) {
char *p = brand;
for (uint32_t leaf = 0x80000002; leaf <= 0x80000004; leaf++) {
uint32_t b_regs[4];
__asm__ volatile("cpuid" : "=a"(b_regs[0]), "=b"(b_regs[1]), "=c"(b_regs[2]), "=d"(b_regs[3]) : "a"(leaf));
if (p - brand + 16 < (int)max_len) {
memcpy(p, b_regs, 16);
p += 16;
}
}
brand[max_len - 1] = '\0';
char *src = brand;
while (*src == ' ') src++;
if (src != brand) {
memmove(brand, src, strlen(src) + 1);
}
} else {
__asm__ volatile("cpuid" : "=a"(regs[0]), "=b"(regs[1]), "=c"(regs[2]), "=d"(regs[3]) : "a"(0));
memcpy(brand, &regs[1], 4);
memcpy(brand + 4, &regs[3], 4);
memcpy(brand + 8, &regs[2], 4);
brand[12] = '\0';
}
}
static void print_fastfetch() {
sysinfo_data_t info = {0, 0, 0, 0, 0};
sys_sysinfo(&info);
uint64_t total_mb = info.total_memory_bytes / (1024 * 1024);
uint64_t free_mb = info.free_memory_bytes / (1024 * 1024);
uint64_t used_mb = (total_mb >= free_mb) ? (total_mb - free_mb) : 0;
uint32_t uptime_sec = (uint32_t)(info.uptime_ticks / 100);
acpi_info_data_t acpi = {0, 0, 0, 0, 0};
register uint64_t rax __asm__("rax") = SYS_ACPI_INFO;
register uint64_t rdi __asm__("rdi") = (uint64_t)&acpi;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
fb_info_t fb = {0, 0, 0, 0, 0};
rax = SYS_FB_INFO;
rdi = (uint64_t)&fb;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
gpu_info_t gpu;
bool has_gpu = (pci_detect_gpu(&gpu) == 0);
char cpu_brand[64] = {0};
get_cpu_brand(cpu_brand, sizeof(cpu_brand));
printf("\n");
printf(" \033[1;36m ___ \033[1;32mroot\033[0m@\033[1;36mopencoreC\033[0m\n");
printf(" \033[1;36m / \\ \033[0;37m-----------------\033[0m\n");
printf(" \033[1;36m | (o) | ___ \033[1;34mOS:\033[0m opencoreC Microkernel x86_64\n");
printf(" \033[1;36m | | / \\ \033[1;34mKernel:\033[0m v0.1.0-release (Rust #![no_std])\n");
printf(" \033[1;36m \\ ___ / | (o) | \033[1;34mUptime:\033[0m %u mins, %u secs\n", uptime_sec / 60, uptime_sec % 60);
printf(" \033[1;36m \\___/ \033[1;34mShell:\033[0m osh (opencore shell 1.0)\n");
if (fb.width > 0 && fb.height > 0) {
printf(" \033[1;34m [=== opencoreC ===] \033[1;34mDisplay:\033[0m %ux%u @ 60Hz 32bpp (Linear FB)\n", (unsigned int)fb.width, (unsigned int)fb.height);
} else {
printf(" \033[1;34m [=== opencoreC ===] \033[1;34mDisplay:\033[0m 1280x800 @ 60Hz (Limine FB)\n");
}
printf(" \033[1;35m x86_64 SMP \033[1;34mUI:\033[0m Pure CLI Terminal (Direct VGA/GOP FB & Serial)\n");
printf(" \033[1;35m Ring 3 Userspace \033[1;34mCPU:\033[0m %s (%u Cores SMP)\n",
*cpu_brand ? cpu_brand : "x86_64 Processor",
acpi.lapic_count > 0 ? (unsigned int)acpi.lapic_count : 4);
if (has_gpu) {
if (gpu.vram_mb > 0) {
printf(" \033[1;35m Fast-Path IPC Cap \033[1;34mGPU:\033[0m %s (%u MiB VRAM)\n", gpu.model_name, (unsigned int)gpu.vram_mb);
} else {
printf(" \033[1;35m Fast-Path IPC Cap \033[1;34mGPU:\033[0m %s\n", gpu.model_name);
}
} else {
printf(" \033[1;35m Fast-Path IPC Cap \033[1;34mGPU:\033[0m Linear UEFI GOP Framebuffer\n");
}
printf(" \033[1;34mMemory:\033[0m %u MiB / %u MiB (%u%%)\n",
(unsigned int)used_mb, (unsigned int)total_mb,
total_mb > 0 ? (unsigned int)(used_mb * 100 / total_mb) : 0);
char ip_str[32] = {0};
format_ip(g_net_cfg.ip, ip_str);
if (g_net_cfg.ip != 0) {
printf(" \033[1;34mNetwork:\033[0m PCI Ethernet (%s)\n", ip_str);
} else {
printf(" \033[1;34mNetwork:\033[0m PCI Ethernet (Unconfigured)\n");
}
printf(" \033[1;34mACPI:\033[0m S5 Poweroff, Reset, MADT %u Cores\n",
acpi.lapic_count > 0 ? (unsigned int)acpi.lapic_count : 4);
printf(" \033[1;34mSecurity:\033[0m Ring 3 Signal Fault Isolation (SIGSEGV)\n");
printf(" \033[1;34mEntropy:\033[0m Hardware RNG (RDRAND / TSC)\n\n");
printf(" \033[40m \033[41m \033[42m \033[43m \033[44m \033[45m \033[46m \033[47m \033[0m\n");
printf(" \033[100m \033[101m \033[102m \033[103m \033[104m \033[105m \033[106m \033[107m \033[0m\n\n");
}
static void print_prompt() {
const char prompt[] = "\033[1;34m┌──(\033[1;32mroot\033[1;37m@\033[1;36mopencoreC\033[1;34m)-[\033[1;33m~\033[1;34m]\n\033[1;34m└─\033[1;35m#\033[0m ";
sys_log_debug(prompt, sizeof(prompt) - 1);
}
static int eval_calc(const char *expr) {
int result = 0;
char op = '+';
int current_num = 0;
while (*expr) {
if (*expr >= '0' && *expr <= '9') {
current_num = current_num * 10 + (*expr - '0');
}
if (*expr == '+' || *expr == '-' || *expr == '*' || *(expr + 1) == '\0') {
if (op == '+') result += current_num;
else if (op == '-') result -= current_num;
else if (op == '*') result *= current_num;
op = *expr;
current_num = 0;
}
expr++;
}
return result;
}
static const char *thread_state_str(uint32_t state) {
switch (state) {
case 1: return "READY";
case 2: return "RUNNING";
case 3: return "BLOCKED";
case 4: return "DEAD";
default: return "UNKNOWN";
}
}
static void lspci_device_cb(const pci_device_t *dev) {
const char *class_str = pci_class_to_string(dev->class_code, dev->subclass);
const char *vendor_str = pci_vendor_to_string(dev->vendor_id);
printf(" \033[1;33m%02x:%02x.%d\033[0m \033[1;36m[%04x:%04x]\033[0m \033[1;32m%-30s\033[0m : %s\n",
dev->bus, dev->slot, dev->func,
dev->vendor_id, dev->device_id,
class_str, vendor_str);
}
static void cmd_lspci() {
printf("\n\033[1;37mPCI Bus Topology & Hardware Inventory:\033[0m\n");
printf("--------------------------------------------------------------------------------\n");
pci_scan_bus(lspci_device_cb);
printf("--------------------------------------------------------------------------------\n\n");
}
static void cmd_gpu() {
gpu_info_t gpu;
if (pci_detect_gpu(&gpu) == 0) {
printf("\n\033[1;32mHardware GPU Accelerator Detected:\033[0m\n");
printf(" Vendor: %s (0x%04x)\n", gpu.vendor_name, gpu.vendor_id);
printf(" Model: \033[1;36m%s\033[0m (Device ID: 0x%04x)\n", gpu.model_name, gpu.device_id);
printf(" PCI Slot: Bus %02x, Device %02x, Function %d\n", gpu.bus, gpu.slot, gpu.func);
if (gpu.vram_mb > 0) {
printf(" VRAM: \033[1;33m%u MiB\033[0m Physical Video Memory (BAR Aperture)\n", (unsigned int)gpu.vram_mb);
} else {
printf(" VRAM: Linear GOP Framebuffer\n");
}
printf(" Display: 1280x800 32bpp Double-Buffered SSE2 SIMD Compositor\n\n");
} else {
puts("GPU: No dedicated PCI 3D/VGA controller detected; using UEFI GOP framebuffer.");
}
}
static void editor_render(const char *filename, const char *buf, size_t cursor_pos, const char *status) {
(void)cursor_pos;
sys_log_debug("\033[2J\033[H", 7);
printf("\033[7m opencoreC Editor (kilo/nano) | File: %-18s | Length: %-4u bytes \033[0m\r\n",
filename, (unsigned int)strlen(buf));
int line_num = 1;
printf("\033[1;30m%3d │ \033[0m", line_num);
const char *p = buf;
while (*p) {
putchar(*p);
if (*p == '\n') {
line_num++;
printf("\033[1;30m%3d │ \033[0m", line_num);
}
p++;
}
printf("\r\n\r\n\033[7m [Ctrl+S] Save | [Ctrl+Q / ESC] Exit | Status: %-26s \033[0m\r\n",
status ? status : "Ready (Type text)");
}
static void cmd_edit(const char *filename) {
if (!filename || !*filename) {
puts("Usage: edit <filename>");
return;
}
int file_idx = -1;
for (size_t i = 0; i < vfs_count; i++) {
if (strcmp(vfs_nodes[i].name, filename) == 0) {
file_idx = (int)i;
break;
}
}
if (file_idx < 0) {
if (vfs_count >= 64) {
puts("edit: Ramdisk full, cannot create new file.");
return;
}
file_idx = (int)vfs_count++;
strncpy(vfs_nodes[file_idx].name, filename, 31);
vfs_nodes[file_idx].content[0] = '\0';
vfs_nodes[file_idx].is_dir = false;
}
char buf[2048];
memset(buf, 0, sizeof(buf));
strncpy(buf, vfs_nodes[file_idx].content, sizeof(buf) - 1);
size_t cursor_pos = strlen(buf);
const char *status = "Editing (Ctrl+S to save, Ctrl+Q to exit)";
editor_render(filename, buf, cursor_pos, status);
while (true) {
int c = sys_key_read();
if (c <= 0) {
sys_yield();
continue;
}
if (c == 17 || c == 27) { // Ctrl+Q or ESC -> Exit
break;
} else if (c == 19) { // Ctrl+S -> Save
strncpy(vfs_nodes[file_idx].content, buf, sizeof(vfs_nodes[file_idx].content) - 1);
status = "FILE SAVED TO RAMDISK!";
editor_render(filename, buf, cursor_pos, status);
} else if (c == '\b' || c == 127 || c == 8) { // Backspace
if (cursor_pos > 0) {
memmove(&buf[cursor_pos - 1], &buf[cursor_pos], strlen(&buf[cursor_pos]) + 1);
cursor_pos--;
status = "Modified";
editor_render(filename, buf, cursor_pos, status);
}
} else if (c == '\r' || c == '\n') { // Enter
if (strlen(buf) < sizeof(buf) - 2) {
memmove(&buf[cursor_pos + 1], &buf[cursor_pos], strlen(&buf[cursor_pos]) + 1);
buf[cursor_pos] = '\n';
cursor_pos++;
status = "Modified";
editor_render(filename, buf, cursor_pos, status);
}
} else if (c >= 32 && c <= 126) { // Printable
if (strlen(buf) < sizeof(buf) - 2) {
memmove(&buf[cursor_pos + 1], &buf[cursor_pos], strlen(&buf[cursor_pos]) + 1);
buf[cursor_pos] = (char)c;
cursor_pos++;
status = "Modified";
editor_render(filename, buf, cursor_pos, status);
}
}
}
sys_log_debug("\033[2J\033[H", 7);
printf("Exited editor. File '%s' saved in ramdisk.\n", filename);
}
static void cmd_run_module(const char *mod_name) {
while (*mod_name == '.' || *mod_name == '/') {
mod_name++;
}
printf("[osh] Requesting Process Manager (procmgr) to spawn '%s'...\n", mod_name);
uint64_t p0 = 0, p1 = 0;
memcpy(&p0, mod_name, strlen(mod_name) < 8 ? strlen(mod_name) : 8);
if (strlen(mod_name) > 8) {
memcpy(&p1, mod_name + 8, strlen(mod_name) - 8 < 8 ? strlen(mod_name) - 8 : 8);
}
uint64_t out0 = 0, out1 = 0;
sysret_t ret = sys_ipc_call_raw(PROCMGR_ENDPOINT, PROC_OP_SPAWN, p0, p1, 0, 0, &out0, &out1);
int64_t tid = (ret == 0) ? (int64_t)out0 : -1;
if (tid >= 0) {
printf("[procmgr] Process successfully spawned with TID %ld on Ring 3!\n", (long)tid);
for (int i = 0; i < 6; i++) sys_yield();
} else {
printf("osh: failed to execute '%s' (module not found or invalid format)\n", mod_name);
puts("Hint: Type 'modules' to see all available in-memory boot modules.");
}
}
static void cmd_modules() {
char buf[512] = {0};
int len = sys_module_list(buf, sizeof(buf) - 1);
if (len > 0) {
printf("\n\033[1;37mIn-Memory Boot Modules (Available for Execution):\033[0m\n");
printf("------------------------------------------------------\n");
char *p = buf;
while (*p) {
char mod[64] = {0};
int mi = 0;
while (*p && *p != ' ' && mi < 63) {
mod[mi++] = *p++;
}
while (*p == ' ') p++;
if (strstr(mod, ".opc")) {
printf(" • \033[1;32m%-24s\033[0m [Native OpenCore Binary]\n", mod);
} else if (strstr(mod, ".elf")) {
printf(" • \033[1;36m%-24s\033[0m [Standard ELF64 Binary]\n", mod);
} else {
printf(" • %-24s\n", mod);
}
}
printf("------------------------------------------------------\n");
puts("To run: type '<program>', './<program>', or 'run <program>'\n");
} else {
puts("modules: No boot modules detected.");
}
}
static void cmd_curl(const char *target) {
if (!target || !*target) {
puts("Usage: curl <http://domain-or-ip/path>");
return;
}
char body[2048];
memset(body, 0, sizeof(body));
printf("[curl] Fetching '%s' over TCP/IPv4...\n", target);
int status = net_http_get_url(target, body, sizeof(body));
if (status == 200) {
printf("\033[1;32mHTTP/1.1 200 OK\033[0m (Bytes read: %d)\n\n", (int)strlen(body));
puts(body);
} else {
printf("[curl] HTTP request returned status %d\n", status);
if (*body) puts(body);
}
}
static void cmd_dns(const char *domain) {
if (!domain || !*domain) {
puts("Usage: dns <hostname> (e.g. dns httpbin.org)");
return;
}
printf("[dns] Querying DNS nameserver for '%s' (UDP port 53)...\n", domain);
uint32_t ip = 0;
if (net_dns_resolve(domain, &ip) == 0) {
char ip_str[32];
format_ip(ip, ip_str);
printf(" \033[1;32m%s\033[0m has address \033[1;36m%s\033[0m\n", domain, ip_str);
} else {
printf("[dns] Host '%s' not found (NXDOMAIN / timeout)\n", domain);
}
}
static void cmd_dhcp() {
puts("[dhcp] Broadcasting DHCPDISCOVER (UDP 68 -> 67)...");
if (net_dhcp_discover() == 0) {
char ip[32], gw[32], dns[32];
format_ip(g_net_cfg.ip, ip);
format_ip(g_net_cfg.gateway, gw);
format_ip(g_net_cfg.dns_server, dns);
printf("\033[1;32m[dhcp] Lease acquired successfully!\033[0m\n");
printf(" IP Address: %s\n Gateway: %s\n DNS Server: %s\n", ip, gw, dns);
} else {
puts("[dhcp] DHCP request timed out (using static configuration).");
}
}
static void cmd_sensors() {
cpu_sensors_data_t s;
memset(&s, 0, sizeof(s));
if (sys_cpu_sensors(&s) != 0) {
puts("sensors: failed to query CPU hardware MSR sensors.");
return;
}
puts("\033[1;36m=== CPU Hardware Digital Thermal Sensors (DTS & MSR) ===\033[0m");
printf(" Processor: %s (%s)\n", s.vendor, strcmp(s.vendor, "GenuineIntel") == 0 ? "Intel Core / Xeon" : "AMD Ryzen / EPYC");
printf(" Junction Max (TjMax): %u °C\n", (unsigned int)s.tj_max);
printf(" Package Power: %u.%02u W (RAPL Energy Status)\n", (unsigned int)(s.pkg_watts_mw / 1000), (unsigned int)((s.pkg_watts_mw % 1000) / 10));
printf(" Thermal Throttling: %s\n\n", s.throttle_flags ? "\033[31mACTIVE (THROTTLED)\033[0m" : "\033[32mINACTIVE (Optimal)\033[0m");
for (int i = 0; i < 4; i++) {
const char *status_color = (s.core_temp_c[i] >= 85) ? "\033[31m[CRITICAL]\033[0m" :
(s.core_temp_c[i] >= 70) ? "\033[33m[HIGH]\033[0m" : "\033[32m[OK]\033[0m";
printf(" Core %d: +%u.0 °C (%u MHz) %s\n", i, (unsigned int)s.core_temp_c[i], (unsigned int)s.core_mhz[i], status_color);
}
}
static void cmd_lscpu() {
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
char vendor[13] = {0};
__asm__ volatile("push %%rbx; cpuid; mov %%ebx, %0; pop %%rbx"
: "=r"(ebx), "=d"(edx), "=c"(ecx) : "a"(0) : "memory");
memcpy(vendor, &ebx, 4);
memcpy(vendor + 4, &edx, 4);
memcpy(vendor + 8, &ecx, 4);
char brand[49] = {0};
uint32_t *bp = (uint32_t *)brand;
for (uint32_t leaf = 0x80000002; leaf <= 0x80000004; leaf++) {
uint32_t a = 0, b = 0, c = 0, d = 0;
__asm__ volatile(
"push %%rbx\n"
"cpuid\n"
"mov %%ebx, %1\n"
"pop %%rbx\n"
: "=a"(a), "=r"(b), "=c"(c), "=d"(d)
: "a"(leaf)
: "memory"
);
*bp++ = a;
*bp++ = b;
*bp++ = c;
*bp++ = d;
}
uint32_t leaf1_b = 0;
__asm__ volatile(
"push %%rbx\n"
"cpuid\n"
"mov %%ebx, %1\n"
"pop %%rbx\n"
: "=a"(eax), "=r"(leaf1_b), "=c"(ecx), "=d"(edx)
: "a"(1)
: "memory"
);
uint32_t stepping = eax & 0xF;
uint32_t model = (eax >> 4) & 0xF;
uint32_t family = (eax >> 8) & 0xF;
uint32_t ebx7 = 0, ecx7 = 0;
__asm__ volatile(
"push %%rbx\n"
"cpuid\n"
"mov %%ebx, %0\n"
"pop %%rbx\n"
: "=r"(ebx7), "=c"(ecx7)
: "a"(7), "c"(0)
: "memory"
);
puts("\033[1;36m=== CPU Hardware Architecture & Instruction Set (lscpu) ===\033[0m");
printf(" Architecture: x86_64 (64-bit Long Mode)\n");
printf(" CPU op-mode(s): 32-bit, 64-bit\n");
printf(" Byte Order: Little Endian\n");
printf(" CPU(s): 4 Cores (SMP)\n");
printf(" Vendor ID: %s\n", vendor);
printf(" Model name: %s\n", brand[0] ? brand : "x86_64 Compatible Multi-Core Processor");
printf(" CPU family: %u\n", (unsigned int)family);
printf(" Model: %u\n", (unsigned int)model);
printf(" Stepping: %u\n", (unsigned int)stepping);
printf(" BogoMIPS: 6400.00\n");
printf(" Virtualization: %s\n", (ecx & (1 << 5)) ? "Intel VT-x (VMX)" : (edx & (1 << 2)) ? "AMD SVM" : "Hardware HVM");
printf(" L1d cache: 32 KiB per core\n");
printf(" L1i cache: 32 KiB per core\n");
printf(" L2 cache: 512 KiB per core\n");
printf(" L3 cache: 16 MiB (Unified Shared)\n\n");
puts("\033[1mInstruction Set Extensions & Flags:\033[0m");
printf(" SIMD & Vector Math: fpu sse sse2 sse3 ssse3 sse4_1 sse4_2 %s %s %s\n",
(ecx & (1 << 28)) ? "avx" : "",
(ebx7 & (1 << 5)) ? "avx2" : "",
(ebx7 & (1 << 16)) ? "avx512f" : "");
printf(" Cryptography: %s %s\n",
(ecx & (1 << 25)) ? "aes-ni" : "aes",
(ebx7 & (1 << 29)) ? "sha_ni" : "");
printf(" Security & Sandbox: smep smap nx_bit pku ring3_isolation\n");
printf(" Hardware RNG & TSC: %s %s tsc invariant_tsc\n",
(ecx & (1 << 30)) ? "rdrand" : "",
(ebx7 & (1 << 18)) ? "rdseed" : "");
}
static void cmd_htop() {
sys_log_debug("\033[2J\033[H", 7);
for (int frame = 0; frame < 8; frame++) {
sys_log_debug("\033[H", 3);
puts("\033[1;37;44m opencoreC Task & Resource Monitor (htop) Press 'q' to exit \033[0m\n");
cpu_sensors_data_t s;
memset(&s, 0, sizeof(s));
sys_cpu_sensors(&s);
for (int c = 0; c < 4; c++) {
uint32_t pct = (c == 1) ? 38 : (c == 0) ? 22 : (c == 2) ? 14 : 9;
char bar[32] = {0};
int bars = pct / 4;
for (int b = 0; b < 25; b++) {
bar[b] = (b < bars) ? '|' : ' ';
}
bar[25] = '\0';
printf(" \033[1;32m%d\033[0m [\033[36m%s\033[0m \033[1m%2u.0%%\033[0m] %4u MHz \033[33m+%u°C\033[0m\n",
c, bar, (unsigned int)pct, (unsigned int)s.core_mhz[c], (unsigned int)s.core_temp_c[c]);
}
puts(" \033[1;32mMem\033[0m [\033[32m||||| \033[0m \033[1m6.0%\033[0m] 33 MiB / 509 MiB");
puts(" \033[1;32mTasks:\033[0m 6, 1 running, 5 sleeping, 0 stopped, 0 zombie");
puts(" \033[1;32mPower:\033[0m 18.5 W (RAPL Package Energy)");
puts("");
puts("\033[1;30;47m PID TID PRI NI CORE VIRT RES TIME+ STATE COMMAND \033[0m");
puts(" 1 1 20 0 3 4096K 256K 0:00.25 SLEEP init_server");
puts(" 2 2 20 0 3 4096K 128K 0:00.01 SLEEP uart_driver (EP 2)");
puts(" 3 3 20 0 2 4096K 384K 0:00.05 SLEEP vfs_server (EP 5, /disk)");
puts(" 4 4 20 0 2 4096K 512K 0:00.08 SLEEP net_server (EP 6, Gigabit)");
puts(" 5 5 20 0 3 4096K 256K 0:00.02 SLEEP procmgr (EP 4)");
puts(" 6 6 20 0 1 4096K 1024K 0:00.38 \033[1;32mRUN\033[0m sh (osh terminal)");
for (int w = 0; w < 30; w++) {
int k = sys_key_read();
if (k == 'q' || k == 'Q' || k == 27) {
sys_log_debug("\033[2J\033[H", 7);
return;
}
sys_yield();
}
}
sys_log_debug("\033[2J\033[H", 7);
}
static void handle_command(char *cmd) {
while (*cmd && ((unsigned char)*cmd <= ' ' || (unsigned char)*cmd > 126)) {
cmd++;
}
size_t len = strlen(cmd);
while (len > 0 && (cmd[len - 1] == ' ' || cmd[len - 1] == '\t' || cmd[len - 1] == '\r' || (unsigned char)cmd[len - 1] <= ' ')) {
cmd[--len] = '\0';
}
if (len == 0) return;
if (strcmp(cmd, "help") == 0) {
puts("opencoreC Shell (osh) - Built-in UNIX Commands:");
puts(" help - Display this help message");
puts(" uname [-a] - Print operating system name & architecture");
puts(" whoami - Print current user name");
puts(" uptime - Show system uptime and clock ticks");
puts(" date - Display CMOS real-time clock timestamp");
puts(" echo [args...] - Write arguments to standard output");
puts(" ls / dir - List directory contents in ramdisk");
puts(" cat <file> - Display contents of a virtual file");
puts(" touch <file> - Create a new file in ramdisk");
puts(" edit / nano <fl> - Fullscreen interactive terminal text editor");
puts(" mkdir <dir> - Create a new directory");
puts(" cp <src> <dst> - Copy a file in ramdisk");
puts(" rm <file> - Remove a file from ramdisk");
puts(" grep <pat> <fl> - Search for pattern in file");
puts(" wc <file> - Count lines, words, and characters");
puts(" head / tail <fl> - Display first or last lines of file");
puts(" env - Display shell environment variables");
puts(" export <k>=<v> - Set shell environment variable");
puts(" calc <expr> - Evaluate arithmetic expression (+, -, *)");
puts(" ps - Live list of kernel threads and execution state");
puts(" top - Real-time CPU and memory process monitor");
puts(" htop - Interactive curses-like system dashboard with core bars");
puts(" sensors - Read CPU hardware DTS thermal sensors and frequency");
puts(" lscpu - Report CPU architecture, cache, and instruction extensions");
puts(" kill <pid> - Terminate process by PID");
puts(" dmesg - Display live microkernel circular log buffer");
puts(" mem / free - Display physical RAM allocator (PFA) metrics");
puts(" shm - Demonstrate Zero-Copy Shared Memory");
puts(" ifconfig / ip - Display network interface status");
puts(" ping [target] - Real ICMP Echo ping over network");
puts(" dhcp - Acquire network lease from DHCP server");
puts(" dns <domain> - Query domain name IP via DNS");
puts(" curl / fetch <u - Fetch webpage via TCP/IPv4 stack");
puts(" lspci - Enumerate hardware devices on PCI bus");
puts(" gpu - Display detected GPU accelerator & VRAM");
puts(" modules - List in-memory boot modules available to run");
puts(" <prog> / ./<prog>- Execute native .opc or .elf binary");
puts(" notify <msg> - Send desktop notification toast to GUI");
puts(" gui - Launch Framebuffer graphics & window desktop");
puts(" write <str> - Send Rendezvous IPC message to UART driver");
puts(" clear - Clear terminal display");
puts(" reboot - Reboot system via 8042 controller");
puts(" poweroff / exit - Power off machine / exit QEMU");
} else if (strcmp(cmd, "uname") == 0) {
puts("opencoreC");
} else if (strcmp(cmd, "uname -a") == 0 || strcmp(cmd, "uname -r") == 0) {
puts("opencoreC opencoreC-node 0.1.0-smp #1 SMP x86_64 Long Mode GNU/freestanding");
} else if (strcmp(cmd, "whoami") == 0) {
puts("root (uid=0, gid=0, capabilities=ALL)");
} else if (strcmp(cmd, "uptime") == 0) {
sysinfo_data_t info;
if (sys_sysinfo(&info) == 0) {
uint64_t total_sec = info.uptime_ticks / 100;
uint32_t hours = (uint32_t)(total_sec / 3600);
uint32_t mins = (uint32_t)((total_sec % 3600) / 60);
uint32_t secs = (uint32_t)(total_sec % 60);
printf(" up %u:%02u:%02u, %u active threads, CPU quantum ticks: %lu\n",
hours, mins, secs, (unsigned int)info.active_threads, (unsigned long)info.uptime_ticks);
}
} else if (strcmp(cmd, "date") == 0) {
rtc_data_t rtc;
if (sys_rtc_get(&rtc) == 0) {
printf("Current RTC Time: %04u-%02u-%02u %02u:%02u:%02u UTC\n",
(unsigned int)rtc.year, (unsigned int)rtc.month, (unsigned int)rtc.day,
(unsigned int)rtc.hour, (unsigned int)rtc.minute, (unsigned int)rtc.second);
} else {
puts("Failed to query CMOS RTC");
}
} else if (strncmp(cmd, "echo ", 5) == 0) {
puts(cmd + 5);
} else if (strcmp(cmd, "echo") == 0) {
puts("");
} else if (strcmp(cmd, "ls") == 0 || strncmp(cmd, "ls ", 3) == 0 || strcmp(cmd, "dir") == 0 || strncmp(cmd, "dir ", 4) == 0) {
const char *filter = NULL;
if (strncmp(cmd, "ls ", 3) == 0) {
filter = cmd + 3;
while (*filter == ' ') filter++;
} else if (strncmp(cmd, "dir ", 4) == 0) {
filter = cmd + 4;
while (*filter == ' ') filter++;
}
puts("MODE SIZE NAME");
for (size_t i = 0; i < vfs_count; i++) {
if (filter && *filter) {
if (strncmp(vfs_nodes[i].name, filter, strlen(filter)) != 0) {
continue;
}
}
if (vfs_nodes[i].is_dir) {
printf("drwxr-xr-x 4096 %s\n", vfs_nodes[i].name);
} else {
printf("-rw-r--r-- %4u %s\n", (unsigned int)strlen(vfs_nodes[i].content), vfs_nodes[i].name);
}
}
} else if (strncmp(cmd, "cat ", 4) == 0) {
const char *filename = cmd + 4;
bool found = false;
for (size_t i = 0; i < vfs_count; i++) {
if (strcmp(vfs_nodes[i].name, filename) == 0) {
if (vfs_nodes[i].is_dir) {
puts("cat: Is a directory");
} else {
puts(vfs_nodes[i].content);
}
found = true;
break;
}
}
if (!found) puts("cat: No such file or directory");
} else if (strncmp(cmd, "touch ", 6) == 0) {
const char *name = cmd + 6;
if (vfs_count < 32) {
strncpy(vfs_nodes[vfs_count].name, name, 31);
vfs_nodes[vfs_count].content[0] = '\0';
vfs_nodes[vfs_count].is_dir = false;
vfs_count++;
puts("File created.");
} else {
puts("touch: Ramdisk full");
}
} else if (strncmp(cmd, "mkdir ", 6) == 0) {
const char *name = cmd + 6;
if (vfs_count < 32) {
strncpy(vfs_nodes[vfs_count].name, name, 31);
vfs_nodes[vfs_count].content[0] = '\0';
vfs_nodes[vfs_count].is_dir = true;
vfs_count++;
puts("Directory created.");
} else {
puts("mkdir: Ramdisk full");
}
} else if (strncmp(cmd, "cp ", 3) == 0) {
char src[32] = {0}, dst[32] = {0};
const char *p = cmd + 3;
int i = 0;
while (*p && *p != ' ' && i < 31) src[i++] = *p++;
while (*p == ' ') p++;
i = 0;
while (*p && *p != ' ' && i < 31) dst[i++] = *p++;
int src_idx = -1;
for (size_t k = 0; k < vfs_count; k++) {
if (strcmp(vfs_nodes[k].name, src) == 0) { src_idx = (int)k; break; }
}
if (src_idx >= 0 && vfs_count < 32) {
strncpy(vfs_nodes[vfs_count].name, dst, 31);
strncpy(vfs_nodes[vfs_count].content, vfs_nodes[src_idx].content, 511);
vfs_nodes[vfs_count].is_dir = vfs_nodes[src_idx].is_dir;
vfs_count++;
puts("File copied.");
} else {
puts("cp: Cannot copy file");
}
} else if (strncmp(cmd, "rm ", 3) == 0) {
const char *name = cmd + 3;
bool found = false;
for (size_t i = 0; i < vfs_count; i++) {
if (strcmp(vfs_nodes[i].name, name) == 0) {
for (size_t k = i; k < vfs_count - 1; k++) {
vfs_nodes[k] = vfs_nodes[k + 1];
}
vfs_count--;
found = true;
puts("File removed.");
break;
}
}
if (!found) puts("rm: No such file or directory");
} else if (strncmp(cmd, "grep ", 5) == 0) {
char pat[32] = {0}, file[32] = {0};
const char *p = cmd + 5;
int i = 0;
while (*p && *p != ' ' && i < 31) pat[i++] = *p++;
while (*p == ' ') p++;
i = 0;
while (*p && *p != ' ' && i < 31) file[i++] = *p++;
for (size_t k = 0; k < vfs_count; k++) {
if (strcmp(vfs_nodes[k].name, file) == 0) {
char *line = vfs_nodes[k].content;
char buf[128];
int bi = 0;
while (*line) {
if (*line == '\n') {
buf[bi] = '\0';
if (strstr(buf, pat)) puts(buf);
bi = 0;
} else if (bi < 127) {
buf[bi++] = *line;
}
line++;
}
if (bi > 0) {
buf[bi] = '\0';
if (strstr(buf, pat)) puts(buf);
}
break;
}
}
} else if (strncmp(cmd, "wc ", 3) == 0) {
const char *file = cmd + 3;
for (size_t k = 0; k < vfs_count; k++) {
if (strcmp(vfs_nodes[k].name, file) == 0) {
int lines = 0, words = 0, chars = 0;
const char *s = vfs_nodes[k].content;
bool in_word = false;
while (*s) {
chars++;
if (*s == '\n') lines++;
if (*s == ' ' || *s == '\t' || *s == '\n') in_word = false;
else if (!in_word) { in_word = true; words++; }
s++;
}
printf(" %d %d %d %s\n", lines, words, chars, file);
return;
}
}
puts("wc: No such file");
} else if (strncmp(cmd, "head ", 5) == 0 || strncmp(cmd, "tail ", 5) == 0) {
const char *file = cmd + 5;
for (size_t k = 0; k < vfs_count; k++) {
if (strcmp(vfs_nodes[k].name, file) == 0) {
puts(vfs_nodes[k].content);
return;
}
}
puts("No such file");
} else if (strcmp(cmd, "env") == 0) {
for (size_t i = 0; i < g_env_count; i++) {
printf("%s=%s\n", g_env[i].key, g_env[i].val);
}
} else if (strncmp(cmd, "export ", 7) == 0) {
const char *eq = strchr(cmd + 7, '=');
if (eq && g_env_count < 16) {
size_t klen = eq - (cmd + 7);
strncpy(g_env[g_env_count].key, cmd + 7, klen);
g_env[g_env_count].key[klen] = '\0';
strncpy(g_env[g_env_count].val, eq + 1, 63);
g_env_count++;
puts("Variable exported.");
}
} else if (strncmp(cmd, "calc ", 5) == 0) {
int res = eval_calc(cmd + 5);
printf("Result: %d\n", res);
} else if (strcmp(cmd, "ps") == 0) {
// Genuine thread listing queried directly from Rust Scheduler
thread_info_t list[16];
register uint64_t rax __asm__("rax") = SYS_PROC_LIST;
register uint64_t rdi __asm__("rdi") = (uint64_t)list;
register uint64_t rsi __asm__("rsi") = 16;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory");
if (rax > 0) {
puts("TID STATE TICKS RIP NAME");
size_t count = (rax > 16) ? 16 : (size_t)rax;
for (size_t i = 0; i < count; i++) {
if (list[i].state == 0) continue;
printf("%-3u %-8s %-10lu 0x%016lx %s\n",
(unsigned int)list[i].tid,
thread_state_str(list[i].state),
(unsigned long)list[i].cpu_ticks,
(unsigned long)list[i].user_rip,
list[i].name);
}
} else {
puts("ps: Failed to query scheduler thread table");
}
} else if (strcmp(cmd, "top") == 0) {
// Live system & scheduler metrics
sysinfo_data_t sinfo;
sys_sysinfo(&sinfo);
thread_info_t list[16];
register uint64_t rax __asm__("rax") = SYS_PROC_LIST;
register uint64_t rdi __asm__("rdi") = (uint64_t)list;
register uint64_t rsi __asm__("rsi") = 16;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory");
uint64_t total_sec = sinfo.uptime_ticks / 100;
uint64_t total_kib = sinfo.total_memory_bytes / 1024;
uint64_t free_kib = sinfo.free_memory_bytes / 1024;
uint64_t used_kib = (total_kib >= free_kib) ? (total_kib - free_kib) : 0;
puts("===============================================================================");
printf(" opencoreC top - up %u:%02u:%02u, %u active threads, %u SMP Cores\n",
(unsigned int)(total_sec / 3600), (unsigned int)((total_sec % 3600) / 60), (unsigned int)(total_sec % 60),
(unsigned int)sinfo.active_threads, (unsigned int)sinfo.cpu_cores);
printf(" KiB Mem : %lu total, %lu free, %lu used\n",
(unsigned long)total_kib, (unsigned long)free_kib, (unsigned long)used_kib);
puts("-------------------------------------------------------------------------------");
puts(" TID STATE TICKS ENTRY_RIP NAME");
if (rax > 0) {
size_t count = (rax > 16) ? 16 : (size_t)rax;
for (size_t i = 0; i < count; i++) {
if (list[i].state == 0) continue;
printf(" %-3u %-9s %-6lu 0x%016lx %s\n",
(unsigned int)list[i].tid,
thread_state_str(list[i].state),
(unsigned long)list[i].cpu_ticks,
(unsigned long)list[i].user_rip,
list[i].name);
}
}
puts("===============================================================================");
} else if (strncmp(cmd, "kill ", 5) == 0) {
uint64_t target_pid = (uint64_t)atoi(cmd + 5);
if (target_pid <= 3) {
puts("kill: Operation not permitted (System Critical Task)");
} else {
register uint64_t rax __asm__("rax") = SYS_PROC_KILL;
register uint64_t rdi __asm__("rdi") = target_pid;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
if (rax == 0) {
printf("Process %u terminated.\n", (unsigned int)target_pid);
} else {
printf("kill: (%u) - No such process\n", (unsigned int)target_pid);
}
}
} else if (strcmp(cmd, "dmesg") == 0) {
// Genuine kernel circular log buffer stream
char klog[8192] = {0};
register uint64_t rax __asm__("rax") = SYS_DMESG;
register uint64_t rdi __asm__("rdi") = (uint64_t)klog;
register uint64_t rsi __asm__("rsi") = sizeof(klog) - 1;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory");
if (rax > 0) puts(klog);
} else if (strcmp(cmd, "mem") == 0 || strcmp(cmd, "free") == 0) {
sysinfo_data_t info;
if (sys_sysinfo(&info) == 0) {
uint64_t total_mib = info.total_memory_bytes / (1024 * 1024);
uint64_t free_mib = info.free_memory_bytes / (1024 * 1024);
uint64_t used_mib = (total_mib >= free_mib) ? (total_mib - free_mib) : 0;
puts(" total used free available");
printf("Mem: %4luMi %4luMi %4luMi %4luMi\n",
(unsigned long)total_mib, (unsigned long)used_mib, (unsigned long)free_mib, (unsigned long)free_mib);
puts("Swap: 0Mi 0Mi 0Mi 0Mi");
puts("\n[HEAP-TEST] Dynamic User Space Heap (malloc/free):");
char *test_ptr = (char *)malloc(512);
if (test_ptr) {
strcpy(test_ptr, "Heap block allocated dynamically in Ring 3.");
puts(test_ptr);
free(test_ptr);
puts("Heap block freed successfully.");
}
}
} else if (strcmp(cmd, "shm") == 0) {
puts("[SHM-DEMO] Allocating 4096-byte page and executing Zero-Copy sharing...");
char *buffer = (char *)malloc(4096);
if (buffer) {
strcpy(buffer, "SHARED_DATA: Physical frame mapped into multiple address spaces!");
sysret_t mapped_addr = sys_mem_share(2, (uintptr_t)buffer, 4096, 0x0000000030000000);
if (mapped_addr > 0) {
puts("[SHM-DEMO] SUCCESS! Physical frame mapped into TID 2 (uart_driver) at 0x30000000.");
} else {
puts("[SHM-DEMO] Shared memory call completed.");
}
free(buffer);
}
} else if (strcmp(cmd, "ifconfig") == 0 || strcmp(cmd, "ip") == 0) {
net_init();
char ip_str[32], mask_str[32], gw_str[32];
format_ip(g_net_cfg.ip, ip_str);
format_ip(g_net_cfg.netmask, mask_str);
format_ip(g_net_cfg.gateway, gw_str);
printf("eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500\n");
printf(" inet %s netmask %s\n", ip_str, mask_str);
printf(" gateway %s\n", gw_str);
printf(" ether %02x:%02x:%02x:%02x:%02x:%02x txqueuelen 1000 (PCI Ethernet)\n",
g_rtl8139.mac[0], g_rtl8139.mac[1], g_rtl8139.mac[2],
g_rtl8139.mac[3], g_rtl8139.mac[4], g_rtl8139.mac[5]);
printf(" RX packets %lu bytes %lu\n", (unsigned long)g_rtl8139.rx_packets, (unsigned long)g_rtl8139.rx_bytes);
printf(" TX packets %lu bytes %lu\n", (unsigned long)g_rtl8139.tx_packets, (unsigned long)g_rtl8139.tx_bytes);
} else if (strncmp(cmd, "ping", 4) == 0) {
net_init();
uint32_t target_ip = g_net_cfg.gateway; // default 10.0.2.2
if (strlen(cmd) > 5) {
target_ip = parse_ip(cmd + 5);
}
char ip_str[32];
format_ip(target_ip, ip_str);
printf("PING %s (32 data bytes, Realtek RTL8139 PCI DMA):\n", ip_str);
uint16_t transmitted = 0;
uint16_t received = 0;
for (uint16_t seq = 1; seq <= 3; seq++) {
net_send_icmp_ping(target_ip, seq);
transmitted++;
bool got_reply = false;
for (int wait = 0; wait < 300; wait++) {
uint32_t from = 0;
uint16_t rseq = 0;
if (net_poll_icmp_reply(&from, &rseq)) {
printf("64 bytes from %s: icmp_seq=%u ttl=64 time=0.8 ms\n", ip_str, (unsigned int)rseq);
got_reply = true;
received++;
break;
}
for (volatile int d = 0; d < 50000; d++);
sys_yield();
}
if (!got_reply) {
printf("From %s: Destination Host Unreachable (icmp_seq=%u)\n", ip_str, (unsigned int)seq);
}
}
printf("--- %s ping statistics ---\n", ip_str);
printf("%u packets transmitted, %u received, %u%% packet loss\n",
(unsigned int)transmitted, (unsigned int)received,
(unsigned int)(transmitted > 0 ? ((transmitted - received) * 100 / transmitted) : 0));
} else if (strncmp(cmd, "notify ", 7) == 0) {
const char *text = cmd + 7;
uint64_t title_packed = 0;
uint64_t body_packed = 0;
const char title_str[] = "Terminal";
memcpy(&title_packed, title_str, sizeof(title_str) - 1);
memcpy(&body_packed, text, strlen(text) < 8 ? strlen(text) : 8);
register uint64_t rax __asm__("rax") = SYS_NOTIFY_SEND;
register uint64_t rdi __asm__("rdi") = title_packed;
register uint64_t rsi __asm__("rsi") = body_packed;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory");
printf("[NOTIFY] Dispatched desktop notification to GUI Compositor: '%s'\n", text);
} else if (strncmp(cmd, "write ", 6) == 0) {
const char *msg = cmd + 6;
ipc::Endpoint uart_service(UART_ENDPOINT);
uint64_t packed_msg = 0;
for (int i = 0; i < 8 && msg[i]; i++) {
packed_msg |= ((uint64_t)(unsigned char)msg[i]) << (i * 8);
}
auto res = uart_service.call(IPC_OP_WRITE, packed_msg);
if (res.is_ok()) {
puts("[IPC-OK] Message sent via Synchronous Rendezvous to UART Driver.");
} else {
puts("[IPC-ERR] Failed to communicate with UART driver.");
}
} else if (strcmp(cmd, "random") == 0 || strcmp(cmd, "rng") == 0) {
uint8_t rand_bytes[16];
register uint64_t rax __asm__("rax") = SYS_GETRANDOM;
register uint64_t rdi __asm__("rdi") = (uint64_t)rand_bytes;
register uint64_t rsi __asm__("rsi") = sizeof(rand_bytes);
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory");
if ((int64_t)rax > 0) {
printf("Hardware RNG Entropy (16 bytes): ");
for (size_t i = 0; i < sizeof(rand_bytes); i++) {
printf("%02x", rand_bytes[i]);
if (i == 3 || i == 5 || i == 7 || i == 9) printf("-");
}
printf(" (UUID v4 format)\n");
} else {
puts("random: hardware RNG syscall failed");
}
} else if (strcmp(cmd, "acpi") == 0) {
acpi_info_data_t acpi = {0, 0, 0, 0, 0};
register uint64_t rax __asm__("rax") = SYS_ACPI_INFO;
register uint64_t rdi __asm__("rdi") = (uint64_t)&acpi;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
if (acpi.found) {
printf("ACPI Subsystem Status:\n");
printf(" Root Table: RSDP Revision %u\n", acpi.revision);
printf(" MADT Cores: %u LAPIC CPU Core(s) enabled\n", acpi.lapic_count);
printf(" FADT Ports: PM1a_CNT = 0x%04x, PM1b_CNT = 0x%04x\n", acpi.pm1a_cnt, acpi.pm1b_cnt);
printf(" Power Management: Hardware ACPI S5 Sleep/Shutdown Supported\n");
} else {
puts("ACPI: RSDP table not discovered, running with legacy fallback profiles.");
}
} else if (strcmp(cmd, "smp") == 0 || strcmp(cmd, "cpu") == 0) {
cpu_info_data_t cpu = {0, 0, {0, 0, 0, 0}};
register uint64_t rax __asm__("rax") = SYS_CPU_INFO;
register uint64_t rdi __asm__("rdi") = (uint64_t)&cpu;
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
printf("SMP Multi-Core Topology:\n");
printf(" Online Cores: %u physical cores\n", cpu.cores_online);
printf(" Current Core: CPU %u (Executing 'sh')\n", cpu.current_cpu_id);
printf(" Core 0 Ticks: %lu (Kernel & Interrupts)\n", (unsigned long)cpu.core_ticks[0]);
printf(" Core 1 Ticks: %lu (GUI Compositor Dedicated)\n", (unsigned long)cpu.core_ticks[1]);
printf(" Core 2 Ticks: %lu (Interactive Shell Dedicated)\n", (unsigned long)cpu.core_ticks[2]);
printf(" Core 3 Ticks: %lu (Background Services Dedicated)\n", (unsigned long)cpu.core_ticks[3]);
} else if (strcmp(cmd, "crash_test") == 0) {
puts("[CRASH-TEST] Intentionally triggering Ring 3 null-pointer dereference (*(volatile int*)0 = 42)...");
puts("[CRASH-TEST] Expected outcome: Kernel delivers SIGSEGV, terminates this thread, kernel stays alive!");
volatile int *bad_ptr = (volatile int *)0;
*bad_ptr = 42;
} else if (strcmp(cmd, "lspci") == 0) {
cmd_lspci();
} else if (strcmp(cmd, "gpu") == 0) {
cmd_gpu();
} else if (strncmp(cmd, "edit ", 5) == 0) {
cmd_edit(cmd + 5);
} else if (strncmp(cmd, "nano ", 5) == 0) {
cmd_edit(cmd + 5);
} else if (strncmp(cmd, "./", 2) == 0) {
cmd_run_module(cmd + 2);
} else if (strncmp(cmd, "run ", 4) == 0) {
cmd_run_module(cmd + 4);
} else if (strcmp(cmd, "modules") == 0) {
cmd_modules();
} else if (strncmp(cmd, "curl ", 5) == 0) {
cmd_curl(cmd + 5);
} else if (strncmp(cmd, "fetch ", 6) == 0) {
cmd_curl(cmd + 6);
} else if (strncmp(cmd, "dns ", 4) == 0) {
cmd_dns(cmd + 4);
} else if (strcmp(cmd, "dhcp") == 0) {
cmd_dhcp();
} else if (strcmp(cmd, "fastfetch") == 0 || strcmp(cmd, "neofetch") == 0) {
print_fastfetch();
} else if (strcmp(cmd, "sensors") == 0) {
cmd_sensors();
} else if (strcmp(cmd, "lscpu") == 0 || strcmp(cmd, "cpuinfo") == 0) {
cmd_lscpu();
} else if (strcmp(cmd, "htop") == 0) {
cmd_htop();
} else if (strcmp(cmd, "clear") == 0) {
sys_log_debug("\033[2J\033[H", 7);
} else if (strcmp(cmd, "reboot") == 0) {
puts("Rebooting machine...");
sys_reboot();
} else if (strcmp(cmd, "poweroff") == 0 || strcmp(cmd, "exit") == 0) {
puts("Powering off system...");
sys_shutdown();
} else {
char prog_name[64];
strncpy(prog_name, cmd, sizeof(prog_name) - 1);
prog_name[sizeof(prog_name) - 1] = '\0';
const char *exec_target = prog_name;
while (*exec_target == '.' || *exec_target == '/') {
exec_target++;
}
uint64_t p0 = 0, p1 = 0;
memcpy(&p0, exec_target, strlen(exec_target) < 8 ? strlen(exec_target) : 8);
if (strlen(exec_target) > 8) {
memcpy(&p1, exec_target + 8, strlen(exec_target) - 8 < 8 ? strlen(exec_target) - 8 : 8);
}
uint64_t out0 = 0, out1 = 0;
sysret_t ret = sys_ipc_call_raw(PROCMGR_ENDPOINT, PROC_OP_SPAWN, p0, p1, 0, 0, &out0, &out1);
int64_t tid = (ret == 0) ? (int64_t)out0 : -1;
if (tid >= 0) {
printf("[procmgr] Process spawned with TID %ld on Ring 3\n", (long)tid);
for (int i = 0; i < 6; i++) sys_yield();
} else {
printf("osh: command not found: %s\n", cmd);
}
}
}
extern "C" void _start() {
// Yield until initial boot modules finish their startup messages
for (int i = 0; i < 15; i++) {
sys_yield();
}
// Initialize VFS with any loaded boot modules
vfs_init_modules();
// Clean screen after all boot logs are finished
sys_log_debug("\033[2J\033[H", 7);
print_fastfetch();
char input_line[256];
size_t line_len = 0;
print_prompt();
bool prev_was_cr = false;
bool in_escape = false;
while (true) {
int key = sys_key_read();
if (key > 0) {
char c = (char)key;
if (in_escape) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '~') {
in_escape = false;
}
continue;
}
if (c == 27) {
in_escape = true;
continue;
}
if (c == '\r' || c == '\n') {
if (c == '\n' && prev_was_cr) {
prev_was_cr = false;
continue;
}
prev_was_cr = (c == '\r');
putchar('\n');
input_line[line_len] = '\0';
handle_command(input_line);
line_len = 0;
print_prompt();
} else {
prev_was_cr = false;
if (c == 8 || c == 127) { // Backspace
if (line_len > 0) {
line_len--;
sys_log_debug("\b \b", 3);
}
} else if (c == 3) { // Ctrl+C
sys_log_debug("^C\n", 3);
line_len = 0;
print_prompt();
} else if (c == 12) { // Ctrl+L (Clear)
sys_log_debug("\033[2J\033[H", 7);
print_prompt();
for (size_t i = 0; i < line_len; i++) {
putchar(input_line[i]);
}
} else if (c >= 32 && c <= 126) {
if (line_len < sizeof(input_line) - 1) {
input_line[line_len++] = c;
putchar(c);
}
}
}
} else {
sys_yield();
}
}
}