Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qemulationdetector_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QEMULATIONDETECTOR_P_H
5#define QEMULATIONDETECTOR_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19
20#if defined(Q_OS_LINUX) && defined(Q_PROCESSOR_ARM)
21#define SHOULD_CHECK_ARM_ON_X86
22
23#include <QFileInfo>
24
25#if QT_CONFIG(process) && QT_CONFIG(regularexpression)
26#include <QProcess>
27#include <QRegularExpression>
28#endif
29
30#endif
31
33
34// Helper functions for detecting if running emulated
35namespace QTestPrivate {
36
37#ifdef SHOULD_CHECK_ARM_ON_X86
38static bool isX86SpecificFileAvailable(void);
39static bool isReportedArchitectureX86(void);
40#endif
41
42/*
43 * Check if we are running Arm binary on x86 machine.
44 *
45 * Currently this is only able to check on Linux. If not able to
46 * detect, return false.
47 */
48[[maybe_unused]] static bool isRunningArmOnX86()
49{
50#ifdef SHOULD_CHECK_ARM_ON_X86
51 if (isX86SpecificFileAvailable())
52 return true;
53
54 if (isReportedArchitectureX86())
55 return true;
56#endif
57 return false;
58}
59
60#ifdef SHOULD_CHECK_ARM_ON_X86
61/*
62 * Check if we can find a file that's only available on x86
63 */
64static bool isX86SpecificFileAvailable()
65{
66 // MTRR (Memory Type Range Registers) are a feature of the x86 architecture
67 // and /proc/mtrr is only present (on Linux) for that family.
68 // However, it's an optional kernel feature, so the absence of the file is
69 // not sufficient to conclude we're on real hardware.
70 QFileInfo mtrr("/proc/mtrr");
71 if (mtrr.exists())
72 return true;
73 return false;
74}
75
76/*
77 * Check if architecture reported by the OS is x86
78 */
79static bool isReportedArchitectureX86(void)
80{
81#if QT_CONFIG(process) && QT_CONFIG(regularexpression)
82 QProcess unamer;
83 QString machineString;
84
85 // Using syscall "uname" is not possible since that would be captured by
86 // QEMU and result would be the architecture being emulated (e.g. armv7l).
87 // By using QProcess we get the architecture used by the host.
88 unamer.start("uname -a");
89 if (!unamer.waitForFinished()) {
90 return false;
91 }
92 machineString = unamer.readAll();
93
94 // Is our current host cpu x86?
95 if (machineString.contains(QRegularExpression("i386|i686|x86"))) {
96 return true;
97 }
98#endif
99
100 return false;
101}
102#endif // SHOULD_CHECK_ARM_ON_X86
103
104} // QTestPrivate namespace
105
107
108#endif
109
\inmodule QtCore \reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
static bool isRunningArmOnX86()