Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
apic.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2011 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "precompiled.h"
25 
26 #include "lib/bits.h"
27 #include "lib/module_init.h"
28 #include "lib/sysdep/cpu.h" // ERR::CPU_FEATURE_MISSING
29 #include "lib/sysdep/os_cpu.h"
31 
32 
34 {
35  x86_x64::CpuidRegs regs = { 0 };
36  regs.eax = 1;
37  // note: CPUID function 1 is always supported, but only processors with
38  // an xAPIC (e.g. P4/Athlon XP) will return a nonzero ID.
39  bool ok = x86_x64::cpuid(&regs);
40  ASSERT(ok); UNUSED2(ok);
41  const u8 apicId = (u8)bits(regs.ebx, 24, 31);
42  return apicId;
43 }
44 
45 
46 static size_t numIds;
49 
51 {
53  struct StoreEachProcessorsApicId
54  {
55  static void Callback(size_t processor, uintptr_t UNUSED(data))
56  {
57  processorApicIds[processor] = GetApicId();
58  }
59  };
60  // (can fail due to restrictions on our process affinity or lack of
61  // support for affinity masks in OS X.)
62  RETURN_STATUS_IF_ERR(os_cpu_CallByEachCPU(StoreEachProcessorsApicId::Callback, 0));
63 
66  ApicId* const end = std::unique(sortedApicIds, sortedApicIds+numIds);
67  const size_t numUnique = end-sortedApicIds;
68 
69  // all IDs are zero - system lacks an xAPIC.
70  // (NB: we exclude single-processor systems in this test -
71  // having one zero-valued ID is legitimate)
72  if(numUnique == 1 && sortedApicIds[0] == 0 && numIds != 1)
73  {
74  debug_printf(L"APIC: all zero\n");
75  return ERR::CPU_FEATURE_MISSING; // NOWARN
76  }
77 
78  // not all unique - probably running in a VM whose emulation is
79  // imperfect or doesn't allow access to all processors.
80  if(numUnique != numIds)
81  {
82  debug_printf(L"APIC: not unique\n");
83  return ERR::FAIL; // NOWARN
84  }
85 
86  return INFO::OK;
87 }
88 
90 {
91  const Status status = GetAndValidateApicIds();
92  if(status < 0) // failed
93  {
94  // generate fake but legitimate APIC IDs
95  for(size_t processor = 0; processor < numIds; processor++)
96  processorApicIds[processor] = sortedApicIds[processor] = (ApicId)processor;
97  }
98 
99  return status;
100 }
101 
103 
104 
106 {
108  if(apicInitState < 0)
109  return false;
110  return true;
111 }
112 
113 
114 static size_t IndexFromApicId(const ApicId* apicIds, ApicId apicId)
115 {
117 
118  const ApicId* pos = std::find(apicIds, apicIds+numIds, apicId);
119  if(pos == apicIds+numIds)
120  {
122  return 0;
123  }
124 
125  const size_t index = pos - apicIds;
126  return index;
127 }
128 
130 {
131  return IndexFromApicId(processorApicIds, apicId);
132 }
133 
135 {
136  return IndexFromApicId(sortedApicIds, apicId);
137 }
138 
139 
140 static ApicId ApicIdFromIndex(const ApicId* apicIds, size_t index)
141 {
143  ASSERT(index < numIds);
144  return apicIds[index];
145 }
146 
147 ApicId ApicIdFromProcessor(size_t processor)
148 {
149  return ApicIdFromIndex(processorApicIds, processor);
150 }
151 
152 ApicId ApicIdFromContiguousId(size_t contiguousId)
153 {
154  return ApicIdFromIndex(sortedApicIds, contiguousId);
155 }
static Status InitApicIds()
Definition: apic.cpp:89
#define u8
Definition: types.h:39
const Status LOGIC
Definition: status.h:409
bool AreApicIdsReliable()
Definition: apic.cpp:105
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static size_t IndexFromApicId(const ApicId *apicIds, ApicId apicId)
Definition: apic.cpp:114
const Status OK
Definition: status.h:386
static ApicId sortedApicIds[os_cpu_MaxProcessors]
Definition: apic.cpp:48
static const size_t os_cpu_MaxProcessors
maximum number of processors supported by the OS (determined by the number of bits in an affinity mas...
Definition: os_cpu.h:50
Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
execute the specified function once on each processor.
Definition: bcpu.cpp:115
static Status GetAndValidateApicIds()
Definition: apic.cpp:50
static ApicId ApicIdFromIndex(const ApicId *apicIds, size_t index)
Definition: apic.cpp:140
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:310
size_t os_cpu_NumProcessors()
Definition: bcpu.cpp:34
ApicId ApicIdFromProcessor(size_t processor)
Definition: apic.cpp:147
size_t ProcessorFromApicId(ApicId apicId)
Definition: apic.cpp:129
ApicId ApicIdFromContiguousId(size_t contiguousId)
Definition: apic.cpp:152
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
intptr_t ModuleInitState
initialization state of a module (class, source file, etc.) must be initialized to zero (e...
Definition: module_init.h:35
bool cpuid(CpuidRegs *regs)
invoke CPUID instruction.
Definition: x86_x64.cpp:98
i64 Status
Error handling system.
Definition: status.h:171
size_t ContiguousIdFromApicId(ApicId apicId)
Definition: apic.cpp:134
T bits(T num, size_t lo_idx, size_t hi_idx)
extract the value of bits hi_idx:lo_idx within num
Definition: bits.h:97
const Status CPU_FEATURE_MISSING
Definition: cpu.h:35
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:331
static size_t numIds
Definition: apic.cpp:46
u8 ApicId
Definition: apic.h:26
registers used/returned by cpuid
Definition: x86_x64.h:46
const Status FAIL
Definition: status.h:406
static ApicId processorApicIds[os_cpu_MaxProcessors]
Definition: apic.cpp:47
Status ModuleInit(volatile ModuleInitState *initState, Status(*init)())
calls a user-defined init function if initState is zero.
Definition: module_init.cpp:40
ApicId GetApicId()
Definition: apic.cpp:33
static ModuleInitState apicInitState
Definition: apic.cpp:102
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142
#define RETURN_STATUS_IF_ERR(expression)
Definition: status.h:276