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 <cstdlib> /* malloc */
17
18 #include "hilog/log_c.h"
19 #include "ux_page_table.h"
20
21 namespace OHOS {
22 namespace PurgeableMem {
23 #ifdef LOG_TAG
24 #undef LOG_TAG
25 #endif
26 #define LOG_TAG "PurgeableMem: UPT"
27
UxPageTable(uint64_t addr,size_t len)28 UxPageTable::UxPageTable(uint64_t addr, size_t len)
29 {
30 uxpt_ = (UxPageTableStruct *)malloc(UxPageTableSize());
31 if (!uxpt_) {
32 HILOG_ERROR(LOG_CORE, "%{public}s: malloc UxPageTableStruct fail", __func__);
33 }
34 PMState err = InitUxPageTable(uxpt_, addr, len); /* dataPtr is aligned */
35 if (err != PM_OK) {
36 HILOG_ERROR(LOG_CORE, "%{public}s: InitUxPageTable fail, %{public}s", __func__, GetPMStateName(err));
37 free(uxpt_);
38 uxpt_ = nullptr;
39 }
40 }
41
~UxPageTable()42 UxPageTable::~UxPageTable()
43 {
44 /* unmap uxpt */
45 if (uxpt_) {
46 PMState err = DeinitUxPageTable(uxpt_);
47 if (err != PM_OK) {
48 HILOG_ERROR(LOG_CORE, "%{public}s: deinit upt fail, %{public}s", __func__, GetPMStateName(err));
49 } else {
50 free(uxpt_);
51 uxpt_ = nullptr;
52 }
53 }
54 }
55
GetUxpte(uint64_t addr,size_t len)56 void UxPageTable::GetUxpte(uint64_t addr, size_t len)
57 {
58 UxpteGet(uxpt_, addr, len);
59 }
60
PutUxpte(uint64_t addr,size_t len)61 void UxPageTable::PutUxpte(uint64_t addr, size_t len)
62 {
63 UxptePut(uxpt_, addr, len);
64 }
65
66
CheckPresent(uint64_t addr,size_t len)67 bool UxPageTable::CheckPresent(uint64_t addr, size_t len)
68 {
69 return UxpteIsPresent(uxpt_, addr, len);
70 }
71
ToString() const72 std::string UxPageTable::ToString() const
73 {
74 std::string uxptStr = uxpt_ ? std::to_string((unsigned long long)uxpt_) : "0";
75 return "uxptAddr: " + uxptStr;
76 }
77 } /* namespace PurgeableMem */
78 } /* namespace OHOS */
79