Rewrite ps2kbproc, rbuf (kernel ring buffer) and pipe read/write, Change to -O0 in kernel code

This commit is contained in:
2025-09-20 16:50:40 +02:00
parent 222e846881
commit 0c65bd9891
8 changed files with 39 additions and 33 deletions

View File

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

View File

@ -6,16 +6,20 @@
#include "dlmalloc/malloc.h"
#include "errors.h"
#include "pipe.h"
#include "kprintf.h"
int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) {
hal_memset(pipe, 0, sizeof(*pipe));
spinlock_init(&pipe->spinlock);
pipe->ownerpid = pid;
pipe->rbuf.buffer = dlmalloc(IPC_PIPE_MAX);
if (pipe->rbuf.buffer == NULL) {
uint8_t *buf = dlmalloc(IPC_PIPE_MAX);
if (buf == NULL) {
return E_NOMEMORY;
}
pipe->rbuf.cap = IPC_PIPE_MAX;
rbuf_init(&pipe->rbuf, buf, IPC_PIPE_MAX);
return E_OK;
}
@ -29,9 +33,7 @@ int32_t ipc_pipewrite(IpcPipe *pipe, const uint8_t *const buffer, size_t n) {
size_t i = 0;
spinlock_acquire(&pipe->spinlock);
for (; i < n; i++) {
if (rbuf_push(&pipe->rbuf, buffer[i]) < 0) {
break;
}
rbuf_push(&pipe->rbuf, buffer[i]);
}
spinlock_release(&pipe->spinlock);
return i;
@ -40,10 +42,11 @@ int32_t ipc_pipewrite(IpcPipe *pipe, const uint8_t *const buffer, size_t n) {
int32_t ipc_piperead(IpcPipe *pipe, uint8_t *const buffer, size_t n) {
size_t i = 0;
spinlock_acquire(&pipe->spinlock);
for (; i < n; i++) {
while (i < n) {
if (rbuf_pop(&pipe->rbuf, &buffer[i]) < 0) {
break;
}
i++;
}
spinlock_release(&pipe->spinlock);
return i;

View File

@ -9,14 +9,13 @@
Proc *PS2KBPROC = NULL;
Ps2KbFastBuf PS2KB_BUF;
void ps2kbproc_init(Proc *proc) {
PS2KBPROC = proc;
PS2KBPROC->bcast_pipes.list = NULL;
PS2KB_BUF.rbuf.buffer = dlmalloc(IPC_PIPE_MAX);
PS2KB_BUF.rbuf.cap = IPC_PIPE_MAX;
uint8_t *buf = dlmalloc(IPC_PIPE_MAX);
rbuf_init(&PS2KB_BUF.rbuf, buf, IPC_PIPE_MAX);
spinlock_init(&PS2KB_BUF.spinlock);
}

View File

@ -1,6 +1,7 @@
#ifndef PROC_PS2KB_PS2KBPROC_H_
#define PROC_PS2KB_PS2KBPROC_H_
#include <stdbool.h>
#include "proc/proc.h"
#include "rbuf/rbuf.h"
#include "spinlock/spinlock.h"

View File

@ -18,7 +18,7 @@ void termproc_fn(void) {
for (;;) {
char c = 0;
int32_t read = ipc_piperead(TERMPROC->pipes[1], (uint8_t *)&c, 1);
if (read > 0 && c != 0) {
if (read > 0) {
kprintf("%c", c);
}
}

View File

@ -1,37 +1,34 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "rbuf.h"
#include "kprintf.h"
#include "hal/hal.h"
void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize) {
rbuf->buffer = buf;
rbuf->tail = 0;
rbuf->head = 0;
rbuf->cap = bufsize;
rbuf->count = 0;
}
int32_t rbuf_push(RBuf *rbuf, uint8_t data) {
size_t next;
next = rbuf->head + 1;
if (next >= rbuf->cap) {
next = 0;
}
if (next == rbuf->tail) {
if (rbuf->count == rbuf->cap) {
return -1;
}
rbuf->buffer[rbuf->head] = data;
rbuf->head = next;
rbuf->head = (rbuf->head + 1) % rbuf->cap;
rbuf->count++;
return 0;
}
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data) {
size_t next;
if (rbuf->head == rbuf->tail) {
if (rbuf->count == 0) {
return -1;
}
next = rbuf->tail + 1;
if (next >= rbuf->cap) {
next = 0;
}
*data = rbuf->buffer[rbuf->tail];
rbuf->tail = next;
rbuf->tail = (rbuf->tail + 1) % rbuf->cap;
rbuf->count--;
return 0;
}

View File

@ -3,15 +3,19 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
typedef struct {
uint8_t *buffer;
size_t head;
size_t tail;
size_t head;
size_t cap;
size_t count;
bool full;
} RBuf;
int32_t rbuf_push(RBuf *rbuf, uint8_t data);
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data);
void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize);
#endif // RBUF_RBUF_H_

View File

@ -120,6 +120,8 @@ void do_mode_interactive(void) {
linebuf[cursor++] = b;
uprintf("%c", b);
}
} else {
schedrelease();
}
}