diff --git a/dev/apic.c b/dev/apic.c index 19de513..bce2f9c 100644 --- a/dev/apic.c +++ b/dev/apic.c @@ -86,6 +86,11 @@ void lapic_enable() lapic_write(lapic_addr, 0xF0, 0x1FF); } +uint8_t lapic_idno() +{ + return lapic_read(lapic_addr, LAPIC_ID) >> 24; +} + void apic_init() { if (__madt == NULL) { diff --git a/include/dev/apic.h b/include/dev/apic.h index 95255f0..41f373e 100644 --- a/include/dev/apic.h +++ b/include/dev/apic.h @@ -131,6 +131,7 @@ void apic_init(); void lapic_eoi(); int madt_parse_next_entry(int offset); void lapic_enable(); +uint8_t lapic_idno(); #endif /* __ASM__ */ #endif /* _APIC_H_ */ diff --git a/include/kernel/common.h b/include/kernel/common.h index 42de0af..6d8f4b5 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -22,6 +22,8 @@ #define MB (KB * KB) #define GB (KB * MB) +#define RFLAGS_IF (1 << 9) + void yield(); void panic(); diff --git a/include/kernel/proc.h b/include/kernel/proc.h new file mode 100644 index 0000000..e9a2944 --- /dev/null +++ b/include/kernel/proc.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _PROC_H_ +#define _PROC_H_ + +#include +#include + +typedef uint64_t pid_t; + +struct procregs { + uint64_t rax; + uint64_t rbx; + uint64_t rcx; + uint64_t rdx; + uint64_t rsi; + uint64_t rdi; + uint64_t rbp; + uint64_t r8; + uint64_t r9; + uint64_t r10; + uint64_t r11; + uint64_t r12; + uint64_t r13; + uint64_t r14; + uint64_t r15; + uint64_t rip; + + uint64_t rflags; + uint64_t rsp; + uint16_t fs; + uint16_t gs; + uint16_t cs; +} PACKED; + +struct proc { + struct procregs regs; + uint64_t pid; + uint64_t ppid; + uint64_t cr3; + + bool is_kernel; +}; + +struct procregs *proc_current_regs(); +void proc_init(unsigned num_cpus); + +#endif /* _PROC_H_ */ diff --git a/include/kernel/rbtree.h b/include/kernel/rbtree.h index 91f2f41..80e24de 100644 --- a/include/kernel/rbtree.h +++ b/include/kernel/rbtree.h @@ -34,6 +34,7 @@ void rbt_delete(struct rbtree *tree, struct rbnode *del); struct rbnode *rbt_search(struct rbtree *tree, uint64_t key); struct rbnode *rbt_successor(struct rbnode *node); +uint64_t rbt_next_key(struct rbtree *tree); void rbt_slab_init(); diff --git a/init/irq.c b/init/irq.c index d039b5d..cda64bb 100644 --- a/init/irq.c +++ b/init/irq.c @@ -118,8 +118,6 @@ void irq(int irq) void (*handler)() = irq_handlers[irq]; handler(); } - - pic_eoi(irq); } void idt_init() diff --git a/init/main.c b/init/main.c index b3d6803..3cd41c6 100644 --- a/init/main.c +++ b/init/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -178,6 +179,7 @@ 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]; diff --git a/lib/rbtree.c b/lib/rbtree.c index c454d8d..fc6b7a9 100644 --- a/lib/rbtree.c +++ b/lib/rbtree.c @@ -343,6 +343,31 @@ struct rbnode *rbt_successor(struct rbnode *node) return parent; } +uint64_t rbt_next_key(struct rbtree *tree) +{ + /* find first available key */ + struct rbnode *node = tree->root; + + if (node == NULL) + return 0; + + while (node->left != NULL) + node = node->left; + + while (true) { + struct rbnode *next = rbt_successor(node); + if (next == NULL) { + return node->key + 1; + } else { + if (next->key != node->key + 1) { + return node->key + 1; + } else { + node = next; + } + } + } +} + void rbtree_print_diagram_fancy(struct rbtree *tree, struct rbnode *node, int indent) { if (node == NULL) { diff --git a/dev/isr.S b/sched/entry.S similarity index 70% rename from dev/isr.S rename to sched/entry.S index 0323e5b..78bfe9b 100644 --- a/dev/isr.S +++ b/sched/entry.S @@ -1,24 +1,67 @@ /* SPDX-License-Identifier: GPL-2.0-only */ + #define __ASM__ #include +.macro push_regs + pushq %rax + pushq %rbx + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %rbp + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + pushq %r12 + pushq %r13 + pushq %r14 + pushq %r15 +.endm + +.macro pop_regs + popq %r15 + popq %r14 + popq %r13 + popq %r12 + popq %r11 + popq %r10 + popq %r9 + popq %r8 + popq %rbp + popq %rdi + popq %rsi + popq %rdx + popq %rcx + popq %rbx + popq %rax +.endm + .macro exception nr .global _exception_\nr _exception_\nr: + cli + push_regs movq $\nr, %rdi movq $0, %rsi call exception call yield + pop_regs iretq .endm .macro exception_err nr .global _exception_\nr _exception_\nr: + cli + push_regs popq %rsi movq $\nr, %rdi call exception call yield + pop_regs iretq .endm @@ -26,9 +69,10 @@ .global _irq_\nr _irq_\nr: cli + push_regs movq $\nr, %rdi call irq - sti + pop_regs iretq .endm @@ -92,3 +136,11 @@ irq 0x0D irq 0x0E irq 0x0F +.global _irq_schedule +_irq_schedule: + cli + push_regs + movq $0x00, %rdi + call irq_schedule + pop_regs + iretq diff --git a/sched/proc.c b/sched/proc.c new file mode 100644 index 0000000..6007ee7 --- /dev/null +++ b/sched/proc.c @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include + +static struct rbtree *proc_tree; +static pid_t *proc_current; + +void return_to_user(struct procregs *regs, paddr_t cr3); + +struct procregs *proc_current_regs() +{ + struct rbnode *proc_node = rbt_search(proc_tree, proc_current[lapic_idno()]); + + if (proc_node) { + struct proc *ret = (void *)proc_node->value; + + return &ret->regs; + } else { + return NULL; + } +} + +void proc_init(unsigned num_cpus) +{ + /* allocate current process structs */ + proc_current = kzalloc(num_cpus * sizeof(pid_t), ALLOC_KERN); + proc_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN); + + for (unsigned i = 0; i < num_cpus; i++) { + proc_current[i] = -1; + + struct proc *proc = kzalloc(sizeof(struct proc), ALLOC_KERN); + + proc->pid = rbt_next_key(proc_tree); + proc->is_kernel = true; + proc->cr3 = kcr3; + + struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid); + proc_node->value = (uintptr_t)proc; + + proc_current[i] = proc->pid; + } +}