1//===-- mem_map.h -----------------------------------------------*- 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#ifndef SCUDO_MEM_MAP_H_
10#define SCUDO_MEM_MAP_H_
11
12#include "mem_map_base.h"
13
14#include "common.h"
15#include "internal_defs.h"
16
17// TODO: This is only used for `MapPlatformData`. Remove these includes when we
18// have all three platform specific `MemMap` and `ReservedMemory`
19// implementations.
20#include "fuchsia.h"
21#include "linux.h"
22#include "trusty.h"
23
24#include "mem_map_fuchsia.h"
25#include "mem_map_linux.h"
26
27namespace scudo {
28
29// This will be deprecated when every allocator has been supported by each
30// platform's `MemMap` implementation.
31class MemMapDefault final : public MemMapBase<MemMapDefault> {
32public:
33 constexpr MemMapDefault() = default;
34 MemMapDefault(uptr Base, uptr Capacity) : Base(Base), Capacity(Capacity) {}
35
36 // Impls for base functions.
37 bool mapImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
38 void unmapImpl(uptr Addr, uptr Size);
39 bool remapImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
40 void setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags);
41 void releasePagesToOSImpl(uptr From, uptr Size) {
42 return releaseAndZeroPagesToOSImpl(From, Size);
43 }
44 void releaseAndZeroPagesToOSImpl(uptr From, uptr Size);
45 uptr getBaseImpl() { return Base; }
46 uptr getCapacityImpl() { return Capacity; }
47 s64 getResidentPagesImpl(uptr From, uptr Size);
48
49 void setMapPlatformData(MapPlatformData &NewData) { Data = NewData; }
50
51private:
52 uptr Base = 0;
53 uptr Capacity = 0;
54 uptr MappedBase = 0;
55 MapPlatformData Data = {};
56};
57
58// This will be deprecated when every allocator has been supported by each
59// platform's `MemMap` implementation.
60class ReservedMemoryDefault final
61 : public ReservedMemory<ReservedMemoryDefault, MemMapDefault> {
62public:
63 constexpr ReservedMemoryDefault() = default;
64
65 bool createImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
66 void releaseImpl();
67 MemMapT dispatchImpl(uptr Addr, uptr Size);
68 uptr getBaseImpl() { return Base; }
69 uptr getCapacityImpl() { return Capacity; }
70
71private:
72 uptr Base = 0;
73 uptr Capacity = 0;
74 MapPlatformData Data = {};
75};
76
77#if SCUDO_LINUX
78using ReservedMemoryT = ReservedMemoryLinux;
79using MemMapT = ReservedMemoryT::MemMapT;
80#elif SCUDO_FUCHSIA
81using ReservedMemoryT = ReservedMemoryFuchsia;
82using MemMapT = ReservedMemoryT::MemMapT;
83#elif SCUDO_TRUSTY
84using ReservedMemoryT = ReservedMemoryDefault;
85using MemMapT = ReservedMemoryT::MemMapT;
86#else
87#error \
88 "Unsupported platform, please implement the ReservedMemory for your platform!"
89#endif
90
91} // namespace scudo
92
93#endif // SCUDO_MEM_MAP_H_
94