hvc_xen.c 14 KB

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