diff options
author | Patrick Ohly <patrick.ohly@intel.com> | 2017-01-20 08:51:07 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2017-03-01 11:17:44 +0000 |
commit | 81b7a9832f9292ced5a113f0c50e1318301f7358 (patch) | |
tree | c58018580b33e880c999e894cd2542a4cd601016 /meta/recipes-devtools/qemu | |
parent | b16192c93834d0a6530169557aa34122e1417bcf (diff) | |
download | poky-81b7a9832f9292ced5a113f0c50e1318301f7358.tar.gz |
qemu: support virtual TPM
This enables the use of swtpm (from meta-security) as a virtual TPM in
qemu. These patches extend the existing support in qemu for TPM
passthrough so that a swtpm daemon can be accessed via CUSE (character
device in user space).
To use this:
- add the meta-security layer including the swtpm enhancements for qemu
- bitbake swtpm-native
- create a TPM instance and initialize it with:
$ mkdir -p my-machine/myvtpm0
$ tmp-glibc/sysroots/x86_64-linux/usr/bin/swtpm_setup_oe.sh --tpm-state my-machine/myvtpm0 --createek
Starting vTPM manufacturing as root:root @ Fri 20 Jan 2017 08:56:18 AM CET
TPM is listening on TCP port 52167.
Successfully created EK.
Successfully authored TPM state.
Ending vTPM manufacturing @ Fri 20 Jan 2017 08:56:19 AM CET
- run swtpm *before each runqemu invocation* (it shuts down after use) and
do it as root (required to set up the /dev/vtpm0 CUSE device):
$ sudo sh -c 'PATH=`pwd`/tmp-glibc/sysroots/x86_64-linux/usr/bin/:`pwd`/tmp-glibc/sysroots/x86_64-linux/usr/sbin/:$PATH; export TPM_PATH=`pwd`/my-machine/myvtpm0; swtpm_cuse -n vtpm0' && sudo chmod a+rw /dev/vtpm0
- run qemu:
$ runqemu 'qemuparams=-tpmdev cuse-tpm,id=tpm0,path=/dev/vtpm0 -device tpm-tis,tpmdev=tpm0' ...
The guest kernel has to have TPM support enabled, which can be done with:
KERNEL_FEATURES_append = " features/tpm/tpm.scc"
(From OE-Core rev: 1264d26fa251ac11a9069f3e602dec6be9d8b9ba)
Signed-off-by: Patrick Ohly <patrick.ohly@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/recipes-devtools/qemu')
5 files changed, 1761 insertions, 0 deletions
diff --git a/meta/recipes-devtools/qemu/qemu/0001-Provide-support-for-the-CUSE-TPM.patch b/meta/recipes-devtools/qemu/qemu/0001-Provide-support-for-the-CUSE-TPM.patch new file mode 100644 index 0000000000..74dc6f5df8 --- /dev/null +++ b/meta/recipes-devtools/qemu/qemu/0001-Provide-support-for-the-CUSE-TPM.patch | |||
@@ -0,0 +1,870 @@ | |||
1 | From 8737eef18f39ed087fd911d0a0886e8174d0468c Mon Sep 17 00:00:00 2001 | ||
2 | From: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
3 | Date: Sat, 31 Dec 2016 11:23:32 -0500 | ||
4 | Subject: [PATCH 1/4] Provide support for the CUSE TPM | ||
5 | |||
6 | Rather than integrating TPM functionality into QEMU directly | ||
7 | using the TPM emulation of libtpms, we now integrate an external | ||
8 | emulated TPM device. This device is expected to implement a Linux | ||
9 | CUSE interface (CUSE = character device in userspace). | ||
10 | |||
11 | QEMU talks to the CUSE TPM using much functionality of the | ||
12 | passthrough driver. For example, the TPM commands and responses | ||
13 | are sent to the CUSE TPM using the read()/write() interface. | ||
14 | However, some out-of-band control needs to be done using the CUSE | ||
15 | TPM's ioctls. The CUSE TPM currently defines and implements 15 | ||
16 | different ioctls for controlling certain life-cycle aspects of | ||
17 | the emulated TPM. The ioctls can be regarded as a replacement for | ||
18 | direct function calls to a TPM emulator if the TPM were to be | ||
19 | directly integrated into QEMU. | ||
20 | |||
21 | One of the ioctls allows to get a bitmask of supported capabilities. | ||
22 | Each returned bit indicates which capabilities have been implemented. | ||
23 | An include file defining the various ioctls is added to QEMU. | ||
24 | |||
25 | The CUSE TPM and associated tools can be found here: | ||
26 | |||
27 | https://github.com/stefanberger/swtpm | ||
28 | |||
29 | (please use the latest version) | ||
30 | |||
31 | To use the external CUSE TPM, the CUSE TPM should be started as follows: | ||
32 | |||
33 | /usr/bin/swtpm_ioctl -s /dev/vtpm-test | ||
34 | |||
35 | /usr/bin/swtpm_cuse -n vtpm-test | ||
36 | |||
37 | QEMU can then be started using the following parameters: | ||
38 | |||
39 | qemu-system-x86_64 \ | ||
40 | [...] \ | ||
41 | -tpmdev cuse-tpm,id=tpm0,cancel-path=/dev/null,path=/dev/vtpm-test \ | ||
42 | -device tpm-tis,id=tpm0,tpmdev=tpm0 \ | ||
43 | [...] | ||
44 | |||
45 | Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
46 | Cc: Eric Blake <eblake@redhat.com> | ||
47 | |||
48 | Conflicts: | ||
49 | docs/qmp-commands.txt | ||
50 | |||
51 | Patch cherry-picked from https://github.com/stefanberger/qemu-tpm, branch v2.8.0+tpm, | ||
52 | commit 27d6cd856d5a14061955df7a93ee490697a7a174. Applied cleanly except for | ||
53 | docs/qmp-commands.txt which did not exist yet in qemu 2.7. | ||
54 | |||
55 | Upstream-Status: Pending [https://lists.nongnu.org/archive/html/qemu-devel/2016-06/msg00252.html] | ||
56 | Signed-off-by: Patrick Ohly <patrick.ohly@intel.com> | ||
57 | --- | ||
58 | hmp.c | 6 ++ | ||
59 | hw/tpm/tpm_int.h | 1 + | ||
60 | hw/tpm/tpm_ioctl.h | 215 +++++++++++++++++++++++++++++++++++++ | ||
61 | hw/tpm/tpm_passthrough.c | 274 +++++++++++++++++++++++++++++++++++++++++++++-- | ||
62 | qapi-schema.json | 18 +++- | ||
63 | qemu-options.hx | 21 +++- | ||
64 | tpm.c | 11 +- | ||
65 | 7 files changed, 529 insertions(+), 17 deletions(-) | ||
66 | create mode 100644 hw/tpm/tpm_ioctl.h | ||
67 | |||
68 | diff --git a/hmp.c b/hmp.c | ||
69 | index cc2056e9e2..277b45ef5a 100644 | ||
70 | --- a/hmp.c | ||
71 | +++ b/hmp.c | ||
72 | @@ -883,6 +883,12 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict) | ||
73 | tpo->has_cancel_path ? ",cancel-path=" : "", | ||
74 | tpo->has_cancel_path ? tpo->cancel_path : ""); | ||
75 | break; | ||
76 | + case TPM_TYPE_OPTIONS_KIND_CUSE_TPM: | ||
77 | + tpo = ti->options->u.passthrough.data; | ||
78 | + monitor_printf(mon, "%s%s", | ||
79 | + tpo->has_path ? ",path=" : "", | ||
80 | + tpo->has_path ? tpo->path : ""); | ||
81 | + break; | ||
82 | case TPM_TYPE_OPTIONS_KIND__MAX: | ||
83 | break; | ||
84 | } | ||
85 | diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h | ||
86 | index f2f285b3cc..6b2c9c953a 100644 | ||
87 | --- a/hw/tpm/tpm_int.h | ||
88 | +++ b/hw/tpm/tpm_int.h | ||
89 | @@ -61,6 +61,7 @@ struct tpm_resp_hdr { | ||
90 | #define TPM_TAG_RSP_AUTH1_COMMAND 0xc5 | ||
91 | #define TPM_TAG_RSP_AUTH2_COMMAND 0xc6 | ||
92 | |||
93 | +#define TPM_SUCCESS 0 | ||
94 | #define TPM_FAIL 9 | ||
95 | |||
96 | #define TPM_ORD_ContinueSelfTest 0x53 | ||
97 | diff --git a/hw/tpm/tpm_ioctl.h b/hw/tpm/tpm_ioctl.h | ||
98 | new file mode 100644 | ||
99 | index 0000000000..a341e15741 | ||
100 | --- /dev/null | ||
101 | +++ b/hw/tpm/tpm_ioctl.h | ||
102 | @@ -0,0 +1,215 @@ | ||
103 | +/* | ||
104 | + * tpm_ioctl.h | ||
105 | + * | ||
106 | + * (c) Copyright IBM Corporation 2014, 2015. | ||
107 | + * | ||
108 | + * This file is licensed under the terms of the 3-clause BSD license | ||
109 | + */ | ||
110 | +#ifndef _TPM_IOCTL_H_ | ||
111 | +#define _TPM_IOCTL_H_ | ||
112 | + | ||
113 | +#include <stdint.h> | ||
114 | +#include <sys/uio.h> | ||
115 | +#include <sys/types.h> | ||
116 | +#include <sys/ioctl.h> | ||
117 | + | ||
118 | +/* | ||
119 | + * Every response from a command involving a TPM command execution must hold | ||
120 | + * the ptm_res as the first element. | ||
121 | + * ptm_res corresponds to the error code of a command executed by the TPM. | ||
122 | + */ | ||
123 | + | ||
124 | +typedef uint32_t ptm_res; | ||
125 | + | ||
126 | +/* PTM_GET_TPMESTABLISHED: get the establishment bit */ | ||
127 | +struct ptm_est { | ||
128 | + union { | ||
129 | + struct { | ||
130 | + ptm_res tpm_result; | ||
131 | + unsigned char bit; /* TPM established bit */ | ||
132 | + } resp; /* response */ | ||
133 | + } u; | ||
134 | +}; | ||
135 | + | ||
136 | +/* PTM_RESET_TPMESTABLISHED: reset establishment bit */ | ||
137 | +struct ptm_reset_est { | ||
138 | + union { | ||
139 | + struct { | ||
140 | + uint8_t loc; /* locality to use */ | ||
141 | + } req; /* request */ | ||
142 | + struct { | ||
143 | + ptm_res tpm_result; | ||
144 | + } resp; /* response */ | ||
145 | + } u; | ||
146 | +}; | ||
147 | + | ||
148 | +/* PTM_INIT */ | ||
149 | +struct ptm_init { | ||
150 | + union { | ||
151 | + struct { | ||
152 | + uint32_t init_flags; /* see definitions below */ | ||
153 | + } req; /* request */ | ||
154 | + struct { | ||
155 | + ptm_res tpm_result; | ||
156 | + } resp; /* response */ | ||
157 | + } u; | ||
158 | +}; | ||
159 | + | ||
160 | +/* above init_flags */ | ||
161 | +#define PTM_INIT_FLAG_DELETE_VOLATILE (1 << 0) | ||
162 | + /* delete volatile state file after reading it */ | ||
163 | + | ||
164 | +/* PTM_SET_LOCALITY */ | ||
165 | +struct ptm_loc { | ||
166 | + union { | ||
167 | + struct { | ||
168 | + uint8_t loc; /* locality to set */ | ||
169 | + } req; /* request */ | ||
170 | + struct { | ||
171 | + ptm_res tpm_result; | ||
172 | + } resp; /* response */ | ||
173 | + } u; | ||
174 | +}; | ||
175 | + | ||
176 | +/* PTM_HASH_DATA: hash given data */ | ||
177 | +struct ptm_hdata { | ||
178 | + union { | ||
179 | + struct { | ||
180 | + uint32_t length; | ||
181 | + uint8_t data[4096]; | ||
182 | + } req; /* request */ | ||
183 | + struct { | ||
184 | + ptm_res tpm_result; | ||
185 | + } resp; /* response */ | ||
186 | + } u; | ||
187 | +}; | ||
188 | + | ||
189 | +/* | ||
190 | + * size of the TPM state blob to transfer; x86_64 can handle 8k, | ||
191 | + * ppc64le only ~7k; keep the response below a 4k page size | ||
192 | + */ | ||
193 | +#define PTM_STATE_BLOB_SIZE (3 * 1024) | ||
194 | + | ||
195 | +/* | ||
196 | + * The following is the data structure to get state blobs from the TPM. | ||
197 | + * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple reads | ||
198 | + * with this ioctl and with adjusted offset are necessary. All bytes | ||
199 | + * must be transferred and the transfer is done once the last byte has been | ||
200 | + * returned. | ||
201 | + * It is possible to use the read() interface for reading the data; however, | ||
202 | + * the first bytes of the state blob will be part of the response to the ioctl(); | ||
203 | + * a subsequent read() is only necessary if the total length (totlength) exceeds | ||
204 | + * the number of received bytes. seek() is not supported. | ||
205 | + */ | ||
206 | +struct ptm_getstate { | ||
207 | + union { | ||
208 | + struct { | ||
209 | + uint32_t state_flags; /* may be: PTM_STATE_FLAG_DECRYPTED */ | ||
210 | + uint32_t type; /* which blob to pull */ | ||
211 | + uint32_t offset; /* offset from where to read */ | ||
212 | + } req; /* request */ | ||
213 | + struct { | ||
214 | + ptm_res tpm_result; | ||
215 | + uint32_t state_flags; /* may be: PTM_STATE_FLAG_ENCRYPTED */ | ||
216 | + uint32_t totlength; /* total length that will be transferred */ | ||
217 | + uint32_t length; /* number of bytes in following buffer */ | ||
218 | + uint8_t data[PTM_STATE_BLOB_SIZE]; | ||
219 | + } resp; /* response */ | ||
220 | + } u; | ||
221 | +}; | ||
222 | + | ||
223 | +/* TPM state blob types */ | ||
224 | +#define PTM_BLOB_TYPE_PERMANENT 1 | ||
225 | +#define PTM_BLOB_TYPE_VOLATILE 2 | ||
226 | +#define PTM_BLOB_TYPE_SAVESTATE 3 | ||
227 | + | ||
228 | +/* state_flags above : */ | ||
229 | +#define PTM_STATE_FLAG_DECRYPTED 1 /* on input: get decrypted state */ | ||
230 | +#define PTM_STATE_FLAG_ENCRYPTED 2 /* on output: state is encrypted */ | ||
231 | + | ||
232 | +/* | ||
233 | + * The following is the data structure to set state blobs in the TPM. | ||
234 | + * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple | ||
235 | + * 'writes' using this ioctl are necessary. The last packet is indicated | ||
236 | + * by the length being smaller than the PTM_STATE_BLOB_SIZE. | ||
237 | + * The very first packet may have a length indicator of '0' enabling | ||
238 | + * a write() with all the bytes from a buffer. If the write() interface | ||
239 | + * is used, a final ioctl with a non-full buffer must be made to indicate | ||
240 | + * that all data were transferred (a write with 0 bytes would not work). | ||
241 | + */ | ||
242 | +struct ptm_setstate { | ||
243 | + union { | ||
244 | + struct { | ||
245 | + uint32_t state_flags; /* may be PTM_STATE_FLAG_ENCRYPTED */ | ||
246 | + uint32_t type; /* which blob to set */ | ||
247 | + uint32_t length; /* length of the data; | ||
248 | + use 0 on the first packet to | ||
249 | + transfer using write() */ | ||
250 | + uint8_t data[PTM_STATE_BLOB_SIZE]; | ||
251 | + } req; /* request */ | ||
252 | + struct { | ||
253 | + ptm_res tpm_result; | ||
254 | + } resp; /* response */ | ||
255 | + } u; | ||
256 | +}; | ||
257 | + | ||
258 | +/* | ||
259 | + * PTM_GET_CONFIG: Data structure to get runtime configuration information | ||
260 | + * such as which keys are applied. | ||
261 | + */ | ||
262 | +struct ptm_getconfig { | ||
263 | + union { | ||
264 | + struct { | ||
265 | + ptm_res tpm_result; | ||
266 | + uint32_t flags; | ||
267 | + } resp; /* response */ | ||
268 | + } u; | ||
269 | +}; | ||
270 | + | ||
271 | +#define PTM_CONFIG_FLAG_FILE_KEY 0x1 | ||
272 | +#define PTM_CONFIG_FLAG_MIGRATION_KEY 0x2 | ||
273 | + | ||
274 | + | ||
275 | +typedef uint64_t ptm_cap; | ||
276 | +typedef struct ptm_est ptm_est; | ||
277 | +typedef struct ptm_reset_est ptm_reset_est; | ||
278 | +typedef struct ptm_loc ptm_loc; | ||
279 | +typedef struct ptm_hdata ptm_hdata; | ||
280 | +typedef struct ptm_init ptm_init; | ||
281 | +typedef struct ptm_getstate ptm_getstate; | ||
282 | +typedef struct ptm_setstate ptm_setstate; | ||
283 | +typedef struct ptm_getconfig ptm_getconfig; | ||
284 | + | ||
285 | +/* capability flags returned by PTM_GET_CAPABILITY */ | ||
286 | +#define PTM_CAP_INIT (1) | ||
287 | +#define PTM_CAP_SHUTDOWN (1<<1) | ||
288 | +#define PTM_CAP_GET_TPMESTABLISHED (1<<2) | ||
289 | +#define PTM_CAP_SET_LOCALITY (1<<3) | ||
290 | +#define PTM_CAP_HASHING (1<<4) | ||
291 | +#define PTM_CAP_CANCEL_TPM_CMD (1<<5) | ||
292 | +#define PTM_CAP_STORE_VOLATILE (1<<6) | ||
293 | +#define PTM_CAP_RESET_TPMESTABLISHED (1<<7) | ||
294 | +#define PTM_CAP_GET_STATEBLOB (1<<8) | ||
295 | +#define PTM_CAP_SET_STATEBLOB (1<<9) | ||
296 | +#define PTM_CAP_STOP (1<<10) | ||
297 | +#define PTM_CAP_GET_CONFIG (1<<11) | ||
298 | + | ||
299 | +enum { | ||
300 | + PTM_GET_CAPABILITY = _IOR('P', 0, ptm_cap), | ||
301 | + PTM_INIT = _IOWR('P', 1, ptm_init), | ||
302 | + PTM_SHUTDOWN = _IOR('P', 2, ptm_res), | ||
303 | + PTM_GET_TPMESTABLISHED = _IOR('P', 3, ptm_est), | ||
304 | + PTM_SET_LOCALITY = _IOWR('P', 4, ptm_loc), | ||
305 | + PTM_HASH_START = _IOR('P', 5, ptm_res), | ||
306 | + PTM_HASH_DATA = _IOWR('P', 6, ptm_hdata), | ||
307 | + PTM_HASH_END = _IOR('P', 7, ptm_res), | ||
308 | + PTM_CANCEL_TPM_CMD = _IOR('P', 8, ptm_res), | ||
309 | + PTM_STORE_VOLATILE = _IOR('P', 9, ptm_res), | ||
310 | + PTM_RESET_TPMESTABLISHED = _IOWR('P', 10, ptm_reset_est), | ||
311 | + PTM_GET_STATEBLOB = _IOWR('P', 11, ptm_getstate), | ||
312 | + PTM_SET_STATEBLOB = _IOWR('P', 12, ptm_setstate), | ||
313 | + PTM_STOP = _IOR('P', 13, ptm_res), | ||
314 | + PTM_GET_CONFIG = _IOR('P', 14, ptm_getconfig), | ||
315 | +}; | ||
316 | + | ||
317 | +#endif /* _TPM_IOCTL_H */ | ||
318 | diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c | ||
319 | index e88c0d20bc..050f2ba850 100644 | ||
320 | --- a/hw/tpm/tpm_passthrough.c | ||
321 | +++ b/hw/tpm/tpm_passthrough.c | ||
322 | @@ -33,6 +33,7 @@ | ||
323 | #include "sysemu/tpm_backend_int.h" | ||
324 | #include "tpm_tis.h" | ||
325 | #include "tpm_util.h" | ||
326 | +#include "tpm_ioctl.h" | ||
327 | |||
328 | #define DEBUG_TPM 0 | ||
329 | |||
330 | @@ -45,6 +46,7 @@ | ||
331 | #define TYPE_TPM_PASSTHROUGH "tpm-passthrough" | ||
332 | #define TPM_PASSTHROUGH(obj) \ | ||
333 | OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH) | ||
334 | +#define TYPE_TPM_CUSE "tpm-cuse" | ||
335 | |||
336 | static const TPMDriverOps tpm_passthrough_driver; | ||
337 | |||
338 | @@ -71,12 +73,18 @@ struct TPMPassthruState { | ||
339 | bool had_startup_error; | ||
340 | |||
341 | TPMVersion tpm_version; | ||
342 | + ptm_cap cuse_cap; /* capabilities of the CUSE TPM */ | ||
343 | + uint8_t cur_locty_number; /* last set locality */ | ||
344 | }; | ||
345 | |||
346 | typedef struct TPMPassthruState TPMPassthruState; | ||
347 | |||
348 | #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0" | ||
349 | |||
350 | +#define TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt) (tpm_pt->cuse_cap != 0) | ||
351 | + | ||
352 | +#define TPM_CUSE_IMPLEMENTS_ALL(S, cap) (((S)->cuse_cap & (cap)) == (cap)) | ||
353 | + | ||
354 | /* functions */ | ||
355 | |||
356 | static void tpm_passthrough_cancel_cmd(TPMBackend *tb); | ||
357 | @@ -148,7 +156,28 @@ static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len) | ||
358 | return false; | ||
359 | } | ||
360 | |||
361 | +static int tpm_passthrough_set_locality(TPMPassthruState *tpm_pt, | ||
362 | + uint8_t locty_number) | ||
363 | +{ | ||
364 | + ptm_loc loc; | ||
365 | + | ||
366 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
367 | + if (tpm_pt->cur_locty_number != locty_number) { | ||
368 | + loc.u.req.loc = locty_number; | ||
369 | + if (ioctl(tpm_pt->tpm_fd, PTM_SET_LOCALITY, &loc) < 0) { | ||
370 | + error_report("tpm_cuse: could not set locality on " | ||
371 | + "CUSE TPM: %s", | ||
372 | + strerror(errno)); | ||
373 | + return -1; | ||
374 | + } | ||
375 | + tpm_pt->cur_locty_number = locty_number; | ||
376 | + } | ||
377 | + } | ||
378 | + return 0; | ||
379 | +} | ||
380 | + | ||
381 | static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, | ||
382 | + uint8_t locality_number, | ||
383 | const uint8_t *in, uint32_t in_len, | ||
384 | uint8_t *out, uint32_t out_len, | ||
385 | bool *selftest_done) | ||
386 | @@ -157,6 +186,11 @@ static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, | ||
387 | bool is_selftest; | ||
388 | const struct tpm_resp_hdr *hdr; | ||
389 | |||
390 | + ret = tpm_passthrough_set_locality(tpm_pt, locality_number); | ||
391 | + if (ret < 0) { | ||
392 | + goto err_exit; | ||
393 | + } | ||
394 | + | ||
395 | tpm_pt->tpm_op_canceled = false; | ||
396 | tpm_pt->tpm_executing = true; | ||
397 | *selftest_done = false; | ||
398 | @@ -207,10 +241,12 @@ err_exit: | ||
399 | } | ||
400 | |||
401 | static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt, | ||
402 | + uint8_t locality_number, | ||
403 | const TPMLocality *locty_data, | ||
404 | bool *selftest_done) | ||
405 | { | ||
406 | return tpm_passthrough_unix_tx_bufs(tpm_pt, | ||
407 | + locality_number, | ||
408 | locty_data->w_buffer.buffer, | ||
409 | locty_data->w_offset, | ||
410 | locty_data->r_buffer.buffer, | ||
411 | @@ -231,6 +267,7 @@ static void tpm_passthrough_worker_thread(gpointer data, | ||
412 | switch (cmd) { | ||
413 | case TPM_BACKEND_CMD_PROCESS_CMD: | ||
414 | tpm_passthrough_unix_transfer(tpm_pt, | ||
415 | + thr_parms->tpm_state->locty_number, | ||
416 | thr_parms->tpm_state->locty_data, | ||
417 | &selftest_done); | ||
418 | |||
419 | @@ -247,6 +284,93 @@ static void tpm_passthrough_worker_thread(gpointer data, | ||
420 | } | ||
421 | |||
422 | /* | ||
423 | + * Gracefully shut down the external CUSE TPM | ||
424 | + */ | ||
425 | +static void tpm_passthrough_shutdown(TPMPassthruState *tpm_pt) | ||
426 | +{ | ||
427 | + ptm_res res; | ||
428 | + | ||
429 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
430 | + if (ioctl(tpm_pt->tpm_fd, PTM_SHUTDOWN, &res) < 0) { | ||
431 | + error_report("tpm_cuse: Could not cleanly shut down " | ||
432 | + "the CUSE TPM: %s", | ||
433 | + strerror(errno)); | ||
434 | + } | ||
435 | + } | ||
436 | +} | ||
437 | + | ||
438 | +/* | ||
439 | + * Probe for the CUSE TPM by sending an ioctl() requesting its | ||
440 | + * capability flags. | ||
441 | + */ | ||
442 | +static int tpm_passthrough_cuse_probe(TPMPassthruState *tpm_pt) | ||
443 | +{ | ||
444 | + int rc = 0; | ||
445 | + | ||
446 | + if (ioctl(tpm_pt->tpm_fd, PTM_GET_CAPABILITY, &tpm_pt->cuse_cap) < 0) { | ||
447 | + error_report("Error: CUSE TPM was requested, but probing failed"); | ||
448 | + rc = -1; | ||
449 | + } | ||
450 | + | ||
451 | + return rc; | ||
452 | +} | ||
453 | + | ||
454 | +static int tpm_passthrough_cuse_check_caps(TPMPassthruState *tpm_pt) | ||
455 | +{ | ||
456 | + int rc = 0; | ||
457 | + ptm_cap caps = 0; | ||
458 | + const char *tpm = NULL; | ||
459 | + | ||
460 | + /* check for min. required capabilities */ | ||
461 | + switch (tpm_pt->tpm_version) { | ||
462 | + case TPM_VERSION_1_2: | ||
463 | + caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED | | ||
464 | + PTM_CAP_SET_LOCALITY; | ||
465 | + tpm = "1.2"; | ||
466 | + break; | ||
467 | + case TPM_VERSION_2_0: | ||
468 | + caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED | | ||
469 | + PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED; | ||
470 | + tpm = "2"; | ||
471 | + break; | ||
472 | + case TPM_VERSION_UNSPEC: | ||
473 | + error_report("tpm_cuse: %s: TPM version has not been set", | ||
474 | + __func__); | ||
475 | + return -1; | ||
476 | + } | ||
477 | + | ||
478 | + if (!TPM_CUSE_IMPLEMENTS_ALL(tpm_pt, caps)) { | ||
479 | + error_report("tpm_cuse: TPM does not implement minimum set of required " | ||
480 | + "capabilities for TPM %s (0x%x)", tpm, (int)caps); | ||
481 | + rc = -1; | ||
482 | + } | ||
483 | + | ||
484 | + return rc; | ||
485 | +} | ||
486 | + | ||
487 | +/* | ||
488 | + * Initialize the external CUSE TPM | ||
489 | + */ | ||
490 | +static int tpm_passthrough_cuse_init(TPMPassthruState *tpm_pt) | ||
491 | +{ | ||
492 | + int rc = 0; | ||
493 | + ptm_init init = { | ||
494 | + .u.req.init_flags = PTM_INIT_FLAG_DELETE_VOLATILE, | ||
495 | + }; | ||
496 | + | ||
497 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
498 | + if (ioctl(tpm_pt->tpm_fd, PTM_INIT, &init) < 0) { | ||
499 | + error_report("tpm_cuse: Detected CUSE TPM but could not " | ||
500 | + "send INIT: %s", | ||
501 | + strerror(errno)); | ||
502 | + rc = -1; | ||
503 | + } | ||
504 | + } | ||
505 | + | ||
506 | + return rc; | ||
507 | +} | ||
508 | + | ||
509 | +/* | ||
510 | * Start the TPM (thread). If it had been started before, then terminate | ||
511 | * and start it again. | ||
512 | */ | ||
513 | @@ -261,6 +385,8 @@ static int tpm_passthrough_startup_tpm(TPMBackend *tb) | ||
514 | tpm_passthrough_worker_thread, | ||
515 | &tpm_pt->tpm_thread_params); | ||
516 | |||
517 | + tpm_passthrough_cuse_init(tpm_pt); | ||
518 | + | ||
519 | return 0; | ||
520 | } | ||
521 | |||
522 | @@ -291,14 +417,43 @@ static int tpm_passthrough_init(TPMBackend *tb, TPMState *s, | ||
523 | |||
524 | static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb) | ||
525 | { | ||
526 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
527 | + ptm_est est; | ||
528 | + | ||
529 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
530 | + if (ioctl(tpm_pt->tpm_fd, PTM_GET_TPMESTABLISHED, &est) < 0) { | ||
531 | + error_report("tpm_cuse: Could not get the TPM established " | ||
532 | + "flag from the CUSE TPM: %s", | ||
533 | + strerror(errno)); | ||
534 | + return false; | ||
535 | + } | ||
536 | + return (est.u.resp.bit != 0); | ||
537 | + } | ||
538 | return false; | ||
539 | } | ||
540 | |||
541 | static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb, | ||
542 | uint8_t locty) | ||
543 | { | ||
544 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
545 | + int rc = 0; | ||
546 | + ptm_reset_est ptmreset_est; | ||
547 | + | ||
548 | /* only a TPM 2.0 will support this */ | ||
549 | - return 0; | ||
550 | + if (tpm_pt->tpm_version == TPM_VERSION_2_0) { | ||
551 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
552 | + ptmreset_est.u.req.loc = tpm_pt->cur_locty_number; | ||
553 | + | ||
554 | + if (ioctl(tpm_pt->tpm_fd, PTM_RESET_TPMESTABLISHED, | ||
555 | + &ptmreset_est) < 0) { | ||
556 | + error_report("tpm_cuse: Could not reset the establishment bit " | ||
557 | + "failed: %s", | ||
558 | + strerror(errno)); | ||
559 | + rc = -1; | ||
560 | + } | ||
561 | + } | ||
562 | + } | ||
563 | + return rc; | ||
564 | } | ||
565 | |||
566 | static bool tpm_passthrough_get_startup_error(TPMBackend *tb) | ||
567 | @@ -329,7 +484,8 @@ static void tpm_passthrough_deliver_request(TPMBackend *tb) | ||
568 | static void tpm_passthrough_cancel_cmd(TPMBackend *tb) | ||
569 | { | ||
570 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
571 | - int n; | ||
572 | + ptm_res res; | ||
573 | + static bool error_printed; | ||
574 | |||
575 | /* | ||
576 | * As of Linux 3.7 the tpm_tis driver does not properly cancel | ||
577 | @@ -338,17 +494,34 @@ static void tpm_passthrough_cancel_cmd(TPMBackend *tb) | ||
578 | * command, e.g., a command executed on the host. | ||
579 | */ | ||
580 | if (tpm_pt->tpm_executing) { | ||
581 | - if (tpm_pt->cancel_fd >= 0) { | ||
582 | - n = write(tpm_pt->cancel_fd, "-", 1); | ||
583 | - if (n != 1) { | ||
584 | - error_report("Canceling TPM command failed: %s", | ||
585 | - strerror(errno)); | ||
586 | - } else { | ||
587 | - tpm_pt->tpm_op_canceled = true; | ||
588 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
589 | + if (TPM_CUSE_IMPLEMENTS_ALL(tpm_pt, PTM_CAP_CANCEL_TPM_CMD)) { | ||
590 | + if (ioctl(tpm_pt->tpm_fd, PTM_CANCEL_TPM_CMD, &res) < 0) { | ||
591 | + error_report("tpm_cuse: Could not cancel command on " | ||
592 | + "CUSE TPM: %s", | ||
593 | + strerror(errno)); | ||
594 | + } else if (res != TPM_SUCCESS) { | ||
595 | + if (!error_printed) { | ||
596 | + error_report("TPM error code from command " | ||
597 | + "cancellation of CUSE TPM: 0x%x", res); | ||
598 | + error_printed = true; | ||
599 | + } | ||
600 | + } else { | ||
601 | + tpm_pt->tpm_op_canceled = true; | ||
602 | + } | ||
603 | } | ||
604 | } else { | ||
605 | - error_report("Cannot cancel TPM command due to missing " | ||
606 | - "TPM sysfs cancel entry"); | ||
607 | + if (tpm_pt->cancel_fd >= 0) { | ||
608 | + if (write(tpm_pt->cancel_fd, "-", 1) != 1) { | ||
609 | + error_report("Canceling TPM command failed: %s", | ||
610 | + strerror(errno)); | ||
611 | + } else { | ||
612 | + tpm_pt->tpm_op_canceled = true; | ||
613 | + } | ||
614 | + } else { | ||
615 | + error_report("Cannot cancel TPM command due to missing " | ||
616 | + "TPM sysfs cancel entry"); | ||
617 | + } | ||
618 | } | ||
619 | } | ||
620 | } | ||
621 | @@ -378,6 +551,11 @@ static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb) | ||
622 | char *dev; | ||
623 | char path[PATH_MAX]; | ||
624 | |||
625 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
626 | + /* not needed, but so we have a fd */ | ||
627 | + return qemu_open("/dev/null", O_WRONLY); | ||
628 | + } | ||
629 | + | ||
630 | if (tb->cancel_path) { | ||
631 | fd = qemu_open(tb->cancel_path, O_WRONLY); | ||
632 | if (fd < 0) { | ||
633 | @@ -412,12 +590,22 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) | ||
634 | { | ||
635 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
636 | const char *value; | ||
637 | + bool have_cuse = false; | ||
638 | + | ||
639 | + value = qemu_opt_get(opts, "type"); | ||
640 | + if (value != NULL && !strcmp("cuse-tpm", value)) { | ||
641 | + have_cuse = true; | ||
642 | + } | ||
643 | |||
644 | value = qemu_opt_get(opts, "cancel-path"); | ||
645 | tb->cancel_path = g_strdup(value); | ||
646 | |||
647 | value = qemu_opt_get(opts, "path"); | ||
648 | if (!value) { | ||
649 | + if (have_cuse) { | ||
650 | + error_report("Missing path to access CUSE TPM"); | ||
651 | + goto err_free_parameters; | ||
652 | + } | ||
653 | value = TPM_PASSTHROUGH_DEFAULT_DEVICE; | ||
654 | } | ||
655 | |||
656 | @@ -432,15 +620,36 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) | ||
657 | goto err_free_parameters; | ||
658 | } | ||
659 | |||
660 | + tpm_pt->cur_locty_number = ~0; | ||
661 | + | ||
662 | + if (have_cuse) { | ||
663 | + if (tpm_passthrough_cuse_probe(tpm_pt)) { | ||
664 | + goto err_close_tpmdev; | ||
665 | + } | ||
666 | + /* init TPM for probing */ | ||
667 | + if (tpm_passthrough_cuse_init(tpm_pt)) { | ||
668 | + goto err_close_tpmdev; | ||
669 | + } | ||
670 | + } | ||
671 | + | ||
672 | if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) { | ||
673 | error_report("'%s' is not a TPM device.", | ||
674 | tpm_pt->tpm_dev); | ||
675 | goto err_close_tpmdev; | ||
676 | } | ||
677 | |||
678 | + if (have_cuse) { | ||
679 | + if (tpm_passthrough_cuse_check_caps(tpm_pt)) { | ||
680 | + goto err_close_tpmdev; | ||
681 | + } | ||
682 | + } | ||
683 | + | ||
684 | + | ||
685 | return 0; | ||
686 | |||
687 | err_close_tpmdev: | ||
688 | + tpm_passthrough_shutdown(tpm_pt); | ||
689 | + | ||
690 | qemu_close(tpm_pt->tpm_fd); | ||
691 | tpm_pt->tpm_fd = -1; | ||
692 | |||
693 | @@ -491,6 +700,8 @@ static void tpm_passthrough_destroy(TPMBackend *tb) | ||
694 | |||
695 | tpm_backend_thread_end(&tpm_pt->tbt); | ||
696 | |||
697 | + tpm_passthrough_shutdown(tpm_pt); | ||
698 | + | ||
699 | qemu_close(tpm_pt->tpm_fd); | ||
700 | qemu_close(tpm_pt->cancel_fd); | ||
701 | |||
702 | @@ -564,3 +775,44 @@ static void tpm_passthrough_register(void) | ||
703 | } | ||
704 | |||
705 | type_init(tpm_passthrough_register) | ||
706 | + | ||
707 | +/* CUSE TPM */ | ||
708 | +static const char *tpm_passthrough_cuse_create_desc(void) | ||
709 | +{ | ||
710 | + return "CUSE TPM backend driver"; | ||
711 | +} | ||
712 | + | ||
713 | +static const TPMDriverOps tpm_cuse_driver = { | ||
714 | + .type = TPM_TYPE_CUSE_TPM, | ||
715 | + .opts = tpm_passthrough_cmdline_opts, | ||
716 | + .desc = tpm_passthrough_cuse_create_desc, | ||
717 | + .create = tpm_passthrough_create, | ||
718 | + .destroy = tpm_passthrough_destroy, | ||
719 | + .init = tpm_passthrough_init, | ||
720 | + .startup_tpm = tpm_passthrough_startup_tpm, | ||
721 | + .realloc_buffer = tpm_passthrough_realloc_buffer, | ||
722 | + .reset = tpm_passthrough_reset, | ||
723 | + .had_startup_error = tpm_passthrough_get_startup_error, | ||
724 | + .deliver_request = tpm_passthrough_deliver_request, | ||
725 | + .cancel_cmd = tpm_passthrough_cancel_cmd, | ||
726 | + .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag, | ||
727 | + .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag, | ||
728 | + .get_tpm_version = tpm_passthrough_get_tpm_version, | ||
729 | +}; | ||
730 | + | ||
731 | +static const TypeInfo tpm_cuse_info = { | ||
732 | + .name = TYPE_TPM_CUSE, | ||
733 | + .parent = TYPE_TPM_BACKEND, | ||
734 | + .instance_size = sizeof(TPMPassthruState), | ||
735 | + .class_init = tpm_passthrough_class_init, | ||
736 | + .instance_init = tpm_passthrough_inst_init, | ||
737 | + .instance_finalize = tpm_passthrough_inst_finalize, | ||
738 | +}; | ||
739 | + | ||
740 | +static void tpm_cuse_register(void) | ||
741 | +{ | ||
742 | + type_register_static(&tpm_cuse_info); | ||
743 | + tpm_register_driver(&tpm_cuse_driver); | ||
744 | +} | ||
745 | + | ||
746 | +type_init(tpm_cuse_register) | ||
747 | diff --git a/qapi-schema.json b/qapi-schema.json | ||
748 | index 5658723b37..53120d0f63 100644 | ||
749 | --- a/qapi-schema.json | ||
750 | +++ b/qapi-schema.json | ||
751 | @@ -3522,10 +3522,12 @@ | ||
752 | # An enumeration of TPM types | ||
753 | # | ||
754 | # @passthrough: TPM passthrough type | ||
755 | +# @cuse-tpm: CUSE TPM type | ||
756 | +# Since: 2.6 | ||
757 | # | ||
758 | # Since: 1.5 | ||
759 | ## | ||
760 | -{ 'enum': 'TpmType', 'data': [ 'passthrough' ] } | ||
761 | +{ 'enum': 'TpmType', 'data': [ 'passthrough', 'cuse-tpm' ] } | ||
762 | |||
763 | ## | ||
764 | # @query-tpm-types: | ||
765 | @@ -3554,6 +3556,17 @@ | ||
766 | '*cancel-path' : 'str'} } | ||
767 | |||
768 | ## | ||
769 | +# @TPMCuseOptions: | ||
770 | +# | ||
771 | +# Information about the CUSE TPM type | ||
772 | +# | ||
773 | +# @path: string describing the path used for accessing the TPM device | ||
774 | +# | ||
775 | +# Since: 2.6 | ||
776 | +## | ||
777 | +{ 'struct': 'TPMCuseOptions', 'data': { 'path' : 'str'}} | ||
778 | + | ||
779 | +## | ||
780 | # @TpmTypeOptions: | ||
781 | # | ||
782 | # A union referencing different TPM backend types' configuration options | ||
783 | @@ -3563,7 +3576,8 @@ | ||
784 | # Since: 1.5 | ||
785 | ## | ||
786 | { 'union': 'TpmTypeOptions', | ||
787 | - 'data': { 'passthrough' : 'TPMPassthroughOptions' } } | ||
788 | + 'data': { 'passthrough' : 'TPMPassthroughOptions', | ||
789 | + 'cuse-tpm' : 'TPMCuseOptions' } } | ||
790 | |||
791 | ## | ||
792 | # @TpmInfo: | ||
793 | diff --git a/qemu-options.hx b/qemu-options.hx | ||
794 | index a71aaf8ea8..e0f1d8e676 100644 | ||
795 | --- a/qemu-options.hx | ||
796 | +++ b/qemu-options.hx | ||
797 | @@ -2763,7 +2763,10 @@ DEF("tpmdev", HAS_ARG, QEMU_OPTION_tpmdev, \ | ||
798 | "-tpmdev passthrough,id=id[,path=path][,cancel-path=path]\n" | ||
799 | " use path to provide path to a character device; default is /dev/tpm0\n" | ||
800 | " use cancel-path to provide path to TPM's cancel sysfs entry; if\n" | ||
801 | - " not provided it will be searched for in /sys/class/misc/tpm?/device\n", | ||
802 | + " not provided it will be searched for in /sys/class/misc/tpm?/device\n" | ||
803 | + "-tpmdev cuse-tpm,id=id,path=path\n" | ||
804 | + " use path to provide path to a character device to talk to the\n" | ||
805 | + " TPM emulator providing a CUSE interface\n", | ||
806 | QEMU_ARCH_ALL) | ||
807 | STEXI | ||
808 | |||
809 | @@ -2772,8 +2775,8 @@ The general form of a TPM device option is: | ||
810 | |||
811 | @item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] | ||
812 | @findex -tpmdev | ||
813 | -Backend type must be: | ||
814 | -@option{passthrough}. | ||
815 | +Backend type must be either one of the following: | ||
816 | +@option{passthrough}, @option{cuse-tpm}. | ||
817 | |||
818 | The specific backend type will determine the applicable options. | ||
819 | The @code{-tpmdev} option creates the TPM backend and requires a | ||
820 | @@ -2823,6 +2826,18 @@ To create a passthrough TPM use the following two options: | ||
821 | Note that the @code{-tpmdev} id is @code{tpm0} and is referenced by | ||
822 | @code{tpmdev=tpm0} in the device option. | ||
823 | |||
824 | +@item -tpmdev cuse-tpm, id=@var{id}, path=@var{path} | ||
825 | + | ||
826 | +(Linux-host only) Enable access to a TPM emulator with a CUSE interface. | ||
827 | + | ||
828 | +@option{path} specifies the path to the CUSE TPM character device. | ||
829 | + | ||
830 | +To create a backend device accessing the CUSE TPM emulator using /dev/vtpm | ||
831 | +use the following two options: | ||
832 | +@example | ||
833 | +-tpmdev cuse-tpm,id=tpm0,path=/dev/vtpm -device tpm-tis,tpmdev=tpm0 | ||
834 | +@end example | ||
835 | + | ||
836 | @end table | ||
837 | |||
838 | ETEXI | ||
839 | diff --git a/tpm.c b/tpm.c | ||
840 | index 9a7c7114d3..5ec2373286 100644 | ||
841 | --- a/tpm.c | ||
842 | +++ b/tpm.c | ||
843 | @@ -25,7 +25,7 @@ static QLIST_HEAD(, TPMBackend) tpm_backends = | ||
844 | |||
845 | |||
846 | #define TPM_MAX_MODELS 1 | ||
847 | -#define TPM_MAX_DRIVERS 1 | ||
848 | +#define TPM_MAX_DRIVERS 2 | ||
849 | |||
850 | static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = { | ||
851 | NULL, | ||
852 | @@ -272,6 +272,15 @@ static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv) | ||
853 | tpo->has_cancel_path = true; | ||
854 | } | ||
855 | break; | ||
856 | + case TPM_TYPE_CUSE_TPM: | ||
857 | + res->options->type = TPM_TYPE_OPTIONS_KIND_CUSE_TPM; | ||
858 | + tpo = g_new0(TPMPassthroughOptions, 1); | ||
859 | + res->options->u.passthrough.data = tpo; | ||
860 | + if (drv->path) { | ||
861 | + tpo->path = g_strdup(drv->path); | ||
862 | + tpo->has_path = true; | ||
863 | + } | ||
864 | + break; | ||
865 | case TPM_TYPE__MAX: | ||
866 | break; | ||
867 | } | ||
868 | -- | ||
869 | 2.11.0 | ||
870 | |||
diff --git a/meta/recipes-devtools/qemu/qemu/0002-Introduce-condition-to-notify-waiters-of-completed-c.patch b/meta/recipes-devtools/qemu/qemu/0002-Introduce-condition-to-notify-waiters-of-completed-c.patch new file mode 100644 index 0000000000..c88c98e565 --- /dev/null +++ b/meta/recipes-devtools/qemu/qemu/0002-Introduce-condition-to-notify-waiters-of-completed-c.patch | |||
@@ -0,0 +1,86 @@ | |||
1 | From b5ffd3aa4e9bd4edb09cc84c46f78da72697a946 Mon Sep 17 00:00:00 2001 | ||
2 | From: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
3 | Date: Sat, 31 Dec 2016 11:23:32 -0500 | ||
4 | Subject: [PATCH 2/4] Introduce condition to notify waiters of completed | ||
5 | command | ||
6 | |||
7 | Introduce a lock and a condition to notify anyone waiting for the completion | ||
8 | of the execution of a TPM command by the backend (thread). The backend | ||
9 | uses the condition to signal anyone waiting for command completion. | ||
10 | We need to place the condition in two locations: one is invoked by the | ||
11 | backend thread, the other by the bottom half thread. | ||
12 | We will use the signalling to wait for command completion before VM | ||
13 | suspend. | ||
14 | |||
15 | Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
16 | |||
17 | Upstream-Status: Pending [https://lists.nongnu.org/archive/html/qemu-devel/2016-06/msg00252.html] | ||
18 | Signed-off-by: Patrick Ohly <patrick.ohly@intel.com> | ||
19 | --- | ||
20 | hw/tpm/tpm_int.h | 3 +++ | ||
21 | hw/tpm/tpm_tis.c | 14 ++++++++++++++ | ||
22 | 2 files changed, 17 insertions(+) | ||
23 | |||
24 | diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h | ||
25 | index 6b2c9c953a..70be1ad8d9 100644 | ||
26 | --- a/hw/tpm/tpm_int.h | ||
27 | +++ b/hw/tpm/tpm_int.h | ||
28 | @@ -30,6 +30,9 @@ struct TPMState { | ||
29 | char *backend; | ||
30 | TPMBackend *be_driver; | ||
31 | TPMVersion be_tpm_version; | ||
32 | + | ||
33 | + QemuMutex state_lock; | ||
34 | + QemuCond cmd_complete; | ||
35 | }; | ||
36 | |||
37 | #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS) | ||
38 | diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c | ||
39 | index 381e7266ea..14d9e83ea2 100644 | ||
40 | --- a/hw/tpm/tpm_tis.c | ||
41 | +++ b/hw/tpm/tpm_tis.c | ||
42 | @@ -368,6 +368,8 @@ static void tpm_tis_receive_bh(void *opaque) | ||
43 | TPMTISEmuState *tis = &s->s.tis; | ||
44 | uint8_t locty = s->locty_number; | ||
45 | |||
46 | + qemu_mutex_lock(&s->state_lock); | ||
47 | + | ||
48 | tpm_tis_sts_set(&tis->loc[locty], | ||
49 | TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE); | ||
50 | tis->loc[locty].state = TPM_TIS_STATE_COMPLETION; | ||
51 | @@ -384,6 +386,10 @@ static void tpm_tis_receive_bh(void *opaque) | ||
52 | tpm_tis_raise_irq(s, locty, | ||
53 | TPM_TIS_INT_DATA_AVAILABLE | TPM_TIS_INT_STS_VALID); | ||
54 | #endif | ||
55 | + | ||
56 | + /* notify of completed command */ | ||
57 | + qemu_cond_signal(&s->cmd_complete); | ||
58 | + qemu_mutex_unlock(&s->state_lock); | ||
59 | } | ||
60 | |||
61 | /* | ||
62 | @@ -403,6 +409,11 @@ static void tpm_tis_receive_cb(TPMState *s, uint8_t locty, | ||
63 | } | ||
64 | } | ||
65 | |||
66 | + qemu_mutex_lock(&s->state_lock); | ||
67 | + /* notify of completed command */ | ||
68 | + qemu_cond_signal(&s->cmd_complete); | ||
69 | + qemu_mutex_unlock(&s->state_lock); | ||
70 | + | ||
71 | qemu_bh_schedule(tis->bh); | ||
72 | } | ||
73 | |||
74 | @@ -1072,6 +1083,9 @@ static void tpm_tis_initfn(Object *obj) | ||
75 | memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops, | ||
76 | s, "tpm-tis-mmio", | ||
77 | TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT); | ||
78 | + | ||
79 | + qemu_mutex_init(&s->state_lock); | ||
80 | + qemu_cond_init(&s->cmd_complete); | ||
81 | } | ||
82 | |||
83 | static void tpm_tis_class_init(ObjectClass *klass, void *data) | ||
84 | -- | ||
85 | 2.11.0 | ||
86 | |||
diff --git a/meta/recipes-devtools/qemu/qemu/0003-Introduce-condition-in-TPM-backend-for-notification.patch b/meta/recipes-devtools/qemu/qemu/0003-Introduce-condition-in-TPM-backend-for-notification.patch new file mode 100644 index 0000000000..e58f019062 --- /dev/null +++ b/meta/recipes-devtools/qemu/qemu/0003-Introduce-condition-in-TPM-backend-for-notification.patch | |||
@@ -0,0 +1,79 @@ | |||
1 | From 732a8e046948fd62b32cd1dd76a6798eb1caf4d6 Mon Sep 17 00:00:00 2001 | ||
2 | From: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
3 | Date: Sat, 31 Dec 2016 11:23:32 -0500 | ||
4 | Subject: [PATCH 3/4] Introduce condition in TPM backend for notification | ||
5 | |||
6 | TPM backends will suspend independently of the frontends. Also | ||
7 | here we need to be able to wait for the TPM command to have been | ||
8 | completely processed. | ||
9 | |||
10 | Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
11 | |||
12 | Upstream-Status: Pending [https://lists.nongnu.org/archive/html/qemu-devel/2016-06/msg00252.html] | ||
13 | Signed-off-by: Patrick Ohly <patrick.ohly@intel.com> | ||
14 | --- | ||
15 | hw/tpm/tpm_passthrough.c | 20 ++++++++++++++++++++ | ||
16 | 1 file changed, 20 insertions(+) | ||
17 | |||
18 | diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c | ||
19 | index 050f2ba850..44739ebad2 100644 | ||
20 | --- a/hw/tpm/tpm_passthrough.c | ||
21 | +++ b/hw/tpm/tpm_passthrough.c | ||
22 | @@ -75,6 +75,10 @@ struct TPMPassthruState { | ||
23 | TPMVersion tpm_version; | ||
24 | ptm_cap cuse_cap; /* capabilities of the CUSE TPM */ | ||
25 | uint8_t cur_locty_number; /* last set locality */ | ||
26 | + | ||
27 | + QemuMutex state_lock; | ||
28 | + QemuCond cmd_complete; /* singnaled once tpm_busy is false */ | ||
29 | + bool tpm_busy; | ||
30 | }; | ||
31 | |||
32 | typedef struct TPMPassthruState TPMPassthruState; | ||
33 | @@ -274,6 +278,11 @@ static void tpm_passthrough_worker_thread(gpointer data, | ||
34 | thr_parms->recv_data_callback(thr_parms->tpm_state, | ||
35 | thr_parms->tpm_state->locty_number, | ||
36 | selftest_done); | ||
37 | + /* result delivered */ | ||
38 | + qemu_mutex_lock(&tpm_pt->state_lock); | ||
39 | + tpm_pt->tpm_busy = false; | ||
40 | + qemu_cond_signal(&tpm_pt->cmd_complete); | ||
41 | + qemu_mutex_unlock(&tpm_pt->state_lock); | ||
42 | break; | ||
43 | case TPM_BACKEND_CMD_INIT: | ||
44 | case TPM_BACKEND_CMD_END: | ||
45 | @@ -401,6 +410,7 @@ static void tpm_passthrough_reset(TPMBackend *tb) | ||
46 | tpm_backend_thread_end(&tpm_pt->tbt); | ||
47 | |||
48 | tpm_pt->had_startup_error = false; | ||
49 | + tpm_pt->tpm_busy = false; | ||
50 | } | ||
51 | |||
52 | static int tpm_passthrough_init(TPMBackend *tb, TPMState *s, | ||
53 | @@ -478,6 +488,11 @@ static void tpm_passthrough_deliver_request(TPMBackend *tb) | ||
54 | { | ||
55 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
56 | |||
57 | + /* TPM considered busy once TPM Request scheduled for processing */ | ||
58 | + qemu_mutex_lock(&tpm_pt->state_lock); | ||
59 | + tpm_pt->tpm_busy = true; | ||
60 | + qemu_mutex_unlock(&tpm_pt->state_lock); | ||
61 | + | ||
62 | tpm_backend_thread_deliver_request(&tpm_pt->tbt); | ||
63 | } | ||
64 | |||
65 | @@ -746,6 +761,11 @@ static const TPMDriverOps tpm_passthrough_driver = { | ||
66 | |||
67 | static void tpm_passthrough_inst_init(Object *obj) | ||
68 | { | ||
69 | + TPMBackend *tb = TPM_BACKEND(obj); | ||
70 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
71 | + | ||
72 | + qemu_mutex_init(&tpm_pt->state_lock); | ||
73 | + qemu_cond_init(&tpm_pt->cmd_complete); | ||
74 | } | ||
75 | |||
76 | static void tpm_passthrough_inst_finalize(Object *obj) | ||
77 | -- | ||
78 | 2.11.0 | ||
79 | |||
diff --git a/meta/recipes-devtools/qemu/qemu/0004-Add-support-for-VM-suspend-resume-for-TPM-TIS.patch b/meta/recipes-devtools/qemu/qemu/0004-Add-support-for-VM-suspend-resume-for-TPM-TIS.patch new file mode 100644 index 0000000000..b8a783d4e9 --- /dev/null +++ b/meta/recipes-devtools/qemu/qemu/0004-Add-support-for-VM-suspend-resume-for-TPM-TIS.patch | |||
@@ -0,0 +1,719 @@ | |||
1 | From 5e9dd9063f514447ea4f54046793f4f01c297ed4 Mon Sep 17 00:00:00 2001 | ||
2 | From: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
3 | Date: Sat, 31 Dec 2016 11:23:32 -0500 | ||
4 | Subject: [PATCH 4/4] Add support for VM suspend/resume for TPM TIS | ||
5 | |||
6 | Extend the TPM TIS code to support suspend/resume. In case a command | ||
7 | is being processed by the external TPM when suspending, wait for the command | ||
8 | to complete to catch the result. In case the bottom half did not run, | ||
9 | run the one function the bottom half is supposed to run. This then | ||
10 | makes the resume operation work. | ||
11 | |||
12 | The passthrough backend does not support suspend/resume operation | ||
13 | and is therefore blocked from suspend/resume and migration. | ||
14 | |||
15 | The CUSE TPM's supported capabilities are tested and if sufficient | ||
16 | capabilities are implemented, suspend/resume, snapshotting and | ||
17 | migration are supported by the CUSE TPM. | ||
18 | |||
19 | Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com> | ||
20 | |||
21 | Upstream-Status: Pending [https://lists.nongnu.org/archive/html/qemu-devel/2016-06/msg00252.html] | ||
22 | Signed-off-by: Patrick Ohly <patrick.ohly@intel.com> | ||
23 | --- | ||
24 | hw/tpm/tpm_passthrough.c | 130 +++++++++++++++++++++++-- | ||
25 | hw/tpm/tpm_tis.c | 137 +++++++++++++++++++++++++- | ||
26 | hw/tpm/tpm_tis.h | 2 + | ||
27 | hw/tpm/tpm_util.c | 223 +++++++++++++++++++++++++++++++++++++++++++ | ||
28 | hw/tpm/tpm_util.h | 7 ++ | ||
29 | include/sysemu/tpm_backend.h | 12 +++ | ||
30 | 6 files changed, 503 insertions(+), 8 deletions(-) | ||
31 | |||
32 | diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c | ||
33 | index 44739ebad2..bc8072d0bc 100644 | ||
34 | --- a/hw/tpm/tpm_passthrough.c | ||
35 | +++ b/hw/tpm/tpm_passthrough.c | ||
36 | @@ -34,6 +34,8 @@ | ||
37 | #include "tpm_tis.h" | ||
38 | #include "tpm_util.h" | ||
39 | #include "tpm_ioctl.h" | ||
40 | +#include "migration/migration.h" | ||
41 | +#include "qapi/error.h" | ||
42 | |||
43 | #define DEBUG_TPM 0 | ||
44 | |||
45 | @@ -49,6 +51,7 @@ | ||
46 | #define TYPE_TPM_CUSE "tpm-cuse" | ||
47 | |||
48 | static const TPMDriverOps tpm_passthrough_driver; | ||
49 | +static const VMStateDescription vmstate_tpm_cuse; | ||
50 | |||
51 | /* data structures */ | ||
52 | typedef struct TPMPassthruThreadParams { | ||
53 | @@ -79,6 +82,10 @@ struct TPMPassthruState { | ||
54 | QemuMutex state_lock; | ||
55 | QemuCond cmd_complete; /* singnaled once tpm_busy is false */ | ||
56 | bool tpm_busy; | ||
57 | + | ||
58 | + Error *migration_blocker; | ||
59 | + | ||
60 | + TPMBlobBuffers tpm_blobs; | ||
61 | }; | ||
62 | |||
63 | typedef struct TPMPassthruState TPMPassthruState; | ||
64 | @@ -306,6 +313,10 @@ static void tpm_passthrough_shutdown(TPMPassthruState *tpm_pt) | ||
65 | strerror(errno)); | ||
66 | } | ||
67 | } | ||
68 | + if (tpm_pt->migration_blocker) { | ||
69 | + migrate_del_blocker(tpm_pt->migration_blocker); | ||
70 | + error_free(tpm_pt->migration_blocker); | ||
71 | + } | ||
72 | } | ||
73 | |||
74 | /* | ||
75 | @@ -360,12 +371,14 @@ static int tpm_passthrough_cuse_check_caps(TPMPassthruState *tpm_pt) | ||
76 | /* | ||
77 | * Initialize the external CUSE TPM | ||
78 | */ | ||
79 | -static int tpm_passthrough_cuse_init(TPMPassthruState *tpm_pt) | ||
80 | +static int tpm_passthrough_cuse_init(TPMPassthruState *tpm_pt, | ||
81 | + bool is_resume) | ||
82 | { | ||
83 | int rc = 0; | ||
84 | - ptm_init init = { | ||
85 | - .u.req.init_flags = PTM_INIT_FLAG_DELETE_VOLATILE, | ||
86 | - }; | ||
87 | + ptm_init init; | ||
88 | + if (is_resume) { | ||
89 | + init.u.req.init_flags = PTM_INIT_FLAG_DELETE_VOLATILE; | ||
90 | + } | ||
91 | |||
92 | if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
93 | if (ioctl(tpm_pt->tpm_fd, PTM_INIT, &init) < 0) { | ||
94 | @@ -394,7 +407,7 @@ static int tpm_passthrough_startup_tpm(TPMBackend *tb) | ||
95 | tpm_passthrough_worker_thread, | ||
96 | &tpm_pt->tpm_thread_params); | ||
97 | |||
98 | - tpm_passthrough_cuse_init(tpm_pt); | ||
99 | + tpm_passthrough_cuse_init(tpm_pt, false); | ||
100 | |||
101 | return 0; | ||
102 | } | ||
103 | @@ -466,6 +479,32 @@ static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb, | ||
104 | return rc; | ||
105 | } | ||
106 | |||
107 | +static int tpm_cuse_get_state_blobs(TPMBackend *tb, | ||
108 | + bool decrypted_blobs, | ||
109 | + TPMBlobBuffers *tpm_blobs) | ||
110 | +{ | ||
111 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
112 | + | ||
113 | + assert(TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)); | ||
114 | + | ||
115 | + return tpm_util_cuse_get_state_blobs(tpm_pt->tpm_fd, decrypted_blobs, | ||
116 | + tpm_blobs); | ||
117 | +} | ||
118 | + | ||
119 | +static int tpm_cuse_set_state_blobs(TPMBackend *tb, | ||
120 | + TPMBlobBuffers *tpm_blobs) | ||
121 | +{ | ||
122 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
123 | + | ||
124 | + assert(TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)); | ||
125 | + | ||
126 | + if (tpm_util_cuse_set_state_blobs(tpm_pt->tpm_fd, tpm_blobs)) { | ||
127 | + return 1; | ||
128 | + } | ||
129 | + | ||
130 | + return tpm_passthrough_cuse_init(tpm_pt, true); | ||
131 | +} | ||
132 | + | ||
133 | static bool tpm_passthrough_get_startup_error(TPMBackend *tb) | ||
134 | { | ||
135 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
136 | @@ -488,7 +527,7 @@ static void tpm_passthrough_deliver_request(TPMBackend *tb) | ||
137 | { | ||
138 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
139 | |||
140 | - /* TPM considered busy once TPM Request scheduled for processing */ | ||
141 | + /* TPM considered busy once TPM request scheduled for processing */ | ||
142 | qemu_mutex_lock(&tpm_pt->state_lock); | ||
143 | tpm_pt->tpm_busy = true; | ||
144 | qemu_mutex_unlock(&tpm_pt->state_lock); | ||
145 | @@ -601,6 +640,25 @@ static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb) | ||
146 | return fd; | ||
147 | } | ||
148 | |||
149 | +static void tpm_passthrough_block_migration(TPMPassthruState *tpm_pt) | ||
150 | +{ | ||
151 | + ptm_cap caps; | ||
152 | + | ||
153 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { | ||
154 | + caps = PTM_CAP_GET_STATEBLOB | PTM_CAP_SET_STATEBLOB | | ||
155 | + PTM_CAP_STOP; | ||
156 | + if (!TPM_CUSE_IMPLEMENTS_ALL(tpm_pt, caps)) { | ||
157 | + error_setg(&tpm_pt->migration_blocker, | ||
158 | + "Migration disabled: CUSE TPM lacks necessary capabilities"); | ||
159 | + migrate_add_blocker(tpm_pt->migration_blocker); | ||
160 | + } | ||
161 | + } else { | ||
162 | + error_setg(&tpm_pt->migration_blocker, | ||
163 | + "Migration disabled: Passthrough TPM does not support migration"); | ||
164 | + migrate_add_blocker(tpm_pt->migration_blocker); | ||
165 | + } | ||
166 | +} | ||
167 | + | ||
168 | static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) | ||
169 | { | ||
170 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); | ||
171 | @@ -642,7 +700,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) | ||
172 | goto err_close_tpmdev; | ||
173 | } | ||
174 | /* init TPM for probing */ | ||
175 | - if (tpm_passthrough_cuse_init(tpm_pt)) { | ||
176 | + if (tpm_passthrough_cuse_init(tpm_pt, false)) { | ||
177 | goto err_close_tpmdev; | ||
178 | } | ||
179 | } | ||
180 | @@ -659,6 +717,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) | ||
181 | } | ||
182 | } | ||
183 | |||
184 | + tpm_passthrough_block_migration(tpm_pt); | ||
185 | |||
186 | return 0; | ||
187 | |||
188 | @@ -766,10 +825,13 @@ static void tpm_passthrough_inst_init(Object *obj) | ||
189 | |||
190 | qemu_mutex_init(&tpm_pt->state_lock); | ||
191 | qemu_cond_init(&tpm_pt->cmd_complete); | ||
192 | + | ||
193 | + vmstate_register(NULL, -1, &vmstate_tpm_cuse, obj); | ||
194 | } | ||
195 | |||
196 | static void tpm_passthrough_inst_finalize(Object *obj) | ||
197 | { | ||
198 | + vmstate_unregister(NULL, &vmstate_tpm_cuse, obj); | ||
199 | } | ||
200 | |||
201 | static void tpm_passthrough_class_init(ObjectClass *klass, void *data) | ||
202 | @@ -802,6 +864,60 @@ static const char *tpm_passthrough_cuse_create_desc(void) | ||
203 | return "CUSE TPM backend driver"; | ||
204 | } | ||
205 | |||
206 | +static void tpm_cuse_pre_save(void *opaque) | ||
207 | +{ | ||
208 | + TPMPassthruState *tpm_pt = opaque; | ||
209 | + TPMBackend *tb = &tpm_pt->parent; | ||
210 | + | ||
211 | + qemu_mutex_lock(&tpm_pt->state_lock); | ||
212 | + /* wait for TPM to finish processing */ | ||
213 | + if (tpm_pt->tpm_busy) { | ||
214 | + qemu_cond_wait(&tpm_pt->cmd_complete, &tpm_pt->state_lock); | ||
215 | + } | ||
216 | + qemu_mutex_unlock(&tpm_pt->state_lock); | ||
217 | + | ||
218 | + /* get the decrypted state blobs from the TPM */ | ||
219 | + tpm_cuse_get_state_blobs(tb, TRUE, &tpm_pt->tpm_blobs); | ||
220 | +} | ||
221 | + | ||
222 | +static int tpm_cuse_post_load(void *opaque, | ||
223 | + int version_id __attribute__((unused))) | ||
224 | +{ | ||
225 | + TPMPassthruState *tpm_pt = opaque; | ||
226 | + TPMBackend *tb = &tpm_pt->parent; | ||
227 | + | ||
228 | + return tpm_cuse_set_state_blobs(tb, &tpm_pt->tpm_blobs); | ||
229 | +} | ||
230 | + | ||
231 | +static const VMStateDescription vmstate_tpm_cuse = { | ||
232 | + .name = "cuse-tpm", | ||
233 | + .version_id = 1, | ||
234 | + .minimum_version_id = 0, | ||
235 | + .minimum_version_id_old = 0, | ||
236 | + .pre_save = tpm_cuse_pre_save, | ||
237 | + .post_load = tpm_cuse_post_load, | ||
238 | + .fields = (VMStateField[]) { | ||
239 | + VMSTATE_UINT32(tpm_blobs.permanent_flags, TPMPassthruState), | ||
240 | + VMSTATE_UINT32(tpm_blobs.permanent.size, TPMPassthruState), | ||
241 | + VMSTATE_VBUFFER_ALLOC_UINT32(tpm_blobs.permanent.buffer, | ||
242 | + TPMPassthruState, 1, NULL, 0, | ||
243 | + tpm_blobs.permanent.size), | ||
244 | + | ||
245 | + VMSTATE_UINT32(tpm_blobs.volatil_flags, TPMPassthruState), | ||
246 | + VMSTATE_UINT32(tpm_blobs.volatil.size, TPMPassthruState), | ||
247 | + VMSTATE_VBUFFER_ALLOC_UINT32(tpm_blobs.volatil.buffer, | ||
248 | + TPMPassthruState, 1, NULL, 0, | ||
249 | + tpm_blobs.volatil.size), | ||
250 | + | ||
251 | + VMSTATE_UINT32(tpm_blobs.savestate_flags, TPMPassthruState), | ||
252 | + VMSTATE_UINT32(tpm_blobs.savestate.size, TPMPassthruState), | ||
253 | + VMSTATE_VBUFFER_ALLOC_UINT32(tpm_blobs.savestate.buffer, | ||
254 | + TPMPassthruState, 1, NULL, 0, | ||
255 | + tpm_blobs.savestate.size), | ||
256 | + VMSTATE_END_OF_LIST() | ||
257 | + } | ||
258 | +}; | ||
259 | + | ||
260 | static const TPMDriverOps tpm_cuse_driver = { | ||
261 | .type = TPM_TYPE_CUSE_TPM, | ||
262 | .opts = tpm_passthrough_cmdline_opts, | ||
263 | diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c | ||
264 | index 14d9e83ea2..9b660cf737 100644 | ||
265 | --- a/hw/tpm/tpm_tis.c | ||
266 | +++ b/hw/tpm/tpm_tis.c | ||
267 | @@ -368,6 +368,8 @@ static void tpm_tis_receive_bh(void *opaque) | ||
268 | TPMTISEmuState *tis = &s->s.tis; | ||
269 | uint8_t locty = s->locty_number; | ||
270 | |||
271 | + tis->bh_scheduled = false; | ||
272 | + | ||
273 | qemu_mutex_lock(&s->state_lock); | ||
274 | |||
275 | tpm_tis_sts_set(&tis->loc[locty], | ||
276 | @@ -415,6 +417,8 @@ static void tpm_tis_receive_cb(TPMState *s, uint8_t locty, | ||
277 | qemu_mutex_unlock(&s->state_lock); | ||
278 | |||
279 | qemu_bh_schedule(tis->bh); | ||
280 | + | ||
281 | + tis->bh_scheduled = true; | ||
282 | } | ||
283 | |||
284 | /* | ||
285 | @@ -1030,9 +1034,140 @@ static void tpm_tis_reset(DeviceState *dev) | ||
286 | tpm_tis_do_startup_tpm(s); | ||
287 | } | ||
288 | |||
289 | + | ||
290 | +/* persistent state handling */ | ||
291 | + | ||
292 | +static void tpm_tis_pre_save(void *opaque) | ||
293 | +{ | ||
294 | + TPMState *s = opaque; | ||
295 | + TPMTISEmuState *tis = &s->s.tis; | ||
296 | + uint8_t locty = tis->active_locty; | ||
297 | + | ||
298 | + DPRINTF("tpm_tis: suspend: locty = %d : r_offset = %d, w_offset = %d\n", | ||
299 | + locty, tis->loc[0].r_offset, tis->loc[0].w_offset); | ||
300 | +#ifdef DEBUG_TIS | ||
301 | + tpm_tis_dump_state(opaque, 0); | ||
302 | +#endif | ||
303 | + | ||
304 | + qemu_mutex_lock(&s->state_lock); | ||
305 | + | ||
306 | + /* wait for outstanding request to complete */ | ||
307 | + if (TPM_TIS_IS_VALID_LOCTY(locty) && | ||
308 | + tis->loc[locty].state == TPM_TIS_STATE_EXECUTION) { | ||
309 | + /* | ||
310 | + * If we get here when the bh is scheduled but did not run, | ||
311 | + * we won't get notified... | ||
312 | + */ | ||
313 | + if (!tis->bh_scheduled) { | ||
314 | + /* backend thread to notify us */ | ||
315 | + qemu_cond_wait(&s->cmd_complete, &s->state_lock); | ||
316 | + } | ||
317 | + if (tis->loc[locty].state == TPM_TIS_STATE_EXECUTION) { | ||
318 | + /* bottom half did not run - run its function */ | ||
319 | + qemu_mutex_unlock(&s->state_lock); | ||
320 | + tpm_tis_receive_bh(opaque); | ||
321 | + qemu_mutex_lock(&s->state_lock); | ||
322 | + } | ||
323 | + } | ||
324 | + | ||
325 | + qemu_mutex_unlock(&s->state_lock); | ||
326 | + | ||
327 | + /* copy current active read or write buffer into the buffer | ||
328 | + written to disk */ | ||
329 | + if (TPM_TIS_IS_VALID_LOCTY(locty)) { | ||
330 | + switch (tis->loc[locty].state) { | ||
331 | + case TPM_TIS_STATE_RECEPTION: | ||
332 | + memcpy(tis->buf, | ||
333 | + tis->loc[locty].w_buffer.buffer, | ||
334 | + MIN(sizeof(tis->buf), | ||
335 | + tis->loc[locty].w_buffer.size)); | ||
336 | + tis->offset = tis->loc[locty].w_offset; | ||
337 | + break; | ||
338 | + case TPM_TIS_STATE_COMPLETION: | ||
339 | + memcpy(tis->buf, | ||
340 | + tis->loc[locty].r_buffer.buffer, | ||
341 | + MIN(sizeof(tis->buf), | ||
342 | + tis->loc[locty].r_buffer.size)); | ||
343 | + tis->offset = tis->loc[locty].r_offset; | ||
344 | + break; | ||
345 | + default: | ||
346 | + /* leak nothing */ | ||
347 | + memset(tis->buf, 0x0, sizeof(tis->buf)); | ||
348 | + break; | ||
349 | + } | ||
350 | + } | ||
351 | +} | ||
352 | + | ||
353 | +static int tpm_tis_post_load(void *opaque, | ||
354 | + int version_id __attribute__((unused))) | ||
355 | +{ | ||
356 | + TPMState *s = opaque; | ||
357 | + TPMTISEmuState *tis = &s->s.tis; | ||
358 | + | ||
359 | + uint8_t locty = tis->active_locty; | ||
360 | + | ||
361 | + if (TPM_TIS_IS_VALID_LOCTY(locty)) { | ||
362 | + switch (tis->loc[locty].state) { | ||
363 | + case TPM_TIS_STATE_RECEPTION: | ||
364 | + memcpy(tis->loc[locty].w_buffer.buffer, | ||
365 | + tis->buf, | ||
366 | + MIN(sizeof(tis->buf), | ||
367 | + tis->loc[locty].w_buffer.size)); | ||
368 | + tis->loc[locty].w_offset = tis->offset; | ||
369 | + break; | ||
370 | + case TPM_TIS_STATE_COMPLETION: | ||
371 | + memcpy(tis->loc[locty].r_buffer.buffer, | ||
372 | + tis->buf, | ||
373 | + MIN(sizeof(tis->buf), | ||
374 | + tis->loc[locty].r_buffer.size)); | ||
375 | + tis->loc[locty].r_offset = tis->offset; | ||
376 | + break; | ||
377 | + default: | ||
378 | + break; | ||
379 | + } | ||
380 | + } | ||
381 | + | ||
382 | + DPRINTF("tpm_tis: resume : locty = %d : r_offset = %d, w_offset = %d\n", | ||
383 | + locty, tis->loc[0].r_offset, tis->loc[0].w_offset); | ||
384 | + | ||
385 | + return 0; | ||
386 | +} | ||
387 | + | ||
388 | +static const VMStateDescription vmstate_locty = { | ||
389 | + .name = "loc", | ||
390 | + .version_id = 1, | ||
391 | + .minimum_version_id = 0, | ||
392 | + .minimum_version_id_old = 0, | ||
393 | + .fields = (VMStateField[]) { | ||
394 | + VMSTATE_UINT32(state, TPMLocality), | ||
395 | + VMSTATE_UINT32(inte, TPMLocality), | ||
396 | + VMSTATE_UINT32(ints, TPMLocality), | ||
397 | + VMSTATE_UINT8(access, TPMLocality), | ||
398 | + VMSTATE_UINT32(sts, TPMLocality), | ||
399 | + VMSTATE_UINT32(iface_id, TPMLocality), | ||
400 | + VMSTATE_END_OF_LIST(), | ||
401 | + } | ||
402 | +}; | ||
403 | + | ||
404 | static const VMStateDescription vmstate_tpm_tis = { | ||
405 | .name = "tpm", | ||
406 | - .unmigratable = 1, | ||
407 | + .version_id = 1, | ||
408 | + .minimum_version_id = 0, | ||
409 | + .minimum_version_id_old = 0, | ||
410 | + .pre_save = tpm_tis_pre_save, | ||
411 | + .post_load = tpm_tis_post_load, | ||
412 | + .fields = (VMStateField[]) { | ||
413 | + VMSTATE_UINT32(s.tis.offset, TPMState), | ||
414 | + VMSTATE_BUFFER(s.tis.buf, TPMState), | ||
415 | + VMSTATE_UINT8(s.tis.active_locty, TPMState), | ||
416 | + VMSTATE_UINT8(s.tis.aborting_locty, TPMState), | ||
417 | + VMSTATE_UINT8(s.tis.next_locty, TPMState), | ||
418 | + | ||
419 | + VMSTATE_STRUCT_ARRAY(s.tis.loc, TPMState, TPM_TIS_NUM_LOCALITIES, 1, | ||
420 | + vmstate_locty, TPMLocality), | ||
421 | + | ||
422 | + VMSTATE_END_OF_LIST() | ||
423 | + } | ||
424 | }; | ||
425 | |||
426 | static Property tpm_tis_properties[] = { | ||
427 | diff --git a/hw/tpm/tpm_tis.h b/hw/tpm/tpm_tis.h | ||
428 | index a1df41fa21..b7fc0ea1a9 100644 | ||
429 | --- a/hw/tpm/tpm_tis.h | ||
430 | +++ b/hw/tpm/tpm_tis.h | ||
431 | @@ -54,6 +54,8 @@ typedef struct TPMLocality { | ||
432 | |||
433 | typedef struct TPMTISEmuState { | ||
434 | QEMUBH *bh; | ||
435 | + bool bh_scheduled; /* bh scheduled but did not run yet */ | ||
436 | + | ||
437 | uint32_t offset; | ||
438 | uint8_t buf[TPM_TIS_BUFFER_MAX]; | ||
439 | |||
440 | diff --git a/hw/tpm/tpm_util.c b/hw/tpm/tpm_util.c | ||
441 | index 7b35429725..b6ff74d946 100644 | ||
442 | --- a/hw/tpm/tpm_util.c | ||
443 | +++ b/hw/tpm/tpm_util.c | ||
444 | @@ -22,6 +22,17 @@ | ||
445 | #include "qemu/osdep.h" | ||
446 | #include "tpm_util.h" | ||
447 | #include "tpm_int.h" | ||
448 | +#include "tpm_ioctl.h" | ||
449 | +#include "qemu/error-report.h" | ||
450 | + | ||
451 | +#define DEBUG_TPM 0 | ||
452 | + | ||
453 | +#define DPRINTF(fmt, ...) do { \ | ||
454 | + if (DEBUG_TPM) { \ | ||
455 | + fprintf(stderr, fmt, ## __VA_ARGS__); \ | ||
456 | + } \ | ||
457 | +} while (0) | ||
458 | + | ||
459 | |||
460 | /* | ||
461 | * A basic test of a TPM device. We expect a well formatted response header | ||
462 | @@ -125,3 +136,215 @@ int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version) | ||
463 | |||
464 | return 1; | ||
465 | } | ||
466 | + | ||
467 | +static void tpm_sized_buffer_reset(TPMSizedBuffer *tsb) | ||
468 | +{ | ||
469 | + g_free(tsb->buffer); | ||
470 | + tsb->buffer = NULL; | ||
471 | + tsb->size = 0; | ||
472 | +} | ||
473 | + | ||
474 | +/* | ||
475 | + * Transfer a TPM state blob from the TPM into a provided buffer. | ||
476 | + * | ||
477 | + * @fd: file descriptor to talk to the CUSE TPM | ||
478 | + * @type: the type of blob to transfer | ||
479 | + * @decrypted_blob: whether we request to receive decrypted blobs | ||
480 | + * @tsb: the TPMSizeBuffer to fill with the blob | ||
481 | + * @flags: the flags to return to the caller | ||
482 | + */ | ||
483 | +static int tpm_util_cuse_get_state_blob(int fd, | ||
484 | + uint8_t type, | ||
485 | + bool decrypted_blob, | ||
486 | + TPMSizedBuffer *tsb, | ||
487 | + uint32_t *flags) | ||
488 | +{ | ||
489 | + ptm_getstate pgs; | ||
490 | + uint16_t offset = 0; | ||
491 | + ptm_res res; | ||
492 | + ssize_t n; | ||
493 | + size_t to_read; | ||
494 | + | ||
495 | + tpm_sized_buffer_reset(tsb); | ||
496 | + | ||
497 | + pgs.u.req.state_flags = (decrypted_blob) ? PTM_STATE_FLAG_DECRYPTED : 0; | ||
498 | + pgs.u.req.type = type; | ||
499 | + pgs.u.req.offset = offset; | ||
500 | + | ||
501 | + if (ioctl(fd, PTM_GET_STATEBLOB, &pgs) < 0) { | ||
502 | + error_report("CUSE TPM PTM_GET_STATEBLOB ioctl failed: %s", | ||
503 | + strerror(errno)); | ||
504 | + goto err_exit; | ||
505 | + } | ||
506 | + res = pgs.u.resp.tpm_result; | ||
507 | + if (res != 0 && (res & 0x800) == 0) { | ||
508 | + error_report("Getting the stateblob (type %d) failed with a TPM " | ||
509 | + "error 0x%x", type, res); | ||
510 | + goto err_exit; | ||
511 | + } | ||
512 | + | ||
513 | + *flags = pgs.u.resp.state_flags; | ||
514 | + | ||
515 | + tsb->buffer = g_malloc(pgs.u.resp.totlength); | ||
516 | + memcpy(tsb->buffer, pgs.u.resp.data, pgs.u.resp.length); | ||
517 | + tsb->size = pgs.u.resp.length; | ||
518 | + | ||
519 | + /* if there are bytes left to get use read() interface */ | ||
520 | + while (tsb->size < pgs.u.resp.totlength) { | ||
521 | + to_read = pgs.u.resp.totlength - tsb->size; | ||
522 | + if (unlikely(to_read > SSIZE_MAX)) { | ||
523 | + to_read = SSIZE_MAX; | ||
524 | + } | ||
525 | + | ||
526 | + n = read(fd, &tsb->buffer[tsb->size], to_read); | ||
527 | + if (n != to_read) { | ||
528 | + error_report("Could not read stateblob (type %d) : %s", | ||
529 | + type, strerror(errno)); | ||
530 | + goto err_exit; | ||
531 | + } | ||
532 | + tsb->size += to_read; | ||
533 | + } | ||
534 | + | ||
535 | + DPRINTF("tpm_util: got state blob type %d, %d bytes, flags 0x%08x, " | ||
536 | + "decrypted=%d\n", type, tsb->size, *flags, decrypted_blob); | ||
537 | + | ||
538 | + return 0; | ||
539 | + | ||
540 | +err_exit: | ||
541 | + return 1; | ||
542 | +} | ||
543 | + | ||
544 | +int tpm_util_cuse_get_state_blobs(int tpm_fd, | ||
545 | + bool decrypted_blobs, | ||
546 | + TPMBlobBuffers *tpm_blobs) | ||
547 | +{ | ||
548 | + if (tpm_util_cuse_get_state_blob(tpm_fd, PTM_BLOB_TYPE_PERMANENT, | ||
549 | + decrypted_blobs, | ||
550 | + &tpm_blobs->permanent, | ||
551 | + &tpm_blobs->permanent_flags) || | ||
552 | + tpm_util_cuse_get_state_blob(tpm_fd, PTM_BLOB_TYPE_VOLATILE, | ||
553 | + decrypted_blobs, | ||
554 | + &tpm_blobs->volatil, | ||
555 | + &tpm_blobs->volatil_flags) || | ||
556 | + tpm_util_cuse_get_state_blob(tpm_fd, PTM_BLOB_TYPE_SAVESTATE, | ||
557 | + decrypted_blobs, | ||
558 | + &tpm_blobs->savestate, | ||
559 | + &tpm_blobs->savestate_flags)) { | ||
560 | + goto err_exit; | ||
561 | + } | ||
562 | + | ||
563 | + return 0; | ||
564 | + | ||
565 | + err_exit: | ||
566 | + tpm_sized_buffer_reset(&tpm_blobs->volatil); | ||
567 | + tpm_sized_buffer_reset(&tpm_blobs->permanent); | ||
568 | + tpm_sized_buffer_reset(&tpm_blobs->savestate); | ||
569 | + | ||
570 | + return 1; | ||
571 | +} | ||
572 | + | ||
573 | +static int tpm_util_cuse_do_set_stateblob_ioctl(int fd, | ||
574 | + uint32_t flags, | ||
575 | + uint32_t type, | ||
576 | + uint32_t length) | ||
577 | +{ | ||
578 | + ptm_setstate pss; | ||
579 | + | ||
580 | + pss.u.req.state_flags = flags; | ||
581 | + pss.u.req.type = type; | ||
582 | + pss.u.req.length = length; | ||
583 | + | ||
584 | + if (ioctl(fd, PTM_SET_STATEBLOB, &pss) < 0) { | ||
585 | + error_report("CUSE TPM PTM_SET_STATEBLOB ioctl failed: %s", | ||
586 | + strerror(errno)); | ||
587 | + return 1; | ||
588 | + } | ||
589 | + | ||
590 | + if (pss.u.resp.tpm_result != 0) { | ||
591 | + error_report("Setting the stateblob (type %d) failed with a TPM " | ||
592 | + "error 0x%x", type, pss.u.resp.tpm_result); | ||
593 | + return 1; | ||
594 | + } | ||
595 | + | ||
596 | + return 0; | ||
597 | +} | ||
598 | + | ||
599 | + | ||
600 | +/* | ||
601 | + * Transfer a TPM state blob to the CUSE TPM. | ||
602 | + * | ||
603 | + * @fd: file descriptor to talk to the CUSE TPM | ||
604 | + * @type: the type of TPM state blob to transfer | ||
605 | + * @tsb: TPMSizeBuffer containing the TPM state blob | ||
606 | + * @flags: Flags describing the (encryption) state of the TPM state blob | ||
607 | + */ | ||
608 | +static int tpm_util_cuse_set_state_blob(int fd, | ||
609 | + uint32_t type, | ||
610 | + TPMSizedBuffer *tsb, | ||
611 | + uint32_t flags) | ||
612 | +{ | ||
613 | + uint32_t offset = 0; | ||
614 | + ssize_t n; | ||
615 | + size_t to_write; | ||
616 | + | ||
617 | + /* initiate the transfer to the CUSE TPM */ | ||
618 | + if (tpm_util_cuse_do_set_stateblob_ioctl(fd, flags, type, 0)) { | ||
619 | + return 1; | ||
620 | + } | ||
621 | + | ||
622 | + /* use the write() interface for transferring the state blob */ | ||
623 | + while (offset < tsb->size) { | ||
624 | + to_write = tsb->size - offset; | ||
625 | + if (unlikely(to_write > SSIZE_MAX)) { | ||
626 | + to_write = SSIZE_MAX; | ||
627 | + } | ||
628 | + | ||
629 | + n = write(fd, &tsb->buffer[offset], to_write); | ||
630 | + if (n != to_write) { | ||
631 | + error_report("Writing the stateblob (type %d) failed: %s", | ||
632 | + type, strerror(errno)); | ||
633 | + goto err_exit; | ||
634 | + } | ||
635 | + offset += to_write; | ||
636 | + } | ||
637 | + | ||
638 | + /* inidicate that the transfer is finished */ | ||
639 | + if (tpm_util_cuse_do_set_stateblob_ioctl(fd, flags, type, 0)) { | ||
640 | + goto err_exit; | ||
641 | + } | ||
642 | + | ||
643 | + DPRINTF("tpm_util: set the state blob type %d, %d bytes, flags 0x%08x\n", | ||
644 | + type, tsb->size, flags); | ||
645 | + | ||
646 | + return 0; | ||
647 | + | ||
648 | +err_exit: | ||
649 | + return 1; | ||
650 | +} | ||
651 | + | ||
652 | +int tpm_util_cuse_set_state_blobs(int tpm_fd, | ||
653 | + TPMBlobBuffers *tpm_blobs) | ||
654 | +{ | ||
655 | + ptm_res res; | ||
656 | + | ||
657 | + if (ioctl(tpm_fd, PTM_STOP, &res) < 0) { | ||
658 | + error_report("tpm_passthrough: Could not stop " | ||
659 | + "the CUSE TPM: %s (%i)", | ||
660 | + strerror(errno), errno); | ||
661 | + return 1; | ||
662 | + } | ||
663 | + | ||
664 | + if (tpm_util_cuse_set_state_blob(tpm_fd, PTM_BLOB_TYPE_PERMANENT, | ||
665 | + &tpm_blobs->permanent, | ||
666 | + tpm_blobs->permanent_flags) || | ||
667 | + tpm_util_cuse_set_state_blob(tpm_fd, PTM_BLOB_TYPE_VOLATILE, | ||
668 | + &tpm_blobs->volatil, | ||
669 | + tpm_blobs->volatil_flags) || | ||
670 | + tpm_util_cuse_set_state_blob(tpm_fd, PTM_BLOB_TYPE_SAVESTATE, | ||
671 | + &tpm_blobs->savestate, | ||
672 | + tpm_blobs->savestate_flags)) { | ||
673 | + return 1; | ||
674 | + } | ||
675 | + | ||
676 | + return 0; | ||
677 | +} | ||
678 | diff --git a/hw/tpm/tpm_util.h b/hw/tpm/tpm_util.h | ||
679 | index df76245e6e..c24071d812 100644 | ||
680 | --- a/hw/tpm/tpm_util.h | ||
681 | +++ b/hw/tpm/tpm_util.h | ||
682 | @@ -26,4 +26,11 @@ | ||
683 | |||
684 | int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version); | ||
685 | |||
686 | +int tpm_util_cuse_get_state_blobs(int tpm_fd, | ||
687 | + bool decrypted_blobs, | ||
688 | + TPMBlobBuffers *tpm_blobs); | ||
689 | + | ||
690 | +int tpm_util_cuse_set_state_blobs(int tpm_fd, | ||
691 | + TPMBlobBuffers *tpm_blobs); | ||
692 | + | ||
693 | #endif /* TPM_TPM_UTIL_H */ | ||
694 | diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h | ||
695 | index b58f52d39f..3403821b9d 100644 | ||
696 | --- a/include/sysemu/tpm_backend.h | ||
697 | +++ b/include/sysemu/tpm_backend.h | ||
698 | @@ -62,6 +62,18 @@ typedef struct TPMSizedBuffer { | ||
699 | uint8_t *buffer; | ||
700 | } TPMSizedBuffer; | ||
701 | |||
702 | +/* blobs from the TPM; part of VM state when migrating */ | ||
703 | +typedef struct TPMBlobBuffers { | ||
704 | + uint32_t permanent_flags; | ||
705 | + TPMSizedBuffer permanent; | ||
706 | + | ||
707 | + uint32_t volatil_flags; | ||
708 | + TPMSizedBuffer volatil; | ||
709 | + | ||
710 | + uint32_t savestate_flags; | ||
711 | + TPMSizedBuffer savestate; | ||
712 | +} TPMBlobBuffers; | ||
713 | + | ||
714 | struct TPMDriverOps { | ||
715 | enum TpmType type; | ||
716 | const QemuOptDesc *opts; | ||
717 | -- | ||
718 | 2.11.0 | ||
719 | |||
diff --git a/meta/recipes-devtools/qemu/qemu_2.8.0.bb b/meta/recipes-devtools/qemu/qemu_2.8.0.bb index e0527a8fd9..f25aa467e5 100644 --- a/meta/recipes-devtools/qemu/qemu_2.8.0.bb +++ b/meta/recipes-devtools/qemu/qemu_2.8.0.bb | |||
@@ -11,6 +11,13 @@ SRC_URI += "file://configure-fix-Darwin-target-detection.patch \ | |||
11 | file://target-ppc-fix-user-mode.patch \ | 11 | file://target-ppc-fix-user-mode.patch \ |
12 | " | 12 | " |
13 | 13 | ||
14 | SRC_URI += " \ | ||
15 | file://0001-Provide-support-for-the-CUSE-TPM.patch \ | ||
16 | file://0002-Introduce-condition-to-notify-waiters-of-completed-c.patch \ | ||
17 | file://0003-Introduce-condition-in-TPM-backend-for-notification.patch \ | ||
18 | file://0004-Add-support-for-VM-suspend-resume-for-TPM-TIS.patch \ | ||
19 | " | ||
20 | |||
14 | SRC_URI =+ "http://wiki.qemu-project.org/download/${BP}.tar.bz2" | 21 | SRC_URI =+ "http://wiki.qemu-project.org/download/${BP}.tar.bz2" |
15 | 22 | ||
16 | SRC_URI[md5sum] = "17940dce063b6ce450a12e719a6c9c43" | 23 | SRC_URI[md5sum] = "17940dce063b6ce450a12e719a6c9c43" |