-
Notifications
You must be signed in to change notification settings - Fork 11
/
sev_mcmd.c
9219 lines (7872 loc) · 315 KB
/
sev_mcmd.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) 2016-2021 Advanced Micro Devices, Inc. All rights reserved.
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "amd_cert.h"
#include "apicid.h"
#include "ccp_direct_cipher.h"
#include "nist_kdf.h"
#include "secure_ops.h"
#include "sev_extended_errors.h"
#include "sev_globals.h"
#include "sev_hal.h"
#include "sev_mcmd.h"
#include "sev_persistent.h"
#include "sev_scmd.h"
#include "sev_tmr.h"
#include "sscb.h"
#include "umc.h"
#include "x86_copy.h"
#include "sw_hash.h"
/* For the SEV COPY command */
#define SEV_COPY_LENGTH_MULT (4096)
#define SEV_COPY_ALIGN_MBZ_MASK (4096-1)
/* For SEV INIT_EX command */
#define SEV_INIT_EX_CMD_LEN (0x24)
#define SEV_INIT_EX_NV_LEN (32*1024)
/**
* These are too big for the stack, so store them in the scratch buffer
*/
typedef struct import_scratch
{
sev_persistent_t saved;
sev_cert_t pek;
sev_cert_t oca;
} import_scratch_t;
typedef struct pdh_cert_chain
{
sev_cert_t pek;
sev_cert_t oca;
sev_cert_t cek;
} pdh_cert_chain_t;
/**
* This command is used to inform the firmware that the guest is bound to a
* particular ASID and designate which specific CCX will be allowed to run
* the guest. The firmware then loads the guest's VEK into the
* memory controller at the key slot for that ASID.
* Once the guest is RUNNING, the designated CCXs will be allowed to execute
* the guest.
*/
typedef struct act_ex_apicids
{
uint32_t apic_ids[APIC_ID_LIST_MAX_CNT];
} act_ex_apicids_t;
typedef struct send_start_scratch
{
pdh_cert_chain_t chain; /* PEK/OCA/CEK */
sev_cert_t pdh;
sev_cert_pubkey_t ask_pubkey;
uint8_t amd_chain[1];
} send_start_scratch_t;
static bool init_cmd_buf_valid(const sev_mcmd_init_t *init)
{
uint16_t mbz_flags = (uint16_t)~SEV_CONFIG_ES_FLAG;
sev_mcmd_init_t zero;
memset(&zero, 0, sizeof(zero));
return init != NULL && flags_valid(init->flags, mbz_flags) &&
memcmp(init->reserved, zero.reserved, sizeof(init->reserved)) == 0;
}
static bool init_ex_cmd_buf_valid(const sev_mcmd_init_ex_t *init)
{
uint16_t mbz_flags = (uint16_t)~SEV_CONFIG_ES_FLAG;
sev_mcmd_init_ex_t zero;
memset(&zero, 0, sizeof(zero));
return init != NULL && flags_valid(init->flags, mbz_flags) &&
memcmp(init->reserved1, zero.reserved1, sizeof(init->reserved1)) == 0 &&
memcmp(&init->reserved2, &zero.reserved2, sizeof(init->reserved2)) == 0;
}
static bool vlek_installed(void)
{
const uint8_t *p = &gpDram->perm.vlek[0][0];
const uint8_t *end = p + sizeof(gpDram->perm.vlek);
bool retval = false;
do {
retval |= *p != 0;
} while (++p < end);
return retval;
}
/**
* (CSF-684) wrbkinvd is needed anytime you are writing to a page with a different
* encryption/asid key than what it currently contains and need to evict any
* stale data in the caches.
* Two main cases
* 1. Reading from a page unencrypted and writing to it encrypted (LaunchUpdate)
* - Only the wrbkinvd is needed. Do not have to do a dummy read on the dst
* page because the read will cause the necessary invalidates
* 2. Writing to a page encrypted that may currently be unencrypted (x86 key)
* or encrypted with a different asid. Need to evict any stale data by
* performing the dummy read on the dst page. See CSF-698 below
*
*
* (CSF-698) Since copy_to_x86_encrypted encryption/decryption operations are not
* being done in-place, and if using cached memory, need to take some extra steps
* to handle caching issues. For DbgEncrypt, for example, the PSP is going to write
* to DstPage with ciphertext, but before it does that, you need to ensure that
* any dirty plaintext data in that page is evicted. To do that, need to do both
* 1) Set the RdSzWrBkInvd bit and then
* 2) Do a copy_from_x86 on the DstPage (and just throw away the result).
* This step ensures that you evict any of those cachelines on the x86
* side before you write to that page. You should only need to do that
* for DbgEncrypt though since with DbgDecrypt you're issuing plaintext
* writes (which will naturally cause appropriate evictions).
* Issue is in DbgEncrypt, LaunchSecret, ReceiveUpdate, and SwapIn
*/
static sev_status_t set_misc_read_sized_wrbkinvd(bool enable)
{
sev_status_t status = SEV_STATUS_SUCCESS;
/* Set/clear the master die */
status = sev_hal_set_misc_read_sized_wrbkinvd(enable);
if (status != SEV_STATUS_SUCCESS)
goto end;
/* Set/clear on slave die also */
if (gTotalDieNum > 1)
{
sev_scmd_t scmd;
memset(&scmd, 0, sizeof(scmd));
scmd.id = SEV_SCMD_ID_RD_SZ_WRBKINVD;
scmd.scmd.sz_wrbkinvd.enable = enable;
status = sev_hal_master_to_slave(1, &scmd, sizeof(scmd));
if (status != SEV_STATUS_SUCCESS)
goto end;
}
end:
return SEV_ERROR(status, EXT_ERR_001);
}
/**
* Refresh the perm.committed* variables from the current FW header
*/
static sev_status_t update_committed_versions(SEV_FW_IMAGE_HEADER *hdr)
{
sev_status_t status = SEV_STATUS_SUCCESS;
if (hdr == NULL)
hdr = (SEV_FW_IMAGE_HEADER *)&Image$$SEV_UAPP_CODE$$Base;
/* Set the cached committed FW versions from the current FW */
gpDram->perm.committed_build_id = (hdr->FWVersion & 0xff);
gpDram->perm.committed_api_minor = (hdr->FWVersion >> 8) & 0xff;
gpDram->perm.committed_api_major = (hdr->FWVersion >> 16) & 0xff;
/* Set the committed TCB SNP component from the current FW */
(void)get_running_tcb(&gpDram->perm.committed_tcb, hdr); /* Can't return an error */
/* Finally, when a "commit" occurs, that flushes any CONFIG'd TCB */
gPersistent.config_tcb.val = 0;
/* Regnerate the VCEK using the new TCB */
status = vcek_hash_derive(gpDram->perm.snp_identity.vcek_hash, DIGEST_SHA384_SIZE_BYTES, NULL);
if (status == SEV_STATUS_SUCCESS)
status = vcek_derive(&gpDram->perm.snp_identity);
/* If we have an error here, nuke the VCEK */
if (status != SEV_STATUS_SUCCESS)
memset(&gpDram->perm.snp_identity, 0, sizeof(gpDram->perm.snp_identity));
return status;
}
/**
* Divide the DesiredTscFreq by the Ref Clk of the PSP and store the
* result in a 8.32 fixed-point binary number format.
* See TSC Ratio MSR (C000_0104h) in APM Vol. 2 for format.
* Also, http://twiki.amd.com/twiki/bin/viewauth/PSArch/PsHldUcSecureTsc
* Input frequencies in kHz.
*/
#define FRAC_WIDTH 32
static uint64_t calc_tsc_scale(uint32_t desired_tsc_freq, uint32_t ref_clk_freq)
{
uint64_t guest_tsc_scale = 0, integer_part = 0, fractional_part = 0;
if(ref_clk_freq != 0)
{
integer_part = desired_tsc_freq / ref_clk_freq;
fractional_part = desired_tsc_freq - integer_part*ref_clk_freq; /* Remainder */
fractional_part = ((fractional_part << FRAC_WIDTH) + (ref_clk_freq - 1)) / ref_clk_freq;
}
/* Truncate any number that's taking up more bits than allowed */
integer_part &= 0xFF;
fractional_part &= (1ULL << FRAC_WIDTH) - 1;
guest_tsc_scale = (integer_part << FRAC_WIDTH) | fractional_part;
return guest_tsc_scale;
}
sscb_t gSSCB = {0};
/* Check that the TSEG MSRs match the SSCB advertised to everyone else */
static sev_status_t check_tseg(void)
{
uint64_t tseg_base = 0;
uint64_t tseg_mask = 0;
uint64_t tseg_end = 0;
sev_status_t status = SEV_STATUS_SUCCESS;
uint32_t is_same = false;
status = sev_hal_check_msr(MSR_TSEG_BASE, &is_same, &tseg_base);
if (status != SEV_STATUS_SUCCESS)
return status;
status = sev_hal_check_msr(MSR_TSEG_MASK, &is_same, &tseg_mask);
if (status != SEV_STATUS_SUCCESS)
return status;
tseg_mask &= GET_MASK64(47,17); /* APM is wrong, PPR is right */
tseg_base &= tseg_mask;
/* tseg_end always has the bottom 17 bits set */
tseg_end = (tseg_base + (~tseg_mask & GET_MASK64(47,17))) | GET_MASK64(16,0);
if (tseg_base != gSSCB.SMM_Base || tseg_end != gSSCB.SMM_End)
return SEV_STATUS_INVALID_CONFIG;
return status;
}
/**
* Note that for the SEV and SNP functions, the hierarchy of checks must be kept
* the same. You must check Platform state before checking Guest state, etc.
* All of the _common functions are assuming that a valid Guest is being passed
* in and not NULL.
*/
/* -------------- Common Functions between SEV and SNP -------------- */
static sev_status_t mcmd_df_flush_common(sev_t *sev)
{
sev_status_t status = SEV_STATUS_SUCCESS;
bool wbinvd_is_done = false;
uint32_t ccds_wbinvd_done = 0;
uint32_t asid_idx = 0;
if (!sev)
{
status = ERR_INVALID_PARAMS;
goto end;
}
/* Check that a WBINVD has been done for the cores on all dies */
status = is_wbinvd_done(&wbinvd_is_done, &ccds_wbinvd_done);
if (status != SEV_STATUS_SUCCESS)
goto end;
/*
* For each CCX for which all of its cores' WBINVD_DONE flags are set,
* all ASIDs that are in FW state "dirty" transition to state "clean".
*/
if (ccds_wbinvd_done)
{
for (asid_idx = 0; asid_idx < MAX_SEV_ASIDS; asid_idx++)
{
uint32_t tmp_mask = gpDram->perm.asid_dirty[asid_idx] & ccds_wbinvd_done;
if (tmp_mask)
{
gpDram->perm.asid_dirty[asid_idx] &= ~tmp_mask;
gpDram->perm.asid_clean[asid_idx] |= tmp_mask;
}
}
}
/* Were all cores done? */
if (!wbinvd_is_done)
{
status = SEV_STATUS_WBINVD_REQUIRED;
goto end;
}
/* Flush pending writes to the UMC */
status = sev_hal_df_write_flush();
if (status != SEV_STATUS_SUCCESS)
goto end;
/* Flush pending writes on slave dies if any */
if (gTotalDieNum > 1)
{
sev_scmd_t scmd;
memset(&scmd, 0, sizeof(scmd));
scmd.id = SEV_SCMD_ID_DF_FLUSH;
status = sev_hal_master_to_slave(1, &scmd, sizeof(scmd));
if (status != SEV_STATUS_SUCCESS)
goto end;
}
/* If the platform is SEV-ES enabled, it is now safe to initialize the TMR */
/* SEV has to be initialized with ES to have a TMR. Don't run if just SNP */
if (sev->sev.state != SEV_STATE_UNINIT && sev_es_platform_enabled(sev) && !sev_es_is_initialized(&sev->sev.es))
{
status = sev_es_init_trusted_region(&sev->sev.es);
if (status != SEV_STATUS_SUCCESS)
goto end;
}
/* Note: Leaving the ASIDs marked invalid until used by guests
when Activated and Running. Likewise for slave dies. */
end:
return status;
}
static sev_status_t mcmd_decommission_common(sev_t *sev, sev_guest_t *guest)
{
sev_status_t status = SEV_STATUS_SUCCESS;
if (!sev || !guest)
{
status = ERR_INVALID_PARAMS;
goto end;
}
if (guest->asid && asid_is_active(sev, guest->asid))
{
status = SEV_STATUS_ACTIVE;
goto end;
}
/*
* If SEV-ES CRC is allocated, then free any blocks used to store the
* VMSA checksums.
*/
if (sev_es_allocation_guest_enabled(guest))
{
pool_vcpu_t *pool = &sev->sev.es.crc32_pool;
/* Free if the blocks are allocated */
if (guest->es.head_index != INVALID_BLOCK && guest->es.tail_index != INVALID_BLOCK)
{
status = pool_vcpu_free_list(pool, guest->es.head_index, guest->es.tail_index, guest->es.num_vcpus);
if (status != SEV_STATUS_SUCCESS)
goto end;
}
}
/* Only count launched guests in guest_count */
if (guest->type == SEV_GUEST_TYPE_SEV)
{
sev->sev.guest_count--;
}
else if ((guest->type == SEV_GUEST_TYPE_SNP) && (guest->snp_state != SNP_GUEST_STATE_INIT)) /* Can call Decommission from any SNP state */
{
gpDram->perm.snp_guest_count--;
}
sev_guest_clear(guest); /* Clears guest_t thus setting state to invalid */
end:
return status;
}
static sev_status_t activate_common(sev_t *sev, sev_guest_t *guest,
uint32_t asid, uint32_t ccxs)
{
sev_status_t status = SEV_STATUS_SUCCESS;
if (!sev || !guest)
{
status = ERR_INVALID_PARAMS;
goto end;
}
/* Do not allow activate/activate-ex for SENT state */
if (guest->sev_state == SEV_GUEST_STATE_SENT)
{
status = SEV_STATUS_INVALID_GUEST_STATE;
goto end;
}
if (!guest_has_asid(guest))
{
/* First time for guest: Make sure ALL CCXs for this ASID are
'clean', else error out. */
if (gpDram->perm.asid_dirty[asid-1])
{
status = SEV_STATUS_DF_FLUSH_REQUIRED;
goto end;
}
if (gpDram->perm.asid_clean[asid-1] != gPersistent.ccx_present_bit_mask)
{
status = SEV_STATUS_ASID_OWNED;
goto end;
}
guest->ccxs = 0; /* Make sure. */
/* Note: Don't set guest asid here. */
}
else
{
/* Allow subsequent Activates for this guest on same ASID only. */
if (asid != guest->asid)
{
status = SEV_STATUS_ACTIVE;
goto end;
}
guest->ccxs |= ccxs;
}
/*
* This will install guest key in the UMC and save/update
* the CCXs to be enabled.
* If the guest is Running, the ASID is enabled in each of the
* CCXs to be enabled, and the guest Active flag is set.
* Otherwise only the guest 'Pending activation' flag is set.
*/
status = sev_guest_activate(guest, asid, ccxs);
if (status != SEV_STATUS_SUCCESS)
goto end;
if (guest_is_pending(guest))
{
/* Add specified CCXs to 'Allocated'. */
gpDram->perm.asid_allocated[asid-1] |= ccxs;
gpDram->perm.asid_clean[asid-1] &= ~ccxs;
}
else if (guest_is_active(guest))
{
/* Guest fully activated. */
/* Mark all of the guest's enabled CCXs for its ASID 'In use'. */
gpDram->perm.asid_in_use[asid-1] |= guest->ccxs;
gpDram->perm.asid_allocated[asid-1] &= ~guest->ccxs;
gpDram->perm.asid_clean[asid-1] &= ~guest->ccxs;
}
end:
return status;
}
static sev_status_t mcmd_activate_common(sev_t *sev, sev_guest_t *guest, uint32_t asid)
{
sev_status_t status = SEV_STATUS_SUCCESS;
if (!sev || !guest)
{
status = ERR_INVALID_PARAMS;
goto end;
}
/* Check that the ASID is valid */
if ((asid == 0) || (asid > GetMaxSEVASID()))
{
status = SEV_STATUS_INVALID_ASID;
goto end;
}
/* Check that no guest has this ASID. */
if ((gpDram->perm.asid_in_use[asid-1]) || (gpDram->perm.asid_allocated[asid-1]))
{
status = SEV_STATUS_ASID_OWNED;
goto end;
}
/* Check if this guest is already active */
if (!(guest_is_inactive(guest)))
{
status = SEV_STATUS_ACTIVE;
goto end;
}
status = activate_common(sev, guest, asid, gPersistent.ccx_present_bit_mask);
end:
return status;
}
static sev_status_t mcmd_activate_ex_common(sev_t *sev, sev_guest_t *guest,
uint32_t asid, uint32_t numids,
uint64_t ids_paddr, uint64_t ma_paddr)
{
sev_status_t status = SEV_STATUS_SUCCESS;
uint32_t ccxs = 0;
act_ex_apicids_t *apicids = (act_ex_apicids_t *)gpSevScratchBuf;
if (!sev || !guest)
{
status = ERR_INVALID_PARAMS;
goto end;
}
/* Make sure ASID is valid */
if ((asid == 0) || (asid > GetMaxSEVASID()))
{
status = SEV_STATUS_INVALID_ASID;
goto end;
}
/*
* If there was a problem with creation of system
* APICID tables, this command is not supported.
*/
if (sev->activate_ex_enable == false)
{
status = SEV_STATUS_UNSUPPORTED;
goto end;
}
/* Validate Activate_EX specific fields. */
if ((numids == 0) || (numids > APIC_ID_LIST_MAX_CNT))
{
status = SEV_STATUS_INVALID_PARAM;
goto end;
}
/* Get the list of apic IDs */
status = copy_from_x86(ids_paddr, (void *)apicids->apic_ids,
numids * sizeof(uint32_t));
if (status != SEV_STATUS_SUCCESS)
goto end;
status = apicid_to_ccx_bitmask(sev, apicids->apic_ids, numids,
(uint32_t *)&ccxs);
if (status != SEV_STATUS_SUCCESS)
goto end;
/* If POLICY.SINGLE_SOCKET is 1 do the following checks
This check has to go here because it needs the ccxs which get decided above */
if (guest->policy_snp & SNP_GUEST_POLICY_SINGLE_SOCKET_FLAG)
{
/* If the guest is bound to a migration agent, the migration agent must
already be activated and completing this command must not result in
activating the guest on a different socket than its migration agent */
if (ma_paddr != PADDR_INVALID)
{
void *ma_gctx_x86_buffer = NULL;
guest_context_page_t *ma_gctx = NULL;
/* Map to the migration agent's context page so we can read */
status = sev_hal_map_guest_context(ma_paddr, &ma_gctx_x86_buffer, PAGE_SIZE_4K);
if (status != SEV_STATUS_SUCCESS)
goto end;
ma_gctx = (guest_context_page_t *)ma_gctx_x86_buffer;
/* Check that the migration agent is activated */
if (!guest_is_active(&ma_gctx->guest))
{
sev_hal_unmap_guest_context(ma_gctx_x86_buffer, PAGE_SIZE_4K); /* Unmap the ma_gctx_page mem */
status = SEV_STATUS_POLICY_FAILURE;
goto end;
}
/* Check that the pending ccxs are on the same socket as the MA's activated ccxs */
if ((CCXS_ON_P0(ma_gctx->guest.ccxs) && CCXS_ON_P1(ccxs)) || /* MA active on P0 and trying to activate on P1 */
(CCXS_ON_P1(ma_gctx->guest.ccxs) && CCXS_ON_P0(ccxs))) /* MA active on P1 and trying to activate on P0 */
{
sev_hal_unmap_guest_context(ma_gctx_x86_buffer, PAGE_SIZE_4K); /* Unmap the ma_gctx_page mem */
status = SEV_STATUS_POLICY_FAILURE;
goto end;
}
/* Unmap the gctx_page mem */
sev_hal_unmap_guest_context(ma_gctx_x86_buffer, PAGE_SIZE_4K);
}
/* Check that completing this command will not result in activating the guest on multiple sockets */
if ((CCXS_ON_P0(ccxs) && CCXS_ON_P1(ccxs)) || /* Trying to activate on P0 and P1 */
(CCXS_ON_P0(guest->ccxs) && CCXS_ON_P1(ccxs)) || /* Active on P0 and trying to activate on P1 */
(CCXS_ON_P1(guest->ccxs) && CCXS_ON_P0(ccxs))) /* Active on P1 and trying to activate on P0 */
{
status = SEV_STATUS_POLICY_FAILURE;
goto end;
}
}
status = activate_common(sev, guest, asid, ccxs);
end:
return status;
}
/* Supports DebugDecrypt and DebugEncrypt */
static sev_status_t debug_crypt_common(sev_t *sev, sev_guest_t *guest, bool encrypt,
uint64_t src_paddr, uint64_t dst_paddr, uint32_t length)
{
sev_status_t status = SEV_STATUS_SUCCESS;
uint32_t bytes_remaining = 0, size = 0;
uint64_t src = 0, dest = 0;
if (!sev || !guest)
{
status = ERR_INVALID_PARAMS;
goto end;
}
if (guest_is_inactive(guest))
{
status = SEV_STATUS_INACTIVE;
goto end;
}
/* Use the scratch buffer as an intermediate buffer for the encryption operation */
bytes_remaining = length;
src = src_paddr;
dest = dst_paddr;
do
{
size = bytes_remaining < SEV_SCRATCH_BUF_LEN ? bytes_remaining
: SEV_SCRATCH_BUF_LEN;
if (encrypt)
status = encrypt_memory(src, dest, gpSevScratchBuf, size, guest->asid);
else
status = decrypt_memory(src, dest, gpSevScratchBuf, size, guest->asid);
if (status != SEV_STATUS_SUCCESS)
goto end;
bytes_remaining -= size;
src += size;
dest += size;
} while (bytes_remaining > 0);
end:
return status;
}
/* -------------------------- SEV Functions -------------------------- */
/**
* The platform must be in the PSTATE.UNINIT state.
*
* The firmware first loads the persistent state into its private memory, and
* then performs the following actions:
* - The CEK is derived from the chip unique values.
* - If no OCA certificate exists, an OCA signing key is generated and a
* self-signed OCA certificate is created. The signing is written to
* persistent memory.
* - If no PEK exists or the OCA was just regenerated, a PEK signing key is
* generated and a PEK certificate is created and signed by the OCA and CEK.
* The PEK and its certificate are written to persistent memory.
* - A new PDH key is generated unconditionally. A certificate is created for
* the PDH and is signed by the PEK.
* - All SEV-related ASIDs on all cores are marked invalid. Each core requires
* a WBINVD before activating any guest. See ACTIVATE and DEACTIVATE.
* - If CONFIG.ES=1, then the TMR region is made inaccessible by the x86 and
* will be used for SEV-ES related operations.
*
* Upon successful completion, the platform transitions to the PSTATE.INIT state.
*/
static sev_status_t sev_mcmd_init_common(sev_t *sev, sev_mcmd_t *cmd, bool is_init_ex)
{
sev_status_t status = SEV_STATUS_SUCCESS;
bool update_storage = false;
bool init_valid = false;
bool rc = false;
uint16_t flags = 0;
if (!sev || !cmd)
{
status = ERR_INVALID_PARAMS;
goto end;
}
if (sev->sev.state != SEV_STATE_UNINIT)
{
status = SEV_STATUS_INVALID_PLATFORM_STATE;
goto end;
}
/* Need mcm_info to know which UMCs to skip checking during umc_encryption_enabled() */
status = sev_init(sev);
if (status != SEV_STATUS_SUCCESS)
goto end;
/* Bergamo is not supported in BL versions less than MIN_BL_VERSION_BERGAMO */
if ((gPersistent.bl_fw_version < MIN_BL_VERSION_BERGAMO) &&
(gPersistent.soc_version >= RSDN_A0_SOC_VER_VAL))
{
status = SEV_STATUS_UNSUPPORTED;
goto end;
}
/*
* We are cleared to do the INIT. Beyond here, goto exit_sev_init_invalid
* on error. That clears sev state back to Shutdown condition.
* Don't do that above here!
*/
/* Hardware check */
if (!umc_encryption_enabled() || !sme_is_enabled_all_cores() || gPersistent.smke_enabled)
{
status = SEV_STATUS_HARDWARE_PLATFORM;
goto exit_sev_init_invalid;
}
/* Validate the command buffer */
rc = (is_init_ex) ? init_ex_cmd_buf_valid(&cmd->sev_init_ex) :
init_cmd_buf_valid(&cmd->sev_init);
if (rc == false)
{
status = SEV_STATUS_INVALID_CONFIG;
goto exit_sev_init_invalid;
}
/*
* If SNPEn is set, the RMP table must be initialized up before calling SEVInit.
* Even if SNPShutdown is called, the RMP will still be set/up protected until the
* next SNPInit, so there is no chance for SEV to be able to modify the RMP.
* To know it's been set up, check that the page state of RMP_BASE is FIRMWARE.
*/
if (snp_is_enabled_all_cores())
{
status = get_rmp_bounds(); /* Global vars may have gotten cleared during dlfw */
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
if (!is_rmp_table_initialized())
{
status = SEV_STATUS_INVALID_PLATFORM_STATE;
goto exit_sev_init_invalid;
}
}
/* Need to populate sev->sev.init_ex_nv_paddr before persistent access */
if (!is_init_ex)
{
sev->sev.init_ex_nv_paddr = SEV_PERSISTENT_SPI_DEV; /* INIT: SPI access. */
}
else
{
/* INIT_EX: First, validate command buffer parameters. */
if (cmd->sev_init_ex.iex_len != SEV_INIT_EX_CMD_LEN)
{
status = SEV_STATUS_INVALID_LENGTH;
goto exit_sev_init_invalid;
}
if (cmd->sev_init_ex.nv_paddr != SEV_PERSISTENT_SPI_DEV)
{
if (!IS_ALIGNED_TO_4KB(cmd->sev_init_ex.nv_paddr))
{
status = SEV_STATUS_INVALID_ADDRESS;
goto exit_sev_init_invalid;
}
status = validate_address_range(cmd->sev_init_ex.nv_paddr,
cmd->sev_init_ex.nv_length);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
if (cmd->sev_init_ex.nv_length != SEV_INIT_EX_NV_LEN)
{
status = SEV_STATUS_INVALID_LENGTH;
goto exit_sev_init_invalid;
}
}
sev->sev.init_ex_nv_paddr = cmd->sev_init_ex.nv_paddr;
}
/* Check page state if SNP is initialized */
if (gpDram->perm.snp_state != SNP_STATE_UNINIT)
{
if (is_init_ex)
{
status = check_page_range_firmware_writable(cmd->sev_init_ex.nv_paddr,
cmd->sev_init_ex.nv_length);
if (status != SEV_STATUS_SUCCESS)
goto end;
}
}
/*
* The data might not always exist. Ex, PLATFORM_RESET clears the
* Persistent OCA, PEK, PDH, etc
*/
status = sev_persistent_store_retrieve(sev->sev.init_ex_nv_paddr,
&sev->sev.identity.persistent);
if (status != SEV_STATUS_SUCCESS && status != ERR_SECURE_DATA_NON_EXIST)
{
if (status == SEV_STATUS_SECURE_DATA_INVALID)
{
/* INIT_EX NV area: Before returning that error, erase the area. */
(void)sev_persistent_store_delete(sev->sev.init_ex_nv_paddr);
}
goto exit_sev_init_invalid;
}
/* Derive the CEK unconditionally */
status = sev_cek_derive(&sev->sev.identity);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
if (!sev_cert_has_pubkey(&sev->sev.identity.persistent.oca_cert))
{
/* Generate a new OCA key and certificate */
status = sev_oca_generate(&sev->sev.identity);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/* Delete the old PEK and generate a new one below */
status = sev_pek_delete(&sev->sev.identity);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
update_storage = true;
}
if (!sev_cert_has_pubkey(&sev->sev.identity.persistent.pek_cert))
{
/* Generate and sign a new PEK */
status = sev_pek_generate(&sev->sev.identity);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/* Delete the old PDH and generate a new one below */
status = sev_pdh_delete(&sev->sev.identity);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
update_storage = true;
}
if (!sev_cert_has_pubkey(&sev->sev.identity.persistent.pdh_cert))
{
/* Generate and sign a new PDH */
status = sev_pdh_generate(&sev->sev.identity);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
update_storage = true;
}
/* Update the persistent storage with any new keys and certificates */
if (update_storage)
{
status = sev_persistent_store_save(sev->sev.init_ex_nv_paddr,
&sev->sev.identity.persistent);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
}
/* ASID initialization should be run only by SEV_INIT or SNP_INIT when
both are in UNINIT state */
if (gpDram->perm.snp_state == SNP_STATE_UNINIT)
{
/* Synchronize APICID Table between master and slave */
/* CCX population. */
status = sync_apicid_tables(sev);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/* Mark ASIDs invalid on all the dies */
status = mark_all_asids_invalid();
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/* Clear WBINVD_DONE bits on all dies */
status = clear_wbinvd_done(gPersistent.ccx_present_bit_mask);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/*
* Init state of all ASIDs/CCXs to 'dirty', not 'clean', not 'in-use',
* and not 'allocated'.
*/
for (uint32_t i = 0; i < SEV_ASID_ARRAY_SIZE; i++)
{
gpDram->perm.asid_dirty[i] = gPersistent.ccx_present_bit_mask;
gpDram->perm.asid_clean[i] = 0;
gpDram->perm.asid_allocated[i] = 0;
gpDram->perm.asid_in_use[i] = 0;
}
}
/* TMR reservation for SEV ES */
flags = is_init_ex ? cmd->sev_init_ex.flags : cmd->sev_init.flags;
if (flags & SEV_CONFIG_ES_FLAG)
{
uint64_t tmr_paddr, tmp_tmr_addr, tmp_nv_addr, tmr_end_addr, nv_end_addr;
uint32_t tmr_length;
if (is_init_ex)
{
tmr_paddr = cmd->sev_init_ex.tmr_paddr;
tmr_length = cmd->sev_init_ex.tmr_length;
/* First, if not using SPI, make sure the TMR and the NV storage
do not overlap. Want to do this before TMR reserve. */
if (cmd->sev_init_ex.nv_paddr)
{
/* Mainly to make sure no unwanted bits are set. */
status = validate_address_range(tmr_paddr, tmr_length);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/* Compare TMR and NV buffer ranges ignoring C-bit. */
tmp_tmr_addr = tmr_paddr;
if (is_rmp_table_initialized())
tmr_end_addr = tmp_tmr_addr + SEV_ES_TMR_SIZE_SNP - 1ULL;
else
tmr_end_addr = tmp_tmr_addr + SEV_ES_TMR_SIZE - 1ULL;
tmp_nv_addr = cmd->sev_init_ex.nv_paddr;
nv_end_addr = tmp_nv_addr + SEV_INIT_EX_NV_LEN - 1ULL;
/* Check for overlap and ignore the C-bit */
if (ranges_overlap(tmp_tmr_addr, tmr_end_addr, tmp_nv_addr, nv_end_addr))
{
status = SEV_STATUS_INVALID_ADDRESS;
goto exit_sev_init_invalid;
}
}
}
else
{
tmr_paddr = cmd->sev_init.tmr_paddr;
tmr_length = cmd->sev_init.tmr_length;
}
/* Check page state if SNP is initialized */
if (gpDram->perm.snp_state != SNP_STATE_UNINIT)
{
if (flags & SEV_CONFIG_ES_FLAG)
{
status = check_page_range_firmware_writable(tmr_paddr, tmr_length);
if (status != SEV_STATUS_SUCCESS)
goto end;
}
}
status = sev_es_reserve_trusted_region(&sev->sev.es, tmr_paddr, tmr_length);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/*
* Don't initialize the pool here. We need to force a WBINVD and
* DF_FLUSH before it is safe to store data in the TMR.
*/
}
/* Advance the platform state machine */
status = sev_state_transition(sev, SEV_MCMD_ID_INIT);
if (status != SEV_STATUS_SUCCESS)
goto exit_sev_init_invalid;
/* No errors. */
sev->sev.config_flags = is_init_ex ? cmd->sev_init_ex.flags : cmd->sev_init.flags;
init_valid = true;
exit_sev_init_invalid:
if (init_valid == false) /* Clear sev state to Shutdown condition. */
{
(void)sev_clear(sev);
}
end:
return status;
}
/* Supports LaunchUpdateData and LaunchUpdateVMSA */
static sev_status_t launch_update_common(sev_t *sev, sev_mcmd_t *cmd, bool is_vmsa)
{
sev_status_t status = SEV_STATUS_SUCCESS;
sev_mcmd_launch_update_t *lud = NULL;
sev_guest_t *guest = NULL;
uint32_t size = 0, bytes_remaining = 0;
uint64_t x86_addr = 0;
if (!sev || !cmd)
{
status = ERR_INVALID_PARAMS;
goto end;
}
if (sev->sev.state != SEV_STATE_WORKING)
{
status = SEV_STATUS_INVALID_PLATFORM_STATE;
goto end;
}
lud = &cmd->sev_launch_update;
if (!handle_is_valid(lud->handle))
{
status = SEV_STATUS_INVALID_GUEST;
goto end;
}
guest = sev_get_guest(sev, lud->handle); /* Guest should not be NULL because we validated the handle */
if (guest->sev_state != SEV_GUEST_STATE_LUPDATE)
{