Compare commits

..

11 Commits

30 changed files with 655 additions and 256 deletions

View File

@ -1,3 +1,4 @@
mkalias pctl base:/bin/pctl mkalias pctl base:/bin/pctl
mkalias tb base:/bin/tb mkalias tb base:/bin/tb
mkalias fs base:/bin/fs mkalias fs base:/bin/fs
mkalias dev base:/bin/dev

View File

@ -58,8 +58,6 @@ SRCFILES += $(call GRABSRC, \
path \ path \
rbuf \ rbuf \
ipc/pipe \ ipc/pipe \
drivers/ps2kb \
drivers/serial \
dev \ dev \
) )

View File

@ -11,7 +11,7 @@ CFLAGS += -m64 \
-mno-red-zone \ -mno-red-zone \
-fno-stack-protector \ -fno-stack-protector \
-fno-stack-check \ -fno-stack-check \
-Os \ -O0 \
LDFLAGS += -m elf_x86_64 \ LDFLAGS += -m elf_x86_64 \
-pie \ -pie \

View File

@ -7,6 +7,7 @@
#include "termdev.h" #include "termdev.h"
#include "ps2kbdev.h" #include "ps2kbdev.h"
#include "serialdev.h" #include "serialdev.h"
#include "fbdev.h"
DevTable DEVTABLE; DevTable DEVTABLE;
@ -17,4 +18,5 @@ void dev_init(void) {
termdev_init(); termdev_init();
ps2kbdev_init(); ps2kbdev_init();
serialdev_init(); serialdev_init();
fbdev_init();
} }

29
kernel/dev/fbdev.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdint.h>
#include <stddef.h>
#include "fbdev.h"
#include "dev.h"
#include "sysdefs/devctl.h"
#include "hshtb.h"
#include "spinlock/spinlock.h"
#include "util/util.h"
#include "hal/hal.h"
#include "bootinfo/bootinfo.h"
#include "errors.h"
int32_t fbdev_getinfo(uint8_t *buffer, size_t len, void *extra) {
FbDevGetInfo info = {
.w = BOOT_INFO.fb->width,
.h = BOOT_INFO.fb->height,
.margin = 20,
.fontw = 8,
.fonth = 16,
};
hal_memcpy(buffer, &info, sizeof(info));
return E_OK;
}
void fbdev_init(void) {
Dev *fbdev;
HSHTB_ALLOC(DEVTABLE.devs, ident, "fbdev", fbdev);
fbdev->fns[DEV_FBDEV_GETINFO] = &fbdev_getinfo;
}

7
kernel/dev/fbdev.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef DEV_FBDEV_H_
#define DEV_FBDEV_H_
int32_t fbdev_getinfo(uint8_t *buffer, size_t len, void *extra);
void fbdev_init(void);
#endif // DEV_FBDEV_H_

View File

