Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_PROCESS_STATE_H |
| 18 | #define ANDROID_HARDWARE_PROCESS_STATE_H |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 20 | #include <hwbinder/IBinder.h> |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <utils/KeyedVector.h> |
| 22 | #include <utils/String8.h> |
| 23 | #include <utils/String16.h> |
| 24 | |
| 25 | #include <utils/threads.h> |
| 26 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 27 | #include <pthread.h> |
| 28 | |
Steven Moreland | b5f6e00 | 2021-02-26 01:51:20 +0000 | [diff] [blame] | 29 | // WARNING: this code is part of libhwbinder, a fork of libbinder. Generally, |
| 30 | // this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific |
| 31 | // code should not try to use these things. |
| 32 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | // --------------------------------------------------------------------------- |
| 34 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 35 | namespace hardware { |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 36 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 37 | class IPCThreadState; |
| 38 | |
| 39 | class ProcessState : public virtual RefBase |
| 40 | { |
| 41 | public: |
| 42 | static sp<ProcessState> self(); |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 43 | static sp<ProcessState> selfOrNull(); |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 44 | // Note: don't call self() or selfOrNull() before initWithMmapSize() |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 45 | // with '0' as an argument, this is the same as selfOrNull |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 46 | static sp<ProcessState> initWithMmapSize(size_t mmapSize); // size in bytes |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 47 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 48 | void startThreadPool(); |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 49 | |
Steven Moreland | e9b4f91 | 2020-11-05 00:25:37 +0000 | [diff] [blame] | 50 | sp<IBinder> getContextObject(const sp<IBinder>& /*caller*/); |
| 51 | // only call once, without creating a pool |
| 52 | void becomeContextManager(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 53 | |
| 54 | sp<IBinder> getStrongProxyForHandle(int32_t handle); |
| 55 | wp<IBinder> getWeakProxyForHandle(int32_t handle); |
| 56 | void expungeHandle(int32_t handle, IBinder* binder); |
| 57 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 58 | void spawnPooledThread(bool isMain); |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 59 | |
| 60 | status_t setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool); |
Hang Lu | d1388da | 2021-03-24 14:30:06 +0800 | [diff] [blame] | 61 | status_t enableOnewaySpamDetection(bool enable); |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 62 | size_t getMaxThreads(); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 63 | void giveThreadPoolName(); |
Mathias Agopian | 8297f50 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 64 | |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 65 | ssize_t getKernelReferences(size_t count, uintptr_t* buf); |
Martijn Coenen | 320e1d3 | 2018-09-07 10:41:33 +0200 | [diff] [blame] | 66 | // This refcount includes: |
| 67 | // 1. Strong references to the node by this and other processes |
| 68 | // 2. Temporary strong references held by the kernel during a |
| 69 | // transaction on the node. |
| 70 | // It does NOT include local strong references to the node |
| 71 | ssize_t getStrongRefCountForNodeByHandle(int32_t handle); |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 72 | size_t getMmapSize(); |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 73 | |
| 74 | enum class CallRestriction { |
| 75 | // all calls okay |
| 76 | NONE, |
| 77 | // log when calls are blocking |
| 78 | ERROR_IF_NOT_ONEWAY, |
| 79 | // abort process on blocking calls |
| 80 | FATAL_IF_NOT_ONEWAY, |
| 81 | }; |
| 82 | // Sets calling restrictions for all transactions in this process. This must be called |
| 83 | // before any threads are spawned. |
| 84 | void setCallRestriction(CallRestriction restriction); |
| 85 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 86 | private: |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 87 | static sp<ProcessState> init(size_t mmapSize, bool requireMmapSize); |
| 88 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 89 | friend class IPCThreadState; |
Steven Moreland | f050d28 | 2020-07-21 02:42:12 +0000 | [diff] [blame] | 90 | explicit ProcessState(size_t mmapSize); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 91 | ~ProcessState(); |
| 92 | |
| 93 | ProcessState(const ProcessState& o); |
| 94 | ProcessState& operator=(const ProcessState& o); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 95 | String8 makeBinderThreadName(); |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 96 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 97 | struct handle_entry { |
| 98 | IBinder* binder; |
| 99 | RefBase::weakref_type* refs; |
| 100 | }; |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 101 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 102 | handle_entry* lookupHandleLocked(int32_t handle); |
| 103 | |
| 104 | int mDriverFD; |
| 105 | void* mVMStart; |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 106 | |
| 107 | // Protects thread count variable below. |
| 108 | pthread_mutex_t mThreadCountLock; |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 109 | // Number of binder threads current executing a command. |
| 110 | size_t mExecutingThreadsCount; |
| 111 | // Maximum number for binder threads allowed for this process. |
| 112 | size_t mMaxThreads; |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 113 | // Time when thread pool was emptied |
| 114 | int64_t mStarvationStartTimeMs; |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 115 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 116 | mutable Mutex mLock; // protects everything below. |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 117 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 118 | Vector<handle_entry>mHandleToObject; |
| 119 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 120 | String8 mRootDir; |
| 121 | bool mThreadPoolStarted; |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 122 | bool mSpawnThreadOnStart; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 123 | volatile int32_t mThreadPoolSeq; |
Martijn Coenen | 418856e | 2018-03-27 09:28:07 +0200 | [diff] [blame] | 124 | const size_t mMmapSize; |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 125 | |
| 126 | CallRestriction mCallRestriction; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 127 | }; |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 128 | |
Steven Moreland | 7173a4c | 2019-09-26 15:55:02 -0700 | [diff] [blame] | 129 | } // namespace hardware |
| 130 | } // namespace android |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 131 | |
| 132 | // --------------------------------------------------------------------------- |
| 133 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 134 | #endif // ANDROID_HARDWARE_PROCESS_STATE_H |