From c1c085ca1598fd7b84f1244aa3b80afa511ff7d1 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Tue, 26 May 2020 18:50:49 +0000 Subject: [PATCH] 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. --- .../include/arch/x86_64/shim_types-arch.h | 133 ------------------ .../include/arch/x86_64/shim_ucontext-arch.h | 49 +++++++ LibOS/shim/include/shim_signal.h | 1 + LibOS/shim/src/bookkeep/shim_signal.c | 5 +- .../arch/x86_64/Linux-SGX/sigcontext.h | 28 ++++ Pal/include/arch/x86_64/Linux-SGX/ucontext.h | 28 ++++ Pal/include/arch/x86_64/Skeleton/sigcontext.h | 20 +++ Pal/include/arch/x86_64/Skeleton/ucontext.h | 28 ++++ 8 files changed, 157 insertions(+), 135 deletions(-) create mode 100644 LibOS/shim/include/arch/x86_64/shim_ucontext-arch.h create mode 100644 Pal/include/arch/x86_64/Linux-SGX/sigcontext.h create mode 100644 Pal/include/arch/x86_64/Linux-SGX/ucontext.h create mode 100644 Pal/include/arch/x86_64/Skeleton/sigcontext.h create mode 100644 Pal/include/arch/x86_64/Skeleton/ucontext.h diff --git a/LibOS/shim/include/arch/x86_64/shim_types-arch.h b/LibOS/shim/include/arch/x86_64/shim_types-arch.h index e79becfe..c2d00033 100644 --- a/LibOS/shim/include/arch/x86_64/shim_types-arch.h +++ b/LibOS/shim/include/arch/x86_64/shim_types-arch.h @@ -13,139 +13,6 @@ typedef struct { unsigned long __val[NUM_SIGS / (8 * sizeof(unsigned long))]; } __sigset_t; -/* sys/ucontext.h */ -/* Type for general register. */ -typedef long int greg_t; - -/* Number of general registers. */ -#define NGREG 23 - -/* Container for all general registers. */ -typedef greg_t gregset_t[NGREG]; - -/* Number of each register in the `gregset_t' array. */ -enum -{ - REG_R8 = 0, -# define REG_R8 REG_R8 - REG_R9, -# define REG_R9 REG_R9 - REG_R10, -# define REG_R10 REG_R10 - REG_R11, -# define REG_R11 REG_R11 - REG_R12, -# define REG_R12 REG_R12 - REG_R13, -# define REG_R13 REG_R13 - REG_R14, -# define REG_R14 REG_R14 - REG_R15, -# define REG_R15 REG_R15 - REG_RDI, -# define REG_RDI REG_RDI - REG_RSI, -# define REG_RSI REG_RSI - REG_RBP, -# define REG_RBP REG_RBP - REG_RBX, -# define REG_RBX REG_RBX - REG_RDX, -# define REG_RDX REG_RDX - REG_RAX, -# define REG_RAX REG_RAX - REG_RCX, -# define REG_RCX REG_RCX - REG_RSP, -# define REG_RSP REG_RSP - REG_RIP, -# define REG_RIP REG_RIP - REG_EFL, -# define REG_EFL REG_EFL - REG_CSGSFS, /* Actually short cs, gs, fs, __pad0. */ -# define REG_CSGSFS REG_CSGSFS - REG_ERR, -# define REG_ERR REG_ERR - REG_TRAPNO, -# define REG_TRAPNO REG_TRAPNO - REG_OLDMASK, -# define REG_OLDMASK REG_OLDMASK - REG_CR2 -# define REG_CR2 REG_CR2 -}; - -struct _libc_fpxreg { - unsigned short int significand[4]; - unsigned short int exponent; - unsigned short int padding[3]; -}; - -struct _libc_xmmreg { - __uint32_t element[4]; -}; - -struct _libc_fpstate { - /* 64-bit FXSAVE format. */ - __uint16_t cwd; - __uint16_t swd; - __uint16_t ftw; - __uint16_t fop; - __uint64_t rip; - __uint64_t rdp; - __uint32_t mxcsr; - __uint32_t mxcr_mask; - struct _libc_fpxreg st[8]; - struct _libc_xmmreg _xmm[16]; - __uint32_t padding[24]; -}; - -/* Structure to describe FPU registers. */ -typedef struct _libc_fpstate *fpregset_t; - -/* Context to describe whole processor state. */ -typedef struct { - gregset_t gregs; - /* Note that fpregs is a pointer. */ - fpregset_t fpregs; - unsigned long __reserved1 [8]; -} mcontext_t; - -/* Userlevel context. */ -typedef struct ucontext { - unsigned long int uc_flags; - struct ucontext* uc_link; - stack_t uc_stack; - mcontext_t uc_mcontext; - __sigset_t uc_sigmask; - struct _libc_fpstate __fpregs_mem; -} ucontext_t; - -static inline void shim_regs_to_ucontext(ucontext_t* context, struct shim_regs* regs) { - context->uc_mcontext.gregs[REG_R8] = regs->r8; - context->uc_mcontext.gregs[REG_R9] = regs->r9; - context->uc_mcontext.gregs[REG_R10] = regs->r10; - context->uc_mcontext.gregs[REG_R11] = regs->r11; - context->uc_mcontext.gregs[REG_R12] = regs->r12; - context->uc_mcontext.gregs[REG_R13] = regs->r13; - context->uc_mcontext.gregs[REG_R14] = regs->r14; - context->uc_mcontext.gregs[REG_R15] = regs->r15; - context->uc_mcontext.gregs[REG_RDI] = regs->rdi; - context->uc_mcontext.gregs[REG_RSI] = regs->rsi; - context->uc_mcontext.gregs[REG_RBP] = regs->rbp; - context->uc_mcontext.gregs[REG_RBX] = regs->rbx; - context->uc_mcontext.gregs[REG_RDX] = regs->rdx; - context->uc_mcontext.gregs[REG_RAX] = regs->orig_rax; - context->uc_mcontext.gregs[REG_RCX] = regs->rcx; - context->uc_mcontext.gregs[REG_RSP] = regs->rsp; - context->uc_mcontext.gregs[REG_RIP] = regs->rip; - context->uc_mcontext.gregs[REG_EFL] = regs->rflags; - context->uc_mcontext.gregs[REG_CSGSFS] = 0; - context->uc_mcontext.gregs[REG_ERR] = 0; - context->uc_mcontext.gregs[REG_TRAPNO] = 0; - context->uc_mcontext.gregs[REG_OLDMASK] = 0; - context->uc_mcontext.gregs[REG_CR2] = 0; -} - #define RED_ZONE_SIZE 128 #endif /* _SHIM_TYPES_ARCH_H_ */ diff --git a/LibOS/shim/include/arch/x86_64/shim_ucontext-arch.h b/LibOS/shim/include/arch/x86_64/shim_ucontext-arch.h new file mode 100644 index 00000000..49e54bb5 --- /dev/null +++ b/LibOS/shim/include/arch/x86_64/shim_ucontext-arch.h @@ -0,0 +1,49 @@ +/* + This file is part of Graphene Library OS. + + Graphene Library OS is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + Graphene Library OS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#ifndef _SHIM_UCONTEXT_ARCH_H_ +#define _SHIM_UCONTEXT_ARCH_H_ + +#include +#include + +static inline void shim_regs_to_ucontext(ucontext_t* context, struct shim_regs* regs) { + context->uc_mcontext.gregs[REG_R8] = regs->r8; + context->uc_mcontext.gregs[REG_R9] = regs->r9; + context->uc_mcontext.gregs[REG_R10] = regs->r10; + context->uc_mcontext.gregs[REG_R11] = regs->r11; + context->uc_mcontext.gregs[REG_R12] = regs->r12; + context->uc_mcontext.gregs[REG_R13] = regs->r13; + context->uc_mcontext.gregs[REG_R14] = regs->r14; + context->uc_mcontext.gregs[REG_R15] = regs->r15; + context->uc_mcontext.gregs[REG_RDI] = regs->rdi; + context->uc_mcontext.gregs[REG_RSI] = regs->rsi; + context->uc_mcontext.gregs[REG_RBP] = regs->rbp; + context->uc_mcontext.gregs[REG_RBX] = regs->rbx; + context->uc_mcontext.gregs[REG_RDX] = regs->rdx; + context->uc_mcontext.gregs[REG_RAX] = regs->orig_rax; + context->uc_mcontext.gregs[REG_RCX] = regs->rcx; + context->uc_mcontext.gregs[REG_RSP] = regs->rsp; + context->uc_mcontext.gregs[REG_RIP] = regs->rip; + context->uc_mcontext.gregs[REG_EFL] = regs->rflags; + context->uc_mcontext.gregs[REG_CSGSFS] = 0; + context->uc_mcontext.gregs[REG_ERR] = 0; + context->uc_mcontext.gregs[REG_TRAPNO] = 0; + context->uc_mcontext.gregs[REG_OLDMASK] = 0; + context->uc_mcontext.gregs[REG_CR2] = 0; +} + +#endif /* _SHIM_UCONTEXT_ARCH_H_ */ diff --git a/LibOS/shim/include/shim_signal.h b/LibOS/shim/include/shim_signal.h index df81046a..f58f0cb9 100644 --- a/LibOS/shim/include/shim_signal.h +++ b/LibOS/shim/include/shim_signal.h @@ -3,6 +3,7 @@ #include #include +#include void sigaction_make_defaults(struct __kernel_sigaction* sig_action); diff --git a/LibOS/shim/src/bookkeep/shim_signal.c b/LibOS/shim/src/bookkeep/shim_signal.c index 73de1515..dfa15d2e 100644 --- a/LibOS/shim/src/bookkeep/shim_signal.c +++ b/LibOS/shim/src/bookkeep/shim_signal.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -186,7 +187,7 @@ void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context, } if (pal_context) { - memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT)); + pal_context_to_ucontext(context, pal_context); signal->context_stored = true; } } @@ -738,7 +739,7 @@ __handle_one_signal(shim_tcb_t* tcb, int sig, struct shim_signal* signal) { tcb->context = *context; if (signal->pal_context) - memcpy(signal->pal_context, signal->context.uc_mcontext.gregs, sizeof(PAL_CONTEXT)); + ucontext_to_pal_context(signal->pal_context, &signal->context); } void __handle_signal (shim_tcb_t * tcb, int sig) diff --git a/Pal/include/arch/x86_64/Linux-SGX/sigcontext.h b/Pal/include/arch/x86_64/Linux-SGX/sigcontext.h new file mode 100644 index 00000000..e7c571e3 --- /dev/null +++ b/Pal/include/arch/x86_64/Linux-SGX/sigcontext.h @@ -0,0 +1,28 @@ +/* + This file is part of Graphene Library OS. + + Graphene Library OS is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + Graphene Library OS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +/* + * sigcontext.h + * + * Linux-SGX PAL uses Linux's sigcontext.h + */ + +#ifndef _LINUX_SGX_SIGCONTEXT_H +#define _LINUX_SGX_SIGCONTEXT_H + +#include + +#endif /* _LINUX_SGX_SIGCONTEXT_H */ diff --git a/Pal/include/arch/x86_64/Linux-SGX/ucontext.h b/Pal/include/arch/x86_64/Linux-SGX/ucontext.h new file mode 100644 index 00000000..d592292d --- /dev/null +++ b/Pal/include/arch/x86_64/Linux-SGX/ucontext.h @@ -0,0 +1,28 @@ +/* + This file is part of Graphene Library OS. + + Graphene Library OS is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + Graphene Library OS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +/* + * ucontext.h + * + * Linux-SGX PAL uses Linux's ucontext.h + */ + +#ifndef _LINUX_SGX_UCONTEXT_H +#define _LINUX_SGX_UCONTEXT_H + +#include + +#endif /* _LINUX_SGX_UCONTEXT_H */ diff --git a/Pal/include/arch/x86_64/Skeleton/sigcontext.h b/Pal/include/arch/x86_64/Skeleton/sigcontext.h new file mode 100644 index 00000000..0ad897c3 --- /dev/null +++ b/Pal/include/arch/x86_64/Skeleton/sigcontext.h @@ -0,0 +1,20 @@ +/* + This file is part of Graphene Library OS. + + Graphene Library OS is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + Graphene Library OS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#ifndef _SKELETON_SIGCONTEXT_H +#define _SKELETON_SIGCONTEXT_H + +#endif /* _SKELETON_SIGCONTEXT_H */ diff --git a/Pal/include/arch/x86_64/Skeleton/ucontext.h b/Pal/include/arch/x86_64/Skeleton/ucontext.h new file mode 100644 index 00000000..101bd0a2 --- /dev/null +++ b/Pal/include/arch/x86_64/Skeleton/ucontext.h @@ -0,0 +1,28 @@ +/* + This file is part of Graphene Library OS. + + Graphene Library OS is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + Graphene Library OS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +/* + * ucontext.h + * + * Skeleton uses Linux's ucontext.h (for now) + */ + +#ifndef _SKELETON_UCONTEXT_H +#define _SKELETON_UCONTEXT_H + +#include + +#endif /* _SKELETON_UCONTEXT_H */