Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arch/riscv/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE userspace.S)
zephyr_library_sources_ifdef(CONFIG_SEMIHOST semihost.c)
zephyr_library_sources_ifdef(CONFIG_EXCEPTION_STACK_TRACE stacktrace.c)
zephyr_linker_sources(ROM_START SORT_KEY 0x0vectors vector_table.ld)
zephyr_library_sources_ifdef(CONFIG_PROFILING_PERF perf.c)
93 changes: 93 additions & 0 deletions arch/riscv/core/perf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2023 KNS Group LLC (YADRO)
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>

static bool valid_stack(uintptr_t addr, k_tid_t current)
{
return current->stack_info.start <= addr &&
addr < current->stack_info.start + current->stack_info.size;
}

/*
* This function use frame pointers to unwind stack and get trace of return addresses.
* Return addresses are translated in corresponding function's names using .elf file.
* So we get function call trace
*/
size_t arch_perf_current_stack_trace(uintptr_t *buf, size_t size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance that this and https://github.com/zephyrproject-rtos/zephyr/blob/main/arch/riscv/core/stacktrace.c can share some of the unwinding logics?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #73587

{
if (size < 2U)
return 0;

size_t idx = 0;

/*
* In riscv (arch/riscv/core/isr.S) ra, ip($mepc) and fp($s0) are saved
* at the beginning of _isr_wrapper in order, specified by z_arch_esf_t.
* Then, before calling interruption handler, core switch $sp to
* _current_cpu->irq_stack and save $sp with offset -16 on irq stack
*
* The following lines lines do the reverse things to get ra, ip anf fp
* from thread stack
*/
const struct arch_esf * const esf =
*((struct arch_esf **)(((uintptr_t)_current_cpu->irq_stack) - 16));

/*
* $s0 is used as frame pointer.
*
* stack frame in memory (commonly):
* (addresses growth up)
* ....
* [-] <- $fp($s0) (curr)
* $ra
* $fp($s0) (next)
* ....
*
* If function do not call any other function, compiller may not save $ra,
* then stack frame will be:
* ....
* [-] <- $fp($s0) (curr)
* $fp($s0) (next)
* ....
*
*/
void **fp = (void **)esf->s0;
void **new_fp = (void **)fp[-1];

buf[idx++] = (uintptr_t)esf->mepc;

/*
* During function prologue and epilogue fp is equal to fp of
* previous function stack frame, it looks like second function
* from top is missed.
* So saving $ra will help in case when irq occurred in
* function prologue or epilogue.
*/
buf[idx++] = (uintptr_t)esf->ra;
if (valid_stack((uintptr_t)new_fp, _current)) {
fp = new_fp;
}
while (valid_stack((uintptr_t)fp, _current)) {
if (idx >= size)
return 0;

buf[idx++] = (uintptr_t)fp[-1];
new_fp = (void **)fp[-2];

/*
* anti-infinity-loop if
* new_fp can't be smaller than fp, cause the stack is growing down
* and trace moves deeper into the stack
*/
if (new_fp <= fp) {
break;
}
fp = new_fp;
}

return idx;
}
2 changes: 2 additions & 0 deletions arch/x86/core/ia32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ zephyr_library_sources_ifdef(CONFIG_GDBSTUB ia32/gdbstub.c)

zephyr_library_sources_ifdef(CONFIG_DEBUG_COREDUMP ia32/coredump.c)

zephyr_library_sources_ifdef(CONFIG_PROFILING_PERF ia32/perf.c)

zephyr_library_sources_ifdef(
CONFIG_X86_USE_THREAD_LOCAL_STORAGE
ia32/tls.c
Expand Down
15 changes: 8 additions & 7 deletions arch/x86/core/ia32/intstub.S
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ SECTION_FUNC(PINNED_TEXT, _interrupt_enter)
* EAX = isr_param, EDX = isr
*/

/* Push EDI as we will use it for scratch space.
/* Push EBP as we will use it for scratch space.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to this file could probably be in another commit / PR to document this change better

* Also it helps in stack unwinding
* Rest of the callee-saved regs get saved by invocation of C
* functions (isr handler, arch_swap(), etc)
*/
pushl %edi
pushl %ebp

/* load %ecx with &_kernel */

Expand All @@ -131,17 +132,17 @@ SECTION_FUNC(PINNED_TEXT, _interrupt_enter)
jne alreadyOnIntStack

/*
* switch to base of the interrupt stack: save esp in edi, then load
* switch to base of the interrupt stack: save esp in ebp, then load
* irq_stack pointer
*/

movl %esp, %edi
movl %esp, %ebp
movl _kernel_offset_to_irq_stack(%ecx), %esp


/* save thread's stack pointer onto base of interrupt stack */

pushl %edi /* Save stack pointer */
pushl %ebp /* Save stack pointer */

#ifdef CONFIG_PM
cmpl $0, _kernel_offset_to_idle(%ecx)
Expand Down Expand Up @@ -265,7 +266,7 @@ alreadyOnIntStack:
#endif /* CONFIG_LAZY_FPU_SHARING */

/* Restore volatile registers and return to the interrupted thread */
popl %edi
popl %ebp
popl %ecx
popl %edx
popl %eax
Expand Down Expand Up @@ -298,7 +299,7 @@ noReschedule:
*/

nestedInterrupt:
popl %edi
popl %ebp
popl %ecx /* pop volatile registers in reverse order */
popl %edx
popl %eax
Expand Down
81 changes: 81 additions & 0 deletions arch/x86/core/ia32/perf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 KNS Group LLC (YADRO)
* Copyright (c) 2020 Yonatan Goldschmidt <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>

static bool valid_stack(uintptr_t addr, k_tid_t current)
{
return current->stack_info.start <= addr &&
addr < current->stack_info.start + current->stack_info.size;
}

/* interruption stack frame */
struct isf {
uint32_t ebp;
uint32_t ecx;
uint32_t edx;
uint32_t eax;
uint32_t eip;
};

/*
* This function use frame pointers to unwind stack and get trace of return addresses.
* Return addresses are translated in corresponding function's names using .elf file.
* So we get function call trace
*/
size_t arch_perf_current_stack_trace(uintptr_t *buf, size_t size)
{
if (size < 1U)
return 0;

size_t idx = 0;

const struct isf * const isf =
*((struct isf **)(((void **)_current_cpu->irq_stack)-1));
/*
* In x86 (arch/x86/core/ia32/intstub.S) %eip and %ebp
* are saved at the beginning of _interrupt_enter in order, that described
* in struct esf. Core switch %esp to
* _current_cpu->irq_stack and push %esp on irq stack
*
* The following lines lines do the reverse things to get %eip and %ebp
* from thread stack
*/
void **fp = (void **)isf->ebp;

/*
* %ebp is frame pointer.
*
* stack frame in memory:
* (addresses growth up)
* ....
* ra
* %ebp (next) <- %ebp (curr)
* ....
*/

buf[idx++] = (uintptr_t)isf->eip;
while (valid_stack((uintptr_t)fp, _current)) {
if (idx >= size)
return 0;

buf[idx++] = (uintptr_t)fp[1];
void **new_fp = (void **)fp[0];

/*
* anti-infinity-loop if
* new_fp can't be smaller than fp, cause the stack is growing down
* and trace moves deeper into the stack
*/
if (new_fp <= fp) {
break;
}
fp = new_fp;
}

return idx;
}
1 change: 1 addition & 0 deletions arch/x86/core/intel64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ zephyr_library_sources_ifdef(CONFIG_IRQ_OFFLOAD intel64/irq_offload.c)
zephyr_library_sources_ifdef(CONFIG_USERSPACE intel64/userspace.S)
zephyr_library_sources_ifdef(CONFIG_THREAD_LOCAL_STORAGE intel64/tls.c)
zephyr_library_sources_ifdef(CONFIG_DEBUG_COREDUMP intel64/coredump.c)
zephyr_library_sources_ifdef(CONFIG_PROFILING_PERF intel64/perf.c)
67 changes: 67 additions & 0 deletions arch/x86/core/intel64/perf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 KNS Group LLC (YADRO)
* Copyright (c) 2020 Yonatan Goldschmidt <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>

static bool valid_stack(uintptr_t addr, k_tid_t current)
{
return current->stack_info.start <= addr &&
addr < current->stack_info.start + current->stack_info.size;
}

/*
* This function use frame pointers to unwind stack and get trace of return addresses.
* Return addresses are translated in corresponding function's names using .elf file.
* So we get function call trace
*/
size_t arch_perf_current_stack_trace(uintptr_t *buf, size_t size)
{
if (size < 1U)
return 0;

size_t idx = 0;

/*
* In x86_64 (arch/x86/core/intel64/locore.S) %rip and %rbp
* are always saved in _current->callee_saved before calling
* handler function if interrupt is not nested
*
* %rip points the location where interrupt was occurred
*/
buf[idx++] = (uintptr_t)_current->callee_saved.rip;
void **fp = (void **)_current->callee_saved.rbp;

/*
* %rbp is frame pointer.
*
* stack frame in memory:
* (addresses growth up)
* ....
* ra
* %rbp (next) <- %rbp (curr)
* ....
*/
while (valid_stack((uintptr_t)fp, _current)) {
if (idx >= size)
return 0;

buf[idx++] = (uintptr_t)fp[1];
void **new_fp = (void **)fp[0];

/*
* anti-infinity-loop if
* new_fp can't be smaller than fp, cause the stack is growing down
* and trace moves deeper into the stack
*/
if (new_fp <= fp) {
break;
}
fp = new_fp;
}

return idx;
}
1 change: 1 addition & 0 deletions doc/services/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OS Services
pm/index.rst
portability/index.rst
poweroff.rst
profiling/index.rst
shell/index.rst
serialization/index.rst
settings/index.rst
Expand Down
11 changes: 11 additions & 0 deletions doc/services/profiling/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. _profiling:

Profiling
#########

Required Kconfig: :kconfig:option:`CONFIG_PROFILING`

.. toctree::
:maxdepth: 1

perf.rst
Loading