54 Commits
Author SHA1 Message Date
Jamie LiuandgVisor bot fbca0560dd kvm: honor memmap.File.MemoryType()
Updates #11436

PiperOrigin-RevId: 737689743
2025-03-17 11:34:50 -07:00
Jamie LiuandgVisor bot 2247aceb99 kvm: enable CPUID faulting on all VCPUs
This feature is controlled by an MSR; MSRs are per-CPU.

The Intel SDM doesn't document CPUID faulting, at least as of the Dec 2024
revision; despite the deleted comment in ring0/kernel_amd64.go, there is no
Vol. 3 Table 2-43, and every table in Vol. 4 ("Model-Specific Registers") lists
bit 31 in MSR_PLATFORM_INFO as "reserved". The only documentation seems to be
that cited by Linux's e9ea1e7f53b85 ("x86/arch_prctl: Add
ARCH_[GET|SET]_CPUID"): "Intel Virtualization Technology FlexMigration
Application Note" 323850-004, 2012. This document positions CPUID faulting as
an alternative way to support cross-CPU migration for VMs that don't use VMX;
consequently it does not clarify if CPUID faulting is effective in guest ("VMX
non-root") mode, or if the CPUID VM exit takes precedence. If the former is the
case then CPUID faulting is probably faster than setting app CPUID with
KVM_SET_CPUID2, and vice versa. But regardless, this is much simpler.

PiperOrigin-RevId: 733113944
2025-03-03 17:15:39 -08:00
Tianyu Zhou 135fb65c75 ring0: set PhysicalAddressBits only when uninitialized
Currently ring0.PhysicalAddressBits will be set in ring0.Init() even
if it has already been initialized elsewhere. For example, when
compiling runsc with cgo enabled, function init() of the package
kvm(machine_cgo.go) sets this variable. And this value would be updated
by ring0.Init() when creating a new KVM context. If two values are
inconsistent, the sandbox would panic during later memory region
setting.

This patch fixes it by checking the PhysicalAddressBits in ring0.Init()
and setting the value only if it is uninitialized.

Signed-off-by: Tianyu Zhou <albert.zty@antgroup.com>
2025-01-27 17:34:19 +08:00
Andrei VaginandgVisor bot 4394801ae6 platform/kvm: enable User-Mode Instruction Prevention (UMIP)
This causes SGDT, SIDT, SLDT, SMSW, and STR to raise a #GP exception when
executed from CPL > 0.

PiperOrigin-RevId: 715944097
2025-01-15 14:23:53 -08:00
Konstantin BogomolovandgVisor bot 0760a3df59 kvm: reduce stack usage
Debug build functions use more stack space than normal, such that the
KVM-nosplit function call chain doesn't fit. This patch replaces calls into
unix.RawSyscall* functions with variants that do not grow the stack, and inlines
some functions in ring0/pagetables in order to reduce stack usage. Additionally
seccompMmapHandler is not used during debug builds anymore for making it fit
into the nosplit stack size requirements.

PiperOrigin-RevId: 679774881
2024-09-27 16:58:17 -07:00
Andrei VaginandgVisor bot c0d0fb0677 kvm: set CR4.PGE
```
CR4.PGE (bit-7) enables Paging Global Extensions (PGE). PGE determines whether
moves to CR3 flush all of the PTE's from the TLB, or only those whose G-bit
(global bit) is not set. Likewise, for task switches which implicitly set CR3,
CR4.PGE controls TLB flushing in the same manner.
```

In case of gvisor, it improves performance of switching to/from the Sentry:
          │     kvm     │               kvm.pge               │
          │ wall_ns/op  │ wall_ns/op   vs base                │
Getpid      1.774k ± 0%   1.607k ± 1%   -9.36% (p=0.000 n=10)
GetpidOpt   1.623k ± 1%   1.452k ± 1%  -10.54% (p=0.000 n=10)
geomean     1.696k        1.528k        -9.95%

          │     kvm     │               kvm.pge               │
          │  cpu_ns/op  │  cpu_ns/op   vs base                │
Getpid      1.749k ± 0%   1.583k ± 1%   -9.52% (p=0.000 n=10)
GetpidOpt   1.600k ± 0%   1.414k ± 2%  -11.62% (p=0.000 n=10)
geomean     1.673k        1.496k       -10.58%

It plays only on hosts without PCID.

PiperOrigin-RevId: 671587163
2024-09-05 19:08:08 -07:00
Jing ChenandgVisor bot cf5c4c9cbf Replace reflect.DeepEqual with [slices/maps].Equal.
They are faster on slice/map comparisons.

PiperOrigin-RevId: 633080355
2024-05-12 21:20:18 -07:00
Jamie LiuandgVisor bot aecf514158 Don't XSAVE PKRU state.
We don't implement any of the `pkey_*` syscalls, so applications can't use
protection keys.

Updates #10087

PiperOrigin-RevId: 611287272
2024-02-28 17:47:27 -08:00
Konstantin BogomolovandgVisor bot bb84006816 Fixup AMX workaround for ptrace.
SETREGSET/GETREGSET expect AMX portions of fpstate to always be used.
For this reason we need to allocate enough memory for this to happen,
even if we never populate the AMX portions within initX86FPState.

PiperOrigin-RevId: 599702181
2024-01-18 20:12:31 -08:00
Konstantin BogomolovandgVisor bot e9bdc76c02 Exclude AMX extended state from being xsave/xrstor'd.
For now we are going to completely disable using AMX, so we will
always subtract extended state size reserved from AMX from the rest
of the extended state size, and hardcode the AMX XCR0 bits to be
always off.

Fixes #9750.

PiperOrigin-RevId: 599302059
2024-01-17 15:11:03 -08:00
9aca9a7429 Elide WriteGS if the GS is already correct.
This caches the GS value in the ring0.CPU struct, in order to avoid a
full pipeline flush in the switch path.

Fixes #2010

Co-authored-by: Adin Scannell <adin@scannell.ca>
Co-authored-by: Lai Jiangshan <jiangshan.ljs@antfin.com>
PiperOrigin-RevId: 578919437
2023-11-02 11:21:42 -07:00
Andrei Vagin 5f4abad306 Fix a few typos
It is an idea of running codespell as part of our presubmit checks.
Before enabling it for new changes, let's fix what it has found.

Signed-off-by: Andrei Vagin <avagin@gmail.com>
2023-10-25 12:13:42 -07:00
Andrei VaginandgVisor bot 876908e5e0 kvm/arm64: enable guest mmu from the host
Right now, the start function is executed with the disabled MMU and it means
every access the PA is equal to the MVA [1]. This is known as a flat address
mapping.

By coincidence, the code segment is mapped in lower addresses and we have the
flat address mapping for it. But it isn't the case if we set buildmode=pie to
enable ASLR.

[1] https://developer.arm.com/documentation/ddi0406/b/System-Level-Architecture/Virtual-Memory-System-Architecture--VMSA-/Memory-access-sequence/Enabling-and-disabling-the-MMU

PiperOrigin-RevId: 523745143
2023-04-12 10:53:24 -07:00
Etienne PerotandgVisor bot 0620c3b638 cpuid: Initialize hostFeatureSet only when it's needed.
This moves the initialization of `cpuid.hostFeatureSet` out of package-level
`init` and instead moves it to an explicit `cpuid.Initialize()` function.

On AMD64, this saves about 512KiB of heap memory that would otherwise always
be live.

PiperOrigin-RevId: 514896323
2023-03-07 18:27:49 -08:00
Adin ScannellandgVisor bot 5ba34bd1a5 Add explicit NOFRAME annotation in assembly functions.
For all trivial and zero frame-sized functions, we now require an explicit
NOFRAME annotation as the heuristic has changed. See go.dev/cl/466316.

Most of these functions do not  *require* NOFRAME, but this change encodes
the existing behavior in order to avoid accidental bugs or regressions.

PiperOrigin-RevId: 513946210
2023-03-04 00:53:20 -08:00
Adin ScannellandgVisor bot 1ceb814544 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Adin ScannellandgVisor bot bd561fd3f9 Move from facts render to facts validation.
This allows fact information to be validated in the underlying source files,
but requires us to explicitly maintain this in appropriate version-tagged, and
architecture-tagged files. This is more explicit and safer.

This mechanism uses a special regular expression for matching a +checkconst
stanza to validate constant values, sizes and offsets. This applies to both Go
source files and assembly files.

PiperOrigin-RevId: 511867507
2023-02-23 13:17:49 -08:00
Adin ScannellandgVisor bot be86ca7d22 Fix assembly declarations.
PiperOrigin-RevId: 511648708
2023-02-22 18:14:26 -08:00
Adin ScannellandgVisor bot 6a4908b262 Process nogo targets across all architectures.
PiperOrigin-RevId: 511343590
2023-02-21 17:06:02 -08:00
Michael PrattandgVisor bot 845fb007de Add constants to package-level fact
Unexported constants can end up unaddressable in objectpath and thus their
object facts get lost on fact import. Work around this by adding a
package-level fact which is a map of all constants to their value.

Remove the Value facts because it is confusing to have two ways to access the
same thing (especially when one only works sometimes).

PiperOrigin-RevId: 505153453
2023-01-27 10:38:13 -08:00
Kevin KrakauerandgVisor bot d8aa09e04c convert uses of interface{} to any
Done via:
  find . -name "*.go" | xargs sed -i -E 's/interface\{\}/any/g'

PiperOrigin-RevId: 487033228
2022-11-08 13:14:06 -08:00
Andrei Vagin 860d5ea687 Go 1.18 support for the KVM platform
It is similar with 36a17a814b ("Go 1.17 support for the KVM platform") that
fixed the same problem for x86. Go 1.18 adds the support of the register-based
calling convention on aarch64.

Signed-off-by: Andrei Vagin <avagin@gmail.com>
2022-09-19 22:07:40 -07:00
Andrei VaginandAndrei Vagin 5c544aaccb kvm: add a few sanity checks on vmexit-s
Here are not visible perfomance effects:

Before:
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz
BenchmarkApplicationSyscall
I0401 16:23:28.351151  2594711 physical_map.go:145] region: virtual [3f1eaf85f000,7f202f85f000)
I0401 16:23:28.351260  2594711 physical_map.go:197] physicalRegion: virtual [1000,3f1eaf85f000) => physical [100001000,3f1faf85f000)
I0401 16:23:28.351287  2594711 physical_map.go:197] physicalRegion: virtual [7f202f85f000,7ffffffff000) => physical [3f1faf85f000,3fff7ffff000)
BenchmarkApplicationSyscall-6           	 2574579	       408.2 ns/op
BenchmarkKernelSyscall
BenchmarkKernelSyscall-6                	 2268025	       500.0 ns/op
BenchmarkKernelVDSO
BenchmarkKernelVDSO-6                   	26895987	        41.30 ns/op
BenchmarkWorldSwitchToUserRoundtrip
BenchmarkWorldSwitchToUserRoundtrip-6   	  188020	      5655 ns/op

