summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/hostcpu
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2019-06-02 06:44:55 +0000
committergVisor bot <gvisor-bot@google.com>2019-06-02 06:44:55 +0000
commitceb0d792f328d1fc0692197d8856a43c3936a571 (patch)
tree83155f302eff44a78bcc30a3a08f4efe59a79379 /pkg/sentry/hostcpu
parentdeb7ecf1e46862d54f4b102f2d163cfbcfc37f3b (diff)
parent216da0b733dbed9aad9b2ab92ac75bcb906fd7ee (diff)
Merge 216da0b7 (automated)
Diffstat (limited to 'pkg/sentry/hostcpu')
-rw-r--r--pkg/sentry/hostcpu/getcpu_amd64.s24
-rw-r--r--pkg/sentry/hostcpu/hostcpu.go67
-rwxr-xr-xpkg/sentry/hostcpu/hostcpu_state_autogen.go4
3 files changed, 95 insertions, 0 deletions
diff --git a/pkg/sentry/hostcpu/getcpu_amd64.s b/pkg/sentry/hostcpu/getcpu_amd64.s
new file mode 100644
index 000000000..aa00316da
--- /dev/null
+++ b/pkg/sentry/hostcpu/getcpu_amd64.s
@@ -0,0 +1,24 @@
+// Copyright 2018 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.
+
+#include "textflag.h"
+
+// func GetCPU() (cpu uint32)
+TEXT ·GetCPU(SB), NOSPLIT, $0-4
+ BYTE $0x0f; BYTE $0x01; BYTE $0xf9; // RDTSCP
+ // On Linux, the bottom 12 bits of IA32_TSC_AUX are CPU and the upper 20
+ // are node. See arch/x86/entry/vdso/vma.c:vgetcpu_cpu_init().
+ ANDL $0xfff, CX
+ MOVL CX, cpu+0(FP)
+ RET
diff --git a/pkg/sentry/hostcpu/hostcpu.go b/pkg/sentry/hostcpu/hostcpu.go
new file mode 100644
index 000000000..d78f78402
--- /dev/null
+++ b/pkg/sentry/hostcpu/hostcpu.go
@@ -0,0 +1,67 @@
+// Copyright 2018 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.
+
+// Package hostcpu provides utilities for working with CPU information provided
+// by a host Linux kernel.
+package hostcpu
+
+import (
+ "fmt"
+ "io/ioutil"
+ "strconv"
+ "strings"
+ "unicode"
+)
+
+// GetCPU returns the caller's current CPU number, without using the Linux VDSO
+// (which is not available to the sentry) or the getcpu(2) system call (which
+// is relatively slow).
+func GetCPU() uint32
+
+// MaxPossibleCPU returns the highest possible CPU number, which is guaranteed
+// not to change for the lifetime of the host kernel.
+func MaxPossibleCPU() (uint32, error) {
+ const path = "/sys/devices/system/cpu/possible"
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ return 0, err
+ }
+ str := string(data)
+ // Linux: drivers/base/cpu.c:show_cpus_attr() =>
+ // include/linux/cpumask.h:cpumask_print_to_pagebuf() =>
+ // lib/bitmap.c:bitmap_print_to_pagebuf()
+ i, err := maxValueInLinuxBitmap(str)
+ if err != nil {
+ return 0, fmt.Errorf("invalid %s (%q): %v", path, str, err)
+ }
+ return uint32(i), nil
+}
+
+// maxValueInLinuxBitmap returns the maximum value specified in str, which is a
+// string emitted by Linux's lib/bitmap.c:bitmap_print_to_pagebuf(list=true).
+func maxValueInLinuxBitmap(str string) (uint64, error) {
+ str = strings.TrimSpace(str)
+ // Find the last decimal number in str.
+ idx := strings.LastIndexFunc(str, func(c rune) bool {
+ return !unicode.IsDigit(c)
+ })
+ if idx != -1 {
+ str = str[idx+1:]
+ }
+ i, err := strconv.ParseUint(str, 10, 64)
+ if err != nil {
+ return 0, err
+ }
+ return i, nil
+}
diff --git a/pkg/sentry/hostcpu/hostcpu_state_autogen.go b/pkg/sentry/hostcpu/hostcpu_state_autogen.go
new file mode 100755
index 000000000..f04a56ec0
--- /dev/null
+++ b/pkg/sentry/hostcpu/hostcpu_state_autogen.go
@@ -0,0 +1,4 @@
+// automatically generated by stateify.
+
+package hostcpu
+