@ -9,15 +9,188 @@
#include "util/util.h" #include "util/util.h"
#include "hshtb.h" #include "hshtb.h"
#include "sysdefs/devctl.h" #include "sysdefs/devctl.h"
#include "proc/proc.h"
Ps2KbFastBuf PS2KB_BUF; #define KB_CTL_STATUS 0x64
#define KB_DATA_IN_BUF 0x01
#define KB_DATA 0x60
#define KB_SHIFT (1<<0)
#define KB_CTL (1<<1)
#define KB_ALT (1<<2)
#define KB_CAPSLOCK (1<<3)
#define KB_NUMLOCK (1<<4)
#define KB_SCRLLOCK (1<<5)
#define KB_E0ESC (1<<6)
#define KB_HOME 0xe0
#define KB_END 0xe1
#define KB_UP 0xe2
#define KB_DOWN 0xe3
#define KB_LEFT 0xe4
#define KB_RIGHT 0xe5
#define KB_PAGEUP 0xe6
#define KB_PAGEDN 0xe7
#define KB_INSERT 0xe8
#define KB_DELETE 0xe9
#define C(x) ((x)-'@')
static uint8_t shiftcode[0x100] = {
[0x1d] KB_CTL,
[0x2a] KB_SHIFT,
[0x36] KB_SHIFT,
[0x38] KB_ALT,
[0x9d] KB_CTL,
[0xb8] KB_ALT,
};
static uint8_t togglecode[0x100] = {
[0x3a] KB_CAPSLOCK,
[0x45] KB_NUMLOCK,
[0x46] KB_SCRLLOCK,
};
static uint8_t normalmap[0x100] = {
0x0, 0x1b, '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '\b', '\t',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
'o', 'p', '[', ']', '\n', 0x0, 'a', 's',
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
'\'', '`', 0x0, '\\', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/', 0x0, '*',
0x0, ' ', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '7',
'8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.', 0x0, 0x0, 0x0, 0x0,
[0x9c] '\n',
[0xb5] '/',
[0xc8] KB_UP, [0xd0] KB_DOWN,
[0xc9] KB_PAGEUP, [0xd1] KB_PAGEDN,
[0xcb] KB_LEFT, [0xcd] KB_RIGHT,
[0x97] KB_HOME, [0xcf] KB_END,
[0xd2] KB_INSERT, [0xd3] KB_DELETE,
[0xc7] KB_HOME,
};
static uint8_t shiftmap[256] = {
0x0, 033,'!', '@', '#', '$', '%', '^',
'&', '*', '(', ')', '_', '+', '\b', '\t',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
'O', 'P', '{', '}', '\n', 0x0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
'"', '~', 0x0, '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', 0x0, '*',
0x0, ' ', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '7',
'8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.', 0x0, 0x0, 0x0, 0x0,
[0x9C] '\n',
[0xB5] '/',
[0xc8] KB_UP, [0xd0] KB_DOWN,
[0xc9] KB_PAGEUP, [0xd1] KB_PAGEDN,
[0xcb] KB_LEFT, [0xcd] KB_RIGHT,
[0x97] KB_HOME, [0xcf] KB_END,
[0xd2] KB_INSERT, [0xd3] KB_DELETE,
[0xc7] KB_HOME,
};
static uint8_t ctlmap[256] =
{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
C('O'), C('P'), 0x0, 0x0, '\r', 0x0, C('A'), C('S'),
C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), 0x0,
0x0, 0x0, 0x0, C('\\'), C('Z'), C('X'), C('C'), C('V'),
C('B'), C('N'), C('M'), 0x0, 0x0, C('/'), 0x0, 0x0,
[0x9C] '\r',
[0xB5] C('/'),
[0xc8] KB_UP, [0xd0] KB_DOWN,
[0xc9] KB_PAGEUP, [0xd1] KB_PAGEDN,
[0xcb] KB_LEFT, [0xcd] KB_RIGHT,
[0x97] KB_HOME, [0xcf] KB_END,
[0xd2] KB_INSERT, [0xd3] KB_DELETE,
[0xc7] KB_HOME,
};
int32_t ps2kb_intr(void) {
static uint8_t shift;
static uint8_t *charcode[4] = { normalmap, shiftmap, ctlmap, ctlmap };
uint32_t st, data, c;
st = io_in8(KB_CTL_STATUS);
if (!(st & KB_DATA_IN_BUF)) {
return -1;
}
data = io_in8(KB_DATA);
if (data == 0xe0) {
shift |= KB_E0ESC;
return 0;
} else if (data & 0x80) {
data = (shift & KB_E0ESC ? data : data & 0x7F);
shift &= ~(shiftcode[data] | KB_E0ESC);
return 0;
} else if (shift & KB_E0ESC) {
data |= 0x80;
shift &= ~KB_E0ESC;
}
shift |= shiftcode[data];
shift ^= togglecode[data];
c = charcode[shift & (KB_CTL | KB_SHIFT)][data];
if (shift & KB_CAPSLOCK) {
if ('a' <= c && c <= 'z') {
c += 'A' - 'a';
} else if ('A' <= c && c <= 'Z') {
c += 'a' - 'A';
}
}
return c;
}
typedef struct Ps2kbEvConsumer {
struct Ps2kbEvConsumer *next;
Proc *proc;
RBuf rbuf;
} Ps2kbEvConsumer;
struct {
SpinLock spinlock;
Ps2kbEvConsumer *list;
} PS2KB_CONSUMERS = {0};
int32_t ps2kbdev_readch(uint8_t *buffer, size_t len, void *extra) { int32_t ps2kbdev_readch(uint8_t *buffer, size_t len, void *extra) {
(void)buffer; (void)len; (void)extra; uint64_t pid = (uint64_t)buffer;
uint8_t b = 0; Proc *consproc = NULL;
spinlock_acquire(&PS2KB_BUF.spinlock); spinlock_acquire(&PROCS.spinlock);
int32_t r = rbuf_pop(&PS2KB_BUF.rbuf, &b); Proc *proc, *proctmp;
spinlock_release(&PS2KB_BUF.spinlock); LL_FOREACH_SAFE(PROCS.procs, proc, proctmp) {
if (proc->pid == pid) {
consproc = proc;
}
}
spinlock_release(&PROCS.spinlock);
if (consproc == NULL) {
return E_INVALIDOPER;
}
uint8_t b;
int32_t r = -1;
spinlock_acquire(&PS2KB_CONSUMERS.spinlock);
Ps2kbEvConsumer *cons, *constmp;
LL_FOREACH_SAFE(PS2KB_CONSUMERS.list, cons, constmp) {
if (cons->proc == consproc) {
r = rbuf_pop(&cons->rbuf, &b);
break;
}
}
spinlock_release(&PS2KB_CONSUMERS.spinlock);
if (r == 0) { if (r == 0) {
return b; return b;
@ -26,14 +199,61 @@ int32_t ps2kbdev_readch(uint8_t *buffer, size_t len, void *extra) {
} }
} }
#define CONSUMER_RBUF_MAX 0x400
int32_t ps2kbdev_attchcons(uint8_t *buffer, size_t len, void *extra) {
uint64_t pid = (uint64_t)buffer;
spinlock_acquire(&PROCS.spinlock);
Proc *proc, *proctmp;
LL_FOREACH_SAFE(PROCS.procs, proc, proctmp) {
if (proc->pid == pid) {
Ps2kbEvConsumer *cons = dlmalloc(sizeof(*cons));
cons->proc = proc;
uint8_t *buf = dlmalloc(CONSUMER_RBUF_MAX);
rbuf_init(&cons->rbuf, buf, CONSUMER_RBUF_MAX);
spinlock_acquire(&PS2KB_CONSUMERS.spinlock);
LL_APPEND(PS2KB_CONSUMERS.list, cons);
spinlock_release(&PS2KB_CONSUMERS.spinlock);
break;
}
}
spinlock_release(&PROCS.spinlock);
return E_OK;
}
void ps2kbdev_intr(void) {
int32_t c = ps2kb_intr();
if (c >= 0) {
uint8_t b = c;
spinlock_acquire(&PS2KB_CONSUMERS.spinlock);
Ps2kbEvConsumer *cons, *constmp;
LL_FOREACH_SAFE(PS2KB_CONSUMERS.list, cons, constmp) {
bool found = false;
spinlock_acquire(&PROCS.spinlock);
Proc *proc, *proctmp;
LL_FOREACH_SAFE(PROCS.procs, proc, proctmp) {
if (proc == cons->proc) {
found = true;
}
}
if (!found) {
LL_REMOVE(PS2KB_CONSUMERS.list, cons);
}
spinlock_release(&PROCS.spinlock);
rbuf_push(&cons->rbuf, b);
}
spinlock_release(&PS2KB_CONSUMERS.spinlock);
}
}
void ps2kbdev_init(void) { void ps2kbdev_init(void) {
const int bufsz = 0x1000; intr_attchhandler(&ps2kbdev_intr, INTR_IRQBASE+1);
uint8_t *buf = dlmalloc(bufsz);
rbuf_init(&PS2KB_BUF.rbuf, buf, bufsz);
PS2KB_BUF.init = true;
Dev *ps2kbdev; Dev *ps2kbdev;
HSHTB_ALLOC(DEVTABLE.devs, ident, "ps2kbdev", ps2kbdev); HSHTB_ALLOC(DEVTABLE.devs, ident, "ps2kbdev", ps2kbdev);
spinlock_init(&ps2kbdev->spinlock); spinlock_init(&ps2kbdev->spinlock);
spinlock_init(&PS2KB_CONSUMERS.spinlock);
ps2kbdev->fns[DEV_PS2KBDEV_READCH] = &ps2kbdev_readch; ps2kbdev->fns[DEV_PS2KBDEV_READCH] = &ps2kbdev_readch;
ps2kbdev->fns[DEV_PS2KBDEV_ATTCHCONS] = &ps2kbdev_attchcons;
} }

View File

@ -4,17 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "dev.h" #include "dev.h"
#include "rbuf/rbuf.h"
#include "spinlock/spinlock.h"
typedef struct {
RBuf rbuf;
SpinLock spinlock;
bool init;
} Ps2KbFastBuf;
void ps2kbdev_init(void); void ps2kbdev_init(void);
extern Ps2KbFastBuf PS2KB_BUF;
#endif // DEV_PS2KBDEV_H_ #endif // DEV_PS2KBDEV_H_

View File

@ -6,8 +6,47 @@
#include "util/util.h" #include "util/util.h"
#include "hshtb.h" #include "hshtb.h"
#include "sysdefs/devctl.h" #include "sysdefs/devctl.h"
#include "drivers/serial/serial.h"
#include "kprintf.h" #include "kprintf.h"
#include "hal/hal.h"
// https://wiki.osdev.org/Serial_Ports
#define PORT 0x3f8
void serial_init(void) {
io_out8(PORT+1, 0x00);
io_out8(PORT+3, 0x80);
io_out8(PORT+0, 0x03);
io_out8(PORT+1, 0x00);
io_out8(PORT+3, 0x03);
io_out8(PORT+2, 0xC7);
io_out8(PORT+4, 0x0B);
io_out8(PORT+4, 0x1E);
io_out8(PORT+0, 0xAE);
if (io_in8(PORT+0) != 0xAE) {
ERR("serial", "serial is faulty!\n");
return;
}
io_out8(PORT+4, 0x0F);
}
int serial_recvready(void) {
return io_in8(PORT+5) & 1;
}
uint8_t serial_recvb(void) {
return io_in8(PORT);
}
int serial_sendready(void) {
return io_in8(PORT+5) & 0x20;
}
void serial_sendb(uint8_t b) {
io_out8(PORT, b);
}
int32_t serialdev_sendb(uint8_t *buffer, size_t len, void *extra) { int32_t serialdev_sendb(uint8_t *buffer, size_t len, void *extra) {
(void)len; (void)extra; (void)len; (void)extra;

View File

@ -1,142 +0,0 @@
#include <stdint.h>
#include "ps2kb.h"
#include "hal/hal.h"
#include "kprintf.h"
#define KB_CTL_STATUS 0x64
#define KB_DATA_IN_BUF 0x01
#define KB_DATA 0x60
#define KB_SHIFT (1<<0)
#define KB_CTL (1<<1)
#define KB_ALT (1<<2)
#define KB_CAPSLOCK (1<<3)
#define KB_NUMLOCK (1<<4)
#define KB_SCRLLOCK (1<<5)
#define KB_E0ESC (1<<6)
#define KB_HOME 0xe0
#define KB_END 0xe1
#define KB_UP 0xe2
#define KB_DOWN 0xe3
#define KB_LEFT 0xe4
#define KB_RIGHT 0xe5
#define KB_PAGEUP 0xe6
#define KB_PAGEDN 0xe7
#define KB_INSERT 0xe8
#define KB_DELETE 0xe9
#define C(x) ((x)-'@')
static uint8_t shiftcode[0x100] = {
[0x1d] KB_CTL,
[0x2a] KB_SHIFT,
[0x36] KB_SHIFT,
[0x38] KB_ALT,
[0x9d] KB_CTL,
[0xb8] KB_ALT,
};
static uint8_t togglecode[0x100] = {
[0x3a] KB_CAPSLOCK,
[0x45] KB_NUMLOCK,
[0x46] KB_SCRLLOCK,
};
static uint8_t normalmap[0x100] = {
0x0, 0x1b, '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '\b', '\t',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
'o', 'p', '[', ']', '\n', 0x0, 'a', 's',
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
'\'', '`', 0x0, '\\', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/', 0x0, '*',
0x0, ' ', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '7',
'8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.', 0x0, 0x0, 0x0, 0x0,
[0x9c] '\n',
[0xb5] '/',
[0xc8] KB_UP, [0xd0] KB_DOWN,
[0xc9] KB_PAGEUP, [0xd1] KB_PAGEDN,
[0xcb] KB_LEFT, [0xcd] KB_RIGHT,
[0x97] KB_HOME, [0xcf] KB_END,
[0xd2] KB_INSERT, [0xd3] KB_DELETE,
};
static uint8_t shiftmap[256] = {
0x0, 033,'!', '@', '#', '$', '%', '^',
'&', '*', '(', ')', '_', '+', '\b', '\t',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
'O', 'P', '{', '}', '\n', 0x0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
'"', '~', 0x0, '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', 0x0, '*',
0x0, ' ', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '7',
'8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.', 0x0, 0x0, 0x0, 0x0,
[0x9C] '\n',
[0xB5] '/',
[0xc8] KB_UP, [0xd0] KB_DOWN,
[0xc9] KB_PAGEUP, [0xd1] KB_PAGEDN,
[0xcb] KB_LEFT, [0xcd] KB_RIGHT,
[0x97] KB_HOME, [0xcf] KB_END,
[0xd2] KB_INSERT, [0xd3] KB_DELETE,
};
static uint8_t ctlmap[256] =
{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
C('O'), C('P'), 0x0, 0x0, '\r', 0x0, C('A'), C('S'),
C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), 0x0,
0x0, 0x0, 0x0, C('\\'), C('Z'), C('X'), C('C'), C('V'),
C('B'), C('N'), C('M'), 0x0, 0x0, C('/'), 0x0, 0x0,
[0x9C] '\r',
[0xB5] C('/'),
[0xc8] KB_UP, [0xd0] KB_DOWN,
[0xc9] KB_PAGEUP, [0xd1] KB_PAGEDN,
[0xcb] KB_LEFT, [0xcd] KB_RIGHT,
[0x97] KB_HOME, [0xcf] KB_END,
[0xd2] KB_INSERT, [0xd3] KB_DELETE,
};
int32_t ps2kb_intr(void) {
static uint8_t shift;
static uint8_t *charcode[4] = { normalmap, shiftmap, ctlmap, ctlmap };
uint32_t st, data, c;
st = io_in8(KB_CTL_STATUS);
if (!(st & KB_DATA_IN_BUF)) {
return -1;
}
data = io_in8(KB_DATA);
if (data == 0xe0) {
shift |= KB_E0ESC;
return 0;
} else if (data & 0x80) {
data = (shift & KB_E0ESC ? data : data & 0x7F);
shift &= ~(shiftcode[data] | KB_E0ESC);
return 0;
} else if (shift & KB_E0ESC) {
data |= 0x80;
shift &= ~KB_E0ESC;
}
shift |= shiftcode[data];
shift ^= togglecode[data];
c = charcode[shift & (KB_CTL | KB_SHIFT)][data];
if (shift & KB_CAPSLOCK) {
if ('a' <= c && c <= 'z') {
c += 'A' - 'a';
} else if ('A' <= c && c <= 'Z') {
c += 'a' - 'A';
}
}
return c;
}

View File

@ -1,8 +0,0 @@
#ifndef DRIVERS_PS2KB_H_
#define DRIVERS_PS2KB_H_
#include <stdint.h>
int32_t ps2kb_intr(void);
#endif // DRIVERS_PS2KB_H_

View File

@ -1,42 +0,0 @@
#include <stdint.h>
#include "hal/hal.h"
#include "kprintf.h"
// https://wiki.osdev.org/Serial_Ports
#define PORT 0x3f8
void serial_init(void) {
io_out8(PORT+1, 0x00);
io_out8(PORT+3, 0x80);
io_out8(PORT+0, 0x03);
io_out8(PORT+1, 0x00);
io_out8(PORT+3, 0x03);
io_out8(PORT+2, 0xC7);
io_out8(PORT+4, 0x0B);
io_out8(PORT+4, 0x1E);
io_out8(PORT+0, 0xAE);
if (io_in8(PORT+0) != 0xAE) {
ERR("serial", "serial is faulty!\n");
return;
}
io_out8(PORT+4, 0x0F);
}
int serial_recvready(void) {
return io_in8(PORT+5) & 1;
}
uint8_t serial_recvb(void) {
return io_in8(PORT);
}
int serial_sendready(void) {
return io_in8(PORT+5) & 0x20;
}
void serial_sendb(uint8_t b) {
io_out8(PORT, b);
}

View File

@ -1,12 +0,0 @@
#ifndef DRIVERS_SERIAL_SERIAL_H_
#define DRIVERS_SERIAL_SERIAL_H_
#include <stdint.h>
void serial_init(void);
int serial_recvready(void);
uint8_t serial_recvb(void);
int serial_sendready(void);
void serial_sendb(uint8_t b);
#endif // DRIVERS_SERIAL_SERIAL_H_

View File

@ -12,10 +12,25 @@
#include "proc/proc.h" #include "proc/proc.h"
#include "syscall/syscall.h" #include "syscall/syscall.h"
#include "errors.h" #include "errors.h"
#include "drivers/ps2kb/ps2kb.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "rbuf/rbuf.h" #include "rbuf/rbuf.h"
#include "dev/ps2kbdev.h" #include "dlmalloc/malloc.h"
#include "util/util.h"
typedef struct IntrHandler {
struct IntrHandler *next;
void (*fn)(void);
int irq;
} IntrHandler;
IntrHandler *INTR_HANDLERS = NULL;
void intr_attchhandler(void (*fn)(void), int irq) {
IntrHandler *ih = dlmalloc(sizeof(*ih));
ih->fn = fn;
ih->irq = irq;
LL_APPEND(INTR_HANDLERS, ih);
}
typedef struct BackTraceFrame { typedef struct BackTraceFrame {
struct BackTraceFrame *rbp; struct BackTraceFrame *rbp;
@ -211,13 +226,12 @@ void intr_handleintr(IntrStackFrame *frame) {
intr_eoi(); intr_eoi();
proc_sched((void *)frame); proc_sched((void *)frame);
break; break;
case INTR_IRQBASE+1: default:
int32_t c = ps2kb_intr(); IntrHandler *ih, *ihtmp;
if (c >= 0 && PS2KB_BUF.init) { LL_FOREACH_SAFE(INTR_HANDLERS, ih, ihtmp) {
uint8_t b = c; if (ih->irq == frame->trapnum) {
spinlock_acquire(&PS2KB_BUF.spinlock); ih->fn();
rbuf_push(&PS2KB_BUF.rbuf, b); }
spinlock_release(&PS2KB_BUF.spinlock);
} }
intr_eoi(); intr_eoi();
break; break;

View File

@ -33,6 +33,7 @@ typedef struct {
uint64_t ss; uint64_t ss;
} PACKED IntrStackFrame; } PACKED IntrStackFrame;
void intr_attchhandler(void (*fn)(void), int irq);
void intr_init(void); void intr_init(void);
#endif // HAL_INTR_H_ #endif // HAL_INTR_H_

View File

@ -62,10 +62,10 @@ void kmain(void) {
hal_init(); hal_init();
pmm_init(); pmm_init();
hal_vmm_init(); hal_vmm_init();
dev_init();
storedev_init(); storedev_init();
baseimg_init(); baseimg_init();
vfs_init(); vfs_init();
dev_init();
proc_init(); proc_init();
for(;;); for(;;);

View File

@ -52,6 +52,44 @@ int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
goto done; goto done;
} }
} break; } break;
case DEVCTL_DEVLS_SZ: {
size_t n = 0;
spinlock_acquire(&DEVTABLE.spinlock);
for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) {
if (DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) {
n++;
}
}
spinlock_release(&DEVTABLE.spinlock);
ret = n;
} break;
case DEVCTL_DEVLS_STAT: {
DevStat *devstat = (DevStat *)buffer1;
size_t idx = (size_t)len1;
if (devstat == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
devstat->present = false;
spinlock_acquire(&DEVTABLE.spinlock);
for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) {
if (i == idx && DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) {
Dev *dev = &DEVTABLE.devs[i];
hal_memcpy(devstat->name, dev->ident, sizeof(dev->ident));
for (size_t j = 0; j < DEV_FNS_MAX; j++) {
if (dev->fns[j] != NULL) {
devstat->nfns++;
}
}
devstat->present = true;
break;
}
}
spinlock_release(&DEVTABLE.spinlock);
} break;
default: { default: {
if (devh == NULL) { if (devh == NULL) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;

View File

@ -51,12 +51,18 @@ void term_doinit(void *addr) {
BOOT_INFO.fb->blue_mask_size, BOOT_INFO.fb->blue_mask_size,
BOOT_INFO.fb->blue_mask_shift, BOOT_INFO.fb->blue_mask_shift,
NULL, // canvas NULL, // canvas
ansi_colours, // ansi colors NULL,
ansi_bright_colours, // ansi bright colors NULL,
&default_bg, // default bg NULL,
&default_fg, // default fg NULL,
&default_fg_bright, // default bg bright NULL,
&default_bg_bright, // default fg bright NULL,
/* ansi_colours, // ansi colors */
/* ansi_bright_colours, // ansi bright colors */
/* &default_bg, // default bg */
/* &default_fg, // default fg */
/* &default_fg_bright, // default bg bright */
/* &default_bg_bright, // default fg bright */
FM_T_437_F16, FM_T_437_F16,
8, 8,
16, 16,

View File

@ -2,20 +2,37 @@
#define SHARE_SYSDEFS_DEVCTL_H_ #define SHARE_SYSDEFS_DEVCTL_H_
#define DEVCTL_GET_HANDLE 100 #define DEVCTL_GET_HANDLE 100
#define DEVCTL_DEVLS_SZ 101
#define DEVCTL_DEVLS_STAT 102
#define DEV_TERMDEV_PUTCH 0 #define DEV_TERMDEV_PUTCH 0
#define DEV_PS2KBDEV_READCH 0 #define DEV_PS2KBDEV_READCH 0
#define DEV_PS2KBDEV_ATTCHCONS 1
#define DEV_SERIALDEV_SENDB 0 #define DEV_SERIALDEV_SENDB 0
#define DEV_SERIALDEV_SENDREADY 1 #define DEV_SERIALDEV_SENDREADY 1
#define DEV_SERIALDEV_RECVB 2 #define DEV_SERIALDEV_RECVB 2
#define DEV_SERIALDEV_RECVREADY 3 #define DEV_SERIALDEV_RECVREADY 3
#define DEV_FBDEV_GETINFO 0
#if !defined(__ASSEMBLER__) #if !defined(__ASSEMBLER__)
typedef uint64_t Dev_t; typedef uint64_t Dev_t;
typedef struct {
uint16_t w, h;
uint16_t margin;
uint8_t fontw, fonth;
} FbDevGetInfo;
typedef struct {
bool present;
char name[0x100];
size_t nfns;
} DevStat;
#endif #endif
#endif // SHARE_SYSDEFS_DEVCTL_H_ #endif // SHARE_SYSDEFS_DEVCTL_H_

138
ulib/dlinklist.h Normal file
View File

@ -0,0 +1,138 @@
#ifndef ULIB_DLINKLIST_H_
#define ULIB_DLINKLIST_H_
#define DL_APPEND(head, new) \
do { \
if ((new) != NULL) { \
(new)->next = NULL; \
if ((head) != NULL) { \
typeof((head)) __tmp = (head); \
while (__tmp->next != NULL) { \
__tmp = __tmp->next; \
} \
__tmp->next = (new); \
(new)->prev = __tmp; \
} else { \
(new)->prev = NULL; \
(head) = (new); \
} \
} \
} while (0)
#define DL_PREPEND(head, new) \
do { \
if ((new) != NULL) { \
(new)->prev = NULL; \
(new)->next = (head); \
if ((head) != NULL) { \
(head)->prev = (new); \
} \
(head) = (new); \
} \
} while (0)
#define DL_REMOVE(head, ele) \
do { \
if ((ele) != NULL) { \
if ((ele)->prev != NULL) { \
(ele)->prev->next = (ele)->next; \
} else { \
(head) = (ele)->next; \
} \
if ((ele)->next != NULL) { \
(ele)->next->prev = (ele)->prev; \
} \
(ele)->next = NULL; \
(ele)->prev = NULL; \
} \
} while (0)
#define DL_FINDPROP(head, out, propname, propvalue) \
do { \
(out) = NULL; \
typeof((head)) __tmp = (head); \
while (__tmp) { \
if (__tmp->propname == (propvalue)) { \
(out) = __tmp; \
break; \
} \
__tmp = __tmp->next; \
} \
} while (0)
#define DL_FOREACH_SAFE(head, var, tmp) \
for (var = (head), tmp = (var ? var->next : NULL); \
var != NULL; \
var = tmp, tmp = (var ? var->next : NULL))
#define DL_FOREACH_SAFE_IDX(head, var, tmp, idx) \
for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \
var != NULL; \
var = tmp, tmp = (var ? var->next : NULL), (idx)++)
#define DL_FOREACH_SAFE_IDX_LIMIT(head, var, tmp, idx, max) \
for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \
var != NULL && (idx) < (max); \
var = tmp, tmp = (var ? var->next : NULL), (idx)++)
#define DL_BACK(head, out) \
do { \
(out) = NULL; \
if ((head) != NULL) { \
typeof((head)) __tmp = (head); \
while (__tmp->next != NULL) { \
__tmp = __tmp->next; \
} \
(out) = __tmp; \
} \
} while (0)
#define DL_FRONT(head, out) \
do { \
(out) = NULL; \
if ((head) != NULL) { \
typeof((head)) __tmp = (head); \
while (__tmp->prev != NULL) { \
__tmp = __tmp->prev; \
} \
(out) = __tmp; \
} \
} while (0)
#define DL_INSERT_AFTER(head, pos, new) \
do { \
if ((pos) != NULL && (new) != NULL) { \
(new)->prev = (pos); \
(new)->next = (pos)->next; \
if ((pos)->next != NULL) { \
(pos)->next->prev = (new); \
} \
(pos)->next = (new); \
} else if ((pos) == NULL && (head) == NULL) { \
/* Empty list: make new head */ \
(new)->prev = NULL; \
(new)->next = NULL; \
(head) = (new); \
} \
} while (0)
#define DL_INSERT_BEFORE(head, pos, new) \
do { \
if ((pos) != NULL && (new) != NULL) { \
(new)->next = (pos); \
(new)->prev = (pos)->prev; \
if ((pos)->prev != NULL) { \
(pos)->prev->next = (new); \
} else { \
(head) = (new); \
} \
(pos)->prev = (new); \
} else if ((pos) == NULL && (head) == NULL) { \
/* Empty list: make new head */ \
(new)->prev = NULL; \
(new)->next = NULL; \
(head) = (new); \
} \
} while (0)
#endif // ULIB_DLINKLIST_H_

View File

@ -183,3 +183,32 @@ char *string_combine(char *dest, const char *src) {
dest[i+j] = '\0'; dest[i+j] = '\0';
return dest; return dest;
} }
// https://aticleworld.com/memmove-function-implementation-in-c/
void * string_memmove(void* dest, const void* src, unsigned int n)
{
char *pDest = (char *)dest;
const char *pSrc =( const char*)src;
//allocate memory for tmp array
char *tmp = (char *)umalloc(sizeof(char ) * n);
if(NULL == tmp)
{
return NULL;
}
else
{
unsigned int i = 0;
// copy src to tmp array
for(i =0; i < n ; ++i)
{
*(tmp + i) = *(pSrc + i);
}
//copy tmp to dest
for(i =0 ; i < n ; ++i)
{
*(pDest + i) = *(tmp + i);
}
ufree(tmp); //free allocated memory
}
return dest;
}

View File

@ -18,6 +18,7 @@ char *string_strchr(const char *s, int c);
int string_strncmp(const char * s1, const char * s2, size_t n); int string_strncmp(const char * s1, const char * s2, size_t n);
char *string_tokenizealloc(char *s, char *delim); char *string_tokenizealloc(char *s, char *delim);
char *string_combine(char *dest, const char *src); char *string_combine(char *dest, const char *src);
void * string_memmove(void* dest, const void* src, unsigned int n);
#endif #endif

View File

@ -17,6 +17,7 @@
#include <assert.h> #include <assert.h>
#include <umalloc/umalloc.h> #include <umalloc/umalloc.h>
#include <fs/path.h> #include <fs/path.h>
#include <dlinklist.h>
#include <errors.h> #include <errors.h>
#include <sysdefs/ioctl.h> #include <sysdefs/ioctl.h>

2
user/dev/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
dev

24
user/dev/Makefile Normal file
View File

@ -0,0 +1,24 @@
include $(ROOT)/mk/grabsrc.mk
include ../Makefile.inc
.PHONY: all clean
TARGET := dev
LDFLAGS += -L$(ROOT)/ulib -l:libulib.a
SRCFILES := $(call GRABSRC, .)
CFILES := $(call GET_CFILES, $(SRCFILES))
OBJ := $(call GET_OBJ, $(SRCFILES))
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
all: $(TARGET)
$(TARGET): $(OBJ)
$(LD) $^ $(LDFLAGS) -o $@
echo $$(realpath $(TARGET)) >> $(FILES)
clean:
rm -f $(OBJ) $(TARGET)

20
user/dev/ls.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdint.h>
#include <stddef.h>
#include <ulib.h>
void dev_ls(void) {
size_t ndevs = devctl(NULL, DEVCTL_DEVLS_SZ, NULL, 0, 0);
uprintf("TOTAL: %zu\n", ndevs);
uprintf("%-20s %-10s\n", "DEVICE", "FUNCTIONS");
for (size_t i = 0; i < 0x100; i++) {
DevStat devstat; ZERO(&devstat);
devctl(NULL, DEVCTL_DEVLS_STAT, (uint8_t *)&devstat, i, 0);
if (!devstat.present)
continue;
uprintf("%-20s %-10zu\n", devstat.name, devstat.nfns);
}
}

24
user/dev/main.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdint.h>
#include <stddef.h>
#include <ulib.h>
#define CMDS(X) \
X(ls)
void main(void) {
if (argslen() == 0) {
return;
}
char *cmd = args()[0];
#define X(name) if (string_strcmp(cmd, #name) == 0) { \
extern void dev_ ## name(void); \
dev_ ## name(); \
return; \
}
CMDS(X)
#undef X
uprintf("dev: unknown command %s\n", cmd);
}

