1*** Variables ***
2${SIMULATION_TIME}            "0.001"
3${TRACE_SIZE_LIMIT}           100
4${URL}                        https://dl.antmicro.com/projects/renode
5${PLAT_RISCV}                 SEPARATOR=\n  """
6...                           cpu: CPU.RiscV32 @ sysbus { cpuType: "rv32i"}
7...                           mem: Memory.MappedMemory @ sysbus 0x1000 { size: 0x1000 }
8...                           """
9${PLAT_ARM}                   SEPARATOR=\n  """
10...                           cpu: CPU.CortexM @ sysbus
11...                           ${SPACE*4}cpuType: "cortex-m4"
12...                           ${SPACE*4}nvic: nvic
13...
14...                           nvic: IRQControllers.NVIC @ sysbus 0xe000e000
15...                           ${SPACE*4}-> cpu@0
16...
17...                           mem: Memory.MappedMemory @ sysbus 0x0
18...                           ${SPACE*4}size: 0x4000
19...                           """
20# The program is constructed in such a way that will insert stack announcements (jal x1, XX(x))
21# but also chain blocks such that we have multiple stack announcements in a single block
22${PROG_RISCV}                 SEPARATOR=\n
23...                           loop:
24...                               jal x1, 4
25...                               jal x1, 4
26...                               jal x1, 4
27...                               jal loop
28# The code below configures MPU region 0 to <0x3000 0x64> to no priviliges, and then
29# triggers the PendSV interrupt. In the PendSV handler the MPU fauilt is triggered, resulting
30# in two execution stacks for one context.
31${PROG_ARM}                   SEPARATOR=\n
32...                           ldr r0, =0xE000ED9C  // MPU_RBAR
33...                           ldr r1, [r0]
34...                           orr r1, r1, #0x3000  // Base address 0x3000
35...                           orr r1, r1, #0x8     // Region enabled
36...                           str r1, [r0]
37...
38...                           ldr r0, =0xE000EDA0  // MPU_RASR
39...                           ldr r1, [r0]
40...                           orr r1, r1, #0xB     // Size 64, no R/W priv
41...                           str r1, [r0]
42...
43...                           ldr r0, =0xE000ED94  // MPU_CTRL
44...                           ldr r1, [r0]
45...                           orr r1, r1, #0x7     // Enable MPU
46...                           str r1, [r0]
47...
48...                           ldr r0, =0xE000ED04  // SCB->ICSR
49...                           ldr r1, [r0]
50...                           orr r1, r1, #0x10000000  // Trigger PENDSVSET
51...                           str r1, [r0]
52${PROG_ARM_HANDLER}           SEPARATOR=\n
53...                           // Trigger MemManageFault
54...                           ldr r0, =0x3000
55...                           ldr r1, [r0]
56
57*** Keywords ***
58Create Machine
59    Execute Command           set bin @${URL}/renode-mbed-pipeline-helloworld.elf-ga2ede71-s_2466384-6e3635e4ed159bc847cf1deb3dc7f24b10d26b41
60    Execute Command           include @scripts/single-node/stm32f746_mbed.resc
61
62Create Machine With Chained Stack Announcements
63    Execute Command           mach create
64    Execute Command           machine LoadPlatformDescriptionFromString ${PLAT_RISCV}
65
66    Execute Command           cpu AssembleBlock 0x1000 "${PROG_RISCV}"
67
68*** Test Cases ***
69Should Stop Writing To File Perfetto
70    Create Machine
71
72    ${TEST_TRACE_FILE}=       Allocate Temporary File
73
74    Execute Command           cpu EnableProfilerPerfetto "${TEST_TRACE_FILE}" true true ${TRACE_SIZE_LIMIT}
75    Execute Command           emulation RunFor ${SIMULATION_TIME}
76
77    ${TRACE_FILE_SIZE}=       Get File Size  ${TEST_TRACE_FILE}
78    Should Be True            ${TRACE_FILE_SIZE} < ${TRACE_SIZE_LIMIT}
79
80Should Stop Writing To File Collapsed Stack
81    Create Machine
82
83    ${TEST_TRACE_FILE}=       Allocate Temporary File
84
85    Execute Command           cpu EnableProfilerCollapsedStack "${TEST_TRACE_FILE}" true ${TRACE_SIZE_LIMIT}
86    Execute Command           emulation RunFor ${SIMULATION_TIME}
87
88    ${TRACE_FILE_SIZE}=       Get File Size  ${TEST_TRACE_FILE}
89    Should Be True            ${TRACE_FILE_SIZE} < ${TRACE_SIZE_LIMIT}
90
91Should Not Limit File Size
92    Create Machine
93
94    ${TEST_TRACE_FILE}=       Allocate Temporary File
95
96    Execute Command           cpu EnableProfilerCollapsedStack "${TEST_TRACE_FILE}" true
97    Execute Command           emulation RunFor ${SIMULATION_TIME}
98
99    ${TRACE_FILE_SIZE}=       Get File Size  ${TEST_TRACE_FILE}
100    Should Be True            ${TRACE_FILE_SIZE} > ${TRACE_SIZE_LIMIT}
101
102Should Disable Profiler Inside Translation Block
103    Create Machine With Chained Stack Announcements
104
105    # The mechanism that disables profiler can trigger it during the execution of a translation block
106    # which means that it can still trigger a stack announcement even after flushing the translation cache
107    # This test verifies that this case is handled
108    Execute Command           cpu EnableProfilerCollapsedStack "profile" true 1000
109    Execute Command           emulation RunFor "0.001"
110
111Should Disable Profiler After Reaching Maximum Nested Context Count
112    Create Log Tester         0
113    Execute Command           mach create
114    Execute Command           machine LoadPlatformDescriptionFromString ${PLAT_ARM}
115
116    ${TEST_TRACE_FILE}=       Allocate Temporary File
117    Execute Command           cpu EnableProfilerCollapsedStack "${TEST_TRACE_FILE}" maximumNestedContexts=1
118
119    # This sample will generate a nested interrupt: the first one is triggered by a PendSV, and the second
120    # is caused by a MemManageFault due to accessing memory restricted by the MPU. The `maximumNestedContexts`
121    # parameter is set to one, so it should disable the profiler upon the second interrupt.
122    Execute Command           cpu AssembleBlock 0x2000 "${PROG_ARM}"
123    Execute Command           cpu AssembleBlock 0x1000 "${PROG_ARM_HANDLER}"
124
125
126    Execute Command           sysbus WriteDoubleWord 0x00 0x100   # Initial SP
127    Execute Command           sysbus WriteDoubleWord 0x04 0x2000  # Initial PC
128    Execute Command           sysbus WriteDoubleWord 0x38 0x1001  # PendSV Handler @ 0x1000
129    Execute Command           cpu VectorTableOffset 0x0
130
131
132    Execute Command           cpu Step 19
133    Wait For Log Entry        maximum nested contexts exceeded, disabling profiler
134