1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #ifndef TRUSTY_SYSDEPS_H_
26 #define TRUSTY_SYSDEPS_H_
27 /*
28  * Change these includes to match your platform to bring in the equivalent
29  * types available in a normal C runtime. At least things like uint64_t,
30  * uintptr_t, and bool (with |false|, |true| keywords) must be present.
31  */
32 #include <compiler.h>
33 #include <stdarg.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 
38 /*
39  * These attribute macros may need to be adjusted if not using gcc or clang.
40  */
41 #define TRUSTY_ATTR_PACKED __attribute__((packed))
42 #define TRUSTY_ATTR_NO_RETURN __attribute__((noreturn))
43 #define TRUSTY_ATTR_SENTINEL __attribute__((__sentinel__))
44 #define TRUSTY_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
45 
46 #define PAGE_SHIFT 12
47 #define PAGE_SIZE (1 << PAGE_SHIFT)
48 
49 /*
50  * Struct containing attributes for memory to be shared with secure size.
51  */
52 struct ns_mem_page_info {
53     uint64_t attr;
54     uint64_t paddr;
55     uint8_t ffa_mem_attr;
56     uint8_t ffa_mem_perm;
57 };
58 
59 struct trusty_dev;
60 
61 /*
62  * Lock/unlock mutex associated with @dev. These can be safely empty in a single
63  * threaded environment.
64  *
65  * @dev: Trusty device initialized with trusty_dev_init
66  */
67 void trusty_lock(struct trusty_dev* dev);
68 void trusty_unlock(struct trusty_dev* dev);
69 /*
70  * Disable/enable IRQ interrupts and save/restore @state
71  */
72 void trusty_local_irq_disable(unsigned long* state);
73 void trusty_local_irq_restore(unsigned long* state);
74 /*
75  * Put in standby state waiting for interrupt.
76  *
77  * @dev: Trusty device initialized with trusty_dev_init
78  * @event_poll: If true this function is entered after trusty_ipc_poll_for_event
79  *              returned TRUSTY_EVENT_NONE. In this case this call needs to
80  *              return without waiting for another interrupt if a secondary CPU
81  *              (any CPU other than the CPU that is waiting for a message) has
82  *              entered idle since the last such call. If there is only one CPU
83  *              calling into trusty this argument can be ignored.
84  */
85 void trusty_idle(struct trusty_dev* dev, bool event_poll);
86 /*
87  * Aborts the program or reboots the device.
88  */
89 void trusty_abort(void) TRUSTY_ATTR_NO_RETURN;
90 /*
91  * Print a formatted string. @format must point to a NULL-terminated string, and
92  * is followed by arguments to be printed.
93  */
94 void trusty_printf(const char* format, ...);
95 /*
96  * Copy @n bytes from @src to @dest.
97  */
98 void* trusty_memcpy(void* dest, const void* src, size_t n);
99 /*
100  * Set @n bytes starting at @dest to @c. Returns @dest.
101  */
102 void* trusty_memset(void* dest, const int c, size_t n);
103 /*
104  * Copy string from @src to @dest, including the terminating NULL byte.
105  *
106  * The size of the array at @dest should be long enough to contain the string
107  * at @src, and should not overlap in memory with @src.
108  */
109 char* trusty_strcpy(char* dest, const char* src);
110 /*
111  * Returns the length of @str, excluding the terminating NULL byte.
112  */
113 size_t trusty_strlen(const char* str);
114 /*
115  * Compare two strings, returns zero if strings are identical, else returns
116  * integer less than or greater than zero respectively if @str1 if less than
117  * or greater than @str2.
118  */
119 int trusty_strcmp(const char* str1, const char* str2);
120 /*
121  * Allocate @n elements of size @size. Initializes memory to 0, returns pointer
122  * to it.
123  */
124 void* trusty_calloc(size_t n, size_t size) TRUSTY_ATTR_WARN_UNUSED_RESULT;
125 /*
126  * Free memory at @addr allocated with trusty_calloc.
127  */
128 void trusty_free(void* addr);
129 /*
130  * Allocate @count contiguous pages to be shared with secure side.
131  *
132  * Returns:   vaddr of allocated memory
133  */
134 void* trusty_alloc_pages(unsigned count) TRUSTY_ATTR_WARN_UNUSED_RESULT;
135 /*
136  * Free @count pages at @vaddr allocated by trusty_alloc_pages
137  */
138 void trusty_free_pages(void* vaddr, unsigned count);
139 
140 #endif /* TRUSTY_SYSDEPS_H_ */
141