Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6b95343

Browse files
committedJan 20, 2023
checktool: Enhance return value with error codes
Currently the result of failed check is returned as 1, which causes the installer cannot read the failed reason exactly. * Use the error code as the return value of check tool - 0 means success - Positive values mean the failed check (returns 1 when the command line argument is invalid) - Negative values mean runtime fault * Support 11 system checks, each of which is indicated by a certain bit in the positive error code. The return value may be the bitwise combination of these failed status bits. Signed-off-by: Wenchao Wang <wenchao.wang@intel.com>
1 parent 6664225 commit 6b95343

File tree

4 files changed

+86
-35
lines changed

4 files changed

+86
-35
lines changed
 

‎CheckTool/common.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ CheckResult ParseArguments(int &argc, char* argv[]) {
6868
}
6969

7070
int Check() {
71-
int ret = 0;
71+
FeatureDetector fd;
7272

73-
haxm::check_util::FeatureDetector fd;
74-
haxm::check_util::CheckResult detect_res = fd.Detect();
75-
76-
if (detect_res == haxm::check_util::kError) {
77-
ret = -1;
78-
} else if (detect_res == haxm::check_util::kFail) {
79-
ret = 1;
73+
if (fd.Detect() == kError) {
74+
std::cout << "The handle is invalid or the system command is executed "
75+
"exceptionally." << std::endl;
76+
return -1;
8077
}
8178

8279
fd.Print();
8380

84-
return ret;
81+
return fd.status();
8582
}
8683

8784
} // namespace check_util

‎CheckTool/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum CheckResult {
4545
kFail,
4646
kNotApplicable, // e.g., CheckHypervDisabled() on macOS
4747
kError,
48+
kMaxResult
4849
};
4950

5051
// Source:

