#define HAX_CAP_FAILREASON_NX 0x2
#define HAX_CAP_MEMQUOTA 0x2
+#define HAX_CAP_UG 0x4
#ifdef __APPLE__
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
typedef HANDLE hax_fd;
#endif
+#define RET_SUPPORT_HAX_AND_UG 0
+#define RET_NO_SUPPORT_UG 1
+#define RET_NO_SUPPORT_HAX 2
+
struct hax_vm {
hax_fd fd;
int id;
uint64_t mem_quota;
};
#ifdef _WIN32
-static inline int hax_invalid_fd( hax_fd fd ) {
- return ( fd == INVALID_HANDLE_VALUE );
+static inline int hax_invalid_fd(hax_fd fd)
+{
+ return (fd == INVALID_HANDLE_VALUE);
}
#endif
#ifdef __APPLE__
#endif
-static hax_fd hax_mod_open( void );
-static int hax_open_device( hax_fd *fd );
-static int hax_get_capability( struct hax_state *hax );
-static int hax_capability( struct hax_state *hax, struct hax_capabilityinfo *cap );
-
-static int check_hax( void ) {
+static hax_fd hax_mod_open(void);
+static int hax_open_device(hax_fd *fd);
+static int hax_get_capability(struct hax_state *hax);
+static int hax_capability(struct hax_state *hax, struct hax_capabilityinfo *cap);
+static int check_hax(void)
+{
struct hax_state hax;
- memset( &hax, 0, sizeof( struct hax_state ) );
+ memset(&hax, 0, sizeof(struct hax_state));
hax.fd = hax_mod_open();
- int ret_fd = hax_invalid_fd( hax.fd );
- if ( ret_fd ) {
- fprintf( stderr, "Invalid fd:%d\n", ret_fd );
- return ret_fd;
+ int ret_fd = hax_invalid_fd(hax.fd);
+ if (ret_fd) {
+ fprintf(stderr, "No support HAX!! Invalid fd: %d\n", hax.fd);
+ return RET_NO_SUPPORT_HAX;
}
- int ret_cap = hax_get_capability( &hax );
- if ( ret_cap ) {
- fprintf( stderr, "Not capable:%d\n", ret_cap );
- return ret_cap;
- }
+ int ret_cap = hax_get_capability(&hax);
- return 0;
+ if (ret_cap) {
+ fprintf(stderr, "Not capable: %d\n", ret_cap);
+ return ((ret_cap == 1) ? RET_NO_SUPPORT_UG : RET_NO_SUPPORT_HAX);
+ }
+ fprintf(stdout, "Support HAX and UG feature\n");
+ return RET_SUPPORT_HAX_AND_UG;
}
+
#ifdef _WIN32
-static int hax_open_device( hax_fd *fd ) {
+static int hax_open_device(hax_fd *fd)
+{
uint32_t errNum = 0;
HANDLE hDevice;
- if ( !fd )
+ if (!fd) {
+ fprintf(stderr, "hax_fd is NULL\n");
return -2;
+ }
- hDevice = CreateFile( "\\\\.\\HAX", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
- NULL );
+ hDevice = CreateFile("\\\\.\\HAX", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if ( hDevice == INVALID_HANDLE_VALUE ) {
- fprintf( stderr, "Failed to open the HAX device!\n" );
+ if (hDevice == INVALID_HANDLE_VALUE) {
+ fprintf(stderr, "Failed to open the HAX device!\n");
errNum = GetLastError();
- if ( errNum == ERROR_FILE_NOT_FOUND )
+ if (errNum == ERROR_FILE_NOT_FOUND)
return -1;
return -2;
}
*fd = hDevice;
- fprintf( stdout, "device fd:%d\n", *fd );
+ fprintf(stdout, "device fd:%d\n", *fd);
return 0;
}
-static hax_fd hax_mod_open( void ) {
+static hax_fd hax_mod_open(void)
+{
int ret;
hax_fd fd;
- ret = hax_open_device( &fd );
- if ( ret != 0 ) {
- fprintf( stderr, "Open HAX device failed\n" );
+ ret = hax_open_device(&fd);
+ if (ret != 0) {
+ fprintf(stderr, "Open HAX device failed\n");
+ fd = INVALID_HANDLE_VALUE;
}
return fd;
{
int fd = open("/dev/HAX", O_RDWR);
- if (fd == -1)
- {
- fprintf(stderr, "hahFailed to open the hax module\n");
- //return -errno;
+ if (fd == -1) {
+ fprintf(stderr, "Failed to open the hax module\n");
}
return fd;
}
-
#endif
-static int hax_get_capability( struct hax_state *hax ) {
+static int hax_get_capability(struct hax_state *hax)
+{
int ret;
struct hax_capabilityinfo capinfo, *cap = &capinfo;
- ret = hax_capability( hax, cap );
- if ( ret )
+ ret = hax_capability(hax, cap);
+ if (ret) {
return ret;
+ }
- if ( ( ( cap->wstatus & HAX_CAP_WORKSTATUS_MASK ) == HAX_CAP_STATUS_NOTWORKING ) ) {
- if ( cap->winfo & HAX_CAP_FAILREASON_VT )
- fprintf( stderr, "VTX feature is not enabled. which will cause HAX driver not working.\n" );
- else if ( cap->winfo & HAX_CAP_FAILREASON_NX )
- fprintf( stderr, "NX feature is not enabled, which will cause HAX driver not working.\n" );
+ if (((cap->wstatus & HAX_CAP_WORKSTATUS_MASK) == HAX_CAP_STATUS_NOTWORKING)) {
+ if (cap->winfo & HAX_CAP_FAILREASON_VT)
+ fprintf(stderr, "VTX feature is not enabled. which will cause HAX driver not working.\n");
+ else if (cap->winfo & HAX_CAP_FAILREASON_NX)
+ fprintf(stderr, "NX feature is not enabled, which will cause HAX driver not working.\n");
return -ENXIO;
}
-/*
- if ( cap->wstatus & HAX_CAP_MEMQUOTA ) {
- if ( cap->mem_quota < hax->mem_quota ) {
- fprintf( stderr, "The memory needed by this VM exceeds the driver limit.\n" );
+ if (!(cap->winfo & HAX_CAP_UG)) {
+ fprintf(stderr, "UG feature is not supported.\n");
+ ret = 1;
+ }
+
+ if (cap->wstatus & HAX_CAP_MEMQUOTA) {
+ if (cap->mem_quota < hax->mem_quota) {
+ fprintf(stderr, "The memory needed by this VM exceeds the driver limit.\n");
return -ENOSPC;
}
}
-*/
- return 0;
+
+ return ret;
}
#ifdef _WIN32
-static int hax_capability( struct hax_state *hax, struct hax_capabilityinfo *cap ) {
+static int hax_capability(struct hax_state *hax, struct hax_capabilityinfo *cap)
+{
int ret;
HANDLE hDevice = hax->fd; //handle to hax module
DWORD dSize = 0;
DWORD err = 0;
- if ( hax_invalid_fd( hDevice ) ) {
- fprintf( stderr, "Invalid fd for hax device!\n" );
+ if (hax_invalid_fd(hDevice)) {
+ fprintf(stderr, "Invalid fd for hax device!\n");
return -ENODEV;
}
- ret = DeviceIoControl( hDevice, HAX_IOCTL_CAPABILITY, NULL, 0, cap, sizeof( *cap ), &dSize, ( LPOVERLAPPED ) NULL );
-
- if ( !ret ) {
+ ret = DeviceIoControl(hDevice, HAX_IOCTL_CAPABILITY, NULL, 0, cap, sizeof(*cap), &dSize, (LPOVERLAPPED) NULL);
+ if (!ret) {
err = GetLastError();
- if ( err == ERROR_INSUFFICIENT_BUFFER || err == ERROR_MORE_DATA )
- fprintf( stderr, "hax capability is too long to hold.\n" );
- fprintf( stderr, "Failed to get Hax capability:%d\n", err );
+ if (err == ERROR_INSUFFICIENT_BUFFER || err == ERROR_MORE_DATA)
+ fprintf(stderr, "hax capability is too long to hold.\n");
+ fprintf(stderr, "Failed to get Hax capability:%d\n", err);
return -EFAULT;
- } else
- return 0;
+ }
+ return 0;
}
#endif
int ret;
ret = ioctl(hax->fd, HAX_IOCTL_CAPABILITY, cap);
- if (ret == -1)
- {
+ if (ret == -1) {
fprintf(stderr, "Failed to get HAX capability\n");
return -errno;
}
}
#endif
-int main(int argc, char* argv[]) {
+int main(int argc, char* argv[])
+{
return check_hax();
}