2
0

xen_console.c 7.3 KB

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