commit 926b74e13774d63362001d959107557f48d3b217 from: Onana Onana Xavier Manuel date: Sun Sep 20 05:19:40 2026 UTC Formatted shared buffer message passing. xhci can now receive data from alpha. pci_init changed for pci_get_xhci. commit - 8d075ecc3061fc5fe32bba25b5d88b4b5a6d1500 commit + 926b74e13774d63362001d959107557f48d3b217 blob - ec51940dd81dada2c03e09336da14d9ff0129520 blob + 2bbc57d927ca294e1fc848cfc9fd79557d66fc1c --- alpha/build.sh +++ alpha/build.sh @@ -4,7 +4,7 @@ set -e echo "alpha: building" -INCS='-I ../common -I ../ -I ../kernel' +INCS='-I ../common -I ../ -I ../kernel -I ../servers/xhci' . ../include blob - 2de5594659a2333bdff30deac6500e8780a602e7 blob + 743f001547a4a48c278d8b2b07631c101f8bce3e --- alpha/libc.c +++ alpha/libc.c @@ -1,4 +1,5 @@ #include "types.h" +#include "libc.h" #include "syscalls/syscalls.h" void (*sys_send)(struct sysc *call) = (void (*)(struct sysc *))SYSC_BELL; @@ -68,3 +69,11 @@ seat(u64 threadid) sys_send(&sysc); return sysc.seat.ret_block; } + +u64 +speak(u8 *block) +{ + struct shared_buffer *sbuf = (struct shared_buffer *)block; + sbuf->busy = 1; // Make sure you check this before speaking + return 0; +} blob - 84979facb56c87769474f78b410905a831d50d5b blob + 6ec7612e28e05baf4e63f97466c6ee8a6eff79c7 --- alpha/libc.h +++ alpha/libc.h @@ -4,6 +4,11 @@ #define MAX_SHARED_BUFFERS 24 +struct shared_buffer { + u8 busy; + u8 data[4095]; +}; + // Resources given to a thread on initialization. Dynamic memory allocation // does not exist hence don't expect malloc/calloc/realloc/free/etc. The // kernel hands your memory and you're on your own. @@ -37,6 +42,6 @@ u8 *seat(u64 thread_id); void getup(u64 seat_id); -u64 speak(u8 *msg); +u64 speak(u8 *block); u8 *listen(u8 *block); // Tells the OS to open the seat for a sender. blob - d4a5bc809b1263c230055cff194d0832934ecd6b blob + b05e33ba0744374aa3a5113154b4b8ca18435939 --- alpha/main.c +++ alpha/main.c @@ -3,6 +3,7 @@ #include "pci.h" #include "libc.h" +#include "xhci.h" #include "syscalls/syscalls.h" #define MAX_TASKS 64 @@ -23,10 +24,17 @@ static struct task* task_new(void *); void alpha_main(struct mmap *km) { + u64 xhci_bar = pci_get_xhci(); + struct shared_buffer *sbuf; + struct xhcip *xhcip; struct task *t; u8 *block = nil; - pci_init(); + if (xhci_bar == 0) { + output("Could not find xhci controller\n"); + repeat: goto repeat; + } + t = task_new((void *)XHCI_ENTRY); if (t == nil) @@ -34,9 +42,16 @@ alpha_main(struct mmap *km) output("alpha: running\n"); while ((block = seat(t->thread)) == nil); - output("alpha: found a seat\n"); - block[0] = 0x55; + sbuf = (struct shared_buffer *)block; + while (sbuf->busy == 1) + yield(); + + xhcip = (struct xhcip *)sbuf->data; + xhcip->address = xhci_bar; + output("alpha: sending data to xhci server...\n"); + speak(block); + for (;;); } blob - 7cab7c0456c075991d302f39dce4edb1c081dd82 blob + 64c57c6a92c04bd599dabb45c4b1a56b67f17af3 --- alpha/pci.c +++ alpha/pci.c @@ -152,8 +152,8 @@ getClass(u8 class, u8 subclass, u8 progif) } } -void -pci_init() +u64 +pci_get_xhci() { u8 hdr; u8 class; @@ -162,8 +162,8 @@ pci_init() u8 subclass; u16 command; u32 bars[6]; - u32 dev32_addr; - u64 dev64_addr; + u32 dev32_addr = 0; + u64 dev64_addr = 0; u8 done = 0; output("Initializing PCIs\n"); @@ -211,18 +211,10 @@ pci_init() if ((bars[0] & (1 << 0)) == 0) { u8 type = (bars[0] & (3 << 1)) >> 1; if (type == 0x0) { - output("32-bit Memory Space Bar Layout "); dev32_addr = (u32)(bars[0] & 0xfffffff0); - outputh(dev32_addr); - output("\n"); - } else if (type == 0x2) { - output("64-bit Memory Space Bar Layout "); dev64_addr = (u64)(bars[0] & 0xfffffff0) + ((u64)(bars[1] & 0xffffffff) << 32); - outputh(dev64_addr); - output("\n"); - } else if (type == 0x1) { output("PCI 3.0 reserved\n"); } @@ -231,15 +223,12 @@ pci_init() command |= (1 << 0); command |= (1 << 1); pciConfigWriteWord(bus, device, func, 0x04, command); - volatile u32 *xhci_base = (volatile u32 *)dev64_addr; - u32 cap_reg = xhci_base[0]; - u8 cap_len = cap_reg & 0xff; - u16 hci_version = (cap_reg >> 16) & 0xffff; - output("xhci version: "); outputh(hci_version); output("\n"); + if (dev64_addr) return dev64_addr; } } } } } + return 0; } blob - c723553e038a68be543c67d2412ac8a9596e6e66 blob + 3abf2bb11c0f6a870ccb94496264c71d9e31842d --- alpha/pci.h +++ alpha/pci.h @@ -1,3 +1,3 @@ #pragma once -void pci_init(void); +u64 pci_get_xhci(void); blob - 42b61e061e1a2e50515816aba016e6ecc8b18f25 blob + 3eeaa91f19f8ac39dbfa6db0de275a95511e28a2 --- kernel/idt.c +++ kernel/idt.c @@ -23,6 +23,7 @@ struct idt_ptr { static struct idt_desc desc[256]; +void division_exception_handler(void); void debug_exception_handler(void); void page_fault_handler(void); void double_fault_handler(void); @@ -39,6 +40,15 @@ idt_init() .base = (u64)&desc }; + // DIVISION BY ZERO + desc[0].offset_1 = (u16)((u64)(division_exception_handler) & 0xffff); + desc[0].selector = 0x08; + desc[0].ist = 0; + desc[0].type_attributes = 0x8E; + desc[0].offset_2 = (u16)(((u64)(division_exception_handler) & 0xffff0000) >> 16); + desc[0].offset_3 = (u32)(((u64)(division_exception_handler) >> 32)); + desc[0].zero = 0; + // DEBUG EXCEPTION desc[1].offset_1 = (u16)((u64)(debug_exception_handler) & 0xffff); desc[1].selector = 0x08; @@ -89,6 +99,12 @@ idt_init() return 0; } +void +division_exception_handler_c() +{ + kprint("#DE interrupt\n"); +} + void debug_exception_handler_c() { blob - 9a2536142cad0000aaa41b994eea2d2b3c4328b7 blob + 05252703e50ce55398b381b8e9aeee73044928d6 --- kernel/lidt.s +++ kernel/lidt.s @@ -3,6 +3,9 @@ global load_idt extern cthread +global division_exception_handler +extern division_exception_handler_c + global debug_exception_handler extern debug_exception_handler_c @@ -22,6 +25,10 @@ load_idt: lidt [rdi] ret +division_exception_handler: + call division_exception_handler_c + hlt + debug_exception_handler: call debug_exception_handler_c hlt blob - 1aee040fdee868f814c9300a4f6431f44d9c08ad blob + 86d510403b44632a759e8867d584a4c73594f824 --- kernel/syscalls.c +++ kernel/syscalls.c @@ -12,7 +12,6 @@ struct seatmap { s64 send; // Sender Thread s64 recv; // Receiver Thread u8 *buff; - u8 open; }; static struct seatmap seatmaps[MAX_SEATS]; @@ -29,9 +28,10 @@ seat_new(u64 threadid) seatmaps[i].recv = threadid; // Zero'd - for (int j = 0; j < PAGE_SIZE; j++) + for (int j = 1; j < PAGE_SIZE; j++) seatmaps[i].buff[j] = 0; + seatmaps[i].buff[0] = 1; // Everything is busy by default until the recv opens it kprint("Shared block mapped to thread(id="); kprinti(threadid); kprint(")\n"); return 0; } @@ -115,7 +115,8 @@ sysc_listen_h(struct sysc_listen *listen) { for (int i = 0; i < MAX_SEATS; i++) { if (listen->shared_block == seatmaps[i].buff) { - seatmaps[i].open = 1; + struct shared_buffer *sbuf = (struct shared_buffer *)seatmaps[i].buff; + sbuf->busy = 0; return; } } @@ -125,7 +126,9 @@ void sysc_seat_h(struct sysc_seat *sysc_seat) { for (int i = 0; i < MAX_SEATS; i++) { - if (sysc_seat->threadid == seatmaps[i].recv && seatmaps[i].open == 1) { + struct shared_buffer *sbuf = (struct shared_buffer *)seatmaps[i].buff; + + if (sysc_seat->threadid == seatmaps[i].recv && sbuf->busy == 0) { sysc_seat->ret_block = seatmaps[i].buff; seatmaps[i].send = thread_getuid(cthread); break; @@ -134,6 +137,11 @@ sysc_seat_h(struct sysc_seat *sysc_seat) } void +sysc_speak_h(struct sysc_speak *sysc_speak) +{ +} + +void syscall_handler(struct sysc *call) { switch (call->type) { @@ -143,6 +151,8 @@ syscall_handler(struct sysc *call) case SYSC_NEW_THREAD: syscall_new_thread_h(call); break; + case SYSC_FREE_THREAD: + break; case SYSC_YIELD: cthread->state = THREAD_YIELDING; while (cthread->state != THREAD_RUNNING); @@ -153,7 +163,8 @@ syscall_handler(struct sysc *call) case SYSC_SEAT: sysc_seat_h(&call->seat); break; - default: + case SYSC_SPEAK: + sysc_speak_h(&call->speak); break; } } @@ -164,11 +175,16 @@ syscall_check_seats() { for (int i = 0; i < MAX_SEATS; i++) { struct seatmap *map = &seatmaps[i]; + struct shared_buffer *sbuf = (struct shared_buffer *)map->buff; - if (map->open == 1 && map->recv != THREADID_NONE && map->send != THREADID_NONE) { - struct thread *th = thread_get_uthread(map->recv); - - if (map->buff[0] != 0) th->state = THREAD_RUNNING; + if (map->recv != THREADID_NONE && map->send != THREADID_NONE) { + if (sbuf->busy == 1) { + struct thread *recv = thread_get_uthread(map->recv); + recv->state = THREAD_RUNNING; + } else { + struct thread *send = thread_get_uthread(map->send); + send->state = THREAD_RUNNING; + } } } } blob - 9b5e3be626986c9a9b0d22f5a4d9aaeaebfb8046 blob + b89597ddddc296a72309b7725c2e1e4c0e65861d --- servers/xhci/main.c +++ servers/xhci/main.c @@ -4,18 +4,30 @@ #include "types.h" #include "libc.h" +#include "xhci.h" void xhci_main(struct contract *con) { + struct shared_buffer *sbuf; + struct xhcip *xhcip; + u64 xhcibar; + output("xhci: running\n"); - output("xhci: listening\n"); listen(con->sharedBlocks[0]); yield(); - output("xhci: listen stop\n"); - output("xhci: first byte of shared block is "); outputh(con->sharedBlocks[0][0]); output("\n"); + sbuf = (struct shared_buffer *)con->sharedBlocks[0]; + xhcip = (struct xhcip *)sbuf->data; + output("xhci: bar address is "); outputh(xhcip->address); output("\n"); + + volatile u32 *xhci_base = (volatile u32 *)xhcip->address; + u32 cap_reg = xhci_base[0]; + u8 cap_len = cap_reg & 0xff; + u16 hci_version = (cap_reg >> 16) & 0xffff; + output("xhci version: "); outputh(hci_version); output("\n"); + for (;;); } blob - /dev/null blob + 4071476e279cf5feab080ced1d47a220a15a2cf8 (mode 644) --- /dev/null +++ servers/xhci/xhci.h @@ -0,0 +1,6 @@ +#pragma once + +struct xhcip { + u64 address; + u8 pad[4087]; +}; blob - e8ba6f6be4b57f7df4b5a9b831a5e2d68e19acd0 blob + af842fff59a0a4ba6a83c62e5e1790dcf79a7826 --- syscalls/syscalls.h +++ syscalls/syscalls.h @@ -6,6 +6,7 @@ enum sysc_type { SYSC_ECHO, SYSC_SEAT, + SYSC_SPEAK, SYSC_YIELD, SYSC_LISTEN, SYSC_NEW_THREAD, @@ -45,6 +46,12 @@ struct sysc_seat { u8 *ret_block; }; +struct sysc_speak { + u8* shared_block; + u8 msg[4095]; + u64 len; +}; + struct sysc { enum sysc_type type; union { @@ -52,6 +59,7 @@ struct sysc { struct sysc_new_thread new_thread; struct sysc_listen listen; struct sysc_seat seat; + struct sysc_speak speak; }; };