1 /*
2 * Copyright (c) 2022 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 "ecmascript/platform/map.h"
17
18 #include <cerrno>
19 #include <sys/mman.h>
20 #include <unistd.h>
21
22 #include "ecmascript/platform/os.h"
23
24 namespace panda::ecmascript {
PageMap(size_t size,int prot,size_t alignment,void * addr,int flags)25 MemMap PageMap(size_t size, int prot, size_t alignment, void *addr, int flags)
26 {
27 ASSERT(size == AlignUp(size, PageSize()));
28 ASSERT(alignment == AlignUp(alignment, PageSize()));
29 size_t allocSize = size + alignment;
30 int newFlags = static_cast<int>(MAP_PRIVATE | MAP_ANONYMOUS | static_cast<unsigned int>(flags));
31 void *result = mmap(addr, allocSize, prot, newFlags, -1, 0);
32 if (reinterpret_cast<intptr_t>(result) == -1) {
33 LOG_ECMA(FATAL) << "mmap failed with error code:" << strerror(errno);
34 }
35 if (alignment != 0) {
36 auto alignResult = AlignUp(reinterpret_cast<uintptr_t>(result), alignment);
37 size_t leftSize = alignResult - reinterpret_cast<uintptr_t>(result);
38 size_t rightSize = alignment - leftSize;
39 void *alignEndResult = reinterpret_cast<void *>(alignResult + size);
40 munmap(result, leftSize);
41 munmap(alignEndResult, rightSize);
42 result = reinterpret_cast<void *>(alignResult);
43 }
44 return MemMap(result, size);
45 }
46
PageUnmap(MemMap it)47 void PageUnmap(MemMap it)
48 {
49 munmap(it.GetMem(), it.GetSize());
50 }
51
MachineCodePageMap(size_t size,int prot,size_t alignment)52 MemMap MachineCodePageMap(size_t size, int prot, size_t alignment)
53 {
54 MemMap memMap = PageMap(size, prot, alignment);
55 PageTag(memMap.GetMem(), memMap.GetSize(), PageTagType::MACHINE_CODE);
56 return memMap;
57 }
58
MachineCodePageUnmap(MemMap it)59 void MachineCodePageUnmap(MemMap it)
60 {
61 PageClearTag(it.GetMem(), it.GetSize());
62 if (!PageProtect(it.GetMem(), it.GetSize(), PAGE_PROT_NONE)) {
63 return;
64 }
65 PageUnmap(it);
66 }
67
PageRelease(void * mem,size_t size)68 void PageRelease(void *mem, size_t size)
69 {
70 madvise(mem, size, MADV_DONTNEED);
71 }
72
PagePreRead(void * mem,size_t size)73 void PagePreRead(void *mem, size_t size)
74 {
75 madvise(mem, size, MADV_WILLNEED);
76 }
77
PageTag(void * mem,size_t size,PageTagType type,const std::string & spaceName,const uint32_t threadId)78 void PageTag(void *mem, size_t size, PageTagType type, const std::string &spaceName, const uint32_t threadId)
79 {
80 const CString &tag = GetPageTagString(type, spaceName, threadId);
81 PrctlSetVMA(mem, size, tag.c_str());
82 }
83
PageClearTag(void * mem,size_t size)84 void PageClearTag(void *mem, size_t size)
85 {
86 PrctlSetVMA(mem, size, nullptr);
87 }
88
PageProtect(void * mem,size_t size,int prot)89 bool PageProtect(void *mem, size_t size, int prot)
90 {
91 int ret = mprotect(mem, size, prot);
92 if (ret != 0) {
93 LOG_ECMA(ERROR) << "PageProtect mem = " << mem << ", size = " << size <<
94 ", change to " << prot << " failed, ret = " << ret << ", error code is " << errno;
95 return false;
96 }
97 return true;
98 }
99
PageSize()100 size_t PageSize()
101 {
102 return getpagesize();
103 }
104 } // namespace panda::ecmascript
105