0ad/source/lib/sysdep/arch/x86_x64/apic.cpp
janwas 0d708b31b4 avoid rare init-order bug when SRAT ACPI table is available AND APIC IDs are NOT
move APIC-related code to separate file, with its own initialization to
avoid circular dependency
fixes #976

This was SVN commit r10315.
2011-09-24 19:47:13 +00:00

138 lines
4 KiB
C++

/* Copyright (c) 2011 Wildfire Games
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "precompiled.h"
#include "lib/sysdep/arch/x86_x64/apic.h"
#include "lib/module_init.h"
#include "lib/sysdep/cpu.h" // ERR::CPU_FEATURE_MISSING
#include "lib/sysdep/os_cpu.h"
#include "lib/sysdep/arch/x86_x64/x86_x64.h" // x86_x64_ApicId
static size_t numIds;
static ApicId processorApicIds[os_cpu_MaxProcessors];
static ApicId sortedApicIds[os_cpu_MaxProcessors];
static Status GetAndValidateApicIds()
{
numIds = os_cpu_NumProcessors();
struct StoreEachProcessorsApicId
{
static void Callback(size_t processor, uintptr_t UNUSED(data))
{
processorApicIds[processor] = x86_x64_ApicId();
}
};
// (can fail due to restrictions on our process affinity or lack of
// support for affinity masks in OS X.)
RETURN_STATUS_IF_ERR(os_cpu_CallByEachCPU(StoreEachProcessorsApicId::Callback, 0));
std::copy(processorApicIds, processorApicIds+numIds, sortedApicIds);
std::sort(sortedApicIds, sortedApicIds+numIds);
ApicId* const end = std::unique(sortedApicIds, sortedApicIds+numIds);
const size_t numUnique = end-sortedApicIds;
// all IDs are zero - system lacks an xAPIC.
if(numUnique == 1 && sortedApicIds[0] == 0)
{
debug_printf(L"APIC: all zero\n");
return ERR::CPU_FEATURE_MISSING; // NOWARN
}
// not all unique - probably running in a VM whose emulation is
// imperfect or doesn't allow access to all processors.
if(numUnique != numIds)
{
debug_printf(L"APIC: not unique\n");
return ERR::FAIL; // NOWARN
}
return INFO::OK;
}
static Status InitApicIds()
{
const Status status = GetAndValidateApicIds();
if(status < 0) // failed
{
// generate fake but legitimate APIC IDs
for(size_t processor = 0; processor < numIds; processor++)
processorApicIds[processor] = sortedApicIds[processor] = (ApicId)processor;
}
return status;
}
static ModuleInitState apicInitState;
bool AreApicIdsReliable()
{
ModuleInit(&apicInitState, InitApicIds);
if(apicInitState < 0)
return false;
return true;
}
static size_t IndexFromApicId(const ApicId* apicIds, size_t apicId)
{
ModuleInit(&apicInitState, InitApicIds);
const ApicId* pos = std::find(apicIds, apicIds+numIds, apicId);
if(pos == apicIds+numIds)
{
DEBUG_WARN_ERR(ERR::LOGIC);
return 0;
}
const size_t index = pos - apicIds;
return index;
}
size_t ProcessorFromApicId(ApicId apicId)
{
return IndexFromApicId(processorApicIds, apicId);
}
size_t ContiguousIdFromApicId(ApicId apicId)
{
return IndexFromApicId(sortedApicIds, apicId);
}
static ApicId ApicIdFromIndex(const ApicId* apicIds, size_t index)
{
ModuleInit(&apicInitState, InitApicIds);
ASSERT(index < numIds);
return apicIds[index];
}
ApicId ApicIdFromProcessor(size_t processor)
{
return ApicIdFromIndex(processorApicIds, processor);
}
ApicId ApicIdFromContiguousId(size_t contiguousId)
{
return ApicIdFromIndex(sortedApicIds, contiguousId);
}