hvc_xen.c 15 KB

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