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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
/* SPDX-License-Identifier: BSD-2-Clause */
#include <console/console.h>
#include <cpu/x86/cr.h>
#include <cpu/x86/mp.h>
#include <cpu/x86/msr.h>
#include <cpu/x86/cache.h>
#include <security/intel/stm/SmmStm.h>
#include <stdbool.h>
#include <string.h>
#define TXT_EVTYPE_BASE 0x400
#define TXT_EVTYPE_STM_HASH (TXT_EVTYPE_BASE + 14)
#define RDWR_ACCS 3
#define FULL_ACCS 7
#define SIZE_4KB 0x00001000
#define SIZE_4MB 0x00400000
#define PTP_SIZE SIZE_4KB
#define IA32_PG_P (1 << 0)
#define IA32_PG_RW (1 << 1)
#define IA32_PG_PS (1 << 7)
#define STM_PAGE_SHIFT 12
#define STM_PAGE_MASK 0xFFF
#define STM_SIZE_TO_PAGES(a) \
(((a) >> STM_PAGE_SHIFT) + (((a)&STM_PAGE_MASK) ? 1 : 0))
#define STM_PAGES_TO_SIZE(a) ((a) << STM_PAGE_SHIFT)
#define STM_ACCESS_DENIED 15
#define STM_UNSUPPORTED 3
#define STM_BUFFER_TOO_SMALL 1
#define STM_SM_MONITOR_STATE_ENABLED 1
typedef struct {
uint64_t vmcs_revision_id : 31;
uint64_t always_zero : 1;
uint64_t vmcs_size : 13;
uint64_t reserved1 : 3;
uint64_t vmxon_add_width : 1;
uint64_t stm_supported : 1;
uint64_t vmcs_memory_type : 4;
uint64_t in_out_reporting : 1;
uint64_t may_clear_defaults : 1;
uint64_t reserved2 : 8;
} VMX_BASIC_MSR_BITS;
typedef union {
VMX_BASIC_MSR_BITS bits;
uint64_t uint64;
msr_t msr;
} VMX_BASIC_MSR;
typedef struct {
uint64_t valid : 1;
uint64_t reserved1 : 1;
uint64_t vmx_off_blockSmi : 1;
uint64_t reserved2 : 9;
uint64_t mseg_address : 20;
uint64_t reserved3 : 32;
} SMM_MONITOR_CTL_MSR_BITS;
extern struct mp_state {
struct mp_ops ops;
int cpu_count;
uintptr_t perm_smbase;
size_t perm_smsize;
size_t smm_save_state_size;
bool do_smm;
} mp_state;
typedef union {
SMM_MONITOR_CTL_MSR_BITS bits;
uint64_t uint64;
msr_t msr;
} SMM_MONITOR_CTL_MSR;
// Template of STM_RSC_END structure for copying.
STM_RSC_END m_rsc_end_node = {
{END_OF_RESOURCES, sizeof(STM_RSC_END)},
};
uint8_t *m_stm_resources_ptr = NULL;
uint32_t m_stm_resource_total_size = 0x0;
uint32_t m_stm_resource_size_used = 0x0;
uint32_t m_stm_resource_size_available = 0x0;
uint8_t *stm_resource_heap = NULL;
uint32_t m_stm_state = 0;
/*
* Handle single Resource to see if it can be merged into Record.
*
* @param resource A pointer to resource node to be added
* @param record A pointer to record node to be merged
*
* @retval true resource handled
* @retval false resource is not handled
*/
static bool handle_single_resource(STM_RSC *resource, STM_RSC *record)
{
uint64_t resource_lo = 0;
uint64_t resource_hi = 0;
uint64_t record_lo = 0;
uint64_t record_hi = 0;
// Calling code is responsible for making sure that
// Resource->Header.RscType == (*Record)->Header.RscType
// thus we use just one of them as switch variable.
switch (resource->header.rsc_type) {
case MEM_RANGE:
case MMIO_RANGE:
resource_lo = resource->mem.base;
resource_hi = resource->mem.base + resource->mem.length;
record_lo = record->mem.base;
record_hi = record->mem.base + record->mem.length;
if (resource->mem.rwx_attributes
!= record->mem.rwx_attributes) {
if ((resource_lo == record_lo)
&& (resource_hi == record_hi)) {
record->mem.rwx_attributes =
resource->mem.rwx_attributes
| record->mem.rwx_attributes;
return true;
} else {
return false;
}
}
break;
case IO_RANGE:
case TRAPPED_IO_RANGE:
resource_lo = (uint64_t)resource->io.base;
resource_hi = (uint64_t)resource->io.base
+ (uint64_t)resource->io.length;
record_lo = (uint64_t)record->io.base;
record_hi =
(uint64_t)record->io.base + (uint64_t)record->io.length;
break;
case PCI_CFG_RANGE:
if ((resource->pci_cfg.originating_bus_number
!= record->pci_cfg.originating_bus_number)
|| (resource->pci_cfg.last_node_index
!= record->pci_cfg.last_node_index))
return false;
if (memcmp(resource->pci_cfg.pci_device_path,
record->pci_cfg.pci_device_path,
sizeof(STM_PCI_DEVICE_PATH_NODE)
* (resource->pci_cfg.last_node_index + 1))
!= 0) {
return false;
}
resource_lo = (uint64_t)resource->pci_cfg.base;
resource_hi = (uint64_t)resource->pci_cfg.base
+ (uint64_t)resource->pci_cfg.length;
record_lo = (uint64_t)record->pci_cfg.base;
record_hi = (uint64_t)record->pci_cfg.base
+ (uint64_t)record->pci_cfg.length;
if (resource->pci_cfg.rw_attributes
!= record->pci_cfg.rw_attributes) {
if ((resource_lo == record_lo)
&& (resource_hi == record_hi)) {
record->pci_cfg.rw_attributes =
resource->pci_cfg.rw_attributes
| record->pci_cfg.rw_attributes;
return true;
} else {
return false;
}
}
break;
case MACHINE_SPECIFIC_REG:
// Special case - merge MSR masks in place.
if (resource->msr.msr_index != record->msr.msr_index)
return false;
record->msr.read_mask |= resource->msr.read_mask;
record->msr.write_mask |= resource->msr.write_mask;
return true;
default:
return false;
}
// If resources are disjoint
if ((resource_hi < record_lo) || (resource_lo > record_hi))
return false;
// If resource is consumed by record.
if ((resource_lo >= record_lo) && (resource_hi <= record_hi))
return true;
// Resources are overlapping.
// Resource and record are merged.
resource_lo = (resource_lo < record_lo) ? resource_lo : record_lo;
resource_hi = (resource_hi > record_hi) ? resource_hi : record_hi;
switch (resource->header.rsc_type) {
case MEM_RANGE:
case MMIO_RANGE:
record->mem.base = resource_lo;
record->mem.length = resource_hi - resource_lo;
break;
case IO_RANGE:
case TRAPPED_IO_RANGE:
record->io.base = (uint64_t)resource_lo;
record->io.length = (uint64_t)(resource_hi - resource_lo);
break;
case PCI_CFG_RANGE:
record->pci_cfg.base = (uint64_t)resource_lo;
record->pci_cfg.length = (uint64_t)(resource_hi - resource_lo);
break;
default:
return false;
}
return true;
}
/*
* Add resource node.
*
* @param Resource A pointer to resource node to be added
*/
static void add_single_resource(STM_RSC *resource)
{
STM_RSC *record;
record = (STM_RSC *)m_stm_resources_ptr;
while (true) {
if (record->header.rsc_type == END_OF_RESOURCES)
break;
// Go to next record if resource and record types don't match.
if (resource->header.rsc_type != record->header.rsc_type) {
record = (STM_RSC *)((void *)record
+ record->header.length);
continue;
}
// Record is handled inside of procedure - don't adjust.
if (handle_single_resource(resource, record))
return;
record = (STM_RSC *)((void *)record + record->header.length);
}
// Add resource to the end of area.
memcpy(m_stm_resources_ptr + m_stm_resource_size_used
- sizeof(m_rsc_end_node),
resource, resource->header.length);
memcpy(m_stm_resources_ptr + m_stm_resource_size_used
- sizeof(m_rsc_end_node) + resource->header.length,
&m_rsc_end_node, sizeof(m_rsc_end_node));
m_stm_resource_size_used += resource->header.length;
m_stm_resource_size_available =
m_stm_resource_total_size - m_stm_resource_size_used;
}
/*
* Add resource list.
*
* @param resource_list A pointer to resource list to be added
* @param num_entries Optional number of entries.
* If 0, list must be terminated by END_OF_RESOURCES.
*/
static void add_resource(STM_RSC *resource_list, uint32_t num_entries)
{
uint32_t count;
uint32_t index;
STM_RSC *resource;
if (num_entries == 0)
count = 0xFFFFFFFF;
else
count = num_entries;
resource = resource_list;
for (index = 0; index < count; index++) {
if (resource->header.rsc_type == END_OF_RESOURCES)
return;
add_single_resource(resource);
resource =
(STM_RSC *)((void *)resource + resource->header.length);
}
}
/*
* Validate resource list.
*
* @param resource_list A pointer to resource list to be added
* @param num_entries Optional number of entries.
* If 0, list must be terminated by END_OF_RESOURCES.
*
* @retval true resource valid
* @retval false resource invalid
*/
static bool validate_resource(STM_RSC *resource_list, uint32_t num_entries)
{
uint32_t count;
uint32_t index;
STM_RSC *resource;
uint32_t sub_index;
// If NumEntries == 0 make it very big. Scan will be terminated by
// END_OF_RESOURCES.
if (num_entries == 0)
count = 0xFFFFFFFF;
else
count = num_entries;
// Start from beginning of resource list.
resource = resource_list;
for (index = 0; index < count; index++) {
printk(BIOS_DEBUG, "STM: %s (%u) - RscType(%x) length(0x%x)\n",
__func__,
index,
resource->header.rsc_type,
resource->header.length);
// Validate resource.
switch (resource->header.rsc_type) {
case END_OF_RESOURCES:
if (resource->header.length != sizeof(STM_RSC_END))
return false;
// If we are passed actual number of resources to add,
// END_OF_RESOURCES structure between them is considered
// an error. If NumEntries == 0 END_OF_RESOURCES is a
// termination.
if (num_entries != 0)
return false;
// If NumEntries == 0 and list reached end - return
// success.
return true;
case MEM_RANGE:
case MMIO_RANGE:
printk(BIOS_DEBUG,
"STM: %s - MEM (0x%0llx, 0x%0llx)\n",
__func__,
resource->mem.base,
resource->mem.length);
if (resource->header.length != sizeof(STM_RSC_MEM_DESC))
return false;
break;
case IO_RANGE:
case TRAPPED_IO_RANGE:
if (resource->header.length != sizeof(STM_RSC_IO_DESC))
return false;
if ((resource->io.base + resource->io.length) > 0xFFFF)
return false;
break;
case PCI_CFG_RANGE:
printk(BIOS_DEBUG,
"STM: %s - PCI (0x%02x, 0x%08x, 0x%02x, 0x%02x)\n",
__func__,
resource->pci_cfg.originating_bus_number,
resource->pci_cfg.last_node_index,
resource->pci_cfg.pci_device_path[0].pci_device,
resource->pci_cfg.pci_device_path[0]
.pci_function);
if (resource->header.length
!= sizeof(STM_RSC_PCI_CFG_DESC)
+ (sizeof(STM_PCI_DEVICE_PATH_NODE)
* resource->pci_cfg.last_node_index))
return false;
for (sub_index = 0;
sub_index <= resource->pci_cfg.last_node_index;
sub_index++) {
if ((resource->pci_cfg
.pci_device_path[sub_index]
.pci_device
> 0x1F)
|| (resource->pci_cfg
.pci_device_path[sub_index]
.pci_function
> 7))
return false;
}
if ((resource->pci_cfg.base + resource->pci_cfg.length)
> 0x1000)
return false;
break;
case MACHINE_SPECIFIC_REG:
if (resource->header.length != sizeof(STM_RSC_MSR_DESC))
return false;
break;
default:
printk(BIOS_DEBUG, "STM: %s - Unknown RscType(%x)\n",
__func__, resource->header.rsc_type);
return false;
}
resource =
(STM_RSC *)((void *)resource + resource->header.length);
}
return true;
}
/*
* Get resource list.
* EndResource is excluded.
*
* @param resou rce_list A pointer to resource list to be added
* @param num_entries Optional number of entries.
* If 0, list must be terminated by END_OF_RESOURCES.
*
* @retval true resource valid
* @retval false resource invalid
*/
static uint32_t get_resource_size(STM_RSC *resource_list, uint32_t num_entries)
{
uint32_t count;
uint32_t index;
STM_RSC *resource;
resource = resource_list;
// If NumEntries == 0 make it very big. Scan will be terminated by
// END_OF_RESOURCES.
if (num_entries == 0)
count = 0xFFFFFFFF;
else
count = num_entries;
// Start from beginning of resource list.
resource = resource_list;
for (index = 0; index < count; index++) {
if (resource->header.rsc_type == END_OF_RESOURCES)
break;
resource =
(STM_RSC *)((void *)resource + resource->header.length);
}
return (uint32_t)((uint32_t)resource - (uint32_t)resource_list);
}
/*
* Add resources in list to database. Allocate new memory areas as needed.
*
* @param resource_list A pointer to resource list to be added
* @param num_entries Optional number of entries.
* If 0, list must be terminated by END_OF_RESOURCES.
*
* @retval SUCCESS If resources are added
* @retval INVALID_PARAMETER If nested procedure detected resource failure
* @retval OUT_OF_RESOURCES If nested procedure returned it and we cannot
* allocate more areas.
*/
int add_pi_resource(STM_RSC *resource_list, uint32_t num_entries)
{
size_t resource_size;
printk(BIOS_DEBUG, "STM: %s - Enter\n", __func__);
if (!validate_resource(resource_list, num_entries))
return -1; // INVALID_PARAMETER;
resource_size = get_resource_size(resource_list, num_entries);
printk(BIOS_DEBUG, "STM: ResourceSize - 0x%08zx\n", resource_size);
if (resource_size == 0)
return -1; // INVALID_PARAMETER;
if (!m_stm_resources_ptr) {
// Copy EndResource for initialization
m_stm_resources_ptr = stm_resource_heap;
m_stm_resource_total_size = CONFIG_BIOS_RESOURCE_LIST_SIZE;
memset(m_stm_resources_ptr, 0, CONFIG_BIOS_RESOURCE_LIST_SIZE);
memcpy(m_stm_resources_ptr, &m_rsc_end_node,
sizeof(m_rsc_end_node));
m_stm_resource_size_used = sizeof(m_rsc_end_node);
m_stm_resource_size_available =
m_stm_resource_total_size - sizeof(m_rsc_end_node);
wbinvd(); // force to memory
} else {
if (m_stm_resource_size_available < resource_size) {
printk(BIOS_DEBUG,
"STM: ERROR - not enough space for SMM resource list\n");
return -1; // OUT_OF_RESOURCES
}
}
// Check duplication
add_resource(resource_list, num_entries);
return 0; // SUCCESS;
}
/*
* Delete resources in list to database.
*
* @param resource_list A pointer to resource list to be deleted
* NULL means delete all resources.
* @param num_entries Optional number of entries.
* If 0, list must be terminated by END_OF_RESOURCES.
*
* @retval SUCCESS If resources are deleted
* @retval INVALID_PARAMETER If nested procedure detected resource failure
*/
int32_t delete_pi_resource(STM_RSC *resource_list, uint32_t num_entries)
{
if (resource_list) {
// ASSERT (false);
return -1; // UNSUPPORTED;
}
// Delete all
memcpy(m_stm_resources_ptr, &m_rsc_end_node, sizeof(m_rsc_end_node));
m_stm_resource_size_used = sizeof(m_rsc_end_node);
m_stm_resource_size_available =
m_stm_resource_total_size - sizeof(m_rsc_end_node);
return 0; // SUCCESS;
}
/*
* Get BIOS resources.
*
* @param resource_list A pointer to resource list to be filled
* @param resource_size On input it means size of resource list input.
* On output it means size of resource list filled,
* or the size of resource list to be filled if size is
* too small.
*
* @retval SUCCESS If resources are returned.
* @retval BUFFER_TOO_SMALL If resource list buffer is too small to hold
* the whole resource list.
*/
int32_t get_pi_resource(STM_RSC *resource_list, uint32_t *resource_size)
{
if (*resource_size < m_stm_resource_size_used) {
*resource_size = (uint32_t)m_stm_resource_size_used;
return -1; // BUFFER_TOO_SMALL;
}
memcpy(resource_list, m_stm_resources_ptr, m_stm_resource_size_used);
*resource_size = (uint32_t)m_stm_resource_size_used;
return 0; // SUCCESS;
}
/*
* Get 4K page aligned VMCS size.
* @return 4K page aligned VMCS size
*/
static uint32_t get_vmcs_size(void)
{
uint32_t this_vmcs_size;
VMX_BASIC_MSR msr_data64;
int stm_support;
msr_data64.msr = rdmsr(IA32_VMX_BASIC_MSR);
this_vmcs_size = msr_data64.bits.vmcs_size;
stm_support = msr_data64.bits.stm_supported;
printk(BIOS_DEBUG, "STM: %s: Size %d StmSupport %d\n", __func__,
this_vmcs_size, stm_support);
// VMCS require 0x1000 alignment
this_vmcs_size = STM_PAGES_TO_SIZE(STM_SIZE_TO_PAGES(this_vmcs_size));
return this_vmcs_size;
}
/*
* Create 4G page table for STM.
* 2M PTEs for x86_64 or 2M PTEs for x86_32.
*
* @param pageable_base The page table base in MSEG
*/
void stm_gen_4g_pagetable_x64(uint32_t pagetable_base)
{
uint32_t index;
uint32_t sub_index;
uint64_t *pde;
uint64_t *pte;
uint64_t *pml4;
pml4 = (uint64_t *)(uint32_t)pagetable_base;
pagetable_base += PTP_SIZE;
*pml4 = pagetable_base | IA32_PG_RW | IA32_PG_P;
pde = (uint64_t *)(uint32_t)pagetable_base;
pagetable_base += PTP_SIZE;
pte = (uint64_t *)(uint32_t)pagetable_base;
for (index = 0; index < 4; index++) {
*pde = pagetable_base | IA32_PG_RW | IA32_PG_P;
pde++;
pagetable_base += PTP_SIZE;
for (sub_index = 0; sub_index < SIZE_4KB / sizeof(*pte);
sub_index++) {
*pte = (((index << 9) + sub_index) << 21) | IA32_PG_PS
| IA32_PG_RW | IA32_PG_P;
pte++;
}
}
}
/*
* Check STM image size.
*
* @param stm_image STM image
* @param stm_imageSize STM image size
*
* @retval true check pass
* @retval false check fail
*/
bool stm_check_stm_image(void *stm_image, uint32_t stm_imagesize)
{
uint32_t min_mseg_size;
STM_HEADER *stm_header;
stm_header = (STM_HEADER *)stm_image;
// Get Minimal required Mseg size
min_mseg_size = (STM_PAGES_TO_SIZE(STM_SIZE_TO_PAGES(
stm_header->sw_stm_hdr.static_image_size))
+ stm_header->sw_stm_hdr.additional_dynamic_memory_size
+ (stm_header->sw_stm_hdr.per_proc_dynamic_memory_size
+ get_vmcs_size() * 2)
* mp_state.cpu_count);
if (min_mseg_size < stm_imagesize)
min_mseg_size = stm_imagesize;
if (stm_header->hw_stm_hdr.cr3_offset
>= stm_header->sw_stm_hdr.static_image_size) {
// We will create page table, just in case that SINIT does not
// create it.
if (min_mseg_size < stm_header->hw_stm_hdr.cr3_offset
+ STM_PAGES_TO_SIZE(6)) {
min_mseg_size = stm_header->hw_stm_hdr.cr3_offset
+ STM_PAGES_TO_SIZE(6);
}
}
// Check if it exceeds MSEG size
if (min_mseg_size > CONFIG_MSEG_SIZE)
return false;
return true;
}
/*
* This function return BIOS STM resource.
* Produced by SmmStm.
* Consumed by SmmMpService when Init.
*
* @return BIOS STM resource
*/
void *get_stm_resource(void)
{
return m_stm_resources_ptr;
}
|