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 | 8ce3053 | 2023-10-16 15:58:38 +0100 | [diff] [blame] | 14 | #include <realm_rsi.h> |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * A printf formatted function used in the Realm world to log messages |
| 18 | * in the shared buffer. |
| 19 | * Locate the shared logging buffer and print its content |
| 20 | */ |
| 21 | void realm_printf(const char *fmt, ...) |
| 22 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 23 | host_shared_data_t *guest_shared_data = realm_get_my_shared_structure(); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 24 | char *log_buffer = (char *)guest_shared_data->log_buffer; |
| 25 | va_list args; |
| 26 | |
| 27 | va_start(args, fmt); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 28 | if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) { |
| 29 | (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE); |
| 30 | } |
| 31 | (void)vsnprintf((char *)log_buffer + |
| 32 | strnlen((const char *)log_buffer, MAX_BUF_SIZE), |
| 33 | MAX_BUF_SIZE, fmt, args); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 34 | va_end(args); |
Shruti Gupta | 8ce3053 | 2023-10-16 15:58:38 +0100 | [diff] [blame] | 35 | rsi_exit_to_host(HOST_CALL_EXIT_PRINT_CMD); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void __attribute__((__noreturn__)) do_panic(const char *file, int line) |
| 39 | { |
| 40 | realm_printf("PANIC in file: %s line: %d\n", file, line); |
| 41 | while (true) { |
| 42 | continue; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* This is used from printf() when crash dump is reached */ |
| 47 | int console_putc(int c) |
| 48 | { |
Shruti Gupta | 550e3e8 | 2023-08-16 13:20:11 +0100 | [diff] [blame] | 49 | host_shared_data_t *guest_shared_data = realm_get_my_shared_structure(); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 50 | char *log_buffer = (char *)guest_shared_data->log_buffer; |
| 51 | |
| 52 | if ((c < 0) || (c > 127)) { |
| 53 | return -1; |
| 54 | } |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 55 | if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) { |
| 56 | (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE); |
| 57 | } |
| 58 | *((char *)log_buffer + strnlen((const char *)log_buffer, MAX_BUF_SIZE)) = c; |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame] | 59 | |
| 60 | return c; |
| 61 | } |