summaryrefslogtreecommitdiff
path: root/src/gc/softwarewritewatch.cpp
blob: e1f305e76ff58d970697cdb69cfa8d69c58e7b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "common.h"
#include "gcenv.h"
#include "env/gcenv.os.h"
#include "softwarewritewatch.h"

#ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
#ifndef DACCESS_COMPILE

static_assert((static_cast<size_t>(1) << SOFTWARE_WRITE_WATCH_AddressToTableByteIndexShift) == WRITE_WATCH_UNIT_SIZE, "Unexpected WRITE_WATCH_UNIT_SIZE");

extern "C"
{
    uint8_t *g_gc_sw_ww_table = nullptr;
    bool g_gc_sw_ww_enabled_for_gc_heap = false;
}

void SoftwareWriteWatch::StaticClose()
{
    if (GetTable() == nullptr)
    {
        return;
    }

    g_gc_sw_ww_enabled_for_gc_heap = false;
    g_gc_sw_ww_table = nullptr;
}

bool SoftwareWriteWatch::GetDirtyFromBlock(
    uint8_t *block,
    uint8_t *firstPageAddressInBlock,
    size_t startByteIndex,
    size_t endByteIndex,
    void **dirtyPages,
    size_t *dirtyPageIndexRef,
    size_t dirtyPageCount,
    bool clearDirty)
{
    assert(block != nullptr);
    assert(ALIGN_DOWN(block, sizeof(size_t)) == block);
    assert(firstPageAddressInBlock == reinterpret_cast<uint8_t *>(GetPageAddress(block - GetTable())));
    assert(startByteIndex < endByteIndex);
    assert(endByteIndex <= sizeof(size_t));
    assert(dirtyPages != nullptr);
    assert(dirtyPageIndexRef != nullptr);

    size_t &dirtyPageIndex = *dirtyPageIndexRef;
    assert(dirtyPageIndex < dirtyPageCount);

    size_t dirtyBytes = *reinterpret_cast<size_t *>(block);
    if (dirtyBytes == 0)
    {
        return true;
    }

    if (startByteIndex != 0)
    {
        size_t numLowBitsToClear = startByteIndex * 8;
        dirtyBytes >>= numLowBitsToClear;
        dirtyBytes <<= numLowBitsToClear;
    }
    if (endByteIndex != sizeof(size_t))
    {
        size_t numHighBitsToClear = (sizeof(size_t) - endByteIndex) * 8;
        dirtyBytes <<= numHighBitsToClear;
        dirtyBytes >>= numHighBitsToClear;
    }

    while (dirtyBytes != 0)
    {
        DWORD bitIndex;
        static_assert(sizeof(size_t) <= 8, "Unexpected sizeof(size_t)");
        if (sizeof(size_t) == 8)
        {
            BitScanForward64(&bitIndex, static_cast<DWORD64>(dirtyBytes));
        }
        else
        {
            BitScanForward(&bitIndex, static_cast<DWORD>(dirtyBytes));
        }

        // Each byte is only ever set to 0 or 0xff
        assert(bitIndex % 8 == 0);
        size_t byteMask = static_cast<size_t>(0xff) << bitIndex;
        assert((dirtyBytes & byteMask) == byteMask);
        dirtyBytes ^= byteMask;

        DWORD byteIndex = bitIndex / 8;
        if (clearDirty)
        {
            // Clear only the bytes for which pages are recorded as dirty
            block[byteIndex] = 0;
        }

        void *pageAddress = firstPageAddressInBlock + byteIndex * WRITE_WATCH_UNIT_SIZE;
        assert(pageAddress >= GetHeapStartAddress());
        assert(pageAddress < GetHeapEndAddress());
        assert(dirtyPageIndex < dirtyPageCount);
        dirtyPages[dirtyPageIndex] = pageAddress;
        ++dirtyPageIndex;
        if (dirtyPageIndex == dirtyPageCount)
        {
            return false;
        }
    }
    return true;
}

