Compare commits

...
Author SHA1 Message Date
5a67532500 utest: rename utest to init
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 01:13:18 -04:00
11bedfe3a8 sched: simplify entry-exit path between processes
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 01:09:27 -04:00
308ba36aab tls: track cpu_id
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 00:33:02 -04:00
3a3e57eb9c main: manually call apic init
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 00:10:58 -04:00
5b977d592a apic: add apic to initcall
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 00:08:05 -04:00
b926d50365 fork: properly set up child process
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 00:06:57 -04:00
ec4e9fc27d amd64: clarify sys_fork code path
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-21 21:31:11 -04:00
19 changed files with 145 additions and 66 deletions

View file

@ -60,6 +60,7 @@ void arch_ap_startup()
u64 id = atomic_xadd(&cpu_ticker, 1);
struct cpu_local *this_local = &cpu_local_arr[id];
this_local->cpu_id = id;
wrmsr(IA32_GS_BASE, (u64)this_local);
tss_init(id, this_local);

View file

@ -88,9 +88,11 @@ irq_common:
swapgs
1:
push_all /* pushes 15 registers */
mov gs:[28], rsp /* save RSP after push_all for scheduler */
mov rdi, [rsp + 120] /* irq no */
call irq_handler
irq_epilogue:
pop_all
test qword ptr [rsp + 24], 3 /* from userspace? */
jz 2f
@ -99,15 +101,10 @@ irq_common:
add rsp, 8
iretq
/* Called by switch_to's ret when a thread is first scheduled.
* RSP points at user_rsp (set up by sys_fork / sched_init).
* rcx and r11 already set from pop_all (rcx=entry, r11=rflags).
*/
.global sched_start
sched_start:
pop rsp
swapgs
sysretq
.global sched_restore_state
sched_restore_state:
mov rsp, [rdi + 8] /* load from thread->kstack_ptr */
jmp irq_epilogue
.global _syscall_enter
_syscall_enter:
@ -131,14 +128,7 @@ _syscall_enter:
swapgs
sysretq
.global switch_to
switch_to:
push_all
mov [rdi + 8], rsp /* 8: struct thread stack_addr offset */
mov rsp, [rsi + 8]
pop_all
ret
/* return path after a process was execed */
.global _enter_userspace_init
_enter_userspace_init:
mov rcx, rdi
@ -163,6 +153,14 @@ _enter_userspace_init:
sysretq
/* set as entrypoint for a new process that was just forked */
.global fork_trampoline
fork_trampoline:
mov rsp, rdx
swapgs
sysretq
/* exceptions */
exception 0x00
exception 0x01

View file

@ -1,7 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/mem.h>
#include <kernel/common.h>
#include <kernel/proc.h>
#include <kernel/sched.h>
#include <drivers/apic.h>
#include <arch/msr.h>
#include <arch/proc.h>
#include <stdint.h>
@ -68,6 +72,15 @@ uint8_t lapic_idno()
return lapic_read(lapic_addr, LAPIC_ID) >> 24;
}
static void timer_handler()
{
struct thread *cur = get_cur_thread();
sched_save_state(cur);
sched_add_thread(cur);
lapic_eoi();
schedule();
}
void apic_init()
{
ioapic_addr += hhdm_start;
@ -90,6 +103,15 @@ void apic_init()
lapic_eoi();
/* Register timer interrupt handler and enable APIC timer */
extern int irq_map(uint8_t irq, void *func);
irq_map(0, timer_handler);
lapic_write(lapic_addr, LAPIC_TPR, 0);
lapic_write(lapic_addr, LAPIC_TIMER_DIV, 0x3);
lapic_write(lapic_addr, LAPIC_TIMER_INIT_COUNT, 0xFFFF0);
lapic_write(lapic_addr, LAPIC_TIMER, 0x20 | 0x20000);
krdy("APIC initialized\n");
}

