Skip to content

Commit 77abc49

Browse files
keith-packardcarlescufi
authored andcommitted
arch/x86/x86-64: Set TLS before calling z_thread_mark_switched_in
Move the FS_BASE MSR code to to the top of __resume to ensure that %fs relative addressing run in the thread switching hook works. Signed-off-by: Keith Packard <[email protected]>
1 parent 95cec04 commit 77abc49

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

arch/x86/core/intel64/locore.S

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,22 @@ z_x86_switch:
379379
*/
380380

381381
__resume:
382+
#ifdef CONFIG_THREAD_LOCAL_STORAGE
383+
/*
384+
* Write the TLS base pointer to FS_BASE MSR,
385+
* where GCC emits code to access TLS data via
386+
* offset to FS.
387+
* Since wrmsr write EDX:EAX to MSR indicated by
388+
* ECX, the high 32-bit needs to be loaded into
389+
* RDX and right shifted by 32 bits so EDX has
390+
* the higher 32-bit value.
391+
*/
392+
movl $X86_FS_BASE, %ecx
393+
movq _thread_offset_to_tls(%rdi), %rax
394+
movq _thread_offset_to_tls(%rdi), %rdx
395+
shrq $32, %rdx
396+
wrmsr
397+
#endif
382398
#if (!defined(CONFIG_X86_KPTI) && defined(CONFIG_USERSPACE)) \
383399
|| defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
384400
pushq %rdi /* Caller-saved, stash it */
@@ -421,22 +437,6 @@ __resume:
421437
movq $0xB9, _thread_offset_to_rip(%rdi)
422438
#endif
423439

424-
#ifdef CONFIG_THREAD_LOCAL_STORAGE
425-
/*
426-
* Write the TLS base pointer to FS_BASE MSR,
427-
* where GCC emits code to access TLS data via
428-
* offset to FS.
429-
* Since wrmsr write EDX:EAX to MSR indicated by
430-
* ECX, the high 32-bit needs to be loaded into
431-
* RDX and right shifted by 32 bits so EDX has
432-
* the higher 32-bit value.
433-
*/
434-
movl $X86_FS_BASE, %ecx
435-
movq _thread_offset_to_tls(%rdi), %rax
436-
movq _thread_offset_to_tls(%rdi), %rdx
437-
shrq $32, %rdx
438-
wrmsr
439-
#endif
440440

441441
movq _thread_offset_to_rbx(%rdi), %rbx
442442
movq _thread_offset_to_rbp(%rdi), %rbp

0 commit comments

Comments
 (0)