blob: 06f381adf16f59ae2c35e5074c293cb5ab65f911 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#ifndef MEMORY_ALLOC_H
7#define MEMORY_ALLOC_H
8
9#include <stddef.h>
10
Soby Mathewb4c6df42022-11-09 11:13:29 +000011typedef struct memory_header_s memory_header_t;
12
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000013/* MbedTLS needs 8K of heap for attestation usecases */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010014#define REC_HEAP_PAGES 2U
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000015#define REC_HEAP_SIZE (REC_HEAP_PAGES * SZ_4K)
16
17/* Number of pages per REC for PMU state */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010018#define REC_PMU_PAGES 1U
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000019#define REC_PMU_SIZE (REC_PMU_PAGES * SZ_4K)
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000020
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000021/*
22 * SIMD context that holds FPU/SVE registers. Space to save max arch supported
23 * SVE vector length of 2048 bits.
24 * Size of 32 Z registers (256 bytes each): 8192 bytes
25 * Size of 16 P registers (32 bytes each) : 512 bytes
26 * Size of 1 FFR register (32 bytes each) : 32 bytes
27 * Size of other status registers : 32 bytes
28 * Total size is ~3 Pages (rounded up to page size).
29 */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010030#define REC_SIMD_PAGES 3U
AlexeiFedorovec35c542023-04-27 17:52:02 +010031#define REC_SIMD_SIZE (REC_SIMD_PAGES * SZ_4K)
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000032
AlexeiFedorovec35c542023-04-27 17:52:02 +010033/* Number of pages per REC for 'rec_attest_data' structure */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +010034#define REC_ATTEST_PAGES 1U
AlexeiFedorovec35c542023-04-27 17:52:02 +010035#define REC_ATTEST_SIZE (REC_ATTEST_PAGES * SZ_4K)
36
37/* Number of pages per REC to be allocated */
38#define REC_NUM_PAGES (REC_HEAP_PAGES + \
39 REC_PMU_PAGES + \
40 REC_SIMD_PAGES + \
41 REC_ATTEST_PAGES)
Soby Mathewb4c6df42022-11-09 11:13:29 +000042
43struct buffer_alloc_ctx {
44 unsigned char *buf;
45 size_t len;
46 memory_header_t *first;
47 memory_header_t *first_free;
48 int verify;
49};
50
51struct memory_header_s {
52 size_t magic1;
53 size_t size;
54 size_t alloc;
55 memory_header_t *prev;
56 memory_header_t *next;
57 memory_header_t *prev_free;
58 memory_header_t *next_free;
59 size_t magic2;
60};
61
Soby Mathewb4c6df42022-11-09 11:13:29 +000062/*
63 * Function to assign a heap context to the current CPU for
64 * use by the MbedCrypto. In case the heap needs to be isolated
65 * to the CPU executing a realm , this function must to be
66 * called before entering a Realm. This will ensure that any
67 * crypto operations triggered via RSI will used the assigned
68 * heap.
69 * Arguments:
70 * ctx - Pointer to a valid context for the memory allocator.
71 * Returns:
72 * 0 on success or a POSIX error code or error.
73 */
74int buffer_alloc_ctx_assign(struct buffer_alloc_ctx *ctx);
75
76/*
77 * Function to unasign a heap context from the current CPU.
78 * In case the heap was isolated to the CPU executing a realm,
79 * this function must to be called after exiting a Realm.
80 */
81void buffer_alloc_ctx_unassign(void);
82
83#endif /* MEMORY_ALLOC_H */