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