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".
22 #include <palacios/vmm_events.h>
23 #include <palacios/vm_guest.h>
27 int v3_init_events(struct v3_vm_info * vm) {
28 struct v3_event_map * map = &(vm->event_map);
31 map->events = V3_Malloc(sizeof(struct list_head) * (V3_EVENT_INVALID+1));
33 if (map->events == NULL) {
34 PrintError(vm, VCORE_NONE, "Error: could not allocate event map\n");
38 // dead code if there are no events, but this is correct
39 for (i = 0; i < V3_EVENT_INVALID; i++) {
40 INIT_LIST_HEAD(&(map->events[i]));
46 int v3_deinit_events(struct v3_vm_info * vm) {
47 struct v3_event_map * map = &(vm->event_map);
51 // dead code if there are no events, but this is correct
52 for (i = 0; i < V3_EVENT_INVALID; i++) {
53 if (!list_empty(&(map->events[i]))) {
54 struct v3_notifier * tmp_notifier = NULL;
55 struct v3_notifier * safe_notifier = NULL;
56 PrintError(vm, VCORE_NONE, "Found orphan notifier for event %d. Probable memory leak detected.\n", i);
58 list_for_each_entry_safe(tmp_notifier, safe_notifier, &(map->events[i]), node) {
59 list_del(&(tmp_notifier->node));
60 V3_Free(tmp_notifier);
73 struct v3_notifier * v3_subscribe_event(struct v3_vm_info * vm,
74 v3_event_type_t event_type,
75 void (*notify)(struct guest_info * core,
76 v3_event_type_t event_type,
80 struct guest_info * current_core) {
81 struct v3_event_map * map = &(vm->event_map);
82 struct v3_notifier * notifier = NULL;
84 if (event_type >= V3_EVENT_INVALID) {
85 PrintError(vm, VCORE_NONE, "Tried to request illegal event (%d)\n", event_type);
89 notifier = V3_Malloc(sizeof(struct v3_notifier));
91 if (notifier == NULL) {
92 PrintError(vm, VCORE_NONE, "Error: Could not allocate notifier\n");
96 memset(notifier, 0, sizeof(struct v3_notifier));
98 notifier->notify = notify;
99 notifier->priv_data = priv_data;
100 notifier->event_type = event_type;
102 while (v3_raise_barrier(vm, current_core) == -1);
103 list_add(&(notifier->node), &(map->events[event_type]));
104 v3_lower_barrier(vm);
110 int v3_unsubscribe_event(struct v3_vm_info * vm, struct v3_notifier * notifier,
111 struct guest_info * current_core) {
112 struct v3_event_map * map = &(vm->event_map);
113 struct v3_notifier * tmp_notifier = NULL;
114 struct v3_notifier * safe_notifier = NULL;
116 if (notifier == NULL) {
117 PrintError(vm, VCORE_NONE, "Could not unsubscribe invalid event notifier\n");
121 if (notifier->event_type >= V3_EVENT_INVALID) {
122 PrintError(vm, VCORE_NONE, "Could not unsubscribe from invalid event\n");
126 while (v3_raise_barrier(vm, current_core) == -1);
127 list_for_each_entry_safe(tmp_notifier, safe_notifier, &(map->events[notifier->event_type]), node) {
128 if (tmp_notifier == notifier) {
129 list_del(&(tmp_notifier->node));
130 V3_Free(tmp_notifier);
133 v3_lower_barrier(vm);