xen-tpmfront.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Implementation of the Xen vTPM device frontend
  3. *
  4. * Author: Daniel De Graaf <dgdegra@tycho.nsa.gov>
  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 version 2,
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <xen/xen.h>
  14. #include <xen/events.h>
  15. #include <xen/interface/io/tpmif.h>
  16. #include <xen/grant_table.h>
  17. #include <xen/xenbus.h>
  18. #include <xen/page.h>
  19. #include "tpm.h"
  20. struct tpm_private {
  21. struct tpm_chip *chip;
  22. struct xenbus_device *dev;
  23. struct vtpm_shared_page *shr;
  24. unsigned int evtchn;
  25. int ring_ref;
  26. domid_t backend_id;
  27. };
  28. enum status_bits {
  29. VTPM_STATUS_RUNNING = 0x1,
  30. VTPM_STATUS_IDLE = 0x2,
  31. VTPM_STATUS_RESULT = 0x4,
  32. VTPM_STATUS_CANCELED = 0x8,
  33. };
  34. static u8 vtpm_status(struct tpm_chip *chip)
  35. {
  36. struct tpm_private *priv = TPM_VPRIV(chip);
  37. switch (priv->shr->state) {
  38. case VTPM_STATE_IDLE:
  39. return VTPM_STATUS_IDLE | VTPM_STATUS_CANCELED;
  40. case VTPM_STATE_FINISH:
  41. return VTPM_STATUS_IDLE | VTPM_STATUS_RESULT;
  42. case VTPM_STATE_SUBMIT:
  43. case VTPM_STATE_CANCEL: /* cancel requested, not yet canceled */
  44. return VTPM_STATUS_RUNNING;
  45. default:
  46. return 0;
  47. }
  48. }
  49. static bool vtpm_req_canceled(struct tpm_chip *chip, u8 status)
  50. {
  51. return status & VTPM_STATUS_CANCELED;
  52. }
  53. static void vtpm_cancel(struct tpm_chip *chip)
  54. {
  55. struct tpm_private *priv = TPM_VPRIV(chip);
  56. priv->shr->state = VTPM_STATE_CANCEL;
  57. wmb();
  58. notify_remote_via_evtchn(priv->evtchn);
  59. }
  60. static unsigned int shr_data_offset(struct vtpm_shared_page *shr)
  61. {
  62. return sizeof(*shr) + sizeof(u32) * shr->nr_extra_pages;
  63. }
  64. static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
  65. {
  66. struct tpm_private *priv = TPM_VPRIV(chip);
  67. struct vtpm_shared_page *shr = priv->shr;
  68. unsigned int offset = shr_data_offset(shr);
  69. u32 ordinal;
  70. unsigned long duration;
  71. if (offset > PAGE_SIZE)
  72. return -EINVAL;
  73. if (offset + count > PAGE_SIZE)
  74. return -EINVAL;
  75. /* Wait for completion of any existing command or cancellation */
  76. if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->vendor.timeout_c,
  77. &chip->vendor.read_queue, true) < 0) {
  78. vtpm_cancel(chip);
  79. return -ETIME;
  80. }
  81. memcpy(offset + (u8 *)shr, buf, count);
  82. shr->length = count;
  83. barrier();
  84. shr->state = VTPM_STATE_SUBMIT;
  85. wmb();
  86. notify_remote_via_evtchn(priv->evtchn);
  87. ordinal = be32_to_cpu(((struct tpm_input_header*)buf)->ordinal);
  88. duration = tpm_calc_ordinal_duration(chip, ordinal);
  89. if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
  90. &chip->vendor.read_queue, true) < 0) {
  91. /* got a signal or timeout, try to cancel */
  92. vtpm_cancel(chip);
  93. return -ETIME;
  94. }
  95. return count;
  96. }
  97. static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  98. {
  99. struct tpm_private *priv = TPM_VPRIV(chip);
  100. struct vtpm_shared_page *shr = priv->shr;
  101. unsigned int offset = shr_data_offset(shr);
  102. size_t length = shr->length;
  103. if (shr->state == VTPM_STATE_IDLE)
  104. return -ECANCELED;
  105. /* In theory the wait at the end of _send makes this one unnecessary */
  106. if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->vendor.timeout_c,
  107. &chip->vendor.read_queue, true) < 0) {
  108. vtpm_cancel(chip);
  109. return -ETIME;
  110. }
  111. if (offset > PAGE_SIZE)
  112. return -EIO;
  113. if (offset + length > PAGE_SIZE)
  114. length = PAGE_SIZE - offset;
  115. if (length > count)
  116. length = count;
  117. memcpy(buf, offset + (u8 *)shr, length);
  118. return length;
  119. }
  120. static const struct file_operations vtpm_ops = {
  121. .owner = THIS_MODULE,
  122. .llseek = no_llseek,
  123. .open = tpm_open,
  124. .read = tpm_read,
  125. .write = tpm_write,
  126. .release = tpm_release,
  127. };
  128. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  129. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  130. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  131. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  132. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  133. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
  134. NULL);
  135. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  136. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  137. static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
  138. static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
  139. static struct attribute *vtpm_attrs[] = {
  140. &dev_attr_pubek.attr,
  141. &dev_attr_pcrs.attr,
  142. &dev_attr_enabled.attr,
  143. &dev_attr_active.attr,
  144. &dev_attr_owned.attr,
  145. &dev_attr_temp_deactivated.attr,
  146. &dev_attr_caps.attr,
  147. &dev_attr_cancel.attr,
  148. &dev_attr_durations.attr,
  149. &dev_attr_timeouts.attr,
  150. NULL,
  151. };
  152. static struct attribute_group vtpm_attr_grp = {
  153. .attrs = vtpm_attrs,
  154. };
  155. static const struct tpm_vendor_specific tpm_vtpm = {
  156. .status = vtpm_status,
  157. .recv = vtpm_recv,
  158. .send = vtpm_send,
  159. .cancel = vtpm_cancel,
  160. .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
  161. .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
  162. .req_canceled = vtpm_req_canceled,
  163. .attr_group = &vtpm_attr_grp,
  164. .miscdev = {
  165. .fops = &vtpm_ops,
  166. },
  167. };
  168. static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
  169. {
  170. struct tpm_private *priv = dev_id;
  171. switch (priv->shr->state) {
  172. case VTPM_STATE_IDLE:
  173. case VTPM_STATE_FINISH:
  174. wake_up_interruptible(&priv->chip->vendor.read_queue);
  175. break;
  176. case VTPM_STATE_SUBMIT:
  177. case VTPM_STATE_CANCEL:
  178. default:
  179. break;
  180. }
  181. return IRQ_HANDLED;
  182. }
  183. static int setup_chip(struct device *dev, struct tpm_private *priv)
  184. {
  185. struct tpm_chip *chip;
  186. chip = tpm_register_hardware(dev, &tpm_vtpm);
  187. if (!chip)
  188. return -ENODEV;
  189. init_waitqueue_head(&chip->vendor.read_queue);
  190. priv->chip = chip;
  191. TPM_VPRIV(chip) = priv;
  192. return 0;
  193. }
  194. /* caller must clean up in case of errors */
  195. static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
  196. {
  197. struct xenbus_transaction xbt;
  198. const char *message = NULL;
  199. int rv;
  200. priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  201. if (!priv->shr) {
  202. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  203. return -ENOMEM;
  204. }
  205. rv = xenbus_grant_ring(dev, virt_to_mfn(priv->shr));
  206. if (rv < 0)
  207. return rv;
  208. priv->ring_ref = rv;
  209. rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
  210. if (rv)
  211. return rv;
  212. rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
  213. "tpmif", priv);
  214. if (rv <= 0) {
  215. xenbus_dev_fatal(dev, rv, "allocating TPM irq");
  216. return rv;
  217. }
  218. priv->chip->vendor.irq = rv;
  219. again:
  220. rv = xenbus_transaction_start(&xbt);
  221. if (rv) {
  222. xenbus_dev_fatal(dev, rv, "starting transaction");
  223. return rv;
  224. }
  225. rv = xenbus_printf(xbt, dev->nodename,
  226. "ring-ref", "%u", priv->ring_ref);
  227. if (rv) {
  228. message = "writing ring-ref";
  229. goto abort_transaction;
  230. }
  231. rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  232. priv->evtchn);
  233. if (rv) {
  234. message = "writing event-channel";
  235. goto abort_transaction;
  236. }
  237. rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
  238. if (rv) {
  239. message = "writing feature-protocol-v2";
  240. goto abort_transaction;
  241. }
  242. rv = xenbus_transaction_end(xbt, 0);
  243. if (rv == -EAGAIN)
  244. goto again;
  245. if (rv) {
  246. xenbus_dev_fatal(dev, rv, "completing transaction");
  247. return rv;
  248. }
  249. xenbus_switch_state(dev, XenbusStateInitialised);
  250. return 0;
  251. abort_transaction:
  252. xenbus_transaction_end(xbt, 1);
  253. if (message)
  254. xenbus_dev_error(dev, rv, "%s", message);
  255. return rv;
  256. }
  257. static void ring_free(struct tpm_private *priv)
  258. {
  259. if (!priv)
  260. return;
  261. if (priv->ring_ref)
  262. gnttab_end_foreign_access(priv->ring_ref, 0,
  263. (unsigned long)priv->shr);
  264. else
  265. free_page((unsigned long)priv->shr);
  266. if (priv->chip && priv->chip->vendor.irq)
  267. unbind_from_irqhandler(priv->chip->vendor.irq, priv);
  268. kfree(priv);
  269. }
  270. static int tpmfront_probe(struct xenbus_device *dev,
  271. const struct xenbus_device_id *id)
  272. {
  273. struct tpm_private *priv;
  274. int rv;
  275. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  276. if (!priv) {
  277. xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
  278. return -ENOMEM;
  279. }
  280. rv = setup_chip(&dev->dev, priv);
  281. if (rv) {
  282. kfree(priv);
  283. return rv;
  284. }
  285. rv = setup_ring(dev, priv);
  286. if (rv) {
  287. tpm_remove_hardware(&dev->dev);
  288. ring_free(priv);
  289. return rv;
  290. }
  291. tpm_get_timeouts(priv->chip);
  292. dev_set_drvdata(&dev->dev, priv->chip);
  293. return rv;
  294. }
  295. static int tpmfront_remove(struct xenbus_device *dev)
  296. {
  297. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  298. struct tpm_private *priv = TPM_VPRIV(chip);
  299. tpm_remove_hardware(&dev->dev);
  300. ring_free(priv);
  301. TPM_VPRIV(chip) = NULL;
  302. return 0;
  303. }
  304. static int tpmfront_resume(struct xenbus_device *dev)
  305. {
  306. /* A suspend/resume/migrate will interrupt a vTPM anyway */
  307. tpmfront_remove(dev);
  308. return tpmfront_probe(dev, NULL);
  309. }
  310. static void backend_changed(struct xenbus_device *dev,
  311. enum xenbus_state backend_state)
  312. {
  313. int val;
  314. switch (backend_state) {
  315. case XenbusStateInitialised:
  316. case XenbusStateConnected:
  317. if (dev->state == XenbusStateConnected)
  318. break;
  319. if (xenbus_scanf(XBT_NIL, dev->otherend,
  320. "feature-protocol-v2", "%d", &val) < 0)
  321. val = 0;
  322. if (!val) {
  323. xenbus_dev_fatal(dev, -EINVAL,
  324. "vTPM protocol 2 required");
  325. return;
  326. }
  327. xenbus_switch_state(dev, XenbusStateConnected);
  328. break;
  329. case XenbusStateClosing:
  330. case XenbusStateClosed:
  331. device_unregister(&dev->dev);
  332. xenbus_frontend_closed(dev);
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. static const struct xenbus_device_id tpmfront_ids[] = {
  339. { "vtpm" },
  340. { "" }
  341. };
  342. MODULE_ALIAS("xen:vtpm");
  343. static DEFINE_XENBUS_DRIVER(tpmfront, ,
  344. .probe = tpmfront_probe,
  345. .remove = tpmfront_remove,
  346. .resume = tpmfront_resume,
  347. .otherend_changed = backend_changed,
  348. );
  349. static int __init xen_tpmfront_init(void)
  350. {
  351. if (!xen_domain())
  352. return -ENODEV;
  353. return xenbus_register_frontend(&tpmfront_driver);
  354. }
  355. module_init(xen_tpmfront_init);
  356. static void __exit xen_tpmfront_exit(void)
  357. {
  358. xenbus_unregister_driver(&tpmfront_driver);
  359. }
  360. module_exit(xen_tpmfront_exit);
  361. MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
  362. MODULE_DESCRIPTION("Xen vTPM Driver");
  363. MODULE_LICENSE("GPL");