hvc_xen.c 15 KB

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