blob: eb7da95326a4b06583e48e4093b1911c64a676da [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 */
AlexeiFedorovea68b552023-10-03 11:11:47 +010014#define REC_HEAP_PAGES 2U
15#define REC_HEAP_SIZE (REC_HEAP_PAGES * SZ_4K)
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000016
17/* Number of pages per REC for PMU state */
AlexeiFedorovea68b552023-10-03 11:11:47 +010018#define REC_PMU_PAGES 1U
19#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 */
AlexeiFedorovea68b552023-10-03 11:11:47 +010030#define REC_SIMD_PAGES 3U
31#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 */
AlexeiFedorovea68b552023-10-03 11:11:47 +010034#define REC_ATTEST_PAGES 1U
35#define REC_ATTEST_SIZE (REC_ATTEST_PAGES * SZ_4K)
36
37/* Number of pages per REC for attestation buffer */
38#define REC_ATTEST_TOKEN_BUF_SIZE (RMM_CCA_TOKEN_BUFFER * SZ_4K)
AlexeiFedorovec35c542023-04-27 17:52:02 +010039
40/* Number of pages per REC to be allocated */
41#define REC_NUM_PAGES (REC_HEAP_PAGES + \
42 REC_PMU_PAGES + \
43 REC_SIMD_PAGES + \
AlexeiFedorovea68b552023-10-03 11:11:47 +010044 REC_ATTEST_PAGES + \
45 RMM_CCA_TOKEN_BUFFER)
Soby Mathewb4c6df42022-11-09 11:13:29 +000046
47struct buffer_alloc_ctx {
48 unsigned char *buf;
49 size_t len;
50 memory_header_t *first;
51 memory_header_t *first_free;
52 int verify;
53};
54
55struct memory_header_s {
56 size_t magic1;
57 size_t size;
58 size_t alloc;
59 memory_header_t *prev;
60 memory_header_t *next;
61 memory_header_t *prev_free;
62 memory_header_t *next_free;
63 size_t magic2;
64};
65
Soby Mathewb4c6df42022-11-09 11:13:29 +000066/*
67 * Function to assign a heap context to the current CPU for
68 * use by the MbedCrypto. In case the heap needs to be isolated
69 * to the CPU executing a realm , this function must to be
70 * called before entering a Realm. This will ensure that any
71 * crypto operations triggered via RSI will used the assigned
72 * heap.
73 * Arguments:
74 * ctx - Pointer to a valid context for the memory allocator.
75 * Returns:
76 * 0 on success or a POSIX error code or error.
77 */
78int buffer_alloc_ctx_assign(struct buffer_alloc_ctx *ctx);
79
80/*
81 * Function to unasign a heap context from the current CPU.
82 * In case the heap was isolated to the CPU executing a realm,
83 * this function must to be called after exiting a Realm.
84 */
85void buffer_alloc_ctx_unassign(void);
86
87#endif /* MEMORY_ALLOC_H */