blob: 6bf8adf2cdcccac90c6e2951e26283eb23dcef6a [file] [log] [blame]
nabkah01002e5692022-10-10 12:36:46 +01001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdarg.h>
8#include <stdbool.h>
9#include <stdio.h>
10#include <string.h>
11
12#include <arch_helpers.h>
13#include <host_shared_data.h>
Shruti Gupta91105082024-11-27 05:29:55 +000014#include <realm_helpers.h>
15#include <realm_psi.h>
Shruti Gupta8ce30532023-10-16 15:58:38 +010016#include <realm_rsi.h>
nabkah01002e5692022-10-10 12:36:46 +010017
18/*
19 * A printf formatted function used in the Realm world to log messages
20 * in the shared buffer.
21 * Locate the shared logging buffer and print its content
22 */
23void realm_printf(const char *fmt, ...)
24{
Shruti Gupta550e3e82023-08-16 13:20:11 +010025 host_shared_data_t *guest_shared_data = realm_get_my_shared_structure();
nabkah01002e5692022-10-10 12:36:46 +010026 char *log_buffer = (char *)guest_shared_data->log_buffer;
27 va_list args;
Shruti Gupta91105082024-11-27 05:29:55 +000028 u_register_t ret;
nabkah01002e5692022-10-10 12:36:46 +010029
30 va_start(args, fmt);
nabkah01002e5692022-10-10 12:36:46 +010031 if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
32 (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
33 }
34 (void)vsnprintf((char *)log_buffer +
35 strnlen((const char *)log_buffer, MAX_BUF_SIZE),
36 MAX_BUF_SIZE, fmt, args);
nabkah01002e5692022-10-10 12:36:46 +010037 va_end(args);
Shruti Gupta91105082024-11-27 05:29:55 +000038 ret = rsi_exit_to_host(HOST_CALL_EXIT_PRINT_CMD);
39
40 /*
41 * Retry with PSI call for secondary planes
42 * Note - RSI_HOST_CALL will fail if test in P0 sets trap_hc flag in plane_enter
43 */
44 if ((ret != RSI_SUCCESS && !realm_is_plane0())) {
45 psi_exit_to_plane0(PSI_CALL_EXIT_PRINT_CMD, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL);
46 }
nabkah01002e5692022-10-10 12:36:46 +010047}
48
49void __attribute__((__noreturn__)) do_panic(const char *file, int line)
50{
51 realm_printf("PANIC in file: %s line: %d\n", file, line);
52 while (true) {
53 continue;
54 }
55}
56
57/* This is used from printf() when crash dump is reached */
58int console_putc(int c)
59{
Shruti Gupta550e3e82023-08-16 13:20:11 +010060 host_shared_data_t *guest_shared_data = realm_get_my_shared_structure();
nabkah01002e5692022-10-10 12:36:46 +010061 char *log_buffer = (char *)guest_shared_data->log_buffer;
62
63 if ((c < 0) || (c > 127)) {
64 return -1;
65 }
nabkah01002e5692022-10-10 12:36:46 +010066 if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
67 (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
68 }
69 *((char *)log_buffer + strnlen((const char *)log_buffer, MAX_BUF_SIZE)) = c;
nabkah01002e5692022-10-10 12:36:46 +010070
71 return c;
72}