Commit Graph
42 Commits
Author SHA1 Message Date
Andrei VaginandgVisor bot 45f2ce4cfc 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 d53fe574e0 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 daafc82dd5 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 2911d8c3b8 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Adin ScannellandgVisor bot f668e26ae9 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 f230d5d6f2 Fix assembly declarations.
PiperOrigin-RevId: 511648708
2023-02-22 18:14:26 -08:00
Adin ScannellandgVisor bot 5dd4a95711 Process nogo targets across all architectures.
PiperOrigin-RevId: 511343590
2023-02-21 17:06:02 -08:00
Michael PrattandgVisor bot a43c102a95 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 3a76efe831 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 a4d723013d Go 1.18 support for the KVM platform
It is similar with a16cf1420a ("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 <[email protected]>
2022-09-19 22:07:40 -07:00
Andrei VaginandAndrei Vagin c87d83d707 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 b4cf704995 ring0: fix off-by-one bug in IsCanonical
PiperOrigin-RevId: 471373714
2022-08-31 15:42:04 -07:00
Michael PrattandgVisor bot 80a136fd09 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
Ayush RanjanandgVisor bot 2026e1f078 Reformat codebase.
PiperOrigin-RevId: 449358041
2022-05-17 17:48:35 -07:00
Andrei VaginandgVisor bot 382b659b3f Remove unused ring0.RestoreKernelFPState.
PiperOrigin-RevId: 439933401
2022-04-06 14:20:46 -07:00
Andrei VaginandgVisor bot fab896f01c platform/kvm: dont' set MPX bits in the XCR register
We don't expect that runsc uses MPX . MPX was deprecated and its support
has been removed from the Linux Kernel.

We have seen when the host XCR has these bits, but a KVM VM doesn't
support MPX and xsetbv generates #GP.

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/gvisor/pull/7366 from avagin:gvisor-vs-mpx 9cd6a27048cbcd0c01dbecd7697e2c4619b29f28
PiperOrigin-RevId: 438922868
2022-04-01 15:21:49 -07:00
Andrei VaginandgVisor bot 38e2e3e64e platform/kvm: limit virtual address space size to 256TB
This is maximum that can be addressed with 4-level paging.

PiperOrigin-RevId: 438668768
2022-03-31 14:59:41 -07:00
Andrei VaginandgVisor bot 5ef07a330f kvm: fix CPU offsets for arm64
We have to calculate offsets in the CPU structure,
but we calculate offsets in the CPUArchState structure.

Fixes: dedef233cd ("Refactor nogo and provide facts render.")
PiperOrigin-RevId: 427336023
2022-02-08 17:27:41 -08:00
Andrei VaginandgVisor bot d98aef80ff kvm: enable supervisor-mode access prevention (SMAP)
SMAP can be temporarily disabled for explicit memory accesses by setting
the EFLAGS.AC (Alignment Check) flag. But in out case, EFLAGS.AC  is in
KernelFlagsClear.

Sentry never reads/writes app memory from app address space.

PiperOrigin-RevId: 423967732
2022-01-24 19:21:32 -08:00
Adin ScannellandgVisor bot dedef233cd Refactor nogo and provide facts render.
This change makes the core nogo package less of a "catch all", and splits
functionality into multiple packages. Instead of separate binaries for each
function, a single "cli" package is added with subcommands, and the core
starlark wrappers are also refactored to minimize redundancy.

The new "cli" package also adds support for a "render" command, which
allows factors to be rendered via a Go text template. This is useful for
debugging, but also allows code generation to be updated to use this
mechanism. This eliminates the use of a QEMU wrapper for the older
arch_genrule, and allows the use of a native bazel transition to extract
facts for the appropriate generated file. In other words, the correct facts
will be rendered for generating XXX_arm64.s, even on amd64.

PiperOrigin-RevId: 422846459
2022-01-19 10:26:27 -08:00
Adin ScannellandgVisor bot 33a9bf8e30 Refactor CPUID to allow for use in KVM and ring0.
Updates #5039

PiperOrigin-RevId: 421696994
2022-01-13 17:24:28 -08:00
Ian LewisandgVisor bot a9153fd1e8 Check for support for xgetbv
Fixes #5738

PiperOrigin-RevId: 413054140
2021-11-29 21:26:45 -08:00
Jamie LiuandgVisor bot c25656aa13 Remove ring0 floating point save/load functions on amd64.
ring0.Save/LoadFloatingPoint() are only usable if the caller can ensure that Go
will not clobber floating point registers before/after calling them
respectively. Due to regabig in Go 1.17, this is no longer the case; regabig
(among other things) maintains a zeroed XMM15 during ABIInternal execution,
including by zeroing it after ABI0-to-ABIInternal transitions. In
ring0.sysenter/exception, this happens in
ring0.kernelSyscall/kernelException.abi0 respectively; in
ring0.CPU.SwitchToUser, this happens after returning from
ring0.sysret/iret.abi0. Delete these functions and do floating point save/load
in assembly.

While arm64 doesn't appear to be immediately affected (so this CL permits us to
resume usage of Go 1.17), its use of Save/LoadFloatingPoint() still seems to be
incorrect for the same fundamental reason (Go code can't sanely assume what
registers the Go compiler will or won't use) and should be fixed eventually.

PiperOrigin-RevId: 401895658
2021-10-08 18:03:23 -07:00
Andrei VaginandAndrei Vagin dbe5cf54ae kvm: trap mmap syscalls to map new regions to the guest
We install seccomp rules so that the SIGSYS signal is generated for
each mmap system call. Then our signal handler executes the real mmap
syscall and if a new regions is created, it maps it to the guest.

Signed-off-by: Andrei Vagin <[email protected]>
2021-09-22 14:39:36 -07:00
Andrei VaginandgVisor bot b82ef7a5c0 kvm: set CR0.NE = 1
CR0.NE enables internal x87 floating point error reporting when set, else
enables PC style x87 error detection.

On AMD, the #MF exception isn't generated if CR0.NE isn't set.

PiperOrigin-RevId: 386340269
2021-07-22 15:55:31 -07:00
Jamie LiuandgVisor bot f53b8a38e5 Add go:build directives as required by Go 1.17's gofmt.
PiperOrigin-RevId: 385894869
2021-07-20 16:28:45 -07:00
Andrei VaginandgVisor bot 36898a7fb9 ring0: Initialize sentryXCR0 from Kernel.init()
Fixes #6300

PiperOrigin-RevId: 385840917
2021-07-20 12:07:23 -07:00
Michael PrattandgVisor bot a16cf1420a Go 1.17 support for the KVM platform
Go 1.17 adds a new register-based calling convention. While transparent for
most applications, the KVM platform needs special work in a few cases.

First of all, we need the actual address of some assembly functions, rather
than the address of a wrapper. See http://gvisor.dev/pr/5832 for complete
discussion of this.

More relevant to this CL is that ABI0-to-ABIInternal wrappers (i.e., calls from
assembly to Go) access the G via FS_BASE. The KVM quite fast-and-loose about
the Go environment, often calling into (nosplit) Go functions with
uninitialized FS_BASE.

That will no longer work in Go 1.17, so this CL changes the platform to
consistently restore FS_BASE before calling into Go code.

This CL does not affect arm64 code. Go 1.17 does not support the register-based
calling convention for arm64 (it will come in 1.18), but arm64 also does not
use a non-standard register like FS_BASE for TLS, so it may not require any
changes.

PiperOrigin-RevId: 384234305
2021-07-12 08:01:53 -07:00
Andrei VaginandgVisor bot f9711bc372 Rework the workaround of the XCR0 issue
XCR0 has to be synchronized with the host. We can call xsave from the host
context and then call xrstor from the guest context and vise versa. This means
we need to support the same set of FPU features in both contexts.

PiperOrigin-RevId: 378988281
2021-06-11 18:43:26 -07:00
Andrei VaginandgVisor bot ea0b513ef0 kvm: prefault a root table page before switching into a user address space
The root table physical page has to be mapped to not fault in iret or sysret
after switching into a user address space.  sysret and iret are in the upper
half that is global and so page tables of lower levels are already mapped.

Fixes #5742

PiperOrigin-RevId: 371458644
2021-04-30 23:07:15 -07:00
gVisor bot cd2fd60634 Merge pull request #5749 from zhlhahaha:2126
PiperOrigin-RevId: 369686298
2021-04-21 10:45:44 -07:00
gVisor bot 05b2377389 Merge pull request #5767 from avagin:mxcsr
PiperOrigin-RevId: 367730917
2021-04-09 17:15:06 -07:00
gVisor bot b80fb49342 Merge pull request #5736 from lubinszARM:pr_bblu_tlb_asid
PiperOrigin-RevId: 367523491
2021-04-08 15:53:37 -07:00
Andrei Vagin 89be1b308d platform/kvm/x86: restore mxcsr when switching from guest to sentry
Goruntime sets mxcsr once and never changes it.

Reported-by: [email protected]
Fixes: #5754
2021-04-01 13:28:15 -07:00
Howard Zhang ea386cc2e7 apply hostarch on pagetables_arm64_test.go
Signed-off-by: Howard Zhang <[email protected]>
2021-03-30 17:29:47 +08:00
Zach KoopmansandgVisor bot d8f34027b3 [syserror] Split usermem package
Split usermem package to help remove syserror dependency in go_marshal.
New hostarch package contains code not dependent on syserror.

PiperOrigin-RevId: 365651233
2021-03-29 13:30:21 -07:00
Robin Luk 24258df3c4 arm64 ring0: don't use inner-sharable to invalidate tlb
It is enough to invalidate the tlb of local vcpu in switch().
TLBI with inner-sharable will invalidate the tlb in other vcpu.

Arm64 hardware supports at least 256 pcid, so I think it's ok
to set the length of pcid pool to 128.

Signed-off-by: Robin Luk <[email protected]>
2021-03-26 16:10:21 +08:00
Howard Zhang 09a53be509 Fix nogo test error
Signed-off-by: Howard Zhang <[email protected]>
2021-03-25 17:39:28 +08:00
Andrei VaginandgVisor bot e5368292ab Move the code that manages floating-point state to a separate package
This change is inspired by Adin's cl/355256448.

PiperOrigin-RevId: 364695931
2021-03-23 18:46:37 -07:00
Robin Luk a38a22f1aa arm64 kvm:implement basic lazy save and restore for FPSIMD registers
Implement basic lazy save and restore for FPSIMD registers, which only
  restore FPSIMD state on el0_fpsimd_acc and save FPSIMD state in switch().

Signed-off-by: Robin Luk <[email protected]>
2021-02-03 11:50:36 +00:00
Robin Luk 5a5995f15f arm64: clean code
In order to improve the performance and stability, I reorg 2 modules slightly.
  arch: no red zone on Arm64.
  ring0: use stp instead of movd, and set RSV_REG_APP=R19.
Signed-off-by: Robin Luk <[email protected]>
2021-02-03 11:50:35 +00:00
Adin ScannellandgVisor bot 431d15921a Move ring0 package.
This allows the package to serve as a general purpose ring0 support package, as
opposed to being bound to specific sentry platforms.

Updates #5039

PiperOrigin-RevId: 355220044
2021-02-02 12:03:26 -08:00