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