From 821459c94264bfa3272b6e3946b5dc23aacae625 Mon Sep 17 00:00:00 2001 From: Konstantin Bogomolov Date: Tue, 8 Aug 2023 12:31:31 -0700 Subject: [PATCH] systrap: Enable using xsaveopt. PiperOrigin-RevId: 554906814 --- pkg/sentry/platform/systrap/shared_context.go | 4 ---- .../platform/systrap/sysmsg/syshandler_amd64.S | 16 +++++----------- .../platform/systrap/sysmsg/sysmsg_amd64.go | 9 ++++++--- .../systrap/sysmsg/sysmsg_offsets_amd64.h | 1 - 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/pkg/sentry/platform/systrap/shared_context.go b/pkg/sentry/platform/systrap/shared_context.go index 8c7b419b9..8cc97db96 100644 --- a/pkg/sentry/platform/systrap/shared_context.go +++ b/pkg/sentry/platform/systrap/shared_context.go @@ -172,16 +172,12 @@ func (sc *sharedContext) threadID() uint32 { // EnableSentryFastPath indicates that the polling mode is enabled for the // Sentry. It has to be called before putting the context into the context queue. -// This function is used if contextDecouplingExp=true because the fastpath -// is negotiated in ThreadContext. func (sc *sharedContext) enableSentryFastPath() { atomic.StoreUint32(&sc.shared.SentryFastPath, 1) } // DisableSentryFastPath indicates that the polling mode for the sentry is // disabled for the Sentry. -// This function is used if contextDecouplingExp=true because the fastpath -// is negotiated in ThreadContext. func (sc *sharedContext) disableSentryFastPath() { atomic.StoreUint32(&sc.shared.SentryFastPath, 0) } diff --git a/pkg/sentry/platform/systrap/sysmsg/syshandler_amd64.S b/pkg/sentry/platform/systrap/sysmsg/syshandler_amd64.S index 8b260f7ed..86855bd91 100644 --- a/pkg/sentry/platform/systrap/sysmsg/syshandler_amd64.S +++ b/pkg/sentry/platform/systrap/sysmsg/syshandler_amd64.S @@ -125,17 +125,11 @@ restored_gs: movl $0xffffffff, %eax movl $0xffffffff, %edx movl __export_arch_state+offsetof_arch_state_xsave_mode(%rip), %esi - // TODO(b/268366549): Fix use of xsavec/xsaveopt. - // cmpl $XSAVE_MODE_XSAVEC, %esi - // jl use_xsaveopt - // xsavec (%rdi) - // jmp fpu_saved - // use_xsaveopt: - // cmpl $XSAVE_MODE_XSAVEOPT, %esi - // jl use_xsave - // xsaveopt (%rdi) - // jmp fpu_saved - // use_xsave: + cmpl $XSAVE_MODE_XSAVEOPT, %esi + jl use_xsave + xsaveopt (%rdi) + jmp fpu_saved +use_xsave: cmpl $XSAVE_MODE_XSAVE, %esi jl use_fxsave xsave (%rdi) diff --git a/pkg/sentry/platform/systrap/sysmsg/sysmsg_amd64.go b/pkg/sentry/platform/systrap/sysmsg/sysmsg_amd64.go index 87ad74ed0..8d14eeeda 100644 --- a/pkg/sentry/platform/systrap/sysmsg/sysmsg_amd64.go +++ b/pkg/sentry/platform/systrap/sysmsg/sysmsg_amd64.go @@ -38,11 +38,13 @@ type ArchState struct { fsgsbase uint32 } +// The linux kernel does not allow using xsavec from userspace, so we are limited +// to xsaveopt. +// See arch/x86/kernel/fpu/xstate.c:validate_user_xstate_header for details. const ( fxsave = iota xsave xsaveopt - xsavec ) // Init initializes the arch specific state. @@ -51,8 +53,9 @@ func (s *ArchState) Init() { fpLenUint, _ := fs.ExtendedStateSize() s.fpLen = uint32(fpLenUint) - // TODO(b/268366549): Fix use of xsavec/xsaveopt. - if fs.UseXsave() { + if fs.UseXsaveopt() { + s.xsaveMode = xsaveopt + } else if fs.UseXsave() { s.xsaveMode = xsave } else { s.xsaveMode = fxsave diff --git a/pkg/sentry/platform/systrap/sysmsg/sysmsg_offsets_amd64.h b/pkg/sentry/platform/systrap/sysmsg/sysmsg_offsets_amd64.h index e01e633aa..57ba70ce2 100644 --- a/pkg/sentry/platform/systrap/sysmsg/sysmsg_offsets_amd64.h +++ b/pkg/sentry/platform/systrap/sysmsg/sysmsg_offsets_amd64.h @@ -24,7 +24,6 @@ #define XSAVE_MODE_FXSAVE (0x0) #define XSAVE_MODE_XSAVE (0x1) #define XSAVE_MODE_XSAVEOPT (0x2) -#define XSAVE_MODE_XSAVEC (0x3) // LINT.ThenChange(sysmsg.h, sysmsg_amd64.go) // LINT.IfChange