-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.c
2793 lines (2310 loc) · 91.4 KB
/
helpers.c
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2023 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom
// the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
//
// SPDX-License-Identifier: MIT
/**
* @file helpers.c
* @brief Common TDX API flow helper functions
*/
#include "helpers.h"
#include "x86_defs/mktme.h"
#include "x86_defs/vmcs_defs.h"
#include "tdx_api_defs.h"
#include "auto_gen/cpuid_configurations.h"
#include "auto_gen/msr_config_lookup.h"
#include "accessors/ia32_accessors.h"
#include "accessors/vt_accessors.h"
#include "memory_handlers/keyhole_manager.h"
#include "memory_handlers/sept_manager.h"
#include "data_structures/td_vmcs_init.h"
#include "td_transitions/td_exit.h"
#include "td_dispatcher/tdx_td_dispatcher.h"
#include "td_dispatcher/vm_exits/td_vmexit.h"
#include "virt_msr_helpers.h"
api_error_code_e program_mktme_keys(uint16_t hkid)
{
mktme_key_program_t mktme_key_program;
api_error_code_e return_val = UNINITIALIZE_ERROR;
uint64_t pconfig_return_code;
basic_memset_to_zero(&mktme_key_program, sizeof(mktme_key_program_t));
// set the command, hkid as keyid and encryption algorithm
mktme_key_program.keyid_ctrl.command = MKTME_KEYID_SET_KEY_RANDOM;
mktme_key_program.keyid = hkid;
if (get_sysinfo_table()->mcheck_fields.tdx_without_integrity)
{
if (get_global_data()->plt_common_config.ia32_tme_activate.algs_aes_xts_256)
{
mktme_key_program.keyid_ctrl.enc_algo = AES_XTS_256;
}
else
{
mktme_key_program.keyid_ctrl.enc_algo = AES_XTS_128;
}
}
else
{
if (get_global_data()->plt_common_config.ia32_tme_activate.algs_aes_xts_256_with_integrity)
{
mktme_key_program.keyid_ctrl.enc_algo = AES_XTS_256_WITH_INTEGRITY;
}
else
{
mktme_key_program.keyid_ctrl.enc_algo = AES_XTS_128_WITH_INTEGRITY;
}
}
// Execute the PCONFIG instruction with the updated struct and return
pconfig_return_code = ia32_mktme_key_program(&mktme_key_program);
if (pconfig_return_code != MKTME_PROG_SUCCESS)
{
TDX_ERROR("pconfig_return_code = %llx\n", pconfig_return_code);
if (pconfig_return_code == MKTME_DEVICE_BUSY)
{
return_val = api_error_with_operand_id(TDX_OPERAND_BUSY, OPERAND_ID_KET);
TDX_ERROR("Failed to program an MKTME key due to lock on KET\n");
goto EXIT;
}
else if (pconfig_return_code == MKTME_ENTROPY_ERROR)
{
return_val = TDX_KEY_GENERATION_FAILED;
TDX_ERROR("Failed to generate a key for the MKTME engine\n");
goto EXIT;
}
else
{
// unexpected - FATAL ERROR
FATAL_ERROR();
}
}
return_val = TDX_SUCCESS;
EXIT:
return return_val;
}
void basic_memset(uint64_t dst, uint64_t dst_bytes, uint8_t val, uint64_t nbytes)
{
tdx_sanity_check (dst_bytes >= nbytes, SCEC_HELPERS_SOURCE, 2);
_ASM_VOLATILE_ ("cld\n"
"rep; stosb;"
:
:"c"(nbytes), "a"(val), "D"(dst)
:"memory", "cc");
}
void basic_memset_to_zero(void * dst, uint64_t nbytes)
{
basic_memset((uint64_t)dst, nbytes, 0, nbytes);
}
void cache_aligned_copy_direct(uint64_t src, uint64_t dst, uint64_t size)
{
uint64_t i = 0;
tdx_debug_assert(!((src % MOVDIR64_CHUNK_SIZE) || (dst % MOVDIR64_CHUNK_SIZE) ||
(size % MOVDIR64_CHUNK_SIZE)));
//To convert from size in bytes to cacheline steps (64bytes), shift by 6:
//1 << 6 = 64; 64 >> 6 = 1
for (i = 0; i < (size >> 6 ); i++)
{
movdir64b((void *)(src + (i << 6)), dst + (i << 6));
}
mfence();
}
api_error_code_e non_shared_hpa_metadata_check_and_lock(
pa_t hpa,
lock_type_t lock_type,
page_type_t expected_pt,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
page_size_t* leaf_size,
bool_t walk_to_leaf_size,
bool_t is_guest
)
{
// 1) Check that the operand’s HPA is within a TDMR (Trust Domain Memory Range) which is covered by a PAMT.
if (!pamt_get_block(hpa, pamt_block))
{
TDX_ERROR("pamt_get_block error hpa = 0x%llx\n", hpa.raw);
return TDX_OPERAND_ADDR_RANGE_ERROR;
}
pamt_entry_t* pamt_entry_lp;
page_size_t requested_leaf_size = *leaf_size;
// 2) Find the PAMT entry for the page and verify that its metadata is as expected.
api_error_code_e errc = pamt_walk(hpa, *pamt_block, lock_type, leaf_size,
walk_to_leaf_size, is_guest, &pamt_entry_lp);
if (errc != TDX_SUCCESS)
{
TDX_ERROR("pamt_walk error\n");
return errc;
}
if (walk_to_leaf_size && (requested_leaf_size != *leaf_size))
{
TDX_ERROR("PAMT entry level = %d , Expected level = %d\n", *leaf_size, requested_leaf_size);
pamt_unwalk(hpa, *pamt_block, pamt_entry_lp, lock_type, *leaf_size);
return TDX_PAGE_METADATA_INCORRECT;
}
if (pamt_entry_lp->pt != expected_pt)
{
TDX_ERROR("pamt_entry_lp->pt = %d , expected_pt = %d\n", pamt_entry_lp->pt, expected_pt);
pamt_unwalk(hpa, *pamt_block, pamt_entry_lp, lock_type, *leaf_size);
return TDX_PAGE_METADATA_INCORRECT;
}
*pamt_entry = pamt_entry_lp;
return TDX_SUCCESS;
}
pa_t assign_hkid_to_hpa(tdr_t* tdr_p, pa_t hpa)
{
uint16_t hkid;
// 1) If the target page is TDR (given TDR pointer is NULL), then use the TDX-SEAM global private HKID.
if (tdr_p == NULL)
{
hkid = get_global_data()->hkid;
}
// 2) Else, read the HKID value associated with the TD from the TDR page.
else
{
hkid = tdr_p->key_management_fields.hkid;
}
return set_hkid_to_pa(hpa, hkid);
}
api_error_code_e shared_hpa_check(pa_t hpa, uint64_t size)
{
// 1) Check that no bits above MAX_PA are set
if (!is_pa_smaller_than_max_pa(hpa.raw))
{
return TDX_OPERAND_INVALID;
}
// 2) Check that the provided HPA is outside SEAMRR.
uint64_t seamrr_base = get_global_data()->seamrr_base;
uint64_t seamrr_size = get_global_data()->seamrr_size;
// SEAMRR and HPA+page ranges are not expected cause integer overflow
// SEAMRR base and size are both 32 bits,
// HPA is checked to be smaller than MAX_PA in above check
// If there's still somehow an unexpected overflow, ot will be caught by the check inside is_overlap()
if (is_overlap(get_addr_from_pa(hpa), size, seamrr_base, seamrr_size))
{
return TDX_OPERAND_INVALID;
}
// 3) Check that HKID bits in the HPA are in the range configured for shared HKIDs (0 to MAX_MKTME_HKIDS – 1).
if ((uint64_t)get_hkid_from_pa(hpa) >= get_global_data()->private_hkid_min)
{
return TDX_OPERAND_INVALID;
}
return TDX_SUCCESS;
}
api_error_code_e shared_hpa_check_with_pwr_2_alignment(pa_t hpa, uint64_t size)
{
if (!is_addr_aligned_pwr_of_2(hpa.raw, size))
{
return TDX_OPERAND_INVALID;
}
return shared_hpa_check(hpa, size);
}
api_error_code_e hpa_check_with_pwr_2_alignment(pa_t hpa, uint64_t size)
{
// 1) Check page alignment
if (!is_addr_aligned_pwr_of_2(hpa.raw, size))
{
return TDX_OPERAND_INVALID;
}
// 2) Check that No HKID bits are set
if (get_hkid_from_pa(hpa) != 0)
{
return TDX_OPERAND_INVALID;
}
// 3) Check that no bits above MAX_PA are set
if (!is_pa_smaller_than_max_pa(hpa.raw))
{
return TDX_OPERAND_INVALID;
}
return TDX_SUCCESS;
}
api_error_type check_lock_and_map_explicit_private_4k_hpa(
pa_t hpa,
uint64_t operand_id,
tdr_t* tdr_p,
mapping_type_t mapping_type,
lock_type_t lock_type,
page_type_t expected_pt,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
bool_t* is_locked,
void** la
)
{
api_error_type errc;
errc = check_and_lock_explicit_4k_private_hpa( hpa, operand_id,
lock_type, expected_pt, pamt_block, pamt_entry, is_locked);
if (errc != TDX_SUCCESS)
{
return errc;
}
pa_t hpa_with_hkid = assign_hkid_to_hpa(tdr_p, hpa);
*la = map_pa((void*)hpa_with_hkid.full_pa, mapping_type);
return TDX_SUCCESS;
}
api_error_type check_lock_and_map_explicit_tdr(
pa_t tdr_hpa,
uint64_t operand_id,
mapping_type_t mapping_type,
lock_type_t lock_type,
page_type_t expected_pt,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
bool_t* is_locked,
tdr_t** tdr_p
)
{
return check_lock_and_map_explicit_private_4k_hpa(tdr_hpa, operand_id, NULL, mapping_type,
lock_type, expected_pt, pamt_block, pamt_entry, is_locked, (void**)tdr_p);
}
api_error_type othertd_check_lock_and_map_explicit_tdr(
pa_t tdr_hpa,
uint64_t operand_id,
mapping_type_t mapping_type,
lock_type_t lock_type,
page_type_t expected_pt,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
bool_t* is_locked,
tdr_t** tdr_p
)
{
api_error_type errc;
page_size_t leaf_size = PT_4KB;
errc = hpa_check_with_pwr_2_alignment(tdr_hpa, _4KB);
if (errc != TDX_SUCCESS)
{
return api_error_with_operand_id(TDX_OPERAND_INVALID, operand_id);
}
errc = non_shared_hpa_metadata_check_and_lock(tdr_hpa, lock_type,
expected_pt, pamt_block, pamt_entry, &leaf_size, true, true);
if (errc != TDX_SUCCESS)
{
return api_error_with_operand_id(errc, operand_id);
}
*is_locked = true;
pa_t hpa_with_hkid = assign_hkid_to_hpa(NULL, tdr_hpa);
*tdr_p = map_pa((void*)hpa_with_hkid.full_pa, mapping_type);
return TDX_SUCCESS;
}
api_error_type check_and_lock_explicit_private_hpa(
pa_t hpa,
uint64_t operand_id,
uint64_t alignment,
lock_type_t lock_type,
page_type_t expected_pt,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
page_size_t* leaf_size,
bool_t walk_to_leaf_size,
bool_t* is_locked
)
{
api_error_code_e errc;
errc = hpa_check_with_pwr_2_alignment(hpa, alignment);
if (errc != TDX_SUCCESS)
{
return api_error_with_operand_id(TDX_OPERAND_INVALID, operand_id);
}
errc = non_shared_hpa_metadata_check_and_lock(hpa, lock_type,
expected_pt, pamt_block, pamt_entry, leaf_size, walk_to_leaf_size, false);
if (errc != TDX_SUCCESS)
{
return api_error_with_operand_id(errc, operand_id);
}
*is_locked = true;
return TDX_SUCCESS;
}
api_error_type check_and_lock_explicit_4k_private_hpa(
pa_t hpa,
uint64_t operand_id,
lock_type_t lock_type,
page_type_t expected_pt,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
bool_t* is_locked
)
{
api_error_type errc;
page_size_t leaf_size = PT_4KB;
errc = check_and_lock_explicit_private_hpa(hpa, operand_id, _4KB, lock_type,
expected_pt, pamt_block, pamt_entry, &leaf_size, true, is_locked);
if (errc != TDX_SUCCESS)
{
return errc;
}
return TDX_SUCCESS;
}
api_error_type check_and_lock_free_range_hpa(
pa_t hpa,
uint64_t operand_id,
lock_type_t lock_type,
page_size_t range_size,
pamt_block_t* pamt_block,
pamt_entry_t** pamt_entry,
bool_t* is_locked
)
{
tdx_debug_assert(range_size != PT_1GB);
tdx_debug_assert(lock_type == TDX_LOCK_EXCLUSIVE);
api_error_type errc;
page_size_t pamt_level = range_size;
uint64_t alignment = (range_size == PT_2MB) ? _2MB : _4KB;
errc = check_and_lock_explicit_private_hpa(hpa, operand_id, alignment, lock_type, PT_NDA,
pamt_block, pamt_entry, &pamt_level, true, is_locked);
if (errc != TDX_SUCCESS)
{
return errc;
}
// Verify 2MB HPA range is entirely free.
if ((range_size == PT_2MB) && !pamt_is_2mb_range_free(hpa, pamt_block))
{
TDX_ERROR("PAMT level (%d) is not as expected (%d) or the 2MB range isn't free\n", pamt_level, range_size);
pamt_unwalk(hpa, *pamt_block, *pamt_entry, lock_type, pamt_level);
*is_locked = false;
return api_error_with_operand_id(TDX_PAGE_METADATA_INCORRECT, operand_id);
}
return TDX_SUCCESS;
}
api_error_type lock_and_map_implicit_tdr(
pa_t tdr_pa,
uint64_t operand_id,
mapping_type_t mapping_type,
lock_type_t lock_type,
pamt_entry_t** pamt_entry,
bool_t* is_locked,
tdr_t** tdr_p
)
{
api_error_code_e errc = pamt_implicit_get_and_lock(tdr_pa, PT_4KB, lock_type, pamt_entry);
if (errc != TDX_SUCCESS)
{
TDX_ERROR("get_implicit_page_pamt_and_lock error\n");
return api_error_with_operand_id(errc, operand_id);
}
*is_locked = true;
uint16_t hkid = get_global_data()->hkid;
tdr_pa = set_hkid_to_pa(tdr_pa, hkid);
uint64_t tdr_addr = tdr_pa.full_pa;
*tdr_p = map_continuous_pages(&tdr_addr, 1, mapping_type, STATIC_KEYHOLE_IDX_TDR);
return TDX_SUCCESS;
}
tdcs_t* map_implicit_tdcs(
tdr_t* tdr_p,
mapping_type_t mapping_type,
bool_t other_td
)
{
return map_continuous_pages(tdr_p->management_fields.tdcx_pa, (uint16_t)tdr_p->management_fields.num_tdcx,
mapping_type,
other_td ? STATIC_KEYHOLE_IDX_OTHERTD_TDCS : STATIC_KEYHOLE_IDX_TDCS);
}
static api_error_type check_td_in_correct_build_state(tdr_t *tdr_p)
{
if (tdr_p->management_fields.fatal)
{
TDX_ERROR("TD is in fatal state\n");
return api_error_fatal(TDX_TD_FATAL);
}
if (tdr_p->management_fields.lifecycle_state != TD_KEYS_CONFIGURED)
{
TDX_ERROR("TD key are not configured\n");
return TDX_TD_KEYS_NOT_CONFIGURED;
}
if (tdr_p->management_fields.num_tdcx < MIN_NUM_TDCS_PAGES)
{
TDX_ERROR("TDCS minimal num of pages %d is not allocated\n", MIN_NUM_TDCS_PAGES);
return TDX_TDCS_NOT_ALLOCATED;
}
return TDX_SUCCESS;
}
static api_error_type check_any_td_state_map_tdcs_and_lock(
tdr_t* tdr_p,
mapping_type_t mapping_type,
lock_type_t op_state_lock_type,
bool_t map_migsc_links,
uint32_t current_leaf,
bool_t other_td,
bool_t guest_side_flow,
tdcs_t** tdcs_p
)
{
UNUSED(map_migsc_links);
api_error_code_e errc = UNINITIALIZE_ERROR;
*tdcs_p = NULL;
errc = check_td_in_correct_build_state(tdr_p);
if (errc != TDX_SUCCESS)
{
TDX_ERROR("TD is in incorrect build state\n");
return errc;
}
// Map the TDCS structure and check the state
tdcs_t* tmp_tdcs_p = map_implicit_tdcs(tdr_p, mapping_type, other_td);
if (op_state_lock_type != TDX_LOCK_NO_LOCK)
{
if ((errc = acquire_sharex_lock_hp(&(tmp_tdcs_p->management_fields.op_state_lock),
op_state_lock_type, guest_side_flow)) != TDX_SUCCESS)
{
free_la(tmp_tdcs_p);
TDX_ERROR("Could not lock TDCS OP state lock\n");
return api_error_with_operand_id(errc, OPERAND_ID_OP_STATE);
}
}
bool_t is_allowed = false;
IF_COMMON (!guest_side_flow)
{
seamcall_leaf_opcode_t seamcall_leaf = (seamcall_leaf_opcode_t)current_leaf;
is_allowed = op_state_is_seamcall_allowed(seamcall_leaf, tmp_tdcs_p->management_fields.op_state, other_td);
}
else
{
tdcall_leaf_opcode_t tdcall_leaf = (tdcall_leaf_opcode_t)current_leaf;
is_allowed = op_state_is_tdcall_allowed(tdcall_leaf, tmp_tdcs_p->management_fields.op_state, other_td);
}
if (!is_allowed)
{
if (op_state_lock_type != TDX_LOCK_NO_LOCK)
{
release_sharex_lock_hp(&(tmp_tdcs_p->management_fields.op_state_lock), op_state_lock_type);
}
free_la(tmp_tdcs_p);
TDX_ERROR("TDCS OP state is not allowed on this SEAMCALL leaf\n");
return TDX_OP_STATE_INCORRECT;
}
*tdcs_p = tmp_tdcs_p;
return TDX_SUCCESS;
}
api_error_type check_state_map_tdcs_and_lock(
tdr_t* tdr_p,
mapping_type_t mapping_type,
lock_type_t op_state_lock_type,
bool_t map_migsc_links,
seamcall_leaf_opcode_t current_leaf,
tdcs_t** tdcs_p
)
{
return check_any_td_state_map_tdcs_and_lock(tdr_p, mapping_type, op_state_lock_type,
map_migsc_links, (uint32_t)current_leaf, false, false, tdcs_p);
}
api_error_type othertd_check_state_map_tdcs_and_lock(
tdr_t* tdr_p,
mapping_type_t mapping_type,
lock_type_t op_state_lock_type,
bool_t map_migsc_links,
uint32_t current_leaf,
bool_t guest_side_flow,
tdcs_t** tdcs_p
)
{
return check_any_td_state_map_tdcs_and_lock(tdr_p, mapping_type, op_state_lock_type,
map_migsc_links, current_leaf, true, guest_side_flow, tdcs_p);
}
tdvps_t* map_tdvps(
pa_t tdvpr_pa,
uint16_t hkid,
uint16_t num_l2_vms,
mapping_type_t mapping_type
)
{
tdvpr_pa.raw = set_hkid_to_pa(tdvpr_pa, hkid).raw;
tdvps_t* tdvpr_lp = map_continuous_pages(&tdvpr_pa.raw, 1, mapping_type, STATIC_KEYHOLE_IDX_TDVPS);
if ((uint32_t)tdvpr_lp->management.num_tdvps_pages < (uint32_t)(MIN_TDVPS_PAGES + (num_l2_vms * TDVPS_PAGES_PER_L2_VM)))
{
TDX_ERROR("Num of TDCX pages (%d) is incorrect\n", tdvpr_lp->management.num_tdvps_pages);
free_la(tdvpr_lp);
return NULL;
}
uint16_t num_of_tdvps_pages = MIN_TDVPS_PAGES + (TDVPS_PAGES_PER_L2_VM * num_l2_vms);
// First TDVX PA is actually the PA of the TDVPR itself, since we already mapped it, it can be skipped
(void)map_continuous_pages(&tdvpr_lp->management.tdvps_pa[1], num_of_tdvps_pages - 1, mapping_type,
STATIC_KEYHOLE_IDX_TDVPS + 1);
return tdvpr_lp;
}
bool_t check_gpa_validity(
pa_t gpa,
bool_t gpaw,
bool_t check_is_private
)
{
uint16_t gpa_width = gpaw ? 52 : 48;
bool_t gpa_shared_bit = get_gpa_shared_bit(gpa.raw, gpaw);
if (check_is_private && (gpa_shared_bit == true))
{
return false;
}
// Bits higher then MAX_PA except shared bit must be zero (bits above SHARED bit must be zero)
if ((gpa.raw & ~BITS(MAX_PA-1,0)) != 0)
{
return false;
}
// When a TD is operating with GPAW 48, the CPU will treat bits 51:48 of every paging-structure
// entry as reserved and will generate reserved-bit page fault upon encountering such an entry.
if (!gpaw && (gpa.raw & BITS(MAX_PA-1, gpa_width)))
{
return false;
}
return true;
}
bool_t verify_page_info_input(page_info_api_input_t gpa_page_info, ept_level_t min_level, ept_level_t max_level)
{
// Verify that GPA mapping input reserved fields equal zero
if (!is_reserved_zero_in_mappings(gpa_page_info))
{
TDX_ERROR("Reserved fields in GPA mappings are not zero\n");
return false;
}
// Verify mapping level input is valid
if (!((gpa_page_info.level >= min_level) && (gpa_page_info.level <= max_level)))
{
TDX_ERROR("Input GPA level (=%d) is not valid\n", gpa_page_info.level);
return false;
}
// Check the page GPA is page aligned
if (!is_gpa_aligned(gpa_page_info))
{
TDX_ERROR("Page GPA 0x%llx is not page aligned\n", gpa_page_info.raw);
return false;
}
return true;
}
typedef enum sept_walk_type_e
{
SEPT_WALK_TO_LEVEL,
SEPT_WALK_TO_LEAF,
SEPT_WALK_TO_LEAF_LEVEL
} sept_walk_type_t;
static api_error_type lock_sept_check_and_walk_internal(
tdcs_t* tdcs_p,
uint64_t operand_id,
pa_t gpa,
uint16_t hkid,
lock_type_t lock_type,
bool_t check_validity,
sept_walk_type_t walk_type,
ia32e_sept_t** sept_entry_ptr,
ept_level_t* level,
ia32e_sept_t* cached_sept_entry,
bool_t* is_sept_locked
)
{
bool_t gpaw = tdcs_p->executions_ctl_fields.gpaw;
*is_sept_locked = false;
if (check_validity && !check_gpa_validity(gpa, gpaw, PRIVATE_ONLY))
{
return api_error_with_operand_id(TDX_OPERAND_INVALID, operand_id);
}
ia32e_eptp_t septp = tdcs_p->executions_ctl_fields.eptp;
if (lock_type != TDX_LOCK_NO_LOCK)
{
if (acquire_sharex_lock(&tdcs_p->executions_ctl_fields.secure_ept_lock, lock_type) != LOCK_RET_SUCCESS)
{
return api_error_with_operand_id(TDX_OPERAND_BUSY, OPERAND_ID_SEPT_TREE);
}
}
ept_level_t requested_level = *level;
*sept_entry_ptr = secure_ept_walk(septp, gpa, hkid, level, cached_sept_entry, false);
if (// When we walk to leaf we check that the final entry is a valid, existing leaf
((walk_type == SEPT_WALK_TO_LEAF) &&
(!is_secure_ept_leaf_entry(cached_sept_entry) || !cached_sept_entry->rwx)) ||
// When we walk to level, we just check that we reached requested level
((walk_type == SEPT_WALK_TO_LEVEL) && (*level != requested_level)) ||
// When we walk to leaf-level, check that we reached an actual leaf
((walk_type == SEPT_WALK_TO_LEAF_LEVEL) && !is_secure_ept_leaf_entry(cached_sept_entry))
)
{
if (lock_type != TDX_LOCK_NO_LOCK)
{
release_sharex_lock(&tdcs_p->executions_ctl_fields.secure_ept_lock, lock_type);
}
free_la(*sept_entry_ptr);
*sept_entry_ptr = NULL;
return api_error_with_operand_id(TDX_EPT_WALK_FAILED, operand_id);
}
if (lock_type != TDX_LOCK_NO_LOCK)
{
*is_sept_locked = true;
}
return TDX_SUCCESS;
}
api_error_type lock_sept_check_and_walk_private_gpa(
tdcs_t* tdcs_p,
uint64_t operand_id,
pa_t gpa,
uint16_t hkid,
lock_type_t lock_type,
ia32e_sept_t** sept_entry_ptr,
ept_level_t* level,
ia32e_sept_t* cached_sept_entry,
bool_t* is_sept_locked
)
{
tdx_debug_assert(lock_type != TDX_LOCK_NO_LOCK);
return lock_sept_check_and_walk_internal(tdcs_p, operand_id, gpa, hkid,
lock_type, // Lock the SEPT tree
true, // Check private GPA validity
SEPT_WALK_TO_LEVEL, // Walk to requested level
sept_entry_ptr, level, cached_sept_entry, is_sept_locked);
}
api_error_type check_and_walk_private_gpa_to_leaf(
tdcs_t* tdcs_p,
uint64_t operand_id,
pa_t gpa,
uint16_t hkid,
ia32e_sept_t** sept_entry_ptr,
ept_level_t* level,
ia32e_sept_t* cached_sept_entry
)
{
bool_t is_sept_locked;
*level = LVL_PT;
// Don't lock SEPT, heck private GPA validity and walk to any leaf
return lock_sept_check_and_walk_internal(tdcs_p, operand_id, gpa, hkid,
TDX_LOCK_NO_LOCK, // Do not lock SEPT tree
true, // Check private GPA validity
SEPT_WALK_TO_LEAF, // Walk to any leaf
sept_entry_ptr, level, cached_sept_entry, &is_sept_locked);
}
api_error_type walk_private_gpa(
tdcs_t* tdcs_p,
pa_t gpa,
uint16_t hkid,
ia32e_sept_t** sept_entry_ptr,
ept_level_t* level,
ia32e_sept_t* cached_sept_entry
)
{
bool_t is_sept_locked;
// Do not check private GPA validity and walk to requested level
return lock_sept_check_and_walk_internal(tdcs_p, 0, gpa, hkid,
TDX_LOCK_NO_LOCK, // Do not lock SEPT tree
false, // Do not check private GPA validity
SEPT_WALK_TO_LEVEL, // Walk to requested level
sept_entry_ptr, level, cached_sept_entry, &is_sept_locked);
}
static void inject_ve_and_return_to_td(tdvps_t* tdvps_p, pa_t gpa, vmx_exit_qualification_t exit_qual)
{
tdx_inject_ve(VMEXIT_REASON_EPT_VIOLATION, exit_qual.raw, tdvps_p, gpa.raw, 0);
bus_lock_exit();
check_pending_voe_on_debug_td_return();
tdx_return_to_td(true, false, &tdvps_p->guest_state.gpr_state);
}
api_error_code_e check_walk_and_map_guest_side_gpa(
tdcs_t* tdcs_p,
tdvps_t* tdvps_p,
pa_t gpa,
uint16_t hkid,
mapping_type_t mapping_type,
bool_t check_gpa_is_private,
void ** la
)
{
ia32e_eptp_t eptp;
ia32e_ept_t ept_entry_copy = {.raw = 0};
ept_walk_result_t walk_result;
access_rights_t accumulated_rwx;
bool_t gpaw = tdcs_p->executions_ctl_fields.gpaw;
vmx_exit_qualification_t exit_qual;
pa_t page_hpa;
bool_t shared_bit = get_gpa_shared_bit(gpa.raw, gpaw);
access_rights_t access_rights = { .raw = 0 };
access_rights.r = 1;
access_rights.w = (mapping_type == TDX_RANGE_RW) ? 1 : 0;
access_rights.x = (uint8_t)0;
exit_qual.raw = (uint64_t)access_rights.raw;
if (!check_gpa_validity(gpa, gpaw, check_gpa_is_private))
{
return TDX_OPERAND_INVALID;
}
if (shared_bit)
{
// read the shared EPT from the TD VMCS
ia32_vmread(VMX_GUEST_SHARED_EPT_POINTER_FULL_ENCODE, &eptp.raw);
eptp.fields.enable_ad_bits = tdcs_p->executions_ctl_fields.eptp.fields.enable_ad_bits;
eptp.fields.enable_sss_control = tdcs_p->executions_ctl_fields.eptp.fields.enable_sss_control;
eptp.fields.ept_ps_mt = tdcs_p->executions_ctl_fields.eptp.fields.ept_ps_mt;
eptp.fields.ept_pwl = tdcs_p->executions_ctl_fields.eptp.fields.ept_pwl;
}
else
{
eptp.raw = tdcs_p->executions_ctl_fields.eptp.raw;
}
walk_result = gpa_translate(eptp, gpa, !shared_bit, hkid, access_rights,
&page_hpa, &ept_entry_copy, &accumulated_rwx);
exit_qual.ept_violation.data_read = access_rights.r & ~accumulated_rwx.r;
exit_qual.ept_violation.data_write = access_rights.w & ~accumulated_rwx.w;
exit_qual.ept_violation.insn_fetch = access_rights.x & ~accumulated_rwx.x;
exit_qual.ept_violation.gpa_readable = accumulated_rwx.r;
exit_qual.ept_violation.gpa_writeable = accumulated_rwx.w;
exit_qual.ept_violation.gpa_executable = accumulated_rwx.x;
vmx_ext_exit_qual_t ext_exit_qual = { .raw = 0 };
vm_vmexit_exit_reason_t vm_exit_reason = { .raw = 0 };
vm_exit_reason.basic_reason = VMEXIT_REASON_EPT_VIOLATION;
IF_RARE (!shared_bit && (walk_result != EPT_WALK_SUCCESS))
{
ia32e_sept_t sept_copy = { .raw = ept_entry_copy.raw };
if (sept_state_is_any_pending_and_guest_acceptable(sept_copy))
{
// This is a pending page waiting for acceptable by the TD
if (tdcs_p->executions_ctl_fields.td_ctls.pending_ve_disable)
{
// The TD is configured to TD exit on access to a PENDING page
ext_exit_qual.type = VMX_EEQ_PENDING_EPT_VIOLATION;
tdx_ept_violation_exit_to_vmm(gpa, vm_exit_reason, exit_qual.raw, ext_exit_qual.raw);
}
else
{
// The TD is configured to throw a #VE on access to a PENDING page
inject_ve_and_return_to_td(tdvps_p, gpa, exit_qual);
}
}
else
{
// This is not a PENDING page, do an EPT Violation TD exit
tdx_ept_violation_exit_to_vmm(gpa, vm_exit_reason, exit_qual.raw, 0);
}
}
IF_RARE (walk_result == EPT_WALK_MISCONFIGURATION)
{
tdx_ept_misconfig_exit_to_vmm(gpa);
}
else IF_RARE (walk_result == EPT_WALK_VIOLATION)
{
tdx_ept_violation_exit_to_vmm(gpa, vm_exit_reason, exit_qual.raw, ext_exit_qual.raw);
}
else IF_RARE (walk_result == EPT_WALK_CONVERTIBLE_VIOLATION)
{
inject_ve_and_return_to_td(tdvps_p, gpa, exit_qual);
}
// Else - success
if (shared_bit)
{
if (ept_entry_copy.fields_4k.mt != MT_WB)
{
*la = map_pa_non_wb(page_hpa.raw_void, mapping_type);
}
else
{
*la = map_pa(page_hpa.raw_void, mapping_type);
}
}
else
{
*la = map_pa_with_hkid(page_hpa.raw_void, hkid, mapping_type);
}
return TDX_SUCCESS;
}
api_error_code_e associate_vcpu(tdvps_t * tdvps_ptr,
tdcs_t * tdcs_ptr,
bool_t* new_association)
{
uint32_t prev_assoc_lpid; // Previous associated LPID
uint32_t curr_lp_id = get_local_data()->lp_info.lp_id;
/**
* Atomically check that this VCPU is not associated with any LP, and
* associate it with the current LP. The VCPU may already be associated
* with the current LP, but if it's associated with another LP this is
* an error.
*/
prev_assoc_lpid = _lock_cmpxchg_32b(VCPU_NO_LP, // Expected value: no lp
curr_lp_id, // New Value
&tdvps_ptr->management.assoc_lpid); // Target
if ((prev_assoc_lpid != VCPU_NO_LP) && (prev_assoc_lpid != curr_lp_id))
{
return TDX_VCPU_ASSOCIATED;
}
// Association succeeded. VCPU state must be VCPU_READY_*
// Set ACTIVE_VMCS to -1 to indicate the need for VMPTRLD
get_local_data()->vp_ctx.active_vmcs = ACTIVE_VMCS_NONE;
uint64_t seamdb_index = get_global_data()->seamdb_index;
if (tdvps_ptr->management.last_seamdb_index != seamdb_index)
{
// The TDX module has been updated since the last time this VCPU was associated.
// The VCPU remains associated with the current LP.
clear_module_host_state_flags(tdvps_ptr);
clear_lp_host_state_flags(tdvps_ptr);
tdvps_ptr->management.last_seamdb_index = seamdb_index;
}
// If this is a new association, update TD VMCS
if (prev_assoc_lpid == VCPU_NO_LP)
{
// The current VCPU is associated with a new LP