From 8c7dd6f85be3543baabb952764d83532e5006ed9 Mon Sep 17 00:00:00 2001 From: Vladislav Belov Date: Sat, 17 Oct 2026 15:53:16 +0200 Subject: [PATCH] Removes APIC functionality --- source/lib/sysdep/arch/x86_x64/apic.cpp | 167 ------------------------ source/lib/sysdep/arch/x86_x64/apic.h | 54 -------- 2 files changed, 221 deletions(-) delete mode 100644 source/lib/sysdep/arch/x86_x64/apic.cpp delete mode 100644 source/lib/sysdep/arch/x86_x64/apic.h diff --git a/source/lib/sysdep/arch/x86_x64/apic.cpp b/source/lib/sysdep/arch/x86_x64/apic.cpp deleted file mode 100644 index 8c38e30f81..0000000000 --- a/source/lib/sysdep/arch/x86_x64/apic.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* Copyright (C) 2025 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 "apic.h" - -#include "lib/bits.h" -#include "lib/debug.h" -#include "lib/module_init.h" -#include "lib/status.h" -#include "lib/sysdep/arch/x86_x64/x86_x64.h" -#include "lib/sysdep/cpu.h" // ERR::CPU_FEATURE_MISSING -#include "lib/sysdep/os_cpu.h" - -#include -#include - -ApicId GetApicId() -{ - x86_x64::CpuidRegs regs = { 0 }; - regs.eax = 1; - // note: CPUID function 1 is always supported, but only processors with - // an xAPIC (e.g. P4/Athlon XP) will return a nonzero ID. - [[maybe_unused]] bool ok = x86_x64::cpuid(®s); - ASSERT(ok); - const std::uint8_t apicId = static_cast(bits(regs.ebx, 24, 31)); - return 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 /*data*/) - { - processorApicIds[processor] = GetApicId(); - } - }; - // (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. - // (NB: we exclude single-processor systems in this test - - // having one zero-valued ID is legitimate) - if(numUnique == 1 && sortedApicIds[0] == 0 && numIds != 1) - { - debug_printf("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("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{ 0 }; - - -bool AreApicIdsReliable() -{ - ModuleInit(&apicInitState, InitApicIds); - if(apicInitState < 0) - return false; - return true; -} - -bool IsProcessorKnown(ApicId apicId) -{ - ModuleInit(&apicInitState, InitApicIds); - - const ApicId* pos = std::find(processorApicIds, processorApicIds+numIds, apicId); - return pos != processorApicIds+numIds; -} - -static size_t IndexFromApicId(const ApicId* apicIds, ApicId 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); -} diff --git a/source/lib/sysdep/arch/x86_x64/apic.h b/source/lib/sysdep/arch/x86_x64/apic.h deleted file mode 100644 index 3c466696f6..0000000000 --- a/source/lib/sysdep/arch/x86_x64/apic.h +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright (C) 2026 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. - */ - -#ifndef INCLUDED_X86_X64_APIC -#define INCLUDED_X86_X64_APIC - -#include - -typedef std::uint8_t ApicId; // not necessarily contiguous values - -/** - * @return APIC ID of the currently executing processor or zero if the - * platform does not have an xAPIC (i.e. 7th generation x86 or below). - * - * rationale: the alternative of accessing the APIC mmio registers is not - * feasible. We also don't want to interfere with the OS's constant use of - * the APIC registers. - **/ -ApicId GetApicId(); - -// if this returns false, apicId = contiguousId = processor. -// otherwise, there are unspecified but bijective mappings between -// apicId<->contiguousId and apicId<->processor. -bool AreApicIdsReliable(); - -// we may get the apicId of a processor we don't have access to. -bool IsProcessorKnown(ApicId apicId); - -size_t ProcessorFromApicId(ApicId apicId); -size_t ContiguousIdFromApicId(ApicId apicId); - -ApicId ApicIdFromProcessor(size_t contiguousId); -ApicId ApicIdFromContiguousId(size_t contiguousId); - -#endif // #ifndef INCLUDED_X86_X64_APIC