Ignore:
Timestamp:
2016-04-27T19:36:56Z (8 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/arch/amd64/src/proc/scheduler.c

    rd6f9fff r1a5eca4  
    4242#include <arch/pm.h>
    4343#include <arch/ddi/ddi.h>
     44#include <arch/kseg_struct.h>
    4445
    4546/** Perform amd64 specific tasks needed before the new task is run.
     
    5556void before_thread_runs_arch(void)
    5657{
    57         CPU->arch.tss->rsp0 =
    58             (uintptr_t) &THREAD->kstack[STACK_SIZE];
    59        
    60         /*
    61          * Syscall support.
    62          */
    63         swapgs();
    64         write_msr(AMD_MSR_GS, (uintptr_t) THREAD->arch.syscall_rsp);
    65         swapgs();
    66        
    67         /* TLS support - set FS to thread local storage */
    68         write_msr(AMD_MSR_FS, THREAD->arch.tls);
     58        CPU->arch.tss->rsp0 = (uintptr_t) &THREAD->kstack[STACK_SIZE];
     59
     60        kseg_t *kseg = (kseg_t *) read_msr(AMD_MSR_GS_KERNEL); 
     61        kseg->kstack_rsp = THREAD->arch.kstack_rsp;
    6962}
    7063
Note: See TracChangeset for help on using the changeset viewer.