1 //===-- memprof_mapping.h --------------------------------------*- 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 //===----------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 // Defines MemProf memory mapping.
12 //===----------------------------------------------------------------------===//
13 #ifndef MEMPROF_MAPPING_H
14 #define MEMPROF_MAPPING_H
16 #include "memprof_internal.h"
18 static const u64 kDefaultShadowScale
= 3;
19 #define SHADOW_SCALE kDefaultShadowScale
21 #define SHADOW_OFFSET __memprof_shadow_memory_dynamic_address
23 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
24 #define MEMPROF_ALIGNMENT 32
28 extern uptr kHighMemEnd
; // Initialized in __memprof_init.
30 } // namespace __memprof
32 #define SHADOW_ENTRY_SIZE 8
34 // Size of memory block mapped to a single shadow location
35 #define MEM_GRANULARITY 64ULL
37 #define SHADOW_MASK ~(MEM_GRANULARITY - 1)
39 #define MEM_TO_SHADOW(mem) \
40 ((((mem) & SHADOW_MASK) >> SHADOW_SCALE) + (SHADOW_OFFSET))
43 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
45 #define kLowShadowBeg SHADOW_OFFSET
46 #define kLowShadowEnd (MEM_TO_SHADOW(kLowMemEnd) + SHADOW_ENTRY_SIZE - 1)
48 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1 + SHADOW_ENTRY_SIZE - 1)
50 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
51 #define kHighShadowEnd (MEM_TO_SHADOW(kHighMemEnd) + SHADOW_ENTRY_SIZE - 1)
53 // With the zero shadow base we can not actually map pages starting from 0.
54 // This constant is somewhat arbitrary.
55 #define kZeroBaseShadowStart 0
56 #define kZeroBaseMaxShadowStart (1 << 18)
58 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 : kZeroBaseShadowStart)
59 #define kShadowGapEnd (kHighShadowBeg - 1)
63 inline uptr
MemToShadowSize(uptr size
) { return size
>> SHADOW_SCALE
; }
64 inline bool AddrIsInLowMem(uptr a
) { return a
<= kLowMemEnd
; }
66 inline bool AddrIsInLowShadow(uptr a
) {
67 return a
>= kLowShadowBeg
&& a
<= kLowShadowEnd
;
70 inline bool AddrIsInHighMem(uptr a
) {
71 return kHighMemBeg
&& a
>= kHighMemBeg
&& a
<= kHighMemEnd
;
74 inline bool AddrIsInHighShadow(uptr a
) {
75 return kHighMemBeg
&& a
>= kHighShadowBeg
&& a
<= kHighShadowEnd
;
78 inline bool AddrIsInShadowGap(uptr a
) {
79 // In zero-based shadow mode we treat addresses near zero as addresses
80 // in shadow gap as well.
81 if (SHADOW_OFFSET
== 0)
82 return a
<= kShadowGapEnd
;
83 return a
>= kShadowGapBeg
&& a
<= kShadowGapEnd
;
86 inline bool AddrIsInMem(uptr a
) {
87 return AddrIsInLowMem(a
) || AddrIsInHighMem(a
) ||
88 (flags()->protect_shadow_gap
== 0 && AddrIsInShadowGap(a
));
91 inline uptr
MemToShadow(uptr p
) {
92 CHECK(AddrIsInMem(p
));
93 return MEM_TO_SHADOW(p
);
96 inline bool AddrIsInShadow(uptr a
) {
97 return AddrIsInLowShadow(a
) || AddrIsInHighShadow(a
);
100 inline bool AddrIsAlignedByGranularity(uptr a
) {
101 return (a
& (SHADOW_GRANULARITY
- 1)) == 0;
104 inline void RecordAccess(uptr a
) {
105 // If we use a different shadow size then the type below needs adjustment.
106 CHECK_EQ(SHADOW_ENTRY_SIZE
, 8);
107 u64
*shadow_address
= (u64
*)MEM_TO_SHADOW(a
);
111 } // namespace __memprof
113 #endif // MEMPROF_MAPPING_H