1 /**
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "runtime/arch/memory_helpers.h"
17 #include "runtime/include/managed_thread.h"
18 #include "runtime/mem/gc/gc_barrier_set.h"
19 #include "runtime/mem/gc/g1/g1-helpers.h"
20 #include "runtime/mem/gc/card_table-inl.h"
21
22 namespace ark::mem {
23
GetG1BarrierSet()24 static inline GCG1BarrierSet *GetG1BarrierSet()
25 {
26 Thread *thread = Thread::GetCurrent();
27 ASSERT(thread != nullptr);
28 GCBarrierSet *barrierSet = thread->GetBarrierSet();
29 ASSERT(barrierSet != nullptr);
30 return static_cast<GCG1BarrierSet *>(barrierSet);
31 }
32
PreWrbFuncEntrypoint(void * oldval)33 extern "C" void PreWrbFuncEntrypoint(void *oldval)
34 {
35 // The cast below is needed to truncate high 32bits from 64bit pointer
36 // in case object pointers have 32bit.
37 oldval = ToVoidPtr(ToObjPtr(oldval));
38 ASSERT(IsAddressInObjectsHeap(oldval));
39 ASSERT(IsAddressInObjectsHeap(static_cast<const ObjectHeader *>(oldval)->ClassAddr<BaseClass>()));
40 LOG(DEBUG, GC) << "G1GC pre barrier val = " << std::hex << oldval;
41 auto *thread = ManagedThread::GetCurrent();
42 // thread can't be null here because pre-barrier is called only in concurrent-mark, but we don't process
43 // weak-references in concurrent mark
44 ASSERT(thread != nullptr);
45 auto bufferVec = thread->GetPreBuff();
46 bufferVec->push_back(static_cast<ObjectHeader *>(oldval));
47 }
48
49 // The declaration for PostWrbUpdateCardFuncEntrypoint is generated from entrypoints.yaml,
50 // so there is no need for it to be in the corresponding header file for this .cpp
PostWrbUpdateCardFuncEntrypoint(const void * from,const void * to)51 extern "C" void PostWrbUpdateCardFuncEntrypoint(const void *from, const void *to)
52 {
53 ASSERT(from != nullptr);
54 ASSERT(to != nullptr);
55 // The cast below is needed to truncate high 32bits from 64bit pointer
56 // in case object pointers have 32bit.
57 from = ToVoidPtr(ToObjPtr(from));
58 GCG1BarrierSet *barriers = GetG1BarrierSet();
59 ASSERT(barriers != nullptr);
60 auto cardTable = barriers->GetCardTable();
61 ASSERT(cardTable != nullptr);
62 // No need to keep remsets for young->young
63 // NOTE(dtrubenkov): add assert that we do not have young -> young reference here
64 auto *card = cardTable->GetCardPtr(ToUintPtr(from));
65 LOG(DEBUG, GC) << "G1GC post queue add ref: " << std::hex << from << " -> " << ToVoidPtr(ToObjPtr(to))
66 << " from_card: " << cardTable->GetMemoryRange(card);
67 if (card->IsYoung()) {
68 return;
69 }
70 // StoreLoad barrier is required to guarantee order of previous reference store and card load
71 arch::StoreLoadBarrier();
72
73 auto cardValue = card->GetCard();
74 auto status = CardTable::Card::GetStatus(cardValue);
75 if (!CardTable::Card::IsMarked(status)) {
76 card->Mark();
77 if (!CardTable::Card::IsHot(cardValue)) {
78 barriers->Enqueue(card);
79 }
80 }
81 }
82
83 } // namespace ark::mem
84