xen-tpmfront.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. ssize_t tpm_show_locality(struct device *dev, struct device_attribute *attr,
  120. char *buf)
  121. {
  122. struct tpm_chip *chip = dev_get_drvdata(dev);
  123. struct tpm_private *priv = TPM_VPRIV(chip);
  124. u8 locality = priv->shr->locality;
  125. return sprintf(buf, "%d\n", locality);
  126. }
  127. ssize_t tpm_store_locality(struct device *dev, struct device_attribute *attr,
  128. const char *buf, size_t len)
  129. {
  130. struct tpm_chip *chip = dev_get_drvdata(dev);
  131. struct tpm_private *priv = TPM_VPRIV(chip);
  132. u8 val;
  133. int rv = kstrtou8(buf, 0, &val);
  134. if (rv)
  135. return rv;
  136. priv->shr->locality = val;
  137. return len;
  138. }
  139. static const struct file_operations vtpm_ops = {
  140. .owner = THIS_MODULE,
  141. .llseek = no_llseek,
  142. .open = tpm_open,
  143. .read = tpm_read,
  144. .write = tpm_write,
  145. .release = tpm_release,
  146. };
  147. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  148. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  149. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  150. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  151. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  152. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
  153. NULL);
  154. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  155. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  156. static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
  157. static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
  158. static DEVICE_ATTR(locality, S_IRUGO | S_IWUSR, tpm_show_locality,
  159. tpm_store_locality);
  160. static struct attribute *vtpm_attrs[] = {
  161. &dev_attr_pubek.attr,
  162. &dev_attr_pcrs.attr,
  163. &dev_attr_enabled.attr,
  164. &dev_attr_active.attr,
  165. &dev_attr_owned.attr,
  166. &dev_attr_temp_deactivated.attr,
  167. &dev_attr_caps.attr,
  168. &dev_attr_cancel.attr,
  169. &dev_attr_durations.attr,
  170. &dev_attr_timeouts.attr,
  171. &dev_attr_locality.attr,
  172. NULL,
  173. };
  174. static struct attribute_group vtpm_attr_grp = {
  175. .attrs = vtpm_attrs,
  176. };
  177. #define TPM_LONG_TIMEOUT (10 * 60 * HZ)
  178. static const struct tpm_vendor_specific tpm_vtpm = {
  179. .status = vtpm_status,
  180. .recv = vtpm_recv,
  181. .send = vtpm_send,
  182. .cancel = vtpm_cancel,
  183. .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
  184. .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
  185. .req_canceled = vtpm_req_canceled,
  186. .attr_group = &vtpm_attr_grp,
  187. .miscdev = {
  188. .fops = &vtpm_ops,
  189. },
  190. .duration = {
  191. TPM_LONG_TIMEOUT,
  192. TPM_LONG_TIMEOUT,
  193. TPM_LONG_TIMEOUT,
  194. },
  195. };
  196. static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
  197. {
  198. struct tpm_private *priv = dev_id;
  199. switch (priv->shr->state) {
  200. case VTPM_STATE_IDLE:
  201. case VTPM_STATE_FINISH:
  202. wake_up_interruptible(&priv->chip->vendor.read_queue);
  203. break;
  204. case VTPM_STATE_SUBMIT:
  205. case VTPM_STATE_CANCEL:
  206. default:
  207. break;
  208. }
  209. return IRQ_HANDLED;
  210. }
  211. static int setup_chip(struct device *dev, struct tpm_private *priv)
  212. {
  213. struct tpm_chip *chip;
  214. chip = tpm_register_hardware(dev, &tpm_vtpm);
  215. if (!chip)
  216. return -ENODEV;
  217. init_waitqueue_head(&chip->vendor.read_queue);
  218. priv->chip = chip;
  219. TPM_VPRIV(chip) = priv;
  220. return 0;
  221. }
  222. /* caller must clean up in case of errors */
  223. static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
  224. {
  225. struct xenbus_transaction xbt;
  226. const char *message = NULL;
  227. int rv;
  228. priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  229. if (!priv->shr) {
  230. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  231. return -ENOMEM;
  232. }
  233. rv = xenbus_grant_ring(dev, virt_to_mfn(priv->shr));
  234. if (rv < 0)
  235. return rv;
  236. priv->ring_ref = rv;
  237. rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
  238. if (rv)
  239. return rv;
  240. rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
  241. "tpmif", priv);
  242. if (rv <= 0) {
  243. xenbus_dev_fatal(dev, rv, "allocating TPM irq");
  244. return rv;
  245. }
  246. priv->chip->vendor.irq = rv;
  247. again:
  248. rv = xenbus_transaction_start(&xbt);
  249. if (rv) {
  250. xenbus_dev_fatal(dev, rv, "starting transaction");
  251. return rv;
  252. }
  253. rv = xenbus_printf(xbt, dev->nodename,
  254. "ring-ref", "%u", priv->ring_ref);
  255. if (rv) {
  256. message = "writing ring-ref";
  257. goto abort_transaction;
  258. }
  259. rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  260. priv->evtchn);
  261. if (rv) {
  262. message = "writing event-channel";
  263. goto abort_transaction;
  264. }
  265. rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
  266. if (rv) {
  267. message = "writing feature-protocol-v2";
  268. goto abort_transaction;
  269. }
  270. rv = xenbus_transaction_end(xbt, 0);
  271. if (rv == -EAGAIN)
  272. goto again;
  273. if (rv) {
  274. xenbus_dev_fatal(dev, rv, "completing transaction");
  275. return rv;
  276. }
  277. xenbus_switch_state(dev, XenbusStateInitialised);
  278. return 0;
  279. abort_transaction:
  280. xenbus_transaction_end(xbt, 1);
  281. if (message)
  282. xenbus_dev_error(dev, rv, "%s", message);
  283. return rv;
  284. }
  285. static void ring_free(struct tpm_private *priv)
  286. {
  287. if (!priv)
  288. return;
  289. if (priv->ring_ref)
  290. gnttab_end_foreign_access(priv->ring_ref, 0,
  291. (unsigned long)priv->shr);
  292. else
  293. free_page((unsigned long)priv->shr);
  294. if (priv->chip && priv->chip->vendor.irq)
  295. unbind_from_irqhandler(priv->chip->vendor.irq, priv);
  296. kfree(priv);
  297. }
  298. static int tpmfront_probe(struct xenbus_device *dev,
  299. const struct xenbus_device_id *id)
  300. {
  301. struct tpm_private *priv;
  302. int rv;
  303. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  304. if (!priv) {
  305. xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
  306. return -ENOMEM;
  307. }
  308. rv = setup_chip(&dev->dev, priv);
  309. if (rv) {
  310. kfree(priv);
  311. return rv;
  312. }
  313. rv = setup_ring(dev, priv);
  314. if (rv) {
  315. tpm_remove_hardware(&dev->dev);
  316. ring_free(priv);
  317. return rv;
  318. }
  319. tpm_get_timeouts(priv->chip);
  320. dev_set_drvdata(&dev->dev, priv->chip);
  321. return rv;
  322. }
  323. static int tpmfront_remove(struct xenbus_device *dev)
  324. {
  325. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  326. struct tpm_private *priv = TPM_VPRIV(chip);
  327. tpm_remove_hardware(&dev->dev);
  328. ring_free(priv);
  329. TPM_VPRIV(chip) = NULL;
  330. return 0;
  331. }
  332. static int tpmfront_resume(struct xenbus_device *dev)
  333. {
  334. /* A suspend/resume/migrate will interrupt a vTPM anyway */
  335. tpmfront_remove(dev);
  336. return tpmfront_probe(dev, NULL);
  337. }
  338. static void backend_changed(struct xenbus_device *dev,
  339. enum xenbus_state backend_state)
  340. {
  341. int val;
  342. switch (backend_state) {
  343. case XenbusStateInitialised:
  344. case XenbusStateConnected:
  345. if (dev->state == XenbusStateConnected)
  346. break;
  347. if (xenbus_scanf(XBT_NIL, dev->otherend,
  348. "feature-protocol-v2", "%d", &val) < 0)
  349. val = 0;
  350. if (!val) {
  351. xenbus_dev_fatal(dev, -EINVAL,
  352. "vTPM protocol 2 required");
  353. return;
  354. }
  355. xenbus_switch_state(dev, XenbusStateConnected);
  356. break;
  357. case XenbusStateClosing:
  358. case XenbusStateClosed:
  359. device_unregister(&dev->dev);
  360. xenbus_frontend_closed(dev);
  361. break;
  362. default:
  363. break;
  364. }
  365. }
  366. static const struct xenbus_device_id tpmfront_ids[] = {
  367. { "vtpm" },
  368. { "" }
  369. };
  370. MODULE_ALIAS("xen:vtpm");
  371. static DEFINE_XENBUS_DRIVER(tpmfront, ,
  372. .probe = tpmfront_probe,
  373. .remove = tpmfront_remove,
  374. .resume = tpmfront_resume,
  375. .otherend_changed = backend_changed,
  376. );
  377. static int __init xen_tpmfront_init(void)
  378. {
  379. if (!xen_domain())
  380. return -ENODEV;
  381. return xenbus_register_frontend(&tpmfront_driver);
  382. }
  383. module_init(xen_tpmfront_init);
  384. static void __exit xen_tpmfront_exit(void)
  385. {
  386. xenbus_unregister_driver(&tpmfront_driver);
  387. }
  388. module_exit(xen_tpmfront_exit);
  389. MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
  390. MODULE_DESCRIPTION("Xen vTPM Driver");
  391. MODULE_LICENSE("GPL");