You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
958 B
C

/* Copyright (C) 2021,2022 fef <owo@fef.moe>. All rights reserved. */
#pragma once
#include <arch/page.h>
#include <arch/sched.h>
#include <gay/cdefs.h>
#include <gay/config.h>
#include <gay/types.h>
static inline int smp_cpuid(void)
{
#if CFG_SMP
/*
* The x86 has a dedicated CPUID instruction that can be used to query
* pretty much anything except literally the cpu id. Therefore, we make
* use of the fact that kernel stacks are always aligned to their size.
* The cpu number is the topmost item on the stack; for cpu 0 this gets
* pushed in the assembly routine in startup{32,64}.S and for all others
* ... there is no implementation yet.
*/
u_register_t sp;
#ifdef __x86_64__
__asm__ volatile("movq %%rsp, %0" : "=r"(sp));
#else
__asm__ volatile("movl %%esp, %0" : "=r"(sp));
#endif
sp &= ~(KERN_STACK_SIZE - 1);
return ((int *)sp)[KERN_STACK_SIZE / sizeof(int) - 1];
#else /* !CFG_SMP */
return 0;
#endif /* !CFG_SMP */
}