diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index 56478fa..9e122ea 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -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); diff --git a/amd64/gdt.c b/amd64/gdt.c index 638c2b8..18511f1 100644 --- a/amd64/gdt.c +++ b/amd64/gdt.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include +#include #include #include #include @@ -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); diff --git a/amd64/include/arch/proc.h b/amd64/include/arch/proc.h index bf7cf15..bbaeba1 100644 --- a/amd64/include/arch/proc.h +++ b/amd64/include/arch/proc.h @@ -39,5 +39,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_ */ diff --git a/amd64/kernel/proc.c b/amd64/kernel/proc.c index b3f2e27..0f873db 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -30,6 +30,11 @@ struct proc *get_cur_proc() return t->proc; } +u32 cpu_id() +{ + return tls_get()->cpu_id; +} + /* the thread that calls this will be lost, so it should only be called * on the initial process, and while doing an exec */