Software APIs
stack_utilization.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/silicon_creator/lib/stack_utilization.h"
6 
7 #include "sw/device/silicon_creator/lib/drivers/uart.h"
8 
9 #ifdef STACK_UTILIZATION_CHECK
10 void stack_utilization_print(void) {
11  extern uint32_t _stack_start[], _stack_end[];
12  // We configure a No-Access ePMP NA4 region at stack_start as a
13  // stack guard. We cannot access that word, so start the scan
14  // after the stack guard.
15  const uint32_t *sp = _stack_start + 1;
16  uint32_t free = 0;
17  uint32_t total = (uintptr_t)_stack_end - (uintptr_t)_stack_start;
18  while (sp < _stack_end && *sp == STACK_UTILIZATION_FREE_PATTERN) {
19  free += sizeof(uint32_t);
20  sp++;
21  }
22  uint32_t used = total - free;
23  // : K T S
24  const uint32_t kPrefix = 0x3a4b5453;
25  uart_write_imm(kPrefix);
26  uart_write_hex(used, sizeof(used), '/');
27  uart_write_hex(total, sizeof(total), '\r');
28  // Send the last char with putchar so we'll wait for the
29  // transmitter to finish.
30  uart_putchar('\n');
31 }
32 #endif