smp: enable smp
This is a very large patch. Don't feel like splitting it up. Change locking discipline for rbtrees. Most changes are this. Fix stack init logic for application processors, and start them up. Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
33d6f72cfe
commit
3e5abada9b
17 changed files with 123 additions and 52 deletions
2
Makefile
2
Makefile
|
|
@ -63,7 +63,7 @@ KOBJ := $(KOBJ_CORE) kernel/module/built-in.o
|
|||
|
||||
obj-y := $(KDIRS)
|
||||
|
||||
QEMUFLAGS := -m 4G
|
||||
QEMUFLAGS := -m 4G -smp 2
|
||||
|
||||
MODULE_CFLAGS := -DMODULE
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,15 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/tty.h>
|
||||
#include <kernel/atomic.h>
|
||||
#include <kernel/buddy.h>
|
||||
#include <kernel/requests.h>
|
||||
#include <arch/proc.h>
|
||||
#include <arch/init.h>
|
||||
#include <drivers/acpi.h>
|
||||
#include <drivers/apic.h>
|
||||
#include <drivers/serial.h>
|
||||
#include <lib/stdio.h>
|
||||
#include <lib/sem.h>
|
||||
#include "arch/msr.h"
|
||||
#include "arch/reg.h"
|
||||
#include "descriptor.h"
|
||||
|
|
@ -55,14 +60,20 @@ void arch_init()
|
|||
wrmsr(IA32_FMASK, 0x3F7FD5);
|
||||
}
|
||||
|
||||
void arch_ap_startup()
|
||||
/* entry point for application processors */
|
||||
int ap_init(bool bsp, struct limine_mp_info *info)
|
||||
{
|
||||
sem_t *sem = info ? (void *)info->extra_argument : NULL;
|
||||
|
||||
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);
|
||||
|
||||
lidt();
|
||||
lgdt();
|
||||
|
||||
tss_init(id, this_local);
|
||||
|
||||
char buf[64];
|
||||
|
|
@ -76,4 +87,43 @@ void arch_ap_startup()
|
|||
panic("failed to create idle thread for cpu %d\n", id);
|
||||
|
||||
this_local->idle_thread = idle;
|
||||
|
||||
if (sem)
|
||||
sem_post(sem);
|
||||
|
||||
if (!bsp)
|
||||
schedule();
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
static void ap_trampoline(struct limine_mp_info *info)
|
||||
{
|
||||
void *ap_stack = buddy_alloc(buddy_order(P_STACK_SIZE));
|
||||
|
||||
void ap_trampoline_2(void *stack_addr, size_t stack_size, struct limine_mp_info *info);
|
||||
ap_trampoline_2(ap_stack, P_STACK_SIZE, info);
|
||||
}
|
||||
|
||||
/* start the application processors */
|
||||
void arch_ap_startup()
|
||||
{
|
||||
ap_init(true, NULL);
|
||||
|
||||
size_t n = mp_res->cpu_count;
|
||||
|
||||
sem_t sem;
|
||||
sem_init(&sem, 0);
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
struct limine_mp_info *cpu = mp_res->cpus[i];
|
||||
if (cpu->lapic_id == mp_res->bsp_lapic_id)
|
||||
continue;
|
||||
|
||||
cpu->extra_argument = (u64)&sem;
|
||||
*((volatile limine_goto_address *)&cpu->goto_address) = ap_trampoline;
|
||||
while (!sem_trywait(&sem))
|
||||
;
|
||||
}
|
||||
krdy("AP startup complete\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ exception_common:
|
|||
push_all /* pushes 15 registers */
|
||||
mov rdi, [rsp + 120] /* vector: 15 regs up = 120 */
|
||||
mov rsi, [rsp + 128] /* error code we pushed in the macro */
|
||||
mov rdx, [rsp + 136] /* RIP from interrupt frame */
|
||||
|
||||
/* we skip storing the user stack in these interrupt entries because
|
||||
* the iretq frame stores it
|
||||
|
|
|
|||
|
|
@ -10,3 +10,12 @@ arch_stack_init:
|
|||
mov rbp, rsp
|
||||
push rdx
|
||||
ret
|
||||
|
||||
.global ap_trampoline_2
|
||||
ap_trampoline_2:
|
||||
add rdi, rsi
|
||||
mov rsp, rdi
|
||||
mov rbp, rsp
|
||||
mov rdi, 0
|
||||
mov rsi, rdx
|
||||
jmp ap_init
|
||||
|
|
|
|||
|
|
@ -84,13 +84,13 @@ static void ipi_handle_tlb_flush()
|
|||
lapic_eoi();
|
||||
}
|
||||
|
||||
void lapic_send_ipi(u32 lapic_id, u8 vector)
|
||||
void lapic_send_ipi(u32 lapic_id, u32 icr)
|
||||
{
|
||||
/* check for current IPI */
|
||||
while (lapic_read(lapic_addr, LAPIC_ICR) & (1 << 12))
|
||||
;
|
||||
lapic_write(lapic_addr, LAPIC_ICR + 0x10, lapic_id << 24);
|
||||
lapic_write(lapic_addr, LAPIC_ICR, vector | (1 << 14));
|
||||
lapic_write(lapic_addr, LAPIC_ICR, icr);
|
||||
}
|
||||
|
||||
void apic_init()
|
||||
|
|
@ -108,22 +108,11 @@ void apic_init()
|
|||
ioapic_redirect_insert(4, 0x24, 0, 0);
|
||||
|
||||
lapic_enable();
|
||||
|
||||
uint64_t msr = rdmsr(MSR_IA32_APIC_BASE);
|
||||
msr |= 1 << 11;
|
||||
wrmsr(MSR_IA32_APIC_BASE, msr);
|
||||
|
||||
lapic_eoi();
|
||||
|
||||
/* Register timer interrupt handler and enable APIC timer */
|
||||
void _timer_handler();
|
||||
idt_insert(0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, _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);
|
||||
|
||||
idt_insert(IPI_KILL, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, ipi_handle_kill);
|
||||
idt_insert(IPI_TLB_FLUSH, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, ipi_handle_tlb_flush);
|
||||
|
||||
|
|
@ -132,15 +121,8 @@ void apic_init()
|
|||
|
||||
void apic_enable_timer()
|
||||
{
|
||||
/* set TPR to ensure interrupts are accepted */
|
||||
lapic_write(lapic_addr, LAPIC_TPR, 0);
|
||||
|
||||
/* set timer divisor */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_DIV, 0x3);
|
||||
|
||||
/* set timer initial count */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_INIT_COUNT, 0xFFF);
|
||||
|
||||
/* set timer vector and enable */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_INIT_COUNT, 0xFFFF0);
|
||||
lapic_write(lapic_addr, LAPIC_TIMER, 0x20 | 0x20000);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <lib/stdio.h>
|
||||
#include <arch/proc.h>
|
||||
#include "arch/reg.h"
|
||||
|
||||
static const char *exception_names[] = {
|
||||
|
|
@ -37,11 +38,11 @@ static const char *exception_names[] = {
|
|||
"Triple Fault",
|
||||
};
|
||||
|
||||
void exception_handler(u64 no, u64 err)
|
||||
void exception_handler(u64 no, u64 err, u64 rip)
|
||||
{
|
||||
if (no == 0x0e) {
|
||||
panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err);
|
||||
panic("(Page fault) cpu%d ip:0x%016lx addr:0x%016lx err:%x\n", cpu_id(), rip, cr2_read(), err);
|
||||
}
|
||||
|
||||
panic("(Exception) %s(%02xh) err: %x\n", exception_names[no], no, err);
|
||||
panic("(Exception) cpu%d ip:0x%016lx %s(%02xh) err: %x\n", cpu_id(), rip, exception_names[no], no, err);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,5 +40,8 @@
|
|||
#define MSR_IA32_APIC_BASE 0x1B
|
||||
|
||||
void apic_init();
|
||||
void apic_enable_timer();
|
||||
void lapic_eoi();
|
||||
void lapic_send_ipi(u32 lapic_id, u32 icr);
|
||||
|
||||
#endif /* _APIC_H_ */
|
||||
|
|
|
|||
|
|
@ -28,13 +28,24 @@ u32 cpu_id()
|
|||
return tls_get()->cpu_id;
|
||||
}
|
||||
|
||||
extern void _enter_userspace_init();
|
||||
|
||||
int thread_set_entry(struct thread *thread, uaddr_t entry)
|
||||
{
|
||||
char *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
|
||||
char *kstack = kalloc(P_STACK_SIZE, ALLOC_KERN);
|
||||
if (!kstack)
|
||||
return -ENOMEM;
|
||||
|
||||
thread->kstack_top = (vaddr_t)kstack + KSTACK_SIZE;
|
||||
vaddr_t ktop = (vaddr_t)kstack + P_STACK_SIZE;
|
||||
struct pt_regs *r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
|
||||
memset(r, 0, sizeof(struct pt_regs));
|
||||
r->rdi = (u64)entry;
|
||||
r->rsi = 0x202;
|
||||
r->rdx = (u64)thread->ustack_top;
|
||||
r->rsp = (u64)_enter_userspace_init;
|
||||
|
||||
thread->kstack_top = ktop;
|
||||
thread->kstack_ptr = (uaddr_t)r;
|
||||
thread->entry = entry;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -98,18 +109,18 @@ struct thread *kthread_create(void *fn, void *arg, const char *name)
|
|||
thread->state = THREAD_STATE_DNS;
|
||||
thread->pgd = kpgd;
|
||||
|
||||
char *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
|
||||
char *kstack = kalloc(P_STACK_SIZE, ALLOC_KERN);
|
||||
if (!kstack) {
|
||||
slab_free(thread_slab, thread);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
|
||||
struct pt_regs *r = (struct pt_regs *)(ktop - sizeof(struct pt_regs) - 8);
|
||||
vaddr_t ktop = (vaddr_t)kstack + P_STACK_SIZE;
|
||||
struct pt_regs *r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
|
||||
memset(r, 0, sizeof(struct pt_regs));
|
||||
r->rdi = (u64)arg;
|
||||
r->rdx = (u64)fn;
|
||||
*(u64 *)(ktop - 8) = (u64)kthread_entry;
|
||||
r->rsp = (u64)kthread_entry;
|
||||
|
||||
thread->kstack_top = ktop;
|
||||
thread->kstack_ptr = (uaddr_t)r;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#ifndef _COMMON_H_
|
||||
#define _COMMON_H_
|
||||
|
||||
#define KSTACK_SIZE 0x4000
|
||||
#define P_STACK_SIZE 0x10000
|
||||
|
||||
#include <limine/limine.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ extern struct limine_rsdp_response *kern_rsdp_res;
|
|||
extern struct limine_module_response *module_res;
|
||||
extern struct limine_framebuffer_response *fb_res;
|
||||
extern struct limine_tsc_frequency_response *tsc_res;
|
||||
extern struct limine_mp_response *mp_res;
|
||||
|
||||
void requests_init();
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,16 @@ rbnode_t *rbt_successor(rbnode_t *node);
|
|||
u64 rbt_next_key(rbtree_t *tree);
|
||||
rbnode_t *rbt_minimum(rbnode_t *node);
|
||||
|
||||
static inline void rbt_lock(rbtree_t *tree)
|
||||
{
|
||||
spinlock_acquire(&tree->lock);
|
||||
}
|
||||
|
||||
static inline void rbt_unlock(rbtree_t *tree)
|
||||
{
|
||||
spinlock_release(&tree->lock);
|
||||
}
|
||||
|
||||
void rbt_init();
|
||||
|
||||
#endif /* _rbtree_tH_ */
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
#include <kernel/slab.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/driver.h>
|
||||
#include <kernel/sched.h>
|
||||
|
||||
#include <arch/init.h>
|
||||
|
||||
|
|
@ -34,8 +35,6 @@
|
|||
#include <lib/list.h>
|
||||
#include <lib/rbtree.h>
|
||||
|
||||
#define P_STACK_SIZE 0x10000
|
||||
|
||||
typedef void (*initcall_t)(void);
|
||||
|
||||
struct tty *fbcon_init();
|
||||
|
|
@ -94,6 +93,7 @@ static void kmain()
|
|||
do_initcalls();
|
||||
arch_ap_startup();
|
||||
apic_init();
|
||||
apic_enable_timer();
|
||||
|
||||
dirent_t *init;
|
||||
res = vfs_open("/bin/init", &init);
|
||||
|
|
@ -117,7 +117,6 @@ static void kmain()
|
|||
kernel_init_ready = true;
|
||||
|
||||
init_thread->state = THREAD_STATE_RUNNING;
|
||||
|
||||
enter_userspace_init(init_thread, entry);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#define REQUEST __attribute__((used, section(".limine_requests"))) volatile
|
||||
|
||||
REQUEST u64 _limine_requests_start[] = LIMINE_REQUESTS_START_MARKER;
|
||||
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
|
||||
|
||||
REQUEST struct limine_executable_address_request kern_addr_req = {.id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, .revision = 0};
|
||||
REQUEST struct limine_memmap_request memmap_req = {.id = LIMINE_MEMMAP_REQUEST_ID, .revision = 0};
|
||||
|
|
@ -12,7 +13,7 @@ REQUEST struct limine_hhdm_request hhdm_req = {.id = LIMINE_HHDM_REQUEST_ID, .re
|
|||
REQUEST struct limine_rsdp_request rsdp_req = {.id = LIMINE_RSDP_REQUEST_ID, .revision = 0};
|
||||
REQUEST struct limine_module_request module_req = {.id = LIMINE_MODULE_REQUEST_ID, .revision = 0};
|
||||
REQUEST struct limine_framebuffer_request fb_req = {.id = LIMINE_FRAMEBUFFER_REQUEST_ID, .revision = 0};
|
||||
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
|
||||
REQUEST struct limine_mp_request mp_req = {.id = LIMINE_MP_REQUEST_ID, .revision = 0};
|
||||
|
||||
REQUEST u64 _limine_requests_end[] = LIMINE_REQUESTS_END_MARKER;
|
||||
|
||||
|
|
@ -22,6 +23,7 @@ struct limine_hhdm_response *kern_hhdm_res = NULL;
|
|||
struct limine_rsdp_response *kern_rsdp_res = NULL;
|
||||
struct limine_module_response *module_res = NULL;
|
||||
struct limine_framebuffer_response *fb_res = NULL;
|
||||
struct limine_mp_response *mp_res = NULL;
|
||||
|
||||
vaddr_t hhdm_start = 0;
|
||||
|
||||
|
|
@ -33,6 +35,7 @@ void requests_init()
|
|||
kern_rsdp_res = rsdp_req.response;
|
||||
module_res = module_req.response;
|
||||
fb_res = fb_req.response;
|
||||
mp_res = mp_req.response;
|
||||
|
||||
hhdm_start = kern_hhdm_res->offset;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,8 @@ static struct thread *dequeue_thread()
|
|||
if (!sched_queue.head)
|
||||
sched_queue.tail = NULL;
|
||||
t->next = NULL;
|
||||
} else {
|
||||
t = tls_get()->idle_thread;
|
||||
}
|
||||
spinlock_release(&sched_queue.lock);
|
||||
return t;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
#include <lib/string.h>
|
||||
|
||||
#define USTACK_DEFAULT_SIZE 0x10000
|
||||
#define P_STACK_SIZE 0x4000
|
||||
|
||||
mem_cache_t *thread_slab;
|
||||
|
||||
|
|
|
|||
12
lib/rbtree.c
12
lib/rbtree.c
|
|
@ -130,11 +130,8 @@ rbnode_t *rbt_insert(rbtree_t *tree, u64 key)
|
|||
new->left = NULL;
|
||||
new->right = NULL;
|
||||
new->color = RB_RED;
|
||||
|
||||
new->value = 0;
|
||||
|
||||
spinlock_acquire(&tree->lock);
|
||||
|
||||
if (parent == NULL)
|
||||
tree->root = new;
|
||||
else if (key < parent->key)
|
||||
|
|
@ -144,7 +141,6 @@ rbnode_t *rbt_insert(rbtree_t *tree, u64 key)
|
|||
|
||||
rbt_insert_fixup(tree, new);
|
||||
tree->num_nodes++;
|
||||
spinlock_release(&tree->lock);
|
||||
|
||||
return new;
|
||||
}
|
||||
|
|
@ -255,23 +251,16 @@ rbnode_t *rbt_minimum(rbnode_t *node)
|
|||
|
||||
void rbt_delete(rbtree_t *tree, rbnode_t *del)
|
||||
{
|
||||
spinlock_acquire(&tree->lock);
|
||||
if (!del->left) {
|
||||
/* case 0: left is NULL */
|
||||
|
||||
rbt_transplant(tree, del, del->right);
|
||||
rbt_delete_fixup(tree, del->right);
|
||||
slab_free(rbt_slab, del);
|
||||
|
||||
} else if (!del->right) {
|
||||
/* case 1: right is NULL */
|
||||
|
||||
rbt_transplant(tree, del, del->left);
|
||||
rbt_delete_fixup(tree, del->left);
|
||||
slab_free(rbt_slab, del);
|
||||
} else {
|
||||
/* case 2: neither is NULL */
|
||||
|
||||
rbnode_t *y = rbt_minimum(del->right);
|
||||
rbnode_t *x = y->right;
|
||||
int orig_color = y->color;
|
||||
|
|
@ -297,7 +286,6 @@ void rbt_delete(rbtree_t *tree, rbnode_t *del)
|
|||
}
|
||||
|
||||
tree->num_nodes--;
|
||||
spinlock_release(&tree->lock);
|
||||
}
|
||||
|
||||
void rbt_destroy(rbtree_t *tree)
|
||||
|
|
|
|||
16
mem/alloc.c
16
mem/alloc.c
|
|
@ -67,11 +67,13 @@ void *kalloc(size_t size, u64 flags)
|
|||
entry->flags = flags;
|
||||
entry->type = alloc_type;
|
||||
|
||||
rbt_lock(&kalloc_tree);
|
||||
rbnode_t *node = rbt_insert(&kalloc_tree, (u64)ret);
|
||||
if (!node)
|
||||
panic("kalloc_tree unavailable\n");
|
||||
|
||||
node->value = (u64)entry;
|
||||
rbt_unlock(&kalloc_tree);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
@ -91,10 +93,13 @@ void kfree(void *ptr)
|
|||
if (!ptr)
|
||||
return;
|
||||
|
||||
rbt_lock(&kalloc_tree);
|
||||
rbnode_t *node = rbt_search(&kalloc_tree, (u64)ptr);
|
||||
|
||||
if (!node)
|
||||
if (!node) {
|
||||
rbt_unlock(&kalloc_tree);
|
||||
panic("invalid kfree: %p\n", ptr);
|
||||
}
|
||||
|
||||
struct alloc_entry *entry = (void *)node->value;
|
||||
|
||||
|
|
@ -112,23 +117,30 @@ void kfree(void *ptr)
|
|||
goto out_1;
|
||||
}
|
||||
}
|
||||
rbt_unlock(&kalloc_tree);
|
||||
panic("kfree: slab size not found\n");
|
||||
out_1:
|
||||
break;
|
||||
default:
|
||||
rbt_unlock(&kalloc_tree);
|
||||
panic("invalid kfree entry type: %d\n", entry->type);
|
||||
break;
|
||||
}
|
||||
rbt_unlock(&kalloc_tree);
|
||||
}
|
||||
|
||||
void *krealloc(void *old, size_t size, u64 flags)
|
||||
{
|
||||
rbt_lock(&kalloc_tree);
|
||||
rbnode_t *node = rbt_search(&kalloc_tree, (u64)old);
|
||||
|
||||
if (!node)
|
||||
if (!node) {
|
||||
rbt_unlock(&kalloc_tree);
|
||||
return kalloc(size, flags);
|
||||
}
|
||||
|
||||
struct alloc_entry *entry = (void *)node->value;
|
||||
rbt_unlock(&kalloc_tree);
|
||||
|
||||
void *new = kalloc(size, flags);
|
||||
if (!new)
|
||||
|
|
|
|||
Loading…
Reference in a new issue