xenfb.c 30 KB

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