32 Commits
Author SHA1 Message Date
Paweł Marczewski 2cfdd510dc [LibOS] Remove dynamic linking
After this change, LibOS will no longer perform dynamic linking.
The ELF loading code executes load commands and passes control to
interpreter (ld.so), which handles necessary relocations and
loading additional libraries. In this way, the code resembles what
Linux kernel does when executing a new program.

Before, dynamic linking was necessary for making LibOS entry point
(syscalldb function) available for applications. However, that meant
duplicating the work already done by ld.so, and introduced a lot of
unnecessary complexity. After changing LibOS entry API to use the
GS register, it's possible to omit dynamic linking entirely.

The main function (__load_elf_object()) still needs cleanup and
possibly rewriting from scratch. However, this change prepares
ground for that rewrite.

Summary of changes:

- Remove dynamic relocation step (DO_DYNAMIC_RELOCATE()).
- Don't call __load_elf_object() again for ELFs reported
  via register_library(). We only need to notify GDB about these.
- Remove fields related to dynamic linking from link_map (dynamic
  section address, hashes, etc.), and setup for these fields.
- need_interp(): To check if we need an interpreter, check only if
  the binary requests one (PT_INTERP), instead of traversing the
  dynamic section (before, we ignored dynamic dependencies on LibOS
  itself, but now there shouldn't be any).
- RELOCATE(): always adjust addresses, instead of checking if
  they're already inside the mapped range. I think the previous
  behaviour was a workaround to make repeated relocations work.
- Get rid of load modes that are no longer used (OBJECT_REMAP,
  OBJECT_USER).
- Remove the workaround for repeated relocation in glibc patches
  (R_X86_64_NONE).

Signed-off-by: Paweł Marczewski <pawel@invisiblethingslab.com>
2021-02-18 02:48:02 +01:00
Paweł Marczewski 6ea8e951f0 [LibOS] Use GS register for syscalls
Instead of depending on dynamic linking for LibOS entry point
(syscalldb), we pass a pointer in the shim_tcb structure, so that
the patched code can enter syscall using 'jmp *%gs:<offset>'.

The same applies to the vDSO syscall code that previously needed
an up-to-date pointer to syscalldb function. Now, there is no
need to adjust the values inside the vDSO page.

In addition, this change removes the other two instances where we
import a symbol directly from LibOS: register_library (can be also
done through GS register) and glibc_version (not important because
we build Graphene and glibc together).

This simplifies things because the dynamic linking necessary to
make the syscalldb function available had to be performed by LibOS
itself (in many cases, effectively doing a second pass of dynamic
linking after ld.so). After this change, there will be no need for
LibOS to perform dynamic linking, and the ELF loading code can be
simplified.

Signed-off-by: Paweł Marczewski <pawel@invisiblethingslab.com>
2021-02-15 22:19:28 +01:00
borysp c24bddd5aa [LibOS] Rework signal handling and syscall emulation
Change log (most important only):
- unify CPU context structures - now we have only one version -
  `PAL_CONTEXT` - which is shared between LibOS and PALs and it should
  depend only on the host architecture (not OS),
- syscalls emulation changed:
  - dedicated LibOS stack is now used for syscalls emulation,
  - removed one indirection level in syscalls table - now it stores
    `shim_do_*` functions directly,
- signal handling - completely rewritten:
  - all signal queues use proper locking schemes now,
  - signals are handled *only* when returning to the user app from LibOS
    or PAL,
  - nested signals are now possible,
  - the app is allowed to jump out of signal handler with the same
    sematics as on normal Linux,
  - signal altstack is now fully supported,
  - syscall restarting is now supported,
  - doing a backtrace from the signal handler works properly,
- disallow injecting host-level signals, with one exception, see
  `sys.enable_sigterm_injection` manifest option for more details.
2021-02-05 14:11:21 +01:00
borysp 287762501b Add infinite loop behind each call to exit syscall
This commit additionally replaces all `while (true) {}` inf loops with
`die_or_inf_loop` which either crashes the process or loops infinitely
and is not an undefined behavior like the original one (C disallows inf
loops without side effects).
2021-01-15 16:35:03 +01:00
Stefan Berger 8c1fc5168e [LibOS] Rename fs_base to tls_base to be more generic 2020-12-25 00:18:20 +01:00
Stefan Berger b4205008ff [LibOS] Move some x86-specific context registers into own struct 2020-12-23 21:30:23 +01:00
borysp 989dac6fc8 [Pal] Fix semantics of DkSegmentRegister
On x64 DkSegmentRegister had weird semantics which also disabled some
usages like `0` as fsbase.
2020-12-17 16:18:26 +01:00
Stefan Berger a01b8743f8 [LibOS] Move CRASH_PROCESS from BUG() into arch-specific header 2020-11-23 22:14:49 +01:00
borysp 3014a0dd1c Miscelaneous inline asm fixes
Mostly adds missing "memory" clobber which caused some nasty bugs - gcc
optimized those inline asms and assumed values returned by them never
change.
2020-11-05 13:48:41 +01:00
Dmitrii Kuvaiskii 3b02bdc80f [LibOS] Add XSAVE area handling and save/restore it on child thread creation
Previously, LibOS (shim) layer of Graphene didn't support XSAVE area.
The XSAVE area stores FP, XMM, YMM, ZMM, etc. registers and control
states and is handled via FXSAVE/XSAVE and FXRSTOR/XRSTOR x86-64
instructions. This commit is the first step towards adding full-
fledged XSAVE support to LibOS. It adds XSAVE related structs and
functions to LibOS code, and propagates XSAVE regs/states from
parent to child on thread creation via clone() (though not really
correctly). New test `fp_multithread` is added to LibOS regression.

Future commits will add correct XSAVE handling on syscall transitions
and arriving signals.
2020-10-29 07:56:31 -07:00
Michał Kowalczyk e4d228d4b9 [LibOS+Pal] Clean up file comments
I don't know any reason why would stating the file name we're in be
helpful for anything. Moreover, this information was incorrect in a few
cases (copy-paste bugs, probably).

Additionally, a few minor type/formatting fixes included.
2020-10-14 20:01:13 +02:00
Juro Bystricky fbb9a5b581 [LibOS] Use correct v5.8 x86 syscall numbers
When compiling against v5.8 (or later) kernel headers, we encounter
compilation errors such as:

  In file included from /usr/include/asm/unistd.h:13,
                   from shim_parser.c:11:
  shim_parser.c:408:10: error: array index in initializer exceeds array bounds
           [__NR_io_uring_setup]    = {.slow = 0, .parser = {NULL}},

The remedy is to extend `syscall_parser_table` by modyfing the
definition of LIBOS_SYSCALL_BOUND so the offending system calls fit in
the table.

While at it, also fix the actual values for syscall numbers in
"shim_syscalls.h" (the numbers are not sequential, there is an
intentional gap between the numers).
2020-10-13 03:18:51 +02:00
Michał Kowalczyk 6b6bdb28b2 [LibOS] Update x64 syscall list to Linux v5.8 2020-09-25 09:29:52 +02:00
Michał Kowalczyk ad477ec7bf Reformat repository to our C formatting rules (final iteration) 2020-09-15 02:00:54 +02:00
Michał Kowalczyk cb7d430655 Remove always_inline where not needed 2020-07-05 17:24:06 +02:00
Dmitrii Kuvaiskii a4304b365f [LibOS] Refactor stack initialization on new process and on execve 2020-06-25 17:47:56 +00:00
Stefan Berger 940e5dc1ec [LibOS] Implement arch-specific tls_to_fs_base() 2020-06-25 14:46:33 +02:00
Stefan Berger 29e2f796f2 [LibOS] Implement arch-specific shim_regs_get_ip() 2020-06-25 14:46:33 +02:00
Stefan Berger df6cd09d0d [LibOS] Define vDSO filename as arch-specific constant 2020-06-24 20:52:41 +00:00
Stefan Berger ba0303a232 [LibOS] Move arch-specific part out of update_fs_base 2020-06-22 20:43:28 +02:00
Stefan Berger 2540c63d21 [LibOS] Move inline assembly to macro in shim_internal-arch.h 2020-06-17 17:58:52 +02:00
Michał Kowalczyk 0f55c6de04 Use SPDX IDs for licenses in source files 2020-06-13 03:25:33 +02:00
borysp 50ca6c4a22 [LibOS] Deliver signal to only one thread
Previously Graphene delivered each signal to all threads instead of
one. This was additionally abused to quit the application. Now each
signal is delivered to only one arbitrary thread. This commit
additionally cleans the signal and exiting code.
2020-06-08 18:21:11 +02:00
Stefan Berger d6cbc621be [LibOS] Move x86_64-specific SHIM_TCB_GET/SET into shim_tcb-arch.h 2020-06-05 02:08:29 +02:00
Stefan Berger dec9456421 [LibOS] Move x86_64-specific macro out of shim_internal.h 2020-06-05 02:08:29 +02:00
Stefan Berger c1c085ca15 Remove duplicate ucontext from shim_types-arch.h
Use the ucontext from PAL instead. LibOS now has access to the inline
functions for copying PAL_CONTEXT to ucontext and vice versa and we use
them where possible.

We need to introduce a ucontext.h for Skeleton. It does need ucontext
to be defined for being able to compile shim_signal.c. The easiest way
to achieve this is to rely on Linux's ucontext.h.

SGX can reuse Linux's ucontext.h and sigcontext.h.
2020-05-28 21:08:49 +02:00
Stefan Berger 3b1691c805 [LibOS] Move x86_64 shim_regs copying to shim_regs_to_ucontext
Implement shim_regs_to_ucontext to copy shim_regs to ucontext.
Leave a FIXME in the inline function pointing out missing registers
in shim_regs and the order of the registers in shim_regs.
2020-05-28 12:38:41 +02:00
Stefan Berger c9cd84dfa9 [LibOS] Add getters and setters for shim context's syscall number 2020-05-28 12:10:05 +02:00
Stefan Berger 20fe60dc46 [LibOS] Add getters and setters for shim context's stack pointer 2020-05-28 12:10:05 +02:00
Stefan Berger 55aa0d2e58 [LibOS] Split off x86_64 specifics from shim_types.h
Split off x86_64 specifics from shim_types.h and move them
into include/arch/x86_64/shim_types-arch.h.
2020-05-17 13:28:36 -07:00
Stefan Berger 0fb766c1b9 [LibOS] Split off x86_64 specifics from shim_tcb.h
Split off x86_64 specifics from shim_tcb.h and move them into
include/arch/x86_64/shim_tcb-arch.h.
2020-05-17 13:28:36 -07:00
Stefan Berger 4a53659df1 [LibOS] Move dl-machine-x86_64.h to arch-specific dir
Move the shim's dl-machine-x86_64.h to include/arch/x86_64/shim_dl-machine.h.
Rename the file to avoid name clashes with Pal's dl-machine.h.
2020-05-13 14:53:09 +02:00