hvc_xen.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 mfn;
  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 mfn = 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. mfn = v;
  196. info->intf = xen_remap(mfn << 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. info->intf = mfn_to_virt(xen_start_info->console.domU.mfn);
  226. info->vtermno = HVC_COOKIE;
  227. spin_lock(&xencons_lock);
  228. list_add_tail(&info->list, &xenconsoles);
  229. spin_unlock(&xencons_lock);
  230. return 0;
  231. }
  232. static int xen_initial_domain_console_init(void)
  233. {
  234. struct xencons_info *info;
  235. if (!xen_initial_domain())
  236. return -ENODEV;
  237. info = vtermno_to_xencons(HVC_COOKIE);
  238. if (!info) {
  239. info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
  240. if (!info)
  241. return -ENOMEM;
  242. }
  243. info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
  244. info->vtermno = HVC_COOKIE;
  245. spin_lock(&xencons_lock);
  246. list_add_tail(&info->list, &xenconsoles);
  247. spin_unlock(&xencons_lock);
  248. return 0;
  249. }
  250. static void xen_console_update_evtchn(struct xencons_info *info)
  251. {
  252. if (xen_hvm_domain()) {
  253. uint64_t v = 0;
  254. int err;
  255. err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
  256. if (!err && v)
  257. info->evtchn = v;
  258. } else
  259. info->evtchn = xen_start_info->console.domU.evtchn;
  260. }
  261. void xen_console_resume(void)
  262. {
  263. struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
  264. if (info != NULL && info->irq) {
  265. if (!xen_initial_domain())
  266. xen_console_update_evtchn(info);
  267. rebind_evtchn_irq(info->evtchn, info->irq);
  268. }
  269. }
  270. static void xencons_disconnect_backend(struct xencons_info *info)
  271. {
  272. if (info->irq > 0)
  273. unbind_from_irqhandler(info->irq, NULL);
  274. info->irq = 0;
  275. if (info->evtchn > 0)
  276. xenbus_free_evtchn(info->xbdev, info->evtchn);
  277. info->evtchn = 0;
  278. if (info->gntref > 0)
  279. gnttab_free_grant_references(info->gntref);
  280. info->gntref = 0;
  281. if (info->hvc != NULL)
  282. hvc_remove(info->hvc);
  283. info->hvc = NULL;
  284. }
  285. static void xencons_free(struct xencons_info *info)
  286. {
  287. free_page((unsigned long)info->intf);
  288. info->intf = NULL;
  289. info->vtermno = 0;
  290. kfree(info);
  291. }
  292. static int xen_console_remove(struct xencons_info *info)
  293. {
  294. xencons_disconnect_backend(info);
  295. spin_lock(&xencons_lock);
  296. list_del(&info->list);
  297. spin_unlock(&xencons_lock);
  298. if (info->xbdev != NULL)
  299. xencons_free(info);
  300. else {
  301. if (xen_hvm_domain())
  302. iounmap(info->intf);
  303. kfree(info);
  304. }
  305. return 0;
  306. }
  307. #ifdef CONFIG_HVC_XEN_FRONTEND
  308. static int xencons_remove(struct xenbus_device *dev)
  309. {
  310. return xen_console_remove(dev_get_drvdata(&dev->dev));
  311. }
  312. static int xencons_connect_backend(struct xenbus_device *dev,
  313. struct xencons_info *info)
  314. {
  315. int ret, evtchn, devid, ref, irq;
  316. struct xenbus_transaction xbt;
  317. grant_ref_t gref_head;
  318. unsigned long mfn;
  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. if (xen_pv_domain())
  333. mfn = virt_to_mfn(info->intf);
  334. else
  335. mfn = __pa(info->intf) >> PAGE_SHIFT;
  336. ret = gnttab_alloc_grant_references(1, &gref_head);
  337. if (ret < 0)
  338. return ret;
  339. info->gntref = gref_head;
  340. ref = gnttab_claim_grant_reference(&gref_head);
  341. if (ref < 0)
  342. return ref;
  343. gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
  344. mfn, 0);
  345. again:
  346. ret = xenbus_transaction_start(&xbt);
  347. if (ret) {
  348. xenbus_dev_fatal(dev, ret, "starting transaction");
  349. return ret;
  350. }
  351. ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
  352. if (ret)
  353. goto error_xenbus;
  354. ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
  355. evtchn);
  356. if (ret)
  357. goto error_xenbus;
  358. ret = xenbus_transaction_end(xbt, 0);
  359. if (ret) {
  360. if (ret == -EAGAIN)
  361. goto again;
  362. xenbus_dev_fatal(dev, ret, "completing transaction");
  363. return ret;
  364. }
  365. xenbus_switch_state(dev, XenbusStateInitialised);
  366. return 0;
  367. error_xenbus:
  368. xenbus_transaction_end(xbt, 1);
  369. xenbus_dev_fatal(dev, ret, "writing xenstore");
  370. return ret;
  371. }
  372. static int xencons_probe(struct xenbus_device *dev,
  373. const struct xenbus_device_id *id)
  374. {
  375. int ret, devid;
  376. struct xencons_info *info;
  377. devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
  378. if (devid == 0)
  379. return -ENODEV;
  380. info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
  381. if (!info)
  382. return -ENOMEM;
  383. dev_set_drvdata(&dev->dev, info);
  384. info->xbdev = dev;
  385. info->vtermno = xenbus_devid_to_vtermno(devid);
  386. info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  387. if (!info->intf)
  388. goto error_nomem;
  389. ret = xencons_connect_backend(dev, info);
  390. if (ret < 0)
  391. goto error;
  392. spin_lock(&xencons_lock);
  393. list_add_tail(&info->list, &xenconsoles);
  394. spin_unlock(&xencons_lock);
  395. return 0;
  396. error_nomem:
  397. ret = -ENOMEM;
  398. xenbus_dev_fatal(dev, ret, "allocating device memory");
  399. error:
  400. xencons_disconnect_backend(info);
  401. xencons_free(info);
  402. return ret;
  403. }
  404. static int xencons_resume(struct xenbus_device *dev)
  405. {
  406. struct xencons_info *info = dev_get_drvdata(&dev->dev);
  407. xencons_disconnect_backend(info);
  408. memset(info->intf, 0, PAGE_SIZE);
  409. return xencons_connect_backend(dev, info);
  410. }
  411. static void xencons_backend_changed(struct xenbus_device *dev,
  412. enum xenbus_state backend_state)
  413. {
  414. switch (backend_state) {
  415. case XenbusStateReconfiguring:
  416. case XenbusStateReconfigured:
  417. case XenbusStateInitialising:
  418. case XenbusStateInitialised:
  419. case XenbusStateUnknown:
  420. break;
  421. case XenbusStateInitWait:
  422. break;
  423. case XenbusStateConnected:
  424. xenbus_switch_state(dev, XenbusStateConnected);
  425. break;
  426. case XenbusStateClosed:
  427. if (dev->state == XenbusStateClosed)
  428. break;
  429. /* Missed the backend's CLOSING state -- fallthrough */
  430. case XenbusStateClosing:
  431. xenbus_frontend_closed(dev);
  432. break;
  433. }
  434. }
  435. static const struct xenbus_device_id xencons_ids[] = {
  436. { "console" },
  437. { "" }
  438. };
  439. static struct xenbus_driver xencons_driver = {
  440. .name = "xenconsole",
  441. .ids = xencons_ids,
  442. .probe = xencons_probe,
  443. .remove = xencons_remove,
  444. .resume = xencons_resume,
  445. .otherend_changed = xencons_backend_changed,
  446. };
  447. #endif /* CONFIG_HVC_XEN_FRONTEND */
  448. static int __init xen_hvc_init(void)
  449. {
  450. int r;
  451. struct xencons_info *info;
  452. const struct hv_ops *ops;
  453. if (!xen_domain())
  454. return -ENODEV;
  455. if (xen_initial_domain()) {
  456. ops = &dom0_hvc_ops;
  457. r = xen_initial_domain_console_init();
  458. if (r < 0)
  459. return r;
  460. info = vtermno_to_xencons(HVC_COOKIE);
  461. } else {
  462. ops = &domU_hvc_ops;
  463. if (xen_hvm_domain())
  464. r = xen_hvm_console_init();
  465. else
  466. r = xen_pv_console_init();
  467. if (r < 0)
  468. return r;
  469. info = vtermno_to_xencons(HVC_COOKIE);
  470. info->irq = bind_evtchn_to_irq(info->evtchn);
  471. }
  472. if (info->irq < 0)
  473. info->irq = 0; /* NO_IRQ */
  474. else
  475. irq_set_noprobe(info->irq);
  476. info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
  477. if (IS_ERR(info->hvc)) {
  478. r = PTR_ERR(info->hvc);
  479. spin_lock(&xencons_lock);
  480. list_del(&info->list);
  481. spin_unlock(&xencons_lock);
  482. if (info->irq)
  483. unbind_from_irqhandler(info->irq, NULL);
  484. kfree(info);
  485. return r;
  486. }
  487. r = 0;
  488. #ifdef CONFIG_HVC_XEN_FRONTEND
  489. r = xenbus_register_frontend(&xencons_driver);
  490. #endif
  491. return r;
  492. }
  493. device_initcall(xen_hvc_init);
  494. static int xen_cons_init(void)
  495. {
  496. const struct hv_ops *ops;
  497. if (!xen_domain())
  498. return 0;
  499. if (xen_initial_domain())
  500. ops = &dom0_hvc_ops;
  501. else {
  502. int r;
  503. ops = &domU_hvc_ops;
  504. if (xen_hvm_domain())
  505. r = xen_hvm_console_init();
  506. else
  507. r = xen_pv_console_init();
  508. if (r < 0)
  509. return r;
  510. }
  511. hvc_instantiate(HVC_COOKIE, 0, ops);
  512. return 0;
  513. }
  514. console_initcall(xen_cons_init);
  515. #ifdef CONFIG_EARLY_PRINTK
  516. static void xenboot_write_console(struct console *console, const char *string,
  517. unsigned len)
  518. {
  519. unsigned int linelen, off = 0;
  520. const char *pos;
  521. if (!xen_pv_domain())
  522. return;
  523. dom0_write_console(0, string, len);
  524. if (xen_initial_domain())
  525. return;
  526. domU_write_console(0, "(early) ", 8);
  527. while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
  528. linelen = pos-string+off;
  529. if (off + linelen > len)
  530. break;
  531. domU_write_console(0, string+off, linelen);
  532. domU_write_console(0, "\r\n", 2);
  533. off += linelen + 1;
  534. }
  535. if (off < len)
  536. domU_write_console(0, string+off, len-off);
  537. }
  538. struct console xenboot_console = {
  539. .name = "xenboot",
  540. .write = xenboot_write_console,
  541. .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
  542. .index = -1,
  543. };
  544. #endif /* CONFIG_EARLY_PRINTK */
  545. void xen_raw_console_write(const char *str)
  546. {
  547. ssize_t len = strlen(str);
  548. int rc = 0;
  549. if (xen_domain()) {
  550. rc = dom0_write_console(0, str, len);
  551. #ifdef CONFIG_X86
  552. if (rc == -ENOSYS && xen_hvm_domain())
  553. goto outb_print;
  554. } else if (xen_cpuid_base()) {
  555. int i;
  556. outb_print:
  557. for (i = 0; i < len; i++)
  558. outb(str[i], 0xe9);
  559. #endif
  560. }
  561. }
  562. void xen_raw_printk(const char *fmt, ...)
  563. {
  564. static char buf[512];
  565. va_list ap;
  566. va_start(ap, fmt);
  567. vsnprintf(buf, sizeof(buf), fmt, ap);
  568. va_end(ap);
  569. xen_raw_console_write(buf);
  570. }