xref: /linux/drivers/tty/hvc/hvc_xen.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * xen console driver interface to hvc_console.c
4  *
5  * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
6  */
7 
8 #include <linux/console.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/irq.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/list.h>
15 #include <linux/serial_core.h>
16 
17 #include <asm/io.h>
18 #include <asm/xen/hypervisor.h>
19 
20 #include <xen/xen.h>
21 #include <xen/interface/xen.h>
22 #include <xen/hvm.h>
23 #include <xen/grant_table.h>
24 #include <xen/page.h>
25 #include <xen/events.h>
26 #include <xen/interface/io/console.h>
27 #include <xen/interface/sched.h>
28 #include <xen/hvc-console.h>
29 #include <xen/xenbus.h>
30 
31 #include "hvc_console.h"
32 
33 #define HVC_COOKIE   0x58656e /* "Xen" in hex */
34 
35 struct xencons_info {
36 	struct list_head list;
37 	struct xenbus_device *xbdev;
38 	struct xencons_interface *intf;
39 	unsigned int evtchn;
40 	XENCONS_RING_IDX out_cons;
41 	unsigned int out_cons_same;
42 	struct hvc_struct *hvc;
43 	int irq;
44 	int vtermno;
45 	grant_ref_t gntref;
46 	spinlock_t ring_lock;
47 };
48 
49 static LIST_HEAD(xenconsoles);
50 static DEFINE_SPINLOCK(xencons_lock);
51 
52 /* ------------------------------------------------------------------ */
53 
54 static bool xen_console_io = false;
55 static int __initdata opt_console_io = -1;
56 
57 static int __init parse_xen_console_io(char *arg)
58 {
59 	bool val;
60 	int ret;
61 
62 	ret = kstrtobool(arg, &val);
63 	if (ret == 0)
64 		opt_console_io = (int)val;
65 
66 	return ret;
67 }
68 early_param("xen_console_io", parse_xen_console_io);
69 
70 static struct xencons_info *vtermno_to_xencons(int vtermno)
71 {
72 	struct xencons_info *entry, *ret = NULL;
73 	unsigned long flags;
74 
75 	spin_lock_irqsave(&xencons_lock, flags);
76 	if (list_empty(&xenconsoles)) {
77 		spin_unlock_irqrestore(&xencons_lock, flags);
78 		return NULL;
79 	}
80 
81 	list_for_each_entry(entry, &xenconsoles, list) {
82 		if (entry->vtermno == vtermno) {
83 			ret  = entry;
84 			break;
85 		}
86 	}
87 	spin_unlock_irqrestore(&xencons_lock, flags);
88 
89 	return ret;
90 }
91 
92 static inline int xenbus_devid_to_vtermno(int devid)
93 {
94 	return devid + HVC_COOKIE;
95 }
96 
97 static inline void notify_daemon(struct xencons_info *cons)
98 {
99 	/* Use evtchn: this is called early, before irq is set up. */
100 	notify_remote_via_evtchn(cons->evtchn);
101 }
102 
103 static ssize_t __write_console(struct xencons_info *xencons,
104 			       const u8 *data, size_t len)
105 {
106 	XENCONS_RING_IDX cons, prod;
107 	struct xencons_interface *intf = xencons->intf;
108 	unsigned long flags;
109 	size_t sent = 0;
110 
111 	spin_lock_irqsave(&xencons->ring_lock, flags);
112 	cons = intf->out_cons;
113 	prod = intf->out_prod;
114 	mb();			/* update queue values before going on */
115 
116 	if ((prod - cons) > sizeof(intf->out)) {
117 		spin_unlock_irqrestore(&xencons->ring_lock, flags);
118 		pr_err_once("xencons: Illegal ring page indices");
119 		return -EINVAL;
120 	}
121 
122 	while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
123 		intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
124 
125 	wmb();			/* write ring before updating pointer */
126 	intf->out_prod = prod;
127 	spin_unlock_irqrestore(&xencons->ring_lock, flags);
128 
129 	if (sent)
130 		notify_daemon(xencons);
131 	return sent;
132 }
133 
134 static ssize_t domU_write_console(uint32_t vtermno, const u8 *data, size_t len)
135 {
136 	struct xencons_info *cons = vtermno_to_xencons(vtermno);
137 	size_t ret = len;
138 
139 	if (cons == NULL)
140 		return -EINVAL;
141 
142 	/*
143 	 * Make sure the whole buffer is emitted, polling if
144 	 * necessary.  We don't ever want to rely on the hvc daemon
145 	 * because the most interesting console output is when the
146 	 * kernel is crippled.
147 	 */
148 	while (len) {
149 		ssize_t sent = __write_console(cons, data, len);
150 
151 		if (sent < 0)
152 			return sent;
153 
154 		data += sent;
155 		len -= sent;
156 
157 		if (unlikely(len))
158 			HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
159 	}
160 
161 	return ret;
162 }
163 
164 static ssize_t domU_read_console(uint32_t vtermno, u8 *buf, size_t len)
165 {
166 	struct xencons_interface *intf;
167 	XENCONS_RING_IDX cons, prod;
168 	struct xencons_info *xencons = vtermno_to_xencons(vtermno);
169 	unsigned int eoiflag = 0;
170 	unsigned long flags;
171 	size_t recv = 0;
172 
173 	if (xencons == NULL)
174 		return -EINVAL;
175 	intf = xencons->intf;
176 
177 	spin_lock_irqsave(&xencons->ring_lock, flags);
178 	cons = intf->in_cons;
179 	prod = intf->in_prod;
180 	mb();			/* get pointers before reading ring */
181 
182 	if ((prod - cons) > sizeof(intf->in)) {
183 		spin_unlock_irqrestore(&xencons->ring_lock, flags);
184 		pr_err_once("xencons: Illegal ring page indices");
185 		return -EINVAL;
186 	}
187 
188 	while (cons != prod && recv < len)
189 		buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
190 
191 	mb();			/* read ring before consuming */
192 	intf->in_cons = cons;
193 
194 	/*
195 	 * When to mark interrupt having been spurious:
196 	 * - there was no new data to be read, and
197 	 * - the backend did not consume some output bytes, and
198 	 * - the previous round with no read data didn't see consumed bytes
199 	 *   (we might have a race with an interrupt being in flight while
200 	 *   updating xencons->out_cons, so account for that by allowing one
201 	 *   round without any visible reason)
202 	 */
203 	if (intf->out_cons != xencons->out_cons) {
204 		xencons->out_cons = intf->out_cons;
205 		xencons->out_cons_same = 0;
206 	}
207 	if (!recv && xencons->out_cons_same++ > 1) {
208 		eoiflag = XEN_EOI_FLAG_SPURIOUS;
209 	}
210 	spin_unlock_irqrestore(&xencons->ring_lock, flags);
211 
212 	if (recv) {
213 		notify_daemon(xencons);
214 	}
215 
216 	xen_irq_lateeoi(xencons->irq, eoiflag);
217 
218 	return recv;
219 }
220 
221 static const struct hv_ops domU_hvc_ops = {
222 	.get_chars = domU_read_console,
223 	.put_chars = domU_write_console,
224 	.notifier_add = notifier_add_irq,
225 	.notifier_del = notifier_del_irq,
226 	.notifier_hangup = notifier_hangup_irq,
227 };
228 
229 static ssize_t dom0_read_console(uint32_t vtermno, u8 *buf, size_t len)
230 {
231 	return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
232 }
233 
234 /*
235  * Either for a dom0 to write to the system console, or a domU with a
236  * debug version of Xen
237  */
238 static ssize_t dom0_write_console(uint32_t vtermno, const u8 *str, size_t len)
239 {
240 	int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (u8 *)str);
241 	if (rc < 0)
242 		return rc;
243 
244 	return len;
245 }
246 
247 static const struct hv_ops dom0_hvc_ops = {
248 	.get_chars = dom0_read_console,
249 	.put_chars = dom0_write_console,
250 	.notifier_add = notifier_add_irq,
251 	.notifier_del = notifier_del_irq,
252 	.notifier_hangup = notifier_hangup_irq,
253 };
254 
255 static int xen_hvm_console_init(void)
256 {
257 	int r;
258 	uint64_t v = 0;
259 	unsigned long gfn, flags;
260 	struct xencons_info *info;
261 
262 	if (!xen_hvm_domain())
263 		return -ENODEV;
264 
265 	info = vtermno_to_xencons(HVC_COOKIE);
266 	if (!info) {
267 		info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
268 		if (!info)
269 			return -ENOMEM;
270 		spin_lock_init(&info->ring_lock);
271 	} else if (info->intf != NULL) {
272 		/* already configured */
273 		return 0;
274 	}
275 	/*
276 	 * If the toolstack (or the hypervisor) hasn't set these values, the
277 	 * default value is 0. Even though gfn = 0 and evtchn = 0 are
278 	 * theoretically correct values, in practice they never are and they
279 	 * mean that a legacy toolstack hasn't initialized the pv console correctly.
280 	 */
281 	r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
282 	if (r < 0 || v == 0)
283 		goto err;
284 	info->evtchn = v;
285 	v = 0;
286 	r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
287 	if (r < 0 || v == 0)
288 		goto err;
289 	gfn = v;
290 	info->intf = memremap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE, MEMREMAP_WB);
291 	if (info->intf == NULL)
292 		goto err;
293 	info->vtermno = HVC_COOKIE;
294 
295 	spin_lock_irqsave(&xencons_lock, flags);
296 	list_add_tail(&info->list, &xenconsoles);
297 	spin_unlock_irqrestore(&xencons_lock, flags);
298 
299 	return 0;
300 err:
301 	kfree(info);
302 	return -ENODEV;
303 }
304 
305 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
306 {
307 	spin_lock_init(&info->ring_lock);
308 	info->evtchn = xen_start_info->console.domU.evtchn;
309 	/* GFN == MFN for PV guest */
310 	info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
311 	info->vtermno = vtermno;
312 
313 	list_add_tail(&info->list, &xenconsoles);
314 
315 	return 0;
316 }
317 
318 static int xen_pv_console_init(void)
319 {
320 	struct xencons_info *info;
321 	unsigned long flags;
322 
323 	if (!xen_pv_domain())
324 		return -ENODEV;
325 
326 	if (!xen_start_info->console.domU.evtchn)
327 		return -ENODEV;
328 
329 	info = vtermno_to_xencons(HVC_COOKIE);
330 	if (!info) {
331 		info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
332 		if (!info)
333 			return -ENOMEM;
334 	} else if (info->intf != NULL) {
335 		/* already configured */
336 		return 0;
337 	}
338 	spin_lock_irqsave(&xencons_lock, flags);
339 	xencons_info_pv_init(info, HVC_COOKIE);
340 	spin_unlock_irqrestore(&xencons_lock, flags);
341 
342 	return 0;
343 }
344 
345 static int xen_initial_domain_console_init(void)
346 {
347 	struct xencons_info *info;
348 	unsigned long flags;
349 
350 	if (!xen_console_io)
351 		return -ENODEV;
352 
353 	info = vtermno_to_xencons(HVC_COOKIE);
354 	if (!info) {
355 		info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
356 		if (!info)
357 			return -ENOMEM;
358 		spin_lock_init(&info->ring_lock);
359 	}
360 
361 	info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
362 	info->vtermno = HVC_COOKIE;
363 
364 	spin_lock_irqsave(&xencons_lock, flags);
365 	list_add_tail(&info->list, &xenconsoles);
366 	spin_unlock_irqrestore(&xencons_lock, flags);
367 
368 	return 0;
369 }
370 
371 static void xen_console_update_evtchn(struct xencons_info *info)
372 {
373 	if (xen_hvm_domain()) {
374 		uint64_t v = 0;
375 		int err;
376 
377 		err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
378 		if (!err && v)
379 			info->evtchn = v;
380 	} else
381 		info->evtchn = xen_start_info->console.domU.evtchn;
382 }
383 
384 void xen_console_resume(void)
385 {
386 	struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
387 	if (info != NULL && info->irq) {
388 		if (!xen_console_io)
389 			xen_console_update_evtchn(info);
390 		rebind_evtchn_irq(info->evtchn, info->irq);
391 	}
392 }
393 
394 #ifdef CONFIG_HVC_XEN_FRONTEND
395 static void xencons_disconnect_backend(struct xencons_info *info)
396 {
397 	if (info->hvc != NULL)
398 		hvc_remove(info->hvc);
399 	info->hvc = NULL;
400 	if (info->irq > 0) {
401 		evtchn_put(info->evtchn);
402 		info->irq = 0;
403 		info->evtchn = 0;
404 	}
405 	/* evtchn_put() will also close it so this is only an error path */
406 	if (info->evtchn > 0)
407 		xenbus_free_evtchn(info->xbdev, info->evtchn);
408 	info->evtchn = 0;
409 	if (info->gntref > 0)
410 		gnttab_free_grant_references(info->gntref);
411 	info->gntref = 0;
412 }
413 
414 static void xencons_free(struct xencons_info *info)
415 {
416 	free_page((unsigned long)info->intf);
417 	info->intf = NULL;
418 	info->vtermno = 0;
419 	kfree(info);
420 }
421 
422 static int xen_console_remove(struct xencons_info *info)
423 {
424 	unsigned long flags;
425 
426 	xencons_disconnect_backend(info);
427 	spin_lock_irqsave(&xencons_lock, flags);
428 	list_del(&info->list);
429 	spin_unlock_irqrestore(&xencons_lock, flags);
430 	if (info->xbdev != NULL)
431 		xencons_free(info);
432 	else {
433 		if (xen_hvm_domain())
434 			iounmap(info->intf);
435 		kfree(info);
436 	}
437 	return 0;
438 }
439 
440 static void xencons_remove(struct xenbus_device *dev)
441 {
442 	xen_console_remove(dev_get_drvdata(&dev->dev));
443 }
444 
445 static int xencons_connect_backend(struct xenbus_device *dev,
446 				  struct xencons_info *info)
447 {
448 	int ret, evtchn, devid, ref, irq;
449 	struct xenbus_transaction xbt;
450 	grant_ref_t gref_head;
451 
452 	ret = xenbus_alloc_evtchn(dev, &evtchn);
453 	if (ret)
454 		return ret;
455 	info->evtchn = evtchn;
456 	irq = bind_evtchn_to_irq_lateeoi(evtchn);
457 	if (irq < 0)
458 		return irq;
459 	info->irq = irq;
460 	devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
461 	info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
462 			irq, &domU_hvc_ops, 256);
463 	if (IS_ERR(info->hvc))
464 		return PTR_ERR(info->hvc);
465 	ret = gnttab_alloc_grant_references(1, &gref_head);
466 	if (ret < 0)
467 		return ret;
468 	info->gntref = gref_head;
469 	ref = gnttab_claim_grant_reference(&gref_head);
470 	if (ref < 0)
471 		return ref;
472 	gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
473 					virt_to_gfn(info->intf), 0);
474 
475  again:
476 	ret = xenbus_transaction_start(&xbt);
477 	if (ret) {
478 		xenbus_dev_fatal(dev, ret, "starting transaction");
479 		return ret;
480 	}
481 	ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
482 	if (ret)
483 		goto error_xenbus;
484 	ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
485 			    evtchn);
486 	if (ret)
487 		goto error_xenbus;
488 	ret = xenbus_transaction_end(xbt, 0);
489 	if (ret) {
490 		if (ret == -EAGAIN)
491 			goto again;
492 		xenbus_dev_fatal(dev, ret, "completing transaction");
493 		return ret;
494 	}
495 
496 	xenbus_switch_state(dev, XenbusStateInitialised);
497 	return 0;
498 
499  error_xenbus:
500 	xenbus_transaction_end(xbt, 1);
501 	xenbus_dev_fatal(dev, ret, "writing xenstore");
502 	return ret;
503 }
504 
505 static int xencons_probe(struct xenbus_device *dev,
506 				  const struct xenbus_device_id *id)
507 {
508 	int ret, devid;
509 	struct xencons_info *info;
510 	unsigned long flags;
511 
512 	devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
513 	if (devid == 0)
514 		return -ENODEV;
515 
516 	info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
517 	if (!info)
518 		return -ENOMEM;
519 	spin_lock_init(&info->ring_lock);
520 	dev_set_drvdata(&dev->dev, info);
521 	info->xbdev = dev;
522 	info->vtermno = xenbus_devid_to_vtermno(devid);
523 	info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
524 	if (!info->intf)
525 		goto error_nomem;
526 
527 	ret = xencons_connect_backend(dev, info);
528 	if (ret < 0)
529 		goto error;
530 	spin_lock_irqsave(&xencons_lock, flags);
531 	list_add_tail(&info->list, &xenconsoles);
532 	spin_unlock_irqrestore(&xencons_lock, flags);
533 
534 	return 0;
535 
536  error_nomem:
537 	ret = -ENOMEM;
538 	xenbus_dev_fatal(dev, ret, "allocating device memory");
539  error:
540 	xencons_disconnect_backend(info);
541 	xencons_free(info);
542 	return ret;
543 }
544 
545 static int xencons_resume(struct xenbus_device *dev)
546 {
547 	struct xencons_info *info = dev_get_drvdata(&dev->dev);
548 
549 	xencons_disconnect_backend(info);
550 	memset(info->intf, 0, XEN_PAGE_SIZE);
551 	return xencons_connect_backend(dev, info);
552 }
553 
554 static void xencons_backend_changed(struct xenbus_device *dev,
555 				   enum xenbus_state backend_state)
556 {
557 	switch (backend_state) {
558 	case XenbusStateReconfiguring:
559 	case XenbusStateReconfigured:
560 	case XenbusStateInitialising:
561 	case XenbusStateInitialised:
562 	case XenbusStateUnknown:
563 		break;
564 
565 	case XenbusStateInitWait:
566 		break;
567 
568 	case XenbusStateConnected:
569 		xenbus_switch_state(dev, XenbusStateConnected);
570 		break;
571 
572 	case XenbusStateClosed:
573 		if (dev->state == XenbusStateClosed)
574 			break;
575 		fallthrough;	/* Missed the backend's CLOSING state */
576 	case XenbusStateClosing: {
577 		struct xencons_info *info = dev_get_drvdata(&dev->dev);
578 
579 		/*
580 		 * Don't tear down the evtchn and grant ref before the other
581 		 * end has disconnected, but do stop userspace from trying
582 		 * to use the device before we allow the backend to close.
583 		 */
584 		if (info->hvc) {
585 			hvc_remove(info->hvc);
586 			info->hvc = NULL;
587 		}
588 
589 		xenbus_frontend_closed(dev);
590 		break;
591 	}
592 	}
593 }
594 
595 static const struct xenbus_device_id xencons_ids[] = {
596 	{ "console" },
597 	{ "" }
598 };
599 
600 static struct xenbus_driver xencons_driver = {
601 	.name = "xenconsole",
602 	.ids = xencons_ids,
603 	.probe = xencons_probe,
604 	.remove = xencons_remove,
605 	.resume = xencons_resume,
606 	.otherend_changed = xencons_backend_changed,
607 	.not_essential = true,
608 };
609 #endif /* CONFIG_HVC_XEN_FRONTEND */
610 
611 static int __init xen_hvc_init(void)
612 {
613 	int r;
614 	struct xencons_info *info;
615 	const struct hv_ops *ops;
616 
617 	if (!xen_domain())
618 		return -ENODEV;
619 
620 	if (xen_console_io) {
621 		ops = &dom0_hvc_ops;
622 		r = xen_initial_domain_console_init();
623 		if (r < 0)
624 			goto register_fe;
625 		info = vtermno_to_xencons(HVC_COOKIE);
626 	} else {
627 		ops = &domU_hvc_ops;
628 		if (xen_hvm_domain())
629 			r = xen_hvm_console_init();
630 		else
631 			r = xen_pv_console_init();
632 		if (r < 0)
633 			goto register_fe;
634 
635 		info = vtermno_to_xencons(HVC_COOKIE);
636 		info->irq = bind_evtchn_to_irq_lateeoi(info->evtchn);
637 	}
638 	if (info->irq < 0)
639 		info->irq = 0; /* NO_IRQ */
640 	else
641 		irq_set_noprobe(info->irq);
642 
643 	info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
644 	if (IS_ERR(info->hvc)) {
645 		unsigned long flags;
646 
647 		r = PTR_ERR(info->hvc);
648 		spin_lock_irqsave(&xencons_lock, flags);
649 		list_del(&info->list);
650 		spin_unlock_irqrestore(&xencons_lock, flags);
651 		if (info->irq)
652 			evtchn_put(info->evtchn);
653 		kfree(info);
654 		return r;
655 	}
656 
657 	r = 0;
658  register_fe:
659 #ifdef CONFIG_HVC_XEN_FRONTEND
660 	r = xenbus_register_frontend(&xencons_driver);
661 #endif
662 	return r;
663 }
664 device_initcall(xen_hvc_init);
665 
666 static int __init xen_cons_init(void)
667 {
668 	const struct hv_ops *ops;
669 
670 	xen_console_io = opt_console_io >= 0 ? opt_console_io :
671 					       xen_initial_domain();
672 
673 	if (!xen_domain())
674 		return 0;
675 
676 	if (xen_console_io)
677 		ops = &dom0_hvc_ops;
678 	else {
679 		int r;
680 		ops = &domU_hvc_ops;
681 
682 		if (xen_hvm_domain())
683 			r = xen_hvm_console_init();
684 		else
685 			r = xen_pv_console_init();
686 		if (r < 0)
687 			return r;
688 	}
689 
690 	hvc_instantiate(HVC_COOKIE, 0, ops);
691 	return 0;
692 }
693 console_initcall(xen_cons_init);
694 
695 #ifdef CONFIG_X86
696 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
697 {
698 	if (xen_cpuid_base())
699 		outsb(0xe9, str, len);
700 }
701 #else
702 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
703 #endif
704 
705 #ifdef CONFIG_EARLY_PRINTK
706 static int __init xenboot_console_setup(struct console *console, char *string)
707 {
708 	static struct xencons_info xenboot;
709 
710 	if (xen_initial_domain() || !xen_pv_domain())
711 		return 0;
712 
713 	return xencons_info_pv_init(&xenboot, 0);
714 }
715 
716 static void xenboot_write_console(struct console *console, const char *string,
717 				  unsigned len)
718 {
719 	unsigned int linelen, off = 0;
720 	const char *pos;
721 
722 	if (dom0_write_console(0, string, len) >= 0)
723 		return;
724 
725 	if (!xen_pv_domain()) {
726 		xen_hvm_early_write(0, string, len);
727 		return;
728 	}
729 
730 	if (domU_write_console(0, "(early) ", 8) < 0)
731 		return;
732 	while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
733 		linelen = pos-string+off;
734 		if (off + linelen > len)
735 			break;
736 		domU_write_console(0, string+off, linelen);
737 		domU_write_console(0, "\r\n", 2);
738 		off += linelen + 1;
739 	}
740 	if (off < len)
741 		domU_write_console(0, string+off, len-off);
742 }
743 
744 struct console xenboot_console = {
745 	.name		= "xenboot",
746 	.write		= xenboot_write_console,
747 	.setup		= xenboot_console_setup,
748 	.flags		= CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
749 	.index		= -1,
750 };
751 #endif	/* CONFIG_EARLY_PRINTK */
752 
753 void xen_raw_console_write(const char *str)
754 {
755 	ssize_t len = strlen(str);
756 	int rc = 0;
757 
758 	if (xen_domain()) {
759 		rc = dom0_write_console(0, str, len);
760 		if (rc != -ENOSYS || !xen_hvm_domain())
761 			return;
762 	}
763 	xen_hvm_early_write(0, str, len);
764 }
765 
766 void xen_raw_printk(const char *fmt, ...)
767 {
768 	static char buf[512];
769 	va_list ap;
770 
771 	va_start(ap, fmt);
772 	vsnprintf(buf, sizeof(buf), fmt, ap);
773 	va_end(ap);
774 
775 	xen_raw_console_write(buf);
776 }
777 
778 static void xenboot_earlycon_write(struct console *console,
779 				  const char *string,
780 				  unsigned len)
781 {
782 	dom0_write_console(0, string, len);
783 }
784 
785 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
786 					    const char *opt)
787 {
788 	device->con->write = xenboot_earlycon_write;
789 	return 0;
790 }
791 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);
792