blob: 99df06d57dfaa19e644a7bcdd64ad54313c9d2f1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17#define LOG_TAG "IMemory"
18
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
24
25#include <sys/types.h>
26#include <sys/mman.h>
27
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/IMemory.h>
Christopher Tate94b0d4e2016-02-05 19:02:56 -080029#include <cutils/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <utils/KeyedVector.h>
31#include <utils/threads.h>
32#include <utils/Atomic.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/CallStack.h>
35
36#define VERBOSE 0
37
38namespace android {
39// ---------------------------------------------------------------------------
40
41class HeapCache : public IBinder::DeathRecipient
42{
43public:
44 HeapCache();
45 virtual ~HeapCache();
Anu Sundararajan5728a922011-06-22 15:58:59 -050046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 virtual void binderDied(const wp<IBinder>& who);
48
Anu Sundararajan5728a922011-06-22 15:58:59 -050049 sp<IMemoryHeap> find_heap(const sp<IBinder>& binder);
50 void free_heap(const sp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 sp<IMemoryHeap> get_heap(const sp<IBinder>& binder);
52 void dump_heaps();
53
54private:
55 // For IMemory.cpp
56 struct heap_info_t {
57 sp<IMemoryHeap> heap;
58 int32_t count;
59 };
60
Anu Sundararajan5728a922011-06-22 15:58:59 -050061 void free_heap(const wp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63 Mutex mHeapCacheLock;
64 KeyedVector< wp<IBinder>, heap_info_t > mHeapCache;
65};
66
67static sp<HeapCache> gHeapCache = new HeapCache();
68
69/******************************************************************************/
70
71enum {
72 HEAP_ID = IBinder::FIRST_CALL_TRANSACTION
73};
74
75class BpMemoryHeap : public BpInterface<IMemoryHeap>
76{
77public:
78 BpMemoryHeap(const sp<IBinder>& impl);
79 virtual ~BpMemoryHeap();
80
81 virtual int getHeapID() const;
82 virtual void* getBase() const;
83 virtual size_t getSize() const;
84 virtual uint32_t getFlags() const;
Anu Sundararajan5728a922011-06-22 15:58:59 -050085 virtual uint32_t getOffset() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086
87private:
88 friend class IMemory;
89 friend class HeapCache;
Anu Sundararajan5728a922011-06-22 15:58:59 -050090
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 // for debugging in this module
92 static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) {
93 return gHeapCache->find_heap(binder);
94 }
95 static inline void free_heap(const sp<IBinder>& binder) {
96 gHeapCache->free_heap(binder);
97 }
98 static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) {
99 return gHeapCache->get_heap(binder);
100 }
101 static inline void dump_heaps() {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500102 gHeapCache->dump_heaps();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104
105 void assertMapped() const;
106 void assertReallyMapped() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 mutable volatile int32_t mHeapId;
109 mutable void* mBase;
110 mutable size_t mSize;
111 mutable uint32_t mFlags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500112 mutable uint32_t mOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 mutable bool mRealHeap;
114 mutable Mutex mLock;
115};
116
117// ----------------------------------------------------------------------------
118
119enum {
120 GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION
121};
122
123class BpMemory : public BpInterface<IMemory>
124{
125public:
126 BpMemory(const sp<IBinder>& impl);
127 virtual ~BpMemory();
128 virtual sp<IMemoryHeap> getMemory(ssize_t* offset=0, size_t* size=0) const;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130private:
131 mutable sp<IMemoryHeap> mHeap;
132 mutable ssize_t mOffset;
133 mutable size_t mSize;
134};
135
136/******************************************************************************/
137
138void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const
139{
140 sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder);
141 void* const base = realHeap->base();
142 if (base == MAP_FAILED)
143 return 0;
144 return static_cast<char*>(base) + offset;
145}
146
147void* IMemory::pointer() const {
148 ssize_t offset;
149 sp<IMemoryHeap> heap = getMemory(&offset);
150 void* const base = heap!=0 ? heap->base() : MAP_FAILED;
151 if (base == MAP_FAILED)
152 return 0;
153 return static_cast<char*>(base) + offset;
154}
155
156size_t IMemory::size() const {
157 size_t size;
158 getMemory(NULL, &size);
159 return size;
160}
161
162ssize_t IMemory::offset() const {
163 ssize_t offset;
164 getMemory(&offset);
165 return offset;
166}
167
168/******************************************************************************/
169
170BpMemory::BpMemory(const sp<IBinder>& impl)
171 : BpInterface<IMemory>(impl), mOffset(0), mSize(0)
172{
173}
174
175BpMemory::~BpMemory()
176{
177}
178
179sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const
180{
181 if (mHeap == 0) {
182 Parcel data, reply;
183 data.writeInterfaceToken(IMemory::getInterfaceDescriptor());
184 if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) {
185 sp<IBinder> heap = reply.readStrongBinder();
186 ssize_t o = reply.readInt32();
187 size_t s = reply.readInt32();
188 if (heap != 0) {
189 mHeap = interface_cast<IMemoryHeap>(heap);
190 if (mHeap != 0) {
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800191 size_t heapSize = mHeap->getSize();
192 if (s <= heapSize
193 && o >= 0
194 && (static_cast<size_t>(o) <= heapSize - s)) {
195 mOffset = o;
196 mSize = s;
197 } else {
198 // Hm.
199 android_errorWriteWithInfoLog(0x534e4554,
200 "26877992", -1, NULL, 0);
201 mOffset = 0;
202 mSize = 0;
203 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 }
205 }
206 }
207 }
208 if (offset) *offset = mOffset;
209 if (size) *size = mSize;
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800210 return (mSize > 0) ? mHeap : 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
213// ---------------------------------------------------------------------------
214
215IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory");
216
Mathias Agopian83c04462009-05-22 19:00:22 -0700217BnMemory::BnMemory() {
218}
219
Anu Sundararajan5728a922011-06-22 15:58:59 -0500220BnMemory::~BnMemory() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700221}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222
223status_t BnMemory::onTransact(
224 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
225{
226 switch(code) {
227 case GET_MEMORY: {
228 CHECK_INTERFACE(IMemory, data, reply);
229 ssize_t offset;
230 size_t size;
231 reply->writeStrongBinder( getMemory(&offset, &size)->asBinder() );
232 reply->writeInt32(offset);
233 reply->writeInt32(size);
234 return NO_ERROR;
235 } break;
236 default:
237 return BBinder::onTransact(code, data, reply, flags);
238 }
239}
240
241
242/******************************************************************************/
243
244BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl)
245 : BpInterface<IMemoryHeap>(impl),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500246 mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247{
248}
249
250BpMemoryHeap::~BpMemoryHeap() {
251 if (mHeapId != -1) {
252 close(mHeapId);
253 if (mRealHeap) {
254 // by construction we're the last one
255 if (mBase != MAP_FAILED) {
256 sp<IBinder> binder = const_cast<BpMemoryHeap*>(this)->asBinder();
257
258 if (VERBOSE) {
Steve Block9d453682011-12-20 16:23:08 +0000259 ALOGD("UNMAPPING binder=%p, heap=%p, size=%d, fd=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 binder.get(), this, mSize, mHeapId);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700261 CallStack stack(LOG_TAG);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 }
263
264 munmap(mBase, mSize);
265 }
266 } else {
267 // remove from list only if it was mapped before
268 sp<IBinder> binder = const_cast<BpMemoryHeap*>(this)->asBinder();
269 free_heap(binder);
270 }
271 }
272}
273
274void BpMemoryHeap::assertMapped() const
275{
276 if (mHeapId == -1) {
277 sp<IBinder> binder(const_cast<BpMemoryHeap*>(this)->asBinder());
278 sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get()));
279 heap->assertReallyMapped();
280 if (heap->mBase != MAP_FAILED) {
281 Mutex::Autolock _l(mLock);
282 if (mHeapId == -1) {
283 mBase = heap->mBase;
284 mSize = heap->mSize;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500285 mOffset = heap->mOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286 android_atomic_write( dup( heap->mHeapId ), &mHeapId );
287 }
288 } else {
289 // something went wrong
290 free_heap(binder);
291 }
292 }
293}
294
295void BpMemoryHeap::assertReallyMapped() const
296{
297 if (mHeapId == -1) {
298
299 // remote call without mLock held, worse case scenario, we end up
300 // calling transact() from multiple threads, but that's not a problem,
301 // only mmap below must be in the critical section.
Anu Sundararajan5728a922011-06-22 15:58:59 -0500302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 Parcel data, reply;
304 data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor());
305 status_t err = remote()->transact(HEAP_ID, data, &reply);
306 int parcel_fd = reply.readFileDescriptor();
307 ssize_t size = reply.readInt32();
308 uint32_t flags = reply.readInt32();
Anu Sundararajan5728a922011-06-22 15:58:59 -0500309 uint32_t offset = reply.readInt32();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Elliott Hughesc47f0982013-03-14 02:56:34 +0000311 ALOGE_IF(err, "binder=%p transaction failed fd=%d, size=%ld, err=%d (%s)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 asBinder().get(), parcel_fd, size, err, strerror(-err));
313
314 int fd = dup( parcel_fd );
Elliott Hughesc47f0982013-03-14 02:56:34 +0000315 ALOGE_IF(fd==-1, "cannot dup fd=%d, size=%ld, err=%d (%s)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 parcel_fd, size, err, strerror(errno));
317
318 int access = PROT_READ;
319 if (!(flags & READ_ONLY)) {
320 access |= PROT_WRITE;
321 }
322
323 Mutex::Autolock _l(mLock);
324 if (mHeapId == -1) {
325 mRealHeap = true;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500326 mBase = mmap(0, size, access, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 if (mBase == MAP_FAILED) {
Elliott Hughesc47f0982013-03-14 02:56:34 +0000328 ALOGE("cannot map BpMemoryHeap (binder=%p), size=%ld, fd=%d (%s)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 asBinder().get(), size, fd, strerror(errno));
330 close(fd);
331 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 mSize = size;
333 mFlags = flags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500334 mOffset = offset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 android_atomic_write(fd, &mHeapId);
336 }
337 }
338 }
339}
340
341int BpMemoryHeap::getHeapID() const {
342 assertMapped();
343 return mHeapId;
344}
345
346void* BpMemoryHeap::getBase() const {
347 assertMapped();
348 return mBase;
349}
350
351size_t BpMemoryHeap::getSize() const {
352 assertMapped();
353 return mSize;
354}
355
356uint32_t BpMemoryHeap::getFlags() const {
357 assertMapped();
358 return mFlags;
359}
360
Anu Sundararajan5728a922011-06-22 15:58:59 -0500361uint32_t BpMemoryHeap::getOffset() const {
362 assertMapped();
363 return mOffset;
364}
365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366// ---------------------------------------------------------------------------
367
368IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap");
369
Anu Sundararajan5728a922011-06-22 15:58:59 -0500370BnMemoryHeap::BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700371}
372
Anu Sundararajan5728a922011-06-22 15:58:59 -0500373BnMemoryHeap::~BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700374}
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376status_t BnMemoryHeap::onTransact(
Mathias Agopian83c04462009-05-22 19:00:22 -0700377 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378{
379 switch(code) {
380 case HEAP_ID: {
381 CHECK_INTERFACE(IMemoryHeap, data, reply);
382 reply->writeFileDescriptor(getHeapID());
383 reply->writeInt32(getSize());
384 reply->writeInt32(getFlags());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500385 reply->writeInt32(getOffset());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 return NO_ERROR;
387 } break;
388 default:
389 return BBinder::onTransact(code, data, reply, flags);
390 }
391}
392
393/*****************************************************************************/
394
395HeapCache::HeapCache()
396 : DeathRecipient()
397{
398}
399
400HeapCache::~HeapCache()
401{
402}
403
404void HeapCache::binderDied(const wp<IBinder>& binder)
405{
Steve Block9d453682011-12-20 16:23:08 +0000406 //ALOGD("binderDied binder=%p", binder.unsafe_get());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500407 free_heap(binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
Anu Sundararajan5728a922011-06-22 15:58:59 -0500410sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411{
412 Mutex::Autolock _l(mHeapCacheLock);
413 ssize_t i = mHeapCache.indexOfKey(binder);
414 if (i>=0) {
415 heap_info_t& info = mHeapCache.editValueAt(i);
Steve Block9d453682011-12-20 16:23:08 +0000416 ALOGD_IF(VERBOSE,
Anu Sundararajan5728a922011-06-22 15:58:59 -0500417 "found binder=%p, heap=%p, size=%d, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 binder.get(), info.heap.get(),
419 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
420 static_cast<BpMemoryHeap*>(info.heap.get())->mHeapId,
421 info.count);
422 android_atomic_inc(&info.count);
423 return info.heap;
424 } else {
425 heap_info_t info;
426 info.heap = interface_cast<IMemoryHeap>(binder);
427 info.count = 1;
Steve Block9d453682011-12-20 16:23:08 +0000428 //ALOGD("adding binder=%p, heap=%p, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 // binder.get(), info.heap.get(), info.count);
430 mHeapCache.add(binder, info);
431 return info.heap;
432 }
433}
434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435void HeapCache::free_heap(const sp<IBinder>& binder) {
436 free_heap( wp<IBinder>(binder) );
437}
438
Anu Sundararajan5728a922011-06-22 15:58:59 -0500439void HeapCache::free_heap(const wp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440{
441 sp<IMemoryHeap> rel;
442 {
443 Mutex::Autolock _l(mHeapCacheLock);
444 ssize_t i = mHeapCache.indexOfKey(binder);
445 if (i>=0) {
446 heap_info_t& info(mHeapCache.editValueAt(i));
447 int32_t c = android_atomic_dec(&info.count);
448 if (c == 1) {
Steve Block9d453682011-12-20 16:23:08 +0000449 ALOGD_IF(VERBOSE,
Anu Sundararajan5728a922011-06-22 15:58:59 -0500450 "removing binder=%p, heap=%p, size=%d, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 binder.unsafe_get(), info.heap.get(),
452 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
453 static_cast<BpMemoryHeap*>(info.heap.get())->mHeapId,
454 info.count);
455 rel = mHeapCache.valueAt(i).heap;
456 mHeapCache.removeItemsAt(i);
457 }
458 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000459 ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 }
461 }
462}
463
464sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder)
465{
466 sp<IMemoryHeap> realHeap;
467 Mutex::Autolock _l(mHeapCacheLock);
468 ssize_t i = mHeapCache.indexOfKey(binder);
469 if (i>=0) realHeap = mHeapCache.valueAt(i).heap;
470 else realHeap = interface_cast<IMemoryHeap>(binder);
471 return realHeap;
472}
473
Anu Sundararajan5728a922011-06-22 15:58:59 -0500474void HeapCache::dump_heaps()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475{
476 Mutex::Autolock _l(mHeapCacheLock);
477 int c = mHeapCache.size();
478 for (int i=0 ; i<c ; i++) {
479 const heap_info_t& info = mHeapCache.valueAt(i);
480 BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get()));
Steve Block9d453682011-12-20 16:23:08 +0000481 ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%d)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 mHeapCache.keyAt(i).unsafe_get(),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500483 info.heap.get(), info.count,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484 h->mHeapId, h->mBase, h->mSize);
485 }
486}
487
488
489// ---------------------------------------------------------------------------
490}; // namespace android