14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_BARRIERS_INL_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_BARRIERS_INL_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/base/config.h" 204514f5e3Sopenharmony_ci#include "ecmascript/daemon/daemon_thread.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 234514f5e3Sopenharmony_ci#include "ecmascript/mem/assert_scope.h" 244514f5e3Sopenharmony_ci#include "ecmascript/mem/barriers.h" 254514f5e3Sopenharmony_ci#include "ecmascript/mem/region-inl.h" 264514f5e3Sopenharmony_ci#include "ecmascript/mem/heap.h" 274514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_cinamespace panda::ecmascript { 304514f5e3Sopenharmony_citemplate<WriteBarrierType writeType = WriteBarrierType::NORMAL> 314514f5e3Sopenharmony_cistatic ARK_INLINE void WriteBarrier(const JSThread *thread, void *obj, size_t offset, JSTaggedType value) 324514f5e3Sopenharmony_ci{ 334514f5e3Sopenharmony_ci // NOTE: The logic in WriteBarrier should be synced with CopyObject. 344514f5e3Sopenharmony_ci // if any new feature/bugfix be added in WriteBarrier, it should also be added to CopyObject. 354514f5e3Sopenharmony_ci ASSERT(value != JSTaggedValue::VALUE_UNDEFINED); 364514f5e3Sopenharmony_ci Region *objectRegion = Region::ObjectAddressToRange(static_cast<TaggedObject *>(obj)); 374514f5e3Sopenharmony_ci Region *valueRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(value)); 384514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_BARRIER_CHECK 394514f5e3Sopenharmony_ci if (!thread->GetEcmaVM()->GetHeap()->IsAlive(JSTaggedValue(value).GetHeapObject())) { 404514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "WriteBarrier checked value:" << value << " is invalid!"; 414514f5e3Sopenharmony_ci } 424514f5e3Sopenharmony_ci#endif 434514f5e3Sopenharmony_ci uintptr_t slotAddr = ToUintPtr(obj) + offset; 444514f5e3Sopenharmony_ci if (objectRegion->InGeneralOldSpace() && valueRegion->InGeneralNewSpace()) { 454514f5e3Sopenharmony_ci // Should align with '8' in 64 and 32 bit platform 464514f5e3Sopenharmony_ci ASSERT((slotAddr % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0); 474514f5e3Sopenharmony_ci objectRegion->InsertOldToNewRSet(slotAddr); 484514f5e3Sopenharmony_ci } else if (!objectRegion->InSharedHeap() && valueRegion->InSharedSweepableSpace()) { 494514f5e3Sopenharmony_ci#ifndef NDEBUG 504514f5e3Sopenharmony_ci if (UNLIKELY(JSTaggedValue(value).IsWeakForHeapObject())) { 514514f5e3Sopenharmony_ci CHECK_NO_LOCAL_TO_SHARE_WEAK_REF_HANDLE; 524514f5e3Sopenharmony_ci } 534514f5e3Sopenharmony_ci#endif 544514f5e3Sopenharmony_ci objectRegion->InsertLocalToShareRSet(slotAddr); 554514f5e3Sopenharmony_ci } else if (valueRegion->InEdenSpace() && objectRegion->InYoungSpace()) { 564514f5e3Sopenharmony_ci objectRegion->InsertNewToEdenRSet(slotAddr); 574514f5e3Sopenharmony_ci } 584514f5e3Sopenharmony_ci ASSERT(!objectRegion->InSharedHeap() || valueRegion->InSharedHeap()); 594514f5e3Sopenharmony_ci if (!valueRegion->InSharedHeap() && thread->IsConcurrentMarkingOrFinished()) { 604514f5e3Sopenharmony_ci Barriers::Update(thread, slotAddr, objectRegion, reinterpret_cast<TaggedObject *>(value), 614514f5e3Sopenharmony_ci valueRegion, writeType); 624514f5e3Sopenharmony_ci // NOTE: ConcurrentMarking and SharedConcurrentMarking can be enabled at the same time, but a specific value 634514f5e3Sopenharmony_ci // can't be "not shared heap" and "in SharedSweepableSpace" at the same time. So using "if - else if" is safe. 644514f5e3Sopenharmony_ci } else if (valueRegion->InSharedSweepableSpace() && thread->IsSharedConcurrentMarkingOrFinished()) { 654514f5e3Sopenharmony_ci if constexpr (writeType != WriteBarrierType::DESERIALIZE) { 664514f5e3Sopenharmony_ci Barriers::UpdateShared(thread, reinterpret_cast<TaggedObject *>(value), valueRegion); 674514f5e3Sopenharmony_ci } else { 684514f5e3Sopenharmony_ci // In deserialize, will never add references from old object(not allocated by deserialing) to 694514f5e3Sopenharmony_ci // new object(allocated by deserializing), only two kinds of references(new->old, new->new) will 704514f5e3Sopenharmony_ci // be added, the old object is considered as serialize_root, and be marked and pushed in 714514f5e3Sopenharmony_ci // SharedGC::MarkRoots, so just mark all the new object is enough, do not need to push them to 724514f5e3Sopenharmony_ci // workmanager and recursively visit slots of that. 734514f5e3Sopenharmony_ci ASSERT(DaemonThread::GetInstance()->IsConcurrentMarkingOrFinished()); 744514f5e3Sopenharmony_ci valueRegion->AtomicMark(JSTaggedValue(value).GetHeapObject()); 754514f5e3Sopenharmony_ci } 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci} 784514f5e3Sopenharmony_ci 794514f5e3Sopenharmony_citemplate<bool needWriteBarrier> 804514f5e3Sopenharmony_ciinline void Barriers::SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value) 814514f5e3Sopenharmony_ci{ 824514f5e3Sopenharmony_ci // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 834514f5e3Sopenharmony_ci *reinterpret_cast<JSTaggedType *>(reinterpret_cast<uintptr_t>(obj) + offset) = value; 844514f5e3Sopenharmony_ci if constexpr (needWriteBarrier) { 854514f5e3Sopenharmony_ci WriteBarrier(thread, obj, offset, value); 864514f5e3Sopenharmony_ci } 874514f5e3Sopenharmony_ci} 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ciinline void Barriers::SynchronizedSetClass(const JSThread *thread, void *obj, JSTaggedType value) 904514f5e3Sopenharmony_ci{ 914514f5e3Sopenharmony_ci reinterpret_cast<volatile std::atomic<JSTaggedType> *>(obj)->store(value, std::memory_order_release); 924514f5e3Sopenharmony_ci WriteBarrier(thread, obj, 0, value); 934514f5e3Sopenharmony_ci} 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_ciinline void Barriers::SynchronizedSetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value, 964514f5e3Sopenharmony_ci bool isPrimitive) 974514f5e3Sopenharmony_ci{ 984514f5e3Sopenharmony_ci reinterpret_cast<volatile std::atomic<JSTaggedType> *>(ToUintPtr(obj) + offset)->store(value, 994514f5e3Sopenharmony_ci std::memory_order_release); 1004514f5e3Sopenharmony_ci if (!isPrimitive) { 1014514f5e3Sopenharmony_ci WriteBarrier(thread, obj, offset, value); 1024514f5e3Sopenharmony_ci } 1034514f5e3Sopenharmony_ci} 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_cistatic inline void CopyMaybeOverlap(JSTaggedValue* dst, JSTaggedValue* src, size_t count) 1064514f5e3Sopenharmony_ci{ 1074514f5e3Sopenharmony_ci std::copy_n(src, count, dst); 1084514f5e3Sopenharmony_ci} 1094514f5e3Sopenharmony_ci 1104514f5e3Sopenharmony_cistatic inline void CopyNoOverlap(JSTaggedValue* __restrict__ dst, JSTaggedValue* __restrict__ src, size_t count) 1114514f5e3Sopenharmony_ci{ 1124514f5e3Sopenharmony_ci std::copy_n(src, count, dst); 1134514f5e3Sopenharmony_ci} 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_citemplate <Region::RegionSpaceKind kind> 1164514f5e3Sopenharmony_ciARK_NOINLINE bool BatchBitSet(const JSThread* thread, Region* objectRegion, JSTaggedValue* dst, size_t count); 1174514f5e3Sopenharmony_ci 1184514f5e3Sopenharmony_citemplate <bool needWriteBarrier, bool maybeOverlap> 1194514f5e3Sopenharmony_civoid Barriers::CopyObject(const JSThread* thread, JSTaggedValue* dst, JSTaggedValue* src, size_t count) 1204514f5e3Sopenharmony_ci{ 1214514f5e3Sopenharmony_ci // NOTE: The logic in CopyObject should be synced with WriteBarrier. 1224514f5e3Sopenharmony_ci // if any new feature/bugfix be added in CopyObject, it should also be added to WriteBarrier. 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci // step 1. copy from src to dst directly. 1254514f5e3Sopenharmony_ci CopyObjectPrimitive<maybeOverlap>(dst, src, count); 1264514f5e3Sopenharmony_ci if constexpr (!needWriteBarrier) { 1274514f5e3Sopenharmony_ci return; 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci // step 2. According to object region, update the corresponding bit set batch. 1304514f5e3Sopenharmony_ci Region* objectRegion = Region::ObjectAddressToRange(ToUintPtr(dst)); 1314514f5e3Sopenharmony_ci if (!objectRegion->InSharedHeap()) { 1324514f5e3Sopenharmony_ci bool allValueNotHeap = false; 1334514f5e3Sopenharmony_ci if (objectRegion->InYoungSpace()) { 1344514f5e3Sopenharmony_ci allValueNotHeap = BatchBitSet<Region::InYoung>(thread, objectRegion, dst, count); 1354514f5e3Sopenharmony_ci } else if (objectRegion->InGeneralOldSpace()) { 1364514f5e3Sopenharmony_ci allValueNotHeap = BatchBitSet<Region::InGeneralOld>(thread, objectRegion, dst, count); 1374514f5e3Sopenharmony_ci } else { 1384514f5e3Sopenharmony_ci allValueNotHeap = BatchBitSet<Region::Other>(thread, objectRegion, dst, count); 1394514f5e3Sopenharmony_ci } 1404514f5e3Sopenharmony_ci if (allValueNotHeap) { 1414514f5e3Sopenharmony_ci return; 1424514f5e3Sopenharmony_ci } 1434514f5e3Sopenharmony_ci } 1444514f5e3Sopenharmony_ci // step 3. According to marking status, update the barriers. 1454514f5e3Sopenharmony_ci const bool marking = thread->IsConcurrentMarkingOrFinished(); 1464514f5e3Sopenharmony_ci const bool sharedMarking = thread->IsSharedConcurrentMarkingOrFinished(); 1474514f5e3Sopenharmony_ci if (!marking && !sharedMarking) { 1484514f5e3Sopenharmony_ci return; 1494514f5e3Sopenharmony_ci } 1504514f5e3Sopenharmony_ci for (uint32_t i = 0; i < count; i++) { 1514514f5e3Sopenharmony_ci JSTaggedValue taggedValue = *(dst + i); 1524514f5e3Sopenharmony_ci if (!taggedValue.IsHeapObject()) { 1534514f5e3Sopenharmony_ci continue; 1544514f5e3Sopenharmony_ci } 1554514f5e3Sopenharmony_ci Region* valueRegion = Region::ObjectAddressToRange(taggedValue.GetTaggedObject()); 1564514f5e3Sopenharmony_ci ASSERT(!objectRegion->InSharedHeap() || valueRegion->InSharedHeap()); 1574514f5e3Sopenharmony_ci if (marking && !valueRegion->InSharedHeap()) { 1584514f5e3Sopenharmony_ci const uintptr_t slotAddr = ToUintPtr(dst) + JSTaggedValue::TaggedTypeSize() * i; 1594514f5e3Sopenharmony_ci Barriers::Update(thread, slotAddr, objectRegion, taggedValue.GetTaggedObject(), valueRegion); 1604514f5e3Sopenharmony_ci // NOTE: ConcurrentMarking and SharedConcurrentMarking can be enabled at the same time, but a specific 1614514f5e3Sopenharmony_ci // value can't be "not shared heap" and "in SharedSweepableSpace" at the same time. So using "if - else if" 1624514f5e3Sopenharmony_ci // is safe. 1634514f5e3Sopenharmony_ci } else if (sharedMarking && valueRegion->InSharedSweepableSpace()) { 1644514f5e3Sopenharmony_ci Barriers::UpdateShared(thread, taggedValue.GetTaggedObject(), valueRegion); 1654514f5e3Sopenharmony_ci } 1664514f5e3Sopenharmony_ci } 1674514f5e3Sopenharmony_ci} 1684514f5e3Sopenharmony_ci 1694514f5e3Sopenharmony_citemplate <bool maybeOverlap> 1704514f5e3Sopenharmony_ciinline void Barriers::CopyObjectPrimitive(JSTaggedValue* dst, JSTaggedValue* src, size_t count) 1714514f5e3Sopenharmony_ci{ 1724514f5e3Sopenharmony_ci // Copy Primitive value don't need thread. 1734514f5e3Sopenharmony_ci ASSERT((ToUintPtr(dst) % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0); 1744514f5e3Sopenharmony_ci if constexpr (maybeOverlap) { 1754514f5e3Sopenharmony_ci CopyMaybeOverlap(dst, src, count); 1764514f5e3Sopenharmony_ci } else { 1774514f5e3Sopenharmony_ci CopyNoOverlap(dst, src, count); 1784514f5e3Sopenharmony_ci } 1794514f5e3Sopenharmony_ci} 1804514f5e3Sopenharmony_ci} // namespace panda::ecmascript 1814514f5e3Sopenharmony_ci 1824514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MEM_BARRIERS_INL_H 183