Changeset 1a5eca4 in mainline for uspace


Ignore:
Timestamp:
2016-04-27T19:36:56Z (9 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
af9dd1e
Parents:
d6f9fff
Message:

amd64: Make TLS settable from uspace

The TLS document[1] mandates that %fs[0] is the thread pointer on amd64.
That is good as it allows userspace-only TLS management for fibrils:
fibril_save/restore() simply manipulate the thread pointer in %fs:0 and
don't need to ask the kernel to modify %fs's base. The kernel treats
%fs:0 as another preserved register and preserves it across context
switches. GCC gets in the way a little bit because it by default assumes
that TLS is accessible from negative %fs offsets (which would
necessitate a kernel-assisted solution). Fortunately, there is a GCC
option to suppress this assumption.

  • Introduce the concept of virtual registers, with VREG_TP (thread pointer) being the first of them
  • Preserve VREG_TP in context_save/restore()
  • Stop using sys_tls_set() in favour of using %fs:0 as the thread pointer
  • Make GCC generate code that always goes through %gs:0 to access TLS
  • Introduce kseg: a per-CPU area accessible from GS_KERNEL that holds the kernel stack, kernel FS base and a scratch space for syscall and int handlers to use

[1] Drepper, U.: ELF Handling For Thread-Local Storage

Location:
uspace/lib/c/arch/amd64
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/arch/amd64/Makefile.common

    rd6f9fff r1a5eca4  
    2727#
    2828
    29 GCC_CFLAGS += -fno-omit-frame-pointer
     29GCC_CFLAGS += -mno-tls-direct-seg-refs -fno-omit-frame-pointer
    3030CLANG_CFLAGS += -fno-omit-frame-pointer
    3131
  • uspace/lib/c/arch/amd64/include/libarch/tls.h

    rd6f9fff r1a5eca4  
    4747static inline void __tcb_set(tcb_t *tcb)
    4848{
    49         __SYSCALL1(SYS_TLS_SET, (sysarg_t) tcb);
     49        asm volatile ("movq %0, %%fs:0" :: "r" (tcb));
    5050}
    5151
    52 static inline tcb_t * __tcb_get(void)
     52static inline tcb_t *__tcb_get(void)
    5353{
    54         void * retval;
     54        void *retval;
    5555
    56         asm ("movq %%fs:0, %0" : "=r"(retval));
     56        asm volatile ("movq %%fs:0, %0" : "=r" (retval));
    5757        return retval;
    5858}
  • uspace/lib/c/arch/amd64/src/fibril.S

    rd6f9fff r1a5eca4  
    5151        movq %r15, CONTEXT_OFFSET_R15(%rdi)
    5252       
    53         # save TLS
    5453        movq %fs:0, %rax
    5554        movq %rax, CONTEXT_OFFSET_TLS(%rdi)
     
    7978        movq %rdx,(%rsp)
    8079       
    81         # Set thread local storage
    82         movq CONTEXT_OFFSET_TLS(%rdi), %rdi  # Set arg1 to TLS addr
    83         movl $1, %eax                        # SYS_TLS_SET
    84         syscall
     80        movq CONTEXT_OFFSET_TLS(%rdi), %rdi
     81        movq %rdi, %fs:0
    8582       
    8683        xorl %eax, %eax                      # context_restore returns 0
Note: See TracChangeset for help on using the changeset viewer.