xen-tpmfront.c 9.7 KB

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