1 /*
2  * Copyright (C) 2018 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 #include <test-runner-arch.h>
26 #include <trusty/sysdeps.h>
27 
28 /* Size limits for bump allocators (trusty_calloc and trusty_alloc_pages) */
29 #define HEAP_SIZE (56 + 6 * 4)
30 #define PAGE_COUNT (3)
31 
32 static uint8_t heap[HEAP_SIZE];
33 static int heap_allocated = 0;
34 
35 static uint8_t pages[PAGE_COUNT * PAGE_SIZE] __ALIGNED(PAGE_SIZE);
36 static int pages_allocated = 0;
37 
38 extern int trusty_encode_page_info(struct ns_mem_page_info* page_info,
39                                    void* vaddr);
40 
41 /* libc functions that the compiler may generate calls to */
memcpy(void * dest,const void * src,size_t count)42 void* memcpy(void* dest, const void* src, size_t count) {
43     uint8_t* d = dest;
44     const uint8_t* s = src;
45     while (count--) {
46         *d++ = *s++;
47     }
48     return dest;
49 }
50 
memset(void * dest,int c,size_t count)51 void* memset(void* dest, int c, size_t count) {
52     uint8_t* d = dest;
53     while (count--) {
54         *d++ = c;
55     }
56     return dest;
57 }
58 
memcmp(const void * lhs,const void * rhs,size_t count)59 int memcmp(const void* lhs, const void* rhs, size_t count) {
60     const uint8_t* l = lhs;
61     const uint8_t* r = rhs;
62     while (count--) {
63         if (*l != *r) {
64             return *l > *r ? +1 : -1;
65         }
66         ++l, ++r;
67     }
68     return 0;
69 }
70 
strcpy(char * dest,char const * src)71 char* strcpy(char* dest, char const* src) {
72     char* ret = dest;
73     while ((*dest++ = *src++) != '\0') {
74     }
75     return ret;
76 }
77 
strlen(char const * s)78 size_t strlen(char const* s) {
79     size_t ret;
80     for (ret = 0; *s++; ret++) {
81     }
82     return ret;
83 }
84 
strcmp(char const * cs,char const * ct)85 int strcmp(char const* cs, char const* ct) {
86     int res;
87     const unsigned char* su1 = (const unsigned char*)cs;
88     const unsigned char* su2 = (const unsigned char*)ct;
89 
90     while (1) {
91         if ((res = *su1 - *su2++) != 0 || !*su1++)
92             break;
93     }
94 
95     return res;
96 }
97 
98 /* ql-tipc sysdeps functions */
99 
trusty_lock(struct trusty_dev * dev)100 void trusty_lock(struct trusty_dev* dev) {}
101 
trusty_unlock(struct trusty_dev * dev)102 void trusty_unlock(struct trusty_dev* dev) {}
103 
trusty_local_irq_disable(unsigned long * state)104 __attribute__((weak)) void trusty_local_irq_disable(unsigned long* state) {}
105 
trusty_local_irq_restore(unsigned long * state)106 void trusty_local_irq_restore(unsigned long* state) {}
107 
trusty_abort(void)108 void trusty_abort(void) {
109     host_exit(2);
110     __builtin_unreachable();
111 }
112 
trusty_printf(const char * format,...)113 void trusty_printf(const char* format, ...) {}
114 
trusty_memcpy(void * dest,const void * src,size_t n)115 void* trusty_memcpy(void* dest, const void* src, size_t n) {
116     return memcpy(dest, src, n);
117 }
118 
trusty_memset(void * dest,const int c,size_t n)119 void* trusty_memset(void* dest, const int c, size_t n) {
120     return memset(dest, c, n);
121 }
122 
trusty_strcpy(char * dest,const char * src)123 char* trusty_strcpy(char* dest, const char* src) {
124     return strcpy(dest, src);
125 }
126 
trusty_strlen(const char * str)127 size_t trusty_strlen(const char* str) {
128     return strlen(str);
129 }
130 
trusty_strcmp(const char * str1,const char * str2)131 int trusty_strcmp(const char* str1, const char* str2) {
132     return strcmp(str1, str2);
133 }
134 
trusty_calloc(size_t n,size_t size)135 void* trusty_calloc(size_t n, size_t size) {
136     void* ret;
137     size_t asize = n * size;
138     if (heap_allocated + asize > HEAP_SIZE) {
139         return NULL;
140     }
141     ret = heap + heap_allocated;
142     heap_allocated += asize;
143     return ret;
144 }
145 
trusty_free(void * addr)146 void trusty_free(void* addr) {
147 }
148 
trusty_alloc_pages(unsigned count)149 void* trusty_alloc_pages(unsigned count) {
150     void* ret;
151     if (pages_allocated + count > PAGE_COUNT)
152         return NULL;
153     ret = pages + pages_allocated * PAGE_SIZE;
154     pages_allocated += count;
155     return ret;
156 }
157 
trusty_free_pages(void * va,unsigned count)158 void trusty_free_pages(void* va, unsigned count) {
159     /*
160      * We don't have a real allocator. Make sure we don't trigger any
161      * code-paths that need dynamic memory.
162      */
163     trusty_abort();
164 }
165