usr: implement user process switching
This is a rather large commit, so the gist of it is this: User context switching is now functional, after a change to the TSS, and a few minor details in the scheduler and loader. ELF parsing is rather primitive, but basically functional, as long as it only loads code. The interrupt 0x80 is for syscalls, and will switch from user mode to kernel mode. It is currently the only interrupt that can be called from user mode This adds the syscall functionality, which currently uses a red-black tree to store the function pointers, although when the table is more flushed out, it will probably be replaced with an array The pager now correctly marks the parent nodes with attributes as it allocates them. Without doing this, the page table entries will never be reached in lower privilege modes, causing page faults Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
d60da85470
commit
37518aea81
19 changed files with 262 additions and 69 deletions
13
dev/pic.c
13
dev/pic.c
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/pio.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <dev/pic.h>
|
||||
|
||||
void pic_mask(uint8_t irq, uint8_t i)
|
||||
|
|
@ -34,6 +35,18 @@ void pic_eoi(uint8_t irq)
|
|||
void exception(int vector, int error)
|
||||
{
|
||||
kprintf(LOG_WARN "EXCEPTION: %x, error code: %d\n", vector, error);
|
||||
pid_t pid = getpid();
|
||||
|
||||
struct proc *proc = proc_find(pid);
|
||||
|
||||
if (proc->is_kernel) {
|
||||
kprintf(LOG_ERROR "Kernel process %d crashed\n", pid);
|
||||
panic();
|
||||
} else {
|
||||
proc_term(pid);
|
||||
kprintf(LOG_WARN "Process %d terminated\n", pid);
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
void pic_init()
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
|
||||
typedef uint32_t ino_t;
|
||||
typedef uint8_t ftype_t;
|
||||
typedef size_t off_t;
|
||||
|
||||
struct vnode {
|
||||
char name[256];
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@
|
|||
#define GDT_ACCESS_CODE_RING3 0b11111010
|
||||
|
||||
#define GDT_SEGMENT_CODE_RING0 0x28
|
||||
#define GDT_SEGMENT_DATA_RING0 0x30
|
||||
|
||||
#define GDT_SEGMENT_CODE_RING3 0x38
|
||||
#define GDT_SEGMENT_DATA_RING3 0x40
|
||||
|
||||
#ifndef __ASM__
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
#define GATE_INTR 0x8E
|
||||
#define GATE_TRAP 0x8F
|
||||
|
||||
#define GATE_INTR_DPL3 0xEE
|
||||
|
||||
#define IDT_GATE_SIZE 16
|
||||
#define IDT_NUM_VECTORS 256
|
||||
#define IDT_SIZE (IDT_GATE_SIZE * IDT_NUM_VECTORS)
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
genrw(rsp);
|
||||
genrw(rbp);
|
||||
genrw(cr3);
|
||||
genrw(ds);
|
||||
|
||||
#undef genrw
|
||||
|
||||
|
|
|
|||
|
|
@ -55,6 +55,10 @@ struct proc {
|
|||
struct procregs *proc_current_regs();
|
||||
struct proc *proc_create();
|
||||
struct proc *proc_get(pid_t pid);
|
||||
pid_t getpid();
|
||||
void proc_term(pid_t pid);
|
||||
struct proc *proc_find(pid_t pid);
|
||||
void proc_set_current(pid_t pid);
|
||||
void proc_init(unsigned num_cpus);
|
||||
|
||||
#endif /* _PROC_H_ */
|
||||
|
|
|
|||
|
|
@ -281,6 +281,7 @@ int kcmd_exec(int argc, char **argv)
|
|||
|
||||
proc->state = PROC_RUNNING;
|
||||
void _return_to_user(struct procregs *regs, paddr_t cr3);
|
||||
proc_set_current(pid);
|
||||
_return_to_user(&proc->regs, proc->cr3);
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -34,13 +34,13 @@ uint16_t gdt_insert_tss(uintptr_t rsp0)
|
|||
|
||||
struct gdt_desc_sys *desc = (struct gdt_desc_sys *)((struct gdt_desc *)__gdt + gdt_index);
|
||||
|
||||
paddr_t tss_addr = (paddr_t)(tss_table + tss_index) & (~hhdm_start);
|
||||
paddr_t tss_addr = (paddr_t)(tss_table + tss_index);
|
||||
desc->limit = sizeof(struct tss) - 1;
|
||||
desc->base = tss_addr & 0xFFFF;
|
||||
desc->base = tss_addr & 0xFFFFFF;
|
||||
desc->base_2 = (tss_addr >> 16) & 0xFF;
|
||||
desc->base_3 = (tss_addr >> 24) & 0xFF;
|
||||
desc->base_4 = (tss_addr >> 32) & 0xFFFFFFFF;
|
||||
desc->access = 0x89;
|
||||
desc->access = 0xE9;
|
||||
desc->flags = 0x00;
|
||||
|
||||
tss_index += 1;
|
||||
|
|
|
|||
10
init/irq.c
10
init/irq.c
|
|
@ -16,6 +16,8 @@
|
|||
static bool irq_mapped[16] = { false };
|
||||
static void *irq_handlers[16] = { NULL };
|
||||
|
||||
void gate_syscall();
|
||||
|
||||
exception_decl(0x00);
|
||||
exception_decl(0x01);
|
||||
exception_decl(0x02);
|
||||
|
|
@ -119,12 +121,6 @@ void irq(int irq)
|
|||
}
|
||||
}
|
||||
|
||||
void gate_syscall(uint64_t rdi)
|
||||
{
|
||||
kprintf("System call: %X\n", rdi);
|
||||
yield();
|
||||
}
|
||||
|
||||
void idt_init()
|
||||
{
|
||||
idt_insert(0x00, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x00));
|
||||
|
|
@ -177,7 +173,7 @@ void idt_init()
|
|||
idt_insert(0x2E, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0E));
|
||||
idt_insert(0x2F, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0F));
|
||||
|
||||
idt_insert(0x80, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, gate_syscall);
|
||||
idt_insert(0x80, GATE_INTR_DPL3, 0, GDT_SEGMENT_CODE_RING0, gate_syscall);
|
||||
|
||||
irq_map(4, serial_trap);
|
||||
|
||||
|
|
|
|||
17
init/main.c
17
init/main.c
|
|
@ -41,20 +41,17 @@ uintptr_t bss_end;
|
|||
|
||||
void gdt_load();
|
||||
void idt_load();
|
||||
void yield();
|
||||
void ap_kmain(struct limine_smp_info *info);
|
||||
void load_stack_and_park(uintptr_t rsp, uintptr_t rbp);
|
||||
|
||||
uintptr_t kstacks[256] = { 0 };
|
||||
|
||||
void prompt()
|
||||
{
|
||||
kprintf(COL_YELLOW_BLACK "vbos # " COL_RESET);
|
||||
}
|
||||
|
||||
void yield()
|
||||
{
|
||||
for (;;) {
|
||||
__asm__("hlt");
|
||||
}
|
||||
}
|
||||
|
||||
void panic()
|
||||
{
|
||||
|
|
@ -159,7 +156,7 @@ void kmain()
|
|||
char *root_path = kcmdline_get_symbol("root");
|
||||
if (!root_path) {
|
||||
kprintf(LOG_ERROR "No root= parameter specified\n");
|
||||
|
||||
|
||||
kerror_print_blkdevs();
|
||||
panic();
|
||||
} else {
|
||||
|
|
@ -184,7 +181,6 @@ void kmain()
|
|||
|
||||
/* init the application processors */
|
||||
struct limine_smp_response *smp_resp = smp_req.response;
|
||||
proc_init(smp_resp->cpu_count);
|
||||
for (unsigned i = 0; i < smp_resp->cpu_count; i++) {
|
||||
struct limine_smp_info *info = smp_resp->cpus[i];
|
||||
|
||||
|
|
@ -193,6 +189,9 @@ void kmain()
|
|||
|
||||
info->goto_address = ap_kmain;
|
||||
}
|
||||
proc_init(smp_resp->cpu_count);
|
||||
void syscall_init();
|
||||
syscall_init();
|
||||
|
||||
/* wait for the application processors to finish their initialization */
|
||||
for (unsigned i = 0; i < 100000; i++)
|
||||
|
|
@ -220,6 +219,8 @@ void ap_kmain(struct limine_smp_info *info)
|
|||
void *kstack = buddy_alloc(KSTACK_SIZE);
|
||||
uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8;
|
||||
|
||||
kstacks[info->lapic_id] = (uintptr_t)ptr;
|
||||
|
||||
/* insert the TSS for each processor into the GDT */
|
||||
#ifdef KDEBUG
|
||||
kprintf(LOG_DEBUG "Inserting TSS for AP #%d\n", info->lapic_id);
|
||||
|
|
|
|||
24
mem/page.c
24
mem/page.c
|
|
@ -17,13 +17,15 @@ static void *kmap_alloc_page()
|
|||
return slab_alloc(page_slab);
|
||||
}
|
||||
|
||||
static uint64_t *pagemap_traverse(uint64_t *this_level, size_t next_num)
|
||||
static uint64_t *pagemap_traverse(uint64_t *this_level, size_t next_num, uint64_t attr)
|
||||
{
|
||||
uint64_t *r;
|
||||
if (!(this_level[next_num] & 1)) {
|
||||
if (!attr)
|
||||
return NULL;
|
||||
r = alloc_page();
|
||||
memset(r, 0, 4096);
|
||||
this_level[next_num] = ((uintptr_t)r | 3) & ~hhdm_start;
|
||||
this_level[next_num] = (((uintptr_t)r | 3) & ~hhdm_start) | attr;
|
||||
} else {
|
||||
r = (void *)((this_level[next_num] & -4096ull) | hhdm_start);
|
||||
}
|
||||
|
|
@ -33,7 +35,8 @@ static uint64_t *pagemap_traverse(uint64_t *this_level, size_t next_num)
|
|||
uintptr_t mmap_find_unmapped(struct rbtree *tree, spinlock_t *lock, uintptr_t start, size_t len)
|
||||
{
|
||||
len = MAX(len, 0x1000);
|
||||
len = npow2(len);
|
||||
len = len | 0xFFF;
|
||||
len += 1;
|
||||
|
||||
spinlock_acquire(lock);
|
||||
if (tree == NULL) {
|
||||
|
|
@ -114,9 +117,9 @@ void mmap(uintptr_t pml4, struct rbtree *tree, paddr_t paddr, uintptr_t vaddr, s
|
|||
pdn = (vaddr >> 30) & 0x1FF;
|
||||
pdpn = (vaddr >> 39) & 0x1FF;
|
||||
|
||||
pdpt = pagemap_traverse((uint64_t *)pml4, pdpn);
|
||||
pdt = pagemap_traverse(pdpt, pdn);
|
||||
pt = pagemap_traverse(pdt, ptn);
|
||||
pdpt = pagemap_traverse((uint64_t *)pml4, pdpn, attr);
|
||||
pdt = pagemap_traverse(pdpt, pdn, attr);
|
||||
pt = pagemap_traverse(pdt, ptn, attr);
|
||||
pt[pn] = paddr | attr;
|
||||
}
|
||||
|
||||
|
|
@ -143,7 +146,7 @@ void *proc_mmap(struct proc *proc, paddr_t paddr, uintptr_t vaddr, size_t len, u
|
|||
}
|
||||
|
||||
spinlock_acquire(&proc->page_map_lock);
|
||||
mmap(proc->cr3 | hhdm_start, &proc->page_map, paddr, vaddr, len, attr | PAGE_USER);
|
||||
mmap(proc->cr3 | hhdm_start, &proc->page_map, paddr, vaddr, len, attr);
|
||||
spinlock_release(&proc->page_map_lock);
|
||||
|
||||
return (void *)vaddr;
|
||||
|
|
@ -219,9 +222,9 @@ void kunmap(uintptr_t vaddr)
|
|||
pdn = (vaddr >> 30) & 0x1FF;
|
||||
pdpn = (vaddr >> 39) & 0x1FF;
|
||||
|
||||
pdpt = pagemap_traverse((uint64_t *)pml4, pdpn);
|
||||
pdt = pagemap_traverse(pdpt, pdn);
|
||||
pt = pagemap_traverse(pdt, ptn);
|
||||
pdpt = pagemap_traverse((uint64_t *)pml4, pdpn, 0);
|
||||
pdt = pagemap_traverse(pdpt, pdn, 0);
|
||||
pt = pagemap_traverse(pdt, ptn, 0);
|
||||
pt[pn] = 0;
|
||||
|
||||
if (!kunmap_check_table(pt, pdt, ptn))
|
||||
|
|
@ -250,7 +253,6 @@ void page_init(paddr_t kpaddr, uintptr_t kvaddr, size_t kernel_size, struct mem_
|
|||
alloc_page = kmap_alloc_page;
|
||||
pml4 = NULL;
|
||||
|
||||
/* do not invalidate TLB entries for kernel pages */
|
||||
uint64_t attrs = PAGE_PRESENT | PAGE_RW;
|
||||
kmap(kpaddr, kvaddr, kernel_size, attrs);
|
||||
|
||||
|
|
|
|||
11
mem/reg.S
11
mem/reg.S
|
|
@ -10,15 +10,26 @@ reg##_write: \
|
|||
movq %rdi, %##reg; \
|
||||
ret;
|
||||
|
||||
#define genrww(reg) \
|
||||
.global reg##_read; \
|
||||
reg##_read: \
|
||||
movw %##reg, %ax; \
|
||||
ret; \
|
||||
.global reg##_write; \
|
||||
reg##_write: \
|
||||
movw %di, %##reg; \
|
||||
ret;
|
||||
|
||||
genrw(rsp)
|
||||
genrw(rbp)
|
||||
genrw(cr3)
|
||||
genrww(ds)
|
||||
|
||||
.global load_stack_and_park
|
||||
load_stack_and_park:
|
||||
movq %rdi, %rsp
|
||||
movq %rsi, %rbp
|
||||
|
||||
__load_stack_and_park_no_escape:
|
||||
call yield
|
||||
jmp __load_stack_and_park_no_escape
|
||||
|
|
|
|||
|
|
@ -190,6 +190,70 @@ irq 0x0D
|
|||
irq 0x0E
|
||||
irq 0x0F
|
||||
|
||||
.global gate_syscall
|
||||
gate_syscall:
|
||||
cli
|
||||
/* push scratch registers */
|
||||
pushq %rax
|
||||
pushq %rcx
|
||||
pushq %rdx
|
||||
pushq %rsi
|
||||
pushq %rdi
|
||||
pushq %r8
|
||||
pushq %r9
|
||||
pushq %r10
|
||||
pushq %r11
|
||||
|
||||
/* get current task registers */
|
||||
call proc_current_regs
|
||||
|
||||
/* save current task registers */
|
||||
popq %rdi
|
||||
movq %rdi, 80(%rax) /* r11 */
|
||||
popq %rdi
|
||||
movq %rdi, 72(%rax) /* r10 */
|
||||
popq %rdi
|
||||
movq %rdi, 64(%rax) /* r9 */
|
||||
popq %rdi
|
||||
movq %rdi, 56(%rax) /* r8 */
|
||||
popq %rdi
|
||||
movq %rdi, 40(%rax) /* rdi */
|
||||
popq %rdi
|
||||
movq %rdi, 32(%rax) /* rsi */
|
||||
popq %rdi
|
||||
movq %rdi, 24(%rax) /* rdx */
|
||||
popq %rdi
|
||||
movq %rdi, 16(%rax) /* rcx */
|
||||
popq %rdi
|
||||
movq %rdi, 0(%rax) /* rax */
|
||||
|
||||
/* save remaining registers */
|
||||
movq %rbx, 8(%rax)
|
||||
movq %rbp, 48(%rax)
|
||||
movq %r12, 88(%rax)
|
||||
movq %r13, 96(%rax)
|
||||
movq %r14, 104(%rax)
|
||||
movq %r15, 112(%rax)
|
||||
|
||||
/* save IRETQ frame */
|
||||
popq %rdi
|
||||
movq %rdi, 120(%rax)
|
||||
popq %rdi
|
||||
movq %rdi, 128(%rax)
|
||||
popq %rdi
|
||||
movq %rdi, 136(%rax)
|
||||
popq %rdi
|
||||
movq %rdi, 144(%rax)
|
||||
popq %rdi
|
||||
movq %rdi, 152(%rax)
|
||||
|
||||
movq 0(%rax), %rdi /* syscall number */
|
||||
|
||||
movq 40(%rax), %rsi /* arguments */
|
||||
call syscall
|
||||
|
||||
call schedule
|
||||
|
||||
/* void return_from_irq(struct procregs *regs, paddr_t cr3); */
|
||||
.global _return_to_user
|
||||
_return_to_user:
|
||||
|
|
@ -224,3 +288,7 @@ _return_to_user:
|
|||
|
||||
iretq
|
||||
|
||||
.global yield
|
||||
yield:
|
||||
hlt
|
||||
jmp yield
|
||||
|
|
|
|||
46
sched/load.c
46
sched/load.c
|
|
@ -66,42 +66,42 @@ pid_t elf_load_proc(char *fname)
|
|||
|
||||
struct proc *proc = proc_create();
|
||||
/* allocate page tables */
|
||||
proc->cr3 = (uintptr_t)buddy_alloc(0x1000) & (~hhdm_start);
|
||||
proc->cr3 = (uintptr_t)buddy_alloc(0x1000);
|
||||
|
||||
/* map kernel pages */
|
||||
memcpy((void *)proc->cr3, (void *)(kcr3 | hhdm_start), 0x1000);
|
||||
|
||||
proc->cr3 &= (~hhdm_start);
|
||||
|
||||
/* map text segment */
|
||||
struct elf64_program_header *phdr = (struct elf64_program_header *)(buf + hdr->e_phoff);
|
||||
for (int i = 0; i < hdr->e_phnum; i++) {
|
||||
if (phdr[i].p_type == PT_LOAD) {
|
||||
/* allocate memory for segment */
|
||||
size_t psize = MAX(0x1000, npow2(phdr[i].p_memsz));
|
||||
uintptr_t prog_buf = (uintptr_t)buddy_alloc(psize);
|
||||
void *addr = proc_mmap(proc, prog_buf & (~hhdm_start), phdr[i].p_vaddr, psize, PAGE_RW | PAGE_PRESENT);
|
||||
if (!addr) {
|
||||
kfree(buf);
|
||||
return -ENOMEM;
|
||||
}
|
||||
for (unsigned int i = 0; i < hdr->e_phnum; i++) {
|
||||
if (phdr[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
/* copy data from file to memory */
|
||||
memcpy((void *)prog_buf, buf + phdr[i].p_offset, phdr[i].p_filesz);
|
||||
|
||||
/* zero out remaining memory */
|
||||
memset((void *)prog_buf + phdr[i].p_filesz, 0, phdr[i].p_memsz - phdr[i].p_filesz);
|
||||
}
|
||||
uintptr_t vaddr = phdr[i].p_vaddr;
|
||||
size_t len = MAX(0x1000, npow2(phdr[i].p_filesz));
|
||||
uintptr_t paddr = (uintptr_t)buddy_alloc(len);
|
||||
(void)proc_mmap(proc, paddr & (~hhdm_start), vaddr, len, PAGE_RW | PAGE_PRESENT | PAGE_USER);
|
||||
memcpy((void *)paddr, buf + phdr[i].p_offset, len);
|
||||
}
|
||||
|
||||
/* set entry point */
|
||||
proc->regs.rip = hdr->e_entry;
|
||||
proc->regs.rflags = 0x202; /* interrupts enabled, IOPL = 0 */
|
||||
proc->regs.rflags = 0x242;
|
||||
|
||||
/* descriptor #7 is the user code segment */
|
||||
proc->regs.cs = 7 * sizeof(struct gdt_desc) | 0x3;
|
||||
proc->regs.ss = 8 * sizeof(struct gdt_desc) | 0x3;
|
||||
proc->regs.cs = 0x38 | 3;
|
||||
proc->regs.ss = 0x40 | 3;
|
||||
ds_write(0x40 | 3);
|
||||
|
||||
/* allocate user stack */
|
||||
uintptr_t stack_buf = (uintptr_t)buddy_alloc(0x1000);
|
||||
uintptr_t u_stack_addr = 0x202020202000;
|
||||
(void)proc_mmap(proc, stack_buf & (~hhdm_start), u_stack_addr, 0x1000, PAGE_RW | PAGE_PRESENT);
|
||||
uintptr_t u_stack_addr = 0x20002000;
|
||||
(void)proc_mmap(proc, stack_buf & (~hhdm_start), u_stack_addr, 0x1000, PAGE_RW | PAGE_PRESENT | PAGE_USER);
|
||||
|
||||
/* set stack pointer */
|
||||
proc->regs.rsp = u_stack_addr + 0x1000;
|
||||
proc->regs.rsp = u_stack_addr + 0xFF0;
|
||||
|
||||
kfree(buf);
|
||||
|
||||
|
|
|
|||
52
sched/proc.c
52
sched/proc.c
|
|
@ -8,10 +8,16 @@
|
|||
static struct rbtree *proc_tree;
|
||||
static pid_t *proc_current;
|
||||
|
||||
static struct proc kernel_procs[256] = { 0 };
|
||||
|
||||
void _return_to_user(struct procregs *regs, paddr_t cr3);
|
||||
extern uintptr_t kstacks[256];
|
||||
|
||||
struct procregs *proc_current_regs()
|
||||
{
|
||||
if (proc_current[lapic_idno()] == 0)
|
||||
return &kernel_procs[lapic_idno()].regs;
|
||||
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, proc_current[lapic_idno()]);
|
||||
|
||||
if (proc_node) {
|
||||
|
|
@ -23,8 +29,16 @@ struct procregs *proc_current_regs()
|
|||
}
|
||||
}
|
||||
|
||||
pid_t getpid()
|
||||
{
|
||||
return proc_current[lapic_idno()];
|
||||
}
|
||||
|
||||
struct proc *proc_find(pid_t pid)
|
||||
{
|
||||
if (pid == 0)
|
||||
return &kernel_procs[lapic_idno()];
|
||||
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||
|
||||
if (proc_node)
|
||||
|
|
@ -33,6 +47,26 @@ struct proc *proc_find(pid_t pid)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
void proc_set_current(pid_t pid)
|
||||
{
|
||||
proc_current[lapic_idno()] = pid;
|
||||
}
|
||||
|
||||
void proc_term(pid_t pid)
|
||||
{
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||
|
||||
if (proc_node) {
|
||||
struct proc *proc = (void *)proc_node->value;
|
||||
|
||||
rbt_delete(proc_tree, proc_node);
|
||||
kfree(proc);
|
||||
} else {
|
||||
kprintf(LOG_ERROR "proc: proc_term: proc %u not found\n", pid);
|
||||
panic();
|
||||
}
|
||||
}
|
||||
|
||||
void schedule()
|
||||
{
|
||||
uint8_t id = lapic_idno();
|
||||
|
|
@ -79,15 +113,23 @@ void proc_init(unsigned num_cpus)
|
|||
for (unsigned i = 0; i < num_cpus; i++) {
|
||||
proc_current[i] = -1;
|
||||
|
||||
struct proc *proc = kzalloc(sizeof(struct proc), ALLOC_KERN);
|
||||
struct proc *proc = &kernel_procs[i];
|
||||
|
||||
proc->pid = rbt_next_key(proc_tree);
|
||||
proc->pid = 0;
|
||||
proc->is_kernel = true;
|
||||
proc->cr3 = kcr3;
|
||||
|
||||
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
|
||||
proc_node->value = (uintptr_t)proc;
|
||||
proc->regs.rip = (uint64_t)yield;
|
||||
proc->regs.cs = 0x28;
|
||||
proc->regs.rflags = 0x246;
|
||||
proc->regs.rsp = kstacks[i] + 0xFF0;
|
||||
|
||||
proc_current[i] = proc->pid;
|
||||
proc_current[i] = 0;
|
||||
}
|
||||
|
||||
/* ensure that the first process has PID 0
|
||||
* this value is never actually used, it prevents other processes from
|
||||
* having PID 0
|
||||
*/
|
||||
(void)rbt_insert(proc_tree, 0);
|
||||
}
|
||||
|
|
|
|||
50
sched/syscall.c
Normal file
50
sched/syscall.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/rbtree.h>
|
||||
|
||||
#define SYS_EXIT 60
|
||||
|
||||
static struct rbtree *syscall_tree;
|
||||
|
||||
typedef void (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
|
||||
|
||||
void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
|
||||
{
|
||||
struct rbnode *node = rbt_search(syscall_tree, syscall_no);
|
||||
if (node == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
syscall_t syscall = (void *)node->value;
|
||||
|
||||
syscall(arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
|
||||
void sys_exit(int status)
|
||||
{
|
||||
struct proc *proc = proc_find(getpid());
|
||||
if (proc == NULL)
|
||||
return;
|
||||
|
||||
proc->state = PROC_STOPPED;
|
||||
|
||||
proc_term(getpid());
|
||||
|
||||
kprintf("(syscall)Process %d exited with status 0x%x\n", getpid(), status);
|
||||
|
||||
proc_set_current(0);
|
||||
}
|
||||
|
||||
static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
|
||||
{
|
||||
struct rbnode *node = rbt_insert(syscall_tree, syscall_no);
|
||||
node->value = (uintptr_t)syscall;
|
||||
}
|
||||
|
||||
void syscall_init()
|
||||
{
|
||||
syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN);
|
||||
syscall_insert(SYS_EXIT, (syscall_t)sys_exit);
|
||||
}
|
||||
|
|
@ -10,12 +10,12 @@ CC := x86_64-elf-gcc
|
|||
LD := x86_64-elf-ld
|
||||
|
||||
OUT := test1.elf
|
||||
CSRC := test.c
|
||||
CSRC :=
|
||||
SSRC := start.S
|
||||
OBJ := $(CSRC:.c=.o) $(SSRC:.S=.o)
|
||||
DEP := $(OBJ:.o=.d)
|
||||
|
||||
CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD
|
||||
CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD -g
|
||||
LDFLAGS := -nostdlib -static -m elf_x86_64
|
||||
|
||||
CURDIR := $(shell pwd | awk -F '/' '{print $$(NF-1),"/",$$NF}' | sed 's/ //g')
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@
|
|||
* Test the basic functionality of the kernel loader
|
||||
*/
|
||||
|
||||
sys_exit = 60
|
||||
sys_exit = 60
|
||||
|
||||
.global _start
|
||||
_start:
|
||||
call main
|
||||
movq %rax, %rdi
|
||||
movq sys_exit, %rax
|
||||
movq $0x123456, %rdi
|
||||
movq $sys_exit, %rax
|
||||
int $0x80
|
||||
_no_exit:
|
||||
movq $0, %rbx
|
||||
jmp _no_exit
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
int main()
|
||||
{
|
||||
return 0x123456;
|
||||
}
|
||||
Loading…
Reference in a new issue