hvc_xen.c 17 KB

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