Ver código fonte

[LibOS] shim_thread.h/shim_tls.h: use static inline instead of macro

Macros are used unnecessarily. Use static inline functions instead.

Signed-off-by: Isaku Yamahata <isaku.yamahata@gmail.com>
Isaku Yamahata 5 anos atrás
pai
commit
9c398764ce

+ 9 - 9
LibOS/shim/include/shim_internal.h

@@ -43,8 +43,8 @@
 #include <shim_tls.h>
 
 /* important macros */
-#define get_cur_tid()           (SHIM_GET_TLS()->tid)
-#define PAL_NATIVE_ERRNO        (SHIM_GET_TLS()->pal_errno)
+#define get_cur_tid()           (shim_get_tls()->tid)
+#define PAL_NATIVE_ERRNO        (shim_get_tls()->pal_errno)
 
 #define INTERNAL_TID_BASE       ((IDTYPE) 1 << (sizeof(IDTYPE) * 8 - 1))
 #define IS_INTERNAL_TID(tid)    ((tid) >= INTERNAL_TID_BASE)
@@ -178,7 +178,7 @@ long convert_pal_errno (long err);
 #define SHIM_ARG_TYPE long
 
 #ifdef PROFILE
-# define ENTER_TIME     SHIM_GET_TLS()->context.enter_time
+# define ENTER_TIME     shim_get_tls()->context.enter_time
 # define BEGIN_SYSCALL_PROFILE()        \
     do { ENTER_TIME = GET_PROFILE_INTERVAL(); } while (0)
 # define END_SYSCALL_PROFILE(name)      \
@@ -196,7 +196,7 @@ long convert_pal_errno (long err);
 void check_stack_hook (void);
 
 static inline uint64_t get_cur_preempt (void) {
-    shim_tcb_t* tcb = SHIM_GET_TLS();
+    shim_tcb_t* tcb = shim_get_tls();
     assert(tcb);
     return tcb->context.preempt;
 }
@@ -434,7 +434,7 @@ static inline void __disable_preempt (shim_tcb_t * tcb)
 
 static inline void disable_preempt (shim_tcb_t * tcb)
 {
-    if (!tcb && !(tcb = SHIM_GET_TLS()))
+    if (!tcb && !(tcb = shim_get_tls()))
         return;
 
     __disable_preempt(tcb);
@@ -453,7 +453,7 @@ void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc);
 
 static inline void enable_preempt (shim_tcb_t * tcb)
 {
-    if (!tcb && !(tcb = SHIM_GET_TLS()))
+    if (!tcb && !(tcb = shim_get_tls()))
         return;
 
     if (!(tcb->context.preempt & ~SIGNAL_DELAYED))
@@ -498,7 +498,7 @@ static inline void __lock (LOCKTYPE * l)
     if (!lock_enabled || !l->lock)
         return;
 
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     disable_preempt(tcb);
 
 #if DEBUG_LOCK == 1
@@ -524,7 +524,7 @@ static inline void __unlock (LOCKTYPE * l)
     if (!lock_enabled || !l->lock)
         return;
 
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
 
 #if DEBUG_LOCK == 1
     debug("unlock(%s=%p) %s:%d\n", name, l, file, line);
@@ -540,7 +540,7 @@ static inline bool __locked (LOCKTYPE * l)
     if (!lock_enabled || !l->lock)
         return false;
 
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     return tcb->tid == l->owner;
 }
 

+ 15 - 11
LibOS/shim/include/shim_thread.h