‎CheckTool/feature_detector.cpp

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,49 @@
4444
namespace haxm {
4545
namespace check_util {
4646

47+
enum Check {
48+
// Hardware support bit
49+
kCpuSupported = 0,
50+
kVmxSupported = 1,
51+
kNxSupported = 2,
52+
kEm64tSupported = 3,
53+
kEptSupported = 4,
54+
// BIOS configuration bit
55+
kVmxEnabled = 8,
56+
kNxEnabled = 9,
57+
kEm64tEnabled = 10,
58+
// Host status bit
59+
kOsVerSupported = 16,
60+
kOsArchSupported = 17,
61+
kHypervDisabled = 18,
62+
kSandboxDisabled = 19,
63+
// Guest status bit
64+
kGuestUnoccupied = 24,
65+
kMaxCheck = 32
66+
};
67+
68+
enum CheckFlag {
69+
// Hardware support flag
70+
kFlagCpuSupported = 1 << kCpuSupported,
71+
kFlagVmxSupported = 1 << kVmxSupported,
72+
kFlagNxSupported = 1 << kNxSupported,
73+
kFlagEm64tSupported = 1 << kEm64tSupported,
74+
kFlagEptSupported = 1 << kEptSupported,
75+
// BIOS configuration flag
76+
kFlagVmxEnabled = 1 << kVmxEnabled,
77+
kFlagNxEnabled = 1 << kNxEnabled,
78+
kFlagEm64tEnabled = 1 << kEm64tEnabled,
79+
// Host status flag
80+
kFlagOsverSupported = 1 << kOsVerSupported,
81+
kFlagOsarchSupported = 1 << kOsArchSupported,
82+
kFlagHypervDisabled = 1 << kHypervDisabled,
83+
kFlagSandboxDisabled = 1 << kSandboxDisabled,
84+
// Guest status flag
85+
kFlagGuestUnoccupied = 1 << kGuestUnoccupied
86+
};
87+
4788
FeatureDetector::FeatureDetector() {
89+
status_ = 0;
4890
os_ = new OsImpl();
4991
}
5092

@@ -145,47 +187,52 @@ std::string FeatureDetector::ToString(OsType os_type) {
145187
}
146188
}
147189

148-
CheckResult FeatureDetector::Detect() const {
149-
CheckResult res[11];
150-
151-
res[0] = CheckCpuVendor();
152-
res[1] = CheckLongModeSupported();
153-
res[2] = CheckNxSupported();
154-
res[3] = CheckNxEnabled();
155-
res[4] = CheckOsVersion();
156-
res[5] = CheckOsArchitecture();
157-
res[6] = CheckGuestOccupied();
158-
res[7] = CheckHyperVDisabled();
159-
res[8] = CheckVmxSupported();
160-
res[9] = CheckVmxEnabled();
161-
res[10] = CheckEptSupported();
162-
163-
int check_num = 11;
190+
CheckResult FeatureDetector::Detect() {
191+
CheckResult res[kMaxCheck] = {};
192+
int i;
193+
194+
res[kCpuSupported] = CheckCpuVendor();
195+
res[kNxSupported] = CheckNxSupported();
196+
res[kEm64tSupported] = CheckLongModeSupported();
197+
res[kNxEnabled] = CheckNxEnabled();
198+
res[kOsVerSupported] = CheckOsVersion();
199+
res[kOsArchSupported] = CheckOsArchitecture();
200+
res[kHypervDisabled] = CheckHyperVDisabled();
201+
res[kGuestUnoccupied] = CheckGuestOccupied();
202+
164203
// When Hyper-V is enabled, it will affect the checking results of VMX
165-
// supported, VMX enabled and EPT supported, so only the first 8 items are
204+
// supported, EPT supported and VMX enabled, so only the first 8 items are
166205
// checked. When Hyper-V is disabled, all items are checked.
167-
if (res[7] == kFail) {
168-
check_num = 8;
206+
if (res[kHypervDisabled] != kFail) {
207+
res[kVmxSupported] = CheckVmxSupported();
208+
res[kEptSupported] = CheckEptSupported();
209+
res[kVmxEnabled] = CheckVmxEnabled();
210+
}
211+
212+
for (i = 0; i < kMaxCheck; ++i) {
213+
if (res[i] == kFail) {
214+
status_ |= 1 << i;
215+
}
169216
}
170217

171-
int detector[5] = {};
218+
int detector[kMaxResult] = {};
172219

173-
for (int i = 0; i < check_num; ++i) {
220+
for (i = 0; i < kMaxCheck; ++i) {
174221
++detector[static_cast<int>(res[i])];
175222
}
176223

177224
if (detector[static_cast<int>(kError)] > 0) {
178225
return kError;
179226
}
180227

181-
if (detector[static_cast<int>(kUnknown)] > 0) {
182-
return kUnknown;
183-
}
184-
185228
if (detector[static_cast<int>(kFail)] > 0) {
186229
return kFail;
187230
}
188231

232+
if (detector[static_cast<int>(kUnknown)] > 0) {
233+
return kUnknown;
234+
}
235+
189236
return kPass;
190237
}
191238

@@ -265,5 +312,9 @@ void FeatureDetector::Print() const {
265312
<< occupied_count << " guest(s)" << std::endl;
266313
}
267314

315+
int FeatureDetector::status() const {
316+
return status_;
317+
}
318+
268319
} // namespace check_util
269320
} // namespace haxm

‎CheckTool/feature_detector.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class FeatureDetector {
4444
public:
4545
FeatureDetector();
4646
~FeatureDetector();
47-
CheckResult Detect() const;
47+
CheckResult Detect();
4848
void Print() const;
49+
int status() const;
4950

5051
private:
5152
CheckResult CheckCpuVendor(std::string* vendor = nullptr) const;
@@ -62,6 +63,7 @@ class FeatureDetector {
6263
static std::string ToString(CheckResult res);
6364
static std::string ToString(OsType os_type);
6465
static std::string ToString(OsArchitecture os_arch);
66+
int status_;
6567
Cpuid cpuid_;
6668
Os* os_;
6769
};

0 commit comments

Comments
 (0)
This repository has been archived.