Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


65749c147a24597ce2a07c641d187b6a306491e3
[palacios.git] / palacios / src / palacios / vmm_hypercall.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm_hypercall.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vm_guest.h>
23
24 void v3_init_hypercall_map(struct guest_info * info) {
25     info->hcall_map.rb_node = NULL;
26 }
27
28
29 struct hypercall {
30     uint_t id;
31   
32     int (*hcall_fn)(struct guest_info * info, uint_t hcall_id, void * priv_data);
33     void * priv_data;
34   
35     struct rb_node tree_node;
36 };
37
38
39
40 static inline struct hypercall * __insert_hypercall(struct guest_info * info, struct hypercall * hcall) {
41     struct rb_node ** p = &(info->hcall_map.rb_node);
42     struct rb_node * parent = NULL;
43     struct hypercall * tmp_hcall = NULL;
44
45     while (*p) {
46         parent = *p;
47         tmp_hcall = rb_entry(parent, struct hypercall, tree_node);
48
49         if (hcall->id < tmp_hcall->id) {
50             p = &(*p)->rb_left;
51         } else if (hcall->id > tmp_hcall->id) {
52             p = &(*p)->rb_right;
53         } else {
54             return tmp_hcall;
55         }
56     }
57
58     rb_link_node(&(hcall->tree_node), parent, p);
59
60     return NULL;
61 }
62
63
64 static inline struct hypercall * insert_hypercall(struct guest_info * info, struct hypercall * hcall) {
65     struct hypercall * ret;
66
67     if ((ret = __insert_hypercall(info, hcall))) {
68         return ret;
69     }
70
71     v3_rb_insert_color(&(hcall->tree_node), &(info->hcall_map));
72
73     return NULL;
74 }
75
76
77 static struct hypercall * get_hypercall(struct guest_info * info, uint_t id) {
78     struct rb_node * n = info->hcall_map.rb_node;
79     struct hypercall * hcall = NULL;
80
81     while (n) {
82         hcall = rb_entry(n, struct hypercall, tree_node);
83     
84         if (id < hcall->id) {
85             n = n->rb_left;
86         } else if (id > hcall->id) {
87             n = n->rb_right;
88         } else {
89             return hcall;
90         }
91     }
92
93     return NULL;
94 }
95
96
97 int v3_register_hypercall(struct guest_info * info, uint_t hypercall_id, 
98                           int (*hypercall)(struct guest_info * info, uint_t hcall_id, void * priv_data), 
99                           void * priv_data) {
100
101     struct hypercall * hcall = (struct hypercall *)V3_Malloc(sizeof(struct hypercall));
102
103     hcall->id = hypercall_id;
104     hcall->priv_data = priv_data;
105     hcall->hcall_fn = hypercall;
106
107     if (insert_hypercall(info, hcall)) {
108         V3_Free(hcall);
109         return -1;
110     }
111
112     return 0;
113 }
114
115
116 int v3_handle_hypercall(struct guest_info * info) {
117     uint_t hypercall_id = *(uint_t *)&info->vm_regs.rax;
118
119     struct hypercall * hcall = get_hypercall(info, hypercall_id);
120
121     if (!hcall) {
122         PrintError("Invalid Hypercall (%d(0x%x) not registered)\n", 
123                    hypercall_id, hypercall_id);
124         return -1;
125     }
126
127     return hcall->hcall_fn(info, hypercall_id, hcall->priv_data);
128 }