Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 11 | typedef struct memory_header_s memory_header_t; |
| 12 | |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 13 | /* MbedTLS needs 8K of heap for attestation usecases */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame^] | 14 | #define REC_HEAP_PAGES 2U |
| 15 | #define REC_HEAP_SIZE (REC_HEAP_PAGES * SZ_4K) |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 16 | |
| 17 | /* Number of pages per REC for PMU state */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame^] | 18 | #define REC_PMU_PAGES 1U |
| 19 | #define REC_PMU_SIZE (REC_PMU_PAGES * SZ_4K) |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 20 | |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 21 | /* |
| 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 | */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame^] | 30 | #define REC_SIMD_PAGES 3U |
| 31 | #define REC_SIMD_SIZE (REC_SIMD_PAGES * SZ_4K) |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 32 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 33 | /* Number of pages per REC for 'rec_attest_data' structure */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame^] | 34 | #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) |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 39 | |
| 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 + \ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame^] | 44 | REC_ATTEST_PAGES + \ |
| 45 | RMM_CCA_TOKEN_BUFFER) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 46 | |
| 47 | struct 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 | |
| 55 | struct 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 66 | /* |
| 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 | */ |
| 78 | int 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 | */ |
| 85 | void buffer_alloc_ctx_unassign(void); |
| 86 | |
| 87 | #endif /* MEMORY_ALLOC_H */ |