diff options
author | Googler <noreply@google.com> | 2018-04-27 10:37:02 -0700 |
---|---|---|
committer | Adin Scannell <ascannell@google.com> | 2018-04-28 01:44:26 -0400 |
commit | d02b74a5dcfed4bfc8f2f8e545bca4d2afabb296 (patch) | |
tree | 54f95eef73aee6bacbfc736fffc631be2605ed53 /pkg/sentry/hostcpu | |
parent | f70210e742919f40aa2f0934a22f1c9ba6dada62 (diff) |
Check in gVisor.
PiperOrigin-RevId: 194583126
Change-Id: Ica1d8821a90f74e7e745962d71801c598c652463
Diffstat (limited to 'pkg/sentry/hostcpu')
-rw-r--r-- | pkg/sentry/hostcpu/BUILD | 20 | ||||
-rw-r--r-- | pkg/sentry/hostcpu/getcpu_amd64.s | 24 | ||||
-rw-r--r-- | pkg/sentry/hostcpu/hostcpu.go | 67 | ||||
-rw-r--r-- | pkg/sentry/hostcpu/hostcpu_test.go | 52 |
4 files changed, 163 insertions, 0 deletions
diff --git a/pkg/sentry/hostcpu/BUILD b/pkg/sentry/hostcpu/BUILD new file mode 100644 index 000000000..9457618d8 --- /dev/null +++ b/pkg/sentry/hostcpu/BUILD @@ -0,0 +1,20 @@ +package(licenses = ["notice"]) # Apache 2.0 + +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "hostcpu", + srcs = [ + "getcpu_amd64.s", + "hostcpu.go", + ], + importpath = "gvisor.googlesource.com/gvisor/pkg/sentry/hostcpu", + visibility = ["//:sandbox"], +) + +go_test( + name = "hostcpu_test", + size = "small", + srcs = ["hostcpu_test.go"], + embed = [":hostcpu"], +) diff --git a/pkg/sentry/hostcpu/getcpu_amd64.s b/pkg/sentry/hostcpu/getcpu_amd64.s new file mode 100644 index 000000000..7f6247d81 --- /dev/null +++ b/pkg/sentry/hostcpu/getcpu_amd64.s @@ -0,0 +1,24 @@ +// Copyright 2018 Google Inc. +// +// 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..fa46499ad --- /dev/null +++ b/pkg/sentry/hostcpu/hostcpu.go @@ -0,0 +1,67 @@ +// Copyright 2018 Google Inc. +// +// 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_test.go b/pkg/sentry/hostcpu/hostcpu_test.go new file mode 100644 index 000000000..a82e1a271 --- /dev/null +++ b/pkg/sentry/hostcpu/hostcpu_test.go @@ -0,0 +1,52 @@ +// Copyright 2018 Google Inc. +// +// 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 + +import ( + "fmt" + "testing" +) + +func TestMaxValueInLinuxBitmap(t *testing.T) { + for _, test := range []struct { + str string + max uint64 + }{ + {"0", 0}, + {"0\n", 0}, + {"0,2", 2}, + {"0-63", 63}, + {"0-3,8-11", 11}, + } { + t.Run(fmt.Sprintf("%q", test.str), func(t *testing.T) { + max, err := maxValueInLinuxBitmap(test.str) + if err != nil || max != test.max { + t.Errorf("maxValueInLinuxBitmap: got (%d, %v), wanted (%d, nil)", max, err, test.max) + } + }) + } +} + +func TestMaxValueInLinuxBitmapErrors(t *testing.T) { + for _, str := range []string{"", "\n"} { + t.Run(fmt.Sprintf("%q", str), func(t *testing.T) { + max, err := maxValueInLinuxBitmap(str) + if err == nil { + t.Errorf("maxValueInLinuxBitmap: got (%d, nil), wanted (_, error)", max) + } + t.Log(err) + }) + } +} |