Compare commits

...

2 Commits

Author SHA1 Message Date
084809ac99 Manage int IDs via id_alloc
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m31s
2026-02-22 20:40:12 +01:00
8fc5418915 XDRV_READ reading in sectors 2026-02-22 18:58:28 +01:00
17 changed files with 238 additions and 97 deletions

View File

@@ -64,6 +64,9 @@ void bootmain (void) {
spin ();
}
proc_pid_alloc_init ();
procgroup_pgid_alloc_init ();
smp_init ();
proc_init ();

View File

@@ -17,8 +17,6 @@
#include <sys/debug.h>
#include <sys/proc.h>
static atomic_int pids = 0;
struct proc* proc_from_elf (uint8_t* elf_contents) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
@@ -30,7 +28,12 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
proc->lock = SPIN_LOCK_INIT;
proc->state = PROC_READY;
proc->pid = atomic_fetch_add (&pids, 1);
proc->pid = proc_alloc_pid ();
if (proc->pid < 0) {
free (proc);
return NULL;
}
proc->procgroup = procgroup_create ();
if (proc->procgroup == NULL) {
@@ -72,7 +75,12 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, uintptr_t ent
proc->lock = SPIN_LOCK_INIT;
proc->state = PROC_READY;
proc->pid = atomic_fetch_add (&pids, 1);
proc->pid = proc_alloc_pid ();
if (proc->pid < 0) {
free (proc);
return NULL;
}
spin_lock (&proto->lock);
@@ -108,6 +116,7 @@ void proc_cleanup (struct proc* proc, struct reschedule_ctx* rctx) {
procgroup_detach (proc->procgroup, proc, rctx);
proc_free_pid (proc->pid);
/* clean the process */
free (proc);
}

View File

@@ -2,6 +2,7 @@
#include <device/ramdrv.h>
#include <device/terminal.h>
#include <kb_device.h>
#include <libk/align.h>
#include <libk/fieldlengthof.h>
#include <libk/rbtree.h>
#include <libk/std.h>

View File

@@ -1,9 +1,11 @@
#include <device/device.h>
#include <device/ramdrv.h>
#include <libk/align.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <status.h>
#include <sys/debug.h>
#include <xdrv_device.h>
bool ramdrv_init (struct device* device, void* arg) {
@@ -14,23 +16,21 @@ bool ramdrv_init (struct device* device, void* arg) {
if (ramdrv == NULL)
return false;
ramdrv->total_size = init->total_size;
ramdrv->sector_size = init->sector_size;
ramdrv->total_size = align_up (init->total_size, ramdrv->sector_size);
if (init->buffer == NULL) {
ramdrv->buffer = malloc (init->total_size);
ramdrv->buffer = malloc (ramdrv->total_size);
if (ramdrv->buffer == NULL) {
free (ramdrv);
return false;
}
ramdrv->managed = false;
} else {
ramdrv->buffer = init->buffer;
ramdrv->managed = true;
if (ramdrv->buffer == NULL) {
free (ramdrv);
return false;
}
memset (ramdrv->buffer, 0, ramdrv->total_size);
if (init->buffer != NULL)
memcpy (ramdrv->buffer, init->buffer, init->total_size);
device->udata = ramdrv;
return true;
@@ -39,8 +39,8 @@ bool ramdrv_init (struct device* device, void* arg) {
void ramdrv_fini (struct device* device) {
struct ramdrv* ramdrv = device->udata;
if (!ramdrv->managed)
free (ramdrv);
free (ramdrv->buffer);
free (ramdrv);
}
int ramdrv_get_device_type (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
@@ -91,19 +91,16 @@ int ramdrv_get_sector_size (struct device* device, struct proc* proc, struct res
int ramdrv_read (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
void* a2, void* a3, void* a4) {
(void)a4;
if (a1 == NULL || a2 == NULL || a3 == NULL)
if (a1 == NULL || a2 == NULL || a3 == NULL || a4 == NULL)
return -ST_BAD_ADDRESS_SPACE;
size_t pos = *(size_t*)a1;
size_t size = *(size_t*)a2;
uint8_t* buffer = a3;
size_t sector = *(size_t*)a1;
size_t off = *(size_t*)a2;
size_t size = *(size_t*)a3;
uint8_t* buffer = a4;
struct ramdrv* ramdrv = device->udata;
if (pos + size > ramdrv->total_size)
return -ST_OOB_ERROR;
size_t pos = sector * ramdrv->sector_size + off;
memcpy (buffer, (void*)(((uintptr_t)ramdrv->buffer) + pos), size);

View File

@@ -19,7 +19,6 @@ struct ramdrv {
size_t total_size;
size_t sector_size;
uint8_t* buffer;
bool managed;
};
bool ramdrv_init (struct device* device, void* arg);

View File

@@ -66,7 +66,7 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
mountpoint->udata = tarfs;
struct device* back_device = mountpoint->back_device;
size_t total_size;
size_t total_size, sector_size;
int ret;
spin_lock (&back_device->lock);
@@ -78,6 +78,14 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
return ret;
}
ret = back_device->ops[XDRV_GET_SECTOR_SIZE](back_device, proc, rctx, &sector_size, NULL, NULL,
NULL);
if (ret < 0) {
spin_unlock (&back_device->lock);
free (mountpoint->udata);
return ret;
}
uint8_t* buffer = malloc (total_size);
if (buffer == NULL) {
@@ -86,8 +94,11 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
return ret;
}
size_t pos = 0;
ret = back_device->ops[XDRV_READ](back_device, proc, rctx, &pos, &total_size, buffer, NULL);
size_t off = 0;
for (size_t sector = 0; sector < total_size / sector_size; sector++) {
uint8_t* dest = (uint8_t*)((uintptr_t)buffer + (sector * sector_size));
ret = back_device->ops[XDRV_READ](back_device, proc, rctx, &sector, &off, &sector_size, dest);
}
spin_unlock (&back_device->lock);

View File

@@ -1,6 +1,7 @@
#include <device/device.h>
#include <fs/tarfs.h>
#include <fs/vfs.h>
#include <id/id_alloc.h>
#include <libk/fieldsizeof.h>
#include <libk/hash.h>
#include <libk/lengthof.h>
@@ -97,7 +98,7 @@ int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* p
spin_lock (&procgroup->lock);
int id = handle->id = procgroup->sys_vfs_handles++;
int id = handle->id = id_alloc (&procgroup->vfs_handle_id_alloc);
rbtree_insert (struct vfs_handle, &procgroup->vfs_handle_tree, &handle->handle_tree_link,
handle_tree_link, id);
@@ -119,8 +120,13 @@ int vfs_close (struct procgroup* procgroup, int id) {
return -ST_NOT_FOUND;
}
spin_lock (&handle->lock);
rbtree_delete (&procgroup->vfs_handle_tree, &handle->handle_tree_link);
id_free (&procgroup->vfs_handle_id_alloc, handle->id);
spin_unlock (&handle->lock);
spin_unlock (&procgroup->lock);
free (handle);
@@ -191,7 +197,6 @@ int vfs_kernel_describe (const char* mountpoint, const char* path, struct desc*
}
void vfs_procgroup_cleanup (struct procgroup* procgroup) {
struct list_node_link* handle_cleanup_list = NULL;
struct vfs_handle* handle;
struct rb_node_link* node;
@@ -202,14 +207,8 @@ void vfs_procgroup_cleanup (struct procgroup* procgroup) {
rbtree_next (node, next);
handle = rbtree_entry (node, struct vfs_handle, handle_tree_link);
node = next;
list_append (handle_cleanup_list, &handle->handle_cleanup_link);
}
struct list_node_link *cleanup_link, *cleanup_tmp_link;
list_foreach (handle_cleanup_list, cleanup_link, cleanup_tmp_link) {
handle = list_entry (cleanup_link, struct vfs_handle, handle_cleanup_link);
list_remove (handle_cleanup_list, &handle->handle_cleanup_link);
free (handle);
vfs_close (procgroup, handle->id);
}
}

View File

@@ -23,7 +23,6 @@ struct vfs_handle {
char path[MAX_PATH];
struct procgroup* ownerpg;
struct rb_node_link handle_tree_link;
struct list_node_link handle_cleanup_link;
spin_lock_t lock;
};

1
kernel/id/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.o

52
kernel/id/id_alloc.c Normal file
View File

@@ -0,0 +1,52 @@
#include <id/id_alloc.h>
#include <libk/bm.h>
#include <libk/std.h>
#include <mm/liballoc.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
bool id_alloc_init (struct id_alloc* ida, size_t nbits) {
size_t buffer_size = (nbits + 7) / 8;
uint8_t* buffer = malloc (buffer_size);
if (buffer == NULL)
return false;
bm_init (&ida->bm, buffer, nbits);
ida->lock = SPIN_LOCK_INIT;
return true;
}
void id_alloc_fini (struct id_alloc* ida) {
free (ida->bm.base);
ida->bm.base = NULL;
ida->bm.nbits = 0;
}
int id_alloc (struct id_alloc* ida) {
spin_lock (&ida->lock);
for (size_t bit = 0; bit < ida->bm.nbits; bit++) {
if (!bm_test (&ida->bm, bit)) {
bm_set (&ida->bm, bit);
spin_unlock (&ida->lock);
return (int)bit;
}
}
spin_unlock (&ida->lock);
return -ST_OOM_ERROR;
}
void id_free (struct id_alloc* ida, int id) {
if (id < 0)
return;
spin_lock (&ida->lock);
bm_clear (&ida->bm, (size_t)id);
spin_unlock (&ida->lock);
}

21
kernel/id/id_alloc.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef _KERNEL_ID_ID_ALLOC_H
#define _KERNEL_ID_ID_ALLOC_H
#include <libk/bm.h>
#include <libk/std.h>
#include <sync/spin_lock.h>
struct id_alloc {
struct bm bm;
spin_lock_t lock;
};
int id_alloc (struct id_alloc* ida);
void id_free (struct id_alloc* ida, int id);
bool id_alloc_init (struct id_alloc* ida, size_t nbits);
void id_alloc_fini (struct id_alloc* ida);
#endif // _KERNEL_ID_ID_ALLOC_H

3
kernel/id/src.mk Normal file
View File

@@ -0,0 +1,3 @@
c += id/id_alloc.c
o += id/id_alloc.o

View File

@@ -9,6 +9,7 @@ include syscall/src.mk
include fs/src.mk
include device/src.mk
include Flanterm/src.mk
include id/src.mk
ifeq ($(PLATFORM_ACPI),1)
include uACPI/src.mk

View File

@@ -2,6 +2,7 @@
#include <aux/elf.h>
#include <desc.h>
#include <fs/vfs.h>
#include <id/id_alloc.h>
#include <irq/irq.h>
#include <libk/align.h>
#include <libk/list.h>
@@ -29,6 +30,8 @@
#include <amd64/intr_defs.h>
#endif
#define PIDS_MAX 1024
#define SCHED_REAP_FREQ 10
static struct rb_node_link* proc_tree = NULL;
@@ -36,6 +39,14 @@ static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
static atomic_int sched_cycles = 0;
static struct id_alloc pid_alloc;
int proc_alloc_pid (void) { return id_alloc (&pid_alloc); }
void proc_free_pid (int pid) { id_free (&pid_alloc, pid); }
void proc_pid_alloc_init (void) { id_alloc_init (&pid_alloc, PIDS_MAX); }
static bool proc_check_elf (uint8_t* elf) {
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
return false;

View File

@@ -59,6 +59,12 @@ struct proc* proc_find_pid (int pid);
struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint, const char* path);
void proc_free_pid (int pid);
int proc_alloc_pid (void);
void proc_pid_alloc_init (void);
void proc_init (void);
#endif // _KERNEL_PROC_PROC_H

View File

@@ -1,4 +1,5 @@
#include <fs/vfs.h>
#include <id/id_alloc.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
@@ -11,9 +12,14 @@
#include <sys/debug.h>
#include <sys/mm.h>
#define PGIDS_MAX 1024
static struct rb_node_link* procgroup_tree = NULL;
static spin_lock_t procgroup_tree_lock = SPIN_LOCK_INIT;
static atomic_int pgids = 0;
static struct id_alloc pgid_alloc;
void procgroup_pgid_alloc_init (void) { id_alloc_init (&pgid_alloc, PGIDS_MAX); }
struct procgroup* procgroup_find (int pgid) {
struct procgroup* procgroup = NULL;
@@ -145,9 +151,20 @@ struct procgroup* procgroup_create (void) {
memset (procgroup, 0, sizeof (*procgroup));
if (!id_alloc_init (&procgroup->vfs_handle_id_alloc, PROCGROUP_VFS_HANDLES_MAX)) {
free (procgroup);
return NULL;
}
procgroup->pgid = id_alloc (&pgid_alloc);
if (procgroup->pgid < 0) {
free (procgroup);
return NULL;
}
procgroup->memb_proc_tree = NULL;
procgroup->lock = SPIN_LOCK_INIT;
procgroup->pgid = atomic_fetch_add (&pgids, 1);
procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys ();
procgroup->map_base = PROC_MAP_BASE;
@@ -175,6 +192,64 @@ void procgroup_attach (struct procgroup* procgroup, struct proc* proc) {
spin_unlock (&procgroup->lock);
}
static void procgroup_delete (struct procgroup* procgroup, struct reschedule_ctx* rctx) {
spin_lock (&procgroup_tree_lock);
spin_lock (&procgroup->lock);
rbtree_delete (&procgroup_tree, &procgroup->procgroup_tree_link);
spin_unlock (&procgroup->lock);
spin_unlock (&procgroup_tree_lock);
struct list_node_link* resource_delete_list = NULL;
/* delete resources */
struct rb_node_link* rnode;
rbtree_first (&procgroup->resource_tree, rnode);
while (rnode) {
struct rb_node_link* next;
rbtree_next (rnode, next);
struct proc_resource* resource = rbtree_entry (rnode, struct proc_resource, resource_tree_link);
rnode = next;
list_append (resource_delete_list, &resource->delete_list_link);
}
struct list_node_link *resource_delete_link, *resource_delete_tmp_link;
list_foreach (resource_delete_list, resource_delete_link, resource_delete_tmp_link) {
struct proc_resource* resource =
list_entry (resource_delete_link, struct proc_resource, delete_list_link);
list_remove (resource_delete_list, &resource->delete_list_link);
proc_delete_resource (resource, rctx);
}
/* unlock VFS owned mountpoints */
vfs_procgroup_cleanup (procgroup);
/* delete mappings */
struct list_node_link *mapping_link, *mapping_link_tmp;
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
struct proc_mapping* mapping =
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
pmm_free (mapping->paddr, mapping->size / PAGE_SIZE);
free (mapping);
}
pmm_free (procgroup->pd.cr3_paddr, 1);
free (procgroup->tls.tls_tmpl);
id_alloc_fini (&procgroup->vfs_handle_id_alloc);
id_free (&pgid_alloc, procgroup->pgid);
free (procgroup);
}
void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
struct reschedule_ctx* rctx) {
spin_lock (&procgroup->lock);
@@ -187,58 +262,6 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
spin_unlock (&procgroup->lock);
if (memb_tree == NULL) {
spin_lock (&procgroup_tree_lock);
spin_lock (&procgroup->lock);
rbtree_delete (&procgroup_tree, &procgroup->procgroup_tree_link);
spin_unlock (&procgroup->lock);
spin_unlock (&procgroup_tree_lock);
struct list_node_link* resource_delete_list = NULL;
/* delete resources */
struct rb_node_link* rnode;
rbtree_first (&procgroup->resource_tree, rnode);
while (rnode) {
struct rb_node_link* next;
rbtree_next (rnode, next);
struct proc_resource* resource =
rbtree_entry (rnode, struct proc_resource, resource_tree_link);
rnode = next;
list_append (resource_delete_list, &resource->delete_list_link);
}
struct list_node_link *resource_delete_link, *resource_delete_tmp_link;
list_foreach (resource_delete_list, resource_delete_link, resource_delete_tmp_link) {
struct proc_resource* resource =
list_entry (resource_delete_link, struct proc_resource, delete_list_link);
list_remove (resource_delete_list, &resource->delete_list_link);
proc_delete_resource (resource, rctx);
}
/* unlock VFS owned mountpoints */
vfs_procgroup_cleanup (procgroup);
/* delete mappings */
struct list_node_link *mapping_link, *mapping_link_tmp;
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
struct proc_mapping* mapping =
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
pmm_free (mapping->paddr, mapping->size / PAGE_SIZE);
free (mapping);
}
pmm_free (procgroup->pd.cr3_paddr, 1);
free (procgroup->tls.tls_tmpl);
free (procgroup);
procgroup_delete (procgroup, rctx);
}
}

View File

@@ -1,6 +1,7 @@
#ifndef _KERNEL_PROC_PROCGROUP_H
#define _KERNEL_PROC_PROCGROUP_H
#include <id/id_alloc.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
@@ -9,6 +10,8 @@
#include <sys/mm.h>
#include <sys/procgroup.h>
#define PROCGROUP_VFS_HANDLES_MAX 256
struct proc;
struct reschedule_ctx;
@@ -32,7 +35,7 @@ struct procgroup {
struct procgroup_tls tls;
uint64_t capabilities;
struct rb_node_link* vfs_handle_tree;
int sys_vfs_handles;
struct id_alloc vfs_handle_id_alloc;
};
struct procgroup* procgroup_create (void);
@@ -48,4 +51,6 @@ bool procgroup_unmap (struct procgroup* procgroup, uintptr_t start_vaddr, size_t
struct procgroup* procgroup_find (int pgid);
void procgroup_pgid_alloc_init (void);
#endif // _KERNEL_PROC_PROCGROUP_H