Android-cuttlefish cvd tool
tpm_resource_manager.h
Go to the documentation of this file.
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 <atomic>
19#include <cstdint>
20#include <memory>
21#include <mutex>
22#include <set>
23
24#include <tss2/tss2_esys.h>
25
26namespace cuttlefish {
27
28class EsysLock {
29 public:
30 ESYS_CONTEXT* operator*() const { return esys_; }
31
32 private:
33 EsysLock(ESYS_CONTEXT*, std::unique_lock<std::mutex>);
34
35 ESYS_CONTEXT* esys_;
36 std::unique_lock<std::mutex> guard_;
37
38 friend class TpmResourceManager;
39};
40
51 public:
52 class ObjectSlot {
53 public:
54 friend class TpmResourceManager;
55
57
58 ESYS_TR get();
59 void set(ESYS_TR resource);
60 private:
61 ObjectSlot(TpmResourceManager* resource_manager);
62 ObjectSlot(TpmResourceManager* resource_manager, ESYS_TR resource);
63
65 ESYS_TR resource_;
66 };
67
68 TpmResourceManager(ESYS_CONTEXT* esys);
70
71 // Returns a wrapped ESYS_CONTEXT* that can be used with Esys calls that also
72 // holds a lock. Callers should not hold onto the inner ESYS_CONTEXT* past the
73 // lifetime of the lock.
74 EsysLock Esys();
75 std::shared_ptr<ObjectSlot> ReserveSlot();
76
77 private:
78 std::mutex mu_;
79 ESYS_CONTEXT* esys_;
80 const std::uint32_t maximum_object_slots_;
81 std::atomic<std::uint32_t> used_slots_;
82};
83
84using TpmObjectSlot = std::shared_ptr<TpmResourceManager::ObjectSlot>;
85
86} // namespace cuttlefish
Definition: tpm_resource_manager.h:28
std::unique_lock< std::mutex > guard_
Definition: tpm_resource_manager.h:36
ESYS_CONTEXT * esys_
Definition: tpm_resource_manager.h:35
ESYS_CONTEXT * operator*() const
Definition: tpm_resource_manager.h:30
EsysLock(ESYS_CONTEXT *, std::unique_lock< std::mutex >)
Definition: tpm_resource_manager.cpp:26
Definition: tpm_resource_manager.h:52
ESYS_TR resource_
Definition: tpm_resource_manager.h:65
void set(ESYS_TR resource)
Definition: tpm_resource_manager.cpp:57
ESYS_TR get()
Definition: tpm_resource_manager.cpp:53
TpmResourceManager * resource_manager_
Definition: tpm_resource_manager.h:64
~ObjectSlot()
Definition: tpm_resource_manager.cpp:39
ObjectSlot(TpmResourceManager *resource_manager)
Definition: tpm_resource_manager.cpp:29
Definition: tpm_resource_manager.h:50
TpmResourceManager(ESYS_CONTEXT *esys)
Definition: tpm_resource_manager.cpp:61
std::atomic< std::uint32_t > used_slots_
Definition: tpm_resource_manager.h:81
ESYS_CONTEXT * esys_
Definition: tpm_resource_manager.h:79
const std::uint32_t maximum_object_slots_
Definition: tpm_resource_manager.h:80
~TpmResourceManager()
Definition: tpm_resource_manager.cpp:67
EsysLock Esys()
Definition: tpm_resource_manager.cpp:74
std::mutex mu_
Definition: tpm_resource_manager.h:78
std::shared_ptr< ObjectSlot > ReserveSlot()
Definition: tpm_resource_manager.cpp:78
Definition: alloc_utils.cpp:23
std::shared_ptr< TpmResourceManager::ObjectSlot > TpmObjectSlot
Definition: tpm_resource_manager.h:84