Commit Graph
15 Commits
Author SHA1 Message Date
Konstantin BogomolovandgVisor bot 88b84624d6 Clean up context decoupling experiment.
This change removes code branches and variables only used in coupled-context
mode.

PiperOrigin-RevId: 529776383
2023-05-05 11:55:50 -07:00
Andrei VaginandgVisor bot 4ca8b7193c systrap: simplify interrupt handling in syshandler
syshandler can be interrupted by SIGCHLD. Here are two separate cases. The first
one is when the interrupt are addressed to a context that has triggered
syshanlder. In this case, the interrupt can be ignored, because the context
is switching to the sentry. Another case is when syshandler is resuming a new
context. It means we need to stop resuming it and return the context back to
the sentry. The good thing is that the context state is up to date, and so
sighandler can do its job ignoring a state from a signal frame.

Reported-by: [email protected]
PiperOrigin-RevId: 520986791
2023-03-31 12:34:16 -07:00
Andrei VaginandgVisor bot 90fc717d61 systrap: kick another stub thread after the handshake timeout
The idea of the handshake timeout is to see if here is any free active thread.

In addition, here are a few fixes that prevent waking up too many threads.

PiperOrigin-RevId: 519871285
2023-03-27 17:21:27 -07:00
Konstantin BogomolovandgVisor bot 319e56bd7b Add debug logging to systrap futex waits.
In general it is probably a good idea to set a timeout on any futex waits that
the sentry is doing. For now just output some helpful logs about what the shared
memory looks like; in the future we may want to do something more useful on
ETIMEDOUT events.

PiperOrigin-RevId: 518919966
2023-03-23 11:44:22 -07:00
Andrei VaginandgVisor bot 0380ad1018 systrap: save context pointer on sysmsg
We don't need to calculate an address from context_id each time.

PiperOrigin-RevId: 518640998
2023-03-22 12:26:03 -07:00
Andrei VaginandgVisor bot 3c4e1f9427 Remove sysmsg->interrupted_context_id
ctx->interrupt can be used to find out where the current context has to
be interrupted or not.

PiperOrigin-RevId: 518597531
2023-03-22 09:58:00 -07:00
Andrei VaginandgVisor bot 52960317d2 systrap: introduce a spinning queue
The spinning queue is a queue of spinning threads. It solves the
fragmentation problem. The idea is to minimize the number of threads
processing requests. We can't control how system threads are scheduled, so
can't distribute requests efficiently. The spinning queue emulates virtual
threads sorted by their spinning time.

PiperOrigin-RevId: 518470754
2023-03-21 22:10:15 -07:00
Konstantin BogomolovandgVisor bot e1e10515a6 Refactor context-related shared memory usage.
This change introduces an abstraction for most accesses to shared thread-context
memory. In general, there are very few cases where accessing this memory is not
supposed to be atomic, so it makes sense to abstract these accesses into
getters/setters that perform the actions atomically. After this change, we
should treat most direct accesses through sharedContext.shared as suspect.

Additionally this cleanup allows the new sharedContext instance to become the
context interruptor. When doing this it is no longer required to use locks, as
was done in context.NotifyInterrupt.

PiperOrigin-RevId: 517166307
2023-03-16 10:50:15 -07:00
Konstantin BogomolovandgVisor bot a43769006c Implement systrap context queue.
This is the initial implementation of the systrap context queue via a ringbuffer
in shared memory between stub threads and the sentry.

In this new model there is no longer a bound sysmsg thread for every context;
instead each subprocess starts with one initial sysmsg thread, which starts
polling the context queue for new contexts arriving from the sentry. If the
sentry detects that contexts are spending too much time in the context queue
without being processed, it will create new sysmsg threads or wake sleeping
ones. Tangentially, sysmsg threads will go to sleep if they spend too much time
busy looping without new context arrivals.

This model does not yet take into account the full load of the host system or
even multiple subprocesses in the same sandbox. Multiple overloaded subprocesses
are liable to make each other run slower by kicking sysmsg threads more often
than they need to; this will be remedied in follow up CLs.

PiperOrigin-RevId: 516680504
2023-03-14 17:48:13 -07:00
Konstantin BogomolovandgVisor bot 31cf4055e0 Handle context interrupts based on syshandler state.
Also add interrupt handling for context decoupling.

Previously syshandler interrupts would be retriggered no matter if the interrupt
arrived before the switch to sentry or after. We only need to handle the case of
it arriving after.

Additionally this CL introduces interrupt handling for the decoupled context
mode, by making interrupts target task contexts rather than sysmsg threads.

PiperOrigin-RevId: 515065101
2023-03-08 09:58:12 -08:00
Konstantin BogomolovandgVisor bot 9176978c83 Implement saving decoupled context from syshandler.
Rewrite the syshandler assembly routine to save the full state of user threads,
like the sighandler would. With fpstate, it does so by writing straight to the
thread context struct, so there is no need to do an intermediate copy.

PiperOrigin-RevId: 514751894
2023-03-07 09:16:13 -08:00
Konstantin BogomolovandgVisor bot 64dfbe5b0c Implement saving decoupled context from sighandler.
Saves task context state to the separate context memory region which is mapped
to all subprocess sysmsg threads, instead of always saving the context to the
thread-specific sysmsg.

When context decoupling is disabled fpstate is not saved to this region, but
GP registers and signal info are.

PiperOrigin-RevId: 514432596
2023-03-06 09:24:24 -08:00
Konstantin BogomolovandgVisor bot a85a6931e6 Map shared region for systrap thread contexts.
Introduces what a ThreadContext struct is in the context of systrap. It
makes the mappings of the region where the contexts will be stored into both the
sentry and the address space of stub processes.

PiperOrigin-RevId: 513913793
2023-03-04 00:42:24 -08:00
Adin ScannellandAndrei Vagin f278d0e433 Add appropriate arch_genrule transitions. 2023-02-28 12:28:17 -08:00
Andrei VaginandgVisor bot a12c3a3f23 Open-sourcing the systrap platform.
The systrap platform like the ptrace platform uses stub processes to manage
the user address space. The difference is how they intercept system calls and
other events like memory faults, exceptions, etc.

In case of systrap, all events that have to be handled by the Sentry trigger
signals that are handled by a custom signal handler installed on stub
processes. The signal handler switches control to the Sentry.

Here are a few other optimizations:
* On x86, system calls can be replaced with a function call to remove overhead
  of signals.
* For fast interactions of sentry and stub processes, futex wait/wake can
  be a bottle neck, so we use a polling mode.

The platform is launched for the purpose of testing and gathering initial
feedback. It is not yet ready for use in production.

PiperOrigin-RevId: 511650064
2023-02-22 18:22:49 -08:00