nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 1 | /* |
| 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 Gupta | 9110508 | 2024-11-27 05:29:55 +0000 | [diff] [blame^] | 14 | #include <realm_helpers.h> |
| 15 | #include <realm_psi.h> |
Shruti Gupta | 8ce3053 | 2023-10-16 15:58:38 +0100 | [diff] [blame] | 16 | #include <realm_rsi.h> |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 17 | |
| 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 | */ |
| 23 | void realm_printf(const char *fmt, ...) |
| 24 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 25 | host_shared_data_t *guest_shared_data = realm_get_my_shared_structure(); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 26 | char *log_buffer = (char *)guest_shared_data->log_buffer; |
| 27 | va_list args; |
Shruti Gupta | 9110508 | 2024-11-27 05:29:55 +0000 | [diff] [blame^] | 28 | u_register_t ret; |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 29 | |
| 30 | va_start(args, fmt); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 31 | 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); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 37 | va_end(args); |
Shruti Gupta | 9110508 | 2024-11-27 05:29:55 +0000 | [diff] [blame^] | 38 | 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 | } |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void __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 */ |
| 58 | int console_putc(int c) |
| 59 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 60 | host_shared_data_t *guest_shared_data = realm_get_my_shared_structure(); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 61 | char *log_buffer = (char *)guest_shared_data->log_buffer; |
| 62 | |
| 63 | if ((c < 0) || (c > 127)) { |
| 64 | return -1; |
| 65 | } |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 66 | 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; |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 70 | |
| 71 | return c; |
| 72 | } |