2
0

xen_console.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "char/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. if (!xendev->dev) {
  159. snprintf(con->console, sizeof(con->console), "%s/console", dom);
  160. } else {
  161. snprintf(con->console, sizeof(con->console), "%s/device/console/%d", dom, xendev->dev);
  162. }
  163. free(dom);
  164. type = xenstore_read_str(con->console, "type");
  165. if (!type || strcmp(type, "ioemu") != 0) {
  166. xen_be_printf(xendev, 1, "not for me (type=%s)\n", type);
  167. ret = -1;
  168. goto out;
  169. }
  170. output = xenstore_read_str(con->console, "output");
  171. /* no Xen override, use qemu output device */
  172. if (output == NULL) {
  173. con->chr = serial_hds[con->xendev.dev];
  174. } else {
  175. snprintf(label, sizeof(label), "xencons%d", con->xendev.dev);
  176. con->chr = qemu_chr_new(label, output, NULL);
  177. }
  178. xenstore_store_pv_console_info(con->xendev.dev, con->chr);
  179. out:
  180. g_free(type);
  181. return ret;
  182. }
  183. static int con_initialise(struct XenDevice *xendev)
  184. {
  185. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  186. int limit;
  187. if (xenstore_read_int(con->console, "ring-ref", &con->ring_ref) == -1)
  188. return -1;
  189. if (xenstore_read_int(con->console, "port", &con->xendev.remote_port) == -1)
  190. return -1;
  191. if (xenstore_read_int(con->console, "limit", &limit) == 0)
  192. con->buffer.max_capacity = limit;
  193. if (!xendev->dev) {
  194. con->sring = xc_map_foreign_range(xen_xc, con->xendev.dom,
  195. XC_PAGE_SIZE,
  196. PROT_READ|PROT_WRITE,
  197. con->ring_ref);
  198. } else {
  199. con->sring = xc_gnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
  200. con->ring_ref,
  201. PROT_READ|PROT_WRITE);
  202. }
  203. if (!con->sring)
  204. return -1;
  205. xen_be_bind_evtchn(&con->xendev);
  206. if (con->chr)
  207. qemu_chr_add_handlers(con->chr, xencons_can_receive, xencons_receive,
  208. NULL, con);
  209. xen_be_printf(xendev, 1, "ring mfn %d, remote port %d, local port %d, limit %zd\n",
  210. con->ring_ref,
  211. con->xendev.remote_port,
  212. con->xendev.local_port,
  213. con->buffer.max_capacity);
  214. return 0;
  215. }
  216. static void con_disconnect(struct XenDevice *xendev)
  217. {
  218. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  219. if (!xendev->dev) {
  220. return;
  221. }
  222. if (con->chr)
  223. qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL);
  224. xen_be_unbind_evtchn(&con->xendev);
  225. if (con->sring) {
  226. if (!xendev->gnttabdev) {
  227. munmap(con->sring, XC_PAGE_SIZE);
  228. } else {
  229. xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
  230. }
  231. con->sring = NULL;
  232. }
  233. }
  234. static void con_event(struct XenDevice *xendev)
  235. {
  236. struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
  237. buffer_append(con);
  238. if (con->buffer.size - con->buffer.consumed)
  239. xencons_send(con);
  240. }
  241. /* -------------------------------------------------------------------- */
  242. struct XenDevOps xen_console_ops = {
  243. .size = sizeof(struct XenConsole),
  244. .flags = DEVOPS_FLAG_IGNORE_STATE|DEVOPS_FLAG_NEED_GNTDEV,
  245. .init = con_init,
  246. .initialise = con_initialise,
  247. .event = con_event,
  248. .disconnect = con_disconnect,
  249. };