hvc_xen.c 16 KB

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