hvc_xen.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * xen console driver interface to hvc_console.c
  3. *
  4. * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/console.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <asm/xen/hypervisor.h>
  26. #include <xen/xen.h>
  27. #include <xen/page.h>
  28. #include <xen/events.h>
  29. #include <xen/interface/io/console.h>
  30. #include <xen/hvc-console.h>
  31. #include "hvc_console.h"
  32. #define HVC_COOKIE 0x58656e /* "Xen" in hex */
  33. static struct hvc_struct *hvc;
  34. static int xencons_irq;
  35. /* ------------------------------------------------------------------ */
  36. static unsigned long console_pfn = ~0ul;
  37. static inline struct xencons_interface *xencons_interface(void)
  38. {
  39. if (console_pfn == ~0ul)
  40. return mfn_to_virt(xen_start_info->console.domU.mfn);
  41. else
  42. return __va(console_pfn << PAGE_SHIFT);
  43. }
  44. static inline void notify_daemon(void)
  45. {
  46. /* Use evtchn: this is called early, before irq is set up. */
  47. notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
  48. }
  49. static int __write_console(const char *data, int len)
  50. {
  51. struct xencons_interface *intf = xencons_interface();
  52. XENCONS_RING_IDX cons, prod;
  53. int sent = 0;
  54. cons = intf->out_cons;
  55. prod = intf->out_prod;
  56. mb(); /* update queue values before going on */
  57. BUG_ON((prod - cons) > sizeof(intf->out));
  58. while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
  59. intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
  60. wmb(); /* write ring before updating pointer */
  61. intf->out_prod = prod;
  62. if (sent)
  63. notify_daemon();
  64. return sent;
  65. }
  66. static int domU_write_console(uint32_t vtermno, const char *data, int len)
  67. {
  68. int ret = len;
  69. /*
  70. * Make sure the whole buffer is emitted, polling if
  71. * necessary. We don't ever want to rely on the hvc daemon
  72. * because the most interesting console output is when the
  73. * kernel is crippled.
  74. */
  75. while (len) {
  76. int sent = __write_console(data, len);
  77. data += sent;
  78. len -= sent;
  79. if (unlikely(len))
  80. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  81. }
  82. return ret;
  83. }
  84. static int domU_read_console(uint32_t vtermno, char *buf, int len)
  85. {
  86. struct xencons_interface *intf = xencons_interface();
  87. XENCONS_RING_IDX cons, prod;
  88. int recv = 0;
  89. cons = intf->in_cons;
  90. prod = intf->in_prod;
  91. mb(); /* get pointers before reading ring */
  92. BUG_ON((prod - cons) > sizeof(intf->in));
  93. while (cons != prod && recv < len)
  94. buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
  95. mb(); /* read ring before consuming */
  96. intf->in_cons = cons;
  97. notify_daemon();
  98. return recv;
  99. }
  100. static struct hv_ops domU_hvc_ops = {
  101. .get_chars = domU_read_console,
  102. .put_chars = domU_write_console,
  103. .notifier_add = notifier_add_irq,
  104. .notifier_del = notifier_del_irq,
  105. .notifier_hangup = notifier_hangup_irq,
  106. };
  107. static int dom0_read_console(uint32_t vtermno, char *buf, int len)
  108. {
  109. return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
  110. }
  111. /*
  112. * Either for a dom0 to write to the system console, or a domU with a
  113. * debug version of Xen
  114. */
  115. static int dom0_write_console(uint32_t vtermno, const char *str, int len)
  116. {
  117. int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
  118. if (rc < 0)
  119. return 0;
  120. return len;
  121. }
  122. static struct hv_ops dom0_hvc_ops = {
  123. .get_chars = dom0_read_console,
  124. .put_chars = dom0_write_console,
  125. .notifier_add = notifier_add_irq,
  126. .notifier_del = notifier_del_irq,
  127. .notifier_hangup = notifier_hangup_irq,
  128. };
  129. static int __init xen_hvc_init(void)
  130. {
  131. struct hvc_struct *hp;
  132. struct hv_ops *ops;
  133. if (!xen_pv_domain())
  134. return -ENODEV;
  135. if (xen_initial_domain()) {
  136. ops = &dom0_hvc_ops;
  137. xencons_irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
  138. } else {
  139. if (!xen_start_info->console.domU.evtchn)
  140. return -ENODEV;
  141. ops = &domU_hvc_ops;
  142. xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
  143. }
  144. if (xencons_irq < 0)
  145. xencons_irq = 0; /* NO_IRQ */
  146. hp = hvc_alloc(HVC_COOKIE, xencons_irq, ops, 256);
  147. if (IS_ERR(hp))
  148. return PTR_ERR(hp);
  149. hvc = hp;
  150. console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
  151. return 0;
  152. }
  153. void xen_console_resume(void)
  154. {
  155. if (xencons_irq)
  156. rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
  157. }
  158. static void __exit xen_hvc_fini(void)
  159. {
  160. if (hvc)
  161. hvc_remove(hvc);
  162. }
  163. static int xen_cons_init(void)
  164. {
  165. struct hv_ops *ops;
  166. if (!xen_pv_domain())
  167. return 0;
  168. if (xen_initial_domain())
  169. ops = &dom0_hvc_ops;
  170. else
  171. ops = &domU_hvc_ops;
  172. hvc_instantiate(HVC_COOKIE, 0, ops);
  173. return 0;
  174. }
  175. module_init(xen_hvc_init);
  176. module_exit(xen_hvc_fini);
  177. console_initcall(xen_cons_init);
  178. #ifdef CONFIG_EARLY_PRINTK
  179. static void xenboot_write_console(struct console *console, const char *string,
  180. unsigned len)
  181. {
  182. unsigned int linelen, off = 0;
  183. const char *pos;
  184. dom0_write_console(0, string, len);
  185. if (xen_initial_domain())
  186. return;
  187. domU_write_console(0, "(early) ", 8);
  188. while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
  189. linelen = pos-string+off;
  190. if (off + linelen > len)
  191. break;
  192. domU_write_console(0, string+off, linelen);
  193. domU_write_console(0, "\r\n", 2);
  194. off += linelen + 1;
  195. }
  196. if (off < len)
  197. domU_write_console(0, string+off, len-off);
  198. }
  199. struct console xenboot_console = {
  200. .name = "xenboot",
  201. .write = xenboot_write_console,
  202. .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
  203. };
  204. #endif /* CONFIG_EARLY_PRINTK */
  205. void xen_raw_console_write(const char *str)
  206. {
  207. dom0_write_console(0, str, strlen(str));
  208. }
  209. void xen_raw_printk(const char *fmt, ...)
  210. {
  211. static char buf[512];
  212. va_list ap;
  213. va_start(ap, fmt);
  214. vsnprintf(buf, sizeof(buf), fmt, ap);
  215. va_end(ap);
  216. xen_raw_console_write(buf);
  217. }