From cedff8d3aef3bc2055b1a7c3ad47a4c8297367ea Mon Sep 17 00:00:00 2001 From: Haibo Xu Date: Wed, 30 Jan 2019 11:48:02 -0800 Subject: [PATCH] Add muldiv/rd_tsc support for arm64 platform. Signed-off-by: Haibo Xu Change-Id: If35459be78e023346a140184401172f8e023c7f9 PiperOrigin-RevId: 231638020 --- pkg/sentry/time/BUILD | 5 ++- pkg/sentry/time/LICENSE | 27 +++++++++++++ pkg/sentry/time/arith_arm64.go | 70 ++++++++++++++++++++++++++++++++++ pkg/sentry/time/muldiv_arm64.s | 44 +++++++++++++++++++++ pkg/sentry/time/tsc_arm64.s | 22 +++++++++++ 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 pkg/sentry/time/LICENSE create mode 100644 pkg/sentry/time/arith_arm64.go create mode 100644 pkg/sentry/time/muldiv_arm64.s create mode 100644 pkg/sentry/time/tsc_arm64.s diff --git a/pkg/sentry/time/BUILD b/pkg/sentry/time/BUILD index 5dadb8a2d..1191010e6 100644 --- a/pkg/sentry/time/BUILD +++ b/pkg/sentry/time/BUILD @@ -1,6 +1,6 @@ load("//tools/go_stateify:defs.bzl", "go_library", "go_test") -package(licenses = ["notice"]) # Apache 2.0 +package(licenses = ["notice"]) # Apache 2.0, portions BSD load("//tools/go_generics:defs.bzl", "go_template_instance") @@ -18,15 +18,18 @@ go_template_instance( go_library( name = "time", srcs = [ + "arith_arm64.go", "calibrated_clock.go", "clock_id.go", "clocks.go", "muldiv_amd64.s", + "muldiv_arm64.s", "parameters.go", "sampler.go", "sampler_unsafe.go", "seqatomic_parameters.go", "tsc_amd64.s", + "tsc_arm64.s", ], importpath = "gvisor.googlesource.com/gvisor/pkg/sentry/time", visibility = ["//pkg/sentry:internal"], diff --git a/pkg/sentry/time/LICENSE b/pkg/sentry/time/LICENSE new file mode 100644 index 000000000..6a66aea5e --- /dev/null +++ b/pkg/sentry/time/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/pkg/sentry/time/arith_arm64.go b/pkg/sentry/time/arith_arm64.go new file mode 100644 index 000000000..b94740c2a --- /dev/null +++ b/pkg/sentry/time/arith_arm64.go @@ -0,0 +1,70 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file provides a generic Go implementation of uint128 divided by uint64. + +// The code is derived from Go's generic math/big.divWW_g +// (src/math/big/arith.go), but is only used on ARM64. + +package time + +import "math/bits" + +type word uint + +const ( + _W = bits.UintSize // word size in bits + _W2 = _W / 2 // half word size in bits + _B2 = 1 << _W2 // half digit base + _M2 = _B2 - 1 // half digit mask +) + +// nlz returns the number of leading zeros in x. +// Wraps bits.LeadingZeros call for convenience. +func nlz(x word) uint { + return uint(bits.LeadingZeros(uint(x))) +} + +// q = (u1<<_W + u0 - r)/y +// Adapted from Warren, Hacker's Delight, p. 152. +func divWW(u1, u0, v word) (q, r word) { + if u1 >= v { + return 1<<_W - 1, 1<<_W - 1 + } + + s := nlz(v) + v <<= s + + vn1 := v >> _W2 + vn0 := v & _M2 + un32 := u1<>(_W-s) + un10 := u0 << s + un1 := un10 >> _W2 + un0 := un10 & _M2 + q1 := un32 / vn1 + rhat := un32 - q1*vn1 + + for q1 >= _B2 || q1*vn0 > _B2*rhat+un1 { + q1-- + rhat += vn1 + + if rhat >= _B2 { + break + } + } + + un21 := un32*_B2 + un1 - q1*v + q0 := un21 / vn1 + rhat = un21 - q0*vn1 + + for q0 >= _B2 || q0*vn0 > _B2*rhat+un0 { + q0-- + rhat += vn1 + if rhat >= _B2 { + break + } + } + + return q1*_B2 + q0, (un21*_B2 + un0 - q0*v) >> s +} diff --git a/pkg/sentry/time/muldiv_arm64.s b/pkg/sentry/time/muldiv_arm64.s new file mode 100644 index 000000000..5fa82a136 --- /dev/null +++ b/pkg/sentry/time/muldiv_arm64.s @@ -0,0 +1,44 @@ +// Copyright 2018 Google LLC +// +// 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. + +#include "textflag.h" + +// Documentation is available in parameters.go. +// +// func muldiv64(value, multiplier, divisor uint64) (uint64, bool) +TEXT ·muldiv64(SB),NOSPLIT,$40-33 + MOVD value+0(FP), R0 + MOVD multiplier+8(FP), R1 + MOVD divisor+16(FP), R2 + + UMULH R0, R1, R3 + MUL R0, R1, R4 + + CMP R2, R3 + BHS overflow + + MOVD R3, 8(RSP) + MOVD R4, 16(RSP) + MOVD R2, 24(RSP) + CALL ·divWW(SB) + MOVD 32(RSP), R0 + MOVD R0, result+24(FP) + MOVD $1, R0 + MOVB R0, ok+32(FP) + RET + +overflow: + MOVD ZR, result+24(FP) + MOVB ZR, ok+32(FP) + RET diff --git a/pkg/sentry/time/tsc_arm64.s b/pkg/sentry/time/tsc_arm64.s new file mode 100644 index 000000000..c1c9760ef --- /dev/null +++ b/pkg/sentry/time/tsc_arm64.s @@ -0,0 +1,22 @@ +// Copyright 2018 Google LLC +// +// 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. + +#include "textflag.h" + +TEXT ·Rdtsc(SB),NOSPLIT,$0-8 + // Get the virtual counter. + ISB $15 + WORD $0xd53be040 //MRS CNTVCT_EL0, R0 + MOVD R0, ret+0(FP) + RET