VFS mountpoint backing device system
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m24s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m24s
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
#include <device/device.h>
|
||||
#include <device/ramdrv.h>
|
||||
#include <device/terminal.h>
|
||||
#include <libk/fieldlengthof.h>
|
||||
#include <libk/rbtree.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <limine/requests.h>
|
||||
#include <m/kb_device.h>
|
||||
#include <m/ramdisk_device.h>
|
||||
#include <m/terminal_device.h>
|
||||
#include <m/xdrv_device.h>
|
||||
#include <mm/liballoc.h>
|
||||
#include <sync/spin_lock.h>
|
||||
#include <sys/debug.h>
|
||||
@@ -18,7 +22,7 @@ static struct rb_node_link* device_tree = NULL;
|
||||
static spin_lock_t device_tree_lock;
|
||||
|
||||
struct device* device_create (int id, device_op_func_t* ops, size_t ops_len,
|
||||
bool (*init) (void* arg), void (*fini) (void), void* arg) {
|
||||
device_init_func_t init, device_fini_func_t fini, void* arg) {
|
||||
if (ops_len >= fieldlengthof (struct device, ops))
|
||||
return NULL;
|
||||
|
||||
@@ -36,7 +40,7 @@ struct device* device_create (int id, device_op_func_t* ops, size_t ops_len,
|
||||
if (ops != NULL)
|
||||
memcpy (device->ops, ops, ops_len * sizeof (device_op_func_t));
|
||||
|
||||
if (!device->init (arg)) {
|
||||
if (!device->init (device, arg)) {
|
||||
free (device);
|
||||
return NULL;
|
||||
}
|
||||
@@ -57,7 +61,8 @@ void device_delete (struct device* device) {
|
||||
spin_unlock (&device->lock);
|
||||
spin_unlock (&device_tree_lock);
|
||||
|
||||
device->fini ();
|
||||
device->fini (device);
|
||||
free (device);
|
||||
}
|
||||
|
||||
struct device* device_find (int id) {
|
||||
@@ -70,19 +75,55 @@ struct device* device_find (int id) {
|
||||
return device;
|
||||
}
|
||||
|
||||
void devices_init (void) {
|
||||
{
|
||||
device_op_func_t ops[] = {
|
||||
[TERMINAL_PUTSTR] = &terminal_putstr,
|
||||
};
|
||||
device_create (TERMINAL_DEVICE, ops, lengthof (ops), &terminal_init, &terminal_fini, NULL);
|
||||
void terminal_device_init (void) {
|
||||
device_op_func_t ops[] = {
|
||||
[TERMINAL_PUTSTR] = &terminal_putstr,
|
||||
};
|
||||
device_create (TERMINAL_DEVICE, ops, lengthof (ops), &terminal_init, &terminal_fini, NULL);
|
||||
}
|
||||
|
||||
void ramdisk_device_init (void) {
|
||||
device_op_func_t ops[] = {
|
||||
[XDRV_GET_SIZE] = &ramdrv_get_size,
|
||||
[XDRV_GET_SECTOR_SIZE] = &ramdrv_get_sector_size,
|
||||
[XDRV_GET_DEVICE_TYPE] = &ramdrv_get_device_type,
|
||||
[XDRV_READ] = &ramdrv_read,
|
||||
};
|
||||
|
||||
struct limine_module_response* module = limine_module_request.response;
|
||||
|
||||
const char* ramdisk_path = "/boot/mop3dist.tar";
|
||||
struct limine_file* file = NULL;
|
||||
|
||||
for (size_t i = 0; i < module->module_count; i++) {
|
||||
file = module->modules[i];
|
||||
|
||||
if (strncmp (file->path, ramdisk_path, strlen (ramdisk_path)) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
struct ramdrv_init init = {
|
||||
.total_size = file->size,
|
||||
.sector_size = 1024,
|
||||
.buffer = file->address,
|
||||
};
|
||||
device_create (RAMDISK_DEVICE, ops, lengthof (ops), &ramdrv_init, &ramdrv_fini, &init);
|
||||
}
|
||||
|
||||
#if defined(__x86_64__)
|
||||
{
|
||||
device_op_func_t ops[] = {
|
||||
[KB_READ_KEY] = &ps2kb_read_key,
|
||||
};
|
||||
device_create (KB_DEVICE, ops, lengthof (ops), &ps2kb_init, &ps2kb_fini, NULL);
|
||||
}
|
||||
void ps2kb_device_init (void) {
|
||||
device_op_func_t ops[] = {
|
||||
[KB_READ_KEY] = &ps2kb_read_key,
|
||||
};
|
||||
device_create (KB_DEVICE, ops, lengthof (ops), &ps2kb_init, &ps2kb_fini, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
void devices_init (void) {
|
||||
terminal_device_init ();
|
||||
ramdisk_device_init ();
|
||||
|
||||
#if defined(__x86_64__)
|
||||
ps2kb_device_init ();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -7,20 +7,32 @@
|
||||
#include <sync/spin_lock.h>
|
||||
#include <sys/smp.h>
|
||||
|
||||
typedef bool (*device_op_func_t) (struct proc* proc, struct cpu** reschedule_cpu, int* ret,
|
||||
void* a1, void* a2, void* a3, void* a4);
|
||||
struct device;
|
||||
|
||||
struct device_op_ctx {
|
||||
struct proc* proc;
|
||||
struct cpu** reschedule_cpu;
|
||||
bool* reschedule;
|
||||
};
|
||||
|
||||
typedef int (*device_op_func_t) (struct device* device, struct device_op_ctx* op_ctx, void* a1,
|
||||
void* a2, void* a3, void* a4);
|
||||
|
||||
typedef bool (*device_init_func_t) (struct device* device, void* arg);
|
||||
typedef void (*device_fini_func_t) (struct device* device);
|
||||
|
||||
struct device {
|
||||
int id;
|
||||
device_op_func_t ops[32];
|
||||
spin_lock_t lock;
|
||||
struct rb_node_link device_tree_link;
|
||||
bool (*init) (void* arg);
|
||||
void (*fini) (void);
|
||||
device_init_func_t init;
|
||||
device_fini_func_t fini;
|
||||
void* udata;
|
||||
};
|
||||
|
||||
struct device* device_create (int id, device_op_func_t* ops, size_t ops_len,
|
||||
bool (*init) (void* arg), void (*fini) (void), void* arg);
|
||||
device_init_func_t init, device_fini_func_t fini, void* arg);
|
||||
struct device* device_find (int id);
|
||||
void device_delete (struct device* device);
|
||||
void devices_init (void);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <amd64/apic.h>
|
||||
#include <amd64/intr_defs.h>
|
||||
#include <amd64/io.h>
|
||||
#include <device/device.h>
|
||||
#include <device/ps2_kb.h>
|
||||
#include <irq/irq.h>
|
||||
#include <libk/ringbuffer.h>
|
||||
#include <libk/std.h>
|
||||
@@ -126,7 +128,7 @@ static uint8_t ctlmap[256] = {
|
||||
/* clang-format on */
|
||||
};
|
||||
|
||||
int32_t ps2kb_keycode (void) {
|
||||
static int32_t ps2kb_keycode (void) {
|
||||
static uint8_t shift;
|
||||
static uint8_t* charcode[4] = {normalmap, shiftmap, ctlmap, ctlmap};
|
||||
uint32_t st, data, c;
|
||||
@@ -163,7 +165,7 @@ int32_t ps2kb_keycode (void) {
|
||||
return c;
|
||||
}
|
||||
|
||||
bool ps2kb_irq (struct cpu** reschedule_cpu, void* arg, void* regs) {
|
||||
static bool ps2kb_irq (struct cpu** reschedule_cpu, void* arg, void* regs) {
|
||||
int32_t keycode = ps2kb_keycode ();
|
||||
|
||||
if (keycode <= 0)
|
||||
@@ -192,12 +194,12 @@ bool ps2kb_irq (struct cpu** reschedule_cpu, void* arg, void* regs) {
|
||||
return PROC_NO_RESCHEDULE;
|
||||
}
|
||||
|
||||
bool ps2kb_read_key (struct proc* proc, struct cpu** reschedule_cpu, int* ret, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
if (!(proc->procgroup->capabilities & PROC_CAP_KB)) {
|
||||
*ret = -ST_PERMISSION_ERROR;
|
||||
return PROC_NO_RESCHEDULE;
|
||||
}
|
||||
int ps2kb_read_key (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
(void)device;
|
||||
|
||||
if ((op_ctx->proc != NULL) && !(op_ctx->proc->procgroup->capabilities & PROC_CAP_KB))
|
||||
return -ST_PERMISSION_ERROR;
|
||||
|
||||
uint8_t* chbuf = (uint8_t*)a1;
|
||||
|
||||
@@ -209,18 +211,21 @@ bool ps2kb_read_key (struct proc* proc, struct cpu** reschedule_cpu, int* ret, v
|
||||
|
||||
size_t new_count = ps2kb_ringbuffer.count;
|
||||
|
||||
*ret = ST_OK;
|
||||
|
||||
/* didn't pop anything */
|
||||
if (prev_count == new_count)
|
||||
return proc_sq_suspend (proc, &ps2kb_sq, &ps2kb_ringbuffer_lock, reschedule_cpu);
|
||||
if (prev_count == new_count) {
|
||||
*op_ctx->reschedule =
|
||||
proc_sq_suspend (op_ctx->proc, &ps2kb_sq, &ps2kb_ringbuffer_lock, op_ctx->reschedule_cpu);
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
spin_unlock (&ps2kb_ringbuffer_lock);
|
||||
|
||||
return PROC_NO_RESCHEDULE;
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
bool ps2kb_init (void* arg) {
|
||||
bool ps2kb_init (struct device* device, void* arg) {
|
||||
(void)device;
|
||||
|
||||
amd64_ioapic_route_irq (PS2KB, 1, 0, thiscpu->lapic_id);
|
||||
irq_attach (&ps2kb_irq, NULL, PS2KB);
|
||||
|
||||
@@ -241,7 +246,9 @@ bool ps2kb_init (void* arg) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void ps2kb_fini (void) {
|
||||
void ps2kb_fini (struct device* device) {
|
||||
(void)device;
|
||||
|
||||
irq_detach (PS2KB);
|
||||
ringbuffer_fini (&ps2kb_ringbuffer);
|
||||
}
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
#define _KERNEL_DEVICE_PS2_KB_H
|
||||
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <sys/smp.h>
|
||||
|
||||
bool ps2kb_read_key (struct proc* proc, struct cpu** reschedule_cpu, int* ret, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
bool ps2kb_init (void* arg);
|
||||
void ps2kb_fini (void);
|
||||
struct device;
|
||||
struct device_op_ctx;
|
||||
|
||||
int ps2kb_read_key (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
bool ps2kb_init (struct device* device, void* arg);
|
||||
void ps2kb_fini (struct device* device);
|
||||
|
||||
#endif // _KERNEL_DEVICE_PS2_KB_H
|
||||
|
||||
109
kernel/device/ramdrv.c
Normal file
109
kernel/device/ramdrv.c
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <device/device.h>
|
||||
#include <device/ramdrv.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <m/status.h>
|
||||
#include <m/xdrv_device.h>
|
||||
#include <mm/liballoc.h>
|
||||
|
||||
bool ramdrv_init (struct device* device, void* arg) {
|
||||
struct ramdrv_init* init = arg;
|
||||
|
||||
struct ramdrv* ramdrv = malloc (sizeof (*ramdrv));
|
||||
|
||||
if (ramdrv == NULL)
|
||||
return false;
|
||||
|
||||
ramdrv->total_size = init->total_size;
|
||||
ramdrv->sector_size = init->sector_size;
|
||||
|
||||
if (init->buffer == NULL) {
|
||||
ramdrv->buffer = malloc (init->total_size);
|
||||
|
||||
if (ramdrv->buffer == NULL) {
|
||||
free (ramdrv);
|
||||
return false;
|
||||
}
|
||||
|
||||
ramdrv->managed = false;
|
||||
} else {
|
||||
ramdrv->buffer = init->buffer;
|
||||
ramdrv->managed = true;
|
||||
}
|
||||
|
||||
device->udata = ramdrv;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ramdrv_fini (struct device* device) {
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
if (!ramdrv->managed)
|
||||
free (ramdrv);
|
||||
}
|
||||
|
||||
int ramdrv_get_device_type (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
(void)device, (void)a2, (void)a3, (void)a4, (void)op_ctx;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
int* device_type = (int*)a1;
|
||||
|
||||
*device_type = XDRV_TYPE_RAMDRV;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int ramdrv_get_size (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
(void)a2, (void)a3, (void)a4, (void)op_ctx;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* size = (size_t*)a1;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
*size = ramdrv->total_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int ramdrv_get_sector_size (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
(void)a2, (void)a3, (void)a4, (void)op_ctx;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* secsize = (size_t*)a1;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
*secsize = ramdrv->sector_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int ramdrv_read (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2, void* a3,
|
||||
void* a4) {
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t pos = *(size_t*)a1;
|
||||
size_t size = *(size_t*)a2;
|
||||
uint8_t* buffer = a3;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
if (pos + size > ramdrv->total_size)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
memcpy (buffer, (void*)(((uintptr_t)ramdrv->buffer) + pos), size);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
38
kernel/device/ramdrv.h
Normal file
38
kernel/device/ramdrv.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef _KERNEL_DEVICE_RAMDRV_H
|
||||
#define _KERNEL_DEVICE_RAMDRV_H
|
||||
|
||||
#include <libk/std.h>
|
||||
|
||||
struct device;
|
||||
struct device_op_ctx;
|
||||
|
||||
struct ramdrv_init {
|
||||
size_t total_size;
|
||||
size_t sector_size;
|
||||
uint8_t* buffer;
|
||||
};
|
||||
|
||||
struct ramdrv {
|
||||
size_t total_size;
|
||||
size_t sector_size;
|
||||
uint8_t* buffer;
|
||||
bool managed;
|
||||
};
|
||||
|
||||
bool ramdrv_init (struct device* device, void* arg);
|
||||
|
||||
void ramdrv_fini (struct device* device);
|
||||
|
||||
int ramdrv_read (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2, void* a3,
|
||||
void* a4);
|
||||
|
||||
int ramdrv_get_device_type (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
|
||||
int ramdrv_get_sector_size (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
|
||||
int ramdrv_get_size (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
|
||||
#endif // _KERNEL_DEVICE_RAMDRV_H
|
||||
@@ -1,8 +1,10 @@
|
||||
c += device/device.c \
|
||||
device/terminal.c
|
||||
device/terminal.c \
|
||||
device/ramdrv.c
|
||||
|
||||
o += device/device.o \
|
||||
device/terminal.o
|
||||
device/terminal.o \
|
||||
device/ramdrv.o
|
||||
|
||||
ifeq ($(platform),amd64)
|
||||
c += device/ps2_kb.c
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Flanterm/src/flanterm.h>
|
||||
#include <Flanterm/src/flanterm_backends/fb.h>
|
||||
#include <device/device.h>
|
||||
#include <device/terminal.h>
|
||||
#include <libk/std.h>
|
||||
#include <limine/requests.h>
|
||||
@@ -8,7 +9,6 @@
|
||||
#include <proc/capability.h>
|
||||
#include <proc/proc.h>
|
||||
#include <sys/debug.h>
|
||||
#include <sys/smp.h>
|
||||
|
||||
struct flanterm_context* ft_ctx;
|
||||
|
||||
@@ -19,8 +19,8 @@ void ft_free (void* ptr, size_t size) {
|
||||
free (ptr);
|
||||
}
|
||||
|
||||
bool terminal_init (void* arg) {
|
||||
(void)arg;
|
||||
bool terminal_init (struct device* device, void* arg) {
|
||||
(void)arg, (void)device;
|
||||
|
||||
struct limine_framebuffer_response* fb_r = limine_framebuffer_request.response;
|
||||
struct limine_framebuffer* fb = fb_r->framebuffers[0];
|
||||
@@ -33,27 +33,22 @@ bool terminal_init (void* arg) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void terminal_fini (void) {}
|
||||
void terminal_fini (struct device* device) { (void)device; }
|
||||
|
||||
bool terminal_putstr (struct proc* proc, struct cpu** reschedule_cpu, int* ret, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
(void)reschedule_cpu, (void)a2, (void)a3, (void)a4;
|
||||
int terminal_putstr (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4) {
|
||||
(void)a2, (void)a3, (void)a4, (void)device;
|
||||
|
||||
if (!(proc->procgroup->capabilities & PROC_CAP_TERMINAL)) {
|
||||
*ret = -ST_PERMISSION_ERROR;
|
||||
return PROC_NO_RESCHEDULE;
|
||||
}
|
||||
if ((op_ctx->proc != NULL) && !(op_ctx->proc->procgroup->capabilities & PROC_CAP_TERMINAL))
|
||||
return -ST_PERMISSION_ERROR;
|
||||
|
||||
char* string = (char*)a1;
|
||||
size_t* len = (size_t*)a2;
|
||||
|
||||
if (string == NULL || len == NULL) {
|
||||
*ret = -ST_BAD_ADDRESS_SPACE;
|
||||
return PROC_NO_RESCHEDULE;
|
||||
}
|
||||
if (string == NULL || len == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
flanterm_write (ft_ctx, string, *len);
|
||||
|
||||
*ret = ST_OK;
|
||||
return PROC_NO_RESCHEDULE;
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
#define _KERNEL_DEVICE_TERMINAL_H
|
||||
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <sys/smp.h>
|
||||
|
||||
bool terminal_init (void* arg);
|
||||
void terminal_fini (void);
|
||||
bool terminal_putstr (struct proc* proc, struct cpu** reschedule_cpu, int* ret, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
struct device;
|
||||
struct device_op_ctx;
|
||||
|
||||
bool terminal_init (struct device* device, void* arg);
|
||||
void terminal_fini (struct device* device);
|
||||
int terminal_putstr (struct device* device, struct device_op_ctx* op_ctx, void* a1, void* a2,
|
||||
void* a3, void* a4);
|
||||
|
||||
#endif // _KERNEL_DEVICE_TERMINAL_H
|
||||
|
||||
Reference in New Issue
Block a user