aboutsummaryrefslogtreecommitdiff
path: root/services/std_svc/drtm/drtm_cache.c
blob: d1b2a3b71cae83155e4af27e1215093f9343a3cb (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier:    BSD-3-Clause
 *
 * DRTM protected-resources cache
 *
 * Authors:
 *	Lucian Paul-Trifu <lucian.paultrifu@gmail.com>
 */

#include <common/debug.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <services/drtm_cache.h>

/*
 * XXX Note: the generic protected DRTM resources are being specialised into
 * DRTM TCB hashes.  Platform resources retrieved through the generic DRTM cache
 * are going to be retrieved through bespoke interfaces instead.
 * This file and drtm_qemu_virt_cached_resources_init.c will be removed once the
 * transition is complete.
 */

static char cache[1 * 1024];
static char *cache_free = cache;
#define CACHE_END ((char *)cache + sizeof(cache))

#include "drtm_qemu_virt_cached_resources_init.c"


static struct cached_res *cache_alloc(size_t bytes)
{
	struct cached_res *r;

	if (cache_free + bytes >= CACHE_END) {
		return NULL;
	}

	r = (struct cached_res *)cache_free;
	cache_free += bytes;

	return r;
}


void drtm_cache_init(void)
{
	const struct cached_res *r;

	memset(&cache, 0, sizeof(cache));

	r = CACHED_RESOURCES_INIT;
	while (r < CACHED_RESOURCES_INIT_END) {
		int rc;

		if (r->data_ptr) {
			rc = drtm_cache_resource_ptr(r->id, r->bytes, r->data_ptr);
		} else {
			rc = drtm_cache_resource(r->id, r->bytes, r->data);
		}
		if (rc) {
			WARN("%s: drtm_cache_resource_opt() failed rc=%d\n", __func__, rc);
			break;
		}

		r = (struct cached_res *)((char *)r + sizeof(*r)
		                          + (r->data_ptr ? 0 : r->bytes));
	}
}

int drtm_cache_resource_opt(const char *id, size_t bytes, const char *data,
                            bool copy_the_data)
{
	struct cached_res *res;
	size_t bytes_req = sizeof(struct cached_res) + (copy_the_data ? bytes : 0);

	if (strnlen(id, sizeof(res->id)) == sizeof(res->id) || !data) {
		return -EINVAL;
	}

	res = cache_alloc(bytes_req);
	if (!res) {
		return -ENOMEM;
	}

	(void)strlcpy(res->id, id, sizeof(res->id));

	res->bytes = bytes;
	if (copy_the_data) {
		res->data_ptr = NULL;
		(void)memcpy((char *)res->data, data, bytes);
	} else {
		res->data_ptr = data;
	}

	return 0;
}

void drtm_cache_get_resource(const char *id,
                             const char **res_out, size_t *res_out_bytes)
{
	struct cached_res *r = (struct cached_res *)cache;

	while ((char *)r < CACHE_END) {
		if (strncmp(r->id, id, sizeof(r->id)) == 0) {
			*res_out = r->data_ptr ? r->data_ptr : r->data;
			*res_out_bytes = r->bytes;
			return;
		}
		r = (struct cached_res *)((char *)r + sizeof(*r)
		                        + (r->data_ptr ? 0 : r->bytes));
	}

	*res_out = NULL;
	*res_out_bytes = 0;
}