//------------------------------------------------------------------- // tryiopl3.cpp (A modification of our 'seeflags.cpp' demo) // // This program allows us to verify two aspects of CPU behavior // pertaining to the software interrupt instruction 'int $9' in // the x86_64 Linux environment: (1) Attempting to execute this // software interrupt will normally cause a segmentation fault; // but (2) after we install our 'iokludge.c' kernel module, the // segmentation fault no longer occurs, but instead the RFLAGS- // register's IOPL-field is set to 3, thereby allowing a normal // program to access I/O ports via 'in' and 'out' instructions. // // to compile: $ g++ tryiopl3.cpp -o tryiopl3 // to execute: $ ./tryiopl3 // // programmer: ALLAN CRUSE // written on: 20 APR 2009 //------------------------------------------------------------------- #include // for printf() long long rflags_image = ~0; int main( int argc, char **argv ) { // see if we can execute this software interrupt without faulting asm(" int $9 "); // transfer the image of the RFLAGS register to a global variable asm(" pushfq \n popq rflags_image "); // display the values of the variable and its 2-bit IOPL-subfield printf( "\n RFLAGS = 0x%016llX ", rflags_image ); printf( " IOPL = %X \n\n", (rflags_image >> 12)&3 ); }