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