]> scripts.mit.edu Git - xen.git/blob - scripts/vtpm-impl
Allow memory to balloon to 8GiB
[xen.git] / scripts / vtpm-impl
1 #!/bin/bash
2 # ===================================================================
3
4 # Copyright (c) 2005, Intel Corp.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without 
8 # modification, are permitted provided that the following conditions 
9 # are met:
10 #
11 #   * Redistributions of source code must retain the above copyright 
12 #     notice, this list of conditions and the following disclaimer.
13 #   * Redistributions in binary form must reproduce the above 
14 #     copyright notice, this list of conditions and the following 
15 #     disclaimer in the documentation and/or other materials provided 
16 #     with the distribution.
17 #   * Neither the name of Intel Corporation nor the names of its 
18 #     contributors may be used to endorse or promote products derived
19 #     from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
28 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
30 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
31 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 # OF THE POSSIBILITY OF SUCH DAMAGE.
33 # ===================================================================
34
35 #            |        SRC        |    TAG  |      CMD SIZE     |        ORD       |mtype|strt
36 TPM_CMD_OPEN=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x01
37 TPM_CMD_RESM=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x02
38 TPM_CMD_CLOS=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x02
39 TPM_CMD_DELE=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x03
40
41 TPM_TYPE_PVM=\\x01
42 TPM_TYPE_HVM=\\x02
43
44 TPM_SUCCESS=00000000
45
46 TX_VTPM_MANAGER=/var/vtpm/fifos/from_console.fifo
47 RX_VTPM_MANAGER=/var/vtpm/fifos/to_console.fifo
48
49 VTPM_MIG=/usr/bin/vtpm_migrator
50
51 # -------------------- Helpers for binary streams -----------
52
53 function str_to_hex32() {
54  printf "%0.8x" $1
55 }
56
57 function hex32_to_bin() {
58  local inst=$(str_to_hex32 $1);
59  
60  local n1=`echo $inst | sed 's/\(..\)....../\\\\x\1/'`
61  local n2=`echo $inst | sed 's/..\(..\)..../\\\\x\1/'`
62  local n3=`echo $inst | sed 's/....\(..\)../\\\\x\1/'`
63  local n4=`echo $inst | sed 's/......\(..\)/\\\\x\1/'`
64
65  echo "$n1$n2$n3$n4"
66 }
67
68 function vtpm_manager_cmd() {
69  local cmd=$1;
70  local inst=$2;
71  local inst_bin=$(hex32_to_bin $inst);
72
73  claim_lock vtpm_mgr
74
75  #send cmd to vtpm_manager
76  printf "$cmd$inst_bin" > $TX_VTPM_MANAGER
77
78  #recv response
79  set +e
80  local resp_hex=`dd skip=10 bs=1 count=4 if=$RX_VTPM_MANAGER 2> /dev/null | xxd -ps`
81  set -e
82
83  release_lock vtpm_mgr
84
85  #return whether the command was successful
86  if [ $resp_hex -ne $TPM_SUCCESS ]; then
87    vtpm_fatal_error=1
88    false
89   else
90    true
91  fi
92 }
93
94 # Helper to get vm type to pass to vtpm_manager open/resume
95 function vtpm_get_type() {
96  local inst=$(xenstore_read $XENBUS_PATH/frontend-id)
97  local vm=$(xenstore_read /local/domain/$inst/vm)
98  if [ "$vm" != "" ]; then
99   local ostype=$(xenstore-read $vm/image/ostype)
100   if [ "$ostype" == "hvm" ]; then
101    echo $TPM_TYPE_HVM;
102   else
103    echo $TPM_TYPE_PVM;
104   fi
105  fi
106 }
107
108 # ------------------ Command handlers -----------------
109
110 # Create new vtpm instance & set it up for use
111 function vtpm_create () {
112  # Creation is handled implicitly by the manager on first setup
113  # so just set it up for use
114  $(vtpm_start $1)
115 }
116
117 # Setup vtpm instance for use.
118 function vtpm_start() {
119  local vmtype=$(vtpm_get_type);
120  $(vtpm_manager_cmd $TPM_CMD_OPEN$vmtype $1)
121 }
122
123 function vtpm_resume() {
124  local vmtype=$(vtpm_get_type);
125  $(vtpm_manager_cmd $TPM_CMD_RESM$vmtype $1)
126 }
127
128 # Reset the vtpm AKA clear PCRs
129 function vtpm_reset() {
130  #not used by current implemenation
131  true
132 }
133
134 # Shutdown the vtpm while the vm is down
135 # This could be a suspend of shutdown
136 # we cannot distinquish, so save the state
137 # and decide on startup if we should keep is
138 function vtpm_suspend() {
139  $(vtpm_manager_cmd $TPM_CMD_CLOS $1)
140 }
141
142
143 function vtpm_delete() {
144  local inst=$1
145  if $(vtpm_manager_cmd $TPM_CMD_DELE $inst); then
146    rm -f /var/vtpm/vtpm_dm_$1.data
147    true
148  else 
149    vtpm_fatal_error=1
150    false
151  fi
152 }
153
154 # Perform a migration step. This function differentiates between migration
155 # to the local host or to a remote machine.
156 # Parameters:
157 # 1st: destination host to migrate to
158 # 2nd: name of the domain to migrate
159 # 3rd: the migration step to perform
160 function vtpm_migrate() {
161  local instance res
162
163  instance=$(vtpmdb_find_instance $2)
164  if [ "$instance" == "" ]; then
165   log err "VTPM Migratoin failed. Unable to translation of domain name"
166   echo "Error: VTPM Migration failed while looking up instance number"
167  fi
168
169  case "$3" in
170   0)
171    #Incicate migration supported
172    echo "0" 
173   ;;
174
175   1)
176    # Get Public Key from Destination
177    # Call vtpm_manager's migration part 1
178    claim_lock vtpm_mgr
179    $VTPM_MIG $1 $2 $instance $3
180    release_lock vtpm_mgr
181   ;;
182
183   2)
184    # Call manager's migration step 2 and send result to destination
185    # If successful remove from db
186    claim_lock vtpm_mgr
187    $VTPM_MIG $1 $2 $instance $3
188    release_lock vtpm_mgr
189   ;;
190
191   3)
192    if `ps x | grep "$VTPM_MIG $1"`; then
193     log err "VTPM Migration failed to complete."
194     echo "Error: VTPM Migration failed to complete."
195    fi
196   ;;
197  esac
198  
199 }
200
201
202 function vtpm_migrate_recover() {
203  echo "Error: Recovery not supported yet" 
204 }
205
206 function vtpm_migrate_local() {
207  echo "Error: local vTPM migration not supported"
208 }