From 6ceb982c652a10d31f7f906f20e69a965a17f85e Mon Sep 17 00:00:00 2001 From: Kitae Kim Date: Tue, 25 Nov 2014 18:49:28 +0900 Subject: [PATCH] check-hax: check ug feature on Windows and Mac OS X. emulator-manager will set default smp value depending on the result of checking ug feature. Change-Id: Ie3a60f2675f1f34ae1727a7a5610fdbc7288bc3b Signed-off-by: Kitae Kim --- tizen/src/util/check_hax.c | 143 +++++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 64 deletions(-) diff --git a/tizen/src/util/check_hax.c b/tizen/src/util/check_hax.c index 7c072e3..54b6eec 100644 --- a/tizen/src/util/check_hax.c +++ b/tizen/src/util/check_hax.c @@ -58,6 +58,7 @@ #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) @@ -70,6 +71,10 @@ typedef int hax_fd; 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; @@ -98,8 +103,9 @@ struct hax_capabilityinfo { 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__ @@ -110,63 +116,69 @@ static inline int hax_invalid_fd(hax_fd fd) #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; @@ -176,66 +188,69 @@ static hax_fd hax_mod_open(void) { 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 @@ -245,8 +260,7 @@ int hax_capability(struct hax_state *hax, struct hax_capabilityinfo *cap) 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; } @@ -255,6 +269,7 @@ int hax_capability(struct hax_state *hax, struct hax_capabilityinfo *cap) } #endif -int main(int argc, char* argv[]) { +int main(int argc, char* argv[]) +{ return check_hax(); } -- 2.7.4