View File

@ -21,12 +21,8 @@ void tb_runinitscript(void) {
r = ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_READ, (uint8_t *)buf, sizeof(buf)-1); r = ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_READ, (uint8_t *)buf, sizeof(buf)-1);
if (r > 0) { if (r > 0) {
devctl(&termdev, DEV_TERMDEV_PUTCH, (uint8_t *)buf, string_len(buf), 0); devctl(&termdev, DEV_TERMDEV_PUTCH, (uint8_t *)buf, string_len(buf), 0);
} } else {
schedrelease();
r = devctl(&ps2kbdev, DEV_PS2KBDEV_READCH, NULL, 0, 0);
if (r != E_NOTYET) {
uint8_t b = r;
ipcpipe(tb, IPCPIPE_IN, IPCPIPE_WRITE, &b, 1);
} }
} }
} }
@ -34,6 +30,7 @@ void tb_runinitscript(void) {
void main(void) { void main(void) {
PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0); PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
devctl(&ps2kbdev, DEVCTL_GET_HANDLE, (uint8_t *)"ps2kbdev", 0, 0); devctl(&ps2kbdev, DEVCTL_GET_HANDLE, (uint8_t *)"ps2kbdev", 0, 0);
devctl(&ps2kbdev, DEV_PS2KBDEV_ATTCHCONS, (uint8_t *)PID, 0, 0);
tb_runinitscript(); tb_runinitscript();

View File

@ -6,6 +6,7 @@
#include "macros.h" #include "macros.h"
extern PID_t PID; extern PID_t PID;
extern Dev_t ps2kbdev;
static InterpResult RES; static InterpResult RES;
@ -214,11 +215,10 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter
processctl(app, PCTL_RUN, 0, 0, 0); processctl(app, PCTL_RUN, 0, 0, 0);
uint8_t b;
while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) { while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) {
if (interactive) { if (interactive) {
int32_t nrd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, &b, 1); int32_t key = devctl(&ps2kbdev, DEV_PS2KBDEV_READCH, (uint8_t *)PID, 0, 0);
if (nrd > 0 && b == C('S')) { if (key > 0 && (uint8_t)key == C('S')) {
processctl(app, PCTL_KILL, 0, 0, 0); processctl(app, PCTL_KILL, 0, 0, 0);
goto cleanup; goto cleanup;
} }

View File

@ -7,6 +7,7 @@
#define LINEBUF_MAX 1024 #define LINEBUF_MAX 1024
PID_t PID; PID_t PID;
Dev_t ps2kbdev;
struct { struct {
char *modestr; char *modestr;
@ -106,8 +107,9 @@ void do_mode_interactive(void) {
uint8_t b = 0; uint8_t b = 0;
for (;;) { for (;;) {
int32_t nrd = ipcpipe(PID, IPCPIPE_IN, IPCPIPE_READ, &b, 1); int32_t key = devctl(&ps2kbdev, DEV_PS2KBDEV_READCH, (uint8_t *)PID, 0, 0);
if (nrd > 0) { if (key > 0) {
b = (uint8_t)key;
switch (b) { switch (b) {
case C('C'): case C('C'):
case 0xE9: case 0xE9:
@ -137,6 +139,7 @@ void do_mode_interactive(void) {
if (cursor < LINEBUF_MAX) { if (cursor < LINEBUF_MAX) {
linebuf[cursor] = '\0'; linebuf[cursor] = '\0';
} }
uprintf(ANSIQ_GR_RESET);
uprintf("\n"); uprintf("\n");
InterpResult *res; InterpResult *res;
if (!interp_runstring(linebuf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE)) { if (!interp_runstring(linebuf, &res, CONFIG.logcmds, CONFIG.mode == MODE_INTERACTIVE)) {
@ -155,6 +158,8 @@ void main(void) {
} }
if (CONFIG.mode == MODE_INTERACTIVE) { if (CONFIG.mode == MODE_INTERACTIVE) {
devctl(&ps2kbdev, DEVCTL_GET_HANDLE, (uint8_t *)"ps2kbdev", 0, 0);
devctl(&ps2kbdev, DEV_PS2KBDEV_ATTCHCONS, (uint8_t *)PID, 0, 0);
do_mode_interactive(); do_mode_interactive();
} else if (CONFIG.mode == MODE_RUNFILE) { } else if (CONFIG.mode == MODE_RUNFILE) {
if (CONFIG.filepath == NULL) { if (CONFIG.filepath == NULL) {