1 //
2 
3 #include <cpuid.h>
4 #include <cstddef>
5 
6 int
main(int argc,char const * argv[])7 main(int argc, char const *argv[])
8 {
9 // PR_MPX_ENABLE_MANAGEMENT won't be defined on linux kernel versions below 3.19
10 #ifndef PR_MPX_ENABLE_MANAGEMENT
11     return -1;
12 #endif
13 
14     // This call returns 0 only if the CPU and the kernel support Intel(R) MPX.
15     if (prctl(PR_MPX_ENABLE_MANAGEMENT, 0, 0, 0, 0) != 0)
16         return -1;
17 
18 // Run Intel(R) MPX test code.
19 #if defined(__x86_64__)
20     asm("mov $16, %rax\n\t"
21         "mov $9, %rdx\n\t"
22         "bndmk (%rax,%rdx), %bnd0\n\t"
23         "mov $32, %rax\n\t"
24         "mov $9, %rdx\n\t"
25         "bndmk (%rax,%rdx), %bnd1\n\t"
26         "mov $48, %rax\n\t"
27         "mov $9, %rdx\n\t"
28         "bndmk (%rax,%rdx), %bnd2\n\t"
29         "mov $64, %rax\n\t"
30         "mov $9, %rdx\n\t"
31         "bndmk (%rax,%rdx), %bnd3\n\t"
32         "bndstx %bnd3, (%rax) \n\t"
33         "nop\n\t");
34 #endif
35 #if defined(__i386__)
36     asm("mov $16, %eax\n\t"
37         "mov $9, %edx\n\t"
38         "bndmk (%eax,%edx), %bnd0\n\t"
39         "mov $32, %eax\n\t"
40         "mov $9, %edx\n\t"
41         "bndmk (%eax,%edx), %bnd1\n\t"
42         "mov $48, %eax\n\t"
43         "mov $9, %edx\n\t"
44         "bndmk (%eax,%edx), %bnd2\n\t"
45         "mov $64, %eax\n\t"
46         "mov $9, %edx\n\t"
47         "bndmk (%eax,%edx), %bnd3\n\t"
48         "bndstx  %bnd3, (%eax)\n\t"
49         "nop\n\t");
50 #endif
51     asm("nop\n\t"); // Set a break point here.
52 
53     return 0;
54 }
55