1 // 2 // Copyright (C) 2020 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #pragma once 17 18 #include <cstdint> 19 #include <memory> 20 #include <set> 21 22 #include <tss2/tss2_esys.h> 23 24 /** 25 * Object slot manager for TPM memory. The TPM can only hold a fixed number of 26 * objects at once. Some TPM operations are defined to consume slots either 27 * temporarily or until the resource is explicitly unloaded. 28 * 29 * This implementation is intended for future extension, to track what objects 30 * are resident if we run out of space, or implement optimizations like LRU 31 * caching to avoid re-loading often-used resources. 32 */ 33 class TpmResourceManager { 34 public: 35 class ObjectSlot { 36 public: 37 friend class TpmResourceManager; 38 39 ~ObjectSlot(); 40 41 ESYS_TR get(); 42 void set(ESYS_TR resource); 43 private: 44 ObjectSlot(TpmResourceManager* resource_manager); 45 ObjectSlot(TpmResourceManager* resource_manager, ESYS_TR resource); 46 47 TpmResourceManager* resource_manager_; 48 ESYS_TR resource_; 49 }; 50 51 TpmResourceManager(ESYS_CONTEXT* esys); 52 ~TpmResourceManager(); 53 54 ESYS_CONTEXT* Esys(); 55 std::shared_ptr<ObjectSlot> ReserveSlot(); 56 private: 57 ESYS_CONTEXT* esys_; 58 const std::uint32_t maximum_object_slots_; 59 std::atomic<std::uint32_t> used_slots_; 60 }; 61 62 using TpmObjectSlot = std::shared_ptr<TpmResourceManager::ObjectSlot>; 63