blob: 1c4ee0370deb2453b8d669609cec4b817520fbb9 [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 Gupta8ce30532023-10-16 15:58:38 +010014#include <realm_rsi.h>
nabkah01002e5692022-10-10 12:36:46 +010015
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 */
21void realm_printf(const char *fmt, ...)
22{
Shruti Gupta550e3e82023-08-16 13:20:11 +010023 host_shared_data_t *guest_shared_data = realm_get_my_shared_structure();
nabkah01002e5692022-10-10 12:36:46 +010024 char *log_buffer = (char *)guest_shared_data->log_buffer;
25 va_list args;
26
27 va_start(args, fmt);
nabkah01002e5692022-10-10 12:36:46 +010028 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);
nabkah01002e5692022-10-10 12:36:46 +010034 va_end(args);
Shruti Gupta8ce30532023-10-16 15:58:38 +010035 rsi_exit_to_host(HOST_CALL_EXIT_PRINT_CMD);
nabkah01002e5692022-10-10 12:36:46 +010036}
37
38void __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 */
47int console_putc(int c)
48{
Shruti Gupta550e3e82023-08-16 13:20:11 +010049 host_shared_data_t *guest_shared_data = realm_get_my_shared_structure();
nabkah01002e5692022-10-10 12:36:46 +010050 char *log_buffer = (char *)guest_shared_data->log_buffer;
51
52 if ((c < 0) || (c > 127)) {
53 return -1;
54 }
nabkah01002e5692022-10-10 12:36:46 +010055 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;
nabkah01002e5692022-10-10 12:36:46 +010059
60 return c;
61}