1// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <xtensa/coreasm.h>
16#include <xtensa/corebits.h>
17#include <xtensa/config/system.h>
18#include <xtensa/hal.h>
19
20/*
21 * esp_backtrace_get_start(uint32_t *pc, uint32_t *sp, uint32_t *next_pc)
22 *
23 *     High Addr
24 * ..................
25 * |     i-3 BS     |
26 * |   i-1 locals   | Function B
27 * .................. i-1 SP
28 * |     i-2 BS     |
29 * |    i locals    | Function A (Start of backtrace)
30 * ------------------ i SP
31 * |     i-1 BS     |
32 * |   i+1 locals   | Backtracing function (e.g. esp_backtrace_print())
33 * ------------------ i+1 SP
34 * |      i BS      |
35 * |   i+2 locals   | esp_backtrace_get_start() <- This function
36 * ------------------ i+2 SP
37 * |     i+1 BS     |
38 * |   i+3 locals   | xthal_window_spill()
39 * ------------------ i+3 SP
40 * .................. Low Addr
41 */
42    .section    .iram1, "ax"
43    .align      4
44    .global     esp_backtrace_get_start
45    .type       esp_backtrace_get_start, @function
46esp_backtrace_get_start:
47    entry   a1, 32
48    call8   xthal_window_spill  //Spill registers onto stack (excluding this function)
49    //a2, a3, a4 should be out arguments for i SP, i PC, i-1 PC respectively. Use a5 and a6 as scratch
50    l32e    a5, sp, -16         //Get i PC, which is ret addres of i+1
51    s32i    a5, a2, 0           //Store i PC to arg *pc
52    l32e    a6, sp, -12         //Get i+1 SP. Used to access i BS
53    l32e    a5, a6, -12         //Get i SP
54    s32i    a5, a3, 0           //Store i SP to arg *sp
55    l32e    a5, a6, -16         //Get i-1 PC, which is ret address of i
56    s32i    a5, a4, 0           //Store i-1 PC to arg *next_pc
57    retw
58