blob: 9edd27b600b990c3c2de6f5483ddc961f6715602 [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
2 * Copyright (C) 2005 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
Martijn Coenen4080edc2016-05-04 14:17:02 +020017#include <hwbinder/Binder.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070018
Bailey Forreste20d6f42015-08-18 17:15:10 -070019#include <atomic>
Dianne Hackborn86a50a62012-05-08 18:54:22 -070020#include <utils/misc.h>
Yifan Hong1e118d22017-01-12 14:42:28 -080021#include <hwbinder/BpHwBinder.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020022#include <hwbinder/IInterface.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020023#include <hwbinder/Parcel.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070024
Steven Morelandd9bdb652019-09-17 15:42:45 -070025#include <linux/sched.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070026#include <stdio.h>
27
28namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020029namespace hardware {
Mathias Agopian7922fa22009-05-18 15:08:03 -070030
31// ---------------------------------------------------------------------------
32
Mathias Agopiana6286c32009-05-22 19:00:22 -070033IBinder::IBinder()
34 : RefBase()
35{
36}
37
38IBinder::~IBinder()
39{
40}
41
42// ---------------------------------------------------------------------------
43
Yifan Hongdde40f32017-01-12 14:22:45 -080044BHwBinder* IBinder::localBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -070045{
Yi Kong55d41072018-07-23 14:55:39 -070046 return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -070047}
48
Yifan Hong1e118d22017-01-12 14:42:28 -080049BpHwBinder* IBinder::remoteBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -070050{
Yi Kong55d41072018-07-23 14:55:39 -070051 return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -070052}
53
54bool IBinder::checkSubclass(const void* /*subclassID*/) const
55{
56 return false;
57}
58
59// ---------------------------------------------------------------------------
60
Yifan Hongdde40f32017-01-12 14:22:45 -080061class BHwBinder::Extras
Mathias Agopian7922fa22009-05-18 15:08:03 -070062{
63public:
Steven Morelandb1a27632019-01-09 18:01:02 -080064 // unlocked objects
65 bool mRequestingSid = false;
66
67 // for below objects
Mathias Agopian7922fa22009-05-18 15:08:03 -070068 Mutex mLock;
Yifan Hong1e118d22017-01-12 14:42:28 -080069 BpHwBinder::ObjectManager mObjects;
Mathias Agopian7922fa22009-05-18 15:08:03 -070070};
71
72// ---------------------------------------------------------------------------
73
Steven Moreland61d78e22019-01-09 19:49:27 +000074BHwBinder::BHwBinder() : mSchedPolicy(SCHED_NORMAL), mSchedPriority(0), mExtras(nullptr)
Mathias Agopian7922fa22009-05-18 15:08:03 -070075{
76}
77
Martijn Coenen3f181282017-05-03 13:45:32 -070078int BHwBinder::getMinSchedulingPolicy() {
79 return mSchedPolicy;
80}
81
82int BHwBinder::getMinSchedulingPriority() {
83 return mSchedPriority;
84}
85
Steven Morelandb1a27632019-01-09 18:01:02 -080086bool BHwBinder::isRequestingSid() {
87 Extras* e = mExtras.load(std::memory_order_acquire);
88
89 return e && e->mRequestingSid;
90}
91
92void BHwBinder::setRequestingSid(bool requestingSid) {
93 Extras* e = mExtras.load(std::memory_order_acquire);
94
95 if (!e) {
96 // default is false. Most things don't need sids, so avoiding allocations when possible.
97 if (!requestingSid) {
98 return;
99 }
100
101 e = getOrCreateExtras();
102 if (!e) return; // out of memory
103 }
104
Steven Moreland3668e032019-02-08 17:56:59 -0800105 e->mRequestingSid = requestingSid;
Steven Morelandb1a27632019-01-09 18:01:02 -0800106}
107
Yifan Hongdde40f32017-01-12 14:22:45 -0800108status_t BHwBinder::transact(
Martijn Coenen79c2f4d2016-05-20 10:55:59 +0200109 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags, TransactCallback callback)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700110{
111 data.setDataPosition(0);
112
113 status_t err = NO_ERROR;
114 switch (code) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700115 default:
Martijn Coenen79c2f4d2016-05-20 10:55:59 +0200116 err = onTransact(code, data, reply, flags,
117 [&](auto &replyParcel) {
118 replyParcel.setDataPosition(0);
Yi Kong55d41072018-07-23 14:55:39 -0700119 if (callback != nullptr) {
Martijn Coenen79c2f4d2016-05-20 10:55:59 +0200120 callback(replyParcel);
121 }
122 });
Mathias Agopian7922fa22009-05-18 15:08:03 -0700123 break;
124 }
125
Mathias Agopian7922fa22009-05-18 15:08:03 -0700126 return err;
127}
128
Yifan Hongdde40f32017-01-12 14:22:45 -0800129status_t BHwBinder::linkToDeath(
Colin Crossf0487982014-02-05 17:42:44 -0800130 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
131 uint32_t /*flags*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700132{
133 return INVALID_OPERATION;
134}
135
Yifan Hongdde40f32017-01-12 14:22:45 -0800136status_t BHwBinder::unlinkToDeath(
Colin Crossf0487982014-02-05 17:42:44 -0800137 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
138 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700139{
140 return INVALID_OPERATION;
141}
142
Yifan Hongdde40f32017-01-12 14:22:45 -0800143void BHwBinder::attachObject(
Mathias Agopian7922fa22009-05-18 15:08:03 -0700144 const void* objectID, void* object, void* cleanupCookie,
145 object_cleanup_func func)
146{
Steven Morelandb1a27632019-01-09 18:01:02 -0800147 Extras* e = getOrCreateExtras();
148 if (!e) return; // out of memory
Mathias Agopian7922fa22009-05-18 15:08:03 -0700149
150 AutoMutex _l(e->mLock);
151 e->mObjects.attach(objectID, object, cleanupCookie, func);
152}
153
Yifan Hongdde40f32017-01-12 14:22:45 -0800154void* BHwBinder::findObject(const void* objectID) const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700155{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700156 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kong55d41072018-07-23 14:55:39 -0700157 if (!e) return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700158
159 AutoMutex _l(e->mLock);
160 return e->mObjects.find(objectID);
161}
162
Yifan Hongdde40f32017-01-12 14:22:45 -0800163void BHwBinder::detachObject(const void* objectID)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700164{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700165 Extras* e = mExtras.load(std::memory_order_acquire);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700166 if (!e) return;
167
168 AutoMutex _l(e->mLock);
169 e->mObjects.detach(objectID);
170}
171
Yifan Hongdde40f32017-01-12 14:22:45 -0800172BHwBinder* BHwBinder::localBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700173{
174 return this;
175}
176
Yifan Hongdde40f32017-01-12 14:22:45 -0800177BHwBinder::~BHwBinder()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700178{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700179 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm99c620a2014-08-12 22:56:00 +0000180 if (e) delete e;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700181}
182
183
Yifan Hongdde40f32017-01-12 14:22:45 -0800184status_t BHwBinder::onTransact(
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100185 uint32_t /*code*/, const Parcel& /*data*/, Parcel* /*reply*/, uint32_t /*flags*/,
186 TransactCallback /*callback*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700187{
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100188 return UNKNOWN_TRANSACTION;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700189}
190
Steven Morelandb1a27632019-01-09 18:01:02 -0800191BHwBinder::Extras* BHwBinder::getOrCreateExtras()
192{
193 Extras* e = mExtras.load(std::memory_order_acquire);
194
195 if (!e) {
196 e = new Extras;
197 Extras* expected = nullptr;
198 if (!mExtras.compare_exchange_strong(expected, e,
199 std::memory_order_release,
200 std::memory_order_acquire)) {
201 delete e;
202 e = expected; // Filled in by CAS
203 }
204 if (e == nullptr) return nullptr; // out of memory
205 }
206
207 return e;
208}
209
Mathias Agopian7922fa22009-05-18 15:08:03 -0700210// ---------------------------------------------------------------------------
211
212enum {
213 // This is used to transfer ownership of the remote binder from
Yifan Honga2ce3b82017-01-12 14:49:26 -0800214 // the BpHwRefBase object holding it (when it is constructed), to the
215 // owner of the BpHwRefBase object when it first acquires that BpHwRefBase.
Mathias Agopian7922fa22009-05-18 15:08:03 -0700216 kRemoteAcquired = 0x00000001
217};
218
Yifan Honga2ce3b82017-01-12 14:49:26 -0800219BpHwRefBase::BpHwRefBase(const sp<IBinder>& o)
Yi Kong55d41072018-07-23 14:55:39 -0700220 : mRemote(o.get()), mRefs(nullptr), mState(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700221{
Mathias Agopian7922fa22009-05-18 15:08:03 -0700222 if (mRemote) {
223 mRemote->incStrong(this); // Removed on first IncStrong().
Mathias Agopian7922fa22009-05-18 15:08:03 -0700224 }
225}
226
Yifan Honga2ce3b82017-01-12 14:49:26 -0800227BpHwRefBase::~BpHwRefBase()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700228{
229 if (mRemote) {
Bailey Forreste20d6f42015-08-18 17:15:10 -0700230 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700231 mRemote->decStrong(this);
232 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700233 }
234}
235
Yifan Honga2ce3b82017-01-12 14:49:26 -0800236void BpHwRefBase::onFirstRef()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700237{
Bailey Forreste20d6f42015-08-18 17:15:10 -0700238 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700239}
240
Yifan Honga2ce3b82017-01-12 14:49:26 -0800241void BpHwRefBase::onLastStrongRef(const void* /*id*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700242{
243 if (mRemote) {
244 mRemote->decStrong(this);
245 }
246}
247
Yifan Honga2ce3b82017-01-12 14:49:26 -0800248bool BpHwRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700249{
Martijn Coenen35a7b1e2017-10-09 10:35:42 +0200250 return false;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700251}
252
253// ---------------------------------------------------------------------------
254
Steven Moreland7173a4c2019-09-26 15:55:02 -0700255} // namespace hardware
256} // namespace android