Compare commits

..

2 Commits

Author SHA1 Message Date
04b7355a3d VFS can now reschedule the calling process
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m48s
2026-03-11 19:07:22 +01:00
e765855309 Per-cpu kernel pseudo process 2026-03-11 17:47:12 +01:00
16 changed files with 285 additions and 153 deletions

View File

@@ -15,6 +15,7 @@
#include <fs_types.h>
#include <irq/irq.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/limine.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
@@ -57,17 +58,22 @@ void bootmain (void) {
ioapic_init ();
hpet_init ();
proc_pid_alloc_init ();
procgroup_pgid_alloc_init ();
bsp_cpu->kproc = kproc_create ();
devices_init ();
vfs_init ();
struct reschedule_ctx rctx;
memset (&rctx, 0, sizeof (rctx));
struct device* rd0 = device_find ("RD0");
vfs_create_volume ("RD", FS_TARFS, rd0, false);
vfs_create_volume (thiscpu->kproc, &rctx, "RD", FS_TARFS, rd0, false);
struct device* temp0 = device_find ("TEMP0");
vfs_create_volume ("TEMP", FS_FAT16, temp0, true);
proc_pid_alloc_init ();
procgroup_pgid_alloc_init ();
vfs_create_volume (thiscpu->kproc, &rctx, "TEMP", FS_FAT16, temp0, true);
smp_init ();

View File

@@ -102,10 +102,12 @@ static void smp_bootstrap (struct limine_mp_info* mp_info) {
atomic_fetch_sub (&cpu_counter, 1);
cpu->kproc = kproc_create ();
struct reschedule_ctx rctx;
memset (&rctx, 0, sizeof (rctx));
struct proc* spin_proc = proc_from_file (VFS_KERNEL, "RD", "/spin", &rctx);
struct proc* spin_proc = proc_from_file (thiscpu->kproc, "RD", "/spin", &rctx);
proc_register (spin_proc, thiscpu, &rctx);
spin_lock (&spin_proc->cpu->lock);

View File

@@ -33,6 +33,7 @@ struct cpu {
struct list_node_link* proc_run_q;
struct proc* proc_current;
int proc_run_q_count;
struct proc* kproc;
};
struct cpu* cpu_make (uint64_t lapic_id, uint64_t acpi_id);

View File

@@ -12,6 +12,8 @@
#include <libk/string.h>
#include <mm/liballoc.h>
#include <path_defs.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <status.h>
#include <sys/debug.h>
#include <write_file.h>
@@ -30,7 +32,7 @@ static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu
spin_lock (&back_device->lock);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, ctx->proc, ctx->rctx, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
@@ -39,7 +41,8 @@ static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
&phys_sector_count);
ret = device_op (back_device, XDRV_READ, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
ret = device_op (back_device, XDRV_READ, ctx->proc, ctx->rctx, &phys_sector, &phys_sector_count,
buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
@@ -66,7 +69,7 @@ static int fat1_diskio_write (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* b
spin_lock (&back_device->lock);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, ctx->proc, ctx->rctx, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
@@ -75,7 +78,8 @@ static int fat1_diskio_write (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* b
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
&phys_sector_count);
ret = device_op (back_device, XDRV_WRITE, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
ret = device_op (back_device, XDRV_WRITE, ctx->proc, ctx->rctx, &phys_sector, &phys_sector_count,
buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
@@ -88,7 +92,8 @@ static int fat1_diskio_write (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* b
return 1;
}
int fatfs_mount (struct vfs_volume* volume, bool format) {
int fatfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
bool format) {
struct fatfs_ctx* fatfs_ctx = malloc (sizeof (*fatfs_ctx));
int r;
@@ -102,9 +107,11 @@ int fatfs_mount (struct vfs_volume* volume, bool format) {
fl_init (fatfs_ctx);
fatfs_ctx->_fs.disk_io.read_media = &fat1_diskio_read;
fatfs_ctx->_fs.disk_io.write_media = &fat1_diskio_write;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
if (format) {
r = volume->driver_ops.format (volume);
r = volume->driver_ops.format (volume, proc, rctx);
if (r < 0)
return -ST_FORMAT_ERROR;
}
@@ -116,13 +123,15 @@ int fatfs_mount (struct vfs_volume* volume, bool format) {
return ST_OK;
}
int fatfs16_format (struct vfs_volume* volume) {
int fatfs16_format (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
struct device* back_device = volume->back_device;
size_t total_size;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
spin_lock (&back_device->lock);
device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
device_op (back_device, XDRV_GET_SIZE, proc, rctx, &total_size);
spin_unlock (&back_device->lock);
size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
@@ -130,13 +139,15 @@ int fatfs16_format (struct vfs_volume* volume) {
return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}
int fatfs32_format (struct vfs_volume* volume) {
int fatfs32_format (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
struct device* back_device = volume->back_device;
size_t total_size;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
spin_lock (&back_device->lock);
device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
device_op (back_device, XDRV_GET_SIZE, proc, rctx, &total_size);
spin_unlock (&back_device->lock);
size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
@@ -144,8 +155,11 @@ int fatfs32_format (struct vfs_volume* volume) {
return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}
int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
int fatfs_describe (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct desc* desc) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
if (fl_is_dir (fatfs_ctx, path)) {
FL_DIR dir;
@@ -176,9 +190,11 @@ int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* de
return ST_OK;
}
int fatfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
int fatfs_read_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
if (fl_is_dir (fatfs_ctx, path))
return -ST_NOT_FOUND;
@@ -196,9 +212,11 @@ int fatfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffe
return ST_OK;
}
int fatfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size, uint32_t flags) {
int fatfs_write_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
if (fl_is_dir (fatfs_ctx, path))
return -ST_NOT_FOUND;
@@ -221,10 +239,12 @@ int fatfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buff
return ST_OK;
}
int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num) {
int fatfs_read_dir_entry (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct dir_entry* entry, size_t entry_num) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
FL_DIR dir;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
if (!fl_is_dir (fatfs_ctx, path))
return -ST_NOT_FOUND;
@@ -248,8 +268,11 @@ int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct di
return ST_OK;
}
int fatfs_create_file (struct vfs_volume* volume, const char* path) {
int fatfs_create_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
FL_FILE* file = fl_fopen (fatfs_ctx, path, "wb+");
@@ -260,15 +283,21 @@ int fatfs_create_file (struct vfs_volume* volume, const char* path) {
return ST_OK;
}
int fatfs_create_dir (struct vfs_volume* volume, const char* path) {
int fatfs_create_dir (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
int r = fl_createdirectory (fatfs_ctx, path);
return r == 0 ? ST_OK : -ST_CREATE_DIR_ERROR;
}
int fatfs_remove (struct vfs_volume* volume, const char* path) {
int fatfs_remove (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx;
int r = fl_remove (fatfs_ctx, path);
return r == 0 ? ST_OK : -ST_REMOVE_ERROR;

View File

@@ -10,27 +10,32 @@
struct vfs_volume;
int fatfs_mount (struct vfs_volume* volume, bool format);
int fatfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
bool format);
int fatfs16_format (struct vfs_volume* volume);
int fatfs16_format (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int fatfs32_format (struct vfs_volume* volume);
int fatfs32_format (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc);
int fatfs_describe (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct desc* desc);
int fatfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int fatfs_read_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size);
int fatfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size, uint32_t flags);
int fatfs_write_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags);
int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num);
int fatfs_read_dir_entry (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct dir_entry* entry, size_t entry_num);
int fatfs_create_file (struct vfs_volume* volume, const char* path);
int fatfs_create_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
int fatfs_create_dir (struct vfs_volume* volume, const char* path);
int fatfs_create_dir (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
int fatfs_remove (struct vfs_volume* volume, const char* path);
int fatfs_remove (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
#endif // _KERNEL_FS_FATFS_H

View File

@@ -1,6 +1,9 @@
#ifndef _KERNEL_FS_FATFS_CTX_H
#define _KERNEL_FS_FATFS_CTX_H
struct proc;
struct reschedule_ctx;
struct fatfs_ctx {
FL_FILE _files[FATFS_MAX_OPEN_FILES];
int _filelib_init;
@@ -9,6 +12,8 @@ struct fatfs_ctx {
struct fat_list _open_file_list;
struct fat_list _free_file_list;
void* udata;
struct proc* proc;
struct reschedule_ctx* rctx;
};
#endif // _KERNEL_FS_FATFS_CTX_H

View File

@@ -11,6 +11,8 @@
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <path_defs.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <status.h>
#include <sys/debug.h>
@@ -63,7 +65,8 @@ static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr, size_t max_size) {
return i;
}
int tarfs_mount (struct vfs_volume* volume, bool format) {
int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
bool format) {
(void)format;
struct tarfs* tarfs = malloc (sizeof (*tarfs));
@@ -107,7 +110,7 @@ int tarfs_mount (struct vfs_volume* volume, bool format) {
size_t sector_count = 1;
for (size_t sector = 0; sector < total_size / sector_size; sector++) {
uint8_t* dest = (uint8_t*)((uintptr_t)buffer + (sector * sector_size));
ret = device_op (back_device, XDRV_READ, NULL, NULL, &sector, &sector_count, dest);
ret = device_op (back_device, XDRV_READ, proc, rctx, &sector, &sector_count, dest);
}
spin_unlock (&back_device->lock);
@@ -126,12 +129,15 @@ int tarfs_mount (struct vfs_volume* volume, bool format) {
return ret;
}
int tarfs_format (struct vfs_volume* volume) {
(void)volume;
int tarfs_format (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
(void)volume, (void)proc, (void)rctx;
return ST_OK;
}
int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
int tarfs_describe (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct desc* desc) {
(void)proc, (void)rctx;
struct tarfs* tarfs = volume->udata;
if ((path[0] == '/') && (path[1] == '\0')) {
@@ -163,9 +169,9 @@ int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* de
}
}
int tarfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
(void)volume;
int tarfs_read_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size) {
(void)volume, (void)proc, (void)rctx;
const char* filename = path_basename (path);
@@ -185,8 +191,10 @@ int tarfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffe
return ST_OK;
}
int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num) {
int tarfs_read_dir_entry (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct dir_entry* entry, size_t entry_num) {
(void)proc, (void)rctx;
struct tarfs* tarfs = volume->udata;
if (strncmp (path, "/", PATH_MAX) != 0) {
@@ -214,23 +222,27 @@ int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct di
return -ST_DIR_NO_ENTRIES;
}
int tarfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size, uint32_t flags) {
(void)volume, (void)path, (void)buffer, (void)off, (void)size, (void)flags;
int tarfs_write_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags) {
(void)volume, (void)path, (void)buffer, (void)off;
(void)size, (void)flags, (void)proc, (void)rctx;
return ST_OK;
}
int tarfs_create_file (struct vfs_volume* volume, const char* path) {
(void)volume, (void)path;
int tarfs_create_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path) {
(void)volume, (void)path, (void)proc, (void)rctx;
return ST_OK;
}
int tarfs_create_dir (struct vfs_volume* volume, const char* path) {
(void)volume, (void)path;
int tarfs_create_dir (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path) {
(void)volume, (void)path, (void)proc, (void)rctx;
return ST_OK;
}
int tarfs_remove (struct vfs_volume* volume, const char* path) {
(void)volume, (void)path;
int tarfs_remove (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path) {
(void)volume, (void)path, (void)proc, (void)rctx;
return ST_OK;
}

View File

@@ -34,25 +34,30 @@ struct tarfs {
struct vfs_volume;
int tarfs_mount (struct vfs_volume* volume, bool format);
int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
bool format);
int tarfs_format (struct vfs_volume* volume);
int tarfs_format (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc);
int tarfs_describe (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct desc* desc);
int tarfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int tarfs_read_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size);
int tarfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size, uint32_t flags);
int tarfs_write_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags);
int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num);
int tarfs_read_dir_entry (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct dir_entry* entry, size_t entry_num);
int tarfs_create_file (struct vfs_volume* volume, const char* path);
int tarfs_create_file (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
int tarfs_create_dir (struct vfs_volume* volume, const char* path);
int tarfs_create_dir (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
int tarfs_remove (struct vfs_volume* volume, const char* path);
int tarfs_remove (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
#endif // _KERNEL_FS_TARFS_H

View File

@@ -37,7 +37,8 @@ static struct vfs_volume* vfs_find_volume (const char* volume) {
return hash_entry (found_link, struct vfs_volume, volume_table_link);
}
int vfs_create_volume (const char* key, int fs_type, struct device* back_device, bool format) {
int vfs_create_volume (struct proc* proc, struct reschedule_ctx* rctx, const char* key, int fs_type,
struct device* back_device, bool format) {
if (strlen_null (key) > VOLUME_MAX)
return -ST_OOB_ERROR;
@@ -92,7 +93,7 @@ int vfs_create_volume (const char* key, int fs_type, struct device* back_device,
return -ST_MOUNT_ERROR;
}
int ret = volume->driver_ops.mount (volume, format);
int ret = volume->driver_ops.mount (volume, proc, rctx, format);
if (ret < 0) {
free (volume);
@@ -125,13 +126,8 @@ int vfs_volume_open (struct proc* proc, const char* volume_name, struct reschedu
spin_unlock (&volume->lock);
return ST_OK;
} else {
if (proc == VFS_KERNEL) {
spin_unlock (&volume->lock);
return -ST_TRY_AGAIN;
} else {
proc_sq_suspend (proc, &volume->sq, &volume->lock, rctx);
return ST_OK;
}
proc_sq_suspend (proc, &volume->sq, &volume->lock, rctx);
return ST_OK;
}
}
@@ -175,7 +171,7 @@ int vfs_volume_close (struct proc* proc, const char* volume_name, struct resched
return ST_OK;
}
int vfs_format (struct proc* proc, const char* volume_name) {
int vfs_format (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -190,11 +186,11 @@ int vfs_format (struct proc* proc, const char* volume_name) {
spin_unlock (&volume->lock);
return volume->driver_ops.format (volume);
return volume->driver_ops.format (volume, proc, rctx);
}
int vfs_read_file (struct proc* proc, const char* volume_name, const char* path, uint8_t* buffer,
size_t off, size_t size) {
int vfs_read_file (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path, uint8_t* buffer, size_t off, size_t size) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -209,11 +205,11 @@ int vfs_read_file (struct proc* proc, const char* volume_name, const char* path,
spin_unlock (&volume->lock);
return volume->driver_ops.read_file (volume, path, buffer, off, size);
return volume->driver_ops.read_file (volume, proc, rctx, path, buffer, off, size);
}
int vfs_write_file (struct proc* proc, const char* volume_name, const char* path, uint8_t* buffer,
size_t off, size_t size, uint32_t flags) {
int vfs_write_file (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -228,10 +224,11 @@ int vfs_write_file (struct proc* proc, const char* volume_name, const char* path
spin_unlock (&volume->lock);
return volume->driver_ops.write_file (volume, path, buffer, off, size, flags);
return volume->driver_ops.write_file (volume, proc, rctx, path, buffer, off, size, flags);
}
int vfs_create_file (struct proc* proc, const char* volume_name, const char* path) {
int vfs_create_file (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -246,10 +243,11 @@ int vfs_create_file (struct proc* proc, const char* volume_name, const char* pat
spin_unlock (&volume->lock);
return volume->driver_ops.create_file (volume, path);
return volume->driver_ops.create_file (volume, proc, rctx, path);
}
int vfs_describe (struct proc* proc, const char* volume_name, const char* path, struct desc* desc) {
int vfs_describe (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path, struct desc* desc) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -264,11 +262,11 @@ int vfs_describe (struct proc* proc, const char* volume_name, const char* path,
spin_unlock (&volume->lock);
return volume->driver_ops.describe (volume, path, desc);
return volume->driver_ops.describe (volume, proc, rctx, path, desc);
}
int vfs_read_dir_entry (struct proc* proc, const char* volume_name, const char* path,
struct dir_entry* entry, size_t entry_num) {
int vfs_read_dir_entry (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path, struct dir_entry* entry, size_t entry_num) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -283,10 +281,11 @@ int vfs_read_dir_entry (struct proc* proc, const char* volume_name, const char*
spin_unlock (&volume->lock);
return volume->driver_ops.read_dir_entry (volume, path, entry, entry_num);
return volume->driver_ops.read_dir_entry (volume, proc, rctx, path, entry, entry_num);
}
int vfs_create_dir (struct proc* proc, const char* volume_name, const char* path) {
int vfs_create_dir (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -301,10 +300,11 @@ int vfs_create_dir (struct proc* proc, const char* volume_name, const char* path
spin_unlock (&volume->lock);
return volume->driver_ops.create_dir (volume, path);
return volume->driver_ops.create_dir (volume, proc, rctx, path);
}
int vfs_remove (struct proc* proc, const char* volume_name, const char* path) {
int vfs_remove (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (volume == NULL)
@@ -319,7 +319,7 @@ int vfs_remove (struct proc* proc, const char* volume_name, const char* path) {
spin_unlock (&volume->lock);
return volume->driver_ops.remove (volume, path);
return volume->driver_ops.remove (volume, proc, rctx, path);
}
void vfs_init (void) {

View File

@@ -16,8 +16,6 @@
#include <proc/suspension_q.h>
#include <sync/spin_lock.h>
#define VFS_KERNEL ((struct proc*)0x123)
struct vfs_volume;
struct vfs_volume {
@@ -29,26 +27,32 @@ struct vfs_volume {
bool locked;
struct proc_suspension_q sq;
struct {
int (*mount) (struct vfs_volume* volume, bool format);
int (*mount) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
bool format);
int (*format) (struct vfs_volume* volume);
int (*format) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int (*describe) (struct vfs_volume* volume, const char* path, struct desc* desc);
int (*describe) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, struct desc* desc);
int (*read_file) (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int (*read_file) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size);
int (*write_file) (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size, uint32_t flags);
int (*write_file) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags);
int (*read_dir_entry) (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
int (*read_dir_entry) (struct vfs_volume* volume, struct proc* proc,
struct reschedule_ctx* rctx, const char* path, struct dir_entry* entry,
size_t entry_num);
int (*create_file) (struct vfs_volume* volume, const char* path);
int (*create_file) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
int (*create_dir) (struct vfs_volume* volume, const char* path);
int (*create_dir) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
int (*remove) (struct vfs_volume* volume, const char* path);
int (*remove) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx,
const char* path);
} driver_ops;
struct device* back_device;
void* udata
@@ -59,30 +63,35 @@ struct vfs_volume_table {
spin_lock_t lock;
};
int vfs_create_volume (const char* key, int fs_type, struct device* back_device, bool format);
int vfs_create_volume (struct proc* proc, struct reschedule_ctx* rctx, const char* key, int fs_type,
struct device* back_device, bool format);
int vfs_volume_open (struct proc* proc, const char* volume, struct reschedule_ctx* rctx);
int vfs_volume_close (struct proc* proc, const char* volume, struct reschedule_ctx* rctx);
int vfs_format (struct proc* proc, const char* volume_name);
int vfs_format (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name);
int vfs_read_file (struct proc* proc, const char* volume, const char* path, uint8_t* buffer,
size_t off, size_t size);
int vfs_read_file (struct proc* proc, struct reschedule_ctx* rctx, const char* volume,
const char* path, uint8_t* buffer, size_t off, size_t size);
int vfs_write_file (struct proc* proc, const char* volume, const char* path, uint8_t* buffer,
size_t off, size_t size, uint32_t flags);
int vfs_write_file (struct proc* proc, struct reschedule_ctx* rctx, const char* volume,
const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags);
int vfs_describe (struct proc* proc, const char* volume, const char* path, struct desc* desc);
int vfs_describe (struct proc* proc, struct reschedule_ctx* rctx, const char* volume,
const char* path, struct desc* desc);
int vfs_read_dir_entry (struct proc* proc, const char* volume, const char* path,
struct dir_entry* entry, size_t entry_num);
int vfs_read_dir_entry (struct proc* proc, struct reschedule_ctx* rctx, const char* volume,
const char* path, struct dir_entry* entry, size_t entry_num);
int vfs_create_file (struct proc* proc, const char* volume_name, const char* path);
int vfs_create_file (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path);
int vfs_create_dir (struct proc* proc, const char* volume_name, const char* path);
int vfs_create_dir (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path);
int vfs_remove (struct proc* proc, const char* volume_name, const char* path);
int vfs_remove (struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name,
const char* path);
void vfs_init (void);

View File

@@ -43,6 +43,27 @@ void proc_free_pid (int pid) { id_free (&pid_alloc, pid); }
void proc_pid_alloc_init (void) { id_alloc_init (&pid_alloc, PIDS_MAX); }
struct proc* kproc_create (void) {
struct proc* kproc = malloc (sizeof (*kproc));
memset (kproc, 0, sizeof (*kproc));
kproc->lock = SPIN_LOCK_INIT;
kproc->flags |= PROC_KPROC;
kproc->pid = proc_alloc_pid ();
kproc->procgroup = procgroup_create ();
procgroup_attach (kproc->procgroup, kproc);
kproc->exec_pid = -1;
struct reschedule_ctx rctx;
memset (&rctx, 0, sizeof (rctx));
proc_register (kproc, NULL, &rctx);
return kproc;
}
static bool proc_check_elf (uint8_t* elf) {
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
return false;
@@ -116,19 +137,38 @@ struct proc* proc_from_file (struct proc* proc1, const char* volume, const char*
struct desc desc;
int ret;
for (;;) {
ret = vfs_volume_open (proc1, volume, rctx);
spin_lock (&proc1->lock);
bool is_kproc = (proc1->flags & PROC_KPROC) != 0;
spin_unlock (&proc1->lock);
if (ret < 0) {
if (ret == -ST_TRY_AGAIN)
continue;
else
if (is_kproc) {
for (;;) {
ret = vfs_volume_open (proc1, volume, rctx);
if (ret < 0)
return NULL;
} else
break;
spin_lock (&proc1->lock);
if (ret == ST_OK && proc1->state != PROC_SUSPENDED) {
spin_unlock (&proc1->lock);
break;
}
for (;;) {
spin_lock (&proc1->lock);
if (proc1->state != PROC_SUSPENDED) {
spin_unlock (&proc1->lock);
break;
}
spin_unlock (&proc1->lock);
}
}
}
if ((ret = vfs_describe (proc1, volume, path, &desc)) < 0) {
if ((ret = vfs_describe (proc1, rctx, volume, path, &desc)) < 0) {
vfs_volume_close (proc1, volume, rctx);
return NULL;
}
@@ -145,7 +185,7 @@ struct proc* proc_from_file (struct proc* proc1, const char* volume, const char*
return NULL;
}
if ((ret = vfs_read_file (proc1, volume, path, temp_buffer, 0, desc.size)) < 0) {
if ((ret = vfs_read_file (proc1, rctx, volume, path, temp_buffer, 0, desc.size)) < 0) {
free (temp_buffer);
vfs_volume_close (proc1, volume, rctx);
return NULL;
@@ -221,7 +261,7 @@ static struct proc* proc_find_sched (struct cpu* cpu) {
int state = proc->state;
if (state == PROC_READY) {
if (state == PROC_READY && !(proc->flags & PROC_KPROC)) {
spin_unlock (&proc->lock);
return proc;
}
@@ -256,6 +296,12 @@ void proc_sched (void) {
void proc_kill (struct proc* proc, struct reschedule_ctx* rctx) {
spin_lock (&proc->lock);
if ((proc->flags & PROC_KPROC)) {
spin_unlock (&proc->lock);
return;
}
struct cpu* cpu = proc->cpu;
spin_unlock (&proc->lock);
@@ -313,10 +359,10 @@ void proc_init (void) {
struct reschedule_ctx rctx;
memset (&rctx, 0, sizeof (rctx));
struct proc* spin_proc = proc_from_file (VFS_KERNEL, "RD", "/spin", &rctx);
struct proc* spin_proc = proc_from_file (thiscpu->kproc, "RD", "/spin", &rctx);
proc_register (spin_proc, thiscpu, &rctx);
struct proc* init = proc_from_file (VFS_KERNEL, "RD", "/init", &rctx);
struct proc* init = proc_from_file (thiscpu->kproc, "RD", "/init", &rctx);
init->procgroup->capabilities |= (PROC_CAP_TERMINAL | PROC_CAP_KB);
proc_register (init, thiscpu, &rctx);

View File

@@ -25,6 +25,7 @@
/* process flags */
#define PROC_USTK_PREALLOC (1 << 0)
#define PROC_KPROC (1 << 1)
struct cpu;
struct reschedule_ctx;
@@ -72,4 +73,6 @@ void proc_wait_for (struct proc* proc, struct reschedule_ctx* rctx, struct proc*
void proc_init (void);
struct proc* kproc_create (void);
#endif // _KERNEL_PROC_PROC_H

View File

@@ -5,19 +5,20 @@
#include <proc/reschedule.h>
#include <proc/resource.h>
#include <proc/suspension_q.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/smp.h>
#include <sys/spin_lock.h>
void proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock,
struct reschedule_ctx* rctx) {
int proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock,
struct reschedule_ctx* rctx) {
struct cpu* cpu = proc->cpu;
struct proc_sq_entry* sq_entry = malloc (sizeof (*sq_entry));
if (!sq_entry) {
if (resource_lock != NULL)
spin_unlock (resource_lock);
return;
return -ST_OOM_ERROR;
}
sq_entry->proc = proc;
@@ -45,16 +46,19 @@ void proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock
cpu->proc_current = NULL;
proc->cpu = NULL;
int state = proc->state;
spin_unlock (&sq->lock);
spin_unlock (&proc->lock);
spin_unlock (&cpu->lock);
rctx_insert_cpu (rctx, cpu);
return state;
}
void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
struct reschedule_ctx* rctx) {
int proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
struct reschedule_ctx* rctx) {
struct cpu* cpu = cpu_find_lightest ();
struct proc_suspension_q* sq = sq_entry->sq;
@@ -76,6 +80,8 @@ void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
cpu->proc_run_q_count++;
int state = proc->state;
spin_unlock (&sq->lock);
spin_unlock (&proc->lock);
spin_unlock (&cpu->lock);
@@ -83,6 +89,8 @@ void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
free (sq_entry);
rctx_insert_cpu (rctx, cpu);
return state;
}
void proc_sqs_cleanup (struct proc* proc) {

View File

@@ -22,10 +22,9 @@ struct proc_sq_entry {
void proc_sqs_cleanup (struct proc* proc);
void proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock,
struct reschedule_ctx* rctx);
void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry,
int proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock,
struct reschedule_ctx* rctx);
int proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry, struct reschedule_ctx* rctx);
#endif // _KERNEL_PROC_SUSPENTION_Q_H

View File

@@ -395,7 +395,7 @@ DEFINE_SYSCALL (sys_read_file) {
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
spin_lock (&proc->lock);
int ret = vfs_read_file (proc, proc->cwv, path, buffer, off, size);
int ret = vfs_read_file (proc, rctx, proc->cwv, path, buffer, off, size);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -425,7 +425,7 @@ DEFINE_SYSCALL (sys_describe) {
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
spin_lock (&proc->lock);
int ret = vfs_describe (proc, proc->cwv, path, desc);
int ret = vfs_describe (proc, rctx, proc->cwv, path, desc);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -479,7 +479,7 @@ DEFINE_SYSCALL (sys_read_dir_entry) {
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
spin_lock (&proc->lock);
int ret = vfs_read_dir_entry (proc, proc->cwv, path, entry, entry_num);
int ret = vfs_read_dir_entry (proc, rctx, proc->cwv, path, entry, entry_num);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -503,7 +503,7 @@ DEFINE_SYSCALL (sys_create_file) {
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
spin_lock (&proc->lock);
int ret = vfs_create_file (proc, proc->cwv, path);
int ret = vfs_create_file (proc, rctx, proc->cwv, path);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -536,7 +536,7 @@ DEFINE_SYSCALL (sys_write_file) {
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
spin_lock (&proc->lock);
int ret = vfs_write_file (proc, proc->cwv, path, buffer, off, size, flags);
int ret = vfs_write_file (proc, rctx, proc->cwv, path, buffer, off, size, flags);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -616,7 +616,7 @@ DEFINE_SYSCALL (sys_create_dir) {
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
spin_lock (&proc->lock);
int ret = vfs_create_dir (proc, proc->cwv, path);
int ret = vfs_create_dir (proc, rctx, proc->cwv, path);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -640,7 +640,7 @@ DEFINE_SYSCALL (sys_remove) {
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
spin_lock (&proc->lock);
int ret = vfs_remove (proc, proc->cwv, path);
int ret = vfs_remove (proc, rctx, proc->cwv, path);
spin_unlock (&proc->lock);
return SYSRESULT (ret);
@@ -683,7 +683,7 @@ DEFINE_SYSCALL (sys_create_volume) {
if (device == NULL)
return SYSRESULT (-ST_NOT_FOUND);
return SYSRESULT (vfs_create_volume (key, type, device, false));
return SYSRESULT (vfs_create_volume (proc, rctx, key, type, device, false));
}
static syscall_handler_func_t handler_table[] = {

View File

@@ -1,6 +1,8 @@
#ifndef _LIBDEBUGCONSOLE_DEBUGCONSOLE_H
#define _LIBDEBUGCONSOLE_DEBUGCONSOLE_H
#include <stddef.h>
#define DEBUG_PRINTF_MAX (16 * 1024)
int debugconsole_print (const char* string, size_t len);