xen_console.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2005
  3. * Author(s): Anthony Liguori <aliguori@us.ibm.com>
  4. *
  5. * Copyright (C) Red Hat 2007
  6. *
  7. * Xen Console
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; under version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include <sys/select.h>
  23. #include <termios.h>
  24. #include "qapi/error.h"
  25. #include "sysemu/sysemu.h"
  26. #include "chardev/char-fe.h"
  27. #include "hw/xen/xen-legacy-backend.h"
  28. #include "hw/xen/interface/io/console.h"
  29. struct buffer {
  30. uint8_t *data;
  31. size_t consumed;
  32. size_t size;
  33. size_t capacity;
  34. size_t max_capacity;
  35. };
  36. struct XenConsole {
  37. struct XenLegacyDevice xendev; /* must be first */
  38. struct buffer buffer;
  39. char console[XEN_BUFSIZE];
  40. int ring_ref;
  41. void *sring;
  42. CharBackend chr;
  43. int backlog;
  44. };
  45. static void buffer_append(struct XenConsole *con)
  46. {
  47. struct buffer *buffer = &con->buffer;
  48. XENCONS_RING_IDX cons, prod, size;
  49. struct xencons_interface *intf = con->sring;
  50. cons = intf->out_cons;
  51. prod = intf->out_prod;
  52. xen_mb();
  53. size = prod - cons;
  54. if ((size == 0) || (size > sizeof(intf->out)))
  55. return;
  56. if ((buffer->capacity - buffer->size) < size) {
  57. buffer->capacity += (size + 1024);
  58. buffer->data = g_realloc(buffer->data, buffer->capacity);
  59. }
  60. while (cons != prod)
  61. buffer->data[buffer->size++] = intf->out[
  62. MASK_XENCONS_IDX(cons++, intf->out)];
  63. xen_mb();
  64. intf->out_cons = cons;
  65. xen_pv_send_notify(&con->xendev);
  66. if (buffer->max_capacity &&
  67. buffer->size > buffer->max_capacity) {
  68. /* Discard the middle of the data. */
  69. size_t over = buffer->size - buffer->max_capacity;
  70. uint8_t *maxpos = buffer->data + buffer->max_capacity;
  71. memmove(maxpos - over, maxpos, over);
  72. buffer->data = g_realloc(buffer->data, buffer->max_capacity);
  73. buffer->size = buffer->capacity = buffer->max_capacity;
  74. if (buffer->consumed > buffer->max_capacity - over)
  75. buffer->consumed = buffer->max_capacity - over;
  76. }
  77. }
  78. static void buffer_advance(struct buffer *buffer, size_t len)
  79. {
  80. buffer->consumed += len;
  81. if (buffer->consumed == buffer->size) {
  82. buffer->consumed = 0;
  83. buffer->size = 0;
  84. }
  85. }
  86. static int ring_free_bytes(struct XenConsole *con)
  87. {
  88. struct xencons_interface *intf = con->sring;
  89. XENCONS_RING_IDX cons, prod, space;
  90. cons = intf->in_cons;
  91. prod = intf->in_prod;
  92. xen_mb();
  93. space = prod - cons;
  94. if (space > sizeof(intf->in))
  95. return 0; /* ring is screwed: ignore it */
  96. return (sizeof(intf->in) - space);
  97. }
  98. static int xencons_can_receive(void *opaque)
  99. {
  100. struct XenConsole *con = opaque;
  101. return ring_free_bytes(con);
  102. }
  103. static void xencons_receive(void *opaque, const uint8_t *buf, int len)
  104. {
  105. struct XenConsole *con = opaque;
  106. struct xencons_interface *intf = con->sring;
  107. XENCONS_RING_IDX prod;
  108. int i, max;
  109. max = ring_free_bytes(con);
  110. /* The can_receive() func limits this, but check again anyway */
  111. if (max < len)
  112. len = max;
  113. prod = intf->in_prod;
  114. for (i = 0; i < len; i++) {
  115. intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
  116. buf[i];
  117. }
  118. xen_wmb();
  119. intf->in_prod = prod;
  120. xen_pv_send_notify(&con->xendev);
  121. }
  122. static void xencons_send(struct XenConsole *con)
  123. {
  124. ssize_t len, size;
  125. size = con->buffer.size - con->buffer.consumed;
  126. if (qemu_chr_fe_backend_connected(&con->chr)) {
  127. len = qemu_chr_fe_write(&con->chr,
  128. con->buffer.data + con->buffer.consumed,
  129. size);
  130. } else {
  131. len = size;
  132. }
  133. if (len < 1) {
  134. if (!con->backlog) {
  135. con->backlog = 1;
  136. xen_pv_printf(&con->xendev, 1,
  137. "backlog piling up, nobody listening?\n");
  138. }
  139. } else {
  140. buffer_advance(&con->buffer, len);
  141. if (con->backlog && len == size) {
  142. con->backlog = 0;
  143. xen_pv_printf(&con->xendev, 1, "backlog is gone\n");
  144. }
  145. }
  146. }
  147. /* -------------------------------------------------------------------- */
  148. static int con_init(struct XenLegacyDevice *xendev)
  149. {
  150. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  151. char *type, *dom, label[32];
  152. int ret = 0;
  153. const char *output;
  154. /* setup */
  155. dom = xs_get_domain_path(xenstore, con->xendev.dom);
  156. if (!xendev->dev) {
  157. snprintf(con->console, sizeof(con->console), "%s/console", dom);
  158. } else {
  159. snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
  160. }
  161. free(dom);
  162. type = xenstore_read_str(con->console, "type");
  163. if (!type || strcmp(type, "ioemu") != 0) {
  164. xen_pv_printf(xendev, 1, "not for me (type=%s)\n", type);
  165. ret = -1;
  166. goto out;
  167. }
  168. output = xenstore_read_str(con->console, "output");
  169. /* no Xen override, use qemu output device */
  170. if (output == NULL) {
  171. if (con->xendev.dev) {
  172. qemu_chr_fe_init(&con->chr, serial_hd(con->xendev.dev),
  173. &error_abort);
  174. }
  175. } else {
  176. snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
  177. qemu_chr_fe_init(&con->chr,
  178. /*
  179. * FIXME: sure we want to support implicit
  180. * muxed monitors here?
  181. */
  182. qemu_chr_new_mux_mon(label, output, NULL),
  183. &error_abort);
  184. }
  185. xenstore_store_pv_console_info(con->xendev.dev,
  186. qemu_chr_fe_get_driver(&con->chr));
  187. out:
  188. g_free(type);
  189. return ret;
  190. }
  191. static int con_initialise(struct XenLegacyDevice *xendev)
  192. {
  193. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  194. int limit;
  195. if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
  196. return -1;
  197. if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
  198. return -1;
  199. if (xenstore_read_int(con->console, "limit", &limit) == 0)
  200. con->buffer.max_capacity = limit;
  201. if (!xendev->dev) {
  202. xen_pfn_t mfn = con->ring_ref;
  203. con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
  204. PROT_READ | PROT_WRITE,
  205. 1, &mfn, NULL);
  206. } else {
  207. con->sring = xen_be_map_grant_ref(xendev, con->ring_ref,
  208. PROT_READ | PROT_WRITE);
  209. }
  210. if (!con->sring)
  211. return -1;
  212. xen_be_bind_evtchn(&con->xendev);
  213. qemu_chr_fe_set_handlers(&con->chr, xencons_can_receive,
  214. xencons_receive, NULL, NULL, con, NULL, true);
  215. xen_pv_printf(xendev, 1,
  216. "ring mfn %d, remote port %d, local port %d, limit %zd\n",
  217. con->ring_ref,
  218. con->xendev.remote_port,
  219. con->xendev.local_port,
  220. con->buffer.max_capacity);
  221. return 0;
  222. }
  223. static void con_disconnect(struct XenLegacyDevice *xendev)
  224. {
  225. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  226. qemu_chr_fe_deinit(&con->chr, false);
  227. xen_pv_unbind_evtchn(&con->xendev);
  228. if (con->sring) {
  229. if (!xendev->dev) {
  230. xenforeignmemory_unmap(xen_fmem, con->sring, 1);
  231. } else {
  232. xen_be_unmap_grant_ref(xendev, con->sring);
  233. }
  234. con->sring = NULL;
  235. }
  236. }
  237. static void con_event(struct XenLegacyDevice *xendev)
  238. {
  239. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  240. buffer_append(con);
  241. if (con->buffer.size - con->buffer.consumed)
  242. xencons_send(con);
  243. }
  244. /* -------------------------------------------------------------------- */
  245. struct XenDevOps xen_console_ops = {
  246. .size = sizeof(struct XenConsole),
  247. .flags = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
  248. .init = con_init,
  249. .initialise = con_initialise,
  250. .event = con_event,
  251. .disconnect = con_disconnect,
  252. };