Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
2,518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. class __attribute__((packed)) interrupt_wrapper
  2. {
  3.     using function_ptr = void(*)(unsigned);
  4.     unsigned int_vector;            // [eax-0x1C]
  5.     selector ds;                    // [eax-0x18]
  6.     selector es;                    // [eax-0x16]
  7.     selector fs;                    // [eax-0x14]
  8.     selector gs;                    // [eax-0x12]
  9.     function_ptr entry_point;       // [eax-0x10]
  10.     std::array<byte, 0x40> code;    // [eax-0x0C]
  11.  
  12. public:
  13.     interrupt_wrapper(unsigned vec, function_ptr f) : int_vector(vec), entry_point(f)
  14.     {
  15.         byte* start;
  16.         std::size_t size;
  17.         asm volatile (
  18.             ".intel_syntax noprefix;"
  19.             "jmp interrupt_wrapper_end%=;"
  20.             // --- \/\/\/\/\/\/ --- //
  21.             "interrupt_wrapper_begin%=:;"   // On entry, the only known register is CS.
  22.             "push ds; push es; push fs; push gs; pusha;"    // 7 bytes
  23.             "call get_eip%=;"  // call near/relative (E8)   // 5 bytes
  24.             "get_eip%=: pop eax;"           // Pop EIP into EAX and use it to find our vars
  25.             "mov ds, cs:[eax-0x18];"        // Restore segment registers
  26.             "mov es, cs:[eax-0x16];"        
  27.             "mov fs, cs:[eax-0x14];"
  28.             "mov gs, cs:[eax-0x12];"
  29.             "push cs:[eax-0x1C];"           // Pass our interrupt vector along
  30.             "call cs:[eax-0x10];"           // Call the entry point
  31.             "add esp, 4;"
  32.             "popa; pop gs; pop fs; pop es; pop ds;"
  33.             "sti;"                          // IRET may or may not reset the interrupt flag.
  34.             "iret;"
  35.             "interrupt_wrapper_end%=:;"
  36.             // --- /\/\/\/\/\/\ --- //
  37.             "mov %0, offset interrupt_wrapper_begin%=;"
  38.             "mov %1, offset interrupt_wrapper_end%=;"
  39.             "sub %1, %0;"
  40.             ".att_syntax prefix"
  41.             : "=r" (start)
  42.             , "=r" (size));
  43.         assert(size <= code.size());
  44.  
  45.         auto* ptr = memory_descriptor(get_cs(), start).get_ptr<byte>();
  46.         std::copy_n(ptr, size, code.data());
  47.  
  48.         asm volatile (
  49.             ".intel_syntax noprefix;"
  50.             "mov ax, ds;"
  51.             "mov bx, es;"
  52.             "mov cx, fs;"
  53.             "mov dx, gs;"
  54.             ".att_syntax prefix"
  55.             : "=a" (ds)
  56.             , "=b" (es)
  57.             , "=c" (fs)
  58.             , "=d" (gs));
  59.     }
  60.  
  61.     auto get_ptr(selector cs) { return far_ptr32 { cs, reinterpret_cast<std::size_t>(code.data()) }; }
  62. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement