gvisor/pkg/sentry/kernel
Andrei Vagin f347a578b7 Move platform.File in memmap
The subsequent systrap changes will need to import memmap from
the platform package.

PiperOrigin-RevId: 323409486
2020-07-27 11:59:10 -07:00
..
auth Implement mount(2) and umount2(2) for VFS2. 2020-06-05 19:12:03 -07:00
contexttest
epoll Avoid an allocation in epoll 2020-06-25 14:18:33 -07:00
eventfd Update package locations. 2020-01-27 15:31:32 -08:00
fasync
futex Implement get/set_robust_list. 2020-07-23 17:42:50 -07:00
g3doc
memevent
pipe Port fallocate to VFS2. 2020-07-01 13:14:44 -07:00
sched Standardize on tools directory. 2020-01-27 12:21:00 -08:00
semaphore
shm Move platform.File in memmap 2020-07-27 11:59:10 -07:00
signalfd Update package locations. 2020-01-27 15:31:32 -08:00
time Add AfterFunc to tcpip.Clock 2020-07-23 18:00:43 -07:00
BUILD Add task work mechanism. 2020-07-23 16:25:34 -07:00
README.md Format documentation 2018-07-12 10:37:21 -07:00
abstract_socket_namespace.go
aio.go
context.go
fd_table.go
fd_table_test.go Add vfs.FileDescription to FD table 2020-01-28 15:31:03 -08:00
fd_table_unsafe.go
fs_context.go
ipc_namespace.go
kernel.go Add AfterFunc to tcpip.Clock 2020-07-23 18:00:43 -07:00
kernel_opts.go Add notes to relevant tests. 2020-02-05 22:46:35 -08:00
kernel_state.go Update canonical repository. 2019-06-13 16:50:15 -07:00
pending_signals.go Update canonical repository. 2019-06-13 16:50:15 -07:00
pending_signals_state.go
posixtimer.go Disable cpuClockTicker when app is idle 2019-10-01 12:21:01 -07:00
ptrace.go
ptrace_amd64.go
ptrace_arm64.go
rseq.go Implement automated marshalling for slices of Marshallable types. 2020-03-31 22:56:09 -07:00
seccomp.go
sessions.go
signal.go
signal_handlers.go
syscalls.go
syscalls_state.go Fix all copy locks violations. 2020-04-08 10:00:14 -07:00
syslog.go
table_test.go Update canonical repository. 2019-06-13 16:50:15 -07:00
task.go
task_acct.go
task_block.go
task_clone.go Fix oom_score_adj. 2020-03-13 13:19:13 -07:00
task_context.go Remove obsolete TODOs for b/38173783 2020-04-13 11:02:14 -07:00
task_exec.go
task_exit.go
task_futex.go Implement get/set_robust_list. 2020-07-23 17:42:50 -07:00
task_identity.go
task_log.go
task_net.go
task_run.go Add task work mechanism. 2020-07-23 16:25:34 -07:00
task_sched.go Enable automated marshalling for mempolicy syscalls. 2020-04-23 18:20:21 -07:00
task_signals.go Enable automated marshalling for signals and the arch package. 2020-04-25 23:56:04 -07:00
task_start.go
task_stop.go
task_syscall.go Minor formatting updates for gvisor.dev. 2020-05-15 20:05:18 -07:00
task_test.go
task_usermem.go Add option to skip stuck tasks waiting for address space 2020-02-25 13:44:18 -08:00
task_work.go Add task work mechanism. 2020-07-23 16:25:34 -07:00
thread_group.go
threads.go Port aio to VFS2. 2020-06-16 08:49:06 -07:00
timekeeper.go Move platform.File in memmap 2020-07-27 11:59:10 -07:00
timekeeper_state.go Update canonical repository. 2019-06-13 16:50:15 -07:00
timekeeper_test.go Update package locations. 2020-01-27 15:31:32 -08:00
tty.go Fix "unlock of unlocked mutex" crash when getting tty 2020-01-15 13:00:59 +08:00
uncaught_signal.proto Change copyright notice to "The gVisor Authors" 2019-04-29 14:26:23 -07:00
uts_namespace.go
vdso.go Move platform.File in memmap 2020-07-27 11:59:10 -07:00
version.go

README.md

