From 0e3e85d8a8379794cff591da4bc701ba98a35d65 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 16 Sep 2025 20:53:43 -0400 Subject: kern: proc: Add initialization code for ring 3 This commit introduces several new functions for switching the processor into a ring three context along with its task's respective process descriptors to be implemented correctly. - Add md_set_ip() to update proc instruction pointer - Add stack definitions - Add stack mapping logic ... Signed-off-by: Ian Moffett --- src/sys/arch/amd64/os/os_proc.c | 86 +++++++++++++++++++++++++++++++++++++++++ src/sys/include/sys/proc.h | 18 +++++++++ 2 files changed, 104 insertions(+) diff --git a/src/sys/arch/amd64/os/os_proc.c b/src/sys/arch/amd64/os/os_proc.c index 8fad7b6..61ef51e 100644 --- a/src/sys/arch/amd64/os/os_proc.c +++ b/src/sys/arch/amd64/os/os_proc.c @@ -30,8 +30,44 @@ #include #include #include +#include #include +#include #include +#include +#include +#include + +/* + * Put the current process into userland for its first + * run. The catch is that we have never been in userland + * from this context so we'll have to fake an IRET frame + * to force the processor to have a CPL of 3. + * + * @tfp: Trapframe of context to enter + */ +static inline void +__proc_kick(struct trapframe *tfp) +{ + __ASMV( + "sti\n" + "mov %0, %%rax\n" + "push %1\n" + "push %2\n" + "push %3\n" + "push %%rax\n" + "push %4\n" + "lfence\n" + "swapgs\n" + "iretq" + : + : "r" (tfp->cs), + "i" (USER_DS | 3), + "r" (tfp->rsp), + "m" (tfp->rflags), + "r" (tfp->rip) + ); +} /* * MD proc init code @@ -40,6 +76,9 @@ int md_proc_init(struct proc *procp, int flags) { struct md_pcb *pcbp; + struct trapframe *tfp; + struct mmu_map spec; + uint8_t cs, ds; int error; if (procp == NULL) { @@ -51,5 +90,52 @@ md_proc_init(struct proc *procp, int flags) printf("md_proc_init: could not create new vas\n"); return error; } + + ds = USER_DS | 3; + cs = USER_CS | 3; + + /* + * Set up the mapping specifier, we'll use zero + * to allocate new pages. + */ + spec.pa = 0; + spec.va = STACK_TOP; + + /* Put the trapframe in a known state */ + tfp = &pcbp->tf; + memset(tfp, 0, sizeof(*tfp)); + tfp->rflags = 0x202; + tfp->cs = cs; + tfp->ss = ds; + + /* Map the stack */ + vm_map( + &pcbp->vas, &spec, + STACK_LEN, + PROT_READ + | PROT_WRITE + | PROT_USER + ); + + tfp->rsp = STACK_TOP; + return 0; +} + +/* + * Set process instruction pointer + */ +int +md_set_ip(struct proc *procp, uintptr_t ip) +{ + struct md_pcb *pcbp; + struct trapframe *tfp; + + if (procp == NULL) { + return -EINVAL; + } + + pcbp = &procp->pcb; + tfp = &pcbp->tf; + tfp->rip = ip; return 0; } diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index deed09b..d09e459 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -34,6 +34,12 @@ #include #include /* standard */ +/* + * The stack starts here and grows down + */ +#define STACK_TOP 0xBFFFFFFF +#define STACK_LEN 4096 + /* * A process describes a running program image * on the system. @@ -69,4 +75,16 @@ int proc_init(struct proc *procp, int flags); */ int md_proc_init(struct proc *procp, int flags); +/* + * Set the instruction pointer field of a specific + * process + * + * @procp: Process to update + * @ip: Instruction pointer to write + * + * Returns zero on success, otherwise a less than + * zero value to indicate failure. + */ +int md_set_ip(struct proc *procp, uintptr_t ip); + #endif /* !_SYS_PROC_H_ */ -- cgit v1.2.3 From 0bb64ec846b1708b6cba138a215be7e1ff2517a5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 16 Sep 2025 20:58:14 -0400 Subject: readme: Fix typo Signed-off-by: Ian Moffett --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4eee8a4..16328ae 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - Printf-style kernel logging - Video console - I/O APIC -- Local APIC timer +- Local APIC timer(s) - Processor local APIC(s) - 16550 UART I/O - i8254 -- cgit v1.2.3 From f399a21cc734851b33f5469831ff7b77ff5729de Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 16 Sep 2025 21:02:54 -0400 Subject: reamde: Add credit Signed-off-by: Ian Moffett --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 16328ae..f0edb18 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # L5Lunos - a clean, modern Solaris +Written mostly solo and completely from scratch, not Linux based nor does it +share any existing operating system sources. + ## Current status **Under heavy development** @@ -17,3 +20,9 @@ - 16550 UART I/O - i8254 - i8259 + +## License + +This project is licensed under the BSD 3 clause + +SPDX identifier: BSD-3-Clause -- cgit v1.2.3