2 * Copyright (C) 2015, 2016 IBM Corporation
4 * Author: Stefan Berger <stefanb@us.ibm.com>
6 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 * Device driver for vTPM (vTPM proxy driver)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2 of the
17 #include <linux/types.h>
18 #include <linux/spinlock.h>
19 #include <linux/uaccess.h>
20 #include <linux/wait.h>
21 #include <linux/miscdevice.h>
22 #include <linux/vtpm_proxy.h>
23 #include <linux/file.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/poll.h>
26 #include <linux/compat.h>
30 #define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
33 struct tpm_chip
*chip
;
35 u32 flags
; /* public API flags */
39 struct mutex buf_lock
; /* protect buffer and flags */
41 long state
; /* internal state */
42 #define STATE_OPENED_FLAG BIT(0)
43 #define STATE_WAIT_RESPONSE_FLAG BIT(1) /* waiting for emulator response */
45 size_t req_len
; /* length of queued TPM request */
46 size_t resp_len
; /* length of queued TPM response */
47 u8 buffer
[TPM_BUFSIZE
]; /* request/response buffer */
49 struct work_struct work
; /* task that retrieves TPM timeouts */
52 /* all supported flags */
53 #define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
55 static struct workqueue_struct
*workqueue
;
57 static void vtpm_proxy_delete_device(struct proxy_dev
*proxy_dev
);
60 * Functions related to 'server side'
64 * vtpm_proxy_fops_read - Read TPM commands on 'server side'
67 * Number of bytes read or negative error code
69 static ssize_t
vtpm_proxy_fops_read(struct file
*filp
, char __user
*buf
,
70 size_t count
, loff_t
*off
)
72 struct proxy_dev
*proxy_dev
= filp
->private_data
;
76 sig
= wait_event_interruptible(proxy_dev
->wq
,
77 proxy_dev
->req_len
!= 0 ||
78 !(proxy_dev
->state
& STATE_OPENED_FLAG
));
82 mutex_lock(&proxy_dev
->buf_lock
);
84 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
85 mutex_unlock(&proxy_dev
->buf_lock
);
89 len
= proxy_dev
->req_len
;
92 mutex_unlock(&proxy_dev
->buf_lock
);
93 pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
98 rc
= copy_to_user(buf
, proxy_dev
->buffer
, len
);
99 memset(proxy_dev
->buffer
, 0, len
);
100 proxy_dev
->req_len
= 0;
103 proxy_dev
->state
|= STATE_WAIT_RESPONSE_FLAG
;
105 mutex_unlock(&proxy_dev
->buf_lock
);
114 * vtpm_proxy_fops_write - Write TPM responses on 'server side'
117 * Number of bytes read or negative error value
119 static ssize_t
vtpm_proxy_fops_write(struct file
*filp
, const char __user
*buf
,
120 size_t count
, loff_t
*off
)
122 struct proxy_dev
*proxy_dev
= filp
->private_data
;
124 mutex_lock(&proxy_dev
->buf_lock
);
126 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
127 mutex_unlock(&proxy_dev
->buf_lock
);
131 if (count
> sizeof(proxy_dev
->buffer
) ||
132 !(proxy_dev
->state
& STATE_WAIT_RESPONSE_FLAG
)) {
133 mutex_unlock(&proxy_dev
->buf_lock
);
137 proxy_dev
->state
&= ~STATE_WAIT_RESPONSE_FLAG
;
139 proxy_dev
->req_len
= 0;
141 if (copy_from_user(proxy_dev
->buffer
, buf
, count
)) {
142 mutex_unlock(&proxy_dev
->buf_lock
);
146 proxy_dev
->resp_len
= count
;
148 mutex_unlock(&proxy_dev
->buf_lock
);
150 wake_up_interruptible(&proxy_dev
->wq
);
156 * vtpm_proxy_fops_poll: Poll status on 'server side'
161 static unsigned int vtpm_proxy_fops_poll(struct file
*filp
, poll_table
*wait
)
163 struct proxy_dev
*proxy_dev
= filp
->private_data
;
166 poll_wait(filp
, &proxy_dev
->wq
, wait
);
170 mutex_lock(&proxy_dev
->buf_lock
);
172 if (proxy_dev
->req_len
)
173 ret
|= POLLIN
| POLLRDNORM
;
175 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
))
178 mutex_unlock(&proxy_dev
->buf_lock
);
184 * vtpm_proxy_fops_open - Open vTPM device on 'server side'
186 * Called when setting up the anonymous file descriptor
188 static void vtpm_proxy_fops_open(struct file
*filp
)
190 struct proxy_dev
*proxy_dev
= filp
->private_data
;
192 proxy_dev
->state
|= STATE_OPENED_FLAG
;
196 * vtpm_proxy_fops_undo_open - counter-part to vtpm_fops_open
198 * Call to undo vtpm_proxy_fops_open
200 static void vtpm_proxy_fops_undo_open(struct proxy_dev
*proxy_dev
)
202 mutex_lock(&proxy_dev
->buf_lock
);
204 proxy_dev
->state
&= ~STATE_OPENED_FLAG
;
206 mutex_unlock(&proxy_dev
->buf_lock
);
208 /* no more TPM responses -- wake up anyone waiting for them */
209 wake_up_interruptible(&proxy_dev
->wq
);
213 * vtpm_proxy_fops_release: Close 'server side'
218 static int vtpm_proxy_fops_release(struct inode
*inode
, struct file
*filp
)
220 struct proxy_dev
*proxy_dev
= filp
->private_data
;
222 filp
->private_data
= NULL
;
224 vtpm_proxy_delete_device(proxy_dev
);
229 static const struct file_operations vtpm_proxy_fops
= {
230 .owner
= THIS_MODULE
,
232 .read
= vtpm_proxy_fops_read
,
233 .write
= vtpm_proxy_fops_write
,
234 .poll
= vtpm_proxy_fops_poll
,
235 .release
= vtpm_proxy_fops_release
,
239 * Functions invoked by the core TPM driver to send TPM commands to
240 * 'server side' and receive responses from there.
244 * Called when core TPM driver reads TPM responses from 'server side'
247 * Number of TPM response bytes read, negative error value otherwise
249 static int vtpm_proxy_tpm_op_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
251 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
255 mutex_lock(&proxy_dev
->buf_lock
);
257 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
258 mutex_unlock(&proxy_dev
->buf_lock
);
262 len
= proxy_dev
->resp_len
;
265 "Invalid size in recv: count=%zd, resp_len=%zd\n",
271 memcpy(buf
, proxy_dev
->buffer
, len
);
272 proxy_dev
->resp_len
= 0;
275 mutex_unlock(&proxy_dev
->buf_lock
);
281 * Called when core TPM driver forwards TPM requests to 'server side'.
284 * 0 in case of success, negative error value otherwise.
286 static int vtpm_proxy_tpm_op_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
288 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
291 if (count
> sizeof(proxy_dev
->buffer
)) {
293 "Invalid size in send: count=%zd, buffer size=%zd\n",
294 count
, sizeof(proxy_dev
->buffer
));
298 mutex_lock(&proxy_dev
->buf_lock
);
300 if (!(proxy_dev
->state
& STATE_OPENED_FLAG
)) {
301 mutex_unlock(&proxy_dev
->buf_lock
);
305 proxy_dev
->resp_len
= 0;
307 proxy_dev
->req_len
= count
;
308 memcpy(proxy_dev
->buffer
, buf
, count
);
310 proxy_dev
->state
&= ~STATE_WAIT_RESPONSE_FLAG
;
312 mutex_unlock(&proxy_dev
->buf_lock
);
314 wake_up_interruptible(&proxy_dev
->wq
);
319 static void vtpm_proxy_tpm_op_cancel(struct tpm_chip
*chip
)
324 static u8
vtpm_proxy_tpm_op_status(struct tpm_chip
*chip
)
326 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
328 if (proxy_dev
->resp_len
)
329 return VTPM_PROXY_REQ_COMPLETE_FLAG
;
334 static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
336 struct proxy_dev
*proxy_dev
= dev_get_drvdata(&chip
->dev
);
339 mutex_lock(&proxy_dev
->buf_lock
);
341 ret
= !(proxy_dev
->state
& STATE_OPENED_FLAG
);
343 mutex_unlock(&proxy_dev
->buf_lock
);
348 static const struct tpm_class_ops vtpm_proxy_tpm_ops
= {
349 .flags
= TPM_OPS_AUTO_STARTUP
,
350 .recv
= vtpm_proxy_tpm_op_recv
,
351 .send
= vtpm_proxy_tpm_op_send
,
352 .cancel
= vtpm_proxy_tpm_op_cancel
,
353 .status
= vtpm_proxy_tpm_op_status
,
354 .req_complete_mask
= VTPM_PROXY_REQ_COMPLETE_FLAG
,
355 .req_complete_val
= VTPM_PROXY_REQ_COMPLETE_FLAG
,
356 .req_canceled
= vtpm_proxy_tpm_req_canceled
,
360 * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
361 * retrieval of timeouts and durations.
364 static void vtpm_proxy_work(struct work_struct
*work
)
366 struct proxy_dev
*proxy_dev
= container_of(work
, struct proxy_dev
,
370 rc
= tpm_chip_register(proxy_dev
->chip
);
377 vtpm_proxy_fops_undo_open(proxy_dev
);
381 * vtpm_proxy_work_stop: make sure the work has finished
383 * This function is useful when user space closed the fd
384 * while the driver still determines timeouts.
386 static void vtpm_proxy_work_stop(struct proxy_dev
*proxy_dev
)
388 vtpm_proxy_fops_undo_open(proxy_dev
);
389 flush_work(&proxy_dev
->work
);
393 * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
395 static inline void vtpm_proxy_work_start(struct proxy_dev
*proxy_dev
)
397 queue_work(workqueue
, &proxy_dev
->work
);
401 * Code related to creation and deletion of device pairs
403 static struct proxy_dev
*vtpm_proxy_create_proxy_dev(void)
405 struct proxy_dev
*proxy_dev
;
406 struct tpm_chip
*chip
;
409 proxy_dev
= kzalloc(sizeof(*proxy_dev
), GFP_KERNEL
);
410 if (proxy_dev
== NULL
)
411 return ERR_PTR(-ENOMEM
);
413 init_waitqueue_head(&proxy_dev
->wq
);
414 mutex_init(&proxy_dev
->buf_lock
);
415 INIT_WORK(&proxy_dev
->work
, vtpm_proxy_work
);
417 chip
= tpm_chip_alloc(NULL
, &vtpm_proxy_tpm_ops
);
420 goto err_proxy_dev_free
;
422 dev_set_drvdata(&chip
->dev
, proxy_dev
);
424 proxy_dev
->chip
= chip
;
435 * Undo what has been done in vtpm_create_proxy_dev
437 static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev
*proxy_dev
)
439 put_device(&proxy_dev
->chip
->dev
); /* frees chip */
444 * Create a /dev/tpm%d and 'server side' file descriptor pair
447 * Returns file pointer on success, an error value otherwise
449 static struct file
*vtpm_proxy_create_device(
450 struct vtpm_proxy_new_dev
*vtpm_new_dev
)
452 struct proxy_dev
*proxy_dev
;
456 if (vtpm_new_dev
->flags
& ~VTPM_PROXY_FLAGS_ALL
)
457 return ERR_PTR(-EOPNOTSUPP
);
459 proxy_dev
= vtpm_proxy_create_proxy_dev();
460 if (IS_ERR(proxy_dev
))
461 return ERR_CAST(proxy_dev
);
463 proxy_dev
->flags
= vtpm_new_dev
->flags
;
465 /* setup an anonymous file for the server-side */
466 fd
= get_unused_fd_flags(O_RDWR
);
469 goto err_delete_proxy_dev
;
472 file
= anon_inode_getfile("[vtpms]", &vtpm_proxy_fops
, proxy_dev
,
476 goto err_put_unused_fd
;
479 /* from now on we can unwind with put_unused_fd() + fput() */
480 /* simulate an open() on the server side */
481 vtpm_proxy_fops_open(file
);
483 if (proxy_dev
->flags
& VTPM_PROXY_FLAG_TPM2
)
484 proxy_dev
->chip
->flags
|= TPM_CHIP_FLAG_TPM2
;
486 vtpm_proxy_work_start(proxy_dev
);
488 vtpm_new_dev
->fd
= fd
;
489 vtpm_new_dev
->major
= MAJOR(proxy_dev
->chip
->dev
.devt
);
490 vtpm_new_dev
->minor
= MINOR(proxy_dev
->chip
->dev
.devt
);
491 vtpm_new_dev
->tpm_num
= proxy_dev
->chip
->dev_num
;
498 err_delete_proxy_dev
:
499 vtpm_proxy_delete_proxy_dev(proxy_dev
);
505 * Counter part to vtpm_create_device.
507 static void vtpm_proxy_delete_device(struct proxy_dev
*proxy_dev
)
509 vtpm_proxy_work_stop(proxy_dev
);
512 * A client may hold the 'ops' lock, so let it know that the server
513 * side shuts down before we try to grab the 'ops' lock when
514 * unregistering the chip.
516 vtpm_proxy_fops_undo_open(proxy_dev
);
518 tpm_chip_unregister(proxy_dev
->chip
);
520 vtpm_proxy_delete_proxy_dev(proxy_dev
);
524 * Code related to the control device /dev/vtpmx
528 * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
531 * Returns 0 on success, a negative error code otherwise.
533 static long vtpmx_fops_ioctl(struct file
*f
, unsigned int ioctl
,
536 void __user
*argp
= (void __user
*)arg
;
537 struct vtpm_proxy_new_dev __user
*vtpm_new_dev_p
;
538 struct vtpm_proxy_new_dev vtpm_new_dev
;
542 case VTPM_PROXY_IOC_NEW_DEV
:
543 if (!capable(CAP_SYS_ADMIN
))
545 vtpm_new_dev_p
= argp
;
546 if (copy_from_user(&vtpm_new_dev
, vtpm_new_dev_p
,
547 sizeof(vtpm_new_dev
)))
549 file
= vtpm_proxy_create_device(&vtpm_new_dev
);
551 return PTR_ERR(file
);
552 if (copy_to_user(vtpm_new_dev_p
, &vtpm_new_dev
,
553 sizeof(vtpm_new_dev
))) {
554 put_unused_fd(vtpm_new_dev
.fd
);
559 fd_install(vtpm_new_dev
.fd
, file
);
568 static long vtpmx_fops_compat_ioctl(struct file
*f
, unsigned int ioctl
,
571 return vtpmx_fops_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
575 static const struct file_operations vtpmx_fops
= {
576 .owner
= THIS_MODULE
,
577 .unlocked_ioctl
= vtpmx_fops_ioctl
,
579 .compat_ioctl
= vtpmx_fops_compat_ioctl
,
581 .llseek
= noop_llseek
,
584 static struct miscdevice vtpmx_miscdev
= {
585 .minor
= MISC_DYNAMIC_MINOR
,
590 static int vtpmx_init(void)
592 return misc_register(&vtpmx_miscdev
);
595 static void vtpmx_cleanup(void)
597 misc_deregister(&vtpmx_miscdev
);
600 static int __init
vtpm_module_init(void)
606 pr_err("couldn't create vtpmx device\n");
610 workqueue
= create_workqueue("tpm-vtpm");
612 pr_err("couldn't create workqueue\n");
614 goto err_vtpmx_cleanup
;
625 static void __exit
vtpm_module_exit(void)
627 destroy_workqueue(workqueue
);
631 module_init(vtpm_module_init
);
632 module_exit(vtpm_module_exit
);
634 MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
635 MODULE_DESCRIPTION("vTPM Driver");
636 MODULE_VERSION("0.1");
637 MODULE_LICENSE("GPL");