1 //===-- trusty.cpp ---------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "platform.h" 10 11 #if SCUDO_TRUSTY 12 13 #include "common.h" 14 #include "mutex.h" 15 #include "trusty.h" 16 17 #include <errno.h> // for errno 18 #include <lk/err_ptr.h> // for PTR_ERR and IS_ERR 19 #include <stdio.h> // for printf() 20 #include <stdlib.h> // for getenv() 21 #include <sys/auxv.h> // for getauxval() 22 #include <time.h> // for clock_gettime() 23 #include <trusty_err.h> // for lk_err_to_errno() 24 #include <trusty_syscalls.h> // for _trusty_brk() 25 #include <uapi/mm.h> // for MMAP flags 26 27 namespace scudo { 28 29 uptr getPageSize() { return getauxval(AT_PAGESZ); } 30 31 void NORETURN die() { abort(); } 32 33 void *map(void *Addr, uptr Size, const char *Name, uptr Flags, 34 UNUSED MapPlatformData *Data) { 35 uint32_t MmapFlags = 36 MMAP_FLAG_ANONYMOUS | MMAP_FLAG_PROT_READ | MMAP_FLAG_PROT_WRITE; 37 38 // If the MAP_NOACCESS flag is set, Scudo tries to reserve 39 // a memory region without mapping physical pages. This corresponds 40 // to MMAP_FLAG_NO_PHYSICAL in Trusty. 41 if (Flags & MAP_NOACCESS) 42 MmapFlags |= MMAP_FLAG_NO_PHYSICAL; 43 if (Addr) 44 MmapFlags |= MMAP_FLAG_FIXED_NOREPLACE; 45 46 if (Flags & MAP_MEMTAG) 47 MmapFlags |= MMAP_FLAG_PROT_MTE; 48 49 void *P = (void *)_trusty_mmap(Addr, Size, MmapFlags, 0); 50 51 if (IS_ERR(P)) { 52 errno = lk_err_to_errno(PTR_ERR(P)); 53 dieOnMapUnmapError(Size); 54 return nullptr; 55 } 56 57 return P; 58 } 59 60 void unmap(UNUSED void *Addr, UNUSED uptr Size, UNUSED uptr Flags, 61 UNUSED MapPlatformData *Data) { 62 if (_trusty_munmap(Addr, Size) != 0) 63 dieOnMapUnmapError(); 64 } 65 66 void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags, 67 UNUSED MapPlatformData *Data) {} 68 69 void releasePagesToOS(UNUSED uptr BaseAddress, UNUSED uptr Offset, 70 UNUSED uptr Size, UNUSED MapPlatformData *Data) {} 71 72 const char *getEnv(const char *Name) { return getenv(Name); } 73 74 // All mutex operations are a no-op since Trusty doesn't currently support 75 // threads. 76 bool HybridMutex::tryLock() { return true; } 77 78 void HybridMutex::lockSlow() {} 79 80 void HybridMutex::unlock() {} 81 82 void HybridMutex::assertHeldImpl() {} 83 84 u64 getMonotonicTime() { 85 timespec TS; 86 clock_gettime(CLOCK_MONOTONIC, &TS); 87 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) + 88 static_cast<u64>(TS.tv_nsec); 89 } 90 91 u64 getMonotonicTimeFast() { 92 #if defined(CLOCK_MONOTONIC_COARSE) 93 timespec TS; 94 clock_gettime(CLOCK_MONOTONIC_COARSE, &TS); 95 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) + 96 static_cast<u64>(TS.tv_nsec); 97 #else 98 return getMonotonicTime(); 99 #endif 100 } 101 102 u32 getNumberOfCPUs() { return 0; } 103 104 u32 getThreadID() { return 0; } 105 106 bool getRandom(UNUSED void *Buffer, UNUSED uptr Length, UNUSED bool Blocking) { 107 return false; 108 } 109 110 void outputRaw(const char *Buffer) { printf("%s", Buffer); } 111 112 void setAbortMessage(UNUSED const char *Message) {} 113 114 } // namespace scudo 115 116 #endif // SCUDO_TRUSTY 117