xenfb.c 30 KB

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