hvc_xen.c 15 KB

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