void SoftwareWriteWatch::GetDirty(
    void *baseAddress,
    size_t regionByteSize,
    void **dirtyPages,
    size_t *dirtyPageCountRef,
    bool clearDirty,
    bool isRuntimeSuspended)
{
    VerifyCreated();
    VerifyMemoryRegion(baseAddress, regionByteSize);
    assert(dirtyPages != nullptr);
    assert(dirtyPageCountRef != nullptr);

    size_t dirtyPageCount = *dirtyPageCountRef;
    if (dirtyPageCount == 0)
    {
        return;
    }

    if (!isRuntimeSuspended)
    {
        // When a page is marked as dirty, a memory barrier is not issued after the write most of the time. Issue a memory
        // barrier on all active threads of the process now to make recent changes to dirty state visible to this thread.
        GCToOSInterface::FlushProcessWriteBuffers();
    }

    uint8_t *tableRegionStart;
    size_t tableRegionByteSize;
    TranslateToTableRegion(baseAddress, regionByteSize, &tableRegionStart, &tableRegionByteSize);
    uint8_t *tableRegionEnd = tableRegionStart + tableRegionByteSize;

    uint8_t *blockStart = ALIGN_DOWN(tableRegionStart, sizeof(size_t));
    assert(blockStart >= GetUntranslatedTable());
    uint8_t *blockEnd = ALIGN_UP(tableRegionEnd, sizeof(size_t));
    assert(blockEnd <= GetUntranslatedTableEnd());
    uint8_t *fullBlockEnd = ALIGN_DOWN(tableRegionEnd, sizeof(size_t));

    size_t dirtyPageIndex = 0;
    uint8_t *currentBlock = blockStart;
    uint8_t *firstPageAddressInCurrentBlock = reinterpret_cast<uint8_t *>(GetPageAddress(currentBlock - GetTable()));

    do
    {
        if (blockStart == fullBlockEnd)
        {
            if (GetDirtyFromBlock(
                    currentBlock,
                    firstPageAddressInCurrentBlock,
                    tableRegionStart - blockStart,
                    tableRegionEnd - fullBlockEnd,
                    dirtyPages,
                    &dirtyPageIndex,
                    dirtyPageCount,
                    clearDirty))
            {
                *dirtyPageCountRef = dirtyPageIndex;
            }
            break;
        }

        if (tableRegionStart != blockStart)
        {
            if (!GetDirtyFromBlock(
                    currentBlock,
                    firstPageAddressInCurrentBlock,
                    tableRegionStart - blockStart,
                    sizeof(size_t),
                    dirtyPages,
                    &dirtyPageIndex,
                    dirtyPageCount,
                    clearDirty))
            {
                break;
            }
            currentBlock += sizeof(size_t);
            firstPageAddressInCurrentBlock += sizeof(size_t) * WRITE_WATCH_UNIT_SIZE;
        }

        while (currentBlock < fullBlockEnd)
        {
            if (!GetDirtyFromBlock(
                    currentBlock,
                    firstPageAddressInCurrentBlock,
                    0,
                    sizeof(size_t),
                    dirtyPages,
                    &dirtyPageIndex,
                    dirtyPageCount,
                    clearDirty))
            {
                break;
            }
            currentBlock += sizeof(size_t);
            firstPageAddressInCurrentBlock += sizeof(size_t) * WRITE_WATCH_UNIT_SIZE;
        }
        if (currentBlock < fullBlockEnd)
        {
            break;
        }

        if (tableRegionEnd != fullBlockEnd &&
            !GetDirtyFromBlock(
                currentBlock,
                firstPageAddressInCurrentBlock,
                0,
                tableRegionEnd - fullBlockEnd,
                dirtyPages,
                &dirtyPageIndex,
                dirtyPageCount,
                clearDirty))
        {
            break;
        }

        *dirtyPageCountRef = dirtyPageIndex;
    } while (false);

    if (!isRuntimeSuspended && clearDirty && dirtyPageIndex != 0)
    {
        // When dirtying a page, the dirty state of the page is first checked to see if the page is already dirty. If already
        // dirty, the write to mark it as dirty is skipped. So, when the dirty state of a page is cleared, we need to make sure
        // the cleared state is visible to other threads that may dirty the page, before marking through objects in the page, so
        // that the GC will not miss marking through dirtied objects in the page. Issue a memory barrier on all active threads
        // of the process now.
        MemoryBarrier(); // flush writes from this thread first to guarantee ordering
        GCToOSInterface::FlushProcessWriteBuffers();
    }
}

#endif // !DACCESS_COMPILE
#endif // FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP