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 "report_linux.h"
16#include "trusty.h"
17
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
27
28namespace scudo {
29
30uptr getPageSize() { return getauxval(AT_PAGESZ); }
31
32void NORETURN die() { abort(); }
33
34void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
35 UNUSED MapPlatformData *Data) {
36 uint32_t MmapFlags =
37 MMAP_FLAG_ANONYMOUS | MMAP_FLAG_PROT_READ | MMAP_FLAG_PROT_WRITE;
38
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;
44 if (Addr)
45 MmapFlags |= MMAP_FLAG_FIXED_NOREPLACE;
46
47 if (Flags & MAP_MEMTAG)
48 MmapFlags |= MMAP_FLAG_PROT_MTE;
49
50 void *P = (void *)_trusty_mmap(Addr, Size, MmapFlags, 0);
51
52 if (IS_ERR(P)) {
53 errno = lk_err_to_errno(PTR_ERR(P));
54 if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
55 reportMapError(Size);
56 return nullptr;
57 }
58
59 return P;
60}
61
62void 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);
66}
67
68void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
69 UNUSED MapPlatformData *Data) {}
70
71void releasePagesToOS(UNUSED uptr BaseAddress, UNUSED uptr Offset,
72 UNUSED uptr Size, UNUSED MapPlatformData *Data) {}
73
74const char *getEnv(const char *Name) { return getenv(Name); }
75
76// All mutex operations are a no-op since Trusty doesn't currently support
77// threads.
78bool HybridMutex::tryLock() { return true; }
79
80void HybridMutex::lockSlow() {}
81
82void HybridMutex::unlock() {}
83
84void HybridMutex::assertHeldImpl() {}
85
86u64 getMonotonicTime() {
87 timespec TS;
88 clock_gettime(CLOCK_MONOTONIC, &TS);
89 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
90 static_cast<u64>(TS.tv_nsec);
91}
92
93u64 getMonotonicTimeFast() {
94#if defined(CLOCK_MONOTONIC_COARSE)
95 timespec TS;
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);
99#else
100 return getMonotonicTime();
101#endif
102}
103
104u32 getNumberOfCPUs() { return 0; }
105
106u32 getThreadID() { return 0; }
107
108bool getRandom(UNUSED void *Buffer, UNUSED uptr Length, UNUSED bool Blocking) {
109 return false;
110}
111
112void outputRaw(const char *Buffer) { printf("%s", Buffer); }
113
114void setAbortMessage(UNUSED const char *Message) {}
115
116} // namespace scudo
117
118#endif // SCUDO_TRUSTY
119

source code of compiler-rt/lib/scudo/standalone/trusty.cpp