hvc_xen.c 15 KB

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