summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/kernel/rseq.go
blob: 1f3de58e374e17358b87eeaeadb02ef0e875cf07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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 kernel

import (
	"gvisor.googlesource.com/gvisor/pkg/sentry/hostcpu"
	"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
	"gvisor.googlesource.com/gvisor/pkg/syserror"
)

// Restartable sequences, as described in https://lwn.net/Articles/650333/.

// RSEQCriticalRegion describes a restartable sequence critical region.
//
// +stateify savable
type RSEQCriticalRegion struct {
	// When a task in this thread group has its CPU preempted (as defined by
	// platform.ErrContextCPUPreempted) or has a signal delivered to an
	// application handler while its instruction pointer is in CriticalSection,
	// set the instruction pointer to Restart and application register r10 (on
	// amd64) to the former instruction pointer.
	CriticalSection usermem.AddrRange
	Restart         usermem.Addr
}

// RSEQAvailable returns true if t supports restartable sequences.
func (t *Task) RSEQAvailable() bool {
	return t.k.useHostCores && t.k.Platform.DetectsCPUPreemption()
}

// RSEQCriticalRegion returns a copy of t's thread group's current restartable
// sequence.
func (t *Task) RSEQCriticalRegion() RSEQCriticalRegion {
	return *t.tg.rscr.Load().(*RSEQCriticalRegion)
}

// SetRSEQCriticalRegion replaces t's thread group's restartable sequence.
//
// Preconditions: t.RSEQAvailable() == true.
func (t *Task) SetRSEQCriticalRegion(rscr RSEQCriticalRegion) error {
	// These checks are somewhat more lenient than in Linux, which (bizarrely)
	// requires rscr.CriticalSection to be non-empty and rscr.Restart to be
	// outside of rscr.CriticalSection, even if rscr.CriticalSection.Start == 0
	// (which disables the critical region).
	if rscr.CriticalSection.Start == 0 {
		rscr.CriticalSection.End = 0
		rscr.Restart = 0
		t.tg.rscr.Store(&rscr)
		return nil
	}
	if rscr.CriticalSection.Start >= rscr.CriticalSection.End {
		return syserror.EINVAL
	}
	if rscr.CriticalSection.Contains(rscr.Restart) {
		return syserror.EINVAL
	}
	// TODO: check that rscr.CriticalSection and rscr.Restart are in
	// the application address range, for consistency with Linux
	t.tg.rscr.Store(&rscr)
	return nil
}

// RSEQCPUAddr returns the address that RSEQ will keep updated with t's CPU
// number.
//
// Preconditions: The caller must be running on the task goroutine.
func (t *Task) RSEQCPUAddr() usermem.Addr {
	return t.rseqCPUAddr
}

// SetRSEQCPUAddr replaces the address that RSEQ will keep updated with t's CPU
// number.
//
// Preconditions: t.RSEQAvailable() == true. The caller must be running on the
// task goroutine. t's AddressSpace must be active.
func (t *Task) SetRSEQCPUAddr(addr usermem.Addr) error {
	t.rseqCPUAddr = addr
	if addr != 0 {
		if err := t.rseqCopyOutCPU(); err != nil {
			t.rseqCPUAddr = 0
			t.rseqCPU = -1
			return syserror.EINVAL // yes, EINVAL, not err or EFAULT
		}
	} else {
		t.rseqCPU = -1
	}
	return nil
}

// Preconditions: The caller must be running on the task goroutine. t's
// AddressSpace must be active.
func (t *Task) rseqCopyOutCPU() error {
	t.rseqCPU = int32(hostcpu.GetCPU())
	buf := t.CopyScratchBuffer(4)
	usermem.ByteOrder.PutUint32(buf, uint32(t.rseqCPU))
	_, err := t.CopyOutBytes(t.rseqCPUAddr, buf)
	return err
}

// Preconditions: The caller must be running on the task goroutine.
func (t *Task) rseqInterrupt() {
	rscr := t.tg.rscr.Load().(*RSEQCriticalRegion)
	if ip := t.Arch().IP(); rscr.CriticalSection.Contains(usermem.Addr(ip)) {
		t.Debugf("Interrupted RSEQ critical section at %#x; restarting at %#x", ip, rscr.Restart)
		t.Arch().SetIP(uintptr(rscr.Restart))
		t.Arch().SetRSEQInterruptedIP(ip)
	}
}