This package contains:

  • A (partial) emulation of the "core Linux kernel", which governs task execution and scheduling, system call dispatch, and signal handling. See below for details.

  • The top-level interface for the sentry's Linux kernel emulation in general, used by the main function of all versions of the sentry. This interface revolves around the Env type (defined in kernel.go).

Background

In Linux, each schedulable context is referred to interchangeably as a "task" or "thread". Tasks can be divided into userspace and kernel tasks. In the sentry, scheduling is managed by the Go runtime, so each schedulable context is a goroutine; only "userspace" (application) contexts are referred to as tasks, and represented by Task objects. (From this point forward, "task" refers to the sentry's notion of a task unless otherwise specified.)

At a high level, Linux application threads can be thought of as repeating a "run loop":

  • Some amount of application code is executed in userspace.

  • A trap (explicit syscall invocation, hardware interrupt or exception, etc.) causes control flow to switch to the kernel.

  • Some amount of kernel code is executed in kernelspace, e.g. to handle the cause of the trap.

  • The kernel "returns from the trap" into application code.

Analogously, each task in the sentry is associated with a task goroutine that executes that task's run loop (Task.run in task_run.go). However, the sentry's task run loop differs in structure in order to support saving execution state to, and resuming execution from, checkpoints.

While in kernelspace, a Linux thread can be descheduled (cease execution) in a variety of ways:

  • It can yield or be preempted, becoming temporarily descheduled but still runnable. At present, the sentry delegates scheduling of runnable threads to the Go runtime.

  • It can exit, becoming permanently descheduled. The sentry's equivalent is returning from Task.run, terminating the task goroutine.

  • It can enter interruptible sleep, a state in which it can be woken by a caller-defined wakeup or the receipt of a signal. In the sentry, interruptible sleep (which is ambiguously referred to as blocking) is implemented by making all events that can end blocking (including signal notifications) communicated via Go channels and using select to multiplex wakeup sources; see task_block.go.

  • It can enter uninterruptible sleep, a state in which it can only be woken by a caller-defined wakeup. Killable sleep is a closely related variant in which the task can also be woken by SIGKILL. (These definitions also include Linux's "group-stopped" (TASK_STOPPED) and "ptrace-stopped" (TASK_TRACED) states.)

To maximize compatibility with Linux, sentry checkpointing appears as a spurious signal-delivery interrupt on all tasks; interrupted system calls return EINTR or are automatically restarted as usual. However, these semantics require that uninterruptible and killable sleeps do not appear to be interrupted. In other words, the state of the task, including its progress through the interrupted operation, must be preserved by checkpointing. For many such sleeps, the wakeup condition is application-controlled, making it infeasible to wait for the sleep to end before checkpointing. Instead, we must support checkpointing progress through sleeping operations.

Implementation

We break the task's control flow graph into states, delimited by:

  1. Points where uninterruptible and killable sleeps may occur. For example, there exists a state boundary between signal dequeueing and signal delivery because there may be an intervening ptrace signal-delivery-stop.

  2. Points where sleep-induced branches may "rejoin" normal execution. For example, the syscall exit state exists because it can be reached immediately following a synchronous syscall, or after a task that is sleeping in execve() or vfork() resumes execution.

  3. Points containing large branches. This is strictly for organizational purposes. For example, the state that processes interrupt-signaled conditions is kept separate from the main "app" state to reduce the size of the latter.

  4. SyscallReinvoke, which does not correspond to anything in Linux, and exists solely to serve the autosave feature.

dot -Tpng -Goverlap=false -orun_states.png run_states.dot

States before which a stop may occur are represented as implementations of the taskRunState interface named run(state), allowing them to be saved and restored. States that cannot be immediately preceded by a stop are simply Task methods named do(state).

Conditions that can require task goroutines to cease execution for unknown lengths of time are called stops. Stops are divided into internal stops, which are stops whose start and end conditions are implemented within the sentry, and external stops, which are stops whose start and end conditions are not known to the sentry. Hence all uninterruptible and killable sleeps are internal stops, and the existence of a pending checkpoint operation is an external stop. Internal stops are reified into instances of the TaskStop type, while external stops are merely counted. The task run loop alternates between checking for stops and advancing the task's state. This allows checkpointing to hold tasks in a stopped state while waiting for all tasks in the system to stop.