akaros/kern/arch/x86/topology.h
<<
>>
Prefs
   1/* Copyright (c) 2015 The Regents of the University of California
   2 * Kevin Klues <klueska@cs.berkeley.edu>
   3 * See LICENSE for details. */
   4
   5#pragma once
   6
   7#include <ros/common.h>
   8#include <arch/apic.h>
   9#include <arch/x86.h>
  10
  11struct core_info {
  12        int numa_id;
  13        int socket_id;
  14        int cpu_id;
  15        int core_id;
  16        int raw_socket_id;
  17        int apic_id;
  18};
  19
  20struct topology_info {
  21        int num_cores;
  22        int num_cpus;
  23        int num_sockets;
  24        int num_numa;
  25        int cores_per_cpu;
  26        int cores_per_socket;
  27        int cores_per_numa;
  28        int cpus_per_socket;
  29        int cpus_per_numa;
  30        int sockets_per_numa;
  31        int max_apic_id;
  32        struct core_info *core_list;
  33};
  34
  35extern struct topology_info cpu_topology_info;
  36extern int *os_coreid_lookup;
  37#define num_cores (cpu_topology_info.num_cores)
  38
  39void topology_init();
  40void print_cpu_topology();
  41
  42static inline int get_hw_coreid(uint32_t coreid)
  43{
  44        return cpu_topology_info.core_list[coreid].apic_id;
  45}
  46
  47static inline int hw_core_id(void)
  48{
  49        return lapic_get_id();
  50}
  51
  52static inline int get_os_coreid(int hw_coreid)
  53{
  54        return os_coreid_lookup[hw_coreid];
  55}
  56
  57static inline int numa_id(void)
  58{
  59        int os_coreid = os_coreid_lookup[lapic_get_id()];
  60
  61        return cpu_topology_info.core_list[os_coreid].numa_id;
  62}
  63
  64static inline int core_id(void)
  65{
  66        int coreid;
  67        /* assuming we're right after stacktop.  gs base is the pcpui struct,
  68         * but we don't have access to the pcpui struct or to the extern
  69         * per_cpu_info here, due to include loops. */
  70        asm volatile ("movl %%gs:8, %0" : "=r"(coreid));
  71        return coreid;
  72}
  73
  74/* Tracks whether it is safe to execute core_id() or not. */
  75static inline int core_id_early(void)
  76{
  77        extern bool core_id_ready;
  78
  79        if (!core_id_ready)
  80                return 0;
  81        return core_id();
  82}
  83