1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <lk/compiler.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <trusty_ipc.h>
23
24 __BEGIN_CDECLS
25
26 /**
27 * struct cov_shm - structure tracking shared memory
28 * @memref: handle to shared memory region
29 * @base: base address of shared memory region if mapped, NULL otherwise
30 * @len: length of shared memory region if mapped, 0 otherwise
31 */
32 struct cov_shm {
33 handle_t memref;
34 void* base;
35 size_t len;
36 };
37
cov_shm_is_mapped(struct cov_shm * shm)38 static bool inline cov_shm_is_mapped(struct cov_shm* shm) {
39 return shm->base;
40 }
41
cov_shm_init(struct cov_shm * shm,handle_t memref,void * base,size_t len)42 static void inline cov_shm_init(struct cov_shm* shm,
43 handle_t memref,
44 void* base,
45 size_t len) {
46 shm->memref = memref;
47 shm->base = base;
48 shm->len = len;
49 }
50
51 /**
52 * cov_shm_alloc() - allocate shared memory
53 * @shm: pointer to &struct shm to be initialized
54 * @len: amount of memory requested
55 *
56 * Return: 0 on success, negative error code on error
57 */
58 int cov_shm_alloc(struct cov_shm* shm, size_t len);
59
60 /**
61 * cov_shm_free() - free shared memory
62 * @shm: pointer to &struct shm previously initialized with shm_alloc()
63 */
64 void cov_shm_free(struct cov_shm* shm);
65
66 /**
67 * cov_shm_mmap() - map shared memory region
68 * @shm: pointer to &struct shm to be initialized
69 * @memref: handle to memory to be mapped
70 * @len: length of memory region referenced by @memref
71 *
72 * Return: 0 on success, negative error code on error
73 */
74 int cov_shm_mmap(struct cov_shm* shm, handle_t memref, size_t len);
75
76 /**
77 * cov_shm_munmap() - unmap shared memory
78 * @shm: pointer to &struct shm previously initialized with shm_mmap()
79 */
80 void cov_shm_munmap(struct cov_shm* shm);
81
82 /**
83 * cov_shm_munmap() - zero out contents of shared memory
84 * @shm: pointer to &struct shm
85 */
86 void cov_shm_clear(struct cov_shm* shm);
87
88 __END_CDECLS
89