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

Improve Check Tool #473

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CheckTool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ available [here][checktool-release].
### Usage

1. `cd X:\path\to\CheckTool`
1. `checktool.exe --verbose`
1. `checktool.exe`

The output will be as below.

Expand Down Expand Up @@ -98,7 +98,7 @@ be omitted.
### Usage

1. `cd /path/to/CheckTool`
1. `./checktool --verbose`
1. `./checktool`

### Build

Expand Down Expand Up @@ -126,7 +126,7 @@ below.
| `cmake -DCMAKE_BUILD_TYPE=Release -B build/Release`
| `make -C build/Release`

[checktool-release]: https://github.com/intel/haxm/releases/tag/checktool-v1.0.0
[checktool-release]: https://github.com/intel/haxm/releases/tag/checktool-v1.1.0
[cmake]: https://cmake.org/download/
[install-on-macos]:
https://github.com/intel/haxm/wiki/Installation-Instructions-on-macOS
Expand Down
51 changes: 25 additions & 26 deletions CheckTool/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,48 @@
namespace haxm {
namespace check_util {

CheckResult ParseArguments(int &argc, char* argv[], bool &is_verbose) {
haxm::check_util::ArgParser arg_parser(argc, argv, {"-h", "--help", "-v",
"--verbose"});
CheckResult ParseArguments(int &argc, char* argv[]) {
CheckResult res = kPass;
ArgParser arg_parser(argc, argv, {"-h", "--help", "-v", "--version"});

if (!arg_parser.Verify()) {
std::cout << "checktool unknown option: " << arg_parser.error()
<< std::endl;
std::cout << "Usage: checktool [-h | --help] [-v | --verbose]"
std::cout << "Usage: checktool [-h | --help] [-v | --version]"
<< std::endl;
return haxm::check_util::kError;
return kError;
}

if (arg_parser.Test("-h") || arg_parser.Test("--help")) {
std::cout << "CheckTool version " << APP_VERSION << std::endl;
std::cout << "-v, --verbose Show detailed system information"
<< std::endl;
return haxm::check_util::kFail;
if (arg_parser.Test("-v") || arg_parser.Test("--version")) {
std::cout << "checktool " << APP_VERSION << std::endl;
std::cout << APP_COPYRIGHT << std::endl;

res = kFail;
}

if (arg_parser.Test("-v") || arg_parser.Test("--verbose")) {
is_verbose = true;
if (arg_parser.Test("-h") || arg_parser.Test("--help")) {
std::cout << "Usage: checktool [-v | --version]" << std::endl;
std::cout << "Check system environment for HAXM." << std::endl;
std::cout << "'*' means passed, while '-' means failed." << std::endl;

res = kFail;
}

return haxm::check_util::kPass;
return res;
}

int Check(bool is_verbose) {
int ret = 0;

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

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;
}

if (is_verbose) {
fd.Print();
}
fd.Print();

return ret;
return fd.status();
}

} // namespace check_util
Expand Down
8 changes: 5 additions & 3 deletions CheckTool/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@
namespace haxm {
namespace check_util {

#define APP_VERSION "1.0.0"
#define APP_VERSION "1.1.0"
#define APP_COPYRIGHT "Copyright (C) 2020 Intel Corporation"

enum CheckResult {
kUnknown = 0,
kPass,
kFail,
kNotApplicable, // e.g., CheckHypervDisabled() on macOS
kError,
kMaxResult
};

// Source:
Expand All @@ -52,8 +54,8 @@ inline static bool IsBitSet(uint32_t reg, int bit) {
return (reg >> bit) & 0x1;
}

CheckResult ParseArguments(int &argc, char* argv[], bool &is_verbose);
int Check(bool is_verbose);
CheckResult ParseArguments(int &argc, char* argv[]);
int Check();

} // namespace check_util
} // namespace haxm
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
10 changes: 3 additions & 7 deletions CheckTool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@

int main(int argc, char* argv[]) {
int ret = 0;
haxm::check_util::CheckResult result;
bool is_verbose = false;

result = haxm::check_util::ParseArguments(argc, argv, is_verbose);

switch (result) {
switch (haxm::check_util::ParseArguments(argc, argv)) {
case haxm::check_util::kPass: {
ret = haxm::check_util::Check(is_verbose);
ret = haxm::check_util::Check();
break;
}
case haxm::check_util::kError: {
ret = -1;
ret = 1;
break;
}
default: {
Expand Down
8 changes: 4 additions & 4 deletions CheckTool/version.rc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

#include <winres.h>

#define FILE_VERSION 1,0,0,0
#define FILE_VERSION_STR "1.0.0.0"
#define FILE_VERSION 1,1,0,0
#define FILE_VERSION_STR "1.1.0.0"

#define PRODUCT_VERSION 7,6,5,0
#define PRODUCT_VERSION_STR "7.6.5.0"
#define PRODUCT_VERSION 7,8,0,0
#define PRODUCT_VERSION_STR "7.8.0.0"

#ifdef DEBUG
#define FILE_FLAGS (VS_FF_PRIVATEBUILD | VS_FF_PRERELEASE | VERSION_DEBUG)
Expand Down