2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2011, Kyle C. Hale <kh@u.norhtwestern.edu>
11 * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Kyle C. Hale <kh@u.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vmcb.h>
22 #include <palacios/vm_guest.h>
23 #include <palacios/vm_guest_mem.h>
24 #include <palacios/vmm_decoder.h>
25 #include <palacios/vmm_extensions.h>
27 #include <interfaces/sw_intr.h>
29 #ifndef V3_CONFIG_DEBUG_EXT_SW_INTERRUPTS
31 #define PrintDebug(fmt, args...)
35 static int init_swintr_intercept (struct v3_vm_info * vm, v3_cfg_tree_t * cfg, void ** priv_data) {
41 static int init_swintr_intercept_core (struct guest_info * core, void * priv_data) {
42 vmcb_t * vmcb = (vmcb_t*)core->vmm_data;
43 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
45 ctrl_area->instrs.INTn = 1;
51 struct v3_swintr_hook {
52 int (*handler)(struct guest_info * core, uint8_t vector, void * priv_data);
57 static struct v3_swintr_hook * swintr_hooks[256];
60 static inline struct v3_swintr_hook * get_swintr_hook (struct guest_info * core, uint8_t vector) {
61 return swintr_hooks[vector];
65 static struct v3_extension_impl swintr_impl = {
66 .name = "swintr_intercept",
67 .init = init_swintr_intercept,
69 .core_init = init_swintr_intercept_core,
76 register_extension(&swintr_impl);
79 int v3_handle_swintr (struct guest_info * core) {
82 void * instr_ptr = NULL;
83 struct x86_instr instr;
85 if (core->mem_mode == PHYSICAL_MEM) {
86 ret = v3_gpa_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
88 ret = v3_gva_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
92 PrintError("V3 SWintr Handler: Could not translate Instruction Address (%p)\n", (void *)core->rip);
96 if (v3_decode(core, (addr_t)instr_ptr, &instr) == -1) {
97 PrintError("V3 SWintr Handler: Decoding Error\n");
101 uint8_t vector = instr.dst_operand.operand;
103 struct v3_swintr_hook * hook = swintr_hooks[vector];
105 #ifdef V3_CONFIG_EXT_SWINTR_PASSTHROUGH
106 if (v3_hook_passthrough_swintr(core, vector) == -1) {
107 PrintDebug("V3 SWintr Handler: Error hooking passthrough swintr\n");
110 hook = swintr_hooks[vector];
112 core->rip += instr.instr_length;
113 return v3_signal_swintr(core, vector);
117 ret = hook->handler(core, vector, NULL);
119 PrintDebug("V3 SWintr Handler: Error in swintr hook\n");
123 /* at some point we _may_ need to prioritize swints
124 so that they finish in time for the next
126 core->rip += instr.instr_length;
127 return v3_signal_swintr(core, vector);
130 int v3_signal_swintr (struct guest_info * core, uint8_t vector) {
131 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
133 PrintDebug("Signaling software interrupt in v3_signal_swintr()\n");
134 PrintDebug("\tINT vector: %d\n", vector);
136 intr_state->swintr_posted = 1;
137 intr_state->swintr_vector = vector;
142 int v3_hook_swintr (struct guest_info * core,
144 int (*handler)(struct guest_info * core, uint8_t vector, void * priv_data),
147 struct v3_swintr_hook * hook = (struct v3_swintr_hook*)V3_Malloc(sizeof(struct v3_swintr_hook));
153 if (get_swintr_hook(core, vector) != NULL) {
154 PrintError("swint %d already hooked\n", vector);
158 hook->handler = handler;
159 hook->priv_data = priv_data;
161 swintr_hooks[vector] = hook;
167 static int passthrough_swintr_handler (struct guest_info * core, uint8_t vector, void * priv_data) {
168 PrintDebug("[passthrough_swint_handler] INT vector=%d (guest=0x%p)\n",
169 vector, (void*)core);
174 int v3_hook_passthrough_swintr (struct guest_info * core, uint8_t vector) {
176 int rc = v3_hook_swintr(core, vector, passthrough_swintr_handler, NULL);
179 PrintError("guest_swintr_injection: failed to hook swint 0x%x (guest=0x%p)\n", vector, (void*)core);
182 PrintDebug("guest_swintr_injection: hooked swint 0x%x (guest=0x%p)\n", vector, (void*)core);
186 /* shouldn't get here */