summaryrefslogtreecommitdiff
path: root/realm/realm_debug.c
blob: e9eb61e482a0189953bdb87cddfc81befddd20e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright (c) 2022, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include <arch_helpers.h>
#include <host_shared_data.h>

/*
 * A printf formatted function used in the Realm world to log messages
 * in the shared buffer.
 * Locate the shared logging buffer and print its content
 */
void realm_printf(const char *fmt, ...)
{
	host_shared_data_t *guest_shared_data = realm_get_shared_structure();
	char *log_buffer = (char *)guest_shared_data->log_buffer;
	va_list args;

	va_start(args, fmt);
	spin_lock((spinlock_t *)&guest_shared_data->printf_lock);
	if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
		(void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
	}
	(void)vsnprintf((char *)log_buffer +
			strnlen((const char *)log_buffer, MAX_BUF_SIZE),
			MAX_BUF_SIZE, fmt, args);
	spin_unlock((spinlock_t *)&guest_shared_data->printf_lock);
	va_end(args);
}

void __attribute__((__noreturn__)) do_panic(const char *file, int line)
{
	realm_printf("PANIC in file: %s line: %d\n", file, line);
	while (true) {
		continue;
	}
}

/* This is used from printf() when crash dump is reached */
int console_putc(int c)
{
	host_shared_data_t *guest_shared_data = realm_get_shared_structure();
	char *log_buffer = (char *)guest_shared_data->log_buffer;

	if ((c < 0) || (c > 127)) {
		return -1;
	}
	spin_lock((spinlock_t *)&guest_shared_data->printf_lock);
	if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
		(void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
	}
	*((char *)log_buffer + strnlen((const char *)log_buffer, MAX_BUF_SIZE)) = c;
	spin_unlock((spinlock_t *)&guest_shared_data->printf_lock);

	return c;
}