xen-tpmfront.c 9.4 KB

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