1f0bfeaa8Sopenharmony_ci/* 2f0bfeaa8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3f0bfeaa8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f0bfeaa8Sopenharmony_ci * you may not use this file except in compliance with the License. 5f0bfeaa8Sopenharmony_ci * You may obtain a copy of the License at 6f0bfeaa8Sopenharmony_ci * 7f0bfeaa8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f0bfeaa8Sopenharmony_ci * 9f0bfeaa8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f0bfeaa8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f0bfeaa8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f0bfeaa8Sopenharmony_ci * See the License for the specific language governing permissions and 13f0bfeaa8Sopenharmony_ci * limitations under the License. 14f0bfeaa8Sopenharmony_ci */ 15f0bfeaa8Sopenharmony_ci 16f0bfeaa8Sopenharmony_ci#include <cstdlib> /* malloc */ 17f0bfeaa8Sopenharmony_ci 18f0bfeaa8Sopenharmony_ci#include "hilog/log_c.h" 19f0bfeaa8Sopenharmony_ci#include "ux_page_table.h" 20f0bfeaa8Sopenharmony_ci 21f0bfeaa8Sopenharmony_cinamespace OHOS { 22f0bfeaa8Sopenharmony_cinamespace PurgeableMem { 23f0bfeaa8Sopenharmony_ci#ifdef LOG_TAG 24f0bfeaa8Sopenharmony_ci#undef LOG_TAG 25f0bfeaa8Sopenharmony_ci#endif 26f0bfeaa8Sopenharmony_ci#define LOG_TAG "PurgeableMem: UPT" 27f0bfeaa8Sopenharmony_ci 28f0bfeaa8Sopenharmony_ciUxPageTable::UxPageTable(uint64_t addr, size_t len) 29f0bfeaa8Sopenharmony_ci{ 30f0bfeaa8Sopenharmony_ci uxpt_ = (UxPageTableStruct *)malloc(UxPageTableSize()); 31f0bfeaa8Sopenharmony_ci if (!uxpt_) { 32f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "%{public}s: malloc UxPageTableStruct fail", __func__); 33f0bfeaa8Sopenharmony_ci } 34f0bfeaa8Sopenharmony_ci PMState err = InitUxPageTable(uxpt_, addr, len); /* dataPtr is aligned */ 35f0bfeaa8Sopenharmony_ci if (err != PM_OK) { 36f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "%{public}s: InitUxPageTable fail, %{public}s", __func__, GetPMStateName(err)); 37f0bfeaa8Sopenharmony_ci free(uxpt_); 38f0bfeaa8Sopenharmony_ci uxpt_ = nullptr; 39f0bfeaa8Sopenharmony_ci } 40f0bfeaa8Sopenharmony_ci} 41f0bfeaa8Sopenharmony_ci 42f0bfeaa8Sopenharmony_ciUxPageTable::~UxPageTable() 43f0bfeaa8Sopenharmony_ci{ 44f0bfeaa8Sopenharmony_ci /* unmap uxpt */ 45f0bfeaa8Sopenharmony_ci if (uxpt_) { 46f0bfeaa8Sopenharmony_ci PMState err = DeinitUxPageTable(uxpt_); 47f0bfeaa8Sopenharmony_ci if (err != PM_OK) { 48f0bfeaa8Sopenharmony_ci HILOG_ERROR(LOG_CORE, "%{public}s: deinit upt fail, %{public}s", __func__, GetPMStateName(err)); 49f0bfeaa8Sopenharmony_ci } else { 50f0bfeaa8Sopenharmony_ci free(uxpt_); 51f0bfeaa8Sopenharmony_ci uxpt_ = nullptr; 52f0bfeaa8Sopenharmony_ci } 53f0bfeaa8Sopenharmony_ci } 54f0bfeaa8Sopenharmony_ci} 55f0bfeaa8Sopenharmony_ci 56f0bfeaa8Sopenharmony_civoid UxPageTable::GetUxpte(uint64_t addr, size_t len) 57f0bfeaa8Sopenharmony_ci{ 58f0bfeaa8Sopenharmony_ci UxpteGet(uxpt_, addr, len); 59f0bfeaa8Sopenharmony_ci} 60f0bfeaa8Sopenharmony_ci 61f0bfeaa8Sopenharmony_civoid UxPageTable::PutUxpte(uint64_t addr, size_t len) 62f0bfeaa8Sopenharmony_ci{ 63f0bfeaa8Sopenharmony_ci UxptePut(uxpt_, addr, len); 64f0bfeaa8Sopenharmony_ci} 65f0bfeaa8Sopenharmony_ci 66f0bfeaa8Sopenharmony_ci 67f0bfeaa8Sopenharmony_cibool UxPageTable::CheckPresent(uint64_t addr, size_t len) 68f0bfeaa8Sopenharmony_ci{ 69f0bfeaa8Sopenharmony_ci return UxpteIsPresent(uxpt_, addr, len); 70f0bfeaa8Sopenharmony_ci} 71f0bfeaa8Sopenharmony_ci 72f0bfeaa8Sopenharmony_cistd::string UxPageTable::ToString() const 73f0bfeaa8Sopenharmony_ci{ 74f0bfeaa8Sopenharmony_ci std::string uxptStr = uxpt_ ? std::to_string((unsigned long long)uxpt_) : "0"; 75f0bfeaa8Sopenharmony_ci return "uxptAddr: " + uxptStr; 76f0bfeaa8Sopenharmony_ci} 77f0bfeaa8Sopenharmony_ci} /* namespace PurgeableMem */ 78f0bfeaa8Sopenharmony_ci} /* namespace OHOS */ 79