Rewrite ps2kbproc, rbuf (kernel ring buffer) and pipe read/write, Change to -O0 in kernel code
This commit is contained in:
@ -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 \
|
||||||
|
@ -6,16 +6,20 @@
|
|||||||
#include "dlmalloc/malloc.h"
|
#include "dlmalloc/malloc.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "pipe.h"
|
#include "pipe.h"
|
||||||
|
#include "kprintf.h"
|
||||||
|
|
||||||
int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) {
|
int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) {
|
||||||
hal_memset(pipe, 0, sizeof(*pipe));
|
hal_memset(pipe, 0, sizeof(*pipe));
|
||||||
spinlock_init(&pipe->spinlock);
|
spinlock_init(&pipe->spinlock);
|
||||||
pipe->ownerpid = pid;
|
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;
|
return E_NOMEMORY;
|
||||||
}
|
}
|
||||||
pipe->rbuf.cap = IPC_PIPE_MAX;
|
|
||||||
|
rbuf_init(&pipe->rbuf, buf, IPC_PIPE_MAX);
|
||||||
|
|
||||||
return E_OK;
|
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;
|
size_t i = 0;
|
||||||
spinlock_acquire(&pipe->spinlock);
|
spinlock_acquire(&pipe->spinlock);
|
||||||
for (; i < n; i++) {
|
for (; i < n; i++) {
|
||||||
if (rbuf_push(&pipe->rbuf, buffer[i]) < 0) {
|
rbuf_push(&pipe->rbuf, buffer[i]);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
spinlock_release(&pipe->spinlock);
|
spinlock_release(&pipe->spinlock);
|
||||||
return i;
|
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) {
|
int32_t ipc_piperead(IpcPipe *pipe, uint8_t *const buffer, size_t n) {
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
spinlock_acquire(&pipe->spinlock);
|
spinlock_acquire(&pipe->spinlock);
|
||||||
for (; i < n; i++) {
|
while (i < n) {
|
||||||
if (rbuf_pop(&pipe->rbuf, &buffer[i]) < 0) {
|
if (rbuf_pop(&pipe->rbuf, &buffer[i]) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
spinlock_release(&pipe->spinlock);
|
spinlock_release(&pipe->spinlock);
|
||||||
return i;
|
return i;
|
||||||
|
@ -9,14 +9,13 @@
|
|||||||
Proc *PS2KBPROC = NULL;
|
Proc *PS2KBPROC = NULL;
|
||||||
Ps2KbFastBuf PS2KB_BUF;
|
Ps2KbFastBuf PS2KB_BUF;
|
||||||
|
|
||||||
|
|
||||||
void ps2kbproc_init(Proc *proc) {
|
void ps2kbproc_init(Proc *proc) {
|
||||||
PS2KBPROC = proc;
|
PS2KBPROC = proc;
|
||||||
|
|
||||||
PS2KBPROC->bcast_pipes.list = NULL;
|
PS2KBPROC->bcast_pipes.list = NULL;
|
||||||
|
|
||||||
PS2KB_BUF.rbuf.buffer = dlmalloc(IPC_PIPE_MAX);
|
uint8_t *buf = dlmalloc(IPC_PIPE_MAX);
|
||||||
PS2KB_BUF.rbuf.cap = IPC_PIPE_MAX;
|
rbuf_init(&PS2KB_BUF.rbuf, buf, IPC_PIPE_MAX);
|
||||||
spinlock_init(&PS2KB_BUF.spinlock);
|
spinlock_init(&PS2KB_BUF.spinlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef PROC_PS2KB_PS2KBPROC_H_
|
#ifndef PROC_PS2KB_PS2KBPROC_H_
|
||||||
#define PROC_PS2KB_PS2KBPROC_H_
|
#define PROC_PS2KB_PS2KBPROC_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include "proc/proc.h"
|
#include "proc/proc.h"
|
||||||
#include "rbuf/rbuf.h"
|
#include "rbuf/rbuf.h"
|
||||||
#include "spinlock/spinlock.h"
|
#include "spinlock/spinlock.h"
|
||||||
|
@ -18,7 +18,7 @@ void termproc_fn(void) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
char c = 0;
|
char c = 0;
|
||||||
int32_t read = ipc_piperead(TERMPROC->pipes[1], (uint8_t *)&c, 1);
|
int32_t read = ipc_piperead(TERMPROC->pipes[1], (uint8_t *)&c, 1);
|
||||||
if (read > 0 && c != 0) {
|
if (read > 0) {
|
||||||
kprintf("%c", c);
|
kprintf("%c", c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,34 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "rbuf.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) {
|
int32_t rbuf_push(RBuf *rbuf, uint8_t data) {
|
||||||
size_t next;
|
if (rbuf->count == rbuf->cap) {
|
||||||
|
|
||||||
next = rbuf->head + 1;
|
|
||||||
if (next >= rbuf->cap) {
|
|
||||||
next = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (next == rbuf->tail) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rbuf->buffer[rbuf->head] = data;
|
rbuf->buffer[rbuf->head] = data;
|
||||||
rbuf->head = next;
|
rbuf->head = (rbuf->head + 1) % rbuf->cap;
|
||||||
|
rbuf->count++;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data) {
|
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data) {
|
||||||
size_t next;
|
if (rbuf->count == 0) {
|
||||||
|
|
||||||
if (rbuf->head == rbuf->tail) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
next = rbuf->tail + 1;
|
|
||||||
if (next >= rbuf->cap) {
|
|
||||||
next = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
*data = rbuf->buffer[rbuf->tail];
|
*data = rbuf->buffer[rbuf->tail];
|
||||||
rbuf->tail = next;
|
rbuf->tail = (rbuf->tail + 1) % rbuf->cap;
|
||||||
|
rbuf->count--;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,19 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t *buffer;
|
uint8_t *buffer;
|
||||||
size_t head;
|
|
||||||
size_t tail;
|
size_t tail;
|
||||||
|
size_t head;
|
||||||
size_t cap;
|
size_t cap;
|
||||||
|
size_t count;
|
||||||
|
bool full;
|
||||||
} RBuf;
|
} RBuf;
|
||||||
|
|
||||||
int32_t rbuf_push(RBuf *rbuf, uint8_t data);
|
int32_t rbuf_push(RBuf *rbuf, uint8_t data);
|
||||||
int32_t rbuf_pop(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_
|
#endif // RBUF_RBUF_H_
|
||||||
|
@ -120,6 +120,8 @@ void do_mode_interactive(void) {
|
|||||||
linebuf[cursor++] = b;
|
linebuf[cursor++] = b;
|
||||||
uprintf("%c", b);
|
uprintf("%c", b);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
schedrelease();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user