* [PATCH 3/4] tools/libxl: add emulated TPM support backed by swtpm
2026-11-06 23:15 [PATCH 0/4] libxl TPM emulator support Jason Andryuk
2026-11-06 23:15 ` [PATCH 1/4] libacpi: Build TPM2 tables for the TIS interface Jason Andryuk
2026-11-06 23:15 ` [PATCH 2/4] hvmloader: Support the TIS interface for TPM 2.0 Jason Andryuk
@ 2026-11-06 23:15 ` Jason Andryuk
2026-11-09 19:54 ` Nick Rosbrook
2026-11-06 23:15 ` [PATCH 4/4] libxl: Add emulated TPM support on ARM Jason Andryuk
3 siblings, 1 reply; 10+ messages in thread
From: Jason Andryuk @ 2026-11-06 23:15 UTC (permalink / raw)
To: xen-devel
Cc: Jason Andryuk, Anthony PERARD, Nick Rosbrook, George Dunlap,
Juergen Gross
Add support for an emulated TPM. QEMU emulates the device and uses
swtpm for the backend. swtpm is started first and QEMU connects to it.
Add the xl.cfg options are:
tpm_version
tpm_statedir
tpm_statefile
tpm_interface
tpm_loglevel
tpm_swtpm_override
When tpm_version is absent, no TPM is configured. tpm_version must be 1
or 2, and exactly one of tpm_statedir or tpm_statefile must be
specified. tpm_statefile is only supported for swtpm 0.7 and later, so
tpm_statedir is provided for compatiblity. tpm_interface can be either
tis or crb. tpm_loglevel is passed to swtpm when non-zero, with swtpm
logging to /var/log/xen/swtpm-<domain>.log. tpm_swtpm_override specifies
the swtpm executable path; otherwise swtpm is found via PATH.
On x86, TPM 1.2 only supports TIS, while TPM 2.0 may use either and
defaults to CRB.
libxl typically tracks spawned daemons through xenstore keys - like QEMU
writing its "state" key as "running". swtpm does not write to
xenstores, so that is not used. Instead the daemon is launched first
and then QEMU launches and connects to it. If there is a failure,
the QEMU log messages will list tpm in the error message.
`interface` is a Go keyword, so the binding generator needs to specify
that.
Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
---
docs/man/xl.cfg.5.pod.in | 36 ++++++
tools/golang/xenlight/gengotypes.py | 2 +-
tools/golang/xenlight/helpers.gen.go | 48 ++++++++
tools/golang/xenlight/types.gen.go | 17 +++
tools/include/libxl.h | 10 ++
tools/libs/light/libxl_create.c | 46 +++++++-
tools/libs/light/libxl_dm.c | 161 +++++++++++++++++++++++++++
tools/libs/light/libxl_domain.c | 3 +
tools/libs/light/libxl_internal.h | 4 +
tools/libs/light/libxl_types.idl | 17 +++
tools/libs/light/libxl_x86.c | 18 +++
tools/xl/xl_parse.c | 20 ++++
12 files changed, 380 insertions(+), 2 deletions(-)
diff --git a/docs/man/xl.cfg.5.pod.in b/docs/man/xl.cfg.5.pod.in
index b19c25c8d1..c12d2dc4db 100644
--- a/docs/man/xl.cfg.5.pod.in
+++ b/docs/man/xl.cfg.5.pod.in
@@ -821,6 +821,42 @@ value is optional if this is a guest domain.
=back
+=item B<tpm_version=NUMBER>
+
+Provides an emulated TPM to a QEMU-based HVM guest, backed by B<swtpm(8)>.
+The TPM version to emulate is either B<1> for TPM 1.2 or B<2> for TPM 2.0.
+When this option is not specified, no TPM is provided and the other
+B<tpm_*> options are ignored.
+
+One of B<tpm_statefile> or B<tpm_statedir> is required, but not both.
+
+=item B<tpm_statefile="PATH">
+
+Path to a single file in which B<swtpm> stores the persistent TPM state.
+This uses the C<--tpmstate backend-uri=file://PATH> option added in swtpm
+version 0.7.0.
+
+=item B<tpm_statedir="PATH">
+
+Path to a directory in which B<swtpm> stores the persistent TPM state.
+
+=item B<tpm_interface="tis"|"crb">
+
+The TPM interface presented to the guest. B<tis> is the TPM Interface
+Specification (FIFO) interface and B<crb> is the Command Response Buffer
+interface. B<tpm_version=1> only supports B<tis>, while B<tpm_version=2>
+supports either and defaults to B<crb>.
+
+=item B<tpm_swtpm_override="PATH">
+
+Path to the B<swtpm> executable. By default, B<swtpm> is searched for in
+B<PATH>.
+
+=item B<tpm_loglevel=NUMBER>
+
+The B<swtpm> log level. When non-zero, B<swtpm> logs to
+F</var/log/xen/swtpm-DOMAIN.log>. Defaults to B<0> (no log).
+
=item B<p9=[ "9PFS_SPEC_STRING", "9PFS_SPEC_STRING", ...]>
Creates a Xen 9pfs connection to share a filesystem from the backend to the
diff --git a/tools/golang/xenlight/gengotypes.py b/tools/golang/xenlight/gengotypes.py
index e4eb7ca1c1..628772d45d 100644
--- a/tools/golang/xenlight/gengotypes.py
+++ b/tools/golang/xenlight/gengotypes.py
@@ -28,7 +28,7 @@ builtin_type_names = {
}
# Some go keywords that conflict with field names in libxl structs.
-go_keywords = ['type', 'func']
+go_keywords = ['type', 'func', 'interface']
go_builtin_types = ['bool', 'string', 'int', 'byte',
'uint16', 'uint32', 'uint64']
diff --git a/tools/golang/xenlight/helpers.gen.go b/tools/golang/xenlight/helpers.gen.go
index 6ab23a708f..58e6179374 100644
--- a/tools/golang/xenlight/helpers.gen.go
+++ b/tools/golang/xenlight/helpers.gen.go
@@ -1076,6 +1076,48 @@ return fmt.Errorf("converting field Quota: %v", err)
return nil
}
+// NewTpm returns an instance of Tpm initialized with defaults.
+func NewTpm() (*Tpm, error) {
+var (
+x Tpm
+xc C.libxl_tpm)
+
+C.libxl_tpm_init(&xc)
+defer C.libxl_tpm_dispose(&xc)
+
+if err := x.fromC(&xc); err != nil {
+return nil, err }
+
+return &x, nil}
+
+func (x *Tpm) fromC(xc *C.libxl_tpm) error {
+ x.Statefile = C.GoString(xc.statefile)
+x.Statedir = C.GoString(xc.statedir)
+x.SwtpmOverride = C.GoString(xc.swtpm_override)
+x.Version = int(xc.version)
+x.Loglevel = int(xc.loglevel)
+x.Interface = TpmInterface(xc._interface)
+
+ return nil}
+
+func (x *Tpm) toC(xc *C.libxl_tpm) (err error){defer func(){
+if err != nil{
+C.libxl_tpm_dispose(xc)}
+}()
+
+if x.Statefile != "" {
+xc.statefile = C.CString(x.Statefile)}
+if x.Statedir != "" {
+xc.statedir = C.CString(x.Statedir)}
+if x.SwtpmOverride != "" {
+xc.swtpm_override = C.CString(x.SwtpmOverride)}
+xc.version = C.int(x.Version)
+xc.loglevel = C.int(x.Loglevel)
+xc._interface = C.libxl_tpm_interface(x.Interface)
+
+ return nil
+ }
+
// NewDomainBuildInfo returns an instance of DomainBuildInfo initialized with defaults.
func NewDomainBuildInfo(dtype DomainType) (*DomainBuildInfo, error) {
var (
@@ -1242,6 +1284,9 @@ if err := x.DmRestrict.fromC(&xc.dm_restrict);err != nil {
return fmt.Errorf("converting field DmRestrict: %v", err)
}
x.Tee = TeeType(xc.tee)
+if err := x.Tpm.fromC(&xc.tpm);err != nil {
+return fmt.Errorf("converting field Tpm: %v", err)
+}
x.Type = DomainType(xc._type)
switch x.Type{
case DomainTypeHvm:
@@ -1626,6 +1671,9 @@ if err := x.DmRestrict.toC(&xc.dm_restrict); err != nil {
return fmt.Errorf("converting field DmRestrict: %v", err)
}
xc.tee = C.libxl_tee_type(x.Tee)
+if err := x.Tpm.toC(&xc.tpm); err != nil {
+return fmt.Errorf("converting field Tpm: %v", err)
+}
xc._type = C.libxl_domain_type(x.Type)
switch x.Type{
case DomainTypeHvm:
diff --git a/tools/golang/xenlight/types.gen.go b/tools/golang/xenlight/types.gen.go
index 39bf9962d1..55b02ffcd7 100644
--- a/tools/golang/xenlight/types.gen.go
+++ b/tools/golang/xenlight/types.gen.go
@@ -553,6 +553,22 @@ type XsQuotaList struct {
Quota []XsQuotaItem
}
+type TpmInterface int
+const(
+TpmInterfaceDefault TpmInterface = 0
+TpmInterfaceTis TpmInterface = 1
+TpmInterfaceCrb TpmInterface = 2
+)
+
+type Tpm struct {
+Statefile string
+Statedir string
+SwtpmOverride string
+Version int
+Loglevel int
+Interface TpmInterface
+}
+
type DomainBuildInfo struct {
MaxVcpus int
AvailVcpus Bitmap
@@ -612,6 +628,7 @@ NestedHvm Defbool
Apic Defbool
DmRestrict Defbool
Tee TeeType
+Tpm Tpm
Type DomainType
TypeUnion DomainBuildInfoTypeUnion
ArchArm struct {
diff --git a/tools/include/libxl.h b/tools/include/libxl.h
index d294fb36bd..8b431b708d 100644
--- a/tools/include/libxl.h
+++ b/tools/include/libxl.h
@@ -1563,6 +1563,16 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
*/
#define LIBXL_HAVE_XENSTORE_QUOTA
+/*
+ * LIBXL_HAVE_TPM
+ *
+ * If this is defined the tpm member of struct domain_build_info is
+ * available. When its version is non-zero, libxl starts swtpm to
+ * provide an emulated TPM to a QEMU-based HVM guest. Its interface
+ * member selects the TPM interface (TIS or CRB) presented to the guest.
+ */
+#define LIBXL_HAVE_TPM
+
typedef char **libxl_string_list;
void libxl_string_list_dispose(libxl_string_list *sl);
int libxl_string_list_length(const libxl_string_list *sl);
diff --git a/tools/libs/light/libxl_create.c b/tools/libs/light/libxl_create.c
index a8b0c8c500..94a2a0129f 100644
--- a/tools/libs/light/libxl_create.c
+++ b/tools/libs/light/libxl_create.c
@@ -230,6 +230,43 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
if (!b_info->event_channels)
b_info->event_channels = 1023;
+ if (b_info->tpm.version) {
+ if (b_info->tpm.version != 1 && b_info->tpm.version != 2) {
+ LOG(ERROR, "TPM version must be 1 or 2.");
+ return ERROR_INVAL;
+ }
+
+ if (b_info->tpm.statefile && !b_info->tpm.statefile[0]) {
+ LOG(ERROR, "TPM statefile must not be empty.");
+ return ERROR_INVAL;
+ }
+
+ if (b_info->tpm.statedir && !b_info->tpm.statedir[0]) {
+ LOG(ERROR, "TPM statedir must not be empty.");
+ return ERROR_INVAL;
+ }
+
+ if (b_info->tpm.swtpm_override && !b_info->tpm.swtpm_override[0]) {
+ LOG(ERROR, "TPM swtpm_override must not be empty.");
+ return ERROR_INVAL;
+ }
+
+ if (b_info->tpm.statefile && b_info->tpm.statedir) {
+ LOG(ERROR, "Only one of TPM statefile or statedir may be specified.");
+ return ERROR_INVAL;
+ }
+
+ if (!b_info->tpm.statefile && !b_info->tpm.statedir) {
+ LOG(ERROR, "TPM statefile or statedir must be specified.");
+ return ERROR_INVAL;
+ }
+
+ if (b_info->tpm.loglevel < 0) {
+ LOG(ERROR, "TPM loglevel must not be negative.");
+ return ERROR_INVAL;
+ }
+ }
+
rc = libxl_get_physinfo(CTX, &info);
if (rc) {
LOG(ERROR, "failed to get hypervisor info");
@@ -487,7 +524,7 @@ int libxl__domain_build(libxl__gc *gc,
vments[4] = "start_time";
vments[5] = GCSPRINTF("%jd.%02d", (intmax_t)start_time.tv_sec,(int)start_time.tv_usec/10000);
- localents = libxl__calloc(gc, 13, sizeof(char *));
+ localents = libxl__calloc(gc, 17, sizeof(char *));
i = 0;
localents[i++] = "platform/acpi";
localents[i++] = libxl__acpi_defbool_val(info) ? "1" : "0";
@@ -510,6 +547,13 @@ int libxl__domain_build(libxl__gc *gc,
}
localents[i++] = "platform/device-model";
localents[i++] = (char *)libxl_device_model_version_to_string(info->device_model_version);
+ if (info->tpm.version) {
+ localents[i++] = "platform/tpm_version";
+ localents[i++] = GCSPRINTF("%d", info->tpm.version);
+ localents[i++] = "platform/tpm-interface";
+ localents[i++] = (char *)libxl_tpm_interface_to_string(
+ info->tpm.interface);
+ }
break;
case LIBXL_DOMAIN_TYPE_PV:
diff --git a/tools/libs/light/libxl_dm.c b/tools/libs/light/libxl_dm.c
index 511ec76a65..843b8e29eb 100644
--- a/tools/libs/light/libxl_dm.c
+++ b/tools/libs/light/libxl_dm.c
@@ -931,6 +931,22 @@ out:
return rc;
}
+static const char *libxl__swtpm_socket_path(libxl__gc *gc, uint32_t domid)
+{
+ return GCSPRINTF("%s/swtpm-%u.sock", libxl__run_dir_path(), domid);
+}
+
+static void libxl__dm_add_tpm_args(libxl__gc *gc, flexarray_t *dm_args,
+ int guest_domid)
+{
+ const char *sock = libxl__swtpm_socket_path(gc, guest_domid);
+
+ flexarray_append_pair(dm_args, "-chardev",
+ GCSPRINTF("socket,id=chrtpm,path=%s", sock));
+ flexarray_append_pair(dm_args, "-tpmdev",
+ "emulator,id=tpm0,chardev=chrtpm");
+}
+
static int libxl__build_device_model_args_new(libxl__gc *gc,
const char *dm, int guest_domid,
const libxl_domain_config *guest_config,
@@ -1615,6 +1631,14 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
flexarray_append_pair(dm_envs, "XEN_DOMAIN_ID", GCSPRINTF("%d", guest_domid));
+ if (b_info->tpm.version) {
+ libxl__dm_add_tpm_args(gc, dm_args, guest_domid);
+ flexarray_append_pair(dm_args, "-device",
+ GCSPRINTF("%s,tpmdev=tpm0",
+ b_info->tpm.interface == LIBXL_TPM_INTERFACE_CRB ?
+ "tpm-crb" : "tpm-tis"));
+ }
+
if (b_info->u.hvm.hdtype == LIBXL_HDTYPE_AHCI)
flexarray_append_pair(dm_args, "-device", "ahci,id=ahci0");
for (i = 0; i < num_disks; i++) {
@@ -1980,6 +2004,75 @@ static int libxl__write_stub_dmargs(libxl__gc *gc,
return rc;
}
+/*
+ * Build the swtpm command line. swtpm is run in --daemon mode: the
+ * process we exec forks the actual daemon and exits once the control
+ * socket is ready, so QEMU can connect to it immediately afterwards.
+ */
+static char **libxl__swtpm_argv(libxl__gc *gc, uint32_t domid,
+ const libxl_domain_config *guest_config)
+{
+ const libxl_domain_build_info *b_info = &guest_config->b_info;
+ const libxl_domain_create_info *c_info = &guest_config->c_info;
+ const char *sock = libxl__swtpm_socket_path(gc, domid);
+ const char *pidfile = GCSPRINTF("%s/swtpm-%u.pid",
+ libxl__run_dir_path(), domid);
+ flexarray_t *args = flexarray_make(gc, 16, 1);
+ const char *tpmstate;
+
+ if (b_info->tpm.statefile)
+ tpmstate = GCSPRINTF("backend-uri=file://%s", b_info->tpm.statefile);
+ else
+ tpmstate = GCSPRINTF("dir=%s", b_info->tpm.statedir);
+
+ flexarray_vappend(args,
+ b_info->tpm.swtpm_override ?: "swtpm", "socket",
+ "--tpmstate", tpmstate,
+ "--ctrl", GCSPRINTF("type=unixio,path=%s", sock),
+ NULL);
+ if (b_info->tpm.version == 2)
+ flexarray_append(args, "--tpm2");
+ if (b_info->tpm.loglevel)
+ flexarray_vappend(args, "--log",
+ GCSPRINTF("file=%s/swtpm-%s.log,level=%d",
+ XEN_LOG_DIR, c_info->name, b_info->tpm.loglevel),
+ NULL);
+ if (b_info->device_model_user)
+ flexarray_vappend(args, "--runas", b_info->device_model_user, NULL);
+ flexarray_vappend(args, "--daemon",
+ "--pid", GCSPRINTF("file=%s", pidfile), NULL);
+ flexarray_append(args, NULL);
+
+ return (char **)flexarray_contents(args);
+}
+
+int libxl__destroy_swtpm(libxl__gc *gc, uint32_t domid)
+{
+ const char *pidfile = GCSPRINTF("%s/swtpm-%u.pid",
+ libxl__run_dir_path(), domid);
+ const char *sock = libxl__swtpm_socket_path(gc, domid);
+ void *data = NULL;
+ int datalen = 0;
+ pid_t pid;
+
+ /* No pidfile means swtpm was never started for this domain. */
+ if (libxl_read_file_contents(CTX, pidfile, &data, &datalen))
+ return 0;
+
+ pid = data ? atoi(libxl__strndup(gc, data, datalen)) : 0;
+ free(data);
+
+ if (pid > 0 && kill(pid, SIGTERM) && errno != ESRCH)
+ LOGED(ERROR, domid, "failed to kill swtpm [%d]", pid);
+
+ if (unlink(pidfile) && errno != ENOENT)
+ LOGED(ERROR, domid, "failed to remove %s", pidfile);
+ if (unlink(sock) && errno != ENOENT)
+ LOGED(ERROR, domid, "failed to remove %s", sock);
+
+ return 0;
+}
+
static int libxl__store_libxl_entry(libxl__gc *gc, uint32_t domid,
const char *name, const char *value)
{
@@ -1994,6 +2087,7 @@ static void dmss_init(libxl__dm_spawn_state *dmss)
{
libxl__ev_qmp_init(&dmss->qmp);
libxl__ev_time_init(&dmss->timeout);
+ libxl__ev_child_init(&dmss->swtpm_child);
}
static void dmss_dispose(libxl__gc *gc, libxl__dm_spawn_state *dmss)
@@ -2629,6 +2723,10 @@ static void device_model_probe_spawn_outcome(libxl__egc *egc,
libxl__dm_spawn_state *dmss, int rc);
static void device_model_launch(libxl__egc *egc,
libxl__dm_spawn_state *dmss, int rc);
+static void device_model_swtpm_cb(libxl__egc *egc,
+ libxl__ev_child *child, pid_t pid, int status);
+static void device_model_spawn_qemu(libxl__egc *egc,
+ libxl__dm_spawn_state *dmss, int rc);
static void device_model_postconfig_chardev(libxl__egc *egc,
libxl__ev_qmp *qmp, const libxl__json_object *response, int rc);
static void device_model_postconfig_vnc(libxl__egc *egc,
@@ -2875,6 +2973,69 @@ static void device_model_probe_spawn_outcome(libxl__egc *egc,
static void device_model_launch(libxl__egc *egc,
libxl__dm_spawn_state *dmss, int rc)
+{
+ STATE_AO_GC(dmss->spawn.ao);
+ const libxl_domain_build_info *b_info = &dmss->guest_config->b_info;
+ const int domid = dmss->guest_domid;
+ pid_t pid;
+
+ if (rc)
+ goto out;
+
+ if (!b_info->tpm.version) {
+ device_model_spawn_qemu(egc, dmss, 0);
+ return;
+ }
+
+ /* Remove any stale pidfile so teardown can't read a previous pid. */
+ rc = libxl__remove_file(gc, GCSPRINTF("%s/swtpm-%u.pid",
+ libxl__run_dir_path(), domid));
+ if (rc)
+ goto out;
+
+ pid = libxl__ev_child_fork(gc, &dmss->swtpm_child, device_model_swtpm_cb);
+ if (pid == -1) {
+ rc = ERROR_FAIL;
+ goto out;
+ }
+ if (!pid) {
+ /* child */
+ char **argv = libxl__swtpm_argv(gc, domid, dmss->guest_config);
+ int null = open("/dev/null", O_RDWR);
+ char **arg;
+
+ LOGD(DEBUG, domid, "Spawning swtpm %s with arguments:", argv[0]);
+ for (arg = argv; *arg; arg++)
+ LOGD(DEBUG, domid, " %s", *arg);
+
+ setsid();
+ libxl__exec(gc, null, null, null, argv[0], argv, NULL);
+ /* not reached */
+ }
+ return;
+
+out:
+ device_model_spawn_qemu(egc, dmss, rc);
+}
+
+static void device_model_swtpm_cb(libxl__egc *egc,
+ libxl__ev_child *child, pid_t pid, int status)
+{
+ libxl__dm_spawn_state *dmss = CONTAINER_OF(child, *dmss, swtpm_child);
+ STATE_AO_GC(dmss->spawn.ao);
+ int rc = 0;
+
+ if (status) {
+ libxl_report_child_exitstatus(CTX, LIBXL__LOG_ERROR, "swtpm",
+ pid, status);
+ rc = ERROR_FAIL;
+ }
+
+ device_model_spawn_qemu(egc, dmss, rc);
+}
+
+static void device_model_spawn_qemu(libxl__egc *egc,
+ libxl__dm_spawn_state *dmss, int rc)
{
STATE_AO_GC(dmss->spawn.ao);
libxl_ctx *ctx = CTX;
diff --git a/tools/libs/light/libxl_domain.c b/tools/libs/light/libxl_domain.c
index 37fcd92871..5f2ca8da7d 100644
--- a/tools/libs/light/libxl_domain.c
+++ b/tools/libs/light/libxl_domain.c
@@ -1275,6 +1275,9 @@ static void dm_destroy_cb(libxl__egc *egc,
libxl__qmp_cleanup(gc, target_domid);
}
+ /* Terminate the emulated TPM backend, if any. No-op otherwise. */
+ libxl__destroy_swtpm(gc, domid);
+
dis->drs.ao = ao;
dis->drs.domid = domid;
dis->drs.callback = devices_destroy_cb;
diff --git a/tools/libs/light/libxl_internal.h b/tools/libs/light/libxl_internal.h
index b65e0064b9..c561d3bc33 100644
--- a/tools/libs/light/libxl_internal.h
+++ b/tools/libs/light/libxl_internal.h
@@ -4106,6 +4106,7 @@ struct libxl__dm_spawn_state {
libxl__spawn_state spawn;
libxl__ev_qmp qmp;
libxl__ev_time timeout;
+ libxl__ev_child swtpm_child;
libxl__dm_resume_state dmrs;
libxl__qemu_available_opts qemu_opts;
const char *dm;
@@ -4123,6 +4124,9 @@ _hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*);
*/
_hidden int libxl__local_dm_preexec_restrict(libxl__gc *gc);
+/* swtpm (emulated TPM) backend for a QEMU-based HVM guest. */
+_hidden int libxl__destroy_swtpm(libxl__gc *gc, uint32_t domid);
+
/* Stubdom device models. */
typedef struct {
diff --git a/tools/libs/light/libxl_types.idl b/tools/libs/light/libxl_types.idl
index ce697303b6..dc6790b821 100644
--- a/tools/libs/light/libxl_types.idl
+++ b/tools/libs/light/libxl_types.idl
@@ -584,6 +584,22 @@ libxl_xs_quota_list = Struct("xs_quota_list", [
("quota", Array(libxl_xs_quota_item, "num_quota"))
])
+libxl_tpm_interface = Enumeration("tpm_interface", [
+ (0, "default"),
+ (1, "tis"),
+ (2, "crb"),
+ ])
+
+# Emulated TPM backed by swtpm. A version of 0 means no TPM.
+libxl_tpm = Struct("tpm", [
+ ("statefile", string),
+ ("statedir", string),
+ ("swtpm_override", string),
+ ("version", integer, {'init_val': '0'}),
+ ("loglevel", integer, {'init_val': '0'}),
+ ("interface", libxl_tpm_interface),
+ ])
+
libxl_domain_build_info = Struct("domain_build_info",[
("max_vcpus", integer),
("avail_vcpus", libxl_bitmap),
@@ -658,6 +674,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
("apic", libxl_defbool),
("dm_restrict", libxl_defbool),
("tee", libxl_tee_type),
+ ("tpm", libxl_tpm),
("u", KeyedUnion(None, libxl_domain_type, "type",
[("hvm", Struct(None, [("firmware", string),
("bios", libxl_bios_type),
diff --git a/tools/libs/light/libxl_x86.c b/tools/libs/light/libxl_x86.c
index 9e8b48372d..3182cfc9df 100644
--- a/tools/libs/light/libxl_x86.c
+++ b/tools/libs/light/libxl_x86.c
@@ -825,6 +825,24 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
libxl_defbool_setdefault(&b_info->arch_x86.msr_relaxed, false);
libxl_defbool_setdefault(&b_info->trap_unmapped_accesses, false);
+ if (b_info->tpm.version && b_info->type == LIBXL_DOMAIN_TYPE_PVH) {
+ LOG(ERROR, "TPM is not supported for PVH guests on x86.");
+ return ERROR_INVAL;
+ }
+
+ /* TPM 1.2 only supports TIS. TPM 2.0 supports TIS or CRB. */
+ if (b_info->tpm.version) {
+ if (b_info->tpm.interface == LIBXL_TPM_INTERFACE_DEFAULT)
+ b_info->tpm.interface = b_info->tpm.version == 2 ?
+ LIBXL_TPM_INTERFACE_CRB :
+ LIBXL_TPM_INTERFACE_TIS;
+ if (b_info->tpm.version == 1 &&
+ b_info->tpm.interface != LIBXL_TPM_INTERFACE_TIS) {
+ LOG(ERROR, "TPM version 1 only supports the tis interface.");
+ return ERROR_INVAL;
+ }
+ }
+
if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
/*
* The config parameter "altp2m" replaces the parameter "altp2mhvm".
diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
index 0ccece3e8a..1fed322380 100644
--- a/tools/xl/xl_parse.c
+++ b/tools/xl/xl_parse.c
@@ -3060,6 +3060,26 @@ skip_usbdev:
}
}
+ if (!xlu_cfg_get_long(config, "tpm_version", &l, 0)) {
+ b_info->tpm.version = l;
+
+ xlu_cfg_replace_string(config, "tpm_statefile",
+ &b_info->tpm.statefile, 0);
+ xlu_cfg_replace_string(config, "tpm_statedir",
+ &b_info->tpm.statedir, 0);
+ xlu_cfg_replace_string(config, "tpm_swtpm_override",
+ &b_info->tpm.swtpm_override, 0);
+ if (!xlu_cfg_get_long(config, "tpm_loglevel", &l, 0))
+ b_info->tpm.loglevel = l;
+
+ if (!xlu_cfg_get_string(config, "tpm_interface", &buf, 0) &&
+ (libxl_tpm_interface_from_string(buf, &b_info->tpm.interface) ||
+ b_info->tpm.interface == LIBXL_TPM_INTERFACE_DEFAULT)) {
+ fprintf(stderr, "xl: tpm_interface must be tis or crb\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
if (!xlu_cfg_get_string (config, "sve", &buf, 1)) {
e = libxl_sve_type_from_string(buf, &b_info->arch_arm.sve_vl);
if (e) {
--
2.34.1
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 4/4] libxl: Add emulated TPM support on ARM
2026-11-06 23:15 [PATCH 0/4] libxl TPM emulator support Jason Andryuk
` (2 preceding siblings ...)
2026-11-06 23:15 ` [PATCH 3/4] tools/libxl: add emulated TPM support backed by swtpm Jason Andryuk
@ 2026-11-06 23:15 ` Jason Andryuk
3 siblings, 0 replies; 10+ messages in thread
From: Jason Andryuk @ 2026-11-06 23:15 UTC (permalink / raw)
To: xen-devel
Cc: Stefano Stabellini, Anthony PERARD, Juergen Gross,
Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
Volodymyr Babchuk, Stefano Stabellini, Jason Andryuk
From: Stefano Stabellini <stefano.stabellini@amd.com>
QEMU's ARM "xenpvh" machine runs as an IOREQ server and, when given a
"tpm-base-addr" machine property, instantiates a TPM TIS sysbus device
at that address connected to the "tpm0" TPM backend. Hook the existing
tpm_* configuration up to it so ARM guests can use an swtpm-backed TPM.
Reserve GUEST_TPM_BASE/GUEST_TPM_SIZE in the ARM guest memory map and
add a "tcg,tpm-tis-mmio" node to the guest device tree when a TPM
version is configured. The xenpvh machine has no CRB device, so TIS is
the default and only interface allowed for both TPM 1.2 and 2.0.
Add libxl__arch_pv_pvh_machinearg() returning the QEMU -machine argument
for a PV/PVH guest. x86 returns plain "xenpv", while ARM uses "xenpvh"
and appends tpm-base-addr when a TPM is configured. uint64_t tpm_base
in ARM's libxl__arch_pvh_machinearg() is to allow PRIx64 to print the
value. Otherwise the ull argument mismatches PRIx64's %lx format.
libxl__arch_pv_pvh_machinearg() also handles PV guests. This keeps the
extra_pv handling identical between PV and PVH guests.
Signed-off-by: Stefano Stabellini <stefano.stabellini@xilinx.com>
[ Merge ARM parts with new libxl_dm implementation. ]
Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
---
docs/man/xl.cfg.5.pod.in | 14 ++++++++---
tools/include/libxl.h | 5 ++--
tools/libs/light/libxl_arch.h | 6 +++++
tools/libs/light/libxl_arm.c | 47 +++++++++++++++++++++++++++++++++++
tools/libs/light/libxl_dm.c | 14 ++++++++++-
tools/libs/light/libxl_x86.c | 6 +++++
xen/include/public/arch-arm.h | 4 +++
7 files changed, 89 insertions(+), 7 deletions(-)
diff --git a/docs/man/xl.cfg.5.pod.in b/docs/man/xl.cfg.5.pod.in
index c12d2dc4db..e5952d7e27 100644
--- a/docs/man/xl.cfg.5.pod.in
+++ b/docs/man/xl.cfg.5.pod.in
@@ -823,11 +823,16 @@ value is optional if this is a guest domain.
=item B<tpm_version=NUMBER>
-Provides an emulated TPM to a QEMU-based HVM guest, backed by B<swtpm(8)>.
-The TPM version to emulate is either B<1> for TPM 1.2 or B<2> for TPM 2.0.
+Provides an emulated TPM to a QEMU-based HVM guest, or to an Arm guest,
+backed by B<swtpm(8)>. The TPM version to emulate is either B<1> for
+TPM 1.2 or B<2> for TPM 2.0.
When this option is not specified, no TPM is provided and the other
B<tpm_*> options are ignored.
+On Arm, QEMU's B<xenpvh> machine is started as an IOREQ server to emulate
+a TPM TIS interface, and the guest device tree gains a
+C<tcg,tpm-tis-mmio> node describing it.
+
One of B<tpm_statefile> or B<tpm_statedir> is required, but not both.
=item B<tpm_statefile="PATH">
@@ -844,8 +849,9 @@ Path to a directory in which B<swtpm> stores the persistent TPM state.
The TPM interface presented to the guest. B<tis> is the TPM Interface
Specification (FIFO) interface and B<crb> is the Command Response Buffer
-interface. B<tpm_version=1> only supports B<tis>, while B<tpm_version=2>
-supports either and defaults to B<crb>.
+interface. On x86, B<tpm_version=1> only supports B<tis>, while
+B<tpm_version=2> supports either and defaults to B<crb>. On Arm, only
+B<tis> is supported, for both TPM versions.
=item B<tpm_swtpm_override="PATH">
diff --git a/tools/include/libxl.h b/tools/include/libxl.h
index 8b431b708d..8d19cf41bc 100644
--- a/tools/include/libxl.h
+++ b/tools/include/libxl.h
@@ -1568,8 +1568,9 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
*
* If this is defined the tpm member of struct domain_build_info is
* available. When its version is non-zero, libxl starts swtpm to
- * provide an emulated TPM to a QEMU-based HVM guest. Its interface
- * member selects the TPM interface (TIS or CRB) presented to the guest.
+ * provide an emulated TPM to a QEMU-based HVM guest, or to an Arm guest
+ * via QEMU's xenpvh machine. Its interface member selects the TPM
+ * interface (TIS or CRB) presented to the guest.
*/
#define LIBXL_HAVE_TPM
diff --git a/tools/libs/light/libxl_arch.h b/tools/libs/light/libxl_arch.h
index c8ef52ddbe..b3355d1e82 100644
--- a/tools/libs/light/libxl_arch.h
+++ b/tools/libs/light/libxl_arch.h
@@ -97,6 +97,12 @@ _hidden
int libxl__arch_hvm_unmap_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid);
_hidden
bool libxl__arch_local_domain_has_pirq_notion(libxl__gc *gc);
+
+/* QEMU -machine argument for a PVH guest. */
+_hidden
+char *libxl__arch_pv_pvh_machinearg(libxl__gc *gc,
+ const libxl_domain_build_info *b_info);
+
#if defined(__i386__) || defined(__x86_64__)
#define LAPIC_BASE_ADDRESS 0xfee00000
diff --git a/tools/libs/light/libxl_arm.c b/tools/libs/light/libxl_arm.c
index 47b662ea5c..6325b19766 100644
--- a/tools/libs/light/libxl_arm.c
+++ b/tools/libs/light/libxl_arm.c
@@ -978,6 +978,29 @@ static int make_xen_iommu_node(libxl__gc *gc, void *fdt)
return 0;
}
+static int make_tpm_node(libxl__gc *gc, void *fdt)
+{
+ int res;
+ const uint64_t base = GUEST_TPM_BASE;
+ const char *name = GCSPRINTF("tpm@%"PRIx64, base);
+
+ res = fdt_begin_node(fdt, name);
+ if (res) return res;
+
+ res = fdt_property_compat(gc, fdt, 1, "tcg,tpm-tis-mmio");
+ if (res) return res;
+
+ res = fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS,
+ GUEST_ROOT_SIZE_CELLS, 1,
+ base, GUEST_TPM_SIZE);
+ if (res) return res;
+
+ res = fdt_end_node(fdt);
+ if (res) return res;
+
+ return 0;
+}
+
/* The caller is responsible to complete / close the fdt node */
static int make_virtio_mmio_node_common(libxl__gc *gc, void *fdt, uint64_t base,
uint32_t irq, uint32_t backend_domid,
@@ -1450,6 +1473,9 @@ next_resize:
if (iommu_needed)
FDT( make_xen_iommu_node(gc, fdt) );
+ if (info->tpm.version)
+ FDT( make_tpm_node(gc, fdt) );
+
if (pfdt)
FDT( copy_partial_fdt(gc, fdt, pfdt) );
@@ -1860,6 +1886,16 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
return ERROR_FAIL;
}
+ /* The xenpvh machine only provides a TIS TPM interface. */
+ if (b_info->tpm.version) {
+ if (b_info->tpm.interface == LIBXL_TPM_INTERFACE_DEFAULT)
+ b_info->tpm.interface = LIBXL_TPM_INTERFACE_TIS;
+ if (b_info->tpm.interface != LIBXL_TPM_INTERFACE_TIS) {
+ LOG(ERROR, "Only the tis TPM interface is supported on Arm.");
+ return ERROR_INVAL;
+ }
+ }
+
/* Sanitise SVE parameter */
if (b_info->arch_arm.sve_vl) {
unsigned int max_sve_vl =
@@ -1949,6 +1985,17 @@ bool libxl__arch_local_domain_has_pirq_notion(libxl__gc *gc)
return true;
}
+char *libxl__arch_pv_pvh_machinearg(libxl__gc *gc,
+ const libxl_domain_build_info *b_info)
+{
+ uint64_t tpm_base = GUEST_TPM_BASE;
+
+ if (b_info->tpm.version)
+ return GCSPRINTF("xenpvh,tpm-base-addr=0x%"PRIx64, tpm_base);
+
+ return libxl__strdup(gc, "xenpvh");
+}
+
/*
* Local variables:
* mode: C
diff --git a/tools/libs/light/libxl_dm.c b/tools/libs/light/libxl_dm.c
index 843b8e29eb..ebdd05586e 100644
--- a/tools/libs/light/libxl_dm.c
+++ b/tools/libs/light/libxl_dm.c
@@ -18,6 +18,7 @@
#include "libxl_osdeps.h" /* must come before any other headers */
#include "libxl_internal.h"
+#include "libxl_arch.h"
#include <xen/hvm/e820.h>
#include <sys/types.h>
@@ -1573,7 +1574,12 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
switch (b_info->type) {
case LIBXL_DOMAIN_TYPE_PVH:
case LIBXL_DOMAIN_TYPE_PV:
- flexarray_append(dm_args, "xenpv");
+ machinearg = libxl__arch_pv_pvh_machinearg(gc, b_info);
+ flexarray_append(dm_args, machinearg);
+
+ if (b_info->tpm.version)
+ libxl__dm_add_tpm_args(gc, dm_args, guest_domid);
+
for (i = 0; b_info->extra_pv && b_info->extra_pv[i] != NULL; i++)
flexarray_append(dm_args, b_info->extra_pv[i]);
break;
@@ -3892,6 +3898,12 @@ int libxl__need_xenpv_qemu(libxl__gc *gc, libxl_domain_config *d_config)
goto out;
}
+ /* QEMU provides the emulated TPM, e.g. via the ARM xenpvh machine. */
+ if (d_config->b_info.tpm.version) {
+ ret = 1;
+ goto out;
+ }
+
for (idx = 0;; idx++) {
dt = device_type_tbl[idx];
if (!dt)
diff --git a/tools/libs/light/libxl_x86.c b/tools/libs/light/libxl_x86.c
index 3182cfc9df..a6e9ad777e 100644
--- a/tools/libs/light/libxl_x86.c
+++ b/tools/libs/light/libxl_x86.c
@@ -937,6 +937,12 @@ bool libxl__arch_local_domain_has_pirq_notion(libxl__gc *gc)
return false;
}
+char *libxl__arch_pv_pvh_machinearg(libxl__gc *gc,
+ const libxl_domain_build_info *b_info)
+{
+ return libxl__strdup(gc, "xenpv");
+}
+
int libxl__arch_hvm_map_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid)
{
int pirq = -1, gsi, r;
diff --git a/xen/include/public/arch-arm.h b/xen/include/public/arch-arm.h
index 5d15f572c7..efb45eb33c 100644
--- a/xen/include/public/arch-arm.h
+++ b/xen/include/public/arch-arm.h
@@ -452,6 +452,10 @@ typedef uint64_t xen_callback_t;
#define GUEST_VPCI_ECAM_BASE xen_mk_ullong(0x10000000)
#define GUEST_VPCI_ECAM_SIZE xen_mk_ullong(0x10000000)
+/* Emulated TPM (TIS over MMIO) */
+#define GUEST_TPM_BASE xen_mk_ullong(0x0c000000)
+#define GUEST_TPM_SIZE xen_mk_ullong(0x00005000)
+
/* ACPI tables physical address */
#define GUEST_ACPI_BASE xen_mk_ullong(0x20000000)
#define GUEST_ACPI_SIZE xen_mk_ullong(0x02000000)
--
2.34.1
^ permalink raw reply related [flat|nested] 10+ messages in thread