xen_console.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <stdlib.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/select.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <termios.h>
  28. #include <stdarg.h>
  29. #include <sys/mman.h>
  30. #include "hw.h"
  31. #include "qemu-char.h"
  32. #include "xen_backend.h"
  33. #include <xen/io/console.h>
  34. struct buffer {
  35. uint8_t *data;
  36. size_t consumed;
  37. size_t size;
  38. size_t capacity;
  39. size_t max_capacity;
  40. };
  41. struct XenConsole {
  42. struct XenDevice xendev; /* must be first */
  43. struct buffer buffer;
  44. char console[XEN_BUFSIZE];
  45. int ring_ref;
  46. void *sring;
  47. CharDriverState *chr;
  48. int backlog;
  49. };
  50. static void buffer_append(struct XenConsole *con)
  51. {
  52. struct buffer *buffer = &con->buffer;
  53. XENCONS_RING_IDX cons, prod, size;
  54. struct xencons_interface *intf = con->sring;
  55. cons = intf->out_cons;
  56. prod = intf->out_prod;
  57. xen_mb();
  58. size = prod - cons;
  59. if ((size == 0) || (size > sizeof(intf->out)))
  60. return;
  61. if ((buffer->capacity - buffer->size) < size) {
  62. buffer->capacity += (size + 1024);
  63. buffer->data = g_realloc(buffer->data, buffer->capacity);
  64. }
  65. while (cons != prod)
  66. buffer->data[buffer->size++] = intf->out[
  67. MASK_XENCONS_IDX(cons++, intf->out)];
  68. xen_mb();
  69. intf->out_cons = cons;
  70. xen_be_send_notify(&con->xendev);
  71. if (buffer->max_capacity &&
  72. buffer->size > buffer->max_capacity) {
  73. /* Discard the middle of the data. */
  74. size_t over = buffer->size - buffer->max_capacity;
  75. uint8_t *maxpos = buffer->data + buffer->max_capacity;
  76. memmove(maxpos - over, maxpos, over);
  77. buffer->data = g_realloc(buffer->data, buffer->max_capacity);
  78. buffer->size = buffer->capacity = buffer->max_capacity;
  79. if (buffer->consumed > buffer->max_capacity - over)
  80. buffer->consumed = buffer->max_capacity - over;
  81. }
  82. }
  83. static void buffer_advance(struct buffer *buffer, size_t len)
  84. {
  85. buffer->consumed += len;
  86. if (buffer->consumed == buffer->size) {
  87. buffer->consumed = 0;
  88. buffer->size = 0;
  89. }
  90. }
  91. static int ring_free_bytes(struct XenConsole *con)
  92. {
  93. struct xencons_interface *intf = con->sring;
  94. XENCONS_RING_IDX cons, prod, space;
  95. cons = intf->in_cons;
  96. prod = intf->in_prod;
  97. xen_mb();
  98. space = prod - cons;
  99. if (space > sizeof(intf->in))
  100. return 0; /* ring is screwed: ignore it */
  101. return (sizeof(intf->in) - space);
  102. }
  103. static int xencons_can_receive(void *opaque)
  104. {
  105. struct XenConsole *con = opaque;
  106. return ring_free_bytes(con);
  107. }
  108. static void xencons_receive(void *opaque, const uint8_t *buf, int len)
  109. {
  110. struct XenConsole *con = opaque;
  111. struct xencons_interface *intf = con->sring;
  112. XENCONS_RING_IDX prod;
  113. int i, max;
  114. max = ring_free_bytes(con);
  115. /* The can_receive() func limits this, but check again anyway */
  116. if (max < len)
  117. len = max;
  118. prod = intf->in_prod;
  119. for (i = 0; i < len; i++) {
  120. intf->in[MASK_XENCONS_IDX(prod++, intf->in)] =
  121. buf[i];
  122. }
  123. xen_wmb();
  124. intf->in_prod = prod;
  125. xen_be_send_notify(&con->xendev);
  126. }
  127. static void xencons_send(struct XenConsole *con)
  128. {
  129. ssize_t len, size;
  130. size = con->buffer.size - con->buffer.consumed;
  131. if (con->chr)
  132. len = qemu_chr_fe_write(con->chr, con->buffer.data + con->buffer.consumed,
  133. size);
  134. else
  135. len = size;
  136. if (len < 1) {
  137. if (!con->backlog) {
  138. con->backlog = 1;
  139. xen_be_printf(&con->xendev, 1, "backlog piling up, nobody listening?\n");
  140. }
  141. } else {
  142. buffer_advance(&con->buffer, len);
  143. if (con->backlog && len == size) {
  144. con->backlog = 0;
  145. xen_be_printf(&con->xendev, 1, "backlog is gone\n");
  146. }
  147. }
  148. }
  149. /* -------------------------------------------------------------------- */
  150. static int con_init(struct XenDevice *xendev)
  151. {
  152. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  153. char *type, *dom, label[32];
  154. int ret = 0;
  155. const char *output;
  156. /* setup */
  157. dom = xs_get_domain_path(xenstore, con->xendev.dom);
  158. snprintf(con->console, sizeof(con->console), "%s/console", dom);
  159. free(dom);
  160. type = xenstore_read_str(con->console, "type");
  161. if (!type || strcmp(type, "ioemu") != 0) {
  162. xen_be_printf(xendev, 1, "not for me (type=%s)\n", type);
  163. ret = -1;
  164. goto out;
  165. }
  166. output = xenstore_read_str(con->console, "output");
  167. /* no Xen override, use qemu output device */
  168. if (output == NULL) {
  169. con->chr = serial_hds[con->xendev.dev];
  170. } else {
  171. snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
  172. con->chr = qemu_chr_new(label, output, NULL);
  173. }
  174. xenstore_store_pv_console_info(con->xendev.dev, con->chr);
  175. out:
  176. g_free(type);
  177. return ret;
  178. }
  179. static int con_initialise(struct XenDevice *xendev)
  180. {
  181. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  182. int limit;
  183. if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
  184. return -1;
  185. if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
  186. return -1;
  187. if (xenstore_read_int(con->console, "limit", &limit) == 0)
  188. con->buffer.max_capacity = limit;
  189. con->sring = xc_map_foreign_range(xen_xc, con->xendev.dom,
  190. XC_PAGE_SIZE,
  191. PROT_READ|PROT_WRITE,
  192. con->ring_ref);
  193. if (!con->sring)
  194. return -1;
  195. xen_be_bind_evtchn(&con->xendev);
  196. if (con->chr)
  197. qemu_chr_add_handlers(con->chr, xencons_can_receive, xencons_receive,
  198. NULL, con);
  199. xen_be_printf(xendev, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
  200. con->ring_ref,
  201. con->xendev.remote_port,
  202. con->xendev.local_port,
  203. con->buffer.max_capacity);
  204. return 0;
  205. }
  206. static void con_disconnect(struct XenDevice *xendev)
  207. {
  208. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  209. if (!xendev->dev) {
  210. return;
  211. }
  212. if (con->chr)
  213. qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL);
  214. xen_be_unbind_evtchn(&con->xendev);
  215. if (con->sring) {
  216. munmap(con->sring, XC_PAGE_SIZE);
  217. con->sring = NULL;
  218. }
  219. }
  220. static void con_event(struct XenDevice *xendev)
  221. {
  222. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  223. buffer_append(con);
  224. if (con->buffer.size - con->buffer.consumed)
  225. xencons_send(con);
  226. }
  227. /* -------------------------------------------------------------------- */
  228. struct XenDevOps xen_console_ops = {
  229. .size = sizeof(struct XenConsole),
  230. .flags = DEVOPS_FLAG_IGNORE_STATE,
  231. .init = con_init,
  232. .initialise = con_initialise,
  233. .event = con_event,
  234. .disconnect = con_disconnect,
  235. };