2 * check if hax is available. reference:target-i386/hax-all.c
4 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
7 * SeokYeon Hwang <syeon.hwang@samsung.com>
8 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
53 #define HAX_MAX_VCPU 0x10
55 #define HAX_CAP_STATUS_NOTWORKING 0x0
56 #define HAX_CAP_WORKSTATUS_MASK 0x1
57 #define HAX_CAP_FAILREASON_VT 0x1
58 #define HAX_CAP_FAILREASON_NX 0x2
60 #define HAX_CAP_MEMQUOTA 0x2
63 #define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
68 #define HAX_DEVICE_TYPE 0x4000
69 #define HAX_IOCTL_CAPABILITY CTL_CODE(HAX_DEVICE_TYPE, 0x910, METHOD_BUFFERED, FILE_ANY_ACCESS)
70 typedef HANDLE hax_fd;
76 struct hax_vcpu_state *vcpus[HAX_MAX_VCPU];
80 hax_fd fd; /* the global hax device interface */
86 struct hax_capabilityinfo {
88 * 0 - not working, possibly because NT/NX disabled
89 * bit 1: 1 - memory limitation working
90 * 0 - no memory limitation
93 /* valid when not working
94 * bit 0: VT not enabeld
95 * bit 1: NX not enabled*/
101 static inline int hax_invalid_fd( hax_fd fd ) {
102 return ( fd == INVALID_HANDLE_VALUE );
106 static inline int hax_invalid_fd(hax_fd fd)
113 static hax_fd hax_mod_open( void );
114 static int hax_open_device( hax_fd *fd );
115 static int hax_get_capability( struct hax_state *hax );
116 static int hax_capability( struct hax_state *hax, struct hax_capabilityinfo *cap );
118 static int check_hax( void ) {
120 struct hax_state hax;
121 memset( &hax, 0, sizeof( struct hax_state ) );
123 hax.fd = hax_mod_open();
125 int ret_fd = hax_invalid_fd( hax.fd );
127 fprintf( stderr, "Invalid fd:%d\n", ret_fd );
131 int ret_cap = hax_get_capability( &hax );
133 fprintf( stderr, "Not capable:%d\n", ret_cap );
141 static int hax_open_device( hax_fd *fd ) {
148 hDevice = CreateFile( "\\\\.\\HAX", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
151 if ( hDevice == INVALID_HANDLE_VALUE ) {
152 fprintf( stderr, "Failed to open the HAX device!\n" );
153 errNum = GetLastError();
154 if ( errNum == ERROR_FILE_NOT_FOUND )
159 fprintf( stdout, "device fd:%d\n", *fd );
163 static hax_fd hax_mod_open( void ) {
167 ret = hax_open_device( &fd );
169 fprintf( stderr, "Open HAX device failed\n" );
175 static hax_fd hax_mod_open(void)
177 int fd = open("/dev/HAX", O_RDWR);
181 fprintf(stderr, "hahFailed to open the hax module\n");
190 static int hax_get_capability( struct hax_state *hax ) {
192 struct hax_capabilityinfo capinfo, *cap = &capinfo;
194 ret = hax_capability( hax, cap );
198 if ( ( ( cap->wstatus & HAX_CAP_WORKSTATUS_MASK ) == HAX_CAP_STATUS_NOTWORKING ) ) {
199 if ( cap->winfo & HAX_CAP_FAILREASON_VT )
200 fprintf( stderr, "VTX feature is not enabled. which will cause HAX driver not working.\n" );
201 else if ( cap->winfo & HAX_CAP_FAILREASON_NX )
202 fprintf( stderr, "NX feature is not enabled, which will cause HAX driver not working.\n" );
207 if ( cap->wstatus & HAX_CAP_MEMQUOTA ) {
208 if ( cap->mem_quota < hax->mem_quota ) {
209 fprintf( stderr, "The memory needed by this VM exceeds the driver limit.\n" );
217 static int hax_capability( struct hax_state *hax, struct hax_capabilityinfo *cap ) {
219 HANDLE hDevice = hax->fd; //handle to hax module
223 if ( hax_invalid_fd( hDevice ) ) {
224 fprintf( stderr, "Invalid fd for hax device!\n" );
228 ret = DeviceIoControl( hDevice, HAX_IOCTL_CAPABILITY, NULL, 0, cap, sizeof( *cap ), &dSize, ( LPOVERLAPPED ) NULL );
231 err = GetLastError();
232 if ( err == ERROR_INSUFFICIENT_BUFFER || err == ERROR_MORE_DATA )
233 fprintf( stderr, "hax capability is too long to hold.\n" );
234 fprintf( stderr, "Failed to get Hax capability:%d\n", err );
243 int hax_capability(struct hax_state *hax, struct hax_capabilityinfo *cap)
247 ret = ioctl(hax->fd, HAX_IOCTL_CAPABILITY, cap);
250 fprintf(stderr, "Failed to get HAX capability\n");
258 int main(int argc, char* argv[]) {