Files
mango/kernel/percpu.c

41 lines
949 B
C
Raw Normal View History

#include <socks/percpu.h>
#include <socks/cpu.h>
#include <socks/vm.h>
#include <stdint.h>
#include <stddef.h>
extern char __percpu_start[];
extern char __percpu_end[];
static void *percpu_buffer = NULL;
static size_t percpu_stride = 0;
extern kern_status_t init_per_cpu_areas(void)
{
unsigned int last_cpu = cpu_get_highest_available() + 1;
percpu_stride = (uintptr_t)__percpu_end - (uintptr_t)__percpu_start;
if (percpu_stride & 0x7) {
percpu_stride &= ~0x7;
percpu_stride += 0x8;
}
percpu_buffer = kmalloc(last_cpu * percpu_stride, 0);
return KERN_OK;
}
extern void *__percpu_get(unsigned int cpu, void *var)
{
uintptr_t pvar = (uintptr_t)var;
uintptr_t percpu_start = (uintptr_t)__percpu_start;
uintptr_t percpu_end = (uintptr_t)__percpu_end;
if (pvar < percpu_start || pvar >= percpu_end) {
return NULL;
}
size_t var_offset = pvar - percpu_start;
return (char *)percpu_buffer + (cpu * percpu_stride) + var_offset;
}