1 //===-- trusty.cpp ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
15 #include "report_linux.h"
18 #include <errno.h> // for errno
19 #include <lk/err_ptr.h> // for PTR_ERR and IS_ERR
20 #include <stdio.h> // for printf()
21 #include <stdlib.h> // for getenv()
22 #include <sys/auxv.h> // for getauxval()
23 #include <time.h> // for clock_gettime()
24 #include <trusty_err.h> // for lk_err_to_errno()
25 #include <trusty_syscalls.h> // for _trusty_brk()
26 #include <uapi/mm.h> // for MMAP flags
30 uptr
getPageSize() { return getauxval(AT_PAGESZ
); }
32 void NORETURN
die() { abort(); }
34 void *map(void *Addr
, uptr Size
, const char *Name
, uptr Flags
,
35 UNUSED MapPlatformData
*Data
) {
37 MMAP_FLAG_ANONYMOUS
| MMAP_FLAG_PROT_READ
| MMAP_FLAG_PROT_WRITE
;
39 // If the MAP_NOACCESS flag is set, Scudo tries to reserve
40 // a memory region without mapping physical pages. This corresponds
41 // to MMAP_FLAG_NO_PHYSICAL in Trusty.
42 if (Flags
& MAP_NOACCESS
)
43 MmapFlags
|= MMAP_FLAG_NO_PHYSICAL
;
45 MmapFlags
|= MMAP_FLAG_FIXED_NOREPLACE
;
47 if (Flags
& MAP_MEMTAG
)
48 MmapFlags
|= MMAP_FLAG_PROT_MTE
;
50 void *P
= (void *)_trusty_mmap(Addr
, Size
, MmapFlags
, 0);
53 errno
= lk_err_to_errno(PTR_ERR(P
));
54 if (!(Flags
& MAP_ALLOWNOMEM
) || errno
!= ENOMEM
)
62 void unmap(UNUSED
void *Addr
, UNUSED uptr Size
, UNUSED uptr Flags
,
63 UNUSED MapPlatformData
*Data
) {
64 if (_trusty_munmap(Addr
, Size
) != 0)
65 reportUnmapError(reinterpret_cast<uptr
>(Addr
), Size
);
68 void setMemoryPermission(UNUSED uptr Addr
, UNUSED uptr Size
, UNUSED uptr Flags
,
69 UNUSED MapPlatformData
*Data
) {}
71 void releasePagesToOS(UNUSED uptr BaseAddress
, UNUSED uptr Offset
,
72 UNUSED uptr Size
, UNUSED MapPlatformData
*Data
) {}
74 const char *getEnv(const char *Name
) { return getenv(Name
); }
76 // All mutex operations are a no-op since Trusty doesn't currently support
78 bool HybridMutex::tryLock() { return true; }
80 void HybridMutex::lockSlow() {}
82 void HybridMutex::unlock() {}
84 void HybridMutex::assertHeldImpl() {}
86 u64
getMonotonicTime() {
88 clock_gettime(CLOCK_MONOTONIC
, &TS
);
89 return static_cast<u64
>(TS
.tv_sec
) * (1000ULL * 1000 * 1000) +
90 static_cast<u64
>(TS
.tv_nsec
);
93 u64
getMonotonicTimeFast() {
94 #if defined(CLOCK_MONOTONIC_COARSE)
96 clock_gettime(CLOCK_MONOTONIC_COARSE
, &TS
);
97 return static_cast<u64
>(TS
.tv_sec
) * (1000ULL * 1000 * 1000) +
98 static_cast<u64
>(TS
.tv_nsec
);
100 return getMonotonicTime();
104 u32
getNumberOfCPUs() { return 0; }
106 u32
getThreadID() { return 0; }
108 bool getRandom(UNUSED
void *Buffer
, UNUSED uptr Length
, UNUSED
bool Blocking
) {
112 void outputRaw(const char *Buffer
) { printf("%s", Buffer
); }
114 void setAbortMessage(UNUSED
const char *Message
) {}
118 #endif // SCUDO_TRUSTY