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