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) 2012, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_exits.h>
23 #include <palacios/vm_guest.h>
26 int v3_init_exit_hooks(struct v3_vm_info * vm) {
27 struct v3_exit_map * map = &(vm->exit_map);
29 map->exits = V3_Malloc(sizeof(struct v3_exit_hook) * V3_EXIT_INVALID);
31 if (map->exits == NULL) {
32 PrintError(vm, VCORE_NONE, "Error allocating exit map\n");
36 memset(map->exits, 0, sizeof(struct v3_exit_hook) * V3_EXIT_INVALID);
42 int v3_deinit_exit_hooks(struct v3_vm_info * vm) {
43 struct v3_exit_map * map = &(vm->exit_map);
53 int v3_init_exit_hooks_core(struct guest_info * core) {
54 struct v3_vm_info * vm = core->vm_info;
55 struct v3_exit_map * map = &(vm->exit_map);
56 struct v3_exit_hook * hook = NULL;
59 for (i = 0; i < V3_EXIT_INVALID; i++) {
60 hook = &(map->exits[i]);
63 if (hook->enable(core, i) != 0) {
64 PrintError(core->vm_info, core, "Error could not enable exit hook %d on core %d\n", i, core->vcpu_id);
73 int v3_deinit_exit_hooks_core(struct guest_info * core) {
80 int v3_dispatch_exit_hook(struct guest_info * core, v3_exit_type_t exit_type, void * exit_data) {
81 struct v3_exit_map * map = &(core->vm_info->exit_map);
82 struct v3_exit_hook * hook = NULL;
84 if (exit_type >= V3_EXIT_INVALID) {
85 PrintError(core->vm_info, core, "Error: Tried to dispatch invalid exit type (%d)\n", exit_type);
89 hook = &(map->exits[exit_type]);
91 if (hook->hooked == 0) {
92 PrintError(core->vm_info, core, "Tried to dispatch an unhooked exit (%d)\n", exit_type);
96 return hook->handler(core, exit_type, hook->priv_data, exit_data);
101 int v3_register_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type,
102 int (*enable)(struct guest_info * core, v3_exit_type_t exit_type),
103 int (*disable)(struct guest_info * core, v3_exit_type_t exit_type)) {
104 struct v3_exit_map * map = &(vm->exit_map);
105 struct v3_exit_hook * hook = NULL;
107 if (exit_type >= V3_EXIT_INVALID) {
108 PrintError(vm, VCORE_NONE, "Error: Tried to register invalid exit type (%d)\n", exit_type);
112 hook = &(map->exits[exit_type]);
114 if (hook->registered == 1) {
115 PrintError(vm, VCORE_NONE, "Tried to reregister an exit (%d)\n", exit_type);
119 hook->registered = 1;
120 hook->enable = enable;
121 hook->disable = disable;
128 int v3_hook_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type,
129 int (*handler)(struct guest_info * core, v3_exit_type_t exit_type,
130 void * priv_data, void * exit_data),
132 struct guest_info * current_core) {
133 struct v3_exit_map * map = &(vm->exit_map);
134 struct v3_exit_hook * hook = NULL;
137 if (exit_type >= V3_EXIT_INVALID) {
138 PrintError(vm, VCORE_NONE, "Error: Tried to hook invalid exit type (%d)\n", exit_type);
142 hook = &(map->exits[exit_type]);
144 if (hook->registered == 0) {
145 PrintError(vm, VCORE_NONE, "Tried to hook unregistered exit (%d)\n", exit_type);
149 if (hook->hooked != 0) {
150 PrintError(vm, VCORE_NONE, "Tried to rehook exit (%d)\n", exit_type);
156 hook->handler = handler;
157 hook->priv_data = priv_data;
159 if (vm->run_state != VM_INVALID) {
162 while (v3_raise_barrier(vm, current_core) == -1);
164 for (i = 0; i < vm->num_cores; i++) {
166 if (hook->enable(&(vm->cores[i]), exit_type) != 0) {
167 PrintError(vm, VCORE_NONE, "Error could not enable exit hook %d on core %d\n", exit_type, i);
168 v3_lower_barrier(vm);
173 v3_lower_barrier(vm);
181 int v3_unhook_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type, struct guest_info * current_core) {
182 struct v3_exit_map * map = &(vm->exit_map);
183 struct v3_exit_hook * hook = NULL;
186 if (exit_type >= V3_EXIT_INVALID) {
187 PrintError(vm, VCORE_NONE, "Error: Tried to unhook invalid exit type (%d)\n", exit_type);
191 hook = &(map->exits[exit_type]);
193 if (hook->registered == 0) {
194 PrintError(vm, VCORE_NONE, "Tried to unhook an unregistered exit (%d)\n", exit_type);
198 if (hook->hooked == 0) {
199 PrintError(vm, VCORE_NONE, "Tried to unhook and unhooked exit (%d)\n", exit_type);
205 hook->handler = NULL;
206 hook->priv_data = NULL;
209 if (vm->run_state != VM_INVALID) {
212 while (v3_raise_barrier(vm, current_core) == -1);
214 for (i = 0; i < vm->num_cores; i++) {
216 if (hook->disable(&(vm->cores[i]), exit_type) != 0) {
217 PrintError(vm, VCORE_NONE, "Error could not enable exit hook %d on core %d\n", exit_type, i);
218 v3_lower_barrier(vm);
223 v3_lower_barrier(vm);