ia64/xen-unstable

changeset 12037:d60b709724f4

[QEMU] Add a TIS device model compliant to the 1.2 TPM specification.
It implements all registers necessary to make the Linux TIS driver
work (tpm_tis.c). All of the basic registers supported by this type of
device are implemented. Also the locality selection has been
implemented, but has not been tested. The legacy registers as
described in the specification are not supported.

Current caveat: The device has so far not yet been integrated with the
virtual TPM available in the repository. It will require changes to
the virtual TPM spawned by the vTPM manager to offer an additional message
interface. The TIS interface itself then needs to have an additional
transport implemented. (see vTPMTransmit array).

The relevant specification for the device model can be found here:
https://www.trustedcomputinggroup.org/groups/pc_client/TCG_PCClientTPMSpecification_1-20_1-00_FINAL.pdf

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author kaf24@localhost.localdomain
date Fri Oct 27 19:15:48 2006 +0100 (2006-10-27)
parents bbcac2aea0e8
children 36679b74e24a
files tools/ioemu/Makefile.target tools/ioemu/hw/pc.c tools/ioemu/hw/tpm_tis.c tools/ioemu/vl.h
line diff
     1.1 --- a/tools/ioemu/Makefile.target	Fri Oct 27 19:13:51 2006 +0100
     1.2 +++ b/tools/ioemu/Makefile.target	Fri Oct 27 19:15:48 2006 +0100
     1.3 @@ -368,6 +368,7 @@ VL_OBJS+= usb-uhci.o
     1.4  VL_OBJS+= piix4acpi.o
     1.5  VL_OBJS+= xenstore.o
     1.6  VL_OBJS+= xen_platform.o
     1.7 +VL_OBJS+= tpm_tis.o
     1.8  DEFINES += -DHAS_AUDIO
     1.9  endif
    1.10  ifeq ($(TARGET_BASE_ARCH), ppc)
     2.1 --- a/tools/ioemu/hw/pc.c	Fri Oct 27 19:13:51 2006 +0100
     2.2 +++ b/tools/ioemu/hw/pc.c	Fri Oct 27 19:15:48 2006 +0100
     2.3 @@ -875,6 +875,9 @@ static void pc_init1(uint64_t ram_size, 
     2.4          }
     2.5      }
     2.6  
     2.7 +    if (has_tpm_device())
     2.8 +        tpm_tis_init(&pic_set_irq_new, isa_pic, 11);
     2.9 +
    2.10      kbd_init();
    2.11      DMA_init(0);
    2.12  #ifdef HAS_AUDIO
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/ioemu/hw/tpm_tis.c	Fri Oct 27 19:15:48 2006 +0100
     3.3 @@ -0,0 +1,1114 @@
     3.4 +/*
     3.5 + * tpm_tis.c - QEMU emulator for a 1.2 TPM with TIS interface
     3.6 + *
     3.7 + * Copyright (C) 2006 IBM Corporation
     3.8 + *
     3.9 + * Author: Stefan Berger <stefanb@us.ibm.com>
    3.10 + *         David Safford <safford@us.ibm.com>
    3.11 + *
    3.12 + * This program is free software; you can redistribute it and/or
    3.13 + * modify it under the terms of the GNU General Public License as
    3.14 + * published by the Free Software Foundation, version 2 of the
    3.15 + * License.
    3.16 + *
    3.17 + *
    3.18 + * Implementation of the TIS interface according to specs at
    3.19 + * https://www.trustedcomputinggroup.org/groups/pc_client/TCG_PCClientTPMSpecification_1-20_1-00_FINAL.pdf
    3.20 + *
    3.21 + */
    3.22 +
    3.23 +#include <sys/types.h>
    3.24 +#include <sys/stat.h>
    3.25 +#include <sys/socket.h>
    3.26 +#include <sys/un.h>
    3.27 +#include <fcntl.h>
    3.28 +#include <errno.h>
    3.29 +#include "vl.h"
    3.30 +
    3.31 +//#define DEBUG_TPM
    3.32 +
    3.33 +#define TPM_MAX_PKT	              4096
    3.34 +
    3.35 +#define VTPM_BAD_INSTANCE             (uint32_t)0xffffffff
    3.36 +
    3.37 +#define TIS_ADDR_BASE                 0xFED40000
    3.38 +
    3.39 +/* tis registers */
    3.40 +#define TPM_REG_ACCESS                0x00
    3.41 +#define TPM_REG_INT_ENABLE            0x08
    3.42 +#define TPM_REG_INT_VECTOR            0x0c
    3.43 +#define TPM_REG_INT_STATUS            0x10
    3.44 +#define TPM_REG_INTF_CAPABILITY       0x14
    3.45 +#define TPM_REG_STS                   0x18
    3.46 +#define TPM_REG_DATA_FIFO             0x24
    3.47 +#define TPM_REG_DID_VID               0xf00
    3.48 +#define TPM_REG_RID                   0xf04
    3.49 +
    3.50 +#define STS_VALID                    (1 << 7)
    3.51 +#define STS_COMMAND_READY            (1 << 6)
    3.52 +#define STS_TPM_GO                   (1 << 5)
    3.53 +#define STS_DATA_AVAILABLE           (1 << 4)
    3.54 +#define STS_EXPECT                   (1 << 3)
    3.55 +#define STS_RESPONSE_RETRY           (1 << 1)
    3.56 +
    3.57 +#define ACCESS_TPM_REG_VALID_STS     (1 << 7)
    3.58 +#define ACCESS_ACTIVE_LOCALITY       (1 << 5)
    3.59 +#define ACCESS_BEEN_SEIZED           (1 << 4)
    3.60 +#define ACCESS_SEIZE                 (1 << 3)
    3.61 +#define ACCESS_PENDING_REQUEST       (1 << 2)
    3.62 +#define ACCESS_REQUEST_USE           (1 << 1)
    3.63 +#define ACCESS_TPM_ESTABLISHMENT     (1 << 0)
    3.64 +
    3.65 +#define INT_ENABLED                  (1 << 31)
    3.66 +#define INT_DATA_AVAILABLE           (1 << 0)
    3.67 +#define INT_LOCALITY_CHANGED         (1 << 2)
    3.68 +#define INT_COMMAND_READY            (1 << 7)
    3.69 +
    3.70 +#define INTERRUPTS_SUPPORTED         (INT_LOCALITY_CHANGED | \
    3.71 +                                      INT_DATA_AVAILABLE   | \
    3.72 +                                      INT_COMMAND_READY)
    3.73 +#define CAPABILITIES_SUPPORTED       ((1 << 4) |            \
    3.74 +                                      INTERRUPTS_SUPPORTED)
    3.75 +
    3.76 +enum {
    3.77 +  STATE_IDLE = 0,
    3.78 +  STATE_READY,
    3.79 +  STATE_COMPLETION,
    3.80 +  STATE_EXECUTION,
    3.81 +  STATE_RECEPTION
    3.82 +};
    3.83 +
    3.84 +#define NUM_LOCALITIES   5
    3.85 +#define NO_LOCALITY      0xff
    3.86 +
    3.87 +#define IS_VALID_LOC(x) ((x) < NUM_LOCALITIES)
    3.88 +
    3.89 +#define TPM_DID          0x0001
    3.90 +#define TPM_VID          0x0001
    3.91 +#define TPM_RID          0x0001
    3.92 +
    3.93 +/* if the connection to the vTPM should be closed after a successfully
    3.94 +   received response; set to '0' to allow keeping the connection */
    3.95 +#define FORCE_CLOSE      0
    3.96 +
    3.97 +/* local data structures */
    3.98 +
    3.99 +typedef struct TPMTx {
   3.100 +    int fd[2];
   3.101 +} tpmTx;
   3.102 +
   3.103 +typedef struct TPMBuffer {
   3.104 +    uint8_t instance[4];      /* instance number in network byte order */
   3.105 +    uint8_t buf[TPM_MAX_PKT];
   3.106 +} __attribute__((packed)) tpmBuffer;
   3.107 +
   3.108 +/* locality data */
   3.109 +typedef struct TPMLocal {
   3.110 +    uint32_t state;
   3.111 +    uint8_t access;
   3.112 +    uint8_t sts;
   3.113 +    uint32_t inte;
   3.114 +    uint32_t ints;
   3.115 +} tpmLoc;
   3.116 +
   3.117 +/* overall state of the TPM interface; 's' marks as save upon suspension */
   3.118 +typedef struct TPMState {
   3.119 +    uint32_t offset;            /* s */
   3.120 +    tpmBuffer buffer;           /* s */
   3.121 +    uint8_t active_loc;         /* s */
   3.122 +    uint8_t aborting_locty;
   3.123 +    uint8_t next_locty;
   3.124 +    uint8_t irq_pending;        /* s */
   3.125 +    tpmLoc loc[NUM_LOCALITIES]; /* s */
   3.126 +    QEMUTimer *poll_timer;
   3.127 +    SetIRQFunc *set_irq;
   3.128 +    void *irq_opaque;
   3.129 +    int irq;
   3.130 +    int poll_attempts;
   3.131 +    uint32_t vtpm_instance;  /* vtpm inst. number; determined from xenstore*/
   3.132 +    int Transmitlayer;
   3.133 +    tpmTx tpmTx;
   3.134 +} tpmState;
   3.135 +
   3.136 +
   3.137 +/* local prototypes */
   3.138 +static int TPM_Send(tpmState *s, tpmBuffer *buffer, char *msg);
   3.139 +static int TPM_Receive(tpmState *s, tpmBuffer *buffer);
   3.140 +static uint32_t vtpm_instance_from_xenstore(void);
   3.141 +static void tis_poll_timer(void *opaque);
   3.142 +static void tis_prep_next_interrupt(tpmState *s);
   3.143 +static void tis_raise_irq(tpmState *s, uint8_t locty, uint32_t irqmask);
   3.144 +static void close_vtpm_channel(tpmState *s, int force);
   3.145 +static void open_vtpm_channel(tpmState *s);
   3.146 +static void tis_attempt_receive(tpmState *s, uint8_t locty);
   3.147 +
   3.148 +/* transport layer functions: local sockets */
   3.149 +static int create_local_socket(tpmState *s, uint32_t vtpm_instance);
   3.150 +static int write_local_socket(tpmState *s, const tpmBuffer *);
   3.151 +static int read_local_socket(tpmState *s, tpmBuffer *);
   3.152 +static int close_local_socket(tpmState *s, int force);
   3.153 +static int has_channel_local_socket(tpmState *s);
   3.154 +#define LOCAL_SOCKET_PATH      "/var/vtpm/vtpm_all.socket"
   3.155 +
   3.156 +
   3.157 +#define NUM_TRANSPORTS 1
   3.158 +
   3.159 +struct vTPM_transmit {
   3.160 +    int (*open) (tpmState *s, uint32_t vtpm_instance);
   3.161 +    int (*write) (tpmState *s, const tpmBuffer *);
   3.162 +    int (*read) (tpmState *s, tpmBuffer *);
   3.163 +    int (*close) (tpmState *s, int);
   3.164 +    int (*has_channel) (tpmState *s);
   3.165 +} vTPMTransmit[NUM_TRANSPORTS] = {
   3.166 +    { .open = create_local_socket,
   3.167 +      .write = write_local_socket,
   3.168 +      .read = read_local_socket,
   3.169 +      .close = close_local_socket,
   3.170 +      .has_channel = has_channel_local_socket,
   3.171 +    }
   3.172 +};
   3.173 +
   3.174 +
   3.175 +#define IS_COMM_WITH_VTPM(s)                            \
   3.176 +     ((s)->Transmitlayer >= 0 &&                        \
   3.177 +      vTPMTransmit[(s)->Transmitlayer].has_channel(s))
   3.178 +
   3.179 +
   3.180 +/**********************************************************************
   3.181 + helper functions
   3.182 + *********************************************************************/
   3.183 +
   3.184 +static inline uint32_t tpm_get_size_from_buffer(const uint8_t *buffer)
   3.185 +{
   3.186 +    uint32_t len = (buffer[4] << 8) + buffer[5];
   3.187 +    return len;
   3.188 +}
   3.189 +
   3.190 +static inline void tpm_initialize_instance(tpmState *s, uint32_t instance)
   3.191 +{
   3.192 +    s->buffer.instance[0] = (instance >> 24) & 0xff;
   3.193 +    s->buffer.instance[1] = (instance >> 16) & 0xff;
   3.194 +    s->buffer.instance[2] = (instance >>  8) & 0xff;
   3.195 +    s->buffer.instance[3] = (instance >>  0) & 0xff;
   3.196 +}
   3.197 +
   3.198 +/*
   3.199 + * open communication channel with a vTPM
   3.200 + */
   3.201 +static void open_vtpm_channel(tpmState *s)
   3.202 +{
   3.203 +    int idx;
   3.204 +    /* search a usable transmit layer */
   3.205 +    for (idx = 0; idx < NUM_TRANSPORTS; idx++) {
   3.206 +        if (1 == vTPMTransmit[idx].open(s, s->vtpm_instance)) {
   3.207 +            /* found one */
   3.208 +            s->Transmitlayer = idx;
   3.209 +            break;
   3.210 +        }
   3.211 +    }
   3.212 +}
   3.213 +
   3.214 +/*
   3.215 + * close the communication channel with the vTPM
   3.216 + */
   3.217 +static inline void close_vtpm_channel(tpmState *s, int force)
   3.218 +{
   3.219 +    if (1 == vTPMTransmit[s->Transmitlayer].close(s, force)) {
   3.220 +        s->Transmitlayer = -1;
   3.221 +    }
   3.222 +}
   3.223 +
   3.224 +static inline uint8_t locality_from_addr(target_phys_addr_t addr)
   3.225 +{
   3.226 +    return (uint8_t)((addr >> 12) & 0x7);
   3.227 +}
   3.228 +
   3.229 +
   3.230 +/**********************************************************************
   3.231 +    low-level transmission layer methods
   3.232 + *********************************************************************/
   3.233 +
   3.234 +/*
   3.235 + * the 'open' method that creates the filedescriptor for communicating
   3.236 + * only one is needed for reading and writing
   3.237 + */
   3.238 +static int create_local_socket(tpmState *s, uint32_t vtpm_instance)
   3.239 +{
   3.240 +    int success = 1;
   3.241 +    if (s->tpmTx.fd[0] < 0) {
   3.242 +        s->tpmTx.fd[0] = socket(PF_LOCAL, SOCK_STREAM, 0);
   3.243 +
   3.244 +        if (has_channel_local_socket(s)) {
   3.245 +            struct sockaddr_un addr;
   3.246 +            memset(&addr, 0x0, sizeof(addr));
   3.247 +            addr.sun_family = AF_LOCAL;
   3.248 +            strcpy(addr.sun_path, LOCAL_SOCKET_PATH);
   3.249 +            if (connect(s->tpmTx.fd[0],
   3.250 +                        (struct sockaddr *)&addr,
   3.251 +                        sizeof(addr)) != 0) {
   3.252 +                close_local_socket(s, 1);
   3.253 +                success = 0;
   3.254 +            } else {
   3.255 +                /* put filedescriptor in non-blocking mode for polling */
   3.256 +                int flags = fcntl(s->tpmTx.fd[0], F_GETFL);
   3.257 +                fcntl(s->tpmTx.fd[0], F_SETFL, flags | O_NONBLOCK);
   3.258 +            }
   3.259 +#ifdef DEBUG_TPM
   3.260 +            if (success)
   3.261 +                fprintf(logfile,"Successfully connected using local socket "
   3.262 +                                LOCAL_SOCKET_PATH ".\n");
   3.263 +            else
   3.264 +                fprintf(logfile,"Could not connect to local socket "
   3.265 +                                LOCAL_SOCKET_PATH ".\n");
   3.266 +#endif
   3.267 +        } else {
   3.268 +            success = 0;
   3.269 +        }
   3.270 +    }
   3.271 +    return success;
   3.272 +}
   3.273 +
   3.274 +/*
   3.275 + * the 'write' method for sending requests to the vTPM
   3.276 + * four bytes with the vTPM instance number are prepended to each request
   3.277 + */
   3.278 +static int write_local_socket(tpmState *s, const tpmBuffer *buffer)
   3.279 +{
   3.280 +    uint32_t size = tpm_get_size_from_buffer(buffer->buf);
   3.281 +    int len;
   3.282 +
   3.283 +    len = write(s->tpmTx.fd[0],
   3.284 +                buffer->instance,
   3.285 +                sizeof(buffer->instance) + size);
   3.286 +    if (len == sizeof(buffer->instance) + size) {
   3.287 +        return len;
   3.288 +    } else {
   3.289 +        return -1;
   3.290 +    }
   3.291 +}
   3.292 +
   3.293 +/*
   3.294 + * the 'read' method for receiving of responses from the TPM
   3.295 + * this function expects that four bytes with the instance number
   3.296 + * are received from the vTPM
   3.297 + */
   3.298 +static int read_local_socket(tpmState *s, tpmBuffer *buffer)
   3.299 +{
   3.300 +    int off;
   3.301 +#ifdef DEBUG_TPM
   3.302 +    fprintf(logfile, "Reading from fd %d\n", s->tpmTx.fd[0]);
   3.303 +#endif
   3.304 +    off = read(s->tpmTx.fd[0],
   3.305 +               buffer->instance,
   3.306 +               sizeof(buffer->instance)+TPM_MAX_PKT);
   3.307 +#ifdef DEBUG_TPM
   3.308 +    fprintf(logfile, "Read %d bytes\n", off);
   3.309 +#endif
   3.310 +    return off;
   3.311 +}
   3.312 +
   3.313 +/*
   3.314 + * the 'close' method
   3.315 + * shut down communication with the vTPM
   3.316 + * 'force' = 1 indicates that the socket *must* be closed
   3.317 + * 'force' = 0 indicates that a connection may be maintained
   3.318 + */
   3.319 +static int close_local_socket(tpmState *s, int force)
   3.320 +{
   3.321 +    if (force) {
   3.322 +        close(s->tpmTx.fd[0]);
   3.323 +#ifdef DEBUG_TPM
   3.324 +        fprintf(logfile,"Closed connection with fd %d\n",s->tpmTx.fd[0]);
   3.325 +#endif
   3.326 +        s->tpmTx.fd[0] = -1;
   3.327 +        return 1; /* socket was closed */
   3.328 +    }
   3.329 +#ifdef DEBUG_TPM
   3.330 +    fprintf(logfile,"Keeping connection with fd %d\n",s->tpmTx.fd[0]);
   3.331 +#endif
   3.332 +    return 0;
   3.333 +}
   3.334 +
   3.335 +/*
   3.336 + * the 'has_channel' method that checks whether there's a communication
   3.337 + * channel with the vTPM
   3.338 + */
   3.339 +static int has_channel_local_socket(tpmState *s)
   3.340 +{
   3.341 +    return (s->tpmTx.fd[0] > 0);
   3.342 +}
   3.343 +
   3.344 +/**********************************************************************/
   3.345 +
   3.346 +/*
   3.347 + * read a byte of response data
   3.348 + */
   3.349 +static uint32_t tpm_data_read(tpmState *s, uint8_t locty)
   3.350 +{
   3.351 +    uint32_t ret, len;
   3.352 +
   3.353 +    /* try to receive data, if none are there it is ok */
   3.354 +    tis_attempt_receive(s, locty);
   3.355 +
   3.356 +    if (s->loc[locty].state != STATE_COMPLETION) {
   3.357 +        return 0xff;
   3.358 +    }
   3.359 +
   3.360 +    len = tpm_get_size_from_buffer(s->buffer.buf);
   3.361 +    ret = s->buffer.buf[s->offset++];
   3.362 +    if (s->offset >= len) {
   3.363 +        s->loc[locty].sts = STS_VALID ;
   3.364 +        s->offset = 0;
   3.365 +    }
   3.366 +#ifdef DEBUG_TPM
   3.367 +    fprintf(logfile,"tpm_data_read byte x%02x   [%d]\n",ret,s->offset-1);
   3.368 +#endif
   3.369 +    return ret;
   3.370 +}
   3.371 +
   3.372 +
   3.373 +
   3.374 +/* raise an interrupt if allowed */
   3.375 +static void tis_raise_irq(tpmState *s, uint8_t locty, uint32_t irqmask)
   3.376 +{
   3.377 +    if (!s->irq_pending &&
   3.378 +        (s->loc[locty].inte & INT_ENABLED) &&
   3.379 +        (s->loc[locty].inte & irqmask)) {
   3.380 +        if ((irqmask & s->loc[locty].ints) == 0) {
   3.381 +#ifdef DEBUG_TPM
   3.382 +            fprintf(logfile,"Raising IRQ for flag %08x\n",irqmask);
   3.383 +#endif
   3.384 +            s->set_irq(s->irq_opaque, s->irq, 1);
   3.385 +            s->irq_pending = 1;
   3.386 +            s->loc[locty].ints |= irqmask;
   3.387 +        }
   3.388 +    }
   3.389 +}
   3.390 +
   3.391 +/* abort execution of command */
   3.392 +static void tis_abort(tpmState *s)
   3.393 +{
   3.394 +    s->offset = 0;
   3.395 +    s->active_loc = s->next_locty;
   3.396 +
   3.397 +    /*
   3.398 +     * Need to react differently depending on who's aborting now and
   3.399 +     * which locality will become active afterwards.
   3.400 +     */
   3.401 +    if (s->aborting_locty == s->next_locty) {
   3.402 +        s->loc[s->aborting_locty].state = STATE_READY;
   3.403 +        s->loc[s->aborting_locty].sts   = STS_COMMAND_READY;
   3.404 +        tis_raise_irq(s, s->aborting_locty, INT_COMMAND_READY);
   3.405 +    }
   3.406 +
   3.407 +    /* locality after abort is another one than the current one */
   3.408 +    if (s->aborting_locty != s->next_locty && s->next_locty != NO_LOCALITY) {
   3.409 +        s->loc[s->aborting_locty].access &= ~ACCESS_ACTIVE_LOCALITY;
   3.410 +        s->loc[s->next_locty].access     |=  ACCESS_ACTIVE_LOCALITY;
   3.411 +        tis_raise_irq(s, s->next_locty, INT_LOCALITY_CHANGED);
   3.412 +    }
   3.413 +
   3.414 +    s->aborting_locty = NO_LOCALITY; /* nobody's aborting a command anymore */
   3.415 +
   3.416 +    qemu_del_timer(s->poll_timer);
   3.417 +}
   3.418 +
   3.419 +/* abort current command */
   3.420 +static void tis_prep_abort(tpmState *s, uint8_t locty, uint8_t newlocty)
   3.421 +{
   3.422 +    s->aborting_locty = locty; /* current locality */
   3.423 +    s->next_locty = newlocty;  /* locality after successful abort */
   3.424 +
   3.425 +    /*
   3.426 +     * only abort a command using an interrupt if currently executing
   3.427 +     * a command AND if there's a valid connection to the vTPM.
   3.428 +     */
   3.429 +    if (s->loc[locty].state == STATE_EXECUTION &&
   3.430 +        IS_COMM_WITH_VTPM(s)) {
   3.431 +        /* start timer and inside the timer wait for the result */
   3.432 +        s->poll_attempts = 0;
   3.433 +        tis_prep_next_interrupt(s);
   3.434 +    } else {
   3.435 +        tis_abort(s);
   3.436 +    }
   3.437 +}
   3.438 +
   3.439 +
   3.440 +/*
   3.441 + * Try to receive a response from the vTPM
   3.442 + */
   3.443 +static void tis_attempt_receive(tpmState *s, uint8_t locty)
   3.444 +{
   3.445 +    /*
   3.446 +     * Attempt to read from the vTPM here if
   3.447 +     * - not aborting a command
   3.448 +     * - command has been sent and state is 'EXECUTION' now
   3.449 +     * - no data are already available (data have already been read)
   3.450 +     * - there's a communication path to the vTPM established
   3.451 +     */
   3.452 +    if (!IS_VALID_LOC(s->aborting_locty)) {
   3.453 +        if (s->loc[locty].state == STATE_EXECUTION) {
   3.454 +            if (0 == (s->loc[locty].sts & STS_DATA_AVAILABLE)){
   3.455 +                if (IS_COMM_WITH_VTPM(s)) {
   3.456 +                    int n = TPM_Receive(s, &s->buffer);
   3.457 +                    if (n > 0) {
   3.458 +                        s->loc[locty].sts = STS_VALID | STS_DATA_AVAILABLE;
   3.459 +                        s->loc[locty].state = STATE_COMPLETION;
   3.460 +                        close_vtpm_channel(s, FORCE_CLOSE);
   3.461 +                        tis_raise_irq(s, locty, INT_DATA_AVAILABLE);
   3.462 +                    }
   3.463 +                }
   3.464 +            }
   3.465 +        }
   3.466 +    }
   3.467 +}
   3.468 +
   3.469 +/*
   3.470 + * Read a register of the TIS interface
   3.471 + * See specs pages 33-63 for description of the registers
   3.472 + */
   3.473 +static uint32_t tis_mem_readl(void *opaque, target_phys_addr_t addr)
   3.474 +{
   3.475 +    tpmState *s = (tpmState *)opaque;
   3.476 +    uint16_t offset = addr & 0xffc;
   3.477 +    uint8_t shift = (addr & 0x3) * 8;
   3.478 +    uint32_t val = 0;
   3.479 +    uint8_t locty = locality_from_addr(addr);
   3.480 +
   3.481 +    if (offset == TPM_REG_ACCESS) {
   3.482 +        if (s->active_loc == locty) {
   3.483 +            s->loc[locty].access |= (1 << 5);
   3.484 +         } else {
   3.485 +            s->loc[locty].access &= ~(1 << 5);
   3.486 +        }
   3.487 +        val = s->loc[locty].access;
   3.488 +    } else
   3.489 +    if (offset == TPM_REG_INT_ENABLE) {
   3.490 +        val = s->loc[locty].inte;
   3.491 +    } else
   3.492 +    if (offset == TPM_REG_INT_VECTOR) {
   3.493 +        val = s->irq;
   3.494 +    } else
   3.495 +    if (offset == TPM_REG_INT_STATUS) {
   3.496 +        tis_attempt_receive(s, locty);
   3.497 +        val = s->loc[locty].ints;
   3.498 +    } else
   3.499 +    if (offset == TPM_REG_INTF_CAPABILITY) {
   3.500 +        val = CAPABILITIES_SUPPORTED;
   3.501 +    } else
   3.502 +    if (offset == TPM_REG_STS) { /* status register */
   3.503 +        tis_attempt_receive(s, locty);
   3.504 +        val = (sizeof(s->buffer.buf) - s->offset) << 8 | s->loc[locty].sts;
   3.505 +    } else
   3.506 +    if (offset == TPM_REG_DATA_FIFO) {
   3.507 +      val = tpm_data_read(s, locty);
   3.508 +    } else
   3.509 +    if (offset == TPM_REG_DID_VID) {
   3.510 +        val = (TPM_DID << 16) | TPM_VID;
   3.511 +    } else
   3.512 +    if (offset == TPM_REG_RID) {
   3.513 +         val = TPM_RID;
   3.514 +    }
   3.515 +
   3.516 +    if (shift)
   3.517 +        val >>= shift;
   3.518 +
   3.519 +#ifdef DEBUG_TPM
   3.520 +    fprintf(logfile," read(%08x) = %08x\n",
   3.521 +            addr,
   3.522 +            val);
   3.523 +#endif
   3.524 +
   3.525 +    return val;
   3.526 +}
   3.527 +
   3.528 +/*
   3.529 + * Write a value to a register of the TIS interface
   3.530 + * See specs pages 33-63 for description of the registers
   3.531 + */
   3.532 +static void tis_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
   3.533 +{
   3.534 +    tpmState* s=(tpmState*)opaque;
   3.535 +    uint16_t off = addr & 0xfff;
   3.536 +    uint8_t locty = locality_from_addr(addr);
   3.537 +    int n, c;
   3.538 +    uint32_t len;
   3.539 +
   3.540 +#ifdef DEBUG_TPM
   3.541 +    fprintf(logfile,"write(%08x) = %08x\n",
   3.542 +            addr,
   3.543 +            val);
   3.544 +#endif
   3.545 +
   3.546 +    if (off == TPM_REG_ACCESS) {
   3.547 +        if (val & ACCESS_ACTIVE_LOCALITY) {
   3.548 +            /* give up locality if currently owned */
   3.549 +            if (s->active_loc == locty) {
   3.550 +                uint8_t newlocty = NO_LOCALITY;
   3.551 +                s->loc[locty].access &= ~(ACCESS_PENDING_REQUEST);
   3.552 +                /* anybody wants the locality ? */
   3.553 +                for (c = NUM_LOCALITIES - 1; c >= 0; c--) {
   3.554 +                    if (s->loc[c].access & ACCESS_REQUEST_USE) {
   3.555 +                        s->loc[c].access |= ACCESS_TPM_REG_VALID_STS;
   3.556 +                        s->loc[c].access &= ~ACCESS_REQUEST_USE;
   3.557 +                        newlocty = c;
   3.558 +                        break;
   3.559 +                    }
   3.560 +                }
   3.561 +                tis_prep_abort(s, locty, newlocty);
   3.562 +            }
   3.563 +        }
   3.564 +        if (val & ACCESS_BEEN_SEIZED) {
   3.565 +            /* clear the flag */
   3.566 +            s->loc[locty].access &= ~ACCESS_BEEN_SEIZED;
   3.567 +        }
   3.568 +        if (val & ACCESS_SEIZE) {
   3.569 +            if (locty > s->active_loc && IS_VALID_LOC(s->active_loc)) {
   3.570 +                s->loc[s->active_loc].access |= ACCESS_BEEN_SEIZED;
   3.571 +                s->loc[locty].access = ACCESS_TPM_REG_VALID_STS;
   3.572 +                tis_prep_abort(s, s->active_loc, locty);
   3.573 +            }
   3.574 +        }
   3.575 +        if (val & ACCESS_REQUEST_USE) {
   3.576 +            if (IS_VALID_LOC(s->active_loc)) {
   3.577 +                /* locality election */
   3.578 +                s->loc[s->active_loc].access |= ACCESS_PENDING_REQUEST;
   3.579 +            } else {
   3.580 +                /* no locality active -> make this one active now */
   3.581 +                s->loc[locty].access |= ACCESS_ACTIVE_LOCALITY;
   3.582 +                s->active_loc = locty;
   3.583 +                tis_raise_irq(s, locty, INT_LOCALITY_CHANGED);
   3.584 +            }
   3.585 +        }
   3.586 +    } else
   3.587 +    if (off == TPM_REG_INT_ENABLE) {
   3.588 +        s->loc[locty].inte = (val & (INT_ENABLED | (0x3 << 3) |
   3.589 +                                     INTERRUPTS_SUPPORTED));
   3.590 +    } else
   3.591 +    if (off == TPM_REG_INT_STATUS) {
   3.592 +        /* clearing of interrupt flags */
   3.593 +        if ((val & INTERRUPTS_SUPPORTED) &&
   3.594 +            (s->loc[locty].ints & INTERRUPTS_SUPPORTED)) {
   3.595 +            s->set_irq(s->irq_opaque, s->irq, 0);
   3.596 +            s->irq_pending = 0;
   3.597 +        }
   3.598 +        s->loc[locty].ints &= ~(val & INTERRUPTS_SUPPORTED);
   3.599 +    } else
   3.600 +    if (off == TPM_REG_STS) {
   3.601 +        if (val & STS_COMMAND_READY) {
   3.602 +            if (s->loc[locty].state == STATE_IDLE) {
   3.603 +                s->loc[locty].sts   = STS_COMMAND_READY;
   3.604 +                s->loc[locty].state = STATE_READY;
   3.605 +                tis_raise_irq(s, locty, INT_COMMAND_READY);
   3.606 +            } else if (s->loc[locty].state == STATE_COMPLETION ||
   3.607 +                       s->loc[locty].state == STATE_EXECUTION  ||
   3.608 +                       s->loc[locty].state == STATE_RECEPTION) {
   3.609 +                /* abort currently running command */
   3.610 +                tis_prep_abort(s, locty, locty);
   3.611 +            }
   3.612 +        }
   3.613 +        if (val & STS_TPM_GO) {
   3.614 +            n = TPM_Send(s, &s->buffer,"tpm_data_write");
   3.615 +            if (n > 0) {
   3.616 +                /* sending of data was successful */
   3.617 +                s->offset = 0;
   3.618 +                s->loc[locty].state = STATE_EXECUTION;
   3.619 +                if (s->loc[locty].inte & (INT_ENABLED | INT_DATA_AVAILABLE)) {
   3.620 +                    s->poll_attempts = 0;
   3.621 +                    tis_prep_next_interrupt(s);
   3.622 +                }
   3.623 +            }
   3.624 +        }
   3.625 +        if (val & STS_RESPONSE_RETRY) {
   3.626 +            s->offset = 0;
   3.627 +        }
   3.628 +    } else if (off == TPM_REG_DATA_FIFO) {
   3.629 +        /* data fifo */
   3.630 +        if (s->loc[locty].state == STATE_IDLE ||
   3.631 +            s->loc[locty].state == STATE_EXECUTION ||
   3.632 +            s->loc[locty].state == STATE_COMPLETION) {
   3.633 +            /* drop the byte */
   3.634 +        } else {
   3.635 +#ifdef TPM_DEBUG
   3.636 +        fprintf(logfile,"Byte to send to TPM: %02x\n", val);
   3.637 +#endif
   3.638 +            s->loc[locty].state = STATE_RECEPTION;
   3.639 +
   3.640 +            if (s->offset < sizeof(s->buffer.buf))
   3.641 +                s->buffer.buf[s->offset++] = (uint8_t)val;
   3.642 +
   3.643 +            if (s->offset > 5) {
   3.644 +                /* we have a packet length - see if we have all of it */
   3.645 +                len = tpm_get_size_from_buffer(s->buffer.buf);
   3.646 +                if (len > s->offset) {
   3.647 +                    s->loc[locty].sts = STS_EXPECT | STS_VALID;
   3.648 +                } else {
   3.649 +                    s->loc[locty].sts = STS_VALID;
   3.650 +                }
   3.651 +            }
   3.652 +        }
   3.653 +    }
   3.654 +}
   3.655 +
   3.656 +/*
   3.657 + * Prepare the next interrupt for example after a command has
   3.658 + * been sent out for the purpose of receiving the response.
   3.659 + * Depending on how many interrupts (used for polling on the fd) have
   3.660 + * already been schedule, this function determines the delta in time
   3.661 + * to the next interrupt. This accomodates for commands that finish
   3.662 + * quickly.
   3.663 + */
   3.664 +static void tis_prep_next_interrupt(tpmState *s)
   3.665 +{
   3.666 +    int64_t expiration;
   3.667 +    int rate = 5; /* 5 times per second */
   3.668 +
   3.669 +    /*
   3.670 +       poll often at the beginning for quickly finished commands,
   3.671 +       then back off
   3.672 +     */
   3.673 +    if (s->poll_attempts < 5) {
   3.674 +        rate = 20;
   3.675 +    } else if (s->poll_attempts < 10) {
   3.676 +        rate = 10;
   3.677 +    }
   3.678 +
   3.679 +    expiration = qemu_get_clock(vm_clock) + (ticks_per_sec / rate);
   3.680 +    qemu_mod_timer(s->poll_timer, expiration);
   3.681 +    s->poll_attempts++;
   3.682 +}
   3.683 +
   3.684 +
   3.685 +/*
   3.686 + * The polling routine called when the 'timer interrupt' fires.
   3.687 + * Tries to receive a command from the vTPM.
   3.688 + */
   3.689 +static void tis_poll_timer(void *opaque)
   3.690 +{
   3.691 +    tpmState* s=(tpmState*)opaque;
   3.692 +    uint8_t locty = s->active_loc;
   3.693 +
   3.694 +    if (!IS_VALID_LOC(locty) ||
   3.695 +        (!(s->loc[locty].inte & INT_ENABLED) &&
   3.696 +          (s->aborting_locty != NO_LOCALITY)) ||
   3.697 +        !IS_COMM_WITH_VTPM(s)) {
   3.698 +        /* no more interrupts requested, so no more polling needed */
   3.699 +        qemu_del_timer(s->poll_timer);
   3.700 +    }
   3.701 +
   3.702 +    if (!IS_COMM_WITH_VTPM(s)) {
   3.703 +        if (s->aborting_locty != NO_LOCALITY) {
   3.704 +            tis_abort(s);
   3.705 +        }
   3.706 +        return;
   3.707 +    }
   3.708 +
   3.709 +    if (s->aborting_locty != NO_LOCALITY) {
   3.710 +        int n = TPM_Receive(s, &s->buffer);
   3.711 +#ifdef DEBUG_TPM
   3.712 +        fprintf(logfile,"Receiving for abort.\n");
   3.713 +#endif
   3.714 +        if (n > 0) {
   3.715 +            close_vtpm_channel(s, FORCE_CLOSE);
   3.716 +            tis_abort(s);
   3.717 +#ifdef DEBUG_TPM
   3.718 +            fprintf(logfile,"Abort is complete.\n");
   3.719 +#endif
   3.720 +        } else {
   3.721 +            tis_prep_next_interrupt(s);
   3.722 +        }
   3.723 +    } else if (IS_VALID_LOC(locty)) {
   3.724 +        if (s->loc[locty].state == STATE_EXECUTION) {
   3.725 +           /* poll for result */
   3.726 +            int n = TPM_Receive(s, &s->buffer);
   3.727 +            if (n > 0) {
   3.728 +                s->loc[locty].sts = STS_VALID | STS_DATA_AVAILABLE;
   3.729 +                s->loc[locty].state = STATE_COMPLETION;
   3.730 +                close_vtpm_channel(s, FORCE_CLOSE);
   3.731 +                tis_raise_irq(s, locty, INT_DATA_AVAILABLE);
   3.732 +            } else {
   3.733 +                /* nothing received */
   3.734 +                tis_prep_next_interrupt(s);
   3.735 +            }
   3.736 +        }
   3.737 +    }
   3.738 +}
   3.739 +
   3.740 +
   3.741 +static CPUReadMemoryFunc *tis_readfn[3]={
   3.742 +    tis_mem_readl,
   3.743 +    tis_mem_readl,
   3.744 +    tis_mem_readl
   3.745 +};
   3.746 +
   3.747 +static CPUWriteMemoryFunc *tis_writefn[3]={
   3.748 +    tis_mem_writel,
   3.749 +    tis_mem_writel,
   3.750 +    tis_mem_writel
   3.751 +};
   3.752 +
   3.753 +/*
   3.754 + * Save the internal state of this interface for later resumption.
   3.755 + * Need to get any outstanding responses from the vTPM back, so
   3.756 + * this might delay the suspend for a while.
   3.757 + */
   3.758 +static void tpm_save(QEMUFile* f,void* opaque)
   3.759 +{
   3.760 +    tpmState* s=(tpmState*)opaque;
   3.761 +    int c;
   3.762 +
   3.763 +    /* need to wait for outstanding requests to complete */
   3.764 +    if (IS_COMM_WITH_VTPM(s)) {
   3.765 +        int repeats = 30; /* 30 seconds; really should be infty */
   3.766 +        while (repeats > 0 &&
   3.767 +               !(s->loc[s->active_loc].sts & STS_DATA_AVAILABLE)) {
   3.768 +            int n = TPM_Receive(s, &s->buffer);
   3.769 +            if (n > 0) {
   3.770 +                if (IS_VALID_LOC(s->active_loc)) {
   3.771 +                    s->loc[s->active_loc].sts = STS_VALID | STS_DATA_AVAILABLE;
   3.772 +                }
   3.773 +                /* close the connection with the vTPM for good */
   3.774 +                close_vtpm_channel(s, 1);
   3.775 +                break;
   3.776 +            }
   3.777 +            sleep(1);
   3.778 +        }
   3.779 +    }
   3.780 +
   3.781 +    qemu_put_be32s(f,&s->offset);
   3.782 +    qemu_put_buffer(f, s->buffer.buf, TPM_MAX_PKT);
   3.783 +    qemu_put_8s(f, &s->active_loc);
   3.784 +    qemu_put_8s(f, &s->irq_pending);
   3.785 +    for (c = 0; c < NUM_LOCALITIES; c++) {
   3.786 +        qemu_put_be32s(f, &s->loc[c].state);
   3.787 +        qemu_put_8s(f, &s->loc[c].access);
   3.788 +        qemu_put_8s(f, &s->loc[c].sts);
   3.789 +        qemu_put_be32s(f, &s->loc[c].inte);
   3.790 +        qemu_put_be32s(f, &s->loc[c].ints);
   3.791 +    }
   3.792 +}
   3.793 +
   3.794 +/*
   3.795 + * load TIS interface state
   3.796 + */
   3.797 +static int tpm_load(QEMUFile* f,void* opaque,int version_id)
   3.798 +{
   3.799 +    tpmState* s=(tpmState*)opaque;
   3.800 +    int c;
   3.801 +
   3.802 +    if (version_id != 1)
   3.803 +        return -EINVAL;
   3.804 +
   3.805 +    qemu_get_be32s(f,&s->offset);
   3.806 +    qemu_get_buffer(f, s->buffer.buf, TPM_MAX_PKT);
   3.807 +    qemu_get_8s(f, &s->active_loc);
   3.808 +    qemu_get_8s(f, &s->irq_pending);
   3.809 +    for (c = 0; c < NUM_LOCALITIES; c++) {
   3.810 +        qemu_get_be32s(f, &s->loc[c].state);
   3.811 +        qemu_get_8s(f, &s->loc[c].access);
   3.812 +        qemu_get_8s(f, &s->loc[c].sts);
   3.813 +        qemu_get_be32s(f, &s->loc[c].inte);
   3.814 +        qemu_get_be32s(f, &s->loc[c].ints);
   3.815 +    }
   3.816 +
   3.817 +    /* need to be able to get the instance number from the xenstore */
   3.818 +    s->vtpm_instance = vtpm_instance_from_xenstore();
   3.819 +    if (s->vtpm_instance == VTPM_BAD_INSTANCE)
   3.820 +        return -EINVAL;
   3.821 +    tpm_initialize_instance(s, s->vtpm_instance);
   3.822 +
   3.823 +    return 0;
   3.824 +}
   3.825 +
   3.826 +
   3.827 +typedef struct LPCtpmState {
   3.828 +    tpmState tpm;
   3.829 +    int mem;
   3.830 +} LPCtpmState;
   3.831 +
   3.832 +
   3.833 +/*
   3.834 + * initialize TIS interface
   3.835 + */
   3.836 +void tpm_tis_init(SetIRQFunc *set_irq, void *opaque, int irq)
   3.837 +{
   3.838 +    LPCtpmState *d;
   3.839 +    tpmState *s;
   3.840 +    int c = 0;
   3.841 +    uint32_t vtpm_in;
   3.842 +
   3.843 +    vtpm_in = vtpm_instance_from_xenstore();
   3.844 +    /* no valid vtpm instance -> no device */
   3.845 +    if (vtpm_in == VTPM_BAD_INSTANCE)
   3.846 +        return;
   3.847 +
   3.848 +    d = qemu_mallocz(sizeof(LPCtpmState));
   3.849 +    d->mem = cpu_register_io_memory(0, tis_readfn, tis_writefn, d);
   3.850 +
   3.851 +    if (d->mem == -1) {
   3.852 +       return;
   3.853 +    }
   3.854 +
   3.855 +    cpu_register_physical_memory(TIS_ADDR_BASE,
   3.856 +                                 0x1000 * NUM_LOCALITIES, d->mem);
   3.857 +
   3.858 +    /* initialize tpmState */
   3.859 +    s = &d->tpm;
   3.860 +
   3.861 +    s->offset = 0;
   3.862 +    s->active_loc = NO_LOCALITY;
   3.863 +
   3.864 +    while (c < NUM_LOCALITIES) {
   3.865 +        s->loc[c].access = (1 << 7);
   3.866 +        s->loc[c].sts = 0;
   3.867 +        s->loc[c].inte = (1 << 3);
   3.868 +        s->loc[c].ints = 0;
   3.869 +        s->loc[c].state = STATE_IDLE;
   3.870 +        c++;
   3.871 +    }
   3.872 +    s->poll_timer = qemu_new_timer(vm_clock, tis_poll_timer, s);
   3.873 +    s->set_irq = set_irq;
   3.874 +    s->irq_opaque = opaque;
   3.875 +    s->irq = irq;
   3.876 +    s->vtpm_instance = vtpm_in;
   3.877 +    s->Transmitlayer = -1;
   3.878 +    s->tpmTx.fd[0] = -1;
   3.879 +    s->tpmTx.fd[1] = -1;
   3.880 +
   3.881 +    tpm_initialize_instance(s, s->vtpm_instance);
   3.882 +    memset(s->buffer.buf,0,sizeof(s->buffer.buf));
   3.883 +
   3.884 +    register_savevm("tpm-tis", 0, 1, tpm_save, tpm_load, s);
   3.885 +}
   3.886 +
   3.887 +/****************************************************************************/
   3.888 +/*  optional verbose logging of data to/from vtpm                           */
   3.889 +/****************************************************************************/
   3.890 +#ifdef DEBUG_TPM
   3.891 +static void showBuff(unsigned char *buff, char *string)
   3.892 +{
   3.893 +    uint32_t i, len;
   3.894 +
   3.895 +    len = tpm_get_size_from_buffer(buff);
   3.896 +    fprintf(logfile,"%s length = %d\n", string, len);
   3.897 +    for (i = 0; i < len; i++) {
   3.898 +        if (i && !(i % 16)) {
   3.899 +            fprintf(logfile,"\n");
   3.900 +        }
   3.901 +        fprintf(logfile,"%.2X ", buff[i]);
   3.902 +    }
   3.903 +    fprintf(logfile,"\n");
   3.904 +}
   3.905 +#endif
   3.906 +
   3.907 +/****************************************************************************/
   3.908 +/* Transmit request to TPM and read Response                                */
   3.909 +/****************************************************************************/
   3.910 +
   3.911 +const static unsigned char tpm_failure[] = {
   3.912 +    0x00, 0x00,
   3.913 +    0x00, 0x00, 0x00, 0x0a,
   3.914 +    0x00, 0x00, 0x00, 0x09
   3.915 +};
   3.916 +
   3.917 +
   3.918 +/*
   3.919 + * Send a TPM request.
   3.920 + */
   3.921 +static int TPM_Send(tpmState *s, tpmBuffer *buffer, char *msg)
   3.922 +{
   3.923 +    int len;
   3.924 +    uint32_t size = tpm_get_size_from_buffer(buffer->buf);
   3.925 +
   3.926 +    /* try to establish a connection to the vTPM */
   3.927 +    if ( !IS_COMM_WITH_VTPM(s)) {
   3.928 +        open_vtpm_channel(s);
   3.929 +    }
   3.930 +
   3.931 +    if ( !IS_COMM_WITH_VTPM(s)) {
   3.932 +        unsigned char tag = buffer->buf[1];
   3.933 +
   3.934 +        /* there's a failure response from the TPM */
   3.935 +        memcpy(buffer->buf, tpm_failure, sizeof(tpm_failure));
   3.936 +        buffer->buf[1] = tag + 3;
   3.937 +        if (IS_VALID_LOC(s->active_loc)) {
   3.938 +            s->loc[s->active_loc].sts = STS_DATA_AVAILABLE | STS_VALID;
   3.939 +        }
   3.940 +#ifdef DEBUG_TPM
   3.941 +        fprintf(logfile,"No TPM running!\n");
   3.942 +#endif
   3.943 +        /* the request went out ok. */
   3.944 +        return sizeof(buffer->instance) + size;
   3.945 +    }
   3.946 +
   3.947 +#ifdef DEBUG_TPM
   3.948 +    showBuff(buffer->buf, "To TPM");
   3.949 +#endif
   3.950 +
   3.951 +    len = vTPMTransmit[s->Transmitlayer].write(s, buffer);
   3.952 +    if (len < 0) {
   3.953 +        s->Transmitlayer = -1;
   3.954 +    }
   3.955 +    return len;
   3.956 +}
   3.957 +
   3.958 +/*
   3.959 + * Try to receive data from the file descriptor. Since it is in
   3.960 + * non-blocking mode it is possible that no data are actually received -
   3.961 + * whatever calls this function needs to try again later.
   3.962 + */
   3.963 +static int TPM_Receive(tpmState *s, tpmBuffer *buffer)
   3.964 +{
   3.965 +    int off;
   3.966 +
   3.967 +    off = vTPMTransmit[s->Transmitlayer].read(s, buffer);
   3.968 +
   3.969 +    if (off < 0) {
   3.970 +        /* EAGAIN is set in errno due to non-blocking mode */
   3.971 +        return -1;
   3.972 +    }
   3.973 +
   3.974 +    if (off == 0) {
   3.975 +#ifdef DEBUG_TPM
   3.976 +        fprintf(logfile,"TPM GONE? errno=%d\n",errno);
   3.977 +#endif
   3.978 +        close_vtpm_channel(s, 1);
   3.979 +        /* pretend that data are available */
   3.980 +        if (IS_VALID_LOC(s->active_loc)) {
   3.981 +            s->loc[s->active_loc].sts = STS_VALID | STS_DATA_AVAILABLE;
   3.982 +            s->loc[s->active_loc].state = STATE_COMPLETION;
   3.983 +            tis_raise_irq(s, s->active_loc, INT_DATA_AVAILABLE);
   3.984 +        }
   3.985 +        return -1;
   3.986 +    }
   3.987 +
   3.988 +#ifdef DEBUG_TPM
   3.989 +    if (off > sizeof(buffer->instance ) + 6) {
   3.990 +        uint32_t size = tpm_get_size_from_buffer(buffer->buf);
   3.991 +        if (size + sizeof(buffer->instance) != off) {
   3.992 +            fprintf(logfile,"TPM: Packet size is bad! %d != %d\n",
   3.993 +                    size + sizeof(buffer->instance),
   3.994 +                    off);
   3.995 +        } else {
   3.996 +            uint32_t ret;
   3.997 +            showBuff(buffer->buf, "From TPM");
   3.998 +            ret = (buffer->buf[8])*256 + buffer->buf[9];
   3.999 +            if (ret)
  3.1000 +                fprintf(logfile,"Receive failed with error %d\n", ret);
  3.1001 +            else
  3.1002 +                fprintf(logfile,"Receive succeeded. Got response of length %d (=%d)\n",
  3.1003 +                       size, off);
  3.1004 +        }
  3.1005 +    }
  3.1006 +#endif
  3.1007 +
  3.1008 +    /* assuming reading in one chunk for now */
  3.1009 +    return off;
  3.1010 +}
  3.1011 +
  3.1012 +
  3.1013 +/****************************************************************************
  3.1014 +   Helper functions for reading data from the xenstore such as
  3.1015 +   reading virtual TPM instance information
  3.1016 + ****************************************************************************/
  3.1017 +int has_tpm_device(void)
  3.1018 +{
  3.1019 +    int ret = 0;
  3.1020 +    struct xs_handle *handle = xs_daemon_open();
  3.1021 +    if (handle) {
  3.1022 +        ret = xenstore_domain_has_devtype(handle, "vtpm");
  3.1023 +        xs_daemon_close(handle);
  3.1024 +    }
  3.1025 +    return ret;
  3.1026 +}
  3.1027 +
  3.1028 +
  3.1029 +/*
  3.1030 + * Wait until hotplug scripts have finished then read the vTPM instance
  3.1031 + * number from the xenstore.
  3.1032 + */
  3.1033 +static uint32_t vtpm_instance_from_xenstore(void)
  3.1034 +{
  3.1035 +    unsigned int num;
  3.1036 +    uint32_t number = VTPM_BAD_INSTANCE;
  3.1037 +    int end = 0;
  3.1038 +    char *token = "tok";
  3.1039 +    int subscribed = 0;
  3.1040 +    int ctr = 0;
  3.1041 +    fd_set readfds;
  3.1042 +
  3.1043 +    struct xs_handle *handle = xs_daemon_open();
  3.1044 +
  3.1045 +    FD_ZERO(&readfds);
  3.1046 +
  3.1047 +    if (handle) {
  3.1048 +        char **e = xenstore_domain_get_devices(handle, "vtpm", &num);
  3.1049 +        int fd = xs_fileno(handle);
  3.1050 +        FD_SET(fd, &readfds);
  3.1051 +        if (e) {
  3.1052 +            do {
  3.1053 +                struct timeval tv = {
  3.1054 +                    .tv_sec  = 30,
  3.1055 +                    .tv_usec = 0,
  3.1056 +                };
  3.1057 +                /* need to make sure that the hotplug scripts have finished */
  3.1058 +                char *status = xenstore_read_hotplug_status(handle,
  3.1059 +                                                            "vtpm",
  3.1060 +                                                            e[0]);
  3.1061 +                if (status) {
  3.1062 +                    if (!strcmp(status, "connected")) {
  3.1063 +                        char *inst = xenstore_backend_read_variable(handle,
  3.1064 +                                                                    "vtpm",
  3.1065 +                                                                    e[0],
  3.1066 +                                                                   "instance");
  3.1067 +                        if (1 != (sscanf(inst,"%d",&number)))
  3.1068 +                            number = VTPM_BAD_INSTANCE;
  3.1069 +                        free(inst);
  3.1070 +                    } else {
  3.1071 +                        fprintf(logfile,
  3.1072 +                                "bad status '%s' from vtpm hotplug\n",
  3.1073 +                                status);
  3.1074 +                    }
  3.1075 +                    free(status);
  3.1076 +                    end = 1;
  3.1077 +                } else {
  3.1078 +                    /* no status, yet */
  3.1079 +                    int rc;
  3.1080 +                    unsigned int nr;
  3.1081 +                    char **f;
  3.1082 +
  3.1083 +                    if (!subscribed) {
  3.1084 +                        rc = xenstore_subscribe_to_hotplug_status(handle,
  3.1085 +                                                                  "vtpm",
  3.1086 +                                                                  e[0],
  3.1087 +                                                                  token);
  3.1088 +                        if (rc != 0)
  3.1089 +                            break;
  3.1090 +                        subscribed = 1;
  3.1091 +                    }
  3.1092 +                    rc = select(fd+1, &readfds, NULL, NULL, &tv);
  3.1093 +                    /* get what's available -- drain the fd */
  3.1094 +                    f = xs_read_watch(handle, &nr);
  3.1095 +                    ctr++;
  3.1096 +                    free(f);
  3.1097 +                    if (ctr > 2)
  3.1098 +                        end = 1;
  3.1099 +                }
  3.1100 +            } while (end == 0);
  3.1101 +            free(e);
  3.1102 +        }
  3.1103 +        if (subscribed) {
  3.1104 +            /* clean up */
  3.1105 +            xenstore_unsubscribe_from_hotplug_status(handle,
  3.1106 +                                                     "vtpm",
  3.1107 +                                                     e[0],
  3.1108 +                                                     token);
  3.1109 +        }
  3.1110 +        xs_daemon_close(handle);
  3.1111 +    }
  3.1112 +    if (number == VTPM_BAD_INSTANCE)
  3.1113 +        fprintf(logfile, "no valid vtpm instance");
  3.1114 +    else
  3.1115 +        fprintf(logfile,"vtpm instance:%d\n",number);
  3.1116 +    return number;
  3.1117 +}
     4.1 --- a/tools/ioemu/vl.h	Fri Oct 27 19:13:51 2006 +0100
     4.2 +++ b/tools/ioemu/vl.h	Fri Oct 27 19:15:48 2006 +0100
     4.3 @@ -929,6 +929,10 @@ extern int acpi_enabled;
     4.4  void piix4_pm_init(PCIBus *bus, int devfn);
     4.5  void acpi_bios_init(void);
     4.6  
     4.7 +/* tpm_tis.c */
     4.8 +int has_tpm_device(void);
     4.9 +void tpm_tis_init(SetIRQFunc *set_irq, void *irq_opaque, int irq);
    4.10 +
    4.11  /* piix4acpi.c */
    4.12  extern void pci_piix4_acpi_init(PCIBus *bus, int devfn);
    4.13