After:
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz
BenchmarkApplicationSyscall
I0401 16:23:56.143275  2595618 physical_map.go:145] region: virtual [3f5ec46d0000,7f60446d0000)
I0401 16:23:56.143387  2595618 physical_map.go:197] physicalRegion: virtual [1000,3f5ec46d0000) => physical [100001000,3f5fc46d0000)
I0401 16:23:56.143403  2595618 physical_map.go:197] physicalRegion: virtual [7f60446d0000,7ffffffff000) => physical [3f5fc46d0000,3fff7ffff000)
BenchmarkApplicationSyscall-6           	 2798324	       409.8 ns/op
BenchmarkKernelSyscall
BenchmarkKernelSyscall-6                	 2314084	       490.8 ns/op
BenchmarkKernelVDSO
BenchmarkKernelVDSO-6                   	26801140	        40.11 ns/op
BenchmarkWorldSwitchToUserRoundtrip
BenchmarkWorldSwitchToUserRoundtrip-6   	  202519	      5739 ns/op
PASS
2022-09-06 14:08:13 -07:00
Andrei VaginandgVisor bot 479fd558e1 ring0: fix off-by-one bug in IsCanonical
PiperOrigin-RevId: 471373714
2022-08-31 15:42:04 -07:00
Michael PrattandgVisor bot 2513787d6d Fix broken go:nosplit directive
There must be no space in "//go:nosplit". This probably never
caused problems because the function is so simple it is almost
certainly inlined.

PiperOrigin-RevId: 464823961
2022-08-02 10:22:33 -07:00