summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/SmmControl2Dxe/SmiFeatures.c
blob: 73c29848b334f1f9b0daa23de97f8a2b06b71eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**@file
  Negotiate SMI features with QEMU, and configure UefiCpuPkg/PiSmmCpuDxeSmm
  accordingly.

  Copyright (C) 2016-2017, Red Hat, Inc.

  This program and the accompanying materials are licensed and made available
  under the terms and conditions of the BSD License which accompanies this
  distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PcdLib.h>
#include <Library/QemuFwCfgLib.h>

#include "SmiFeatures.h"

//
// The following bit value stands for "broadcast SMI" in the
// "etc/smi/supported-features" and "etc/smi/requested-features" fw_cfg files.
//
#define ICH9_LPC_SMI_F_BROADCAST BIT0

//
// Provides a scratch buffer (allocated in EfiReservedMemoryType type memory)
// for the S3 boot script fragment to write to and read from. The buffer
// captures a combined fw_cfg item selection + write command using the DMA
// access method. Note that we don't trust the runtime OS to preserve the
// contents of the buffer, the boot script will first rewrite it.
//
#pragma pack (1)
typedef struct {
  FW_CFG_DMA_ACCESS Access;
  UINT64            Features;
} SCRATCH_BUFFER;
#pragma pack ()

//
// These carry the selector keys of the "etc/smi/requested-features" and
// "etc/smi/features-ok" fw_cfg files from NegotiateSmiFeatures() to
// SaveSmiFeatures().
//
STATIC FIRMWARE_CONFIG_ITEM mRequestedFeaturesItem;
STATIC FIRMWARE_CONFIG_ITEM mFeaturesOkItem;

//
// Carries the negotiated SMI features from NegotiateSmiFeatures() to
// SaveSmiFeatures().
//
STATIC UINT64 mSmiFeatures;