View file

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/proc.h>
#include <kernel/atomic.h>
#include <kernel/common.h>
#include <kernel/slab.h>
#include <lib/string.h>
@ -46,8 +47,8 @@ void gdt_init()
void tss_init(int cpu_id, struct cpu_local *local)
{
int gdt_idx = 9 + cpu_id * 2;
uint16_t sel = gdt_idx * 8;
int idx = atomic_xadd(&gdt_index, 2);
uint16_t sel = idx * 8;
char *tss_stack = kalloc(TSS_STACK_SIZE, ALLOC_KERN);
if (!tss_stack)
@ -59,15 +60,15 @@ void tss_init(int cpu_id, struct cpu_local *local)
tss->rsp0 = (u64)tss_stack + TSS_STACK_SIZE;
uint64_t addr = (uint64_t)tss;
struct gdt_desc_sys *desc = (struct gdt_desc_sys *)&__gdt[gdt_idx];
struct gdt_desc_sys *desc = (struct gdt_desc_sys *)&__gdt[idx];
desc->limit = sizeof(struct tss) - 1;
desc->base = addr & 0xFFFF;
desc->base = addr & 0xFFFF;
desc->base_2 = (addr >> 16) & 0xFF;
desc->base_3 = (addr >> 24) & 0xFF;
desc->base_4 = addr >> 32;
desc->access = 0x89;
desc->flags = 0x00;
desc->flags = 0x00;
desc->reserved = 0;
ltr(sel);

View file

@ -12,6 +12,7 @@ struct cpu_local {
vaddr_t kernel_rsp;
vaddr_t user_rsp;
u32 cpu_id;
vaddr_t irq_rsp;
struct tss tss;
} PACKED;
@ -39,5 +40,6 @@ struct proc *get_cur_proc();
struct thread *get_cur_thread();
void enter_userspace_init(struct thread *thread, uaddr_t entry);
struct pt_regs *current_pt_regs();
u32 cpu_id();
#endif /* _ARCH_PROC_H_ */

View file

@ -30,6 +30,16 @@ struct proc *get_cur_proc()
return t->proc;
}
u32 cpu_id()
{
return tls_get()->cpu_id;
}
void sched_save_state(struct thread *t)
{
t->kstack_ptr = tls_get()->irq_rsp;
}
/* the thread that calls this will be lost, so it should only be called
* on the initial process, and while doing an exec
*/
@ -39,19 +49,20 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry)
tls->cur_thread = thread;
void *kstack = kalloc(PAGE_SIZE, ALLOC_KERN);
tls->kernel_rsp = (vaddr_t)kstack + PAGE_SIZE;
tls->tss.rsp0 = tls->kernel_rsp;
void *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
thread->kstack_top = (vaddr_t)kstack + KSTACK_SIZE;
tls->kernel_rsp = thread->kstack_top;
tls->tss.rsp0 = thread->kstack_top;
pgd_load(thread->proc->pgd);
thread->state = PROC_STATE_RUNNING;
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top);
_enter_userspace_init(entry, 0x202, thread->stack_addr);
_enter_userspace_init(entry, 0x202, thread->ustack_top);
}
extern void sched_start(void);
void fork_trampoline();
pid_t sys_fork()
{
@ -68,13 +79,18 @@ pid_t sys_fork()
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
child->kstack_top = ktop;
/* Copy parent's register state, then:
* rax = 0 (child fork return value)
* rdx = parent_r->rsp (user stack for fork_trampoline's mov rsp, rdx)
* ret_addr = fork_trampoline (does mov rsp, rdx; swapgs; sysretq)
*/
struct pt_regs *parent_r = current_pt_regs();
u64 *sp = (u64 *)(ktop - 128);
sp[0] = 0;
memcpy(&sp[1], &parent_r->rbx, 14 * sizeof(u64));
*(u64 *)(ktop - 8) = (u64)sched_start;
*(u64 *)(ktop) = parent_r->rsp;
child->stack_addr = (uaddr_t)sp;
struct pt_regs *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
memcpy(child_r, parent_r, sizeof(struct pt_regs) - sizeof(u64));
child_r->rax = 0;
child_r->rdx = parent_r->rsp;
*(u64 *)(ktop - 8) = (u64)fork_trampoline;
child->kstack_ptr = (uaddr_t)child_r;
child->state = PROC_STATE_RUNNING;
sched_add_thread(child);

View file

@ -21,6 +21,12 @@ struct timespec get_realtime()
return ret;
}
time_t unix_time()
{
u64 sec = (rdtsc() - timekeeper.boot_tsc) / timekeeper.tsc_hz;
return timekeeper.boot_unix_seconds + sec;
}
void time_init()
{
timekeeper.tsc_hz = tsc_calibrate();

View file

@ -40,7 +40,8 @@ struct proc;
struct thread {
tid_t tid;
uaddr_t stack_addr;
uaddr_t kstack_ptr; /* kernel RSP save slot for sched_restore_state (offset 8) */
uaddr_t ustack_top; /* user stack top */
size_t stack_size;
u64 state;
@ -49,7 +50,7 @@ struct thread {
struct proc *proc;
struct thread *next;
uaddr_t kstack_top;
uaddr_t kstack_top; /* top of kernel stack allocation */
};
struct proc {
@ -84,7 +85,6 @@ void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags);
ssize_t proc_load_file(const char *file, struct proc *proc);
struct proc *proc_create(struct thread *parent);
void proc_free(struct proc *proc);
void switch_to(struct thread *from, struct thread *to);
void proc_init();
pid_t sys_fork();
void schedule();

View file

@ -5,5 +5,8 @@
#include <kernel/proc.h>
void sched_add_thread(struct thread *t);
void schedule();
void sched_save_state(struct thread *t);
void sched_restore_state(struct thread *t);
#endif /* _SCHED_H_ */

View file

@ -5,11 +5,12 @@
#include <kernel/types.h>
struct timespec {
i64 tv_sec;
time_t tv_sec;
i64 tv_nsec;
};
time_t arch_get_real_time();
time_t unix_time();
u64 tsc_calibrate();
u64 rdtsc();
struct timespec get_realtime();

View file

@ -20,9 +20,8 @@
#include <kernel/tty.h>
#include <drivers/serial.h>
#include <drivers/pci.h>
#include <drivers/acpi.h>
#include <drivers/apic.h>
#include <drivers/acpi.h>
#include <fs/vfs.h>
#include <fs/inode.h>
@ -93,9 +92,10 @@ static void kmain()
do_initcalls();
arch_ap_startup();
apic_init();
dirent_t *init;
res = vfs_open("/bin/utest", &init);
res = vfs_open("/bin/init", &init);
if (res < 0)
panic("failed to open /bin/utest: %s\n", strerror(-res));

View file

@ -71,6 +71,13 @@ static int copy_parent(struct proc *new, struct proc *parent)
spinlock_release(&parent->mem_lock);
if (parent->fd_array_size) {
spinlock_acquire(&parent->fd_lock);
new->fd_array_size = parent->fd_array_size;
new->fdt = kalloc(new->fd_array_size * sizeof(struct file), ALLOC_KERN);
spinlock_release(&parent->fd_lock);
}
return 0;
}
@ -89,7 +96,7 @@ struct proc *proc_create(struct thread *parent)
}
r_mt->stack_size = USTACK_DEFAULT_SIZE;
r_mt->stack_addr = 0x7FFFFFFFF000;
r_mt->ustack_top = 0x7FFFFFFFF000;
r_mt->proc = ret;
r_mt->state = PROC_STATE_DNS;
@ -109,7 +116,7 @@ struct proc *proc_create(struct thread *parent)
return NULL;
}
proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW | PAGE_USER);
proc_mmap(ret, TO_PHYS(ustack), r_mt->ustack_top - r_mt->stack_size, r_mt->stack_size, PAGE_RW | PAGE_USER);
ret->main_thread = r_mt;

