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 <[email protected]>
  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 count)
  120. {
  121. struct tpm_private *priv = dev_get_drvdata(&chip->dev);
  122. struct vtpm_shared_page *shr = priv->shr;
  123. size_t offset = shr_data_offset(shr);
  124. u32 ordinal;
  125. unsigned long duration;
  126. if (offset > PAGE_SIZE)
  127. return -EINVAL;
  128. if (offset + count > PAGE_SIZE)
  129. return -EINVAL;
  130. /* Wait for completion of any existing command or cancellation */
  131. if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->timeout_c,
  132. &priv->read_queue, true) < 0) {
  133. vtpm_cancel(chip);
  134. return -ETIME;
  135. }
  136. memcpy(offset + (u8 *)shr, buf, count);
  137. shr->length = count;
  138. barrier();
  139. shr->state = VTPM_STATE_SUBMIT;
  140. wmb();
  141. notify_remote_via_evtchn(priv->evtchn);
  142. ordinal = be32_to_cpu(((struct tpm_header *)buf)->ordinal);
  143. duration = tpm_calc_ordinal_duration(chip, ordinal);
  144. if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
  145. &priv->read_queue, true) < 0) {
  146. /* got a signal or timeout, try to cancel */
  147. vtpm_cancel(chip);
  148. return -ETIME;
  149. }
  150. return 0;
  151. }
  152. static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  153. {
  154. struct tpm_private *priv = dev_get_drvdata(&chip->dev);
  155. struct vtpm_shared_page *shr = priv->shr;
  156. size_t offset = shr_data_offset(shr);
  157. size_t length = shr->length;
  158. if (shr->state == VTPM_STATE_IDLE)
  159. return -ECANCELED;
  160. /* In theory the wait at the end of _send makes this one unnecessary */
  161. if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->timeout_c,
  162. &priv->read_queue, true) < 0) {
  163. vtpm_cancel(chip);
  164. return -ETIME;
  165. }
  166. if (offset > PAGE_SIZE)
  167. return -EIO;
  168. if (offset + length > PAGE_SIZE)
  169. length = PAGE_SIZE - offset;
  170. if (length > count)
  171. length = count;
  172. memcpy(buf, offset + (u8 *)shr, length);
  173. return length;
  174. }
  175. static const struct tpm_class_ops tpm_vtpm = {
  176. .status = vtpm_status,
  177. .recv = vtpm_recv,
  178. .send = vtpm_send,
  179. .cancel = vtpm_cancel,
  180. .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
  181. .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
  182. .req_canceled = vtpm_req_canceled,
  183. };
  184. static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
  185. {
  186. struct tpm_private *priv = dev_id;
  187. switch (priv->shr->state) {
  188. case VTPM_STATE_IDLE:
  189. case VTPM_STATE_FINISH:
  190. wake_up_interruptible(&priv->read_queue);
  191. break;
  192. case VTPM_STATE_SUBMIT:
  193. case VTPM_STATE_CANCEL:
  194. default:
  195. break;
  196. }
  197. return IRQ_HANDLED;
  198. }
  199. static int setup_chip(struct device *dev, struct tpm_private *priv)
  200. {
  201. struct tpm_chip *chip;
  202. chip = tpmm_chip_alloc(dev, &tpm_vtpm);
  203. if (IS_ERR(chip))
  204. return PTR_ERR(chip);
  205. init_waitqueue_head(&priv->read_queue);
  206. priv->chip = chip;
  207. dev_set_drvdata(&chip->dev, priv);
  208. return 0;
  209. }
  210. /* caller must clean up in case of errors */
  211. static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
  212. {
  213. struct xenbus_transaction xbt;
  214. const char *message = NULL;
  215. int rv;
  216. rv = xenbus_setup_ring(dev, GFP_KERNEL, (void **)&priv->shr, 1,
  217. &priv->ring_ref);
  218. if (rv < 0)
  219. return rv;
  220. rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
  221. if (rv)
  222. return rv;
  223. rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
  224. "tpmif", priv);
  225. if (rv <= 0) {
  226. xenbus_dev_fatal(dev, rv, "allocating TPM irq");
  227. return rv;
  228. }
  229. priv->irq = rv;
  230. again:
  231. rv = xenbus_transaction_start(&xbt);
  232. if (rv) {
  233. xenbus_dev_fatal(dev, rv, "starting transaction");
  234. return rv;
  235. }
  236. rv = xenbus_printf(xbt, dev->nodename,
  237. "ring-ref", "%u", priv->ring_ref);
  238. if (rv) {
  239. message = "writing ring-ref";
  240. goto abort_transaction;
  241. }
  242. rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  243. priv->evtchn);
  244. if (rv) {
  245. message = "writing event-channel";
  246. goto abort_transaction;
  247. }
  248. rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
  249. if (rv) {
  250. message = "writing feature-protocol-v2";
  251. goto abort_transaction;
  252. }
  253. rv = xenbus_transaction_end(xbt, 0);
  254. if (rv == -EAGAIN)
  255. goto again;
  256. if (rv) {
  257. xenbus_dev_fatal(dev, rv, "completing transaction");
  258. return rv;
  259. }
  260. xenbus_switch_state(dev, XenbusStateInitialised);
  261. return 0;
  262. abort_transaction:
  263. xenbus_transaction_end(xbt, 1);
  264. if (message)
  265. xenbus_dev_error(dev, rv, "%s", message);
  266. return rv;
  267. }
  268. static void ring_free(struct tpm_private *priv)
  269. {
  270. if (!priv)
  271. return;
  272. xenbus_teardown_ring((void **)&priv->shr, 1, &priv->ring_ref);
  273. if (priv->irq)
  274. unbind_from_irqhandler(priv->irq, priv);
  275. kfree(priv);
  276. }
  277. static int tpmfront_probe(struct xenbus_device *dev,
  278. const struct xenbus_device_id *id)
  279. {
  280. struct tpm_private *priv;
  281. int rv;
  282. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  283. if (!priv) {
  284. xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
  285. return -ENOMEM;
  286. }
  287. rv = setup_chip(&dev->dev, priv);
  288. if (rv) {
  289. kfree(priv);
  290. return rv;
  291. }
  292. rv = setup_ring(dev, priv);
  293. if (rv) {
  294. ring_free(priv);
  295. return rv;
  296. }
  297. tpm_get_timeouts(priv->chip);
  298. return tpm_chip_register(priv->chip);
  299. }
  300. static int tpmfront_remove(struct xenbus_device *dev)
  301. {
  302. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  303. struct tpm_private *priv = dev_get_drvdata(&chip->dev);
  304. tpm_chip_unregister(chip);
  305. ring_free(priv);
  306. dev_set_drvdata(&chip->dev, NULL);
  307. return 0;
  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 <[email protected]>");
  367. MODULE_DESCRIPTION("Xen vTPM Driver");
  368. MODULE_LICENSE("GPL");