Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
checktool: Enhance return value with error codes
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
wcwang committed Jan 20, 2023
1 parent 6664225 commit 6b95343
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 35 deletions.
15 changes: 6 additions & 9 deletions CheckTool/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,17 @@ CheckResult ParseArguments(int &argc, char* argv[]) {
}

int Check() {
int ret = 0;
FeatureDetector fd;

haxm::check_util::FeatureDetector fd;
haxm::check_util::CheckResult detect_res = fd.Detect();

if (detect_res == haxm::check_util::kError) {
ret = -1;
} else if (detect_res == haxm::check_util::kFail) {
ret = 1;
if (fd.Detect() == kError) {
std::cout << "The handle is invalid or the system command is executed "
"exceptionally." << std::endl;
return -1;
}

fd.Print();

return ret;
return fd.status();
}

} // namespace check_util
Expand Down
1 change: 1 addition & 0 deletions CheckTool/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum CheckResult {
kFail,
kNotApplicable, // e.g., CheckHypervDisabled() on macOS
kError,
kMaxResult
};

// Source:
Expand Down
101 changes: 76 additions & 25 deletions CheckTool/feature_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,49 @@
namespace haxm {
namespace check_util {

enum Check {
// Hardware support bit
kCpuSupported = 0,
kVmxSupported = 1,
kNxSupported = 2,
kEm64tSupported = 3,
kEptSupported = 4,
// BIOS configuration bit
kVmxEnabled = 8,
kNxEnabled = 9,
kEm64tEnabled = 10,
// Host status bit
kOsVerSupported = 16,
kOsArchSupported = 17,
kHypervDisabled = 18,
kSandboxDisabled = 19,
// Guest status bit
kGuestUnoccupied = 24,
kMaxCheck = 32
};

enum CheckFlag {
// Hardware support flag
kFlagCpuSupported = 1 << kCpuSupported,
kFlagVmxSupported = 1 << kVmxSupported,
kFlagNxSupported = 1 << kNxSupported,
kFlagEm64tSupported = 1 << kEm64tSupported,
kFlagEptSupported = 1 << kEptSupported,
// BIOS configuration flag
kFlagVmxEnabled = 1 << kVmxEnabled,
kFlagNxEnabled = 1 << kNxEnabled,
kFlagEm64tEnabled = 1 << kEm64tEnabled,
// Host status flag
kFlagOsverSupported = 1 << kOsVerSupported,
kFlagOsarchSupported = 1 << kOsArchSupported,
kFlagHypervDisabled = 1 << kHypervDisabled,
kFlagSandboxDisabled = 1 << kSandboxDisabled,
// Guest status flag
kFlagGuestUnoccupied = 1 << kGuestUnoccupied
};

FeatureDetector::FeatureDetector() {
status_ = 0;
os_ = new OsImpl();
}

Expand Down Expand Up @@ -145,47 +187,52 @@ std::string FeatureDetector::ToString(OsType os_type) {
}
}

CheckResult FeatureDetector::Detect() const {
CheckResult res[11];

res[0] = CheckCpuVendor();
res[1] = CheckLongModeSupported();
res[2] = CheckNxSupported();
res[3] = CheckNxEnabled();
res[4] = CheckOsVersion();
res[5] = CheckOsArchitecture();
res[6] = CheckGuestOccupied();
res[7] = CheckHyperVDisabled();
res[8] = CheckVmxSupported();
res[9] = CheckVmxEnabled();
res[10] = CheckEptSupported();

int check_num = 11;
CheckResult FeatureDetector::Detect() {
CheckResult res[kMaxCheck] = {};
int i;

res[kCpuSupported] = CheckCpuVendor();
res[kNxSupported] = CheckNxSupported();
res[kEm64tSupported] = CheckLongModeSupported();
res[kNxEnabled] = CheckNxEnabled();
res[kOsVerSupported] = CheckOsVersion();
res[kOsArchSupported] = CheckOsArchitecture();
res[kHypervDisabled] = CheckHyperVDisabled();
res[kGuestUnoccupied] = CheckGuestOccupied();

// When Hyper-V is enabled, it will affect the checking results of VMX
// supported, VMX enabled and EPT supported, so only the first 8 items are
// supported, EPT supported and VMX enabled, so only the first 8 items are
// checked. When Hyper-V is disabled, all items are checked.
if (res[7] == kFail) {
check_num = 8;
if (res[kHypervDisabled] != kFail) {
res[kVmxSupported] = CheckVmxSupported();
res[kEptSupported] = CheckEptSupported();
res[kVmxEnabled] = CheckVmxEnabled();
}

for (i = 0; i < kMaxCheck; ++i) {
if (res[i] == kFail) {
status_ |= 1 << i;
}
}

int detector[5] = {};
int detector[kMaxResult] = {};

for (int i = 0; i < check_num; ++i) {
for (i = 0; i < kMaxCheck; ++i) {
++detector[static_cast<int>(res[i])];
}

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

if (detector[static_cast<int>(kUnknown)] > 0) {
return kUnknown;
}

if (detector[static_cast<int>(kFail)] > 0) {
return kFail;
}

if (detector[static_cast<int>(kUnknown)] > 0) {
return kUnknown;
}

return kPass;
}

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

int FeatureDetector::status() const {
return status_;
}

} // namespace check_util
} // namespace haxm
4 changes: 3 additions & 1 deletion CheckTool/feature_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class FeatureDetector {
public:
FeatureDetector();
~FeatureDetector();
CheckResult Detect() const;
CheckResult Detect();
void Print() const;
int status() const;

private:
CheckResult CheckCpuVendor(std::string* vendor = nullptr) const;
Expand All @@ -62,6 +63,7 @@ class FeatureDetector {
static std::string ToString(CheckResult res);
static std::string ToString(OsType os_type);
static std::string ToString(OsArchitecture os_arch);
int status_;
Cpuid cpuid_;
Os* os_;
};
Expand Down

0 comments on commit 6b95343

Please sign in to comment.