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