sched: begin scheduling module
This commit is contained in:
parent
06b02fc9a6
commit
2fe0b0068b
10 changed files with 182 additions and 3 deletions
|
|
@ -86,6 +86,11 @@ void lapic_enable()
|
||||||
lapic_write(lapic_addr, 0xF0, 0x1FF);
|
lapic_write(lapic_addr, 0xF0, 0x1FF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t lapic_idno()
|
||||||
|
{
|
||||||
|
return lapic_read(lapic_addr, LAPIC_ID) >> 24;
|
||||||
|
}
|
||||||
|
|
||||||
void apic_init()
|
void apic_init()
|
||||||
{
|
{
|
||||||
if (__madt == NULL) {
|
if (__madt == NULL) {
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,7 @@ void apic_init();
|
||||||
void lapic_eoi();
|
void lapic_eoi();
|
||||||
int madt_parse_next_entry(int offset);
|
int madt_parse_next_entry(int offset);
|
||||||
void lapic_enable();
|
void lapic_enable();
|
||||||
|
uint8_t lapic_idno();
|
||||||
|
|
||||||
#endif /* __ASM__ */
|
#endif /* __ASM__ */
|
||||||
#endif /* _APIC_H_ */
|
#endif /* _APIC_H_ */
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@
|
||||||
#define MB (KB * KB)
|
#define MB (KB * KB)
|
||||||
#define GB (KB * MB)
|
#define GB (KB * MB)
|
||||||
|
|
||||||
|
#define RFLAGS_IF (1 << 9)
|
||||||
|
|
||||||
void yield();
|
void yield();
|
||||||
void panic();
|
void panic();
|
||||||
|
|
||||||
|
|
|
||||||
47
include/kernel/proc.h
Normal file
47
include/kernel/proc.h
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _PROC_H_
|
||||||
|
#define _PROC_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
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_ */
|
||||||
|
|
@ -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_search(struct rbtree *tree, uint64_t key);
|
||||||
struct rbnode *rbt_successor(struct rbnode *node);
|
struct rbnode *rbt_successor(struct rbnode *node);
|
||||||
|
uint64_t rbt_next_key(struct rbtree *tree);
|
||||||
|
|
||||||
void rbt_slab_init();
|
void rbt_slab_init();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,8 +118,6 @@ void irq(int irq)
|
||||||
void (*handler)() = irq_handlers[irq];
|
void (*handler)() = irq_handlers[irq];
|
||||||
handler();
|
handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
pic_eoi(irq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void idt_init()
|
void idt_init()
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
#include <kernel/pio.h>
|
#include <kernel/pio.h>
|
||||||
#include <kernel/block.h>
|
#include <kernel/block.h>
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
|
||||||
#include <dev/pic.h>
|
#include <dev/pic.h>
|
||||||
#include <dev/console.h>
|
#include <dev/console.h>
|
||||||
|
|
@ -178,6 +179,7 @@ void kmain()
|
||||||
|
|
||||||
/* init the application processors */
|
/* init the application processors */
|
||||||
struct limine_smp_response *smp_resp = smp_req.response;
|
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++) {
|
for (unsigned i = 0; i < smp_resp->cpu_count; i++) {
|
||||||
struct limine_smp_info *info = smp_resp->cpus[i];
|
struct limine_smp_info *info = smp_resp->cpus[i];
|
||||||
|
|
||||||
|
|
|
||||||
25
lib/rbtree.c
25
lib/rbtree.c
|
|
@ -343,6 +343,31 @@ struct rbnode *rbt_successor(struct rbnode *node)
|
||||||
return parent;
|
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)
|
void rbtree_print_diagram_fancy(struct rbtree *tree, struct rbnode *node, int indent)
|
||||||
{
|
{
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,67 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
|
||||||
#define __ASM__
|
#define __ASM__
|
||||||
#include <dev/serial.h>
|
#include <dev/serial.h>
|
||||||
|
|
||||||
|
.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
|
.macro exception nr
|
||||||
.global _exception_\nr
|
.global _exception_\nr
|
||||||
_exception_\nr:
|
_exception_\nr:
|
||||||
|
cli
|
||||||
|
push_regs
|
||||||
movq $\nr, %rdi
|
movq $\nr, %rdi
|
||||||
movq $0, %rsi
|
movq $0, %rsi
|
||||||
call exception
|
call exception
|
||||||
call yield
|
call yield
|
||||||
|
pop_regs
|
||||||
iretq
|
iretq
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
.macro exception_err nr
|
.macro exception_err nr
|
||||||
.global _exception_\nr
|
.global _exception_\nr
|
||||||
_exception_\nr:
|
_exception_\nr:
|
||||||
|
cli
|
||||||
|
push_regs
|
||||||
popq %rsi
|
popq %rsi
|
||||||
movq $\nr, %rdi
|
movq $\nr, %rdi
|
||||||
call exception
|
call exception
|
||||||
call yield
|
call yield
|
||||||
|
pop_regs
|
||||||
iretq
|
iretq
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
|
|
@ -26,9 +69,10 @@
|
||||||
.global _irq_\nr
|
.global _irq_\nr
|
||||||
_irq_\nr:
|
_irq_\nr:
|
||||||
cli
|
cli
|
||||||
|
push_regs
|
||||||
movq $\nr, %rdi
|
movq $\nr, %rdi
|
||||||
call irq
|
call irq
|
||||||
sti
|
pop_regs
|
||||||
iretq
|
iretq
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
|
|
@ -92,3 +136,11 @@ irq 0x0D
|
||||||
irq 0x0E
|
irq 0x0E
|
||||||
irq 0x0F
|
irq 0x0F
|
||||||
|
|
||||||
|
.global _irq_schedule
|
||||||
|
_irq_schedule:
|
||||||
|
cli
|
||||||
|
push_regs
|
||||||
|
movq $0x00, %rdi
|
||||||
|
call irq_schedule
|
||||||
|
pop_regs
|
||||||
|
iretq
|
||||||
46
sched/proc.c
Normal file
46
sched/proc.c
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
#include <kernel/rbtree.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
#include <dev/apic.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue