• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef __TZDRIVER_COMPAT_H
33 #define __TZDRIVER_COMPAT_H
34 
35 #include <linux/interrupt.h>
36 #include <linux/kernel.h>
37 #include <linux/workqueue.h>
38 #include "arm.h"
39 #include "fs/driver.h"
40 #include "hisoc/random.h"
41 #include "los_process_pri.h"
42 #include "los_sched_pri.h"
43 #include "los_task_pri.h"
44 #include "los_vm_lock.h"
45 #include "los_vm_map.h"
46 #include "los_vm_phys.h"
47 #include "mbedtls/sha256.h"
48 
49 /* TEE config */
50 #define DEF_ENG 1
51 #define CONFIG_TEE_SMP 1
52 #define CONFIG_TEELOG 1
53 #define CONFIG_CPU_AFF_NR 0
54 #define CONFIG_TEE_SMP 1
55 /* TEE config end */
56 
57 #define VERIFY_READ 0
58 #define VERIFY_WRITE 1
59 #define MAX_DEV_NAME_SIZE  32
60 #define SHA256_DIGEST_LENGTH 32
61 #define ALIGN_TZ(x, boundary) (((x) + ((boundary) - 1)) & ~((boundary) - 1))
62 
63 #ifndef ARRAY_SIZE
64 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
65 #endif
66 
67 typedef pthread_mutex_t     mutex_t;
68 
69 #ifndef IS_ERR_OR_NULL
70 #ifndef IS_ERR_VALUE
71 #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long) - 4095)
72 #endif
73 #define IS_ERR_OR_NULL(x) ((!x) || IS_ERR_VALUE((UINTPTR)x))
74 #endif
75 
76 #define TEE_DEV_PRI 0660
77 
78 #define TASK_COMM_LEN OS_TCB_NAME_LEN
79 
80 #define WQ_HIGHPRI (1 << 4)
81 #define IRQF_NO_SUSPEND        0x00004000
82 #define __GFP_ZERO 0x8000u
83 
84 #define SZ_4K 0x1000UL
85 #define SZ_1M (1024 * 1024)
86 #define SZ_4M (4 * SZ_1M)
87 #define SZ_8M (8 * SZ_1M)
88 
89 #define MAX_POW_TWO(n) \
90 ( \
91 ((n) >> 31) ? 31 : ((n) >> 30) ? 30 : \
92 ((n) >> 29) ? 29 : ((n) >> 28) ? 28 : \
93 ((n) >> 27) ? 27 : ((n) >> 26) ? 26 : \
94 ((n) >> 25) ? 25 : ((n) >> 25) ? 25 : \
95 ((n) >> 23) ? 23 : ((n) >> 22) ? 22 : \
96 ((n) >> 21) ? 21 : ((n) >> 20) ? 20 : \
97 ((n) >> 19) ? 19 : ((n) >> 18) ? 18 : \
98 ((n) >> 17) ? 17 : ((n) >> 16) ? 16 : \
99 ((n) >> 15) ? 15 : ((n) >> 14) ? 14 : \
100 ((n) >> 13) ? 13 : ((n) >> 12) ? 12 : \
101 ((n) >> 11) ? 11 : ((n) >> 10) ? 10 : \
102 ((n) >> 9) ? 9: ((n) >> 8) ? 8 : \
103 ((n) >> 7) ? 7: ((n) >> 6) ? 6 : \
104 ((n) >> 5) ? 5: ((n) >> 4) ? 4 : \
105 ((n) >> 3) ? 3: ((n) >> 2) ? 2 : 1)
106 
107 #define GET_ORDER(n) \
108 ( \
109     n <= PAGE_SIZE ? 0 : (MAX_POW_TWO(n - 1) - PAGE_SHIFT + 1) \
110 )
111 
112 #ifndef MSEC_PER_SEC
113 #define MSEC_PER_SEC 1000
114 #endif
115 
116 #ifndef NSEC_PER_MSEC
117 #define NSEC_PER_MSEC 1000000L
118 #endif
119 
120 #ifndef USEC_PER_SEC
121 #define USEC_PER_SEC 1000000L
122 #endif
123 
124 #ifndef NSEC_PER_USEC
125 #define NSEC_PER_USEC 1000
126 #endif
127 
128 #define CRASH_RET_EXIT 0
129 #define CRASH_RET_TA 1
130 #define CRASH_RET_IP 2
131 
132 #undef DIV_ROUND_UP
133 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
134 #undef BITS_PER_BYTE
135 #define BITS_PER_BYTE 8
136 #undef BITS_PER_LONG
137 #define BITS_PER_LONG 64
138 #undef BITS_TO_LONGS
139 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(uint64_t))
140 #undef BIT_MASK
141 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
142 #undef BIT_WORD
143 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
144 #undef DECLARE_BITMAP
145 #define DECLARE_BITMAP(name, bits) uint64_t name[BITS_TO_LONGS(bits)]
146 
147 #define INIT_WORK_ONSTACK(_work, _func) \
148 do { \
149     INIT_WORK(_work, _func); \
150 } while (0)
151 
152 bool ScheduleWorkOn(int cpu, struct work_struct *work);
153 
154 #define __WORK_INIT(n, f) { \
155     .data = 0, \
156     .entry = { &(n).entry, &(n).entry }, \
157     .func = f \
158 }
159 #define DECLARE_WORK(work, func) \
160     struct work_struct work = __WORK_INIT(work, func);
161 
162 #define noinline __attribute__((noinline))
163 
164 LosTaskCB *KthreadRun(int (*threadfn)(UINTPTR data, int dataLen), void *data, int len, char *name);
165 void KthreadStop(const LosTaskCB *k);
166 int KthreadShouldStop(void);
167 INT32 DoVmallocRemap(LosVmMapRegion *vma, void *kvaddr);
168 int RemapVmallocRange(LosVmMapRegion *vma, void *addr, unsigned long pgoff);
169 int CreateTcClientDevice(const char *devName, const struct file_operations_vfs *op);
170 ssize_t SimpleReadFromBuffer(void *to, size_t count, const void *from, size_t available);
171 LosVmPage *MailboxPoolAllocPages(unsigned int order);
172 void MailboxPoolFreePages(LosVmPage *pageArray, size_t order);
173 
174 struct AesParam {
175     unsigned char *iv;
176     const unsigned char *key;
177     int size;
178     unsigned int encryptoType;
179 };
180 
181 int CryptoAescbcKey256(unsigned char *output, const unsigned char *input, struct AesParam *param);
182 
183 #define INT_SIZE 4
184 
AllocOrderedWorkqueue(const char * fmt,unsigned int flags)185 static inline struct workqueue_struct *AllocOrderedWorkqueue(const char *fmt, unsigned int flags)
186 {
187     return create_workqueue((char *)fmt);
188 }
189 
AccessOk(int type,unsigned long ptr,unsigned int size)190 static inline int AccessOk(int type, unsigned long ptr, unsigned int size)
191 {
192     if (ptr + size < ptr) {
193         return false;
194     }
195     return LOS_IsUserAddress(ptr + size);
196 }
197 
GetTaskUid(LosTaskCB * task)198 static inline int GetTaskUid(LosTaskCB *task)
199 {
200 #ifdef LOSCFG_SECURITY_CAPABILITY
201     return (int)OsProcessUserIDGet(task);
202 #else
203     return 0;
204 #endif
205 }
206 
DevmRequestIrq(unsigned int irq,irq_handler_t handler,unsigned long irqflags,const char * devname,void * devId)207 static inline int DevmRequestIrq(unsigned int irq, irq_handler_t handler,
208     unsigned long irqflags, const char *devname, void *devId)
209 {
210     return request_irq(irq, handler, irqflags, devname, NULL);
211 }
212 
GetPhyPage(void)213 static inline void *GetPhyPage(void)
214 {
215     LosVmPage *page = LOS_PhysPageAlloc();
216     if (page == NULL) {
217         return NULL;
218     }
219     return OsVmPageToVaddr(page);
220 }
221 
FreePhyPage(void * ptr)222 static inline void FreePhyPage(void *ptr)
223 {
224     if (ptr == NULL) {
225         return;
226     }
227     LosVmPage *page = OsVmVaddrToPage(ptr);
228     if (page != NULL) {
229         LOS_PhysPageFree(page);
230     }
231 }
232 
KthreadBindMask(LosTaskCB * p,UINT16 mask)233 static inline void KthreadBindMask(LosTaskCB *p, UINT16 mask)
234 {
235     if (p == NULL) {
236         return;
237     }
238     LOS_TaskCpuAffiSet(p->taskID, mask);
239 }
240 
HmSetBit(int nr,volatile uint64_t * addr)241 static inline void HmSetBit(int nr, volatile uint64_t *addr)
242 {
243     if (addr == NULL) {
244         return;
245     }
246     uint64_t mask = BIT_MASK(nr);
247     uint64_t *p   = ((uint64_t *)addr) + BIT_WORD(nr);
248     *p |= mask;
249 }
250 
HmClearBit(int nr,volatile uint64_t * addr)251 static inline void HmClearBit(int nr, volatile uint64_t *addr)
252 {
253     if (addr == NULL) {
254         return;
255     }
256 
257     uint64_t mask = BIT_MASK(nr);
258     uint64_t *p   = ((uint64_t *)addr) + BIT_WORD(nr);
259     *p &= ~mask;
260 }
261 
HmTestBit(int nr,const volatile uint64_t * addr)262 static inline int HmTestBit(int nr, const volatile uint64_t *addr)
263 {
264     if (addr == NULL) {
265         return 0;
266     }
267 
268     return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG - 1)));
269 }
270 
PreemptDisable(void)271 static inline void PreemptDisable(void)
272 {
273     UINT32 intSave = LOS_IntLock();
274     OsSchedLock();
275     LOS_IntRestore(intSave);
276 }
277 
PreemptEnable(void)278 static inline void PreemptEnable(void)
279 {
280     UINT32 intSave = LOS_IntLock();
281     OsSchedUnlock();
282     LOS_IntRestore(intSave);
283 }
284 
CmpXchg(unsigned int * lock,int old,int new)285 static inline int CmpXchg(unsigned int *lock, int old, int new)
286 {
287     return LOS_AtomicCmpXchg32bits((Atomic *)lock, new, old);
288 }
289 
RawSmpProcessorId(void)290 static inline int RawSmpProcessorId(void)
291 {
292     return ArchCurrCpuid();
293 }
294 
WakeUpProcess(LosTaskCB * p)295 static inline int WakeUpProcess(LosTaskCB *p)
296 {
297     LOS_TaskYield();
298     return 0;
299 }
300 
GetRandomBytesArch(void * data,uint32_t size)301 static inline void GetRandomBytesArch(void *data, uint32_t size)
302 {
303     HiRandomHwGetNumber((char *)data, size);
304 }
305 
GetUser(unsigned int * value,const unsigned int * userPtr)306 static inline void GetUser(unsigned int *value, const unsigned int *userPtr)
307 {
308     copy_from_user(value, userPtr, sizeof(unsigned int));
309 }
310 
GetCurrentPid(void)311 static inline int GetCurrentPid(void)
312 {
313     return OsCurrTaskGet()->processID;
314 }
315 
316 /* unsupport restart syscall */
RestartSyscall(void)317 static inline int RestartSyscall(void)
318 {
319     return 0;
320 }
321 
GetProcessGroupLeader(LosTaskCB * task)322 static inline LosTaskCB *GetProcessGroupLeader(LosTaskCB *task)
323 {
324     if (task == NULL) {
325         return NULL;
326     }
327     return OS_TCB_FROM_TID(OsProcessThreadGroupIDGet(task));
328 }
329 
MsecsToJiffies(const unsigned int m)330 static inline unsigned long MsecsToJiffies(const unsigned int m)
331 {
332     if ((int)m < 0) {
333         return 0;
334     }
335 
336     return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
337 }
338 
CurrentKernelTime(void)339 static inline struct timespec CurrentKernelTime(void)
340 {
341     struct timespec ts;
342     clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
343     return ts;
344 }
345 
InitDeferrableWork(struct delayed_work * w,void (* wq)(struct work_struct *))346 static inline void InitDeferrableWork(struct delayed_work *w, void(* wq)(struct work_struct *))
347 {
348     INIT_DELAYED_WORK(w, wq);
349 }
350 
IsKernelThread(LosTaskCB * task)351 static inline int IsKernelThread(LosTaskCB *task)
352 {
353     if (task == NULL) {
354         return true;
355     }
356     return !(OsProcessIsUserMode(OS_PCB_FROM_PID(task->processID)));
357 }
358 
IsTeecdProcess(LosTaskCB * teecd,LosTaskCB * task)359 static inline int IsTeecdProcess(LosTaskCB *teecd, LosTaskCB *task)
360 {
361     if (teecd == NULL || task == NULL) {
362         return 0;
363     }
364     return teecd->processID == task->processID;
365 }
366 
367 typedef mbedtls_sha256_context TeeSha256Context;
368 
TeeSha256Init(TeeSha256Context * ctx)369 static inline void TeeSha256Init(TeeSha256Context *ctx)
370 {
371     mbedtls_sha256_init(ctx);
372     (void)mbedtls_sha256_starts_ret(ctx, 0);
373 }
374 
TeeSha256Update(TeeSha256Context * ctx,const unsigned char * input,size_t ilen)375 static inline void TeeSha256Update(TeeSha256Context *ctx, const unsigned char *input, size_t ilen)
376 {
377     (void)mbedtls_sha256_update_ret(ctx, input, ilen);
378 }
379 
TeeSha256Final(TeeSha256Context * ctx,unsigned char output[32])380 static inline void TeeSha256Final(TeeSha256Context *ctx, unsigned char output[32])
381 {
382     (void)mbedtls_sha256_finish_ret(ctx, output);
383 }
384 
385 #endif
386