blob: 839338ebf6d36ec6fbdbae1e24eb0215d3725206 [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
Mate Toth-Pal0a8f37c2023-08-09 13:37:55 +020013#ifndef CBMC
14
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000015/* MbedTLS needs 8K of heap for attestation usecases */
AlexeiFedorovea68b552023-10-03 11:11:47 +010016#define REC_HEAP_PAGES 2U
17#define REC_HEAP_SIZE (REC_HEAP_PAGES * SZ_4K)
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000018
19/* Number of pages per REC for PMU state */
AlexeiFedorovea68b552023-10-03 11:11:47 +010020#define REC_PMU_PAGES 1U
21#define REC_PMU_SIZE (REC_PMU_PAGES * SZ_4K)
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000022
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000023/*
24 * SIMD context that holds FPU/SVE registers. Space to save max arch supported
25 * SVE vector length of 2048 bits.
26 * Size of 32 Z registers (256 bytes each): 8192 bytes
27 * Size of 16 P registers (32 bytes each) : 512 bytes
28 * Size of 1 FFR register (32 bytes each) : 32 bytes
29 * Size of other status registers : 32 bytes
30 * Total size is ~3 Pages (rounded up to page size).
31 */
AlexeiFedorovea68b552023-10-03 11:11:47 +010032#define REC_SIMD_PAGES 3U
33#define REC_SIMD_SIZE (REC_SIMD_PAGES * SZ_4K)
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000034
AlexeiFedorovec35c542023-04-27 17:52:02 +010035/* Number of pages per REC for 'rec_attest_data' structure */
AlexeiFedorovea68b552023-10-03 11:11:47 +010036#define REC_ATTEST_PAGES 1U
37#define REC_ATTEST_SIZE (REC_ATTEST_PAGES * SZ_4K)
38
39/* Number of pages per REC for attestation buffer */
40#define REC_ATTEST_TOKEN_BUF_SIZE (RMM_CCA_TOKEN_BUFFER * SZ_4K)
AlexeiFedorovec35c542023-04-27 17:52:02 +010041
42/* Number of pages per REC to be allocated */
43#define REC_NUM_PAGES (REC_HEAP_PAGES + \
44 REC_PMU_PAGES + \
45 REC_SIMD_PAGES + \
AlexeiFedorovea68b552023-10-03 11:11:47 +010046 REC_ATTEST_PAGES + \
47 RMM_CCA_TOKEN_BUFFER)
Soby Mathewb4c6df42022-11-09 11:13:29 +000048
Mate Toth-Pal0a8f37c2023-08-09 13:37:55 +020049#else /* CBMC */
50
51#define REC_HEAP_PAGES 2U
52#define REC_HEAP_SIZE (REC_HEAP_PAGES * SZ_4K)
53
54#define REC_PMU_PAGES 0U
55#define REC_PMU_SIZE (REC_PMU_PAGES * SZ_4K)
56
57#define REC_SIMD_PAGES 0U
58#define REC_SIMD_SIZE (REC_SIMD_PAGES * SZ_4K)
59
60#define REC_ATTEST_PAGES 0U
61#define REC_ATTEST_SIZE (REC_ATTEST_PAGES * SZ_4K)
62
63/* Number of aux granules pages per REC to be used */
64#define REC_NUM_PAGES (1U)
65
66#endif /* CBMC */
67
Soby Mathewb4c6df42022-11-09 11:13:29 +000068struct buffer_alloc_ctx {
69 unsigned char *buf;
70 size_t len;
71 memory_header_t *first;
72 memory_header_t *first_free;
73 int verify;
74};
75
76struct memory_header_s {
77 size_t magic1;
78 size_t size;
79 size_t alloc;
80 memory_header_t *prev;
81 memory_header_t *next;
82 memory_header_t *prev_free;
83 memory_header_t *next_free;
84 size_t magic2;
85};
86
Soby Mathewb4c6df42022-11-09 11:13:29 +000087/*
88 * Function to assign a heap context to the current CPU for
89 * use by the MbedCrypto. In case the heap needs to be isolated
90 * to the CPU executing a realm , this function must to be
91 * called before entering a Realm. This will ensure that any
92 * crypto operations triggered via RSI will used the assigned
93 * heap.
94 * Arguments:
95 * ctx - Pointer to a valid context for the memory allocator.
96 * Returns:
97 * 0 on success or a POSIX error code or error.
98 */
99int buffer_alloc_ctx_assign(struct buffer_alloc_ctx *ctx);
100
101/*
102 * Function to unasign a heap context from the current CPU.
103 * In case the heap was isolated to the CPU executing a realm,
104 * this function must to be called after exiting a Realm.
105 */
106void buffer_alloc_ctx_unassign(void);
107
108#endif /* MEMORY_ALLOC_H */