void * Identity(void *addr);
 
+void Hook_IRQ(int irq, void (*handler)());
+
 #endif
 
 
   memcpy(&(data->thereg), src, 1);
 
-  return 0;
+  return 1;
 }
 
 
 
   memcpy(dst, &(data->mem_state[data->thereg]), 1);
 
-  return 0;
+  return 1;
 }
 
 int nvram_write_data_port(ushort_t port,
 
   memcpy(&(data->mem_state[data->thereg]), src, 1);
 
-  return 0;
+  return 1;
 }
 
 
 
 
 
 int IO_Read(ushort_t port, void * dst, uint_t length, void * priv_data) {
-  uchar_t * iter = dst;
-  uint_t i;
 
-  for (i = 0; i < length; i++) {
-    *iter = VM_In_Byte(port);    
-    iter++;
+  if (length != 1) {
+    return 0;
   }
-  
-  return 0;
+
+  *(uchar_t*)dst = VM_In_Byte(port);    
+  return 1;
 }
 
 
 
 int IO_Write(ushort_t port, void * src, uint_t length, void * priv_data) {
-  uchar_t * iter = src;
-  uint_t i;
 
-
-  for (i = 0; i < length; i++) {
-    VM_Out_Byte(port, *iter);    
-    iter++;
+  if (length != 1) {
+    return 0;
   }
 
-  return 0;
+  VM_Out_Byte(port, *(uchar_t *)src);    
+
+  return 1;
 }
 
 
 
       {
        struct vm_device * nvram = create_nvram();
-       struct vm_device * timer = create_timer();
+       //struct vm_device * timer = create_timer();
 
        attach_device(&(vm_info), nvram);
        //attach_device(&(vm_info), timer);
 
 
       }
-      PrintDebug("foo\n");
 
 
       vm_info.rip = 0xfff0;
 
 extern uint_t launch_svm(vmcb_t * vmcb_addr);
 extern void safe_svm_launch(vmcb_t * vmcb_addr, struct guest_gprs * gprs);
 
+extern void STGI();
+extern void CLGI();
+
 extern uint_t Get_CR3();
 
-extern void GetGDTR(void * gdt);
-extern void GetIDTR(void * idt);
 
 extern void DisableInts();
 
 
   while (1) {
 
+    CLGI();
+
     //PrintDebug("SVM Launch Args (vmcb=%x), (info=%x), (vm_regs=%x)\n", info->vmm_data,  &(info->vm_regs));
     //PrintDebug("Launching to RIP: %x\n", info->rip);
     safe_svm_launch((vmcb_t*)(info->vmm_data), &(info->vm_regs));
     //launch_svm((vmcb_t*)(info->vmm_data));
-    //PrintDebug("SVM Returned\n");
+    // PrintDebug("SVM Returned\n");
 
+    STGI();
+    
     if (handle_svm_exit(info) != 0) {
-      // handle exit code....
+      PrintDebug("SVM ERROR!!\n");
       break;
     }
   }
   }
 
 
-  if (vm_info.irq_map.num_hooks > 0) {
-    PrintDebug("Exiting on interrupts\n");
-    ctrl_area->guest_ctrl.V_INTR_MASKING = 1;
-    ctrl_area->instrs.INTR = 1;
-  }
+
+  PrintDebug("Exiting on interrupts\n");
+  ctrl_area->guest_ctrl.V_INTR_MASKING = 1;
+  ctrl_area->instrs.INTR = 1;
 
 
   if (vm_info.page_mode == SHADOW_PAGING) {
 }
 
 
+#if 0
 void Init_VMCB_pe(vmcb_t *vmcb, struct guest_info vm_info) {
   vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
   vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
 
 
 
-
+#endif
 
 
 
 
  
   // PrintDebugVMCB((vmcb_t*)(info->vmm_data));
 
+
+  // PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data); 
+  //PrintDebug("RIP: %x\n", guest_state->rip);
+
+  
+  //PrintDebug("SVM Returned: Exit Code: %x\n",exit_code); 
+  
   if (exit_code == VMEXIT_IOIO) {
     struct svm_io_info * io_info = (struct svm_io_info *)&(guest_ctrl->exit_info1);
     
     if (io_info->type == 0) {
       if (io_info->str) {
-       handle_svm_io_outs(info);
+       if (handle_svm_io_outs(info) == -1 ) {
+         return -1;
+       }
       } else {
-       handle_svm_io_out(info);
+       if (handle_svm_io_out(info) == -1) {
+         return -1;
+       }
       }
     } else {
       if (io_info->str) {
-       handle_svm_io_ins(info);
+       if (handle_svm_io_ins(info) == -1) {
+         return -1;
+       }
       } else {
-       handle_svm_io_in(info);
+       if (handle_svm_io_in(info) == -1) {
+         return -1;
+       }
       }
     }
   } else if (exit_code == VMEXIT_CR0_WRITE) {
     */
 
   } else if (exit_code == VMEXIT_INTR) {
-    handle_svm_intr(info);
+
+    //    handle_svm_intr(info);
   } else {
     addr_t rip_addr = get_addr_linear(info, guest_state->rip, guest_state->cs.selector);
     char buf[15];
 
   uint_t read_size = 0;
 
   if (hook == NULL) {
+    PrintDebug("Hook Not present for in on port %x\n", io_info->port);
     // error, we should not have exited on this port
     return -1;
   }
 
-  //PrintDebug("IN on  port %d (0x%x)\n", io_info->port, io_info->port);
+  PrintDebug("IN on  port %d (0x%x)\n", io_info->port, io_info->port);
 
   if (io_info->sz8) { 
     read_size = 1;
 
   if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
     // not sure how we handle errors.....
+    PrintDebug("Read Failure for in on port %x\n", io_info->port);
     return -1;
   }
 
 
 
   if (hook == NULL) {
+   PrintDebug("Hook Not present for ins on port %x\n", io_info->port);
     // error, we should not have exited on this port
     return -1;
   }
 
-  //PrintDebug("INS on  port %d (0x%x)\n", io_info->port, io_info->port);
+  PrintDebug("INS on  port %d (0x%x)\n", io_info->port, io_info->port);
 
   if (io_info->sz8) { 
     read_size = 1;
 
     if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
       // not sure how we handle errors.....
+      PrintDebug("Read Failure for ins on port %x\n", io_info->port);
       return -1;
     }
 
   uint_t write_size = 0;
 
   if (hook == NULL) {
+    PrintDebug("Hook Not present for out on port %x\n", io_info->port);
     // error, we should not have exited on this port
     return -1;
   }
 
-  //PrintDebug("OUT on  port %d (0x%x)\n", io_info->port, io_info->port);
+  PrintDebug("OUT on  port %d (0x%x)\n", io_info->port, io_info->port);
 
   if (io_info->sz8) { 
     write_size = 1;
 
   if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
     // not sure how we handle errors.....
+    PrintDebug("Write Failure for out on port %x\n", io_info->port);
     return -1;
   }
 
 
 
   if (hook == NULL) {
+    PrintDebug("Hook Not present for outs on port %x\n", io_info->port);
     // error, we should not have exited on this port
     return -1;
   }
 
-  //PrintDebug("OUTS on  port %d (0x%x)\n", io_info->port, io_info->port);
+  PrintDebug("OUTS on  port %d (0x%x)\n", io_info->port, io_info->port);
 
   if (io_info->sz8) { 
     write_size = 1;
 
     if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
       // not sure how we handle errors.....
+      PrintDebug("Write Failure for outs on port %x\n", io_info->port);
       return -1;
     }
 
 
 
 EXPORT DisableInts
 
-EXPORT GetGDTR
-EXPORT GetIDTR
-EXPORT GetTR
 
 EXPORT exit_test
 
 EXPORT launch_svm
 EXPORT safe_svm_launch
 
+EXPORT STGI
+EXPORT CLGI
 
 
 
        db      00fh, 001h, 0dah
 %endmacro
 
+%macro stgi 0
+       db      00fh, 001h, 0dch
+%endmacro
+
+%macro clgi 0
+       db      00fh, 001h, 0ddh
+%endmacro
+
 ;VMRUN  equ db 0Fh, 01h, D8h
 ;VMLOAD equ db 0x0F,0x01,0xDA
 ;VMSAVE equ db 0x0F,0x01,0xDB
        ret
 
 
-align 8
-GetGDTR:
-       push    ebp
-       mov     ebp, esp
-       pusha   
-       mov     ebx, [ebp + 8]
-       sgdt    [ebx]
-       
-       popa
-       pop     ebp
-       ret
-
 
 align 8
-GetIDTR:
-       push    ebp
-       mov     ebp, esp
-       pusha   
-
-       mov     ebx, [ebp + 8]
-       sidt    [ebx]
-       
-       popa
-       pop     ebp
+CLGI:
+       clgi
        ret
 
-
-
 align 8
-GetTR:
-       push    ebp
-       mov     ebp, esp
-       pusha   
-       mov     ebx, [ebp + 8]
-       str     [ebx]
-       
-       popa
-       pop     ebp
+STGI:
+       stgi
        ret