xenfb.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * xen paravirt framebuffer backend
  3. *
  4. * Copyright IBM, Corp. 2005-2006
  5. * Copyright Red Hat, Inc. 2006-2008
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>,
  9. * Markus Armbruster <armbru@redhat.com>,
  10. * Daniel P. Berrange <berrange@redhat.com>,
  11. * Pat Campbell <plc@novell.com>,
  12. * Gerd Hoffmann <kraxel@redhat.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; under version 2 of the License.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <sys/mman.h>
  32. #include <errno.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <time.h>
  36. #include "hw/hw.h"
  37. #include "ui/console.h"
  38. #include "sysemu/char.h"
  39. #include "hw/xen/xen_backend.h"
  40. #include <xen/event_channel.h>
  41. #include <xen/io/fbif.h>
  42. #include <xen/io/kbdif.h>
  43. #include <xen/io/protocols.h>
  44. #ifndef BTN_LEFT
  45. #define BTN_LEFT 0x110 /* from <linux/input.h> */
  46. #endif
  47. /* -------------------------------------------------------------------- */
  48. struct common {
  49. struct XenDevice xendev; /* must be first */
  50. void *page;
  51. QemuConsole *con;
  52. };
  53. struct XenInput {
  54. struct common c;
  55. int abs_pointer_wanted; /* Whether guest supports absolute pointer */
  56. int button_state; /* Last seen pointer button state */
  57. int extended;
  58. QEMUPutMouseEntry *qmouse;
  59. };
  60. #define UP_QUEUE 8
  61. struct XenFB {
  62. struct common c;
  63. size_t fb_len;
  64. int row_stride;
  65. int depth;
  66. int width;
  67. int height;
  68. int offset;
  69. void *pixels;
  70. int fbpages;
  71. int feature_update;
  72. int bug_trigger;
  73. int have_console;
  74. int do_resize;
  75. struct {
  76. int x,y,w,h;
  77. } up_rects[UP_QUEUE];
  78. int up_count;
  79. int up_fullscreen;
  80. };
  81. /* -------------------------------------------------------------------- */
  82. static int common_bind(struct common *c)
  83. {
  84. int mfn;
  85. if (xenstore_read_fe_int(&c->xendev, "page-ref", &mfn) == -1)
  86. return -1;
  87. if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
  88. return -1;
  89. c->page = xc_map_foreign_range(xen_xc, c->xendev.dom,
  90. XC_PAGE_SIZE,
  91. PROT_READ | PROT_WRITE, mfn);
  92. if (c->page == NULL)
  93. return -1;
  94. xen_be_bind_evtchn(&c->xendev);
  95. xen_be_printf(&c->xendev, 1, "ring mfn %d, remote-port %d, local-port %d\n",
  96. mfn, c->xendev.remote_port, c->xendev.local_port);
  97. return 0;
  98. }
  99. static void common_unbind(struct common *c)
  100. {
  101. xen_be_unbind_evtchn(&c->xendev);
  102. if (c->page) {
  103. munmap(c->page, XC_PAGE_SIZE);
  104. c->page = NULL;
  105. }
  106. }
  107. /* -------------------------------------------------------------------- */
  108. #if 0
  109. /*
  110. * These two tables are not needed any more, but left in here
  111. * intentionally as documentation, to show how scancode2linux[]
  112. * was generated.
  113. *
  114. * Tables to map from scancode to Linux input layer keycode.
  115. * Scancodes are hardware-specific. These maps assumes a
  116. * standard AT or PS/2 keyboard which is what QEMU feeds us.
  117. */
  118. const unsigned char atkbd_set2_keycode[512] = {
  119. 0, 67, 65, 63, 61, 59, 60, 88, 0, 68, 66, 64, 62, 15, 41,117,
  120. 0, 56, 42, 93, 29, 16, 2, 0, 0, 0, 44, 31, 30, 17, 3, 0,
  121. 0, 46, 45, 32, 18, 5, 4, 95, 0, 57, 47, 33, 20, 19, 6,183,
  122. 0, 49, 48, 35, 34, 21, 7,184, 0, 0, 50, 36, 22, 8, 9,185,
  123. 0, 51, 37, 23, 24, 11, 10, 0, 0, 52, 53, 38, 39, 25, 12, 0,
  124. 0, 89, 40, 0, 26, 13, 0, 0, 58, 54, 28, 27, 0, 43, 0, 85,
  125. 0, 86, 91, 90, 92, 0, 14, 94, 0, 79,124, 75, 71,121, 0, 0,
  126. 82, 83, 80, 76, 77, 72, 1, 69, 87, 78, 81, 74, 55, 73, 70, 99,
  127. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  128. 217,100,255, 0, 97,165, 0, 0,156, 0, 0, 0, 0, 0, 0,125,
  129. 173,114, 0,113, 0, 0, 0,126,128, 0, 0,140, 0, 0, 0,127,
  130. 159, 0,115, 0,164, 0, 0,116,158, 0,150,166, 0, 0, 0,142,
  131. 157, 0, 0, 0, 0, 0, 0, 0,155, 0, 98, 0, 0,163, 0, 0,
  132. 226, 0, 0, 0, 0, 0, 0, 0, 0,255, 96, 0, 0, 0,143, 0,
  133. 0, 0, 0, 0, 0, 0, 0, 0, 0,107, 0,105,102, 0, 0,112,
  134. 110,111,108,112,106,103, 0,119, 0,118,109, 0, 99,104,119, 0,
  135. };
  136. const unsigned char atkbd_unxlate_table[128] = {
  137. 0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
  138. 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
  139. 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
  140. 50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88, 5, 6, 4, 12, 3,
  141. 11, 2, 10, 1, 9,119,126,108,117,125,123,107,115,116,121,105,
  142. 114,122,112,113,127, 96, 97,120, 7, 15, 23, 31, 39, 47, 55, 63,
  143. 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
  144. 19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
  145. };
  146. #endif
  147. /*
  148. * for (i = 0; i < 128; i++) {
  149. * scancode2linux[i] = atkbd_set2_keycode[atkbd_unxlate_table[i]];
  150. * scancode2linux[i | 0x80] = atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80];
  151. * }
  152. */
  153. static const unsigned char scancode2linux[512] = {
  154. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  155. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  156. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  157. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  158. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  159. 80, 81, 82, 83, 99, 0, 86, 87, 88,117, 0, 0, 95,183,184,185,
  160. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  161. 93, 0, 0, 89, 0, 0, 85, 91, 90, 92, 0, 94, 0,124,121, 0,
  162. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  163. 165, 0, 0, 0, 0, 0, 0, 0, 0,163, 0, 0, 96, 97, 0, 0,
  164. 113,140,164, 0,166, 0, 0, 0, 0, 0,255, 0, 0, 0,114, 0,
  165. 115, 0,150, 0, 0, 98,255, 99,100, 0, 0, 0, 0, 0, 0, 0,
  166. 0, 0, 0, 0, 0,119,119,102,103,104, 0,105,112,106,118,107,
  167. 108,109,110,111, 0, 0, 0, 0, 0, 0, 0,125,126,127,116,142,
  168. 0, 0, 0,143, 0,217,156,173,128,159,158,157,155,226, 0,112,
  169. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  170. };
  171. /* Send an event to the keyboard frontend driver */
  172. static int xenfb_kbd_event(struct XenInput *xenfb,
  173. union xenkbd_in_event *event)
  174. {
  175. struct xenkbd_page *page = xenfb->c.page;
  176. uint32_t prod;
  177. if (xenfb->c.xendev.be_state != XenbusStateConnected)
  178. return 0;
  179. if (!page)
  180. return 0;
  181. prod = page->in_prod;
  182. if (prod - page->in_cons == XENKBD_IN_RING_LEN) {
  183. errno = EAGAIN;
  184. return -1;
  185. }
  186. xen_mb(); /* ensure ring space available */
  187. XENKBD_IN_RING_REF(page, prod) = *event;
  188. xen_wmb(); /* ensure ring contents visible */
  189. page->in_prod = prod + 1;
  190. return xen_be_send_notify(&xenfb->c.xendev);
  191. }
  192. /* Send a keyboard (or mouse button) event */
  193. static int xenfb_send_key(struct XenInput *xenfb, bool down, int keycode)
  194. {
  195. union xenkbd_in_event event;
  196. memset(&event, 0, XENKBD_IN_EVENT_SIZE);
  197. event.type = XENKBD_TYPE_KEY;
  198. event.key.pressed = down ? 1 : 0;
  199. event.key.keycode = keycode;
  200. return xenfb_kbd_event(xenfb, &event);
  201. }
  202. /* Send a relative mouse movement event */
  203. static int xenfb_send_motion(struct XenInput *xenfb,
  204. int rel_x, int rel_y, int rel_z)
  205. {
  206. union xenkbd_in_event event;
  207. memset(&event, 0, XENKBD_IN_EVENT_SIZE);
  208. event.type = XENKBD_TYPE_MOTION;
  209. event.motion.rel_x = rel_x;
  210. event.motion.rel_y = rel_y;
  211. #if __XEN_LATEST_INTERFACE_VERSION__ >= 0x00030207
  212. event.motion.rel_z = rel_z;
  213. #endif
  214. return xenfb_kbd_event(xenfb, &event);
  215. }
  216. /* Send an absolute mouse movement event */
  217. static int xenfb_send_position(struct XenInput *xenfb,
  218. int abs_x, int abs_y, int z)
  219. {
  220. union xenkbd_in_event event;
  221. memset(&event, 0, XENKBD_IN_EVENT_SIZE);
  222. event.type = XENKBD_TYPE_POS;
  223. event.pos.abs_x = abs_x;
  224. event.pos.abs_y = abs_y;
  225. #if __XEN_LATEST_INTERFACE_VERSION__ == 0x00030207
  226. event.pos.abs_z = z;
  227. #endif
  228. #if __XEN_LATEST_INTERFACE_VERSION__ >= 0x00030208
  229. event.pos.rel_z = z;
  230. #endif
  231. return xenfb_kbd_event(xenfb, &event);
  232. }
  233. /*
  234. * Send a key event from the client to the guest OS
  235. * QEMU gives us a raw scancode from an AT / PS/2 style keyboard.
  236. * We have to turn this into a Linux Input layer keycode.
  237. *
  238. * Extra complexity from the fact that with extended scancodes
  239. * (like those produced by arrow keys) this method gets called
  240. * twice, but we only want to send a single event. So we have to
  241. * track the '0xe0' scancode state & collapse the extended keys
  242. * as needed.
  243. *
  244. * Wish we could just send scancodes straight to the guest which
  245. * already has code for dealing with this...
  246. */
  247. static void xenfb_key_event(void *opaque, int scancode)
  248. {
  249. struct XenInput *xenfb = opaque;
  250. int down = 1;
  251. if (scancode == 0xe0) {
  252. xenfb->extended = 1;
  253. return;
  254. } else if (scancode & 0x80) {
  255. scancode &= 0x7f;
  256. down = 0;
  257. }
  258. if (xenfb->extended) {
  259. scancode |= 0x80;
  260. xenfb->extended = 0;
  261. }
  262. xenfb_send_key(xenfb, down, scancode2linux[scancode]);
  263. }
  264. /*
  265. * Send a mouse event from the client to the guest OS
  266. *
  267. * The QEMU mouse can be in either relative, or absolute mode.
  268. * Movement is sent separately from button state, which has to
  269. * be encoded as virtual key events. We also don't actually get
  270. * given any button up/down events, so have to track changes in
  271. * the button state.
  272. */
  273. static void xenfb_mouse_event(void *opaque,
  274. int dx, int dy, int dz, int button_state)
  275. {
  276. struct XenInput *xenfb = opaque;
  277. DisplaySurface *surface = qemu_console_surface(xenfb->c.con);
  278. int dw = surface_width(surface);
  279. int dh = surface_height(surface);
  280. int i;
  281. if (xenfb->abs_pointer_wanted)
  282. xenfb_send_position(xenfb,
  283. dx * (dw - 1) / 0x7fff,
  284. dy * (dh - 1) / 0x7fff,
  285. dz);
  286. else
  287. xenfb_send_motion(xenfb, dx, dy, dz);
  288. for (i = 0 ; i < 8 ; i++) {
  289. int lastDown = xenfb->button_state & (1 << i);
  290. int down = button_state & (1 << i);
  291. if (down == lastDown)
  292. continue;
  293. if (xenfb_send_key(xenfb, down, BTN_LEFT+i) < 0)
  294. return;
  295. }
  296. xenfb->button_state = button_state;
  297. }
  298. static int input_init(struct XenDevice *xendev)
  299. {
  300. xenstore_write_be_int(xendev, "feature-abs-pointer", 1);
  301. return 0;
  302. }
  303. static int input_initialise(struct XenDevice *xendev)
  304. {
  305. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  306. int rc;
  307. if (!in->c.con) {
  308. xen_be_printf(xendev, 1, "ds not set (yet)\n");
  309. return -1;
  310. }
  311. rc = common_bind(&in->c);
  312. if (rc != 0)
  313. return rc;
  314. qemu_add_kbd_event_handler(xenfb_key_event, in);
  315. return 0;
  316. }
  317. static void input_connected(struct XenDevice *xendev)
  318. {
  319. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  320. if (xenstore_read_fe_int(xendev, "request-abs-pointer",
  321. &in->abs_pointer_wanted) == -1) {
  322. in->abs_pointer_wanted = 0;
  323. }
  324. if (in->qmouse) {
  325. qemu_remove_mouse_event_handler(in->qmouse);
  326. }
  327. in->qmouse = qemu_add_mouse_event_handler(xenfb_mouse_event, in,
  328. in->abs_pointer_wanted,
  329. "Xen PVFB Mouse");
  330. }
  331. static void input_disconnect(struct XenDevice *xendev)
  332. {
  333. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  334. if (in->qmouse) {
  335. qemu_remove_mouse_event_handler(in->qmouse);
  336. in->qmouse = NULL;
  337. }
  338. qemu_add_kbd_event_handler(NULL, NULL);
  339. common_unbind(&in->c);
  340. }
  341. static void input_event(struct XenDevice *xendev)
  342. {
  343. struct XenInput *xenfb = container_of(xendev, struct XenInput, c.xendev);
  344. struct xenkbd_page *page = xenfb->c.page;
  345. /* We don't understand any keyboard events, so just ignore them. */
  346. if (page->out_prod == page->out_cons)
  347. return;
  348. page->out_cons = page->out_prod;
  349. xen_be_send_notify(&xenfb->c.xendev);
  350. }
  351. /* -------------------------------------------------------------------- */
  352. static void xenfb_copy_mfns(int mode, int count, unsigned long *dst, void *src)
  353. {
  354. uint32_t *src32 = src;
  355. uint64_t *src64 = src;
  356. int i;
  357. for (i = 0; i < count; i++)
  358. dst[i] = (mode == 32) ? src32[i] : src64[i];
  359. }
  360. static int xenfb_map_fb(struct XenFB *xenfb)
  361. {
  362. struct xenfb_page *page = xenfb->c.page;
  363. char *protocol = xenfb->c.xendev.protocol;
  364. int n_fbdirs;
  365. unsigned long *pgmfns = NULL;
  366. unsigned long *fbmfns = NULL;
  367. void *map, *pd;
  368. int mode, ret = -1;
  369. /* default to native */
  370. pd = page->pd;
  371. mode = sizeof(unsigned long) * 8;
  372. if (!protocol) {
  373. /*
  374. * Undefined protocol, some guesswork needed.
  375. *
  376. * Old frontends which don't set the protocol use
  377. * one page directory only, thus pd[1] must be zero.
  378. * pd[1] of the 32bit struct layout and the lower
  379. * 32 bits of pd[0] of the 64bit struct layout have
  380. * the same location, so we can check that ...
  381. */
  382. uint32_t *ptr32 = NULL;
  383. uint32_t *ptr64 = NULL;
  384. #if defined(__i386__)
  385. ptr32 = (void*)page->pd;
  386. ptr64 = ((void*)page->pd) + 4;
  387. #elif defined(__x86_64__)
  388. ptr32 = ((void*)page->pd) - 4;
  389. ptr64 = (void*)page->pd;
  390. #endif
  391. if (ptr32) {
  392. if (ptr32[1] == 0) {
  393. mode = 32;
  394. pd = ptr32;
  395. } else {
  396. mode = 64;
  397. pd = ptr64;
  398. }
  399. }
  400. #if defined(__x86_64__)
  401. } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
  402. /* 64bit dom0, 32bit domU */
  403. mode = 32;
  404. pd = ((void*)page->pd) - 4;
  405. #elif defined(__i386__)
  406. } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
  407. /* 32bit dom0, 64bit domU */
  408. mode = 64;
  409. pd = ((void*)page->pd) + 4;
  410. #endif
  411. }
  412. if (xenfb->pixels) {
  413. munmap(xenfb->pixels, xenfb->fbpages * XC_PAGE_SIZE);
  414. xenfb->pixels = NULL;
  415. }
  416. xenfb->fbpages = (xenfb->fb_len + (XC_PAGE_SIZE - 1)) / XC_PAGE_SIZE;
  417. n_fbdirs = xenfb->fbpages * mode / 8;
  418. n_fbdirs = (n_fbdirs + (XC_PAGE_SIZE - 1)) / XC_PAGE_SIZE;
  419. pgmfns = g_malloc0(sizeof(unsigned long) * n_fbdirs);
  420. fbmfns = g_malloc0(sizeof(unsigned long) * xenfb->fbpages);
  421. xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
  422. map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
  423. PROT_READ, pgmfns, n_fbdirs);
  424. if (map == NULL)
  425. goto out;
  426. xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
  427. munmap(map, n_fbdirs * XC_PAGE_SIZE);
  428. xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
  429. PROT_READ, fbmfns, xenfb->fbpages);
  430. if (xenfb->pixels == NULL)
  431. goto out;
  432. ret = 0; /* all is fine */
  433. out:
  434. g_free(pgmfns);
  435. g_free(fbmfns);
  436. return ret;
  437. }
  438. static int xenfb_configure_fb(struct XenFB *xenfb, size_t fb_len_lim,
  439. int width, int height, int depth,
  440. size_t fb_len, int offset, int row_stride)
  441. {
  442. size_t mfn_sz = sizeof(*((struct xenfb_page *)0)->pd);
  443. size_t pd_len = sizeof(((struct xenfb_page *)0)->pd) / mfn_sz;
  444. size_t fb_pages = pd_len * XC_PAGE_SIZE / mfn_sz;
  445. size_t fb_len_max = fb_pages * XC_PAGE_SIZE;
  446. int max_width, max_height;
  447. if (fb_len_lim > fb_len_max) {
  448. xen_be_printf(&xenfb->c.xendev, 0, "fb size limit %zu exceeds %zu, corrected\n",
  449. fb_len_lim, fb_len_max);
  450. fb_len_lim = fb_len_max;
  451. }
  452. if (fb_len_lim && fb_len > fb_len_lim) {
  453. xen_be_printf(&xenfb->c.xendev, 0, "frontend fb size %zu limited to %zu\n",
  454. fb_len, fb_len_lim);
  455. fb_len = fb_len_lim;
  456. }
  457. if (depth != 8 && depth != 16 && depth != 24 && depth != 32) {
  458. xen_be_printf(&xenfb->c.xendev, 0, "can't handle frontend fb depth %d\n",
  459. depth);
  460. return -1;
  461. }
  462. if (row_stride <= 0 || row_stride > fb_len) {
  463. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend stride %d\n", row_stride);
  464. return -1;
  465. }
  466. max_width = row_stride / (depth / 8);
  467. if (width < 0 || width > max_width) {
  468. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend width %d limited to %d\n",
  469. width, max_width);
  470. width = max_width;
  471. }
  472. if (offset < 0 || offset >= fb_len) {
  473. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend offset %d (max %zu)\n",
  474. offset, fb_len - 1);
  475. return -1;
  476. }
  477. max_height = (fb_len - offset) / row_stride;
  478. if (height < 0 || height > max_height) {
  479. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend height %d limited to %d\n",
  480. height, max_height);
  481. height = max_height;
  482. }
  483. xenfb->fb_len = fb_len;
  484. xenfb->row_stride = row_stride;
  485. xenfb->depth = depth;
  486. xenfb->width = width;
  487. xenfb->height = height;
  488. xenfb->offset = offset;
  489. xenfb->up_fullscreen = 1;
  490. xenfb->do_resize = 1;
  491. xen_be_printf(&xenfb->c.xendev, 1, "framebuffer %dx%dx%d offset %d stride %d\n",
  492. width, height, depth, offset, row_stride);
  493. return 0;
  494. }
  495. /* A convenient function for munging pixels between different depths */
  496. #define BLT(SRC_T,DST_T,RSB,GSB,BSB,RDB,GDB,BDB) \
  497. for (line = y ; line < (y+h) ; line++) { \
  498. SRC_T *src = (SRC_T *)(xenfb->pixels \
  499. + xenfb->offset \
  500. + (line * xenfb->row_stride) \
  501. + (x * xenfb->depth / 8)); \
  502. DST_T *dst = (DST_T *)(data \
  503. + (line * linesize) \
  504. + (x * bpp / 8)); \
  505. int col; \
  506. const int RSS = 32 - (RSB + GSB + BSB); \
  507. const int GSS = 32 - (GSB + BSB); \
  508. const int BSS = 32 - (BSB); \
  509. const uint32_t RSM = (~0U) << (32 - RSB); \
  510. const uint32_t GSM = (~0U) << (32 - GSB); \
  511. const uint32_t BSM = (~0U) << (32 - BSB); \
  512. const int RDS = 32 - (RDB + GDB + BDB); \
  513. const int GDS = 32 - (GDB + BDB); \
  514. const int BDS = 32 - (BDB); \
  515. const uint32_t RDM = (~0U) << (32 - RDB); \
  516. const uint32_t GDM = (~0U) << (32 - GDB); \
  517. const uint32_t BDM = (~0U) << (32 - BDB); \
  518. for (col = x ; col < (x+w) ; col++) { \
  519. uint32_t spix = *src; \
  520. *dst = (((spix << RSS) & RSM & RDM) >> RDS) | \
  521. (((spix << GSS) & GSM & GDM) >> GDS) | \
  522. (((spix << BSS) & BSM & BDM) >> BDS); \
  523. src = (SRC_T *) ((unsigned long) src + xenfb->depth / 8); \
  524. dst = (DST_T *) ((unsigned long) dst + bpp / 8); \
  525. } \
  526. }
  527. /*
  528. * This copies data from the guest framebuffer region, into QEMU's
  529. * displaysurface. qemu uses 16 or 32 bpp. In case the pv framebuffer
  530. * uses something else we must convert and copy, otherwise we can
  531. * supply the buffer directly and no thing here.
  532. */
  533. static void xenfb_guest_copy(struct XenFB *xenfb, int x, int y, int w, int h)
  534. {
  535. DisplaySurface *surface = qemu_console_surface(xenfb->c.con);
  536. int line, oops = 0;
  537. int bpp = surface_bits_per_pixel(surface);
  538. int linesize = surface_stride(surface);
  539. uint8_t *data = surface_data(surface);
  540. if (!is_buffer_shared(surface)) {
  541. switch (xenfb->depth) {
  542. case 8:
  543. if (bpp == 16) {
  544. BLT(uint8_t, uint16_t, 3, 3, 2, 5, 6, 5);
  545. } else if (bpp == 32) {
  546. BLT(uint8_t, uint32_t, 3, 3, 2, 8, 8, 8);
  547. } else {
  548. oops = 1;
  549. }
  550. break;
  551. case 24:
  552. if (bpp == 16) {
  553. BLT(uint32_t, uint16_t, 8, 8, 8, 5, 6, 5);
  554. } else if (bpp == 32) {
  555. BLT(uint32_t, uint32_t, 8, 8, 8, 8, 8, 8);
  556. } else {
  557. oops = 1;
  558. }
  559. break;
  560. default:
  561. oops = 1;
  562. }
  563. }
  564. if (oops) /* should not happen */
  565. xen_be_printf(&xenfb->c.xendev, 0, "%s: oops: convert %d -> %d bpp?\n",
  566. __FUNCTION__, xenfb->depth, bpp);
  567. dpy_gfx_update(xenfb->c.con, x, y, w, h);
  568. }
  569. #ifdef XENFB_TYPE_REFRESH_PERIOD
  570. static int xenfb_queue_full(struct XenFB *xenfb)
  571. {
  572. struct xenfb_page *page = xenfb->c.page;
  573. uint32_t cons, prod;
  574. if (!page)
  575. return 1;
  576. prod = page->in_prod;
  577. cons = page->in_cons;
  578. return prod - cons == XENFB_IN_RING_LEN;
  579. }
  580. static void xenfb_send_event(struct XenFB *xenfb, union xenfb_in_event *event)
  581. {
  582. uint32_t prod;
  583. struct xenfb_page *page = xenfb->c.page;
  584. prod = page->in_prod;
  585. /* caller ensures !xenfb_queue_full() */
  586. xen_mb(); /* ensure ring space available */
  587. XENFB_IN_RING_REF(page, prod) = *event;
  588. xen_wmb(); /* ensure ring contents visible */
  589. page->in_prod = prod + 1;
  590. xen_be_send_notify(&xenfb->c.xendev);
  591. }
  592. static void xenfb_send_refresh_period(struct XenFB *xenfb, int period)
  593. {
  594. union xenfb_in_event event;
  595. memset(&event, 0, sizeof(event));
  596. event.type = XENFB_TYPE_REFRESH_PERIOD;
  597. event.refresh_period.period = period;
  598. xenfb_send_event(xenfb, &event);
  599. }
  600. #endif
  601. /*
  602. * Periodic update of display.
  603. * Also transmit the refresh interval to the frontend.
  604. *
  605. * Never ever do any qemu display operations
  606. * (resize, screen update) outside this function.
  607. * Our screen might be inactive. When asked for
  608. * an update we know it is active.
  609. */
  610. static void xenfb_update(void *opaque)
  611. {
  612. struct XenFB *xenfb = opaque;
  613. DisplaySurface *surface;
  614. int i;
  615. if (xenfb->c.xendev.be_state != XenbusStateConnected)
  616. return;
  617. if (!xenfb->feature_update) {
  618. /* we don't get update notifications, thus use the
  619. * sledge hammer approach ... */
  620. xenfb->up_fullscreen = 1;
  621. }
  622. /* resize if needed */
  623. if (xenfb->do_resize) {
  624. xenfb->do_resize = 0;
  625. switch (xenfb->depth) {
  626. case 16:
  627. case 32:
  628. /* console.c supported depth -> buffer can be used directly */
  629. surface = qemu_create_displaysurface_from
  630. (xenfb->width, xenfb->height, xenfb->depth,
  631. xenfb->row_stride, xenfb->pixels + xenfb->offset,
  632. false);
  633. break;
  634. default:
  635. /* we must convert stuff */
  636. surface = qemu_create_displaysurface(xenfb->width, xenfb->height);
  637. break;
  638. }
  639. dpy_gfx_replace_surface(xenfb->c.con, surface);
  640. xen_be_printf(&xenfb->c.xendev, 1, "update: resizing: %dx%d @ %d bpp%s\n",
  641. xenfb->width, xenfb->height, xenfb->depth,
  642. is_buffer_shared(surface) ? " (shared)" : "");
  643. xenfb->up_fullscreen = 1;
  644. }
  645. /* run queued updates */
  646. if (xenfb->up_fullscreen) {
  647. xen_be_printf(&xenfb->c.xendev, 3, "update: fullscreen\n");
  648. xenfb_guest_copy(xenfb, 0, 0, xenfb->width, xenfb->height);
  649. } else if (xenfb->up_count) {
  650. xen_be_printf(&xenfb->c.xendev, 3, "update: %d rects\n", xenfb->up_count);
  651. for (i = 0; i < xenfb->up_count; i++)
  652. xenfb_guest_copy(xenfb,
  653. xenfb->up_rects[i].x,
  654. xenfb->up_rects[i].y,
  655. xenfb->up_rects[i].w,
  656. xenfb->up_rects[i].h);
  657. } else {
  658. xen_be_printf(&xenfb->c.xendev, 3, "update: nothing\n");
  659. }
  660. xenfb->up_count = 0;
  661. xenfb->up_fullscreen = 0;
  662. }
  663. static void xenfb_update_interval(void *opaque, uint64_t interval)
  664. {
  665. struct XenFB *xenfb = opaque;
  666. if (xenfb->feature_update) {
  667. #ifdef XENFB_TYPE_REFRESH_PERIOD
  668. if (xenfb_queue_full(xenfb)) {
  669. return;
  670. }
  671. xenfb_send_refresh_period(xenfb, interval);
  672. #endif
  673. }
  674. }
  675. /* QEMU display state changed, so refresh the framebuffer copy */
  676. static void xenfb_invalidate(void *opaque)
  677. {
  678. struct XenFB *xenfb = opaque;
  679. xenfb->up_fullscreen = 1;
  680. }
  681. static void xenfb_handle_events(struct XenFB *xenfb)
  682. {
  683. uint32_t prod, cons;
  684. struct xenfb_page *page = xenfb->c.page;
  685. prod = page->out_prod;
  686. if (prod == page->out_cons)
  687. return;
  688. xen_rmb(); /* ensure we see ring contents up to prod */
  689. for (cons = page->out_cons; cons != prod; cons++) {
  690. union xenfb_out_event *event = &XENFB_OUT_RING_REF(page, cons);
  691. int x, y, w, h;
  692. switch (event->type) {
  693. case XENFB_TYPE_UPDATE:
  694. if (xenfb->up_count == UP_QUEUE)
  695. xenfb->up_fullscreen = 1;
  696. if (xenfb->up_fullscreen)
  697. break;
  698. x = MAX(event->update.x, 0);
  699. y = MAX(event->update.y, 0);
  700. w = MIN(event->update.width, xenfb->width - x);
  701. h = MIN(event->update.height, xenfb->height - y);
  702. if (w < 0 || h < 0) {
  703. xen_be_printf(&xenfb->c.xendev, 1, "bogus update ignored\n");
  704. break;
  705. }
  706. if (x != event->update.x ||
  707. y != event->update.y ||
  708. w != event->update.width ||
  709. h != event->update.height) {
  710. xen_be_printf(&xenfb->c.xendev, 1, "bogus update clipped\n");
  711. }
  712. if (w == xenfb->width && h > xenfb->height / 2) {
  713. /* scroll detector: updated more than 50% of the lines,
  714. * don't bother keeping track of the rectangles then */
  715. xenfb->up_fullscreen = 1;
  716. } else {
  717. xenfb->up_rects[xenfb->up_count].x = x;
  718. xenfb->up_rects[xenfb->up_count].y = y;
  719. xenfb->up_rects[xenfb->up_count].w = w;
  720. xenfb->up_rects[xenfb->up_count].h = h;
  721. xenfb->up_count++;
  722. }
  723. break;
  724. #ifdef XENFB_TYPE_RESIZE
  725. case XENFB_TYPE_RESIZE:
  726. if (xenfb_configure_fb(xenfb, xenfb->fb_len,
  727. event->resize.width,
  728. event->resize.height,
  729. event->resize.depth,
  730. xenfb->fb_len,
  731. event->resize.offset,
  732. event->resize.stride) < 0)
  733. break;
  734. xenfb_invalidate(xenfb);
  735. break;
  736. #endif
  737. }
  738. }
  739. xen_mb(); /* ensure we're done with ring contents */
  740. page->out_cons = cons;
  741. }
  742. static int fb_init(struct XenDevice *xendev)
  743. {
  744. #ifdef XENFB_TYPE_RESIZE
  745. xenstore_write_be_int(xendev, "feature-resize", 1);
  746. #endif
  747. return 0;
  748. }
  749. static int fb_initialise(struct XenDevice *xendev)
  750. {
  751. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  752. struct xenfb_page *fb_page;
  753. int videoram;
  754. int rc;
  755. if (xenstore_read_fe_int(xendev, "videoram", &videoram) == -1)
  756. videoram = 0;
  757. rc = common_bind(&fb->c);
  758. if (rc != 0)
  759. return rc;
  760. fb_page = fb->c.page;
  761. rc = xenfb_configure_fb(fb, videoram * 1024 * 1024U,
  762. fb_page->width, fb_page->height, fb_page->depth,
  763. fb_page->mem_length, 0, fb_page->line_length);
  764. if (rc != 0)
  765. return rc;
  766. rc = xenfb_map_fb(fb);
  767. if (rc != 0)
  768. return rc;
  769. #if 0 /* handled in xen_init_display() for now */
  770. if (!fb->have_console) {
  771. fb->c.ds = graphic_console_init(xenfb_update,
  772. xenfb_invalidate,
  773. NULL,
  774. NULL,
  775. fb);
  776. fb->have_console = 1;
  777. }
  778. #endif
  779. if (xenstore_read_fe_int(xendev, "feature-update", &fb->feature_update) == -1)
  780. fb->feature_update = 0;
  781. if (fb->feature_update)
  782. xenstore_write_be_int(xendev, "request-update", 1);
  783. xen_be_printf(xendev, 1, "feature-update=%d, videoram=%d\n",
  784. fb->feature_update, videoram);
  785. return 0;
  786. }
  787. static void fb_disconnect(struct XenDevice *xendev)
  788. {
  789. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  790. /*
  791. * FIXME: qemu can't un-init gfx display (yet?).
  792. * Replacing the framebuffer with anonymous shared memory
  793. * instead. This releases the guest pages and keeps qemu happy.
  794. */
  795. fb->pixels = mmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE,
  796. PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
  797. -1, 0);
  798. if (fb->pixels == MAP_FAILED) {
  799. xen_be_printf(xendev, 0,
  800. "Couldn't replace the framebuffer with anonymous memory errno=%d\n",
  801. errno);
  802. }
  803. common_unbind(&fb->c);
  804. fb->feature_update = 0;
  805. fb->bug_trigger = 0;
  806. }
  807. static void fb_frontend_changed(struct XenDevice *xendev, const char *node)
  808. {
  809. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  810. /*
  811. * Set state to Connected *again* once the frontend switched
  812. * to connected. We must trigger the watch a second time to
  813. * workaround a frontend bug.
  814. */
  815. if (fb->bug_trigger == 0 && strcmp(node, "state") == 0 &&
  816. xendev->fe_state == XenbusStateConnected &&
  817. xendev->be_state == XenbusStateConnected) {
  818. xen_be_printf(xendev, 2, "re-trigger connected (frontend bug)\n");
  819. xen_be_set_state(xendev, XenbusStateConnected);
  820. fb->bug_trigger = 1; /* only once */
  821. }
  822. }
  823. static void fb_event(struct XenDevice *xendev)
  824. {
  825. struct XenFB *xenfb = container_of(xendev, struct XenFB, c.xendev);
  826. xenfb_handle_events(xenfb);
  827. xen_be_send_notify(&xenfb->c.xendev);
  828. }
  829. /* -------------------------------------------------------------------- */
  830. struct XenDevOps xen_kbdmouse_ops = {
  831. .size = sizeof(struct XenInput),
  832. .init = input_init,
  833. .initialise = input_initialise,
  834. .connected = input_connected,
  835. .disconnect = input_disconnect,
  836. .event = input_event,
  837. };
  838. struct XenDevOps xen_framebuffer_ops = {
  839. .size = sizeof(struct XenFB),
  840. .init = fb_init,
  841. .initialise = fb_initialise,
  842. .disconnect = fb_disconnect,
  843. .event = fb_event,
  844. .frontend_changed = fb_frontend_changed,
  845. };
  846. static const GraphicHwOps xenfb_ops = {
  847. .invalidate = xenfb_invalidate,
  848. .gfx_update = xenfb_update,
  849. .update_interval = xenfb_update_interval,
  850. };
  851. /*
  852. * FIXME/TODO: Kill this.
  853. * Temporary needed while DisplayState reorganization is in flight.
  854. */
  855. void xen_init_display(int domid)
  856. {
  857. struct XenDevice *xfb, *xin;
  858. struct XenFB *fb;
  859. struct XenInput *in;
  860. int i = 0;
  861. wait_more:
  862. i++;
  863. main_loop_wait(true);
  864. xfb = xen_be_find_xendev("vfb", domid, 0);
  865. xin = xen_be_find_xendev("vkbd", domid, 0);
  866. if (!xfb || !xin) {
  867. if (i < 256) {
  868. usleep(10000);
  869. goto wait_more;
  870. }
  871. xen_be_printf(NULL, 1, "displaystate setup failed\n");
  872. return;
  873. }
  874. /* vfb */
  875. fb = container_of(xfb, struct XenFB, c.xendev);
  876. fb->c.con = graphic_console_init(NULL, 0, &xenfb_ops, fb);
  877. fb->have_console = 1;
  878. /* vkbd */
  879. in = container_of(xin, struct XenInput, c.xendev);
  880. in->c.con = fb->c.con;
  881. /* retry ->init() */
  882. xen_be_check_state(xin);
  883. xen_be_check_state(xfb);
  884. }