commit - 8d075ecc3061fc5fe32bba25b5d88b4b5a6d1500
commit + 926b74e13774d63362001d959107557f48d3b217
blob - ec51940dd81dada2c03e09336da14d9ff0129520
blob + 2bbc57d927ca294e1fc848cfc9fd79557d66fc1c
--- alpha/build.sh
+++ alpha/build.sh
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
#include "types.h"
+#include "libc.h"
#include "syscalls/syscalls.h"
void (*sys_send)(struct sysc *call) = (void (*)(struct sysc *))SYSC_BELL;
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
#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.
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
#include "pci.h"
#include "libc.h"
+#include "xhci.h"
#include "syscalls/syscalls.h"
#define MAX_TASKS 64
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)
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
}
}
-void
-pci_init()
+u64
+pci_get_xhci()
{
u8 hdr;
u8 class;
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");
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");
}
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
#pragma once
-void pci_init(void);
+u64 pci_get_xhci(void);
blob - 42b61e061e1a2e50515816aba016e6ecc8b18f25
blob + 3eeaa91f19f8ac39dbfa6db0de275a95511e28a2
--- kernel/idt.c
+++ kernel/idt.c
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);
.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;
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
global load_idt
extern cthread
+global division_exception_handler
+extern division_exception_handler_c
+
global debug_exception_handler
extern debug_exception_handler_c
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
s64 send; // Sender Thread
s64 recv; // Receiver Thread
u8 *buff;
- u8 open;
};
static struct seatmap seatmaps[MAX_SEATS];
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;
}
{
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;
}
}
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;
}
void
+sysc_speak_h(struct sysc_speak *sysc_speak)
+{
+}
+
+void
syscall_handler(struct sysc *call)
{
switch (call->type) {
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);
case SYSC_SEAT:
sysc_seat_h(&call->seat);
break;
- default:
+ case SYSC_SPEAK:
+ sysc_speak_h(&call->speak);
break;
}
}
{
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
#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
+#pragma once
+
+struct xhcip {
+ u64 address;
+ u8 pad[4087];
+};
blob - e8ba6f6be4b57f7df4b5a9b831a5e2d68e19acd0
blob + af842fff59a0a4ba6a83c62e5e1790dcf79a7826
--- syscalls/syscalls.h
+++ syscalls/syscalls.h
enum sysc_type {
SYSC_ECHO,
SYSC_SEAT,
+ SYSC_SPEAK,
SYSC_YIELD,
SYSC_LISTEN,
SYSC_NEW_THREAD,
u8 *ret_block;
};
+struct sysc_speak {
+ u8* shared_block;
+ u8 msg[4095];
+ u64 len;
+};
+
struct sysc {
enum sysc_type type;
union {
struct sysc_new_thread new_thread;
struct sysc_listen listen;
struct sysc_seat seat;
+ struct sysc_speak speak;
};
};