14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022 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#include "ecmascript/platform/map.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include <cerrno> 194514f5e3Sopenharmony_ci#include <sys/mman.h> 204514f5e3Sopenharmony_ci#include <unistd.h> 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_ci#include "ecmascript/platform/os.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::ecmascript { 254514f5e3Sopenharmony_ciMemMap PageMap(size_t size, int prot, size_t alignment, void *addr, int flags) 264514f5e3Sopenharmony_ci{ 274514f5e3Sopenharmony_ci ASSERT(size == AlignUp(size, PageSize())); 284514f5e3Sopenharmony_ci ASSERT(alignment == AlignUp(alignment, PageSize())); 294514f5e3Sopenharmony_ci size_t allocSize = size + alignment; 304514f5e3Sopenharmony_ci int newFlags = static_cast<int>(MAP_PRIVATE | MAP_ANONYMOUS | static_cast<unsigned int>(flags)); 314514f5e3Sopenharmony_ci void *result = mmap(addr, allocSize, prot, newFlags, -1, 0); 324514f5e3Sopenharmony_ci if (reinterpret_cast<intptr_t>(result) == -1) { 334514f5e3Sopenharmony_ci LOG_ECMA(FATAL) << "mmap failed with error code:" << strerror(errno); 344514f5e3Sopenharmony_ci } 354514f5e3Sopenharmony_ci if (alignment != 0) { 364514f5e3Sopenharmony_ci auto alignResult = AlignUp(reinterpret_cast<uintptr_t>(result), alignment); 374514f5e3Sopenharmony_ci size_t leftSize = alignResult - reinterpret_cast<uintptr_t>(result); 384514f5e3Sopenharmony_ci size_t rightSize = alignment - leftSize; 394514f5e3Sopenharmony_ci void *alignEndResult = reinterpret_cast<void *>(alignResult + size); 404514f5e3Sopenharmony_ci munmap(result, leftSize); 414514f5e3Sopenharmony_ci munmap(alignEndResult, rightSize); 424514f5e3Sopenharmony_ci result = reinterpret_cast<void *>(alignResult); 434514f5e3Sopenharmony_ci } 444514f5e3Sopenharmony_ci return MemMap(result, size); 454514f5e3Sopenharmony_ci} 464514f5e3Sopenharmony_ci 474514f5e3Sopenharmony_civoid PageUnmap(MemMap it) 484514f5e3Sopenharmony_ci{ 494514f5e3Sopenharmony_ci munmap(it.GetMem(), it.GetSize()); 504514f5e3Sopenharmony_ci} 514514f5e3Sopenharmony_ci 524514f5e3Sopenharmony_ciMemMap MachineCodePageMap(size_t size, int prot, size_t alignment) 534514f5e3Sopenharmony_ci{ 544514f5e3Sopenharmony_ci MemMap memMap = PageMap(size, prot, alignment); 554514f5e3Sopenharmony_ci PageTag(memMap.GetMem(), memMap.GetSize(), PageTagType::MACHINE_CODE); 564514f5e3Sopenharmony_ci return memMap; 574514f5e3Sopenharmony_ci} 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_civoid MachineCodePageUnmap(MemMap it) 604514f5e3Sopenharmony_ci{ 614514f5e3Sopenharmony_ci PageClearTag(it.GetMem(), it.GetSize()); 624514f5e3Sopenharmony_ci if (!PageProtect(it.GetMem(), it.GetSize(), PAGE_PROT_NONE)) { 634514f5e3Sopenharmony_ci return; 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci PageUnmap(it); 664514f5e3Sopenharmony_ci} 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_civoid PageRelease(void *mem, size_t size) 694514f5e3Sopenharmony_ci{ 704514f5e3Sopenharmony_ci madvise(mem, size, MADV_DONTNEED); 714514f5e3Sopenharmony_ci} 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_civoid PagePreRead(void *mem, size_t size) 744514f5e3Sopenharmony_ci{ 754514f5e3Sopenharmony_ci madvise(mem, size, MADV_WILLNEED); 764514f5e3Sopenharmony_ci} 774514f5e3Sopenharmony_ci 784514f5e3Sopenharmony_civoid PageTag(void *mem, size_t size, PageTagType type, [[maybe_unused]] const std::string &spaceName, 794514f5e3Sopenharmony_ci [[maybe_unused]] const uint32_t threadId) 804514f5e3Sopenharmony_ci{ 814514f5e3Sopenharmony_ci#if defined(CROSS_PLATFORM) 824514f5e3Sopenharmony_ci const char *tag = GetPageTagString(type); 834514f5e3Sopenharmony_ci PrctlSetVMA(mem, size, tag); 844514f5e3Sopenharmony_ci#else 854514f5e3Sopenharmony_ci const std::string &tag = GetPageTagString(type, spaceName, threadId); 864514f5e3Sopenharmony_ci PrctlSetVMA(mem, size, tag.c_str()); 874514f5e3Sopenharmony_ci#endif 884514f5e3Sopenharmony_ci} 894514f5e3Sopenharmony_ci 904514f5e3Sopenharmony_civoid PageClearTag(void *mem, size_t size) 914514f5e3Sopenharmony_ci{ 924514f5e3Sopenharmony_ci PrctlSetVMA(mem, size, nullptr); 934514f5e3Sopenharmony_ci} 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_cibool PageProtect(void *mem, size_t size, int prot) 964514f5e3Sopenharmony_ci{ 974514f5e3Sopenharmony_ci int ret = mprotect(mem, size, prot); 984514f5e3Sopenharmony_ci if (ret != 0) { 994514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "PageProtect mem = " << mem << ", size = " << size << 1004514f5e3Sopenharmony_ci ", change to " << prot << " failed, ret = " << ret << ", error code is " << errno; 1014514f5e3Sopenharmony_ci return false; 1024514f5e3Sopenharmony_ci } 1034514f5e3Sopenharmony_ci return true; 1044514f5e3Sopenharmony_ci} 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_cisize_t PageSize() 1074514f5e3Sopenharmony_ci{ 1084514f5e3Sopenharmony_ci return getpagesize(); 1094514f5e3Sopenharmony_ci} 1104514f5e3Sopenharmony_ci} // namespace panda::ecmascript 111