Compare commits
2 Commits
4f3053bc8e
...
80a788617e
Author | SHA1 | Date | |
---|---|---|---|
80a788617e | |||
9644ad0b4e |
@ -53,6 +53,10 @@ SRCFILES += $(call GRABSRC, \
|
|||||||
fs/portlfs \
|
fs/portlfs \
|
||||||
baseimg \
|
baseimg \
|
||||||
proc \
|
proc \
|
||||||
|
proc/kproc \
|
||||||
|
proc/ps2kbproc \
|
||||||
|
proc/termproc \
|
||||||
|
proc/serialproc \
|
||||||
hal \
|
hal \
|
||||||
hal/$(ARCH) \
|
hal/$(ARCH) \
|
||||||
std \
|
std \
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
|
@ -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_
|
||||||
|
@ -2,17 +2,12 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "hal/hal.h"
|
#include "hal/hal.h"
|
||||||
#include "kprintf.h"
|
#include "kprintf.h"
|
||||||
#include "serial.h"
|
|
||||||
#include "gdt.h"
|
#include "gdt.h"
|
||||||
#include "intr.h"
|
#include "intr.h"
|
||||||
#include "pic.h"
|
#include "pic.h"
|
||||||
#include "pit.h"
|
#include "pit.h"
|
||||||
|
|
||||||
void hal_init(void) {
|
void hal_init(void) {
|
||||||
if (!serial_init()) {
|
|
||||||
hal_hang(); // going further makes no sense
|
|
||||||
}
|
|
||||||
LOG("hal", "serial init\n");
|
|
||||||
gdt_init();
|
gdt_init();
|
||||||
intr_init();
|
intr_init();
|
||||||
pic_init();
|
pic_init();
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include "io.h"
|
|
||||||
#include "putchar.h"
|
|
||||||
|
|
||||||
#define SERIAL_PORT 0x3f8
|
|
||||||
|
|
||||||
static int serial_received(void) {
|
|
||||||
return io_in8(SERIAL_PORT + 5) & 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t serial_read(void) {
|
|
||||||
while (serial_received() == 0);
|
|
||||||
return io_in8(SERIAL_PORT);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int serial_trans_empty(void) {
|
|
||||||
return io_in8(SERIAL_PORT + 5) & 0x20;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void serial_write(uint8_t value) {
|
|
||||||
while (!serial_trans_empty());
|
|
||||||
io_out8(SERIAL_PORT, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// REFERENCE: https://wiki.osdev.org/Serial_Ports
|
|
||||||
bool serial_init(void) {
|
|
||||||
io_out8(SERIAL_PORT + 1, 0x00);
|
|
||||||
io_out8(SERIAL_PORT + 3, 0x80);
|
|
||||||
io_out8(SERIAL_PORT + 0, 0x03);
|
|
||||||
io_out8(SERIAL_PORT + 1, 0x00);
|
|
||||||
io_out8(SERIAL_PORT + 3, 0x03);
|
|
||||||
io_out8(SERIAL_PORT + 2, 0xc7);
|
|
||||||
io_out8(SERIAL_PORT + 4, 0x0b);
|
|
||||||
io_out8(SERIAL_PORT + 4, 0x1e);
|
|
||||||
io_out8(SERIAL_PORT + 0, 0xae);
|
|
||||||
|
|
||||||
if (io_in8(SERIAL_PORT + 0) != 0xae) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
io_out8(SERIAL_PORT + 4, 0x0f);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if PUTCHAR_ == PUTCHAR_SERIAL
|
|
||||||
// For printf library
|
|
||||||
void putchar_(char c) {
|
|
||||||
serial_write(c);
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,8 +0,0 @@
|
|||||||
#ifndef HAL_SERIAL_H_
|
|
||||||
#define HAL_SERIAL_H_
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
bool serial_init(void);
|
|
||||||
|
|
||||||
#endif // HAL_SERIAL_H_
|
|
16
kernel/proc/kproc/kproc.c
Normal file
16
kernel/proc/kproc/kproc.c
Normal 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
11
kernel/proc/kproc/kproc.h
Normal 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_
|
@ -13,6 +13,10 @@
|
|||||||
#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"
|
||||||
|
#include "serialproc/serialproc.h"
|
||||||
|
|
||||||
#define PROC_REAPER_FREQ 30
|
#define PROC_REAPER_FREQ 30
|
||||||
|
|
||||||
@ -281,30 +285,25 @@ 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);
|
||||||
|
|
||||||
|
serialproc_init(proc_spawnkern(&serialproc_fn, "serialproc"));
|
||||||
|
proc_register(SERIALPROC);
|
||||||
|
|
||||||
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);
|
||||||
|
19
kernel/proc/ps2kbproc/ps2kbproc.c
Normal file
19
kernel/proc/ps2kbproc/ps2kbproc.c
Normal 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 (;;) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
11
kernel/proc/ps2kbproc/ps2kbproc.h
Normal file
11
kernel/proc/ps2kbproc/ps2kbproc.h
Normal 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_
|
73
kernel/proc/serialproc/serialproc.c
Normal file
73
kernel/proc/serialproc/serialproc.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include "proc/proc.h"
|
||||||
|
#include "ipc/pipe/pipe.h"
|
||||||
|
#include "hal/hal.h"
|
||||||
|
#include "dlmalloc/malloc.h"
|
||||||
|
|
||||||
|
#define SERIAL_PORT 0x3f8
|
||||||
|
|
||||||
|
Proc *SERIALPROC;
|
||||||
|
|
||||||
|
void serialproc_init(Proc *proc) {
|
||||||
|
SERIALPROC = proc;
|
||||||
|
SERIALPROC->pipes[0] = dlmalloc(sizeof(IpcPipe));
|
||||||
|
ipc_pipeinit(SERIALPROC->pipes[0]);
|
||||||
|
SERIALPROC->pipes[1] = dlmalloc(sizeof(IpcPipe));
|
||||||
|
ipc_pipeinit(SERIALPROC->pipes[1]);
|
||||||
|
|
||||||
|
io_out8(SERIAL_PORT + 1, 0x00);
|
||||||
|
io_out8(SERIAL_PORT + 3, 0x80);
|
||||||
|
io_out8(SERIAL_PORT + 0, 0x03);
|
||||||
|
io_out8(SERIAL_PORT + 1, 0x00);
|
||||||
|
io_out8(SERIAL_PORT + 3, 0x03);
|
||||||
|
io_out8(SERIAL_PORT + 2, 0xc7);
|
||||||
|
io_out8(SERIAL_PORT + 4, 0x0b);
|
||||||
|
io_out8(SERIAL_PORT + 4, 0x1e);
|
||||||
|
io_out8(SERIAL_PORT + 0, 0xae);
|
||||||
|
|
||||||
|
if (io_in8(SERIAL_PORT + 0) != 0xae) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
io_out8(SERIAL_PORT + 4, 0x0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
int serialproc_received(void) {
|
||||||
|
return io_in8(SERIAL_PORT + 5) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t serialproc_read(void) {
|
||||||
|
while (serialproc_received() == 0);
|
||||||
|
return io_in8(SERIAL_PORT);
|
||||||
|
}
|
||||||
|
|
||||||
|
int serialproc_trans_empty(void) {
|
||||||
|
return io_in8(SERIAL_PORT + 5) & 0x20;
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialproc_write(uint8_t value) {
|
||||||
|
while (!serialproc_trans_empty());
|
||||||
|
io_out8(SERIAL_PORT, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialproc_fn(void) {
|
||||||
|
char buf[0x100];
|
||||||
|
for (;;) {
|
||||||
|
|
||||||
|
hal_memset(buf, 0, sizeof(buf));
|
||||||
|
int32_t read = ipc_piperead(SERIALPROC->pipes[1], (uint8_t *)buf, sizeof(buf));
|
||||||
|
if (read > 0) {
|
||||||
|
for (size_t i = 0; i < sizeof(buf); i++) {
|
||||||
|
serialproc_write(buf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serialproc_received() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t inchar = io_in8(SERIAL_PORT);
|
||||||
|
ipc_pipewrite(SERIALPROC->pipes[0], &inchar, sizeof(inchar));
|
||||||
|
}
|
||||||
|
}
|
11
kernel/proc/serialproc/serialproc.h
Normal file
11
kernel/proc/serialproc/serialproc.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef PROC_SERIALPROC_SERIALPROC_H_
|
||||||
|
#define PROC_SERIALPROC_SERIALPROC_H_
|
||||||
|
|
||||||
|
#include "proc/proc.h"
|
||||||
|
|
||||||
|
extern Proc *SERIALPROC;
|
||||||
|
|
||||||
|
void serialproc_init(Proc *proc);
|
||||||
|
void serialproc_fn(void);
|
||||||
|
|
||||||
|
#endif // PROC_SERIALPROC_SERIALPROC_H_
|
25
kernel/proc/termproc/termproc.c
Normal file
25
kernel/proc/termproc/termproc.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
kernel/proc/termproc/termproc.h
Normal file
11
kernel/proc/termproc/termproc.h
Normal 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_
|
@ -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);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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")
|
||||||
|
@ -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;
|
||||||
|
@ -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
|
||||||
|
@ -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,16 @@ 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);
|
||||||
|
ipcpipe(3, 1, IPCPIPE_WRITE, (uint8_t *)&kbchar, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for(;;);
|
for(;;);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user