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.


672ca6c8e209ddf4f8730e0ff5a67c216f0f3a12
[palacios-OLD.git] / kitten / palacios-glue / vmm_stubs.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 <lwk/palacios.h>
21 #include <lwk/types.h>
22 #include <lwk/pmem.h>
23 #include <lwk/string.h>
24 #include <lwk/cpuinfo.h>
25 #include <lwk/kernel.h>
26 #include <arch/page.h>
27 #include <arch/ptrace.h>
28 #include <arch/apic.h>
29
30 struct guest_info * g_vm_guest = NULL;
31
32
33 // This is the function the interface code should call to deliver
34 // the interrupt to the vmm for handling
35 //extern int v3_deliver_interrupt(struct guest_info * vm, struct v3_interrupt *intr);
36
37
38 struct guest_info * irq_to_guest_map[256];
39
40
41
42
43
44 void
45 v3vee_init_stubs(
46         struct guest_info * info
47 )
48 {
49         memset(irq_to_guest_map, 0, sizeof(struct guest_info *) * 256);
50         g_vm_guest = info;
51 }
52
53
54 static void *
55 kitten_pa_to_va(void *ptr)
56 {
57         return (void*) __va(ptr);
58 }
59
60
61 static void *
62 kitten_va_to_pa(
63         void *                  ptr
64 )
65 {
66         return (void*) __pa(ptr);
67 }
68
69
70 static void *
71 Allocate_VMM_Pages(
72         int                     num_pages
73
74 {
75         struct pmem_region result;
76   
77         int rc = pmem_alloc_umem( num_pages*PAGE_SIZE,PAGE_SIZE, &result );
78         if( rc )
79                 return 0;
80
81         return result.start;
82 }
83
84 static void
85 Free_VMM_Page(
86         void *                  page
87
88 {
89         struct pmem_region      query;
90         struct pmem_region      result;
91
92         pmem_region_unset_all(&query);
93
94         query.start     = page;
95         query.end       = page+PAGE_SIZE;
96
97         int rc = pmem_query(&query,&result);
98
99         if (rc)
100                 panic( "BAD" );
101
102         result.allocated = 0;
103         pmem_update(&result);
104 }
105
106
107
108 void
109 send_key_to_vmm(
110         unsigned char           status,
111         unsigned char           scancode
112 )
113 {
114         if( !g_vm_guest )
115                 return;
116
117         struct v3_keyboard_event evt = {
118                 .status         = status,
119                 .scan_code      = scancode,
120         };
121
122         v3_deliver_keyboard_event( g_vm_guest, &evt );
123 }
124
125
126 void
127 send_mouse_to_vmm(
128         unsigned char           packet[3]
129 )
130 {
131         if( !g_vm_guest )
132                 return;
133
134         struct v3_mouse_event evt;
135         memcpy(evt.data, packet, 3);
136
137         v3_deliver_mouse_event(g_vm_guest, &evt);
138 }
139
140
141 void
142 send_tick_to_vmm(
143         unsigned int            period_us
144 )
145 {
146         if( !g_vm_guest )
147                 return;
148
149         struct v3_timer_event evt = {
150                 .period_us      = period_us,
151         };
152
153         v3_deliver_timer_event( g_vm_guest, &evt );
154 }
155
156
157 void
158 translate_intr_handler(
159         struct pt_regs *        regs,
160         unsigned int            vector
161
162 {
163         struct v3_interrupt intr = {
164                 .irq            = vector-32,
165                 .error          = regs->orig_rax,
166                 .should_ack     = 0,
167         };
168
169         //  PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
170         v3_deliver_irq( irq_to_guest_map[intr.irq], &intr );
171 }
172
173
174 int
175 kitten_hook_interrupt(
176         struct guest_info *     vm,
177         unsigned int            irq
178 )
179 {
180         if( irq_to_guest_map[irq] ) {
181                 //PrintBoth("Attempt to hook interrupt that is already hooked\n");
182                 return -1;
183         }
184
185         //PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n", irq, vm);
186         irq_to_guest_map[irq] = vm;
187
188         set_idtvec_handler( irq, translate_intr_handler );
189         return 0;
190 }
191
192
193 static int
194 ack_irq(
195         int                     irq
196
197 {
198         lapic_ack_interrupt();
199         return 0;
200 }
201
202   
203 static unsigned int
204 get_cpu_khz( void ) 
205 {
206         return cpu_info[0].arch.cur_cpu_khz;
207 }
208
209
210 struct v3_os_hooks v3vee_os_hooks = {
211         .print_debug            = printk,  // serial print ideally
212         .print_info             = printk,   // serial print ideally
213         .print_trace            = printk,  // serial print ideally
214         .allocate_pages         = Allocate_VMM_Pages, // defined in vmm_stubs
215         .free_page              = Free_VMM_Page, // defined in vmm_stubs
216         .malloc                 = kmem_alloc,
217         .free                   = kmem_free,
218         .vaddr_to_paddr         = kitten_va_to_pa,
219         .paddr_to_vaddr         = kitten_pa_to_va,
220         .hook_interrupt         = kitten_hook_interrupt,
221         .ack_irq                = ack_irq,
222         .get_cpu_khz            = get_cpu_khz,
223 };
224