xenfb.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. uint64_t mfn;
  85. if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn) == -1)
  86. return -1;
  87. assert(mfn == (xen_pfn_t)mfn);
  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 %"PRIx64", 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. DisplaySurface *surface = qemu_console_surface(xenfb->c.con);
  279. int dw = surface_width(surface);
  280. int dh = surface_height(surface);
  281. int i;
  282. if (xenfb->abs_pointer_wanted)
  283. xenfb_send_position(xenfb,
  284. dx * (dw - 1) / 0x7fff,
  285. dy * (dh - 1) / 0x7fff,
  286. dz);
  287. else
  288. xenfb_send_motion(xenfb, dx, dy, dz);
  289. for (i = 0 ; i < 8 ; i++) {
  290. int lastDown = xenfb->button_state & (1 << i);
  291. int down = button_state & (1 << i);
  292. if (down == lastDown)
  293. continue;
  294. if (xenfb_send_key(xenfb, down, BTN_LEFT+i) < 0)
  295. return;
  296. }
  297. xenfb->button_state = button_state;
  298. }
  299. static int input_init(struct XenDevice *xendev)
  300. {
  301. xenstore_write_be_int(xendev, "feature-abs-pointer", 1);
  302. return 0;
  303. }
  304. static int input_initialise(struct XenDevice *xendev)
  305. {
  306. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  307. int rc;
  308. if (!in->c.con) {
  309. xen_be_printf(xendev, 1, "ds not set (yet)\n");
  310. return -1;
  311. }
  312. rc = common_bind(&in->c);
  313. if (rc != 0)
  314. return rc;
  315. qemu_add_kbd_event_handler(xenfb_key_event, in);
  316. return 0;
  317. }
  318. static void input_connected(struct XenDevice *xendev)
  319. {
  320. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  321. if (xenstore_read_fe_int(xendev, "request-abs-pointer",
  322. &in->abs_pointer_wanted) == -1) {
  323. in->abs_pointer_wanted = 0;
  324. }
  325. if (in->qmouse) {
  326. qemu_remove_mouse_event_handler(in->qmouse);
  327. }
  328. in->qmouse = qemu_add_mouse_event_handler(xenfb_mouse_event, in,
  329. in->abs_pointer_wanted,
  330. "Xen PVFB Mouse");
  331. }
  332. static void input_disconnect(struct XenDevice *xendev)
  333. {
  334. struct XenInput *in = container_of(xendev, struct XenInput, c.xendev);
  335. if (in->qmouse) {
  336. qemu_remove_mouse_event_handler(in->qmouse);
  337. in->qmouse = NULL;
  338. }
  339. qemu_add_kbd_event_handler(NULL, NULL);
  340. common_unbind(&in->c);
  341. }
  342. static void input_event(struct XenDevice *xendev)
  343. {
  344. struct XenInput *xenfb = container_of(xendev, struct XenInput, c.xendev);
  345. struct xenkbd_page *page = xenfb->c.page;
  346. /* We don't understand any keyboard events, so just ignore them. */
  347. if (page->out_prod == page->out_cons)
  348. return;
  349. page->out_cons = page->out_prod;
  350. xen_be_send_notify(&xenfb->c.xendev);
  351. }
  352. /* -------------------------------------------------------------------- */
  353. static void xenfb_copy_mfns(int mode, int count, xen_pfn_t *dst, void *src)
  354. {
  355. uint32_t *src32 = src;
  356. uint64_t *src64 = src;
  357. int i;
  358. for (i = 0; i < count; i++)
  359. dst[i] = (mode == 32) ? src32[i] : src64[i];
  360. }
  361. static int xenfb_map_fb(struct XenFB *xenfb)
  362. {
  363. struct xenfb_page *page = xenfb->c.page;
  364. char *protocol = xenfb->c.xendev.protocol;
  365. int n_fbdirs;
  366. xen_pfn_t *pgmfns = NULL;
  367. xen_pfn_t *fbmfns = NULL;
  368. void *map, *pd;
  369. int mode, ret = -1;
  370. /* default to native */
  371. pd = page->pd;
  372. mode = sizeof(unsigned long) * 8;
  373. if (!protocol) {
  374. /*
  375. * Undefined protocol, some guesswork needed.
  376. *
  377. * Old frontends which don't set the protocol use
  378. * one page directory only, thus pd[1] must be zero.
  379. * pd[1] of the 32bit struct layout and the lower
  380. * 32 bits of pd[0] of the 64bit struct layout have
  381. * the same location, so we can check that ...
  382. */
  383. uint32_t *ptr32 = NULL;
  384. uint32_t *ptr64 = NULL;
  385. #if defined(__i386__)
  386. ptr32 = (void*)page->pd;
  387. ptr64 = ((void*)page->pd) + 4;
  388. #elif defined(__x86_64__)
  389. ptr32 = ((void*)page->pd) - 4;
  390. ptr64 = (void*)page->pd;
  391. #endif
  392. if (ptr32) {
  393. if (ptr32[1] == 0) {
  394. mode = 32;
  395. pd = ptr32;
  396. } else {
  397. mode = 64;
  398. pd = ptr64;
  399. }
  400. }
  401. #if defined(__x86_64__)
  402. } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
  403. /* 64bit dom0, 32bit domU */
  404. mode = 32;
  405. pd = ((void*)page->pd) - 4;
  406. #elif defined(__i386__)
  407. } else if (strcmp(protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
  408. /* 32bit dom0, 64bit domU */
  409. mode = 64;
  410. pd = ((void*)page->pd) + 4;
  411. #endif
  412. }
  413. if (xenfb->pixels) {
  414. munmap(xenfb->pixels, xenfb->fbpages * XC_PAGE_SIZE);
  415. xenfb->pixels = NULL;
  416. }
  417. xenfb->fbpages = (xenfb->fb_len + (XC_PAGE_SIZE - 1)) / XC_PAGE_SIZE;
  418. n_fbdirs = xenfb->fbpages * mode / 8;
  419. n_fbdirs = (n_fbdirs + (XC_PAGE_SIZE - 1)) / XC_PAGE_SIZE;
  420. pgmfns = g_malloc0(sizeof(xen_pfn_t) * n_fbdirs);
  421. fbmfns = g_malloc0(sizeof(xen_pfn_t) * xenfb->fbpages);
  422. xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
  423. map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
  424. PROT_READ, pgmfns, n_fbdirs);
  425. if (map == NULL)
  426. goto out;
  427. xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
  428. munmap(map, n_fbdirs * XC_PAGE_SIZE);
  429. xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
  430. PROT_READ, fbmfns, xenfb->fbpages);
  431. if (xenfb->pixels == NULL)
  432. goto out;
  433. ret = 0; /* all is fine */
  434. out:
  435. g_free(pgmfns);
  436. g_free(fbmfns);
  437. return ret;
  438. }
  439. static int xenfb_configure_fb(struct XenFB *xenfb, size_t fb_len_lim,
  440. int width, int height, int depth,
  441. size_t fb_len, int offset, int row_stride)
  442. {
  443. size_t mfn_sz = sizeof(*((struct xenfb_page *)0)->pd);
  444. size_t pd_len = sizeof(((struct xenfb_page *)0)->pd) / mfn_sz;
  445. size_t fb_pages = pd_len * XC_PAGE_SIZE / mfn_sz;
  446. size_t fb_len_max = fb_pages * XC_PAGE_SIZE;
  447. int max_width, max_height;
  448. if (fb_len_lim > fb_len_max) {
  449. xen_be_printf(&xenfb->c.xendev, 0, "fb size limit %zu exceeds %zu, corrected\n",
  450. fb_len_lim, fb_len_max);
  451. fb_len_lim = fb_len_max;
  452. }
  453. if (fb_len_lim && fb_len > fb_len_lim) {
  454. xen_be_printf(&xenfb->c.xendev, 0, "frontend fb size %zu limited to %zu\n",
  455. fb_len, fb_len_lim);
  456. fb_len = fb_len_lim;
  457. }
  458. if (depth != 8 && depth != 16 && depth != 24 && depth != 32) {
  459. xen_be_printf(&xenfb->c.xendev, 0, "can't handle frontend fb depth %d\n",
  460. depth);
  461. return -1;
  462. }
  463. if (row_stride <= 0 || row_stride > fb_len) {
  464. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend stride %d\n", row_stride);
  465. return -1;
  466. }
  467. max_width = row_stride / (depth / 8);
  468. if (width < 0 || width > max_width) {
  469. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend width %d limited to %d\n",
  470. width, max_width);
  471. width = max_width;
  472. }
  473. if (offset < 0 || offset >= fb_len) {
  474. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend offset %d (max %zu)\n",
  475. offset, fb_len - 1);
  476. return -1;
  477. }
  478. max_height = (fb_len - offset) / row_stride;
  479. if (height < 0 || height > max_height) {
  480. xen_be_printf(&xenfb->c.xendev, 0, "invalid frontend height %d limited to %d\n",
  481. height, max_height);
  482. height = max_height;
  483. }
  484. xenfb->fb_len = fb_len;
  485. xenfb->row_stride = row_stride;
  486. xenfb->depth = depth;
  487. xenfb->width = width;
  488. xenfb->height = height;
  489. xenfb->offset = offset;
  490. xenfb->up_fullscreen = 1;
  491. xenfb->do_resize = 1;
  492. xen_be_printf(&xenfb->c.xendev, 1, "framebuffer %dx%dx%d offset %d stride %d\n",
  493. width, height, depth, offset, row_stride);
  494. return 0;
  495. }
  496. /* A convenient function for munging pixels between different depths */
  497. #define BLT(SRC_T,DST_T,RSB,GSB,BSB,RDB,GDB,BDB) \
  498. for (line = y ; line < (y+h) ; line++) { \
  499. SRC_T *src = (SRC_T *)(xenfb->pixels \
  500. + xenfb->offset \
  501. + (line * xenfb->row_stride) \
  502. + (x * xenfb->depth / 8)); \
  503. DST_T *dst = (DST_T *)(data \
  504. + (line * linesize) \
  505. + (x * bpp / 8)); \
  506. int col; \
  507. const int RSS = 32 - (RSB + GSB + BSB); \
  508. const int GSS = 32 - (GSB + BSB); \
  509. const int BSS = 32 - (BSB); \
  510. const uint32_t RSM = (~0U) << (32 - RSB); \
  511. const uint32_t GSM = (~0U) << (32 - GSB); \
  512. const uint32_t BSM = (~0U) << (32 - BSB); \
  513. const int RDS = 32 - (RDB + GDB + BDB); \
  514. const int GDS = 32 - (GDB + BDB); \
  515. const int BDS = 32 - (BDB); \
  516. const uint32_t RDM = (~0U) << (32 - RDB); \
  517. const uint32_t GDM = (~0U) << (32 - GDB); \
  518. const uint32_t BDM = (~0U) << (32 - BDB); \
  519. for (col = x ; col < (x+w) ; col++) { \
  520. uint32_t spix = *src; \
  521. *dst = (((spix << RSS) & RSM & RDM) >> RDS) | \
  522. (((spix << GSS) & GSM & GDM) >> GDS) | \
  523. (((spix << BSS) & BSM & BDM) >> BDS); \
  524. src = (SRC_T *) ((unsigned long) src + xenfb->depth / 8); \
  525. dst = (DST_T *) ((unsigned long) dst + bpp / 8); \
  526. } \
  527. }
  528. /*
  529. * This copies data from the guest framebuffer region, into QEMU's
  530. * displaysurface. qemu uses 16 or 32 bpp. In case the pv framebuffer
  531. * uses something else we must convert and copy, otherwise we can
  532. * supply the buffer directly and no thing here.
  533. */
  534. static void xenfb_guest_copy(struct XenFB *xenfb, int x, int y, int w, int h)
  535. {
  536. DisplaySurface *surface = qemu_console_surface(xenfb->c.con);
  537. int line, oops = 0;
  538. int bpp = surface_bits_per_pixel(surface);
  539. int linesize = surface_stride(surface);
  540. uint8_t *data = surface_data(surface);
  541. if (!is_buffer_shared(surface)) {
  542. switch (xenfb->depth) {
  543. case 8:
  544. if (bpp == 16) {
  545. BLT(uint8_t, uint16_t, 3, 3, 2, 5, 6, 5);
  546. } else if (bpp == 32) {
  547. BLT(uint8_t, uint32_t, 3, 3, 2, 8, 8, 8);
  548. } else {
  549. oops = 1;
  550. }
  551. break;
  552. case 24:
  553. if (bpp == 16) {
  554. BLT(uint32_t, uint16_t, 8, 8, 8, 5, 6, 5);
  555. } else if (bpp == 32) {
  556. BLT(uint32_t, uint32_t, 8, 8, 8, 8, 8, 8);
  557. } else {
  558. oops = 1;
  559. }
  560. break;
  561. default:
  562. oops = 1;
  563. }
  564. }
  565. if (oops) /* should not happen */
  566. xen_be_printf(&xenfb->c.xendev, 0, "%s: oops: convert %d -> %d bpp?\n",
  567. __FUNCTION__, xenfb->depth, bpp);
  568. dpy_gfx_update(xenfb->c.con, x, y, w, h);
  569. }
  570. #ifdef XENFB_TYPE_REFRESH_PERIOD
  571. static int xenfb_queue_full(struct XenFB *xenfb)
  572. {
  573. struct xenfb_page *page = xenfb->c.page;
  574. uint32_t cons, prod;
  575. if (!page)
  576. return 1;
  577. prod = page->in_prod;
  578. cons = page->in_cons;
  579. return prod - cons == XENFB_IN_RING_LEN;
  580. }
  581. static void xenfb_send_event(struct XenFB *xenfb, union xenfb_in_event *event)
  582. {
  583. uint32_t prod;
  584. struct xenfb_page *page = xenfb->c.page;
  585. prod = page->in_prod;
  586. /* caller ensures !xenfb_queue_full() */
  587. xen_mb(); /* ensure ring space available */
  588. XENFB_IN_RING_REF(page, prod) = *event;
  589. xen_wmb(); /* ensure ring contents visible */
  590. page->in_prod = prod + 1;
  591. xen_be_send_notify(&xenfb->c.xendev);
  592. }
  593. static void xenfb_send_refresh_period(struct XenFB *xenfb, int period)
  594. {
  595. union xenfb_in_event event;
  596. memset(&event, 0, sizeof(event));
  597. event.type = XENFB_TYPE_REFRESH_PERIOD;
  598. event.refresh_period.period = period;
  599. xenfb_send_event(xenfb, &event);
  600. }
  601. #endif
  602. /*
  603. * Periodic update of display.
  604. * Also transmit the refresh interval to the frontend.
  605. *
  606. * Never ever do any qemu display operations
  607. * (resize, screen update) outside this function.
  608. * Our screen might be inactive. When asked for
  609. * an update we know it is active.
  610. */
  611. static void xenfb_update(void *opaque)
  612. {
  613. struct XenFB *xenfb = opaque;
  614. DisplaySurface *surface;
  615. int i;
  616. if (xenfb->c.xendev.be_state != XenbusStateConnected)
  617. return;
  618. if (!xenfb->feature_update) {
  619. /* we don't get update notifications, thus use the
  620. * sledge hammer approach ... */
  621. xenfb->up_fullscreen = 1;
  622. }
  623. /* resize if needed */
  624. if (xenfb->do_resize) {
  625. xenfb->do_resize = 0;
  626. switch (xenfb->depth) {
  627. case 16:
  628. case 32:
  629. /* console.c supported depth -> buffer can be used directly */
  630. surface = qemu_create_displaysurface_from
  631. (xenfb->width, xenfb->height, xenfb->depth,
  632. xenfb->row_stride, xenfb->pixels + xenfb->offset,
  633. false);
  634. break;
  635. default:
  636. /* we must convert stuff */
  637. surface = qemu_create_displaysurface(xenfb->width, xenfb->height);
  638. break;
  639. }
  640. dpy_gfx_replace_surface(xenfb->c.con, surface);
  641. xen_be_printf(&xenfb->c.xendev, 1, "update: resizing: %dx%d @ %d bpp%s\n",
  642. xenfb->width, xenfb->height, xenfb->depth,
  643. is_buffer_shared(surface) ? " (shared)" : "");
  644. xenfb->up_fullscreen = 1;
  645. }
  646. /* run queued updates */
  647. if (xenfb->up_fullscreen) {
  648. xen_be_printf(&xenfb->c.xendev, 3, "update: fullscreen\n");
  649. xenfb_guest_copy(xenfb, 0, 0, xenfb->width, xenfb->height);
  650. } else if (xenfb->up_count) {
  651. xen_be_printf(&xenfb->c.xendev, 3, "update: %d rects\n", xenfb->up_count);
  652. for (i = 0; i < xenfb->up_count; i++)
  653. xenfb_guest_copy(xenfb,
  654. xenfb->up_rects[i].x,
  655. xenfb->up_rects[i].y,
  656. xenfb->up_rects[i].w,
  657. xenfb->up_rects[i].h);
  658. } else {
  659. xen_be_printf(&xenfb->c.xendev, 3, "update: nothing\n");
  660. }
  661. xenfb->up_count = 0;
  662. xenfb->up_fullscreen = 0;
  663. }
  664. static void xenfb_update_interval(void *opaque, uint64_t interval)
  665. {
  666. struct XenFB *xenfb = opaque;
  667. if (xenfb->feature_update) {
  668. #ifdef XENFB_TYPE_REFRESH_PERIOD
  669. if (xenfb_queue_full(xenfb)) {
  670. return;
  671. }
  672. xenfb_send_refresh_period(xenfb, interval);
  673. #endif
  674. }
  675. }
  676. /* QEMU display state changed, so refresh the framebuffer copy */
  677. static void xenfb_invalidate(void *opaque)
  678. {
  679. struct XenFB *xenfb = opaque;
  680. xenfb->up_fullscreen = 1;
  681. }
  682. static void xenfb_handle_events(struct XenFB *xenfb)
  683. {
  684. uint32_t prod, cons;
  685. struct xenfb_page *page = xenfb->c.page;
  686. prod = page->out_prod;
  687. if (prod == page->out_cons)
  688. return;
  689. xen_rmb(); /* ensure we see ring contents up to prod */
  690. for (cons = page->out_cons; cons != prod; cons++) {
  691. union xenfb_out_event *event = &XENFB_OUT_RING_REF(page, cons);
  692. int x, y, w, h;
  693. switch (event->type) {
  694. case XENFB_TYPE_UPDATE:
  695. if (xenfb->up_count == UP_QUEUE)
  696. xenfb->up_fullscreen = 1;
  697. if (xenfb->up_fullscreen)
  698. break;
  699. x = MAX(event->update.x, 0);
  700. y = MAX(event->update.y, 0);
  701. w = MIN(event->update.width, xenfb->width - x);
  702. h = MIN(event->update.height, xenfb->height - y);
  703. if (w < 0 || h < 0) {
  704. xen_be_printf(&xenfb->c.xendev, 1, "bogus update ignored\n");
  705. break;
  706. }
  707. if (x != event->update.x ||
  708. y != event->update.y ||
  709. w != event->update.width ||
  710. h != event->update.height) {
  711. xen_be_printf(&xenfb->c.xendev, 1, "bogus update clipped\n");
  712. }
  713. if (w == xenfb->width && h > xenfb->height / 2) {
  714. /* scroll detector: updated more than 50% of the lines,
  715. * don't bother keeping track of the rectangles then */
  716. xenfb->up_fullscreen = 1;
  717. } else {
  718. xenfb->up_rects[xenfb->up_count].x = x;
  719. xenfb->up_rects[xenfb->up_count].y = y;
  720. xenfb->up_rects[xenfb->up_count].w = w;
  721. xenfb->up_rects[xenfb->up_count].h = h;
  722. xenfb->up_count++;
  723. }
  724. break;
  725. #ifdef XENFB_TYPE_RESIZE
  726. case XENFB_TYPE_RESIZE:
  727. if (xenfb_configure_fb(xenfb, xenfb->fb_len,
  728. event->resize.width,
  729. event->resize.height,
  730. event->resize.depth,
  731. xenfb->fb_len,
  732. event->resize.offset,
  733. event->resize.stride) < 0)
  734. break;
  735. xenfb_invalidate(xenfb);
  736. break;
  737. #endif
  738. }
  739. }
  740. xen_mb(); /* ensure we're done with ring contents */
  741. page->out_cons = cons;
  742. }
  743. static int fb_init(struct XenDevice *xendev)
  744. {
  745. #ifdef XENFB_TYPE_RESIZE
  746. xenstore_write_be_int(xendev, "feature-resize", 1);
  747. #endif
  748. return 0;
  749. }
  750. static int fb_initialise(struct XenDevice *xendev)
  751. {
  752. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  753. struct xenfb_page *fb_page;
  754. int videoram;
  755. int rc;
  756. if (xenstore_read_fe_int(xendev, "videoram", &videoram) == -1)
  757. videoram = 0;
  758. rc = common_bind(&fb->c);
  759. if (rc != 0)
  760. return rc;
  761. fb_page = fb->c.page;
  762. rc = xenfb_configure_fb(fb, videoram * 1024 * 1024U,
  763. fb_page->width, fb_page->height, fb_page->depth,
  764. fb_page->mem_length, 0, fb_page->line_length);
  765. if (rc != 0)
  766. return rc;
  767. rc = xenfb_map_fb(fb);
  768. if (rc != 0)
  769. return rc;
  770. #if 0 /* handled in xen_init_display() for now */
  771. if (!fb->have_console) {
  772. fb->c.ds = graphic_console_init(xenfb_update,
  773. xenfb_invalidate,
  774. NULL,
  775. NULL,
  776. fb);
  777. fb->have_console = 1;
  778. }
  779. #endif
  780. if (xenstore_read_fe_int(xendev, "feature-update", &fb->feature_update) == -1)
  781. fb->feature_update = 0;
  782. if (fb->feature_update)
  783. xenstore_write_be_int(xendev, "request-update", 1);
  784. xen_be_printf(xendev, 1, "feature-update=%d, videoram=%d\n",
  785. fb->feature_update, videoram);
  786. return 0;
  787. }
  788. static void fb_disconnect(struct XenDevice *xendev)
  789. {
  790. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  791. /*
  792. * FIXME: qemu can't un-init gfx display (yet?).
  793. * Replacing the framebuffer with anonymous shared memory
  794. * instead. This releases the guest pages and keeps qemu happy.
  795. */
  796. fb->pixels = mmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE,
  797. PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
  798. -1, 0);
  799. if (fb->pixels == MAP_FAILED) {
  800. xen_be_printf(xendev, 0,
  801. "Couldn't replace the framebuffer with anonymous memory errno=%d\n",
  802. errno);
  803. }
  804. common_unbind(&fb->c);
  805. fb->feature_update = 0;
  806. fb->bug_trigger = 0;
  807. }
  808. static void fb_frontend_changed(struct XenDevice *xendev, const char *node)
  809. {
  810. struct XenFB *fb = container_of(xendev, struct XenFB, c.xendev);
  811. /*
  812. * Set state to Connected *again* once the frontend switched
  813. * to connected. We must trigger the watch a second time to
  814. * workaround a frontend bug.
  815. */
  816. if (fb->bug_trigger == 0 && strcmp(node, "state") == 0 &&
  817. xendev->fe_state == XenbusStateConnected &&
  818. xendev->be_state == XenbusStateConnected) {
  819. xen_be_printf(xendev, 2, "re-trigger connected (frontend bug)\n");
  820. xen_be_set_state(xendev, XenbusStateConnected);
  821. fb->bug_trigger = 1; /* only once */
  822. }
  823. }
  824. static void fb_event(struct XenDevice *xendev)
  825. {
  826. struct XenFB *xenfb = container_of(xendev, struct XenFB, c.xendev);
  827. xenfb_handle_events(xenfb);
  828. xen_be_send_notify(&xenfb->c.xendev);
  829. }
  830. /* -------------------------------------------------------------------- */
  831. struct XenDevOps xen_kbdmouse_ops = {
  832. .size = sizeof(struct XenInput),
  833. .init = input_init,
  834. .initialise = input_initialise,
  835. .connected = input_connected,
  836. .disconnect = input_disconnect,
  837. .event = input_event,
  838. };
  839. struct XenDevOps xen_framebuffer_ops = {
  840. .size = sizeof(struct XenFB),
  841. .init = fb_init,
  842. .initialise = fb_initialise,
  843. .disconnect = fb_disconnect,
  844. .event = fb_event,
  845. .frontend_changed = fb_frontend_changed,
  846. };
  847. static const GraphicHwOps xenfb_ops = {
  848. .invalidate = xenfb_invalidate,
  849. .gfx_update = xenfb_update,
  850. .update_interval = xenfb_update_interval,
  851. };
  852. /*
  853. * FIXME/TODO: Kill this.
  854. * Temporary needed while DisplayState reorganization is in flight.
  855. */
  856. void xen_init_display(int domid)
  857. {
  858. struct XenDevice *xfb, *xin;
  859. struct XenFB *fb;
  860. struct XenInput *in;
  861. int i = 0;
  862. wait_more:
  863. i++;
  864. main_loop_wait(true);
  865. xfb = xen_be_find_xendev("vfb", domid, 0);
  866. xin = xen_be_find_xendev("vkbd", domid, 0);
  867. if (!xfb || !xin) {
  868. if (i < 256) {
  869. usleep(10000);
  870. goto wait_more;
  871. }
  872. xen_be_printf(NULL, 1, "displaystate setup failed\n");
  873. return;
  874. }
  875. /* vfb */
  876. fb = container_of(xfb, struct XenFB, c.xendev);
  877. fb->c.con = graphic_console_init(NULL, 0, &xenfb_ops, fb);
  878. fb->have_console = 1;
  879. /* vkbd */
  880. in = container_of(xin, struct XenInput, c.xendev);
  881. in->c.con = fb->c.con;
  882. /* retry ->init() */
  883. xen_be_check_state(xin);
  884. xen_be_check_state(xfb);
  885. }