diff options
author | Ian Moffett <ian@osmora.org> | 2025-10-15 19:02:52 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-10-15 19:04:01 -0400 |
commit | 810b5c49ebfb3398a8d9a89dedba6b97cb8688b1 (patch) | |
tree | e95d4dc0b10f2552a654eefab0ee7c8f933aaeb7 /src/sys | |
parent | c3817dc16c1deab64756e4a9fcf8f7be91aaa12f (diff) |
kern/amd64: Introduce kernel threads
Introduces kernel threads. by using the proc_ktd() function, one can
spawn a CPL 0 running thread to the desired code.
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/arch/amd64/os/os_proc.c | 10 | ||||
-rw-r--r-- | src/sys/include/sys/proc.h | 12 | ||||
-rw-r--r-- | src/sys/os/os_proc.c | 30 |
3 files changed, 50 insertions, 2 deletions
diff --git a/src/sys/arch/amd64/os/os_proc.c b/src/sys/arch/amd64/os/os_proc.c index 93e5d73..c658547 100644 --- a/src/sys/arch/amd64/os/os_proc.c +++ b/src/sys/arch/amd64/os/os_proc.c @@ -112,8 +112,14 @@ md_proc_init(struct proc *procp, int flags) return error; } - ds = USER_DS | 3; - cs = USER_CS | 3; + if (ISSET(flags, SPAWN_KTD)) { + ds = KERNEL_DS; + cs = KERNEL_CS; + procp->flags |= PROC_KTD; + } else { + ds = USER_DS | 3; + cs = USER_CS | 3; + } /* * Set up the mapping specifier, we'll use zero diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index 618ddf3..f23c0b0 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -101,6 +101,10 @@ struct proc { #define PROC_EXITING BIT(0) /* Process is exiting */ #define PROC_SLEEPING BIT(1) /* Process is sleeping */ +#define PROC_KTD BIT(2) /* Process is kernel thread */ + +/* Flags for PROC_SPAWN */ +#define SPAWN_KTD BIT(0) /* Spawn kernel thread */ /* * Initialize a process into a basic minimal @@ -136,6 +140,14 @@ struct proc *proc_self(void); int proc_add_range(struct proc *procp, vaddr_t va, paddr_t pa, size_t len); /* + * Spawn a kernel thread + * + * @procp_res: Result is written here + * @fn: Function where kernel thread should end up + */ +int proc_ktd(struct proc **procp_res, void(*fn)(void *)); + +/* * Kill a process with a specific status code * * @procp: Process to kill diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c index 582c077..a8f49d7 100644 --- a/src/sys/os/os_proc.c +++ b/src/sys/os/os_proc.c @@ -393,6 +393,36 @@ proc_spawn(const char *path, struct penv_blk *envbp) return proc->pid; } +int +proc_ktd(struct proc **procp_res, void(*fn)(void *)) +{ + struct proc *proc; + struct pcore *core; + + if (procp_res == NULL || fn == NULL) { + return -EINVAL; + } + + proc = kalloc(sizeof(*proc)); + if (proc == NULL) { + return -ENOMEM; + } + + core = cpu_sched(); + if (core == NULL) { + kfree(proc); + return -EIO; + } + + proc_init(proc, SPAWN_KTD); + md_set_ip(proc, (uintptr_t)fn); + sched_enq(&core->scq, proc); + + *procp_res = proc; + TAILQ_INSERT_TAIL(&procq, proc, lup_link); + return 0; +} + /* * ARG0: Pathname to spawn * ARG1: Process environment block |