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 namespace cuttlefish { 25 26 /** 27 * Object slot manager for TPM memory. The TPM can only hold a fixed number of 28 * objects at once. Some TPM operations are defined to consume slots either 29 * temporarily or until the resource is explicitly unloaded. 30 * 31 * This implementation is intended for future extension, to track what objects 32 * are resident if we run out of space, or implement optimizations like LRU 33 * caching to avoid re-loading often-used resources. 34 */ 35 class TpmResourceManager { 36 public: 37 class ObjectSlot { 38 public: 39 friend class TpmResourceManager; 40 41 ~ObjectSlot(); 42 43 ESYS_TR get(); 44 void set(ESYS_TR resource); 45 private: 46 ObjectSlot(TpmResourceManager* resource_manager); 47 ObjectSlot(TpmResourceManager* resource_manager, ESYS_TR resource); 48 49 TpmResourceManager* resource_manager_; 50 ESYS_TR resource_; 51 }; 52 53 TpmResourceManager(ESYS_CONTEXT* esys); 54 ~TpmResourceManager(); 55 56 ESYS_CONTEXT* Esys(); 57 std::shared_ptr<ObjectSlot> ReserveSlot(); 58 private: 59 ESYS_CONTEXT* esys_; 60 const std::uint32_t maximum_object_slots_; 61 std::atomic<std::uint32_t> used_slots_; 62 }; 63 64 using TpmObjectSlot = std::shared_ptr<TpmResourceManager::ObjectSlot>; 65 66 } // namespace cuttlefish 67