blob: e9eb61e482a0189953bdb87cddfc81befddd20e6 [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>
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 */
20void realm_printf(const char *fmt, ...)
21{
22 host_shared_data_t *guest_shared_data = realm_get_shared_structure();
23 char *log_buffer = (char *)guest_shared_data->log_buffer;
24 va_list args;
25
26 va_start(args, fmt);
27 spin_lock((spinlock_t *)&guest_shared_data->printf_lock);
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);
34 spin_unlock((spinlock_t *)&guest_shared_data->printf_lock);
35 va_end(args);
36}
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{
49 host_shared_data_t *guest_shared_data = realm_get_shared_structure();
50 char *log_buffer = (char *)guest_shared_data->log_buffer;
51
52 if ((c < 0) || (c > 127)) {
53 return -1;
54 }
55 spin_lock((spinlock_t *)&guest_shared_data->printf_lock);
56 if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
57 (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
58 }
59 *((char *)log_buffer + strnlen((const char *)log_buffer, MAX_BUF_SIZE)) = c;
60 spin_unlock((spinlock_t *)&guest_shared_data->printf_lock);
61
62 return c;
63}