@@ -5,11 +5,14 @@ package qemu
55import (
66 "fmt"
77 "strconv"
8+
9+ "github.com/caarlos0/env/v10"
810)
911
1012const (
1113 BaseGuestCID = 3
1214 KernelCommandLine = "quiet console=null"
15+ TDXObject = "'{\" qom-type\" :\" tdx-guest\" ,\" id\" :\" %s\" ,\" quote-generation-socket\" :{\" type\" : \" vsock\" , \" cid\" :\" 2\" ,\" port\" :\" %d\" }}'"
1316)
1417
1518type MemoryConfig struct {
@@ -52,14 +55,20 @@ type DiskImgConfig struct {
5255 RootFsFile string `env:"DISK_IMG_ROOTFS_FILE" envDefault:"img/rootfs.cpio.gz"`
5356}
5457
55- type SevConfig struct {
58+ type SEVConfig struct {
5659 ID string `env:"SEV_ID" envDefault:"sev0"`
5760 CBitPos int `env:"SEV_CBITPOS" envDefault:"51"`
5861 ReducedPhysBits int `env:"SEV_REDUCED_PHYS_BITS" envDefault:"1"`
5962 EnableHostData bool `env:"ENABLE_HOST_DATA" envDefault:"false"`
6063 HostData string `env:"HOST_DATA" envDefault:""`
6164}
6265
66+ type TDXConfig struct {
67+ ID string `env:"TDX_ID" envDefault:"tdx0"`
68+ QuoteGenerationPort int `env:"QUOTE_GENERATION_PORT" envDefault:"4050"`
69+ OVMF string `env:"OVMF_FILE" envDefault:"/usr/share/ovmf/OVMF.fd"`
70+ }
71+
6372type IGVMConfig struct {
6473 ID string `env:"IGVM_ID" envDefault:"igvm0"`
6574 File string `env:"IGVM_FILE" envDefault:"/root/coconut-qemu.igvm"`
@@ -71,10 +80,11 @@ type VSockConfig struct {
7180}
7281
7382type Config struct {
83+ EnableSEV bool
84+ EnableSEVSNP bool
85+ EnableTDX bool
7486 QemuBinPath string `env:"BIN_PATH" envDefault:"qemu-system-x86_64"`
7587 UseSudo bool `env:"USE_SUDO" envDefault:"false"`
76- EnableSEV bool `env:"ENABLE_SEV" envDefault:"false"`
77- EnableSEVSNP bool `env:"ENABLE_SEV_SNP" envDefault:"true"`
7888
7989 EnableKVM bool `env:"ENABLE_KVM" envDefault:"true"`
8090
@@ -101,7 +111,10 @@ type Config struct {
101111 DiskImgConfig
102112
103113 // SEV
104- SevConfig
114+ SEVConfig
115+
116+ // TDX
117+ TDXConfig
105118
106119 // vTPM
107120 IGVMConfig
@@ -142,7 +155,7 @@ func (config Config) ConstructQemuArgs() []string {
142155 config .MemoryConfig .Slots ,
143156 config .MemoryConfig .Max ))
144157
145- if ! config .EnableSEVSNP {
158+ if ! config .EnableSEVSNP && ! config . EnableTDX {
146159 // OVMF
147160 args = append (args , "-drive" ,
148161 fmt .Sprintf ("if=%s,format=%s,unit=%d,file=%s,readonly=%s" ,
@@ -183,15 +196,15 @@ func (config Config) ConstructQemuArgs() []string {
183196
184197 args = append (args , "-machine" ,
185198 fmt .Sprintf ("confidential-guest-support=%s,memory-backend=%s,igvm-cfg=%s" ,
186- config .SevConfig .ID ,
199+ config .SEVConfig .ID ,
187200 config .MemID ,
188201 config .IGVMConfig .ID ))
189202
190203 if config .EnableSEVSNP {
191204 sevType = "sev-snp-guest"
192205
193- if config .SevConfig .EnableHostData {
194- hostData = fmt .Sprintf (",host-data=%s" , config .SevConfig .HostData )
206+ if config .SEVConfig .EnableHostData {
207+ hostData = fmt .Sprintf (",host-data=%s" , config .SEVConfig .HostData )
195208 }
196209 }
197210
@@ -203,9 +216,9 @@ func (config Config) ConstructQemuArgs() []string {
203216 args = append (args , "-object" ,
204217 fmt .Sprintf ("%s,id=%s,cbitpos=%d,reduced-phys-bits=%d%s" ,
205218 sevType ,
206- config .SevConfig .ID ,
207- config .SevConfig .CBitPos ,
208- config .SevConfig .ReducedPhysBits ,
219+ config .SEVConfig .ID ,
220+ config .SEVConfig .CBitPos ,
221+ config .SEVConfig .ReducedPhysBits ,
209222 hostData ))
210223
211224 args = append (args , "-object" ,
@@ -214,6 +227,26 @@ func (config Config) ConstructQemuArgs() []string {
214227 config .IGVMConfig .File ))
215228 }
216229
230+ if config .EnableTDX {
231+ args = append (args , "-object" ,
232+ fmt .Sprintf (TDXObject ,
233+ config .TDXConfig .ID ,
234+ config .TDXConfig .QuoteGenerationPort ))
235+
236+ args = append (args , "-machine" ,
237+ fmt .Sprintf ("confidential-guest-support=%s,memory-backend=%s,hpet=off" ,
238+ config .TDXConfig .ID ,
239+ config .MemID ))
240+
241+ args = append (args , "-object" ,
242+ fmt .Sprintf ("memory-backend-memfd,id=%s,size=%s,share=true,prealloc=false" ,
243+ config .MemID ,
244+ config .MemoryConfig .Size ))
245+
246+ args = append (args , "-bios" , config .TDXConfig .OVMF )
247+ args = append (args , "-nodefaults" )
248+ }
249+
217250 args = append (args , "-kernel" , config .DiskImgConfig .KernelFile )
218251 args = append (args , "-append" , strconv .Quote (KernelCommandLine ))
219252 args = append (args , "-initrd" , config .DiskImgConfig .RootFsFile )
@@ -237,3 +270,17 @@ func (config Config) ConstructQemuArgs() []string {
237270
238271 return args
239272}
273+
274+ func NewConfig () (* Config , error ) {
275+ cfg := Config {}
276+
277+ if err := env .Parse (& cfg ); err != nil {
278+ return nil , err
279+ }
280+
281+ cfg .EnableSEV = SEVEnabledOnHost ()
282+ cfg .EnableSEVSNP = SEVSNPEnabledOnHost ()
283+ cfg .EnableTDX = TDXEnabledOnHost ()
284+
285+ return & cfg , nil
286+ }
0 commit comments