tls: track cpu_id

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-22 00:33:02 -04:00
commit 308ba36aab
4 changed files with 13 additions and 5 deletions

View file

@ -60,6 +60,7 @@ void arch_ap_startup()
u64 id = atomic_xadd(&cpu_ticker, 1); u64 id = atomic_xadd(&cpu_ticker, 1);
struct cpu_local *this_local = &cpu_local_arr[id]; struct cpu_local *this_local = &cpu_local_arr[id];
this_local->cpu_id = id;
wrmsr(IA32_GS_BASE, (u64)this_local); wrmsr(IA32_GS_BASE, (u64)this_local);
tss_init(id, this_local); tss_init(id, this_local);

View file

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/proc.h> #include <arch/proc.h>
#include <kernel/atomic.h>
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <lib/string.h> #include <lib/string.h>
@ -46,8 +47,8 @@ void gdt_init()
void tss_init(int cpu_id, struct cpu_local *local) void tss_init(int cpu_id, struct cpu_local *local)
{ {
int gdt_idx = 9 + cpu_id * 2; int idx = atomic_xadd(&gdt_index, 2);
uint16_t sel = gdt_idx * 8; uint16_t sel = idx * 8;
char *tss_stack = kalloc(TSS_STACK_SIZE, ALLOC_KERN); char *tss_stack = kalloc(TSS_STACK_SIZE, ALLOC_KERN);
if (!tss_stack) 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; tss->rsp0 = (u64)tss_stack + TSS_STACK_SIZE;
uint64_t addr = (uint64_t)tss; 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->limit = sizeof(struct tss) - 1;
desc->base = addr & 0xFFFF; desc->base = addr & 0xFFFF;
desc->base_2 = (addr >> 16) & 0xFF; desc->base_2 = (addr >> 16) & 0xFF;
desc->base_3 = (addr >> 24) & 0xFF; desc->base_3 = (addr >> 24) & 0xFF;
desc->base_4 = addr >> 32; desc->base_4 = addr >> 32;
desc->access = 0x89; desc->access = 0x89;
desc->flags = 0x00; desc->flags = 0x00;
desc->reserved = 0; desc->reserved = 0;
ltr(sel); ltr(sel);

View file

@ -39,5 +39,6 @@ struct proc *get_cur_proc();
struct thread *get_cur_thread(); struct thread *get_cur_thread();
void enter_userspace_init(struct thread *thread, uaddr_t entry); void enter_userspace_init(struct thread *thread, uaddr_t entry);
struct pt_regs *current_pt_regs(); struct pt_regs *current_pt_regs();
u32 cpu_id();
#endif /* _ARCH_PROC_H_ */ #endif /* _ARCH_PROC_H_ */

View file

@ -30,6 +30,11 @@ struct proc *get_cur_proc()
return t->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 /* the thread that calls this will be lost, so it should only be called
* on the initial process, and while doing an exec * on the initial process, and while doing an exec
*/ */