Skip to content

Commit 61d42cf

Browse files
Andrew Boienashif
authored andcommitted
x86-64: fix thread tracing
The current instrumentation point for CONFIG_TRACING added in PR zephyrproject-rtos#28512 had two problems: - If userspace and KPTI are enabled, the tracing point is simply never run if we are resuming a user thread as the z_x86_trampoline_to_user function is jumped to and calls 'iret' from there - Only %rdi is being saved. However, at that location, *all* caller-saved registers are in use as they contain the resumed thread's context Simplest solution is to move this up near where we update page tables. The #ifdefs are used to make sure we don't push/pop %rdi more than once. At that point in the code only %rdi is in use among the volatile registers. Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
1 parent d312c6e commit 61d42cf

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

arch/x86/core/intel64/locore.S

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,23 @@ z_x86_switch:
324324
*/
325325

326326
__resume:
327-
#ifdef CONFIG_USERSPACE
328-
#ifndef CONFIG_X86_KPTI
327+
#if (!defined(CONFIG_X86_KPTI) && defined(CONFIG_USERSPACE)) \
328+
|| defined(CONFIG_TRACING)
329+
pushq %rdi /* Caller-saved, stash it */
330+
#if !defined(CONFIG_X86_KPTI) && defined(CONFIG_USERSPACE)
329331
/* If KPTI is enabled we're always on the kernel's page tables in
330332
* this context and the appropriate page table switch takes place
331333
* when trampolining back to user mode
332334
*/
333-
pushq %rdi /* Caller-saved, stash it */
334335
call z_x86_swap_update_page_tables
336+
#endif
337+
#ifdef CONFIG_TRACING
338+
call sys_trace_thread_switched_in
339+
#endif
335340
popq %rdi
336-
#endif /* CONFIG_X86_KPTI */
341+
#endif /* (!CONFIG_X86_KPTI && CONFIG_USERSPACE) || CONFIG_TRACING */
337342

343+
#ifdef CONFIG_USERSPACE
338344
/* Set up exception return stack frame */
339345
pushq _thread_offset_to_ss(%rdi) /* SS */
340346
#else
@@ -402,12 +408,6 @@ __resume:
402408
/* swapgs variant of Spectre V1. Disable speculation past this point */
403409
lfence
404410
#endif /* CONFIG_X86_BOUNDS_CHECK_BYPASS_MITIGATION */
405-
406-
#ifdef CONFIG_TRACING
407-
pushq %rdi
408-
call sys_trace_thread_switched_in
409-
popq %rdi
410-
#endif
411411
iretq
412412

413413

0 commit comments

Comments
 (0)