@@ -121,16 +121,20 @@ struct shim_simple_thread {
 
 int init_thread (void);
 
-#define SHIM_THREAD_SELF()                                     \
-    ({ struct shim_thread * __self;                            \
-        asm ("movq %%fs:%c1,%q0" : "=r" (__self)               \
-           : "i" (offsetof(__libc_tcb_t, shim_tcb.tp)));       \
-      __self; })
+static inline struct shim_thread * shim_thread_self(void)
+{
+    struct shim_thread * __self;
+    asm ("movq %%fs:%c1,%q0" : "=r" (__self)
+         : "i" (offsetof(__libc_tcb_t, shim_tcb.tp)));
+    return __self;
+}
 
-#define SAVE_SHIM_THREAD_SELF(__self)                         \
-  ({ asm ("movq %q0,%%fs:%c1" : : "r" (__self),               \
-          "i" (offsetof(__libc_tcb_t, shim_tcb.tp)));         \
-     __self; })
+static inline struct shim_thread * save_shim_thread_self(struct shim_thread * __self)
+{
+     asm ("movq %q0,%%fs:%c1" : : "r" (__self),
+          "i" (offsetof(__libc_tcb_t, shim_tcb.tp)));
+     return __self;
+}
 
 void get_thread (struct shim_thread * thread);
 void put_thread (struct shim_thread * thread);
@@ -159,7 +163,7 @@ static inline
 __attribute__((always_inline))
 struct shim_thread * get_cur_thread (void)
 {
-    return SHIM_THREAD_SELF();
+    return shim_thread_self();
 }
 
 static inline
@@ -174,7 +178,7 @@ static inline
 __attribute__((always_inline))
 void set_cur_thread (struct shim_thread * thread)
 {
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     IDTYPE tid = 0;
 
     if (thread) {

+ 23 - 17
LibOS/shim/include/shim_tls.h

@@ -91,23 +91,29 @@ typedef struct
 
 #include <stddef.h>
 
-#define SHIM_TLS_CHECK_CANARY()                                \
-    ({ uint64_t __canary;                                      \
-        asm ("movq %%fs:%c1,%q0" : "=r" (__canary)             \
-           : "i" (offsetof(__libc_tcb_t, shim_tcb.canary)));   \
-      __canary == SHIM_TLS_CANARY; })
-
-#define SHIM_GET_TLS()                                         \
-    ({ shim_tcb_t *__self;                                     \
-        asm ("movq %%fs:%c1,%q0" : "=r" (__self)               \
-           : "i" (offsetof(__libc_tcb_t, shim_tcb.self)));     \
-      __self; })
-
-#define GET_LIBC_TCB()                                         \
-    ({ void *__self;                                           \
-        asm ("movq %%fs:%c1,%q0" : "=r" (__self)               \
-           : "i" (offsetof(__libc_tcb_t, tcb)));               \
-      __self; })
+static inline bool shim_tls_check_canary(void)
+{
+    uint64_t __canary;
+    asm ("movq %%fs:%c1,%q0" : "=r" (__canary)
+         : "i" (offsetof(__libc_tcb_t, shim_tcb.canary)));
+    return __canary == SHIM_TLS_CANARY;
+}
+
+static inline shim_tcb_t * shim_get_tls(void)
+{
+    shim_tcb_t *__self;
+    asm ("movq %%fs:%c1,%q0" : "=r" (__self)
+         : "i" (offsetof(__libc_tcb_t, shim_tcb.self)));
+    return __self;
+}
+
+static inline __libc_tcb_t * shim_libc_tcb(void)
+{
+    __libc_tcb_t *__self;
+    asm ("movq %%fs:%c1,%q0" : "=r" (__self)
+         : "i" (offsetof(__libc_tcb_t, tcb)));
+    return __self;
+}
 
 #endif /* IN_SHIM */
 

+ 6 - 6
LibOS/shim/src/bookkeep/shim_signal.c

@@ -145,7 +145,7 @@ void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
 
 void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
 {
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     assert(tcb);
 
     // Signals should not be delivered before the user process starts
@@ -240,7 +240,7 @@ static void arithmetic_error_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * c
 
 static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
 {
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     assert(tcb);
 
     if (tcb->test_range.cont_addr && arg
@@ -314,7 +314,7 @@ bool test_user_memory (void * addr, size_t size, bool write)
     if (!size)
         return false;
 
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     assert(tcb && tcb->tp);
     __disable_preempt(tcb);
 
@@ -358,7 +358,7 @@ ret_fault:
  */
 bool test_user_string (const char * addr)
 {
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     assert(tcb && tcb->tp);
     __disable_preempt(tcb);
 
@@ -432,7 +432,7 @@ static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
 
 static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
 {
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     if (!tcb || !tcb->tp)
         return;
 
@@ -588,7 +588,7 @@ void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
 
 void handle_signal (bool delayed_only)
 {
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     assert(tcb);
 
     struct shim_thread * thread = (struct shim_thread *) tcb->tp;

+ 3 - 3
LibOS/shim/src/bookkeep/shim_thread.c

@@ -101,12 +101,12 @@ struct shim_thread * lookup_thread (IDTYPE tid)
 
 struct shim_thread * __get_cur_thread (void)
 {
-    return SHIM_THREAD_SELF();
+    return shim_thread_self();
 }
 
 shim_tcb_t * __get_cur_tcb (void)
 {
-    return SHIM_GET_TLS();
+    return shim_get_tls();
 }
 
 IDTYPE get_pid (void)
@@ -753,7 +753,7 @@ BEGIN_RS_FUNC(running_thread)
         if (libc_tcb) {
             shim_tcb_t * tcb = &libc_tcb->shim_tcb;
             assert(tcb->context.sp);
-            tcb->debug_buf = SHIM_GET_TLS()->debug_buf;
+            tcb->debug_buf = shim_get_tls()->debug_buf;
             allocate_tls(libc_tcb, thread->user_tcb, thread);
             /* Temporarily disable preemption until the thread resumes. */
             __disable_preempt(tcb);

+ 1 - 1
LibOS/shim/src/elf/shim_rtld.c

@@ -1579,7 +1579,7 @@ int execute_elf_object (struct shim_handle * exec, int argc, const char ** argp,
     ElfW(Addr) entry = interp_map ? interp_map->l_entry : exec_map->l_entry;
 
     /* Ready to start execution, re-enable preemption. */
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     __enable_preempt(tcb);
 
 #if defined(__x86_64__)

+ 1 - 1
LibOS/shim/src/shim_checkpoint.c

@@ -1269,7 +1269,7 @@ void restore_context (struct shim_context * context)
     *(void **) (context->sp - 128 - 8) = context->ret_ip;
 
     /* Ready to resume execution, re-enable preemption. */
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     __enable_preempt(tcb);
 
     memset(context, 0, sizeof(struct shim_context));

+ 6 - 6
LibOS/shim/src/shim_init.c

@@ -53,7 +53,7 @@ const unsigned int glibc_version = GLIBC_VERSION;
 
 static void handle_failure (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
 {
-    SHIM_GET_TLS()->pal_errno = (arg <= PAL_ERROR_BOUND) ? arg : 0;
+    shim_get_tls()->pal_errno = (arg <= PAL_ERROR_BOUND) ? arg : 0;
 }
 
 void __abort(void) {
@@ -216,7 +216,7 @@ void allocate_tls (void * tcb_location, bool user, struct shim_thread * thread)
     }
 
     DkSegmentRegister(PAL_SEGMENT_FS, tcb);
-    assert(SHIM_TLS_CHECK_CANARY());
+    assert(shim_tls_check_canary());
 }
 
 void populate_tls (void * tcb_location, bool user)
@@ -224,7 +224,7 @@ void populate_tls (void * tcb_location, bool user)
     __libc_tcb_t * tcb = (__libc_tcb_t *) tcb_location;
     assert(tcb);
     tcb->tcb = tcb;
-    copy_tcb(&tcb->shim_tcb, SHIM_GET_TLS());
+    copy_tcb(&tcb->shim_tcb, shim_get_tls());
 
     struct shim_thread * thread = (struct shim_thread *) tcb->shim_tcb.tp;
     if (thread) {
@@ -233,7 +233,7 @@ void populate_tls (void * tcb_location, bool user)
     }
 
     DkSegmentRegister(PAL_SEGMENT_FS, tcb);
-    assert(SHIM_TLS_CHECK_CANARY());
+    assert(shim_tls_check_canary());
 }
 
 DEFINE_PROFILE_OCCURENCE(alloc_stack, memory);
@@ -805,7 +805,7 @@ restore:
     if (thread_start_event)
         DkEventSet(thread_start_event);
 
-    shim_tcb_t * cur_tcb = SHIM_GET_TLS();
+    shim_tcb_t * cur_tcb = shim_get_tls();
     struct shim_thread * cur_thread = (struct shim_thread *) cur_tcb->tp;
 
     if (cur_tcb->context.sp)
@@ -1128,7 +1128,7 @@ int shim_clean (void)
 
 #ifdef PROFILE
     if (ENTER_TIME) {
-        switch (SHIM_GET_TLS()->context.syscall_nr) {
+        switch (shim_get_tls()->context.syscall_nr) {
             case __NR_exit_group:
                 SAVE_PROFILE_INTERVAL_SINCE(syscall_exit_group, ENTER_TIME);
                 break;

+ 1 - 1
LibOS/shim/src/sys/shim_migrate.c

@@ -273,7 +273,7 @@ int shim_do_checkpoint (const char * filename)
     if (ret < 0)
         return ret;
 
-    shim_tcb_t * tcb = SHIM_GET_TLS();
+    shim_tcb_t * tcb = shim_get_tls();
     assert(tcb && tcb->tp);
     struct shim_signal signal;
     __store_context(tcb, NULL, &signal);

+ 3 - 3
LibOS/shim/src/utils/printf.c

@@ -78,7 +78,7 @@ debug_fputch (void * f, int ch, void * b)
 void debug_puts (const char * str)
 {
     int len = strlen(str);
-    struct debug_buf * buf = SHIM_GET_TLS()->debug_buf;
+    struct debug_buf * buf = shim_get_tls()->debug_buf;
 
     while (len) {
         int rem = DEBUGBUF_SIZE - 4 - buf->end;
@@ -109,12 +109,12 @@ void debug_puts (const char * str)
 
 void debug_putch (int ch)
 {
-    debug_fputch(NULL, ch, SHIM_GET_TLS()->debug_buf);
+    debug_fputch(NULL, ch, shim_get_tls()->debug_buf);
 }
 
 void debug_vprintf (const char * fmt, va_list * ap)
 {
-    vfprintfmt((void *) debug_fputch, NULL, SHIM_GET_TLS()->debug_buf,
+    vfprintfmt((void *) debug_fputch, NULL, shim_get_tls()->debug_buf,
                fmt, ap);
 }