Manage int IDs via id_alloc
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m31s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m31s
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user