View file

@ -9,50 +9,70 @@ static struct {
struct thread *head;
struct thread *tail;
spinlock_t lock;
} _run_queue;
} sched_queue;
void sched_add_thread(struct thread *t)
{
spinlock_acquire(&_run_queue.lock);
spinlock_acquire(&sched_queue.lock);
t->next = NULL;
if (_run_queue.tail)
_run_queue.tail->next = t;
_run_queue.tail = t;
if (!_run_queue.head)
_run_queue.head = t;
spinlock_release(&_run_queue.lock);
if (sched_queue.tail)
sched_queue.tail->next = t;
sched_queue.tail = t;
if (!sched_queue.head)
sched_queue.head = t;
spinlock_release(&sched_queue.lock);
}
void sched_purge_proc(struct proc *proc)
{
spinlock_acquire(&sched_queue.lock);
struct thread *t = sched_queue.head;
struct thread *p = NULL;
while (t) {
if (t->proc == proc) {
if (p)
p->next = t->next;
else
sched_queue.head = t->next;
if (!t->next)
sched_queue.tail = p;
t->next = NULL;
} else {
p = t;
}
t = t->next;
}
spinlock_release(&sched_queue.lock);
}
static struct thread *dequeue_thread()
{
spinlock_acquire(&_run_queue.lock);
struct thread *t = _run_queue.head;
spinlock_acquire(&sched_queue.lock);
struct thread *t = sched_queue.head;
if (t) {
_run_queue.head = t->next;
if (!_run_queue.head)
_run_queue.tail = NULL;
sched_queue.head = t->next;
if (!sched_queue.head)
sched_queue.tail = NULL;
t->next = NULL;
}
spinlock_release(&_run_queue.lock);
spinlock_release(&sched_queue.lock);
return t;
}
void schedule()
{
struct thread *cur = get_cur_thread();
struct thread *next = dequeue_thread();
if (!next)
return;
struct thread *prev = cur;
if (cur)
sched_add_thread(cur);
struct cpu_local *tls = tls_get();
tls->cur_thread = next;
tls->kernel_rsp = next->kstack_top;
tls->tss.rsp0 = next->kstack_top;
pgd_load(next->proc->pgd);
switch_to(prev, next);
sched_restore_state(next);
}

View file

@ -4,7 +4,7 @@ else
Q :=
endif
usr_dirs := utest
usr_dirs := init
usr_bins := $(foreach d,$(usr_dirs),$d/$d)
.PHONY: all $(usr_dirs)

View file

@ -10,6 +10,8 @@ USR_CFLAGS ?= -g3 -O0 -pipe -Wall -ffreestanding -nostdlib \
USR_LDFLAGS ?= -nostdlib -static -m elf_x86_64 -z max-page-size=0x1000
DEPS := $(UOBJ:.o=.d)
$(OUT): $(UOBJ)
$(Q)echo " LD [U] $(DIR)/$@"
$(Q)$(LD) $(USR_LDFLAGS) -o $@ $^
@ -23,7 +25,7 @@ $(OUT): $(UOBJ)
$(Q)$(CC) $(USR_CFLAGS) -c $< -o $@
clean:
$(Q)rm -rf $(UOBJ) $(OUT)
$(Q)rm -rf $(UOBJ) $(OUT) $(DEPS)
print-%:
$(Q)echo $($(*))

View file

@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
OUT := utest
UOBJ := utest.o syscall.o
OUT := init
UOBJ := init.o syscall.o
-include ../Makefile.common