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.
45 struct palacios_graphics_console {
46 // descriptor for the data in the shared frame buffer
47 struct v3_frame_buffer_spec spec;
49 // the actual shared frame buffer
50 // Note that "shared" here means shared between palacios and us
51 // This data could of course also be shared with userland
61 int (*render_request)(v3_graphics_console_t cons,
65 int (*update_inquire)(v3_graphics_console_t cons,
73 static v3_graphics_console_t g_open(void * priv_data,
74 struct v3_frame_buffer_spec *desired_spec,
75 struct v3_frame_buffer_spec *actual_spec)
77 struct v3_guest * guest = (struct v3_guest *)priv_data;
78 struct palacios_graphics_console * gc = NULL;
85 gc = get_vm_ext_data(guest, "GRAPHICS_CONSOLE_INTERFACE");
88 ERROR("palacios: Could not locate graphics console data for extension GRAPHICS_CONSOLE_INTERFACE\n");
92 if (gc->data != NULL) {
93 DEBUG("palacios: framebuffer already allocated - returning it\n");
95 *actual_spec = gc->spec;
102 mem = desired_spec->width * desired_spec->height * desired_spec->bytes_per_pixel;
104 DEBUG("palacios: allocating %u bytes for %u by %u by %u buffer\n",
105 mem, desired_spec->width, desired_spec->height, desired_spec->bytes_per_pixel);
107 gc->data = vmalloc(mem);
110 ERROR("palacios: unable to allocate memory for frame buffer\n");
114 gc->spec = *desired_spec;
116 *actual_spec = gc->spec;
122 INFO("palacios: allocated frame buffer\n");
127 static void g_close(v3_graphics_console_t cons)
129 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
134 if (gc->data_refcount < gc->cons_refcount) {
135 ERROR("palacios: error! data refcount is less than console refcount for graphics console\n");
138 if (gc->cons_refcount > 0) {
141 if (gc->cons_refcount < 0) {
142 ERROR("palacios: error! refcount for graphics console is negative on close!\n");
144 if (gc->data_refcount > 0) {
145 ERROR("palacios: error! refcount for graphics console data is positive on close - LEAKING MEMORY\n");
155 static void * g_get_data_read(v3_graphics_console_t cons,
156 struct v3_frame_buffer_spec *cur_spec)
158 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
160 if (gc->data_refcount<=0) {
161 ERROR("palacios: error! data refcount is <= 0 in get_data_read\n");
171 static void g_release_data_read(v3_graphics_console_t cons)
173 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
177 if (gc->data_refcount<=0) {
178 ERROR("palacios: error! data refcount is <= zero in release_data_read\n");
183 static void *g_get_data_rw(v3_graphics_console_t cons,
184 struct v3_frame_buffer_spec *cur_spec)
186 return g_get_data_read(cons,cur_spec);
190 static void g_release_data_rw(v3_graphics_console_t cons)
192 return g_release_data_read(cons);
196 static int g_changed(v3_graphics_console_t cons)
199 struct palacios_graphics_console *gc =
200 (struct palacios_graphics_console *) cons;
203 cr = gc->change_request;
205 gc->change_request=0;
214 static int g_register_render_request(
215 v3_graphics_console_t cons,
216 int (*render_request)(v3_graphics_console_t,
220 struct palacios_graphics_console *gc =
221 (struct palacios_graphics_console *) cons;
223 gc->render_data = priv_data;
224 gc->render_request = render_request;
226 INFO("palacios: installed rendering callback function for graphics console\n");
232 static int g_register_update_inquire(
233 v3_graphics_console_t cons,
234 int (*update_inquire)(v3_graphics_console_t,
238 struct palacios_graphics_console *gc =
239 (struct palacios_graphics_console *) cons;
241 gc->update_data = priv_data;
242 gc->update_inquire = update_inquire;
244 INFO("palacios: installed update inquiry callback function for graphics console\n");
250 static int palacios_graphics_console_key(struct v3_guest * guest,
251 struct palacios_graphics_console *cons,
254 struct v3_keyboard_event e;
256 e.scan_code = scancode;
258 //DEBUG("palacios: start key delivery\n");
260 v3_deliver_keyboard_event(guest->v3_ctx, &e);
262 //DEBUG("palacios: end key delivery\n");
267 static int palacios_graphics_console_mouse(struct v3_guest * guest,
268 struct palacios_graphics_console *cons,
269 uint8_t x, uint8_t y, uint8_t buttons)
271 struct v3_mouse_event e;
274 e.data[2]=buttons; // These three are completely wrong, of course - ignoring mouse for now
276 // mouse delivery is broken, so don't do it.
277 // v3_deliver_mouse_event(guest->v3_ctx,&e);
282 static struct v3_graphics_console_hooks palacios_graphics_console_hooks =
287 .get_data_read = g_get_data_read,
288 .release_data_read = g_release_data_read,
289 .get_data_rw = g_get_data_rw,
290 .release_data_rw = g_release_data_rw,
292 .changed = g_changed,
293 .register_render_request = g_register_render_request,
294 .register_update_inquire = g_register_update_inquire,
298 static int graphics_console_init( void ) {
300 V3_Init_Graphics_Console(&palacios_graphics_console_hooks);
306 static int fb_query(struct v3_guest * guest, unsigned int cmd, unsigned long arg,
309 struct palacios_graphics_console * cons = priv_data;
310 struct v3_fb_query_response q;
313 if (copy_from_user(&q, (void __user *) arg, sizeof(struct v3_fb_query_response))) {
314 ERROR("palacios: copy from user in getting query in fb\n");
318 switch (q.request_type) {
320 //INFO("palacios: request for db spec from Userland\n");
321 // returns only the spec for the FB
327 //DEBUG("palacios: test for fb updatei from Userland\n");
328 // returns whether an update is available for the region
329 if (cons->update_inquire) {
330 q.updated = cons->update_inquire(cons,cons->update_data);
334 //DEBUG("palacios: update=%d\n",q.updated);
336 // request a render, since a FB_DATA will probably soon come
337 cons->change_request = 1;
341 case V3_FB_DATA_BOX: {
342 // Not curently implemented
343 ERROR("palacios: request for data in bounding box unsupported currently\n");
350 case V3_FB_DATA_ALL: {
351 //DEBUG("palacios: got FrameBuffer Request from Userland\n");
352 // First let's sanity check to see if they are requesting the same
354 if (memcmp(&(q.spec),&(cons->spec),sizeof(struct v3_frame_buffer_spec))) {
355 ERROR("palacios: request for data with non-matching fb spec \n");
358 // Now we will force a render if we can
359 if (cons->render_request) {
360 //DEBUG("palacios: making rendering request\n");
361 cons->render_request(cons,cons->render_data);
364 // Now let's indicate an update is in the pointer and copy across the data
365 if (copy_to_user(q.data,cons->data,cons->spec.width*cons->spec.height*cons->spec.bytes_per_pixel)) {
366 ERROR("palacios: unable to copy fb content to user\n");
369 //DEBUG("palacios: FrameBuffer copy out done\n");
371 // Now we don't need to request a render
372 cons->change_request = 0;
380 // now we'll copy back any changes we made to the query/response structure
381 if (copy_to_user((void __user *) arg, (void*)&q, sizeof(struct v3_fb_query_response))) {
382 ERROR("palacios: unable to copy fb response to user\n");
390 static int fb_input(struct v3_guest * guest,
395 struct palacios_graphics_console * cons = priv_data;
396 struct v3_fb_input inp;
400 if (copy_from_user(&inp, (void __user *) arg, sizeof(struct v3_fb_input))) {
401 ERROR("palacios: copy from user in getting input in fb\n");
405 //DEBUG("palacios: input from Userland\n");
407 if ((inp.data_type == V3_FB_KEY) || (inp.data_type == V3_FB_BOTH)) {
408 rc = palacios_graphics_console_key(guest, cons, inp.scan_code);
409 //DEBUG("palacios: key delivered to palacios\n");
412 if ((inp.data_type == V3_FB_MOUSE) || (inp.data_type == V3_FB_BOTH)) {
413 rc |= palacios_graphics_console_mouse(guest, cons, inp.mouse_data[0],
414 inp.mouse_data[1], inp.mouse_data[2]);
415 //DEBUG("palacios: mouse delivered to palacios\n");
421 cons->change_request=1;
427 static int graphics_console_guest_init(struct v3_guest * guest, void ** vm_data) {
428 struct palacios_graphics_console * graphics_cons = kmalloc(sizeof(struct palacios_graphics_console), GFP_KERNEL);
430 if (!graphics_cons) {
431 ERROR("palacios: filed to do guest_init for graphics console\n");
435 memset(graphics_cons, 0, sizeof(struct palacios_graphics_console));
437 *vm_data = graphics_cons;
439 add_guest_ctrl(guest, V3_VM_FB_INPUT, fb_input, graphics_cons);
440 add_guest_ctrl(guest, V3_VM_FB_QUERY, fb_query, graphics_cons);
447 static struct linux_ext graphics_cons_ext = {
448 .name = "GRAPHICS_CONSOLE_INTERFACE",
449 .init = graphics_console_init,
451 .guest_init = graphics_console_guest_init,
456 register_extension(&graphics_cons_ext);