diff --git a/pkg/sentry/platform/kvm/BUILD b/pkg/sentry/platform/kvm/BUILD index 6c7c461e5..2aac1a1ef 100644 --- a/pkg/sentry/platform/kvm/BUILD +++ b/pkg/sentry/platform/kvm/BUILD @@ -41,6 +41,7 @@ go_library( "bluepill_arm64_unsafe.go", "bluepill_fault.go", "bluepill_unsafe.go", + "config.go", "context.go", "filters.go", "filters_amd64.go", diff --git a/pkg/sentry/platform/kvm/config.go b/pkg/sentry/platform/kvm/config.go new file mode 100644 index 000000000..8cf8fa5c8 --- /dev/null +++ b/pkg/sentry/platform/kvm/config.go @@ -0,0 +1,23 @@ +// Copyright 2025 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !false +// +build !false + +package kvm + +// Config sets configuration options for each platform instance. +type Config struct{} + +func (*machine) applyConfig(config *Config) error { return nil } diff --git a/pkg/sentry/platform/kvm/kvm.go b/pkg/sentry/platform/kvm/kvm.go index 987d6cb4f..69070b3e8 100644 --- a/pkg/sentry/platform/kvm/kvm.go +++ b/pkg/sentry/platform/kvm/kvm.go @@ -92,7 +92,7 @@ func OpenDevice(devicePath string) (*fd.FD, error) { } // New returns a new KVM-based implementation of the platform interface. -func New(deviceFile *fd.FD) (*KVM, error) { +func New(deviceFile *fd.FD, config Config) (*KVM, error) { fd := deviceFile.FD() // Ensure global initialization is done. @@ -122,7 +122,7 @@ func New(deviceFile *fd.FD) (*KVM, error) { deviceFile.Close() // Create a VM context. - machine, err := newMachine(int(vm)) + machine, err := newMachine(int(vm), &config) if err != nil { return nil, err } @@ -185,7 +185,7 @@ func (k *KVM) NewContext(pkgcontext.Context) platform.Context { type constructor struct{} func (*constructor) New(f *fd.FD) (platform.Platform, error) { - return New(f) + return New(f, Config{}) } func (*constructor) OpenDevice(devicePath string) (*fd.FD, error) { diff --git a/pkg/sentry/platform/kvm/kvm_const.go b/pkg/sentry/platform/kvm/kvm_const.go index 327acaf34..3a3fc43c8 100644 --- a/pkg/sentry/platform/kvm/kvm_const.go +++ b/pkg/sentry/platform/kvm/kvm_const.go @@ -42,6 +42,7 @@ const ( KVM_GET_VCPU_EVENTS = 0x8040ae9f KVM_SET_VCPU_EVENTS = 0x4040aea0 KVM_SET_DEVICE_ATTR = 0x4018aee1 + KVM_ENABLE_CAP = 0x4068aea3 ) // KVM exit reasons. diff --git a/pkg/sentry/platform/kvm/kvm_test.go b/pkg/sentry/platform/kvm/kvm_test.go index 4874185ec..48a501c01 100644 --- a/pkg/sentry/platform/kvm/kvm_test.go +++ b/pkg/sentry/platform/kvm/kvm_test.go @@ -48,7 +48,7 @@ func kvmTest(t testHarness, setup func(*KVM), fn func(*vCPU) bool) { if err != nil { t.Fatalf("error opening device file: %v", err) } - k, err := New(deviceFile) + k, err := New(deviceFile, Config{}) if err != nil { t.Fatalf("error creating KVM instance: %v", err) } diff --git a/pkg/sentry/platform/kvm/machine.go b/pkg/sentry/platform/kvm/machine.go index 4cf52ed92..9bfb642b0 100644 --- a/pkg/sentry/platform/kvm/machine.go +++ b/pkg/sentry/platform/kvm/machine.go @@ -267,11 +267,15 @@ func (m *machine) createVCPU(id int) *vCPU { var forceMappingEntireAddressSpace = false // newMachine returns a new VM context. -func newMachine(vm int) (*machine, error) { +func newMachine(vm int, config *Config) (*machine, error) { // Create the machine. m := &machine{fd: vm} m.available.L = &m.mu + if err := m.applyConfig(config); err != nil { + panic(fmt.Sprintf("error setting config parameters: %s", err)) + } + // Pull the maximum vCPUs. m.getMaxVCPU() log.Debugf("The maximum number of vCPUs is %d.", m.maxVCPUs) @@ -876,3 +880,10 @@ func seccompMmapRules(m *machine) { m.machinePoolIndex = i machinePoolMu.Unlock() } + +type kvmEnableCap struct { + capability uint32 + flags uint32 + args [4]uint64 + pad [64]uint8 +}