VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/RTSystemGetNativeArch-win.cpp@ 107452

Last change on this file since 107452 was 107099, checked in by vboxsync, 2 months ago

IPRT: Adding RTArchValToString and RTSystemGetNativeArch for detecting win.amd64 binaries running in an emulator in win.arm64 and suchlike. jiraref:VBP-1466

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1/* $Id: RTSystemGetNativeArch-win.cpp 107099 2024-11-22 01:44:29Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemGetNativeArch, Windows ring-3.
4 */
5
6/*
7 * Copyright (C) 2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/system.h>
42#include "internal/iprt.h"
43
44#include <iprt/nt/nt-and-windows.h>
45
46#include "internal-r3-win.h"
47
48
49typedef BOOL (WINAPI *PFNISWOW64PROCESS2)(HANDLE, USHORT *, USHORT *);
50
51
52static uint32_t rtSystemNativeToArchVal(USHORT uMachine)
53{
54 switch (uMachine)
55 {
56 case IMAGE_FILE_MACHINE_I386: return RT_ARCH_VAL_X86;
57 case IMAGE_FILE_MACHINE_AMD64: return RT_ARCH_VAL_AMD64;
58 case IMAGE_FILE_MACHINE_ARM: return RT_ARCH_VAL_ARM32;
59 case IMAGE_FILE_MACHINE_ARM64: return RT_ARCH_VAL_ARM64;
60 default:
61 AssertMsgFailed(("Unknown value: uMachine=%#x\n", uMachine));
62 return 0;
63 }
64}
65
66
67RTDECL(uint32_t) RTSystemGetNativeArch(void)
68{
69 Assert(g_hModKernel32 != NULL);
70
71 /*
72 * We try IsWow64Process2 first.
73 */
74 static PFNISWOW64PROCESS2 s_pfnIsWow64Process2 = NULL;
75 PFNISWOW64PROCESS2 pfnIsWow64Process2 = s_pfnIsWow64Process2;
76 if (!pfnIsWow64Process2)
77 s_pfnIsWow64Process2 = pfnIsWow64Process2 = (PFNISWOW64PROCESS2)GetProcAddress(g_hModKernel32, "IsWow64Process2");
78 if (s_pfnIsWow64Process2)
79 {
80 USHORT uProcess = 0;
81 USHORT uNative = 0;
82 if (pfnIsWow64Process2(GetCurrentProcess(), &uProcess, &uNative))
83 return rtSystemNativeToArchVal(uNative);
84 AssertFailed();
85 }
86
87 /*
88 * The fallback is KUSER_SHARED_DATA::NativeProcessorArchitecture.
89 * This works for NT4 and later.
90 */
91 if (g_enmWinVer >= kRTWinOSType_NT4)
92 {
93 KUSER_SHARED_DATA volatile *pUserSharedData = (KUSER_SHARED_DATA volatile *)MM_SHARED_USER_DATA_VA;
94 return rtSystemNativeToArchVal(pUserSharedData->NativeProcessorArchitecture);
95 }
96 return RT_ARCH_VAL;
97}
98
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette