/** * @file * @section AUTHORS * * Copyright IBM, Corp. 2005-2006 * Copyright Red Hat, Inc. 2006-2008 * * Authors: * Eamon Walsh * Anthony Liguori , * Markus Armbruster , * Daniel P. Berrange , * Pat Campbell , * Gerd Hoffmann * * @section LICENSE * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; under version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * @section DESCRIPTION * * This is the stock framebuffer and input backend code from QEMU, * modified to call Linpicker instead of QEMU functions. * * This is the code for linpicker-server, which does not update anything * in XenStore (that is the job of linpicker-monitor). Rather, this code * is responsible for waiting until the frontend goes to Connected, then * mapping the shared pages and framebuffer grant references. Events are * handled here, with framebuffer updates being sent to Linpicker and input * events coming from Linpicker sent off to the guest. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "xen_backend.h" #include "xen_linpicker.h" /* -------------------------------------------------------------------- */ static int common_bind(struct common *c) { int ref; if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1) return -1; if (xenstore_read_fe_int(&c->xendev, "page-gref", &ref) == 0) { c->page = xc_gnttab_map_grant_ref(c->xendev.gnttabdev, c->xendev.dom, ref, PROT_READ | PROT_WRITE); c->uses_gref = 1; } else if (xenstore_read_fe_int(&c->xendev, "page-ref", &ref) == 0) { xen_pfn_t pfn = ref; c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, PROT_READ | PROT_WRITE, &pfn, 1); c->uses_gref = 0; } else return -1; if (c->page == NULL) return -1; xen_be_bind_evtchn(&c->xendev); XEN_BE_LOG(&c->xendev, 1, "ring ref %d, remote-port %d, local-port %d\n", ref, c->xendev.remote_port, c->xendev.local_port); return 0; } static void common_unbind(struct common *c) { xen_be_unbind_evtchn(&c->xendev); if (c->page && c->uses_gref) { xc_gnttab_munmap(c->xendev.gnttabdev, c->page, 1); } else if (c->page) { munmap(c->page, XC_PAGE_SIZE); } c->page = NULL; } /* -------------------------------------------------------------------- */ /* Send an event to the keyboard frontend driver */ static int xenfb_kbd_event(struct XenInput *xenfb, union xenkbd_in_event *event) { struct xenkbd_page *page = xenfb->c.page; uint32_t prod; if (xenfb->c.xendev.be_state != XenbusStateConnected) return 0; if (!page) return 0; prod = page->in_prod; if (prod - page->in_cons == XENKBD_IN_RING_LEN) { errno = EAGAIN; return -1; } xen_mb(); /* ensure ring space available */ XENKBD_IN_RING_REF(page, prod) = *event; xen_wmb(); /* ensure ring contents visible */ page->in_prod = prod + 1; return xen_be_send_notify(&xenfb->c.xendev); } /* Send a keyboard (or mouse button) event */ int xenfb_send_key(struct XenInput *xenfb, bool down, int keycode) { union xenkbd_in_event event; memset(&event, 0, XENKBD_IN_EVENT_SIZE); event.type = XENKBD_TYPE_KEY; event.key.pressed = down ? 1 : 0; event.key.keycode = keycode; return xenfb_kbd_event(xenfb, &event); } /* Send a relative mouse movement event */ int xenfb_send_motion(struct XenInput *xenfb, int rel_x, int rel_y, int rel_z) { union xenkbd_in_event event; memset(&event, 0, XENKBD_IN_EVENT_SIZE); event.type = XENKBD_TYPE_MOTION; event.motion.rel_x = rel_x; event.motion.rel_y = rel_y; #if __XEN_LATEST_INTERFACE_VERSION__ >= 0x00030207 event.motion.rel_z = rel_z; #endif return xenfb_kbd_event(xenfb, &event); } /* Send an absolute mouse movement event */ int xenfb_send_position(struct XenInput *xenfb, int abs_x, int abs_y, int z) { union xenkbd_in_event event; memset(&event, 0, XENKBD_IN_EVENT_SIZE); event.type = XENKBD_TYPE_POS; event.pos.abs_x = abs_x; event.pos.abs_y = abs_y; #if __XEN_LATEST_INTERFACE_VERSION__ == 0x00030207 event.pos.abs_z = z; #endif #if __XEN_LATEST_INTERFACE_VERSION__ >= 0x00030208 event.pos.rel_z = z; #endif return xenfb_kbd_event(xenfb, &event); } static int input_initialise(struct XenDevice *xendev) { struct XenInput *in = (struct XenInput *)xendev; int rc; rc = common_bind(&in->c); if (rc != 0) return rc; return xen_linpicker_input_connect(in); } static void input_connected(struct XenDevice *xendev) { struct XenInput *in = (struct XenInput *)xendev; if (xenstore_read_fe_int(xendev, "request-abs-pointer", &in->abs_pointer_wanted) == -1) in->abs_pointer_wanted = 0; } static void input_disconnect(struct XenDevice *xendev) { struct XenInput *in = (struct XenInput *)xendev; xen_linpicker_input_disconnect(in); common_unbind(&in->c); } static void input_event(struct XenDevice *xendev) { struct XenInput *xenfb = (struct XenInput *)xendev; struct xenkbd_page *page = xenfb->c.page; /* We don't understand any keyboard events, so just ignore them. */ if (page->out_prod == page->out_cons) return; page->out_cons = page->out_prod; xen_be_send_notify(&xenfb->c.xendev); /* XXX here is where we would get events through XEN */ } /* -------------------------------------------------------------------- */ static void xenfb_copy_mfns(int mode, int count, unsigned long *dst, void *src) { uint32_t *src32 = src; uint64_t *src64 = src; int i; for (i = 0; i < count; i++) dst[i] = (mode == 32) ? src32[i] : src64[i]; } static int xenfb_map_fb(struct XenFB *xenfb) { struct xenfb_page *page = xenfb->c.page; char *protocol = xenfb->c.xendev.protocol; int n_fbdirs; void *map, *pd; int mode, ret = -1; /* default to native */ pd = page->pd; mode = sizeof(unsigned long) * 8; if (!protocol) { /* * Undefined protocol, some guesswork needed. * * Old frontends which don't set the protocol use * one page directory only, thus pd[1] must be zero. * pd[1] of the 32bit struct layout and the lower * 32 bits of pd[0] of the 64bit struct layout have * the same location, so we can check that ... */ uint32_t *ptr32 = NULL; uint32_t *ptr64 = NULL; #if defined(__i386__) ptr32 = (void*)page->pd; ptr64 = ((void*)page->pd) + 4; #elif defined(__x86_64__) ptr32 = ((void*)page->pd) - 4; ptr64 = (void*)page->pd; #endif if (ptr32) { if (ptr32[1] == 0) { mode = 32; pd = ptr32; } else { mode = 64; pd = ptr64; } } #if defined(__x86_64__) } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_32) == 0) { /* 64bit dom0, 32bit domU */ mode = 32; pd = ((void*)page->pd) - 4; #elif defined(__i386__) } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_64) == 0) { /* 32bit dom0, 64bit domU */ mode = 64; pd = ((void*)page->pd) + 4; #endif } if (xenfb->pixels && xenfb->c.uses_gref) { xc_gnttab_munmap(xenfb->c.xendev.gnttabdev, xenfb->pixels, xenfb->fbpages); } else if (xenfb->pixels) { munmap(xenfb->pixels, xenfb->fbpages * XC_PAGE_SIZE); } xenfb->pixels = NULL; xenfb->fbpages = (xenfb->fb_len + (XC_PAGE_SIZE - 1)) / XC_PAGE_SIZE; n_fbdirs = xenfb->fbpages * mode / 8; n_fbdirs = (n_fbdirs + (XC_PAGE_SIZE - 1)) / XC_PAGE_SIZE; if (xenfb->c.uses_gref) { uint32_t* domids = calloc(n_fbdirs, sizeof(uint32_t)); uint32_t* refs = calloc(n_fbdirs, sizeof(uint32_t)); int i; for(i=0; i < n_fbdirs; i++) { domids[i] = xenfb->c.xendev.dom; refs[i] = (mode == 32) ? ((uint32_t*)pd)[i] : ((uint64_t*)pd)[i]; } map = xc_gnttab_map_grant_refs(xenfb->c.xendev.gnttabdev, n_fbdirs, domids, refs, PROT_READ); free(domids); free(refs); if (map == NULL) goto out; domids = calloc(xenfb->fbpages, sizeof(uint32_t)); refs = calloc(xenfb->fbpages, sizeof(uint32_t)); for(i=0; i < xenfb->fbpages; i++) { domids[i] = xenfb->c.xendev.dom; refs[i] = (mode == 32) ? ((uint32_t*)map)[i] : ((uint64_t*)map)[i]; } xc_gnttab_munmap(xenfb->c.xendev.gnttabdev, map, n_fbdirs); xenfb->pixels = xc_gnttab_map_grant_refs(xenfb->c.xendev.gnttabdev, xenfb->fbpages, domids, refs, PROT_READ); free(domids); free(refs); } else { unsigned long *pgmfns = calloc(n_fbdirs, sizeof(unsigned long)); xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd); map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom, PROT_READ, pgmfns, n_fbdirs); free(pgmfns); if (map == NULL) goto out; unsigned long *fbmfns = calloc(xenfb->fbpages, sizeof(unsigned long)); xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map); munmap(map, n_fbdirs * XC_PAGE_SIZE); xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom, PROT_READ | PROT_WRITE, fbmfns, xenfb->fbpages); free(fbmfns); } if (xenfb->pixels == NULL) goto out; ret = 0; /* all is fine */ out: return ret; } static int xenfb_configure_fb(struct XenFB *xenfb, size_t fb_len_lim, int width, int height, int depth, size_t fb_len, int offset, int row_stride) { size_t mfn_sz = sizeof(*((struct xenfb_page *)0)->pd); size_t pd_len = sizeof(((struct xenfb_page *)0)->pd) / mfn_sz; size_t fb_pages = pd_len * XC_PAGE_SIZE / mfn_sz; size_t fb_len_max = fb_pages * XC_PAGE_SIZE; int max_width, max_height; if (fb_len_lim > fb_len_max) { XEN_BE_LOG(&xenfb->c.xendev, 0, "fb size limit %zu exceeds %zu, corrected\n", fb_len_lim, fb_len_max); fb_len_lim = fb_len_max; } if (fb_len_lim && fb_len > fb_len_lim) { XEN_BE_LOG(&xenfb->c.xendev, 0, "frontend fb size %zu limited to %zu\n", fb_len, fb_len_lim); fb_len = fb_len_lim; } if (depth != 32) { XEN_BE_LOG(&xenfb->c.xendev, 0, "can't handle frontend fb depth %d\n", depth); return -1; } if (row_stride <= 0 || row_stride > fb_len) { XEN_BE_LOG(&xenfb->c.xendev, 0, "invalid frontend stride %d\n", row_stride); return -1; } max_width = row_stride / (depth / 8); if (width < 0 || width > max_width) { XEN_BE_LOG(&xenfb->c.xendev, 0, "invalid frontend width %d limited to %d\n", width, max_width); width = max_width; } if (offset < 0 || offset >= fb_len) { XEN_BE_LOG(&xenfb->c.xendev, 0, "invalid frontend offset %d (max %zu)\n", offset, fb_len - 1); return -1; } max_height = (fb_len - offset) / row_stride; if (height < 0 || height > max_height) { XEN_BE_LOG(&xenfb->c.xendev, 0, "invalid frontend height %d limited to %d\n", height, max_height); height = max_height; } xenfb->fb_len = fb_len; xenfb->row_stride = row_stride; xenfb->depth = depth; xenfb->width = width; xenfb->height = height; xenfb->offset = offset; xenfb->up_fullscreen = 1; xenfb->do_resize = 1; XEN_BE_LOG(&xenfb->c.xendev, 1, "framebuffer %dx%dx%d offset %d stride %d\n", width, height, depth, offset, row_stride); return 0; } #ifdef XENFB_TYPE_REFRESH_PERIOD static int xenfb_queue_full(struct XenFB *xenfb) { struct xenfb_page *page = xenfb->c.page; uint32_t cons, prod; if (!page) return 1; prod = page->in_prod; cons = page->in_cons; return prod - cons == XENFB_IN_RING_LEN; } static void xenfb_send_event(struct XenFB *xenfb, union xenfb_in_event *event) { uint32_t prod; struct xenfb_page *page = xenfb->c.page; prod = page->in_prod; /* caller ensures !xenfb_queue_full() */ xen_mb(); /* ensure ring space available */ XENFB_IN_RING_REF(page, prod) = *event; xen_wmb(); /* ensure ring contents visible */ page->in_prod = prod + 1; xen_be_send_notify(&xenfb->c.xendev); } static void xenfb_send_refresh_period(struct XenFB *xenfb, int period) { union xenfb_in_event event; memset(&event, 0, sizeof(event)); event.type = XENFB_TYPE_REFRESH_PERIOD; event.refresh_period.period = period; xenfb_send_event(xenfb, &event); } #endif /* * Periodic update of display. * Also transmit the refresh interval to the frontend. * * Never ever do any qemu display operations * (resize, screen update) outside this function. * Our screen might be inactive. When asked for * an update we know it is active. */ static void xenfb_update(void *opaque) { struct XenFB *xenfb = opaque; int i; if (xenfb->c.xendev.be_state != XenbusStateConnected) return; if (xenfb->feature_update) { #ifdef XENFB_TYPE_REFRESH_PERIOD int period = 99999999; int idle = 1; if (xenfb_queue_full(xenfb)) return; if (idle) period = XENFB_NO_REFRESH; if (xenfb->refresh_period != period) { xenfb_send_refresh_period(xenfb, period); xenfb->refresh_period = period; XEN_BE_LOG(&xenfb->c.xendev, 1, "refresh period: %d\n", period); } #else ; /* nothing */ #endif } else { /* we don't get update notifications, thus use the * sledge hammer approach ... */ xenfb->up_fullscreen = 1; } /* resize if needed */ if (xenfb->do_resize) { xen_linpicker_fb_resize(xenfb); xenfb->do_resize = 0; xenfb->up_fullscreen = 1; } /* run queued updates */ if (xenfb->up_fullscreen) { XEN_BE_LOG(&xenfb->c.xendev, 3, "update: fullscreen\n"); xen_linpicker_fb_update(xenfb, 0, 0, xenfb->width, xenfb->height); } else if (xenfb->up_count) { XEN_BE_LOG(&xenfb->c.xendev, 3, "update: %d rects\n", xenfb->up_count); for (i = 0; i < xenfb->up_count; i++) xen_linpicker_fb_update(xenfb, xenfb->up_rects[i].x, xenfb->up_rects[i].y, xenfb->up_rects[i].w, xenfb->up_rects[i].h); } else { XEN_BE_LOG(&xenfb->c.xendev, 3, "update: nothing\n"); } xenfb->up_count = 0; xenfb->up_fullscreen = 0; } /* QEMU display state changed, so refresh the framebuffer copy */ static void xenfb_invalidate(void *opaque) { struct XenFB *xenfb = opaque; xenfb->up_fullscreen = 1; } static void xenfb_handle_events(struct XenFB *xenfb) { uint32_t prod, cons; struct xenfb_page *page = xenfb->c.page; prod = page->out_prod; if (prod == page->out_cons) return; xen_rmb(); /* ensure we see ring contents up to prod */ for (cons = page->out_cons; cons != prod; cons++) { union xenfb_out_event *event = &XENFB_OUT_RING_REF(page, cons); int x, y, w, h; switch (event->type) { case XENFB_TYPE_UPDATE: if (xenfb->up_count == UP_QUEUE) xenfb->up_fullscreen = 1; if (xenfb->up_fullscreen) break; x = MAX(event->update.x, 0); y = MAX(event->update.y, 0); w = MIN(event->update.width, xenfb->width - x); h = MIN(event->update.height, xenfb->height - y); if (w < 0 || h < 0) { XEN_BE_LOG(&xenfb->c.xendev, 1, "bogus update ignored\n"); break; } if (x != event->update.x || y != event->update.y || w != event->update.width || h != event->update.height) { XEN_BE_LOG(&xenfb->c.xendev, 1, "bogus update clipped\n"); } if (w == xenfb->width && h > xenfb->height / 2) { /* scroll detector: updated more than 50% of the lines, * don't bother keeping track of the rectangles then */ xenfb->up_fullscreen = 1; } else { xenfb->up_rects[xenfb->up_count].x = x; xenfb->up_rects[xenfb->up_count].y = y; xenfb->up_rects[xenfb->up_count].w = w; xenfb->up_rects[xenfb->up_count].h = h; xenfb->up_count++; } break; #ifdef XENFB_TYPE_RESIZE case XENFB_TYPE_RESIZE: if (xenfb_configure_fb(xenfb, xenfb->fb_len, event->resize.width, event->resize.height, event->resize.depth, xenfb->fb_len, event->resize.offset, event->resize.stride) < 0) break; xenfb_invalidate(xenfb); break; #endif } } xen_mb(); /* ensure we're done with ring contents */ page->out_cons = cons; xenfb_update(xenfb); } static int fb_init(struct XenDevice *xendev) { struct XenFB *fb = (struct XenFB *)xendev; fb->refresh_period = -1; return 0; } static int fb_initialise(struct XenDevice *xendev) { struct XenFB *fb = (struct XenFB *)xendev; struct xenfb_page *fb_page; int videoram; int rc; if (xenstore_read_fe_int(xendev, "videoram", &videoram) == -1) videoram = 0; rc = common_bind(&fb->c); if (rc != 0) return rc; fb_page = fb->c.page; rc = xenfb_configure_fb(fb, videoram * 1024 * 1024U, fb_page->width, fb_page->height, fb_page->depth, fb_page->mem_length, 0, fb_page->line_length); if (rc != 0) return rc; rc = xenfb_map_fb(fb); if (rc != 0) return rc; #if 0 /* handled in xen_init_display() for now */ if (!fb->have_console) { fb->c.ds = graphic_console_init(xenfb_update, xenfb_invalidate, NULL, NULL, fb); fb->have_console = 1; } #endif if (xenstore_read_fe_int(xendev, "feature-update", &fb->feature_update) == -1) fb->feature_update = 0; XEN_BE_LOG(xendev, 1, "feature-update=%d, videoram=%d\n", fb->feature_update, videoram); return xen_linpicker_fb_connect(fb); } static void fb_disconnect(struct XenDevice *xendev) { struct XenFB *fb = (struct XenFB *)xendev; xen_linpicker_fb_disconnect(fb); if (fb->pixels && fb->c.uses_gref) { xc_gnttab_munmap(fb->c.xendev.gnttabdev, fb->pixels, fb->fbpages); } else if (fb->pixels) { munmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE); } common_unbind(&fb->c); fb->feature_update = 0; fb->bug_trigger = 0; } static void fb_event(struct XenDevice *xendev) { struct XenFB *xenfb = (struct XenFB *)xendev; xenfb_handle_events(xenfb); xen_be_send_notify(&xenfb->c.xendev); } /* -------------------------------------------------------------------- */ struct XenDevOps xen_kbdmouse_ops = { .size = sizeof(struct XenInput), .initialise = input_initialise, .connected = input_connected, .disconnect = input_disconnect, .event = input_event, }; struct XenDevOps xen_framebuffer_ops = { .size = sizeof(struct XenFB), .init = fb_init, .initialise = fb_initialise, .disconnect = fb_disconnect, .event = fb_event, };