/**
  Negotiate SMI features with QEMU.

  @retval FALSE  If SMI feature negotiation is not supported by QEMU. This is
                 not an error, it just means that SaveSmiFeatures() should not
                 be called.

  @retval TRUE   SMI feature negotiation is supported, and it has completed
                 successfully as well. (Failure to negotiate is a fatal error
                 and the function never returns in that case.)
**/
BOOLEAN
NegotiateSmiFeatures (
  VOID
  )
{
  FIRMWARE_CONFIG_ITEM SupportedFeaturesItem;
  UINTN                SupportedFeaturesSize;
  UINTN                RequestedFeaturesSize;
  UINTN                FeaturesOkSize;

  //
  // Look up the fw_cfg files used for feature negotiation. The selector keys
  // of "etc/smi/requested-features" and "etc/smi/features-ok" are saved
  // statically. If the files are missing, then QEMU doesn't support SMI
  // feature negotiation.
  //
  if (RETURN_ERROR (QemuFwCfgFindFile ("etc/smi/supported-features",
                      &SupportedFeaturesItem, &SupportedFeaturesSize)) ||
      RETURN_ERROR (QemuFwCfgFindFile ("etc/smi/requested-features",
                      &mRequestedFeaturesItem, &RequestedFeaturesSize)) ||
      RETURN_ERROR (QemuFwCfgFindFile ("etc/smi/features-ok",
                      &mFeaturesOkItem, &FeaturesOkSize))) {
    DEBUG ((DEBUG_INFO, "%a: SMI feature negotiation unavailable\n",
      __FUNCTION__));
    return FALSE;
  }

  //
  // If the files are present but their sizes disagree with us, that's a fatal
  // error (we can't trust the behavior of SMIs either way).
  //
  if (SupportedFeaturesSize != sizeof mSmiFeatures ||
      RequestedFeaturesSize != sizeof mSmiFeatures ||
      FeaturesOkSize != sizeof (UINT8)) {
    DEBUG ((DEBUG_ERROR, "%a: size mismatch in feature negotiation\n",
      __FUNCTION__));
    goto FatalError;
  }

  //
  // Get the features supported by the host.
  //
  QemuFwCfgSelectItem (SupportedFeaturesItem);
  QemuFwCfgReadBytes (sizeof mSmiFeatures, &mSmiFeatures);

  //
  // We want broadcast SMI and nothing else.
  //
  mSmiFeatures &= ICH9_LPC_SMI_F_BROADCAST;
  QemuFwCfgSelectItem (mRequestedFeaturesItem);
  QemuFwCfgWriteBytes (sizeof mSmiFeatures, &mSmiFeatures);

  //
  // Invoke feature validation in QEMU. If the selection is accepted, the
  // features will be locked down. If the selection is rejected, feature
  // negotiation remains open; however we don't know what to do in that case,
  // so that's a fatal error.
  //
  QemuFwCfgSelectItem (mFeaturesOkItem);
  if (QemuFwCfgRead8 () != 1) {
    DEBUG ((DEBUG_ERROR, "%a: negotiation failed for feature bitmap 0x%Lx\n",
      __FUNCTION__, mSmiFeatures));
    goto FatalError;
  }

  if ((mSmiFeatures & ICH9_LPC_SMI_F_BROADCAST) == 0) {
    //
    // If we can't get broadcast SMIs from QEMU, that's acceptable too,
    // although not optimal.
    //
    DEBUG ((DEBUG_INFO, "%a: SMI broadcast unavailable\n", __FUNCTION__));
  } else {
    //
    // Configure the traditional AP sync / SMI delivery mode for
    // PiSmmCpuDxeSmm. Effectively, restore the UefiCpuPkg defaults, from which
    // the original QEMU behavior (i.e., unicast SMI) used to differ.
    //
    if (RETURN_ERROR (PcdSet64S (PcdCpuSmmApSyncTimeout, 1000000)) ||
        RETURN_ERROR (PcdSet8S (PcdCpuSmmSyncMode, 0x00))) {
      DEBUG ((DEBUG_ERROR, "%a: PiSmmCpuDxeSmm PCD configuration failed\n",
        __FUNCTION__));
      goto FatalError;
    }
    DEBUG ((DEBUG_INFO, "%a: using SMI broadcast\n", __FUNCTION__));
  }

  //
  // Negotiation successful (although we may not have gotten the optimal
  // feature set).
  //
  return TRUE;

FatalError:
  ASSERT (FALSE);
  CpuDeadLoop ();
  //
  // Keep the compiler happy.
  //
  return FALSE;
}

