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>
26 #include <palacios/vmm_intr.h>
28 #include <gears/sw_intr.h>
30 #ifdef V3_CONFIG_EXT_CODE_INJECT
31 #include <gears/code_inject.h>
34 #ifndef V3_CONFIG_DEBUG_EXT_SW_INTERRUPTS
36 #define PrintDebug(fmt, args...)
40 static int init_swintr_intercept (struct v3_vm_info * vm, v3_cfg_tree_t * cfg, void ** priv_data) {
45 static int init_swintr_core_svm (struct guest_info * core, void * priv_data) {
46 vmcb_t * vmcb = (vmcb_t*)core->vmm_data;
47 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
49 ctrl_area->instrs.INTn = 1;
54 static int init_swintr_core_vmx (struct guest_info * core, void * priv_data) {
55 PrintError("Not implemented!\n");
60 static int init_swintr_intercept_core (struct guest_info * core, void * priv_data) {
61 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
65 case V3_SVM_REV3_CPU: {
66 if (init_swintr_core_svm(core, priv_data) == -1) {
67 PrintError("Problem initializing svm software interrupt intercept\n");
74 case V3_VMX_EPT_UG_CPU: {
75 if (init_swintr_core_vmx(core, priv_data) == -1) {
76 PrintError("Problem initializing vmx software interrupt intercept\n");
82 PrintError("software interrupt interception not supported on this architecture\n");
89 struct v3_swintr_hook {
90 int (*handler)(struct guest_info * core, uint8_t vector, void * priv_data);
95 static struct v3_swintr_hook * swintr_hooks[256];
97 static inline struct v3_swintr_hook * get_swintr_hook (struct guest_info * core, uint8_t vector) {
98 return swintr_hooks[vector];
102 static struct v3_extension_impl swintr_impl = {
103 .name = "swintr_intercept",
104 .init = init_swintr_intercept,
106 .core_init = init_swintr_intercept_core,
113 register_extension(&swintr_impl);
116 int v3_handle_swintr (struct guest_info * core) {
119 void * instr_ptr = NULL;
120 struct x86_instr instr;
122 if (core->mem_mode == PHYSICAL_MEM) {
123 ret = v3_gpa_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
125 ret = v3_gva_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
129 PrintError("V3 SWintr Handler: Could not translate Instruction Address (%p)\n", (void *)core->rip);
133 if (v3_decode(core, (addr_t)instr_ptr, &instr) == -1) {
134 PrintError("V3 SWintr Handler: Decoding Error\n");
138 uint8_t vector = instr.dst_operand.operand;
140 struct v3_swintr_hook * hook = swintr_hooks[vector];
142 #ifdef V3_CONFIG_EXT_SWINTR_PASSTHROUGH
143 if (v3_hook_passthrough_swintr(core, vector) == -1) {
144 PrintDebug("V3 SWintr Handler: Error hooking passthrough swintr\n");
147 hook = swintr_hooks[vector];
149 core->rip += instr.instr_length;
150 return v3_raise_swintr(core, vector);
154 ret = hook->handler(core, vector, NULL);
156 PrintDebug("V3 SWintr Handler: Error in swintr hook\n");
160 #ifdef V3_CONFIG_EXT_CODE_INJECT
161 // this is for injecting page faults
162 // we don't want to increment rip or inject
163 // the swint if we need to fault a page in
164 if (ret == E_NEED_PF) {
168 /* at some point we _may_ need to prioritize swints
169 so that they finish in time for the next
171 core->rip += instr.instr_length;
172 return v3_raise_swintr(core, vector);
177 int v3_hook_swintr (struct guest_info * core,
179 int (*handler)(struct guest_info * core, uint8_t vector, void * priv_data),
182 struct v3_swintr_hook * hook = (struct v3_swintr_hook*)V3_Malloc(sizeof(struct v3_swintr_hook));
185 PrintError("Cannot allocate for swintr hook\n");
189 if (get_swintr_hook(core, vector) != NULL) {
190 PrintError("swint %d already hooked\n", vector);
194 hook->handler = handler;
195 hook->priv_data = priv_data;
197 swintr_hooks[vector] = hook;
199 PrintDebug("Hooked Swintr #%d\n", vector);
205 static int passthrough_swintr_handler (struct guest_info * core, uint8_t vector, void * priv_data) {
206 PrintDebug("[passthrough_swint_handler] INT vector=%d (guest=0x%p)\n",
207 vector, (void*)core);
212 int v3_hook_passthrough_swintr (struct guest_info * core, uint8_t vector) {
214 int rc = v3_hook_swintr(core, vector, passthrough_swintr_handler, NULL);
217 PrintError("guest_swintr_injection: failed to hook swint 0x%x (guest=0x%p)\n", vector, (void*)core);
220 PrintDebug("guest_swintr_injection: hooked swint 0x%x (guest=0x%p)\n", vector, (void*)core);
224 /* shouldn't get here */