kernel: implement cpu IDs and per-cpu variables
This commit is contained in:
40
kernel/percpu.c
Normal file
40
kernel/percpu.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#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();
|
||||
|
||||
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(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 + (this_cpu() * percpu_stride) + var_offset;
|
||||
}
|
||||
Reference in New Issue
Block a user