Check whether userfaultfd is available

    I have been focusing on linux for several years, most time I read articles blogged by others and found they were great helpful. Now it's my turn.

    Started from April, I found great interest in qemu/KVM. Qemu is a big project with a sophisticated architecture. A great amount of knowledge to learn. OMG!

    Userfaultfd system call is the key component of qemu/KVM postcopy live migration. It requires linux kernel 4.3 or above and userfaultfd feature enabled in kernel configuration. This little app checks whether this feature is available.

/*
* Check whether USERFAULFD is available.
* CPU architecture related preprocessor macros can be found here:
* https://sourceforge.net/p/predef/wiki/Architectures/
*
* references:
* [1] https://www.kernel.org/doc/Documentation/vm/userfaultfd.txt
* [2] https://lists.gnu.org/archive/html/qemu-devel/2016-04/msg04137.html
* [3] http://xiaogr.com/?p=96
*/
#include<stdio.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#if defined(__LP64__) || defined(_LP64)
#define __NR_userfaultfd 323
#else
#define __NR_userfaultfd 374
#endif

#if !defined(__linux__)
int check(void){
	printf("Must be run in linux!\n");
	return -1;
}
#else
/*
* where do you locate userfaultfd.h?
*/
 #include"userfaultfd.h"
int check(void){
	struct uffdio_api userfaultapi;
	int tmp=syscall(__NR_userfaultfd,O_CLOEXEC);
	if (tmp<0){
		printf("1:Check failed:%s\n",strerror(errno));
		return -1;
	}
	userfaultapi.api=UFFD_API;
	userfaultapi.features=0;
	tmp=ioctl(tmp,UFFDIO_API,&userfaultapi);
	if (tmp<0){
		printf("2:Check failed:%s\n",strerror(errno));
		return -1;
	}
	printf("Success:ioctls bitmask:%llx\n",userfaultapi.ioctls);
	return 0;
}
#endif

int main(void){
#if !defined(__i386__) && !defined(__x86_64__)
	printf("Must be run in i386 or x86_64 arch!\n");
	return -1;
#endif
	return(check());
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值