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
59 // Currently keystrokes and mouse movements are ignored
61 // currently, we will not worry about locking this
66 static v3_graphics_console_t g_open(void * priv_data,
67 struct v3_frame_buffer_spec *desired_spec,
68 struct v3_frame_buffer_spec *actual_spec)
70 struct v3_guest * guest = (struct v3_guest *)priv_data;
71 struct palacios_graphics_console * gc = NULL;
78 gc = get_vm_ext_data(guest, "GRAPHICS_CONSOLE_INTERFACE");
81 printk("palacios: Could not locate graphics console data for extension GRAPHICS_CONSOLE_INTERFACE\n");
85 if (gc->data != NULL) {
86 printk("palacios: framebuffer already allocated - returning it\n");
88 *actual_spec = gc->spec;
95 mem = desired_spec->width * desired_spec->height * desired_spec->bytes_per_pixel;
97 printk("palacios: allocating %u bytes for %u by %u by %u buffer\n",
98 mem, desired_spec->width, desired_spec->height, desired_spec->bytes_per_pixel);
100 gc->data = vmalloc(mem);
103 printk("palacios: unable to allocate memory for frame buffer\n");
107 gc->spec = *desired_spec;
109 *actual_spec = gc->spec;
115 printk("palacios: allocated frame buffer\n");
120 static void g_close(v3_graphics_console_t cons)
122 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
127 if (gc->data_refcount < gc->cons_refcount) {
128 printk("palacios: error! data refcount is less than console refcount for graphics console\n");
131 if (gc->cons_refcount > 0) {
134 if (gc->cons_refcount < 0) {
135 printk("palacios: error! refcount for graphics console is negative on close!\n");
137 if (gc->data_refcount > 0) {
138 printk("palacios: error! refcount for graphics console data is positive on close - LEAKING MEMORY\n");
148 static void * g_get_data_read(v3_graphics_console_t cons,
149 struct v3_frame_buffer_spec *cur_spec)
151 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
153 if (gc->data_refcount<=0) {
154 printk("palacios: error! data refcount is <= 0 in get_data_read\n");
164 static void g_release_data_read(v3_graphics_console_t cons)
166 struct palacios_graphics_console *gc = (struct palacios_graphics_console *) cons;
170 if (gc->data_refcount<=0) {
171 printk("palacios: error! data refcount is <= zero in release_data_read\n");
176 static void *g_get_data_rw(v3_graphics_console_t cons,
177 struct v3_frame_buffer_spec *cur_spec)
179 return g_get_data_read(cons,cur_spec);
183 static void g_release_data_rw(v3_graphics_console_t cons)
185 return g_release_data_read(cons);
189 static int g_changed(v3_graphics_console_t cons)
193 struct palacios_graphics_console *gc =
194 (struct palacios_graphics_console *) cons;
196 int rc = !(gc->num_updates % 1000);
208 static int palacios_graphics_console_key(struct v3_guest * guest,
209 struct palacios_graphics_console *cons,
212 struct v3_keyboard_event e;
214 e.scan_code = scancode;
216 v3_deliver_keyboard_event(guest->v3_ctx, &e);
221 static int palacios_graphics_console_mouse(struct v3_guest * guest,
222 struct palacios_graphics_console *cons,
223 uint8_t x, uint8_t y, uint8_t buttons)
225 struct v3_mouse_event e;
228 e.data[2]=buttons; // These three are completely wrong, of course - ignoring mouse for now
230 v3_deliver_mouse_event(guest->v3_ctx,&e);
235 static struct v3_graphics_console_hooks palacios_graphics_console_hooks =
240 .get_data_read = g_get_data_read,
241 .release_data_read = g_release_data_read,
242 .get_data_rw = g_get_data_rw,
243 .release_data_rw = g_release_data_rw,
245 .changed = g_changed,
249 static int graphics_console_init( void ) {
251 V3_Init_Graphics_Console(&palacios_graphics_console_hooks);
257 static int fb_query(struct v3_guest * guest, unsigned int cmd, unsigned long arg,
260 struct palacios_graphics_console * cons = priv_data;
261 struct v3_fb_query_response q;
264 if (copy_from_user(&q, (void __user *) arg, sizeof(struct v3_fb_query_response))) {
265 printk("palacios: copy from user in getting query in fb\n");
269 switch (q.request_type) {
271 // returns only the spec for the FB
277 // returns whether an update is available for the region
278 // currently always true
283 case V3_FB_DATA_BOX: {
284 // Not curently implemented
285 printk("palacios: request for data in bounding box unsupported currently\n");
292 case V3_FB_DATA_ALL: {
293 // First let's sanity check to see if they are requesting the same
295 if (memcmp(&(q.spec),&(cons->spec),sizeof(struct v3_frame_buffer_spec))) {
296 printk("palacios: request for data with non-matching fb spec \n");
299 // Now let's indicate an update is in the pointer and copy across the data
300 if (copy_to_user(q.data,cons->data,cons->spec.width*cons->spec.height*cons->spec.bytes_per_pixel)) {
301 printk("palacios: unable to copy fb content to user\n");
312 // now we'll copy back any changes we made to the query/response structure
313 if (copy_to_user((void __user *) arg, (void*)&q, sizeof(struct v3_fb_query_response))) {
314 printk("palacios: unable to copy fb response to user\n");
322 static int fb_input(struct v3_guest * guest,
327 struct palacios_graphics_console * cons = priv_data;
328 struct v3_fb_input inp;
332 if (copy_from_user(&inp, (void __user *) arg, sizeof(struct v3_fb_input))) {
333 printk("palacios: copy from user in getting input in fb\n");
337 if ((inp.data_type == V3_FB_KEY) || (inp.data_type == V3_FB_BOTH)) {
338 rc = palacios_graphics_console_key(guest, cons, inp.scan_code);
341 if ((inp.data_type == V3_FB_MOUSE) || (inp.data_type == V3_FB_BOTH)) {
342 rc |= palacios_graphics_console_mouse(guest, cons, inp.mouse_data[0],
343 inp.mouse_data[1], inp.mouse_data[2]);
354 static int graphics_console_guest_init(struct v3_guest * guest, void ** vm_data) {
355 struct palacios_graphics_console * graphics_cons = kmalloc(sizeof(struct palacios_graphics_console), GFP_KERNEL);
357 if (!graphics_cons) {
358 printk("palacios: filed to do guest_init for graphics console\n");
362 memset(graphics_cons, 0, sizeof(struct palacios_graphics_console));
364 *vm_data = graphics_cons;
366 add_guest_ctrl(guest, V3_VM_FB_INPUT, fb_input, graphics_cons);
367 add_guest_ctrl(guest, V3_VM_FB_QUERY, fb_query, graphics_cons);
374 static struct linux_ext graphics_cons_ext = {
375 .name = "GRAPHICS_CONSOLE_INTERFACE",
376 .init = graphics_console_init,
378 .guest_init = graphics_console_guest_init,
383 register_extension(&graphics_cons_ext);