hvc_xen.c 15 KB

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