1 /*
2 * x86-specific confidential guest methods.
3 *
4 * Copyright (c) 2024 Red Hat Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12 #ifndef TARGET_I386_CG_H
13 #define TARGET_I386_CG_H
14
15 #include "qom/object.h"
16
17 #include "system/confidential-guest-support.h"
18
19 #define TYPE_X86_CONFIDENTIAL_GUEST "x86-confidential-guest"
20
21 OBJECT_DECLARE_TYPE(X86ConfidentialGuest,
22 X86ConfidentialGuestClass,
23 X86_CONFIDENTIAL_GUEST)
24
25 struct X86ConfidentialGuest {
26 /* <private> */
27 ConfidentialGuestSupport parent_obj;
28 };
29
30 /**
31 * X86ConfidentialGuestClass:
32 *
33 * Class to be implemented by confidential-guest-support concrete objects
34 * for the x86 target.
35 */
36 struct X86ConfidentialGuestClass {
37 /* <private> */
38 ConfidentialGuestSupportClass parent;
39
40 /* <public> */
41 int (*kvm_type)(X86ConfidentialGuest *cg);
42 void (*cpu_instance_init)(X86ConfidentialGuest *cg, CPUState *cpu);
43 uint32_t (*adjust_cpuid_features)(X86ConfidentialGuest *cg, uint32_t feature,
44 uint32_t index, int reg, uint32_t value);
45 int (*check_features)(X86ConfidentialGuest *cg, CPUState *cs);
46 };
47
48 /**
49 * x86_confidential_guest_kvm_type:
50 *
51 * Calls #X86ConfidentialGuestClass.kvm_type() callback.
52 */
x86_confidential_guest_kvm_type(X86ConfidentialGuest * cg)53 static inline int x86_confidential_guest_kvm_type(X86ConfidentialGuest *cg)
54 {
55 X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg);
56
57 if (klass->kvm_type) {
58 return klass->kvm_type(cg);
59 } else {
60 return 0;
61 }
62 }
63
x86_confidential_guest_cpu_instance_init(X86ConfidentialGuest * cg,CPUState * cpu)64 static inline void x86_confidential_guest_cpu_instance_init(X86ConfidentialGuest *cg,
65 CPUState *cpu)
66 {
67 X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg);
68
69 if (klass->cpu_instance_init) {
70 klass->cpu_instance_init(cg, cpu);
71 }
72 }
73
74 /**
75 * x86_confidential_guest_adjust_cpuid_features:
76 *
77 * Adjust the supported features from a confidential guest's CPUID values,
78 * returns the adjusted value. There are bits being removed that are not
79 * supported by the confidential computing firmware or bits being added that
80 * are forcibly exposed to guest by the confidential computing firmware.
81 */
x86_confidential_guest_adjust_cpuid_features(X86ConfidentialGuest * cg,uint32_t feature,uint32_t index,int reg,uint32_t value)82 static inline int x86_confidential_guest_adjust_cpuid_features(X86ConfidentialGuest *cg,
83 uint32_t feature, uint32_t index,
84 int reg, uint32_t value)
85 {
86 X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg);
87
88 if (klass->adjust_cpuid_features) {
89 return klass->adjust_cpuid_features(cg, feature, index, reg, value);
90 } else {
91 return value;
92 }
93 }
94
x86_confidential_guest_check_features(X86ConfidentialGuest * cg,CPUState * cs)95 static inline int x86_confidential_guest_check_features(X86ConfidentialGuest *cg,
96 CPUState *cs)
97 {
98 X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg);
99
100 if (klass->check_features) {
101 return klass->check_features(cg, cs);
102 }
103
104 return 0;
105 }
106
107 #endif
108