-
Notifications
You must be signed in to change notification settings - Fork 25
/
tdx.c
988 lines (829 loc) · 24.1 KB
/
tdx.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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2020 Intel Corporation */
#undef pr_fmt
#define pr_fmt(fmt) "tdx: " fmt
#include <linux/cpuhotplug.h>
#include <linux/io.h>
#include <asm/tdx.h>
#include <asm/i8259.h>
#include <asm/apic.h>
#include <asm/idtentry.h>
#include <asm/irq_regs.h>
#include <asm/desc.h>
#include <asm/idtentry.h>
#include <asm/vmx.h>
#include <asm/insn.h>
#include <asm/insn-eval.h>
#include <linux/sched/signal.h> /* force_sig_fault() */
#include <linux/swiotlb.h>
#include <linux/pci.h>
#include <linux/nmi.h>
#include <linux/random.h>
#define CREATE_TRACE_POINTS
#include <asm/trace/tdx.h>
/* TDX Module call Leaf IDs */
#define TDX_GET_INFO 1
#define TDX_GET_VEINFO 3
#define TDX_GET_REPORT 4
#define TDX_ACCEPT_PAGE 6
/* TDX hypercall Leaf IDs */
#define TDVMCALL_MAP_GPA 0x10001
#define TDVMCALL_GET_QUOTE 0x10002
#define TDVMCALL_SETUP_NOTIFY_INTR 0x10004
/* TDX Module call error codes */
#define TDX_PAGE_ALREADY_ACCEPTED 0x00000b0a00000000
#define TDCALL_RETURN_CODE_MASK 0xFFFFFFFF00000000
#define TDCALL_OPERAND_BUSY 0x8000020000000000
#define TDCALL_INVALID_OPERAND 0x8000000000000000
#define TDCALL_SUCCESS 0x0
#define TDCALL_RETURN_CODE(a) ((a) & TDCALL_RETURN_CODE_MASK)
/* TDX hypercall error codes */
#define TDVMCALL_SUCCESS 0x0
#define TDVMCALL_INVALID_OPERAND 0x8000000000000000
#define TDVMCALL_TDREPORT_FAILED 0x8000000000000001
#define VE_IS_IO_OUT(exit_qual) (((exit_qual) & 8) ? 0 : 1)
#define VE_GET_IO_SIZE(exit_qual) (((exit_qual) & 7) + 1)
#define VE_GET_PORT_NUM(exit_qual) ((exit_qual) >> 16)
#define VE_IS_IO_STRING(exit_qual) ((exit_qual) & 16 ? 1 : 0)
static struct {
unsigned int gpa_width;
unsigned long attributes;
} td_info __ro_after_init;
/*
* Currently it will be used only by the attestation
* driver. So, race condition with read/write operation
* is not considered.
*/
void (*tdx_event_notify_handler)(void);
EXPORT_SYMBOL_GPL(tdx_event_notify_handler);
bool is_tdx_guest(void)
{
static int tdx_guest = -1;
u32 eax, sig[3];
if (tdx_guest >= 0)
goto done;
if (cpuid_eax(0) < TDX_CPUID_LEAF_ID) {
tdx_guest = 0;
goto done;
}
cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]);
tdx_guest = !memcmp("IntelTDX ", sig, 12);
done:
return !!tdx_guest;
}
/*
* Wrapper for standard use of __tdx_hypercall with BUG_ON() check
* for TDCALL error.
*/
static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14,
u64 r15, struct tdx_hypercall_output *out)
{
struct tdx_hypercall_output outl;
u64 err;
/* __tdx_hypercall() does not accept NULL output pointer */
if (!out)
out = &outl;
err = __tdx_hypercall(TDX_HYPERCALL_STANDARD, fn, r12, r13, r14,
r15, out);
/* Non zero return value indicates buggy TDX module, so panic */
BUG_ON(err);
return out->r10;
}
/* Traced version of _tdx_hypercall() */
static u64 _trace_tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,
struct tdx_hypercall_output *out)
{
struct tdx_hypercall_output dummy_out;
u64 err;
trace_tdx_hypercall_enter_rcuidle(fn, r12, r13, r14, r15);
err = _tdx_hypercall(fn, r12, r13, r14, r15, out);
if (!out)
out = &dummy_out;
trace_tdx_hypercall_exit_rcuidle(err, out->r11, out->r12, out->r13,
out->r14, out->r15);
return err;
}
static u64 __trace_tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
struct tdx_module_output *out)
{
struct tdx_module_output dummy_out;
u64 err;
trace_tdx_module_call_enter_rcuidle(fn, rcx, rdx, r8, r9);
err = __tdx_module_call(fn, rcx, rdx, r8, r9, out);
if (!out)
out = &dummy_out;
trace_tdx_module_call_exit_rcuidle(err, out->rcx, out->rdx, out->r8,
out->r9, out->r10, out->r11);
return err;
}
/* The highest bit of a guest physical address is the "sharing" bit */
phys_addr_t tdx_shared_mask(void)
{
return 1ULL << (td_info.gpa_width - 1);
}
bool tdx_debug_enabled(void)
{
return td_info.attributes & BIT(0);
}
/* TDX guest event notification handler */
DEFINE_IDTENTRY_SYSVEC(sysvec_tdx_event_notify)
{
struct pt_regs *old_regs = set_irq_regs(regs);
inc_irq_stat(irq_tdx_event_notify_count);
if (tdx_event_notify_handler)
tdx_event_notify_handler();
/*
* The hypervisor requires that the APIC EOI should be acked.
* If the APIC EOI is not acked, the APIC ISR bit for the
* TDX_GUEST_EVENT_NOTIFY_VECTOR will not be cleared and then it
* will block the interrupt whose vector is lower than
* TDX_GUEST_EVENT_NOTIFY_VECTOR.
*/
ack_APIC_irq();
set_irq_regs(old_regs);
}
/*
* tdx_mcall_tdreport() - Generate TDREPORT_STRUCT using TDCALL.
*
* @data : Physical address of 1024B aligned data to store
* TDREPORT_STRUCT.
* @reportdata : Physical address of 64B aligned report data
*
* return 0 on success or failure error number.
*/
int tdx_mcall_tdreport(u64 data, u64 reportdata)
{
u64 ret;
/*
* Use confidential guest TDX check to ensure this API is only
* used by TDX guest platforms.
*/
if (!data || !reportdata || !cc_platform_has(CC_ATTR_GUEST_TDX))
return -EINVAL;
/*
* Pass the physical address of user generated reportdata
* and the physical address of out pointer to store the
* tdreport data to the TDX module to generate the
* TD report. Generated data contains measurements/configuration
* data of the TD guest. More info about ABI can be found in TDX
* Guest-Host-Communication Interface (GHCI), sec 2.4.5.
*/
ret = __trace_tdx_module_call(TDX_GET_REPORT, data, reportdata, 0, 0,
NULL);
if (ret == TDCALL_SUCCESS)
return 0;
else if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
return -EINVAL;
else if (TDCALL_RETURN_CODE(ret) == TDCALL_OPERAND_BUSY)
return -EBUSY;
return -EIO;
}
EXPORT_SYMBOL_GPL(tdx_mcall_tdreport);
/*
* tdx_hcall_get_quote() - Generate TDQUOTE using TDREPORT_STRUCT.
*
* @data : Physical address of 4KB GPA memory which contains
* TDREPORT_STRUCT.
*
* return 0 on success or failure error number.
*/
int tdx_hcall_get_quote(u64 data)
{
u64 ret;
/*
* Use confidential guest TDX check to ensure this API is only
* used by TDX guest platforms.
*/
if (!data || !cc_platform_has(CC_ATTR_GUEST_TDX))
return -EINVAL;
/*
* Pass the physical address of tdreport data to the VMM
* and trigger the tdquote generation. Quote data will be
* stored back in the same physical address space. More info
* about ABI can be found in TDX Guest-Host-Communication
* Interface (GHCI), sec 3.3.
*/
ret = _trace_tdx_hypercall(TDVMCALL_GET_QUOTE, data, 0, 0, 0, NULL);
if (ret == TDVMCALL_SUCCESS)
return 0;
else if (ret == TDVMCALL_INVALID_OPERAND)
return -EINVAL;
else if (ret == TDVMCALL_TDREPORT_FAILED)
return -EBUSY;
return -EIO;
}
EXPORT_SYMBOL_GPL(tdx_hcall_get_quote);
/*
* tdx_hcall_set_notify_intr() - Setup Event Notify Interrupt Vector.
*
* @vector : Vector address to be used for notification.
*
* return 0 on success or failure error number.
*/
int tdx_hcall_set_notify_intr(u8 vector)
{
u64 ret;
/* Minimum vector value allowed is 32 */
if (vector < 32)
return -EINVAL;
/*
* Register callback vector address with VMM. More details
* about the ABI can be found in TDX Guest-Host-Communication
* Interface (GHCI), sec 3.5.
*/
ret = _trace_tdx_hypercall(TDVMCALL_SETUP_NOTIFY_INTR, vector, 0, 0, 0,
NULL);
if (ret == TDVMCALL_SUCCESS)
return 0;
else if (ret == TDCALL_INVALID_OPERAND)
return -EINVAL;
return -EIO;
}
static void tdx_get_info(void)
{
struct tdx_module_output out;
u64 ret;
/*
* TDINFO TDX Module call is used to get the TD
* execution environment information like GPA
* width, number of available vcpus, debug mode
* information, etc. More details about the ABI
* can be found in TDX Guest-Host-Communication
* Interface (GHCI), sec 2.4.2 TDCALL [TDG.VP.INFO].
*/
ret = __trace_tdx_module_call(TDX_GET_INFO, 0, 0, 0, 0, &out);
/*
* Non zero return means buggy TDX module (which is
* fatal). So raise a BUG().
*/
BUG_ON(ret);
td_info.gpa_width = out.rcx & GENMASK(5, 0);
td_info.attributes = out.rdx;
/* Exclude Shared bit from the __PHYSICAL_MASK */
physical_mask &= ~tdx_shared_mask();
}
static void tdx_accept_page(phys_addr_t gpa)
{
u64 ret;
/*
* Pass the page physical address and size (0-4KB) to the
* TDX module to accept the pending, private page. More info
* about ABI can be found in TDX Guest-Host-Communication
* Interface (GHCI), sec 2.4.7.
*/
ret = __trace_tdx_module_call(TDX_ACCEPT_PAGE, gpa, 0, 0, 0, NULL);
/*
* Non zero return value means buggy TDX module (which is
* fatal for TDX guest). So panic here.
*/
BUG_ON(ret && TDCALL_RETURN_CODE(ret) != TDX_PAGE_ALREADY_ACCEPTED);
}
/*
* Inform the VMM of the guest's intent for this physical page:
* shared with the VMM or private to the guest. The VMM is
* expected to change its mapping of the page in response.
*/
int tdx_hcall_gpa_intent(phys_addr_t gpa, int numpages,
enum tdx_map_type map_type)
{
u64 ret = 0;
int i;
if (map_type == TDX_MAP_SHARED)
gpa |= tdx_shared_mask();
/*
* Notify VMM about page mapping conversion. More info
* about ABI can be found in TDX Guest-Host-Communication
* Interface (GHCI), sec 3.2.
*/
ret = _tdx_hypercall(TDVMCALL_MAP_GPA, gpa, PAGE_SIZE * numpages, 0, 0,
NULL);
if (ret)
ret = -EIO;
if (ret || map_type == TDX_MAP_SHARED)
return ret;
/*
* For shared->private conversion, accept the page using
* TDX_ACCEPT_PAGE TDX module call.
*/
for (i = 0; i < numpages; i++)
tdx_accept_page(gpa + i * PAGE_SIZE);
return 0;
}
static __cpuidle void _tdx_halt(const bool irq_disabled, const bool do_sti)
{
u64 ret;
/*
* Emulate HLT operation via hypercall. More info about ABI
* can be found in TDX Guest-Host-Communication Interface
* (GHCI), sec 3.8 TDG.VP.VMCALL<Instruction.HLT>.
*
* The VMM uses the "IRQ disabled" param to understand IRQ
* enabled status (RFLAGS.IF) of TD guest and determine
* whether or not it should schedule the halted vCPU if an
* IRQ becomes pending. E.g. if IRQs are disabled the VMM
* can keep the vCPU in virtual HLT, even if an IRQ is
* pending, without hanging/breaking the guest.
*
* do_sti parameter is used by __tdx_hypercall() to decide
* whether to call STI instruction before executing TDCALL
* instruction.
*/
ret = _trace_tdx_hypercall(EXIT_REASON_HLT, irq_disabled, 0, 0,
do_sti, NULL);
/*
* Use WARN_ONCE() to report the failure. Since tdx_*halt() calls
* are also used in pv_ops, #VE error handler cannot be used to
* report the failure.
*/
WARN_ONCE(ret, "HLT instruction emulation failed\n");
}
static __cpuidle void tdx_halt(void)
{
/*
* Non safe halt is mainly used in CPU offlining and
* the guest will stay in halt state. So, STI
* instruction call is not required (set do_sti as
* false).
*/
const bool irq_disabled = irqs_disabled();
const bool do_sti = false;
_tdx_halt(irq_disabled, do_sti);
}
static __cpuidle void tdx_safe_halt(void)
{
/*
* Since STI instruction will be called in __tdx_hypercall()
* set irq_disabled as false.
*/
const bool irq_disabled = false;
const bool do_sti = true;
_tdx_halt(irq_disabled, do_sti);
}
static bool tdx_read_msr_safe(unsigned int msr, u64 *val)
{
struct tdx_hypercall_output out;
/*
* Emulate the MSR read via hypercall. More info about ABI
* can be found in TDX Guest-Host-Communication Interface
* (GHCI), sec titled "TDG.VP.VMCALL<Instruction.RDMSR>".
*/
if (_trace_tdx_hypercall(EXIT_REASON_MSR_READ, msr, 0, 0, 0, &out))
return false;
*val = out.r11;
return true;
}
/*
* TDX has context switched MSRs and emulated MSRs. The emulated MSRs
* normally trigger a #VE, but that is expensive, which can be avoided
* by doing a direct TDCALL. Unfortunately, this cannot be done for all
* because some MSRs are "context switched" and need WRMSR.
*
* The list for this is unfortunately quite long. To avoid maintaining
* very long switch statements just do a fast path for the few critical
* MSRs that need TDCALL, currently only TSC_DEADLINE.
*
* More can be added as needed.
*
* The others will be handled by the #VE handler as needed.
* See 18.1 "MSR virtualization" in the TDX Module EAS
*/
static bool tdx_fast_tdcall_path_msr(unsigned int msr)
{
switch (msr) {
case MSR_IA32_TSC_DEADLINE:
return true;
default:
return false;
}
}
static bool tdx_write_msr_safe(unsigned int msr, unsigned int low,
unsigned int high)
{
u64 ret;
/*
* Emulate the MSR write via hypercall. More info about ABI
* can be found in TDX Guest-Host-Communication Interface
* (GHCI) sec titled "TDG.VP.VMCALL<Instruction.WRMSR>".
*/
ret = _trace_tdx_hypercall(EXIT_REASON_MSR_WRITE, msr,
(u64)high << 32 | low, 0, 0, NULL);
return ret ? false : true;
}
void notrace tdx_write_msr(unsigned int msr, u32 low, u32 high)
{
if (tdx_fast_tdcall_path_msr(msr))
tdx_write_msr_safe(msr, low, high);
else
native_write_msr(msr, low, high);
}
static bool tdx_handle_cpuid(struct pt_regs *regs)
{
struct tdx_hypercall_output out;
/*
* Emulate CPUID instruction via hypercall. More info about
* ABI can be found in TDX Guest-Host-Communication Interface
* (GHCI), section titled "VP.VMCALL<Instruction.CPUID>".
*/
if (_trace_tdx_hypercall(EXIT_REASON_CPUID, regs->ax, regs->cx,
0, 0, &out))
return false;
/*
* As per TDX GHCI CPUID ABI, r12-r15 registers contains contents of
* EAX, EBX, ECX, EDX registers after CPUID instruction execution.
* So copy the register contents back to pt_regs.
*/
regs->ax = out.r12;
regs->bx = out.r13;
regs->cx = out.r14;
regs->dx = out.r15;
return true;
}
/*
* tdx_handle_early_io() cannot be re-used in #VE handler for handling
* I/O because the way of handling string I/O is different between
* normal and early I/O case. Also, once trace support is enabled,
* tdx_handle_io() will be extended to use trace calls which is also
* not valid for early I/O cases.
*/
static bool tdx_handle_io(struct pt_regs *regs, u32 exit_qual)
{
struct tdx_hypercall_output outh;
int out, size, port, ret;
bool string;
u64 mask;
string = VE_IS_IO_STRING(exit_qual);
/* I/O strings ops are unrolled at build time. */
BUG_ON(string);
out = VE_IS_IO_OUT(exit_qual);
size = VE_GET_IO_SIZE(exit_qual);
port = VE_GET_PORT_NUM(exit_qual);
mask = GENMASK(8 * size, 0);
if (!tdx_allowed_port(port)) {
if (!out) {
regs->ax &= ~mask;
regs->ax |= (UINT_MAX & mask);
}
return true;
}
if (!out) {
ret = _trace_tdx_hypercall(EXIT_REASON_IO_INSTRUCTION,
size, out, port, regs->ax,
&outh);
regs->ax &= ~mask;
regs->ax |= (ret ? UINT_MAX : outh.r11) & mask;
} else {
ret = _tdx_hypercall(EXIT_REASON_IO_INSTRUCTION,
size, out, port, regs->ax,
&outh);
}
return ret ? false : true;
}
static unsigned long tdx_mmio(int size, bool write, unsigned long addr,
unsigned long *val)
{
struct tdx_hypercall_output out;
u64 err;
err = _trace_tdx_hypercall(EXIT_REASON_EPT_VIOLATION, size, write,
addr, *val, &out);
*val = out.r11;
return err;
}
static int tdx_handle_mmio(struct pt_regs *regs, struct ve_info *ve)
{
char buffer[MAX_INSN_SIZE];
unsigned long *reg, val;
struct insn insn = {};
enum mmio_type mmio;
int size, ret;
u8 sign_byte;
if (user_mode(regs)) {
ret = insn_fetch_from_user(regs, buffer);
if (!ret)
return -EFAULT;
if (!insn_decode_from_regs(&insn, regs, buffer, ret))
return -EFAULT;
} else {
ret = copy_from_kernel_nofault(buffer, (void *)regs->ip,
MAX_INSN_SIZE);
if (ret)
return -EFAULT;
insn_init(&insn, buffer, MAX_INSN_SIZE, 1);
insn_get_length(&insn);
}
mmio = insn_decode_mmio(&insn, &size);
if (mmio == MMIO_DECODE_FAILED)
return -EFAULT;
if (mmio != MMIO_WRITE_IMM && mmio != MMIO_MOVS) {
reg = insn_get_modrm_reg_ptr(&insn, regs);
if (!reg)
return -EFAULT;
}
switch (mmio) {
case MMIO_WRITE:
memcpy(&val, reg, size);
ret = tdx_mmio(size, true, ve->gpa, &val);
break;
case MMIO_WRITE_IMM:
val = insn.immediate.value;
ret = tdx_mmio(size, true, ve->gpa, &val);
break;
case MMIO_READ:
ret = tdx_mmio(size, false, ve->gpa, &val);
if (ret)
break;
/* Zero-extend for 32-bit operation */
if (size == 4)
*reg = 0;
memcpy(reg, &val, size);
break;
case MMIO_READ_ZERO_EXTEND:
ret = tdx_mmio(size, false, ve->gpa, &val);
if (ret)
break;
/* Zero extend based on operand size */
memset(reg, 0, insn.opnd_bytes);
memcpy(reg, &val, size);
break;
case MMIO_READ_SIGN_EXTEND:
ret = tdx_mmio(size, false, ve->gpa, &val);
if (ret)
break;
if (size == 1)
sign_byte = (val & 0x80) ? 0xff : 0x00;
else
sign_byte = (val & 0x8000) ? 0xff : 0x00;
/* Sign extend based on operand size */
memset(reg, sign_byte, insn.opnd_bytes);
memcpy(reg, &val, size);
break;
case MMIO_MOVS:
case MMIO_DECODE_FAILED:
return -EFAULT;
}
if (ret)
return -EFAULT;
return insn.length;
}
static unsigned long tdx_virt_mmio(int size, bool write, unsigned long vaddr,
unsigned long *val)
{
pte_t *pte;
int level;
pte = lookup_address(vaddr, &level);
if (!pte)
return -EIO;
return tdx_mmio(size, write,
(pte_pfn(*pte) << PAGE_SHIFT) +
(vaddr & ~page_level_mask(level)),
val);
}
static unsigned char tdx_mmio_readb(void __iomem *addr)
{
unsigned long val;
if (tdx_virt_mmio(1, false, (unsigned long)addr, &val))
return 0xff;
return val;
}
static unsigned short tdx_mmio_readw(void __iomem *addr)
{
unsigned long val;
if (tdx_virt_mmio(2, false, (unsigned long)addr, &val))
return 0xffff;
return val;
}
static unsigned int tdx_mmio_readl(void __iomem *addr)
{
unsigned long val;
if (tdx_virt_mmio(4, false, (unsigned long)addr, &val))
return 0xffffffff;
return val;
}
unsigned long tdx_mmio_readq(void __iomem *addr)
{
unsigned long val;
if (tdx_virt_mmio(8, false, (unsigned long)addr, &val))
return 0xffffffffffffffff;
return val;
}
static void tdx_mmio_writeb(unsigned char v, void __iomem *addr)
{
unsigned long val = v;
tdx_virt_mmio(1, true, (unsigned long)addr, &val);
}
static void tdx_mmio_writew(unsigned short v, void __iomem *addr)
{
unsigned long val = v;
tdx_virt_mmio(2, true, (unsigned long)addr, &val);
}
static void tdx_mmio_writel(unsigned int v, void __iomem *addr)
{
unsigned long val = v;
tdx_virt_mmio(4, true, (unsigned long)addr, &val);
}
static void tdx_mmio_writeq(unsigned long v, void __iomem *addr)
{
unsigned long val = v;
tdx_virt_mmio(8, true, (unsigned long)addr, &val);
}
static const struct iomap_mmio tdx_iomap_mmio = {
.ireadb = tdx_mmio_readb,
.ireadw = tdx_mmio_readw,
.ireadl = tdx_mmio_readl,
.ireadq = tdx_mmio_readq,
.iwriteb = tdx_mmio_writeb,
.iwritew = tdx_mmio_writew,
.iwritel = tdx_mmio_writel,
.iwriteq = tdx_mmio_writeq,
};
static int tdx_cpu_offline_prepare(unsigned int cpu)
{
/*
* Per Intel TDX Virtual Firmware Design Guide,
* sec 4.3.5 and sec 9.4, Hotplug is not supported
* in TDX platforms. So don't support CPU
* offline feature once it is turned on.
*/
return -EOPNOTSUPP;
}
bool tdx_get_ve_info(struct ve_info *ve)
{
struct tdx_module_output out;
u64 ret;
if (!ve)
return false;
/*
* NMIs and machine checks are suppressed. Before this point any
* #VE is fatal. After this point (TDGETVEINFO call), NMIs and
* additional #VEs are permitted (but it is expected not to
* happen unless kernel panics).
*/
ret = __trace_tdx_module_call(TDX_GET_VEINFO, 0, 0, 0, 0, &out);
if (ret)
return false;
ve->exit_reason = out.rcx;
ve->exit_qual = out.rdx;
ve->gla = out.r8;
ve->gpa = out.r9;
ve->instr_len = out.r10 & UINT_MAX;
ve->instr_info = out.r10 >> 32;
return true;
}
bool tdx_handle_virtualization_exception(struct pt_regs *regs,
struct ve_info *ve)
{
bool ret = true;
u64 val;
trace_tdx_virtualization_exception_rcuidle(regs->ip, ve->exit_reason,
ve->exit_qual, ve->gpa,
ve->instr_len,
ve->instr_info, regs->cx,
regs->ax, regs->dx);
switch (ve->exit_reason) {
case EXIT_REASON_HLT:
tdx_halt();
break;
case EXIT_REASON_MSR_READ:
ret = tdx_read_msr_safe(regs->cx, &val);
if (ret) {
regs->ax = (u32)val;
regs->dx = val >> 32;
}
break;
case EXIT_REASON_MSR_WRITE:
ret = tdx_write_msr_safe(regs->cx, regs->ax, regs->dx);
break;
case EXIT_REASON_CPUID:
ret = tdx_handle_cpuid(regs);
break;
case EXIT_REASON_IO_INSTRUCTION:
ret = tdx_handle_io(regs, ve->exit_qual);
break;
case EXIT_REASON_EPT_VIOLATION:
/* Currently only MMIO triggers EPT violation */
ve->instr_len = tdx_handle_mmio(regs, ve);
if (ve->instr_len < 0) {
pr_warn_once("MMIO failed\n");
ret = false;
}
break;
case EXIT_REASON_MONITOR_INSTRUCTION:
case EXIT_REASON_MWAIT_INSTRUCTION:
/*
* Something in the kernel used MONITOR or MWAIT despite
* X86_FEATURE_MWAIT being cleared for TDX guests.
*/
WARN_ONCE(1, "TD Guest used unsupported MWAIT/MONITOR instruction\n");
break;
default:
pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
return false;
}
/* After successful #VE handling, move the IP */
if (ret)
regs->ip += ve->instr_len;
return ret;
}
/*
* Handle early IO, mainly for early printks serial output.
* This avoids anything that doesn't work early on, like tracing
* or printks, by calling the low level functions directly. Any
* problems are handled by falling back to a standard early exception.
*
* Assumes the IO instruction was using ax, which is enforced
* by the standard io.h macros.
*/
static __init bool tdx_early_io(struct pt_regs *regs, u32 exit_qual)
{
struct tdx_hypercall_output outh;
int out, size, port, ret;
bool string;
u64 mask;
string = VE_IS_IO_STRING(exit_qual);
/* I/O strings ops are unrolled at build time. */
if (string)
return 0;
out = VE_IS_IO_OUT(exit_qual);
size = VE_GET_IO_SIZE(exit_qual);
port = VE_GET_PORT_NUM(exit_qual);
mask = GENMASK(8 * size, 0);
ret = _tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, out, port,
regs->ax, &outh);
if (!out && !ret) {
regs->ax &= ~mask;
regs->ax |= outh.r11 & mask;
}
return !ret;
}
/*
* Early #VE exception handler. Just used to handle port IOs
* for early_printk. If anything goes wrong handle it like
* a normal early exception.
*/
__init bool tdx_early_handle_ve(struct pt_regs *regs)
{
struct ve_info ve;
int ret;
if (tdx_get_ve_info(&ve))
return false;
if (ve.exit_reason == EXIT_REASON_IO_INSTRUCTION) {
ret = tdx_early_io(regs, ve.exit_qual);
if (!ret)
regs->ip += ve.instr_len;
return ret;
}
return false;
}
void __init tdx_early_init(void)
{
if (!is_tdx_guest())
return;
setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);
setup_clear_cpu_cap(X86_FEATURE_MCE);
setup_clear_cpu_cap(X86_FEATURE_MTRR);
setup_clear_cpu_cap(X86_FEATURE_APERFMPERF);
setup_clear_cpu_cap(X86_FEATURE_TME);
setup_clear_cpu_cap(X86_FEATURE_CQM_LLC);
/*
* The only secure (mononotonous) timer inside a TD guest
* is the TSC. The TDX module does various checks on the TSC.
* There are no other reliable fall back options. Also checking
* against jiffies is very unreliable. So force the TSC reliable.
*/
setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
tdx_get_info();
tdx_filter_init();
pv_ops.irq.safe_halt = tdx_safe_halt;
pv_ops.irq.halt = tdx_halt;
pv_ops.cpu.write_msr = tdx_write_msr;
legacy_pic = &null_legacy_pic;
swiotlb_force = SWIOTLB_FORCE;
/*
* Disable NMI watchdog because of the risk of false positives
* and also can increase overhead in the TDX module.
* This is already done for KVM, but covers other hypervisors
* here.
*/
hardlockup_detector_disable();
/*
* In TDX relying on environmental noise like interrupt
* timing alone is dubious, because it can be directly
* controlled by a untrusted hypervisor. Make sure to
* mix in the CPU hardware random number generator too.
*/
random_enable_trust_cpu();
iomap_mmio = &tdx_iomap_mmio;
/*
* Make sure there is a panic if something goes wrong,
* just in case it's some kind of host attack.
*/
panic_on_oops = 1;
cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "tdx:cpu_hotplug",
NULL, tdx_cpu_offline_prepare);
alloc_intr_gate(TDX_GUEST_EVENT_NOTIFY_VECTOR,
asm_sysvec_tdx_event_notify);
if (tdx_hcall_set_notify_intr(TDX_GUEST_EVENT_NOTIFY_VECTOR))
pr_warn("Setting event notification interrupt failed\n");
pci_disable_early();
pci_disable_mmconf();
pr_info("Guest initialized\n");
}