2 * Palacios VM Graphics Console Interface (shared framebuffer between palacios and host)
3 * Copyright (c) 2011 Peter Dinda <pdinda@northwestern.edu>
6 #include <linux/device.h>
7 #include <linux/cdev.h>
8 #include <linux/errno.h>
10 #include <linux/uaccess.h>
11 #include <linux/poll.h>
12 #include <linux/anon_inodes.h>
13 #include <linux/file.h>
14 #include <linux/sched.h>
16 #include <interfaces/vmm_console.h>
17 #include <palacios/vmm_host_events.h>
18 #include "iface-graphics-console.h"
22 #include "linux-exts.h"
25 #include <linux/vmalloc.h>
29 This is an implementation of the Palacios Graphics Console interface that
30 is designed to interact with a vnc server running in user space,
31 typically something based on x0vncserver.
33 The basic idea is that we manage a frame buffer that we share with
34 palacios. Palacios draws whatever it likes on it.
35 The user-land vncserver will send us requests for fb updates, which
36 we implement by copying the FB to it. When the user-land sends us characters, etc,
37 we deliver those immediately to palacios via the deliver_key and deliver_mouse
38 event interfaces. The end-result is that whatever the graphics system
39 in palacios renders is visible via vnc.
44 static struct list_head global_gcons;
46 struct palacios_graphics_console {
47 // descriptor for the data in the shared frame buffer
48 struct v3_frame_buffer_spec spec;
50 // the actual shared frame buffer
51 // Note that "shared" here means shared between palacios and us
52 // This data could of course also be shared with userland
62 int (*render_request)(v3_graphics_console_t cons,
66 int (*update_inquire)(v3_graphics_console_t cons,
71 struct list_head gcons_node;
76 static v3_graphics_console_t g_open(void * priv_data,
77 struct v3_frame_buffer_spec *desired_spec,
78 struct v3_frame_buffer_spec *actual_spec)
80 struct v3_guest * guest = (struct v3_guest *)priv_data;
81 struct palacios_graphics_console * gc = NULL;
88 gc = get_vm_ext_data(guest, "GRAPHICS_CONSOLE_INTERFACE");
91 ERROR("palacios: Could not locate graphics console data for extension GRAPHICS_CONSOLE_INTERFACE\n");
95 if (gc->data != NULL) {
96 DEBUG("palacios: framebuffer already allocated - returning it\n");
98 *actual_spec = gc->spec;
105 mem = desired_spec->width * desired_spec->height * desired_spec->bytes_per_pixel;
107 DEBUG("palacios: allocating %u bytes for %u by %u by %u buffer\n",
108 mem, desired_spec->width, desired_spec->height, desired_spec->bytes_per_pixel);
110 gc->data = vmalloc(mem);
113 ERROR("palacios: unable to allocate memory for frame buffer\n");
117 gc->spec = *desired_spec;
119 *actual_spec = gc->spec;
125 INFO("palacios: allocated frame buffer\n");
130 static void g_close(v3_graphics_console_t cons)
132 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
137 if (gc->data_refcount < gc->cons_refcount) {
138 ERROR("palacios: error! data refcount is less than console refcount for graphics console\n");
141 if (gc->cons_refcount > 0) {
144 if (gc->cons_refcount < 0) {
145 ERROR("palacios: error! refcount for graphics console is negative on close!\n");
147 if (gc->data_refcount > 0) {
148 ERROR("palacios: error! refcount for graphics console data is positive on close - LEAKING MEMORY\n");
158 static void * g_get_data_read(v3_graphics_console_t cons,
159 struct v3_frame_buffer_spec *cur_spec)
161 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
163 if (gc->data_refcount<=0) {
164 ERROR("palacios: error! data refcount is <= 0 in get_data_read\n");
174 static void g_release_data_read(v3_graphics_console_t cons)
176 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
180 if (gc->data_refcount<=0) {
181 ERROR("palacios: error! data refcount is <= zero in release_data_read\n");
186 static void *g_get_data_rw(v3_graphics_console_t cons,
187 struct v3_frame_buffer_spec *cur_spec)
189 return g_get_data_read(cons,cur_spec);
193 static void g_release_data_rw(v3_graphics_console_t cons)
195 return g_release_data_read(cons);
199 static int g_changed(v3_graphics_console_t cons)
202 struct palacios_graphics_console *gc =
203 (struct palacios_graphics_console *) cons;
206 cr = gc->change_request;
208 gc->change_request=0;
217 static int g_register_render_request(
218 v3_graphics_console_t cons,
219 int (*render_request)(v3_graphics_console_t,
223 struct palacios_graphics_console *gc =
224 (struct palacios_graphics_console *) cons;
226 gc->render_data = priv_data;
227 gc->render_request = render_request;
229 INFO("palacios: installed rendering callback function for graphics console\n");
235 static int g_register_update_inquire(
236 v3_graphics_console_t cons,
237 int (*update_inquire)(v3_graphics_console_t,
241 struct palacios_graphics_console *gc =
242 (struct palacios_graphics_console *) cons;
244 gc->update_data = priv_data;
245 gc->update_inquire = update_inquire;
247 INFO("palacios: installed update inquiry callback function for graphics console\n");
253 static int palacios_graphics_console_key(struct v3_guest * guest,
254 struct palacios_graphics_console *cons,
257 struct v3_keyboard_event e;
259 e.scan_code = scancode;
261 //DEBUG("palacios: start key delivery\n");
263 v3_deliver_keyboard_event(guest->v3_ctx, &e);
265 //DEBUG("palacios: end key delivery\n");
270 static int palacios_graphics_console_mouse(struct v3_guest * guest,
271 struct palacios_graphics_console *cons,
272 uint8_t sx, uint8_t dx,
273 uint8_t sy, uint8_t dy,
277 struct v3_mouse_event e;
285 v3_deliver_mouse_event(guest->v3_ctx,&e);
290 static struct v3_graphics_console_hooks palacios_graphics_console_hooks =
295 .get_data_read = g_get_data_read,
296 .release_data_read = g_release_data_read,
297 .get_data_rw = g_get_data_rw,
298 .release_data_rw = g_release_data_rw,
300 .changed = g_changed,
301 .register_render_request = g_register_render_request,
302 .register_update_inquire = g_register_update_inquire,
306 static int graphics_console_init( void ) {
308 INIT_LIST_HEAD(&(global_gcons));
310 V3_Init_Graphics_Console(&palacios_graphics_console_hooks);
316 static int graphics_console_deinit( void ) {
317 struct palacios_graphics_console * gc = NULL;
318 struct palacios_graphics_console * tmp = NULL;
320 list_for_each_entry_safe(gc, tmp, &(global_gcons), gcons_node) {
321 list_del(&(gc->gcons_node));
332 static int fb_query(struct v3_guest * guest, unsigned int cmd, unsigned long arg,
335 struct palacios_graphics_console * cons = priv_data;
336 struct v3_fb_query_response q;
339 if (copy_from_user(&q, (void __user *) arg, sizeof(struct v3_fb_query_response))) {
340 ERROR("palacios: copy from user in getting query in fb\n");
344 switch (q.request_type) {
346 //INFO("palacios: request for db spec from Userland\n");
347 // returns only the spec for the FB
353 //DEBUG("palacios: test for fb updatei from Userland\n");
354 // returns whether an update is available for the region
355 if (cons->update_inquire) {
356 q.updated = cons->update_inquire(cons,cons->update_data);
360 //DEBUG("palacios: update=%d\n",q.updated);
362 // request a render, since a FB_DATA will probably soon come
363 cons->change_request = 1;
367 case V3_FB_DATA_BOX: {
368 // Not curently implemented
369 ERROR("palacios: request for data in bounding box unsupported currently\n");
376 case V3_FB_DATA_ALL: {
377 //DEBUG("palacios: got FrameBuffer Request from Userland\n");
378 // First let's sanity check to see if they are requesting the same
380 if (memcmp(&(q.spec),&(cons->spec),sizeof(struct v3_frame_buffer_spec))) {
381 ERROR("palacios: request for data with non-matching fb spec \n");
384 // Now we will force a render if we can
385 if (cons->render_request) {
386 //DEBUG("palacios: making rendering request\n");
387 cons->render_request(cons,cons->render_data);
390 // Now let's indicate an update is in the pointer and copy across the data
391 if (copy_to_user(q.data,cons->data,cons->spec.width*cons->spec.height*cons->spec.bytes_per_pixel)) {
392 ERROR("palacios: unable to copy fb content to user\n");
395 //DEBUG("palacios: FrameBuffer copy out done\n");
397 // Now we don't need to request a render
398 cons->change_request = 0;
406 // now we'll copy back any changes we made to the query/response structure
407 if (copy_to_user((void __user *) arg, (void*)&q, sizeof(struct v3_fb_query_response))) {
408 ERROR("palacios: unable to copy fb response to user\n");
416 static int fb_input(struct v3_guest * guest,
421 struct palacios_graphics_console * cons = priv_data;
422 struct v3_fb_input inp;
426 if (copy_from_user(&inp, (void __user *) arg, sizeof(struct v3_fb_input))) {
427 ERROR("palacios: copy from user in getting input in fb\n");
431 //DEBUG("palacios: input from Userland\n");
433 if ((inp.data_type == V3_FB_KEY) || (inp.data_type == V3_FB_BOTH)) {
434 rc = palacios_graphics_console_key(guest, cons, inp.scan_code);
435 //DEBUG("palacios: key delivered to palacios\n");
438 if ((inp.data_type == V3_FB_MOUSE) || (inp.data_type == V3_FB_BOTH)) {
439 rc |= palacios_graphics_console_mouse(guest, cons, inp.sx, inp.dx, inp.sy, inp.dy, inp.buttons);
440 //DEBUG("palacios: mouse delivered to palacios\n");
446 cons->change_request=1;
452 static int graphics_console_guest_init(struct v3_guest * guest, void ** vm_data) {
453 struct palacios_graphics_console * graphics_cons = palacios_alloc(sizeof(struct palacios_graphics_console));
455 if (!graphics_cons) {
456 ERROR("palacios: filed to do guest_init for graphics console\n");
460 memset(graphics_cons, 0, sizeof(struct palacios_graphics_console));
462 *vm_data = graphics_cons;
464 add_guest_ctrl(guest, V3_VM_FB_INPUT, fb_input, graphics_cons);
465 add_guest_ctrl(guest, V3_VM_FB_QUERY, fb_query, graphics_cons);
467 list_add(&(graphics_cons->gcons_node),&global_gcons);
474 static int graphics_console_guest_deinit(struct v3_guest * guest, void * vm_data) {
475 struct palacios_graphics_console * graphics_cons = (struct palacios_graphics_console *)vm_data;
477 list_del(&(graphics_cons->gcons_node));
479 if (graphics_cons->data) {
480 vfree(graphics_cons->data);
483 palacios_free(graphics_cons);
489 static struct linux_ext graphics_cons_ext = {
490 .name = "GRAPHICS_CONSOLE_INTERFACE",
491 .init = graphics_console_init,
492 .deinit = graphics_console_deinit,
493 .guest_init = graphics_console_guest_init,
494 .guest_deinit = graphics_console_guest_deinit,
498 register_extension(&graphics_cons_ext);