Handle ps2 keyboard via special process

This commit is contained in:
2025-09-07 20:53:36 +02:00
parent 4f3053bc8e
commit 9644ad0b4e
16 changed files with 312 additions and 40 deletions

View File

@ -53,6 +53,9 @@ SRCFILES += $(call GRABSRC, \
fs/portlfs \ fs/portlfs \
baseimg \ baseimg \
proc \ proc \
proc/kproc \
proc/ps2kbproc \
proc/termproc \
hal \ hal \
hal/$(ARCH) \ hal/$(ARCH) \
std \ std \

View File

@ -3,17 +3,139 @@
#include "hal/hal.h" #include "hal/hal.h"
#include "kprintf.h" #include "kprintf.h"
void ps2kb_write(uint8_t val) { #define KB_CTL_STATUS 0x64
while (io_in8(0x64) & 2); #define KB_DATA_IN_BUF 0x01
io_out8(0x64, val); #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,
[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;
} }
uint8_t ps2kb_read(void) { shift |= shiftcode[data];
while (!(io_in8(0x64) & 1)); shift ^= togglecode[data];
return io_in8(0x60); 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';
}
} }
void ps2kb_intr(void) { return c;
uint8_t scancode = ps2kb_read();
kprintf("%02x\n", scancode);
} }

View File

@ -3,8 +3,6 @@
#include <stdint.h> #include <stdint.h>
void ps2kb_write(uint8_t val); int32_t ps2kb_intr(void);
uint8_t ps2kb_read(void);
void ps2kb_intr(void);
#endif // DRIVERS_PS2KB_H_ #endif // DRIVERS_PS2KB_H_

View File