/**
  Append a boot script fragment that will re-select the previously negotiated
  SMI features during S3 resume.

  @param[in] S3SaveState  The EFI_S3_SAVE_STATE_PROTOCOL instance to append to
                          the S3 boot script with.
**/
VOID
SaveSmiFeatures (
  IN EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState
  )
{
  SCRATCH_BUFFER *ScratchBuffer;
  EFI_STATUS     Status;
  UINT64         AccessAddress;
  UINT32         ControlPollData;
  UINT32         ControlPollMask;
  UINT16         FeaturesOkItemAsUint16;
  UINT8          FeaturesOkData;
  UINT8          FeaturesOkMask;

  ScratchBuffer = AllocateReservedPool (sizeof *ScratchBuffer);
  if (ScratchBuffer == NULL) {
    DEBUG ((DEBUG_ERROR, "%a: scratch buffer allocation failed\n",
      __FUNCTION__));
    goto FatalError;
  }

  //
  // Populate the scratch buffer with a select + write fw_cfg DMA command that
  // will write the negotiated feature bitmap into
  // "etc/smi/requested-features".
  //
  ScratchBuffer->Access.Control = SwapBytes32 (
                                    (UINT32)mRequestedFeaturesItem << 16 |
                                    FW_CFG_DMA_CTL_SELECT |
                                    FW_CFG_DMA_CTL_WRITE
                                    );
  ScratchBuffer->Access.Length = SwapBytes32 (
                                   (UINT32)sizeof ScratchBuffer->Features);
  ScratchBuffer->Access.Address = SwapBytes64 (
                                    (UINTN)&ScratchBuffer->Features);
  ScratchBuffer->Features       = mSmiFeatures;

  //
  // Copy the scratch buffer into the boot script. When replayed, this
  // EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE will restore the current contents of the
  // scratch buffer, in-place.
  //
  Status = S3SaveState->Write (
                          S3SaveState,                      // This
                          EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode
                          EfiBootScriptWidthUint8,          // Width
                          (UINT64)(UINTN)ScratchBuffer,     // Address
                          sizeof *ScratchBuffer,            // Count
                          (VOID*)ScratchBuffer              // Buffer
                          );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE: %r\n",
      __FUNCTION__, __LINE__, Status));
    goto FatalError;
  }

  //
  // Append an opcode that will write the address of the scratch buffer to the
  // fw_cfg DMA address register, which consists of two 32-bit IO ports. The
  // second (highest address, least significant) write will start the transfer.
  //
  AccessAddress = SwapBytes64 ((UINTN)&ScratchBuffer->Access);
  Status = S3SaveState->Write (
                          S3SaveState,                     // This
                          EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode
                          EfiBootScriptWidthUint32,        // Width
                          (UINT64)0x514,                   // Address
                          (UINTN)2,                        // Count
                          &AccessAddress                   // Buffer
                          );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_IO_WRITE_OPCODE: %r\n",
      __FUNCTION__, __LINE__, Status));
    goto FatalError;
  }

  //
  // The EFI_BOOT_SCRIPT_MEM_POLL_OPCODE will wait until the Control word reads
  // as zero (transfer complete). As timeout we use MAX_UINT64 * 100ns, which
  // is approximately 58494 years.
  //
  ControlPollData = 0;
  ControlPollMask = MAX_UINT32;
  Status = S3SaveState->Write (
                     S3SaveState,                                   // This
                     EFI_BOOT_SCRIPT_MEM_POLL_OPCODE,               // OpCode
                     EfiBootScriptWidthUint32,                      // Width
                     (UINT64)(UINTN)&ScratchBuffer->Access.Control, // Address
                     &ControlPollData,                              // Data
                     &ControlPollMask,                              // DataMask
                     MAX_UINT64                                     // Delay
                     );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_MEM_POLL_OPCODE: %r\n",
      __FUNCTION__, __LINE__, Status));
    goto FatalError;
  }

  //
  // Select the "etc/smi/features-ok" fw_cfg file, which invokes the feature
  // validation & lockdown. (The validation succeeded at first boot.)
  //
  FeaturesOkItemAsUint16 = (UINT16)mFeaturesOkItem;
  Status = S3SaveState->Write (
                          S3SaveState,                     // This
                          EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode
                          EfiBootScriptWidthUint16,        // Width
                          (UINT64)FW_CFG_IO_SELECTOR,      // Address
                          (UINTN)1,                        // Count
                          &FeaturesOkItemAsUint16          // Buffer
                          );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_IO_WRITE_OPCODE: %r\n",
      __FUNCTION__, __LINE__, Status));
    goto FatalError;
  }

  //
  // Read the contents (one byte) of "etc/smi/features-ok". If the value is
  // one, we're good. Otherwise, continue reading the data port: QEMU returns 0
  // past the end of the fw_cfg item, so this will hang the resume process,
  // which matches our intent.
  //
  FeaturesOkData = 1;
  FeaturesOkMask = MAX_UINT8;
  Status = S3SaveState->Write (
                     S3SaveState,                    // This
                     EFI_BOOT_SCRIPT_IO_POLL_OPCODE, // OpCode
                     EfiBootScriptWidthUint8,        // Width
                     (UINT64)(UINTN)FW_CFG_IO_DATA,  // Address
                     &FeaturesOkData,                // Data
                     &FeaturesOkMask,                // DataMask
                     MAX_UINT64                      // Delay
                     );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_IO_POLL_OPCODE: %r\n",
      __FUNCTION__, __LINE__, Status));
    goto FatalError;
  }

  DEBUG ((DEBUG_VERBOSE, "%a: ScratchBuffer@%p\n", __FUNCTION__,
    (VOID *)ScratchBuffer));
  return;

FatalError:
  ASSERT (FALSE);
  CpuDeadLoop ();
}