@ -13,6 +13,8 @@
#include "syscall/syscall.h" #include "syscall/syscall.h"
#include "errors.h" #include "errors.h"
#include "drivers/ps2kb/ps2kb.h" #include "drivers/ps2kb/ps2kb.h"
#include "ipc/pipe/pipe.h"
#include "proc/ps2kbproc/ps2kbproc.h"
void hal_intr_disable(void) { void hal_intr_disable(void) {
asm volatile("cli"); asm volatile("cli");
@ -197,7 +199,9 @@ void intr_handleintr(IntrStackFrame *frame) {
proc_sched((void *)frame); proc_sched((void *)frame);
break; break;
case INTR_IRQBASE+1: case INTR_IRQBASE+1:
ps2kb_intr(); int32_t c = ps2kb_intr();
uint8_t *bytes = (uint8_t *)&c;
ipc_pipewrite(PS2KBPROC->pipes[0], bytes, sizeof(c));
intr_eoi(frame->trapnum - INTR_IRQBASE); intr_eoi(frame->trapnum - INTR_IRQBASE);
break; break;
} }

16
kernel/proc/kproc/kproc.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdint.h>
#include "proc/proc.h"
#include "hal/hal.h"
#include "ipc/pipe/pipe.h"
#include "kprintf.h"
Proc *KPROC;
void kproc_init(Proc *proc) {
KPROC = proc;
}
void kproc_fn(void) {
for (;;) {
}
}

11
kernel/proc/kproc/kproc.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef PROC_KPROC_KPROC_H_
#define PROC_KPROC_KPROC_H_
#include "proc/proc.h"
extern Proc *KPROC;
void kproc_fn(void);
void kproc_init(Proc *proc);
#endif // PROC_KPROC_KPROC_H_

View File

@ -13,6 +13,9 @@
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "kproc/kproc.h"
#include "ps2kbproc/ps2kbproc.h"
#include "termproc/termproc.h"
#define PROC_REAPER_FREQ 30 #define PROC_REAPER_FREQ 30
@ -281,30 +284,22 @@ void proc_status(void) {
} }
} }
Proc *kproc = NULL;
void proc_kproc(void) {
char buf[100];
hal_memset(buf, 0, sizeof(buf));
for (;;) {
int32_t read = ipc_piperead(kproc->pipes[1], buf, sizeof(buf));
kprintf("%.*s", read, buf);
hal_memset(buf, 0, sizeof(buf));
}
}
void proc_init(void) { void proc_init(void) {
spinlock_init(&PROCS.spinlock); spinlock_init(&PROCS.spinlock);
PROCS.procs = NULL; PROCS.procs = NULL;
kproc = proc_spawnkern(&proc_kproc, "kproc"); kproc_init(proc_spawnkern(&kproc_fn, "kproc"));
kproc->pipes[1] = dlmalloc(sizeof(IpcPipe)); proc_register(KPROC);
ipc_pipeinit(kproc->pipes[1]); PROCS.current = KPROC;
proc_register(kproc);
PROCS.current = kproc; ps2kbproc_init(proc_spawnkern(&ps2kbproc_fn, "ps2kbproc"));
proc_register(PS2KBPROC);
termproc_init(proc_spawnkern(&termproc_fn, "termproc"));
proc_register(TERMPROC);
Proc *init = proc_spawnuser("base", "/bin/init"); Proc *init = proc_spawnuser("base", "/bin/init");
init->pipes[0] = kproc->pipes[1]; init->pipes[0] = TERMPROC->pipes[1];
proc_register(init); proc_register(init);
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);

View File

@ -0,0 +1,19 @@
#include <stdint.h>
#include "proc/proc.h"
#include "dlmalloc/malloc.h"
#include "ipc/pipe/pipe.h"
Proc *PS2KBPROC;
void ps2kbproc_init(Proc *proc) {
PS2KBPROC = proc;
PS2KBPROC->pipes[0] = dlmalloc(sizeof(IpcPipe));
ipc_pipeinit(PS2KBPROC->pipes[0]);
}
void ps2kbproc_fn(void) {
for (;;) {
}
}

View File

@ -0,0 +1,11 @@
#ifndef PROC_PS2KB_PS2KBPROC_H_
#define PROC_PS2KB_PS2KBPROC_H_
#include "proc/proc.h"
extern Proc *PS2KBPROC;
void ps2kbproc_fn(void);
void ps2kbproc_init(Proc *proc);
#endif // PROC_PS2KB_PS2KBPROC_H_

View File

@ -0,0 +1,25 @@
#include <stdint.h>
#include "proc/proc.h"
#include "ipc/pipe/pipe.h"
#include "kprintf.h"
#include "hal/hal.h"
#include "dlmalloc/malloc.h"
Proc *TERMPROC;
void termproc_init(Proc *proc) {
TERMPROC = proc;
TERMPROC->pipes[1] = dlmalloc(sizeof(IpcPipe));
ipc_pipeinit(TERMPROC->pipes[1]);
}
void termproc_fn(void) {
char buf[100];
for (;;) {
hal_memset(buf, 0, sizeof(buf));
int32_t read = ipc_piperead(TERMPROC->pipes[1], (uint8_t *)buf, sizeof(buf));
if (read > 0) {
kprintf("%.*s", read, buf);
}
}
}

View File

@ -0,0 +1,11 @@
#ifndef PROC_TERMPROC_TERMPROC_H_
#define PROC_TERMPROC_TERMPROC_H_
#include "proc/proc.h"
extern Proc *TERMPROC;
void termproc_init(Proc *proc);
void termproc_fn(void);
#endif // PROC_TERMPROC_TERMPROC_H_

View File

@ -1,24 +1,36 @@
#include <stdatomic.h> #include <stdatomic.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include "spinlock.h" #include "spinlock.h"
#include "hal/hal.h" #include "hal/hal.h"
#include "kprintf.h"
void spinlock_init(SpinLock *sl) { void spinlock_init(SpinLock *sl) {
atomic_store(&sl->lock, false); atomic_store(&sl->lock, false);
sl->flags = 0;
}
uint64_t spinlock_irqsave(void) {
uint64_t flags;
asm volatile("pushfq; cli; popq %0" : "=r"(flags));
return flags;
}
void spinlock_irqrestore(uint64_t flags) {
asm volatile("pushq %0; popfq" :: "r"(flags));
} }
void spinlock_acquire(SpinLock *sl) { void spinlock_acquire(SpinLock *sl) {
bool unlocked = false; sl->flags = spinlock_irqsave();
while (!atomic_compare_exchange_weak(&sl->lock, &unlocked, true)) {
unlocked = false; while (atomic_test_and_set_explicit(&sl->lock, memory_order_acquire)) {
SPINLOCK_HINT(); SPINLOCK_HINT();
} }
} }
void spinlock_release(SpinLock *sl) { void spinlock_release(SpinLock *sl) {
bool locked = true; atomic_clear_flag_explicit(&sl->lock, memory_order_release);
if (atomic_compare_exchange_strong(&sl->lock, &locked, false)) {
atomic_store(&sl->lock, false); spinlock_irqrestore(sl->flags);
}
} }

View File

@ -6,6 +6,7 @@
typedef struct { typedef struct {
atomic_bool lock; atomic_bool lock;
uint64_t flags;
} SpinLock; } SpinLock;
#define SPINLOCK_HINT() asm volatile("pause") #define SPINLOCK_HINT() asm volatile("pause")

View File

@ -8,6 +8,7 @@
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "errors.h" #include "errors.h"
#include "util/util.h" #include "util/util.h"
#include "kprintf.h"
int32_t SYSCALL5(sys_ipcpipe, pid1, pipenum1, cmd1, buffer1, len1) { int32_t SYSCALL5(sys_ipcpipe, pid1, pipenum1, cmd1, buffer1, len1) {
uint64_t pid = pid1; uint64_t pid = pid1;
@ -64,8 +65,34 @@ int32_t SYSCALL5(sys_ipcpipe, pid1, pipenum1, cmd1, buffer1, len1) {
IpcPipe *pipe = proc->pipes[pipenum]; IpcPipe *pipe = proc->pipes[pipenum];
if (pipe == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = ipc_pipewrite(pipe, buffer, len1); ret = ipc_pipewrite(pipe, buffer, len1);
} break; } break;
case IPCPIPE_READ: {
if (pipenum >= PROC_PIPEHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
uint8_t *const buffer = (uint8_t *const)buffer1;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
IpcPipe *pipe = proc->pipes[pipenum];
if (pipe == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = ipc_piperead(pipe, buffer, len1);
} break;
default: { default: {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
} break; } break;

View File

@ -20,6 +20,12 @@
#define atomic_compare_exchange_strong(p, old, new) \ #define atomic_compare_exchange_strong(p, old, new) \
__atomic_compare_exchange_n(p, old, new, false, memory_order_relaxed, memory_order_relaxed) __atomic_compare_exchange_n(p, old, new, false, memory_order_relaxed, memory_order_relaxed)
#define atomic_test_and_set_explicit(p, memory_order) \
__atomic_test_and_set(p, memory_order)
#define atomic_clear_flag_explicit(p, memory_order) \
__atomic_clear(p, memory_order)
#define atomic_bool _Atomic(bool) #define atomic_bool _Atomic(bool)
#endif #endif

View File

@ -5,6 +5,8 @@
#include <system/ioctl.h> #include <system/ioctl.h>
#include <uprintf.h> #include <uprintf.h>
#include <ansiq/all.h> #include <ansiq/all.h>
#include <sysdefs/ipcpipe.h>
#include <system/ipcpipe.h>
void main(void) { void main(void) {
debugprint(ANSIQ_SCR_CLR_ALL); debugprint(ANSIQ_SCR_CLR_ALL);
@ -24,6 +26,15 @@ void main(void) {
uprintf("Hello world using uprintf\n"); uprintf("Hello world using uprintf\n");
while(1) {
int32_t kbchar;
int32_t read = ipcpipe(1, 0, IPCPIPE_READ, (uint8_t *)&kbchar, sizeof(kbchar));
if (read > 0 && (kbchar >= 0x20 && kbchar <= 0x7F)) {
uprintf("%c", (char)kbchar);
}
}
for(;;); for(;;);
} }