1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
29*bbb1b6f9SApple OSS Distributions
30*bbb1b6f9SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*bbb1b6f9SApple OSS Distributions
32*bbb1b6f9SApple OSS Distributions #include <string.h>
33*bbb1b6f9SApple OSS Distributions #include <sys/cdefs.h>
34*bbb1b6f9SApple OSS Distributions
35*bbb1b6f9SApple OSS Distributions #include <kern/bits.h>
36*bbb1b6f9SApple OSS Distributions #include <kern/locks.h>
37*bbb1b6f9SApple OSS Distributions #include <kern/smr_hash.h>
38*bbb1b6f9SApple OSS Distributions #include <kern/thread_call.h>
39*bbb1b6f9SApple OSS Distributions
40*bbb1b6f9SApple OSS Distributions #if defined(__arm64__)
41*bbb1b6f9SApple OSS Distributions #include <arm64/amcc_rorgn.h> /* rorgn_contains */
42*bbb1b6f9SApple OSS Distributions #endif
43*bbb1b6f9SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
44*bbb1b6f9SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
45*bbb1b6f9SApple OSS Distributions #include <libkern/c++/OSLib.h>
46*bbb1b6f9SApple OSS Distributions #include <os/cpp_util.h>
47*bbb1b6f9SApple OSS Distributions #include <os/hash.h>
48*bbb1b6f9SApple OSS Distributions #include <string.h>
49*bbb1b6f9SApple OSS Distributions
50*bbb1b6f9SApple OSS Distributions static ZONE_DEFINE(OSSymbol_zone, "iokit.OSSymbol", sizeof(OSSymbol), ZC_NONE);
51*bbb1b6f9SApple OSS Distributions static LCK_GRP_DECLARE(lock_group, "OSSymbolPool");
52*bbb1b6f9SApple OSS Distributions
53*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic push
54*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Winvalid-offsetof"
55*bbb1b6f9SApple OSS Distributions
56*bbb1b6f9SApple OSS Distributions /*
57*bbb1b6f9SApple OSS Distributions * This implements a relativistic hash table, using <kern/smr.h> as underlying
58*bbb1b6f9SApple OSS Distributions * safe memory reclamation scheme.
59*bbb1b6f9SApple OSS Distributions *
60*bbb1b6f9SApple OSS Distributions * (https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf)
61*bbb1b6f9SApple OSS Distributions *
62*bbb1b6f9SApple OSS Distributions * One twist is that the OSSymbol_smr_free() callback must be
63*bbb1b6f9SApple OSS Distributions * preemption-disabled safe, which means the `kfree_data()` it calls _MUST_ be
64*bbb1b6f9SApple OSS Distributions * smaller than KALLOC_SAFE_ALLOC_SIZE. To deal with that, if a Symbol is made
65*bbb1b6f9SApple OSS Distributions * with a string that is much larger (should be rare), these go on a lock-based
66*bbb1b6f9SApple OSS Distributions * "huge" queue.
67*bbb1b6f9SApple OSS Distributions */
68*bbb1b6f9SApple OSS Distributions class OSSymbolPool
69*bbb1b6f9SApple OSS Distributions {
70*bbb1b6f9SApple OSS Distributions /* empirically most devices have at least 10+k symbols */
71*bbb1b6f9SApple OSS Distributions static constexpr uint32_t MIN_SIZE = 4096;
72*bbb1b6f9SApple OSS Distributions
73*bbb1b6f9SApple OSS Distributions static inline smrh_key_t
OSSymbol_get_key(const OSSymbol * sym)74*bbb1b6f9SApple OSS Distributions OSSymbol_get_key(const OSSymbol *sym)
75*bbb1b6f9SApple OSS Distributions {
76*bbb1b6f9SApple OSS Distributions return {
77*bbb1b6f9SApple OSS Distributions .smrk_string = sym->string,
78*bbb1b6f9SApple OSS Distributions .smrk_len = (size_t)(sym->length - 1)
79*bbb1b6f9SApple OSS Distributions };
80*bbb1b6f9SApple OSS Distributions }
81*bbb1b6f9SApple OSS Distributions
82*bbb1b6f9SApple OSS Distributions static uint32_t
OSSymbol_obj_hash(const struct smrq_slink * link,uint32_t seed)83*bbb1b6f9SApple OSS Distributions OSSymbol_obj_hash(const struct smrq_slink *link, uint32_t seed)
84*bbb1b6f9SApple OSS Distributions {
85*bbb1b6f9SApple OSS Distributions OSSymbol *sym = __container_of(link, OSSymbol, hashlink);
86*bbb1b6f9SApple OSS Distributions
87*bbb1b6f9SApple OSS Distributions return smrh_key_hash_str(OSSymbol_get_key(sym), seed);
88*bbb1b6f9SApple OSS Distributions }
89*bbb1b6f9SApple OSS Distributions
90*bbb1b6f9SApple OSS Distributions static bool
OSSymbol_obj_equ(const struct smrq_slink * link,smrh_key_t key)91*bbb1b6f9SApple OSS Distributions OSSymbol_obj_equ(const struct smrq_slink *link, smrh_key_t key)
92*bbb1b6f9SApple OSS Distributions {
93*bbb1b6f9SApple OSS Distributions OSSymbol *sym = __container_of(link, OSSymbol, hashlink);
94*bbb1b6f9SApple OSS Distributions
95*bbb1b6f9SApple OSS Distributions return smrh_key_equ_str(OSSymbol_get_key(sym), key);
96*bbb1b6f9SApple OSS Distributions }
97*bbb1b6f9SApple OSS Distributions
98*bbb1b6f9SApple OSS Distributions static bool
OSSymbol_obj_try_get(void * obj)99*bbb1b6f9SApple OSS Distributions OSSymbol_obj_try_get(void *obj)
100*bbb1b6f9SApple OSS Distributions {
101*bbb1b6f9SApple OSS Distributions OSSymbol *sym = (OSSymbol *)obj;
102*bbb1b6f9SApple OSS Distributions
103*bbb1b6f9SApple OSS Distributions return (sym->flags & kOSSSymbolPermanent) ||
104*bbb1b6f9SApple OSS Distributions sym->taggedTryRetain(nullptr);
105*bbb1b6f9SApple OSS Distributions }
106*bbb1b6f9SApple OSS Distributions
107*bbb1b6f9SApple OSS Distributions SMRH_TRAITS_DEFINE_STR(hash_traits, OSSymbol, hashlink,
108*bbb1b6f9SApple OSS Distributions .domain = &smr_iokit,
109*bbb1b6f9SApple OSS Distributions .obj_hash = OSSymbol_obj_hash,
110*bbb1b6f9SApple OSS Distributions .obj_equ = OSSymbol_obj_equ,
111*bbb1b6f9SApple OSS Distributions .obj_try_get = OSSymbol_obj_try_get);
112*bbb1b6f9SApple OSS Distributions
113*bbb1b6f9SApple OSS Distributions mutable lck_mtx_t _mutex;
114*bbb1b6f9SApple OSS Distributions struct smr_hash _hash;
115*bbb1b6f9SApple OSS Distributions smrq_slist_head _huge_head;
116*bbb1b6f9SApple OSS Distributions thread_call_t _tcall;
117*bbb1b6f9SApple OSS Distributions uint32_t _hugeCount = 0;
118*bbb1b6f9SApple OSS Distributions bool _tcallScheduled;
119*bbb1b6f9SApple OSS Distributions
120*bbb1b6f9SApple OSS Distributions private:
121*bbb1b6f9SApple OSS Distributions
122*bbb1b6f9SApple OSS Distributions inline void
lock() const123*bbb1b6f9SApple OSS Distributions lock() const
124*bbb1b6f9SApple OSS Distributions {
125*bbb1b6f9SApple OSS Distributions lck_mtx_lock(&_mutex);
126*bbb1b6f9SApple OSS Distributions }
127*bbb1b6f9SApple OSS Distributions
128*bbb1b6f9SApple OSS Distributions inline void
unlock() const129*bbb1b6f9SApple OSS Distributions unlock() const
130*bbb1b6f9SApple OSS Distributions {
131*bbb1b6f9SApple OSS Distributions lck_mtx_unlock(&_mutex);
132*bbb1b6f9SApple OSS Distributions }
133*bbb1b6f9SApple OSS Distributions
134*bbb1b6f9SApple OSS Distributions inline bool
shouldShrink() const135*bbb1b6f9SApple OSS Distributions shouldShrink() const
136*bbb1b6f9SApple OSS Distributions {
137*bbb1b6f9SApple OSS Distributions /* shrink if there are more than 2 buckets per 1 symbol */
138*bbb1b6f9SApple OSS Distributions return smr_hash_serialized_should_shrink(&_hash, MIN_SIZE, 2, 1);
139*bbb1b6f9SApple OSS Distributions }
140*bbb1b6f9SApple OSS Distributions
141*bbb1b6f9SApple OSS Distributions inline bool
shouldGrow() const142*bbb1b6f9SApple OSS Distributions shouldGrow() const
143*bbb1b6f9SApple OSS Distributions {
144*bbb1b6f9SApple OSS Distributions /* shrink if there less more than 1 bucket per 4 symbol */
145*bbb1b6f9SApple OSS Distributions return smr_hash_serialized_should_grow(&_hash, 1, 4);
146*bbb1b6f9SApple OSS Distributions }
147*bbb1b6f9SApple OSS Distributions
148*bbb1b6f9SApple OSS Distributions public:
149*bbb1b6f9SApple OSS Distributions
150*bbb1b6f9SApple OSS Distributions static void rehash(thread_call_param_t, thread_call_param_t);
151*bbb1b6f9SApple OSS Distributions inline static OSSymbolPool &instance() __pure2;
152*bbb1b6f9SApple OSS Distributions
OSSymbolPool()153*bbb1b6f9SApple OSS Distributions OSSymbolPool()
154*bbb1b6f9SApple OSS Distributions {
155*bbb1b6f9SApple OSS Distributions lck_mtx_init(&_mutex, &lock_group, LCK_ATTR_NULL);
156*bbb1b6f9SApple OSS Distributions
157*bbb1b6f9SApple OSS Distributions smr_hash_init(&_hash, MIN_SIZE);
158*bbb1b6f9SApple OSS Distributions smrq_init(&_huge_head);
159*bbb1b6f9SApple OSS Distributions
160*bbb1b6f9SApple OSS Distributions _tcall = thread_call_allocate_with_options(rehash, this,
161*bbb1b6f9SApple OSS Distributions THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
162*bbb1b6f9SApple OSS Distributions }
163*bbb1b6f9SApple OSS Distributions OSSymbolPool(const OSSymbolPool &) = delete;
164*bbb1b6f9SApple OSS Distributions OSSymbolPool(OSSymbolPool &&) = delete;
165*bbb1b6f9SApple OSS Distributions OSSymbolPool &operator=(const OSSymbolPool &) = delete;
166*bbb1b6f9SApple OSS Distributions OSSymbolPool &operator=(OSSymbolPool &&) = delete;
167*bbb1b6f9SApple OSS Distributions
168*bbb1b6f9SApple OSS Distributions ~OSSymbolPool() = delete;
169*bbb1b6f9SApple OSS Distributions
170*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> findSymbol(smrh_key_t key) const;
171*bbb1b6f9SApple OSS Distributions
172*bbb1b6f9SApple OSS Distributions void insertSymbol(
173*bbb1b6f9SApple OSS Distributions OSSharedPtr<OSSymbol> &sym,
174*bbb1b6f9SApple OSS Distributions smrh_key_t key,
175*bbb1b6f9SApple OSS Distributions bool makePermanent = false);
176*bbb1b6f9SApple OSS Distributions
177*bbb1b6f9SApple OSS Distributions void removeSymbol(OSSymbol *sym);
178*bbb1b6f9SApple OSS Distributions
179*bbb1b6f9SApple OSS Distributions void rehash();
180*bbb1b6f9SApple OSS Distributions
181*bbb1b6f9SApple OSS Distributions void checkForPageUnload(void *startAddr, void *endAddr);
182*bbb1b6f9SApple OSS Distributions };
183*bbb1b6f9SApple OSS Distributions
184*bbb1b6f9SApple OSS Distributions static _Alignas(OSSymbolPool) uint8_t OSSymbolPoolStorage[sizeof(OSSymbolPool)];
185*bbb1b6f9SApple OSS Distributions
186*bbb1b6f9SApple OSS Distributions OSSymbolPool &
instance()187*bbb1b6f9SApple OSS Distributions OSSymbolPool::instance()
188*bbb1b6f9SApple OSS Distributions {
189*bbb1b6f9SApple OSS Distributions return reinterpret_cast<OSSymbolPool &>(OSSymbolPoolStorage);
190*bbb1b6f9SApple OSS Distributions }
191*bbb1b6f9SApple OSS Distributions
192*bbb1b6f9SApple OSS Distributions static inline bool
OSSymbol_is_huge(size_t size)193*bbb1b6f9SApple OSS Distributions OSSymbol_is_huge(size_t size)
194*bbb1b6f9SApple OSS Distributions {
195*bbb1b6f9SApple OSS Distributions return size > KALLOC_SAFE_ALLOC_SIZE;
196*bbb1b6f9SApple OSS Distributions }
197*bbb1b6f9SApple OSS Distributions
198*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol>
findSymbol(smrh_key_t key) const199*bbb1b6f9SApple OSS Distributions OSSymbolPool::findSymbol(smrh_key_t key) const
200*bbb1b6f9SApple OSS Distributions {
201*bbb1b6f9SApple OSS Distributions OSSymbol *sym;
202*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> ret;
203*bbb1b6f9SApple OSS Distributions
204*bbb1b6f9SApple OSS Distributions if (!OSSymbol_is_huge(key.smrk_len)) {
205*bbb1b6f9SApple OSS Distributions char tmp_buf[128]; /* empirically all keys are < 110 bytes */
206*bbb1b6f9SApple OSS Distributions char *copy_s = NULL;
207*bbb1b6f9SApple OSS Distributions
208*bbb1b6f9SApple OSS Distributions /*
209*bbb1b6f9SApple OSS Distributions * rdar://105075708: the key might be in pageable memory,
210*bbb1b6f9SApple OSS Distributions * and smr_hash_get() disable preemption which prevents
211*bbb1b6f9SApple OSS Distributions * faulting the memory.
212*bbb1b6f9SApple OSS Distributions */
213*bbb1b6f9SApple OSS Distributions if (key.smrk_len <= sizeof(tmp_buf)) {
214*bbb1b6f9SApple OSS Distributions memcpy(tmp_buf, key.smrk_opaque, key.smrk_len);
215*bbb1b6f9SApple OSS Distributions key.smrk_string = tmp_buf;
216*bbb1b6f9SApple OSS Distributions } else {
217*bbb1b6f9SApple OSS Distributions copy_s = (char *)kalloc_data(key.smrk_len,
218*bbb1b6f9SApple OSS Distributions Z_WAITOK_ZERO_NOFAIL);
219*bbb1b6f9SApple OSS Distributions memcpy(copy_s, key.smrk_opaque, key.smrk_len);
220*bbb1b6f9SApple OSS Distributions key.smrk_string = copy_s;
221*bbb1b6f9SApple OSS Distributions }
222*bbb1b6f9SApple OSS Distributions sym = smr_hash_get(&_hash, key, &hash_traits);
223*bbb1b6f9SApple OSS Distributions if (copy_s) {
224*bbb1b6f9SApple OSS Distributions kfree_data(copy_s, key.smrk_len);
225*bbb1b6f9SApple OSS Distributions }
226*bbb1b6f9SApple OSS Distributions } else {
227*bbb1b6f9SApple OSS Distributions lock();
228*bbb1b6f9SApple OSS Distributions sym = (OSSymbol *)__smr_hash_serialized_find(&_huge_head, key,
229*bbb1b6f9SApple OSS Distributions &hash_traits.smrht);
230*bbb1b6f9SApple OSS Distributions if (sym && !OSSymbol_obj_try_get(sym)) {
231*bbb1b6f9SApple OSS Distributions sym = NULL;
232*bbb1b6f9SApple OSS Distributions }
233*bbb1b6f9SApple OSS Distributions unlock();
234*bbb1b6f9SApple OSS Distributions }
235*bbb1b6f9SApple OSS Distributions
236*bbb1b6f9SApple OSS Distributions if (sym) {
237*bbb1b6f9SApple OSS Distributions ret.reset(sym, OSNoRetain);
238*bbb1b6f9SApple OSS Distributions }
239*bbb1b6f9SApple OSS Distributions
240*bbb1b6f9SApple OSS Distributions return ret;
241*bbb1b6f9SApple OSS Distributions }
242*bbb1b6f9SApple OSS Distributions
243*bbb1b6f9SApple OSS Distributions void
insertSymbol(OSSharedPtr<OSSymbol> & symToInsert,smrh_key_t key,bool make_permanent)244*bbb1b6f9SApple OSS Distributions OSSymbolPool::insertSymbol(
245*bbb1b6f9SApple OSS Distributions OSSharedPtr<OSSymbol> &symToInsert,
246*bbb1b6f9SApple OSS Distributions smrh_key_t key,
247*bbb1b6f9SApple OSS Distributions bool make_permanent)
248*bbb1b6f9SApple OSS Distributions {
249*bbb1b6f9SApple OSS Distributions OSSymbol *sym;
250*bbb1b6f9SApple OSS Distributions
251*bbb1b6f9SApple OSS Distributions /* make sure no one ever subclassed OSSymbols */
252*bbb1b6f9SApple OSS Distributions zone_require(OSSymbol_zone, symToInsert.get());
253*bbb1b6f9SApple OSS Distributions
254*bbb1b6f9SApple OSS Distributions symToInsert->flags |= kOSSSymbolHashed;
255*bbb1b6f9SApple OSS Distributions if (make_permanent) {
256*bbb1b6f9SApple OSS Distributions symToInsert->flags |= kOSSSymbolPermanent;
257*bbb1b6f9SApple OSS Distributions }
258*bbb1b6f9SApple OSS Distributions
259*bbb1b6f9SApple OSS Distributions lock();
260*bbb1b6f9SApple OSS Distributions
261*bbb1b6f9SApple OSS Distributions if (!OSSymbol_is_huge(key.smrk_len)) {
262*bbb1b6f9SApple OSS Distributions sym = smr_hash_serialized_get_or_insert(&_hash, key,
263*bbb1b6f9SApple OSS Distributions &symToInsert->hashlink, &hash_traits);
264*bbb1b6f9SApple OSS Distributions
265*bbb1b6f9SApple OSS Distributions if (shouldGrow() && !_tcallScheduled &&
266*bbb1b6f9SApple OSS Distributions startup_phase >= STARTUP_SUB_THREAD_CALL) {
267*bbb1b6f9SApple OSS Distributions _tcallScheduled = true;
268*bbb1b6f9SApple OSS Distributions thread_call_enter(_tcall);
269*bbb1b6f9SApple OSS Distributions }
270*bbb1b6f9SApple OSS Distributions } else {
271*bbb1b6f9SApple OSS Distributions sym = (OSSymbol *)__smr_hash_serialized_find(&_huge_head, key,
272*bbb1b6f9SApple OSS Distributions &hash_traits.smrht);
273*bbb1b6f9SApple OSS Distributions if (!sym || !OSSymbol_obj_try_get(sym)) {
274*bbb1b6f9SApple OSS Distributions smrq_serialized_insert_head(&_huge_head,
275*bbb1b6f9SApple OSS Distributions &symToInsert->hashlink);
276*bbb1b6f9SApple OSS Distributions _hugeCount++;
277*bbb1b6f9SApple OSS Distributions sym = NULL;
278*bbb1b6f9SApple OSS Distributions }
279*bbb1b6f9SApple OSS Distributions }
280*bbb1b6f9SApple OSS Distributions
281*bbb1b6f9SApple OSS Distributions unlock();
282*bbb1b6f9SApple OSS Distributions
283*bbb1b6f9SApple OSS Distributions if (sym) {
284*bbb1b6f9SApple OSS Distributions symToInsert->flags &= ~(kOSSSymbolHashed | kOSSSymbolPermanent);
285*bbb1b6f9SApple OSS Distributions symToInsert.reset(sym, OSNoRetain);
286*bbb1b6f9SApple OSS Distributions }
287*bbb1b6f9SApple OSS Distributions }
288*bbb1b6f9SApple OSS Distributions
289*bbb1b6f9SApple OSS Distributions void
removeSymbol(OSSymbol * sym)290*bbb1b6f9SApple OSS Distributions OSSymbolPool::removeSymbol(OSSymbol *sym)
291*bbb1b6f9SApple OSS Distributions {
292*bbb1b6f9SApple OSS Distributions lock();
293*bbb1b6f9SApple OSS Distributions
294*bbb1b6f9SApple OSS Distributions assert(sym->flags & kOSSSymbolHashed);
295*bbb1b6f9SApple OSS Distributions sym->flags &= ~kOSSSymbolHashed;
296*bbb1b6f9SApple OSS Distributions
297*bbb1b6f9SApple OSS Distributions if (!OSSymbol_is_huge(sym->length)) {
298*bbb1b6f9SApple OSS Distributions smr_hash_serialized_remove(&_hash, &sym->hashlink, &hash_traits);
299*bbb1b6f9SApple OSS Distributions
300*bbb1b6f9SApple OSS Distributions if (shouldShrink() && !_tcallScheduled &&
301*bbb1b6f9SApple OSS Distributions startup_phase >= STARTUP_SUB_THREAD_CALL) {
302*bbb1b6f9SApple OSS Distributions _tcallScheduled = true;
303*bbb1b6f9SApple OSS Distributions thread_call_enter(_tcall);
304*bbb1b6f9SApple OSS Distributions }
305*bbb1b6f9SApple OSS Distributions } else {
306*bbb1b6f9SApple OSS Distributions smrq_serialized_remove(&_huge_head, &sym->hashlink);
307*bbb1b6f9SApple OSS Distributions _hugeCount--;
308*bbb1b6f9SApple OSS Distributions }
309*bbb1b6f9SApple OSS Distributions
310*bbb1b6f9SApple OSS Distributions unlock();
311*bbb1b6f9SApple OSS Distributions }
312*bbb1b6f9SApple OSS Distributions
313*bbb1b6f9SApple OSS Distributions void
rehash(thread_call_param_t arg0,thread_call_param_t arg1 __unused)314*bbb1b6f9SApple OSS Distributions OSSymbolPool::rehash(thread_call_param_t arg0, thread_call_param_t arg1 __unused)
315*bbb1b6f9SApple OSS Distributions {
316*bbb1b6f9SApple OSS Distributions reinterpret_cast<OSSymbolPool *>(arg0)->rehash();
317*bbb1b6f9SApple OSS Distributions }
318*bbb1b6f9SApple OSS Distributions
319*bbb1b6f9SApple OSS Distributions void
rehash()320*bbb1b6f9SApple OSS Distributions OSSymbolPool::rehash()
321*bbb1b6f9SApple OSS Distributions {
322*bbb1b6f9SApple OSS Distributions lock();
323*bbb1b6f9SApple OSS Distributions _tcallScheduled = false;
324*bbb1b6f9SApple OSS Distributions
325*bbb1b6f9SApple OSS Distributions if (shouldShrink()) {
326*bbb1b6f9SApple OSS Distributions smr_hash_shrink_and_unlock(&_hash, &_mutex, &hash_traits);
327*bbb1b6f9SApple OSS Distributions } else if (shouldGrow()) {
328*bbb1b6f9SApple OSS Distributions smr_hash_grow_and_unlock(&_hash, &_mutex, &hash_traits);
329*bbb1b6f9SApple OSS Distributions } else {
330*bbb1b6f9SApple OSS Distributions unlock();
331*bbb1b6f9SApple OSS Distributions }
332*bbb1b6f9SApple OSS Distributions }
333*bbb1b6f9SApple OSS Distributions
334*bbb1b6f9SApple OSS Distributions void
checkForPageUnload(void * startAddr,void * endAddr)335*bbb1b6f9SApple OSS Distributions OSSymbolPool::checkForPageUnload(void *startAddr, void *endAddr)
336*bbb1b6f9SApple OSS Distributions {
337*bbb1b6f9SApple OSS Distributions OSSymbol *sym;
338*bbb1b6f9SApple OSS Distributions char *s;
339*bbb1b6f9SApple OSS Distributions bool mustSync = false;
340*bbb1b6f9SApple OSS Distributions
341*bbb1b6f9SApple OSS Distributions lock();
342*bbb1b6f9SApple OSS Distributions smr_hash_foreach(sym, &_hash, &hash_traits) {
343*bbb1b6f9SApple OSS Distributions if (sym->string >= startAddr && sym->string < endAddr) {
344*bbb1b6f9SApple OSS Distributions assert(sym->flags & kOSStringNoCopy);
345*bbb1b6f9SApple OSS Distributions
346*bbb1b6f9SApple OSS Distributions s = (char *)kalloc_data(sym->length,
347*bbb1b6f9SApple OSS Distributions Z_WAITOK_ZERO);
348*bbb1b6f9SApple OSS Distributions if (s) {
349*bbb1b6f9SApple OSS Distributions memcpy(s, sym->string, sym->length);
350*bbb1b6f9SApple OSS Distributions /*
351*bbb1b6f9SApple OSS Distributions * make sure the memcpy is visible for readers
352*bbb1b6f9SApple OSS Distributions * who dereference `string` below.
353*bbb1b6f9SApple OSS Distributions *
354*bbb1b6f9SApple OSS Distributions * We can't use os_atomic_store(&..., release)
355*bbb1b6f9SApple OSS Distributions * because OSSymbol::string is PACed
356*bbb1b6f9SApple OSS Distributions */
357*bbb1b6f9SApple OSS Distributions os_atomic_thread_fence(release);
358*bbb1b6f9SApple OSS Distributions }
359*bbb1b6f9SApple OSS Distributions sym->string = s;
360*bbb1b6f9SApple OSS Distributions sym->flags &= ~kOSStringNoCopy;
361*bbb1b6f9SApple OSS Distributions mustSync = true;
362*bbb1b6f9SApple OSS Distributions }
363*bbb1b6f9SApple OSS Distributions }
364*bbb1b6f9SApple OSS Distributions
365*bbb1b6f9SApple OSS Distributions unlock();
366*bbb1b6f9SApple OSS Distributions
367*bbb1b6f9SApple OSS Distributions /* Make sure no readers can see stale pointers that we rewrote */
368*bbb1b6f9SApple OSS Distributions if (mustSync) {
369*bbb1b6f9SApple OSS Distributions smr_iokit_synchronize();
370*bbb1b6f9SApple OSS Distributions }
371*bbb1b6f9SApple OSS Distributions }
372*bbb1b6f9SApple OSS Distributions
373*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic pop /* -Winvalid-offsetof */
374*bbb1b6f9SApple OSS Distributions
375*bbb1b6f9SApple OSS Distributions /*
376*bbb1b6f9SApple OSS Distributions *********************************************************************
377*bbb1b6f9SApple OSS Distributions * From here on we are actually implementing the OSSymbol class
378*bbb1b6f9SApple OSS Distributions *********************************************************************
379*bbb1b6f9SApple OSS Distributions */
380*bbb1b6f9SApple OSS Distributions #define super OSString
381*bbb1b6f9SApple OSS Distributions
382*bbb1b6f9SApple OSS Distributions OSDefineMetaClassWithInit(OSSymbol, OSString, OSSymbol::initialize());
383*bbb1b6f9SApple OSS Distributions OSMetaClassConstructorInit(OSSymbol, OSString, OSSymbol::initialize());
384*bbb1b6f9SApple OSS Distributions OSDefineBasicStructors(OSSymbol, OSString)
385*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 0);
386*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 1);
387*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 2);
388*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 3);
389*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 4);
390*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 5);
391*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 6);
392*bbb1b6f9SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 7);
393*bbb1b6f9SApple OSS Distributions
394*bbb1b6f9SApple OSS Distributions static void
OSSymbol_smr_free(void * sym,vm_size_t size __unused)395*bbb1b6f9SApple OSS Distributions OSSymbol_smr_free(void *sym, vm_size_t size __unused)
396*bbb1b6f9SApple OSS Distributions {
397*bbb1b6f9SApple OSS Distributions reinterpret_cast<OSSymbol *>(sym)->smr_free();
398*bbb1b6f9SApple OSS Distributions }
399*bbb1b6f9SApple OSS Distributions
400*bbb1b6f9SApple OSS Distributions void
initialize()401*bbb1b6f9SApple OSS Distributions OSSymbol::initialize()
402*bbb1b6f9SApple OSS Distributions {
403*bbb1b6f9SApple OSS Distributions zone_enable_smr(OSSymbol_zone, &smr_iokit, &OSSymbol_smr_free);
404*bbb1b6f9SApple OSS Distributions new (OSSymbolPoolStorage) OSSymbolPool();
405*bbb1b6f9SApple OSS Distributions }
406*bbb1b6f9SApple OSS Distributions
407*bbb1b6f9SApple OSS Distributions bool
initWithCStringNoCopy(const char *)408*bbb1b6f9SApple OSS Distributions OSSymbol::initWithCStringNoCopy(const char *)
409*bbb1b6f9SApple OSS Distributions {
410*bbb1b6f9SApple OSS Distributions return false;
411*bbb1b6f9SApple OSS Distributions }
412*bbb1b6f9SApple OSS Distributions bool
initWithCString(const char *)413*bbb1b6f9SApple OSS Distributions OSSymbol::initWithCString(const char *)
414*bbb1b6f9SApple OSS Distributions {
415*bbb1b6f9SApple OSS Distributions return false;
416*bbb1b6f9SApple OSS Distributions }
417*bbb1b6f9SApple OSS Distributions bool
initWithString(const OSString *)418*bbb1b6f9SApple OSS Distributions OSSymbol::initWithString(const OSString *)
419*bbb1b6f9SApple OSS Distributions {
420*bbb1b6f9SApple OSS Distributions return false;
421*bbb1b6f9SApple OSS Distributions }
422*bbb1b6f9SApple OSS Distributions
423*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol>
withString(const OSString * aString)424*bbb1b6f9SApple OSS Distributions OSSymbol::withString(const OSString *aString)
425*bbb1b6f9SApple OSS Distributions {
426*bbb1b6f9SApple OSS Distributions // This string may be a OSSymbol already, cheap check.
427*bbb1b6f9SApple OSS Distributions if (OSDynamicCast(OSSymbol, aString)) {
428*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> aStringNew((const OSSymbol *)aString, OSRetain);
429*bbb1b6f9SApple OSS Distributions return aStringNew;
430*bbb1b6f9SApple OSS Distributions } else if (((const OSSymbol *) aString)->flags & kOSStringNoCopy) {
431*bbb1b6f9SApple OSS Distributions return OSSymbol::withCStringNoCopy(aString->getCStringNoCopy());
432*bbb1b6f9SApple OSS Distributions } else {
433*bbb1b6f9SApple OSS Distributions return OSSymbol::withCString(aString->getCStringNoCopy());
434*bbb1b6f9SApple OSS Distributions }
435*bbb1b6f9SApple OSS Distributions }
436*bbb1b6f9SApple OSS Distributions
437*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol>
withCString(const char * cString)438*bbb1b6f9SApple OSS Distributions OSSymbol::withCString(const char *cString)
439*bbb1b6f9SApple OSS Distributions {
440*bbb1b6f9SApple OSS Distributions auto &pool = OSSymbolPool::instance();
441*bbb1b6f9SApple OSS Distributions smrh_key_t key = {
442*bbb1b6f9SApple OSS Distributions .smrk_string = cString,
443*bbb1b6f9SApple OSS Distributions .smrk_len = strnlen(cString, kMaxStringLength),
444*bbb1b6f9SApple OSS Distributions };
445*bbb1b6f9SApple OSS Distributions bool permanent = false;
446*bbb1b6f9SApple OSS Distributions
447*bbb1b6f9SApple OSS Distributions if (key.smrk_len >= kMaxStringLength) {
448*bbb1b6f9SApple OSS Distributions return nullptr;
449*bbb1b6f9SApple OSS Distributions }
450*bbb1b6f9SApple OSS Distributions
451*bbb1b6f9SApple OSS Distributions auto symbol = pool.findSymbol(key);
452*bbb1b6f9SApple OSS Distributions if (__probable(symbol)) {
453*bbb1b6f9SApple OSS Distributions return symbol;
454*bbb1b6f9SApple OSS Distributions }
455*bbb1b6f9SApple OSS Distributions
456*bbb1b6f9SApple OSS Distributions #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) || defined(KERNEL_INTEGRITY_PV_CTRR)
457*bbb1b6f9SApple OSS Distributions /*
458*bbb1b6f9SApple OSS Distributions * Empirically, symbols which string is from the rorgn part of the
459*bbb1b6f9SApple OSS Distributions * kernel are asked about all the time.
460*bbb1b6f9SApple OSS Distributions *
461*bbb1b6f9SApple OSS Distributions * Making them noCopy + permanent avoids a significant amount of
462*bbb1b6f9SApple OSS Distributions * useless refcounting traffic.
463*bbb1b6f9SApple OSS Distributions *
464*bbb1b6f9SApple OSS Distributions * On embedded, this policy causes about 200 extra symbols to be made
465*bbb1b6f9SApple OSS Distributions * from baseline (~6k), but avoiding the string copies saves about 60k.
466*bbb1b6f9SApple OSS Distributions */
467*bbb1b6f9SApple OSS Distributions permanent = rorgn_contains((vm_offset_t)cString, key.smrk_len + 1, false);
468*bbb1b6f9SApple OSS Distributions #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) || defined(KERNEL_INTEGRITY_PV_CTRR) */
469*bbb1b6f9SApple OSS Distributions
470*bbb1b6f9SApple OSS Distributions /*
471*bbb1b6f9SApple OSS Distributions * can't use OSString::initWithCString* because it calls
472*bbb1b6f9SApple OSS Distributions * OSObject::init() which tries to enroll in IOTracking if it's on.
473*bbb1b6f9SApple OSS Distributions */
474*bbb1b6f9SApple OSS Distributions
475*bbb1b6f9SApple OSS Distributions auto newSymb = OSMakeShared<OSSymbol>();
476*bbb1b6f9SApple OSS Distributions
477*bbb1b6f9SApple OSS Distributions if (permanent) {
478*bbb1b6f9SApple OSS Distributions newSymb->flags = kOSStringNoCopy;
479*bbb1b6f9SApple OSS Distributions newSymb->length = (uint32_t)(key.smrk_len + 1);
480*bbb1b6f9SApple OSS Distributions newSymb->string = const_cast<char *>(cString);
481*bbb1b6f9SApple OSS Distributions pool.insertSymbol(/* inout */ newSymb, key, permanent);
482*bbb1b6f9SApple OSS Distributions } else if (char *s = (char *)kalloc_data(key.smrk_len + 1, Z_WAITOK_ZERO)) {
483*bbb1b6f9SApple OSS Distributions memcpy(s, cString, key.smrk_len);
484*bbb1b6f9SApple OSS Distributions newSymb->flags = 0;
485*bbb1b6f9SApple OSS Distributions newSymb->length = (uint32_t)(key.smrk_len + 1);
486*bbb1b6f9SApple OSS Distributions newSymb->string = s;
487*bbb1b6f9SApple OSS Distributions pool.insertSymbol(/* inout */ newSymb, key, permanent);
488*bbb1b6f9SApple OSS Distributions } else {
489*bbb1b6f9SApple OSS Distributions newSymb.reset();
490*bbb1b6f9SApple OSS Distributions }
491*bbb1b6f9SApple OSS Distributions
492*bbb1b6f9SApple OSS Distributions return os::move(newSymb); // return the newly created & inserted symbol.
493*bbb1b6f9SApple OSS Distributions }
494*bbb1b6f9SApple OSS Distributions
495*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol>
withCStringNoCopy(const char * cString)496*bbb1b6f9SApple OSS Distributions OSSymbol::withCStringNoCopy(const char *cString)
497*bbb1b6f9SApple OSS Distributions {
498*bbb1b6f9SApple OSS Distributions auto &pool = OSSymbolPool::instance();
499*bbb1b6f9SApple OSS Distributions smrh_key_t key = {
500*bbb1b6f9SApple OSS Distributions .smrk_string = cString,
501*bbb1b6f9SApple OSS Distributions .smrk_len = strnlen(cString, kMaxStringLength),
502*bbb1b6f9SApple OSS Distributions };
503*bbb1b6f9SApple OSS Distributions bool permanent = false;
504*bbb1b6f9SApple OSS Distributions
505*bbb1b6f9SApple OSS Distributions if (key.smrk_len >= kMaxStringLength) {
506*bbb1b6f9SApple OSS Distributions return nullptr;
507*bbb1b6f9SApple OSS Distributions }
508*bbb1b6f9SApple OSS Distributions
509*bbb1b6f9SApple OSS Distributions auto symbol = pool.findSymbol(key);
510*bbb1b6f9SApple OSS Distributions if (__probable(symbol)) {
511*bbb1b6f9SApple OSS Distributions return symbol;
512*bbb1b6f9SApple OSS Distributions }
513*bbb1b6f9SApple OSS Distributions
514*bbb1b6f9SApple OSS Distributions #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) || defined(KERNEL_INTEGRITY_PV_CTRR)
515*bbb1b6f9SApple OSS Distributions permanent = rorgn_contains((vm_offset_t)cString, key.smrk_len + 1, false);
516*bbb1b6f9SApple OSS Distributions #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) || defined(KERNEL_INTEGRITY_PV_CTRR) */
517*bbb1b6f9SApple OSS Distributions
518*bbb1b6f9SApple OSS Distributions auto newSymb = OSMakeShared<OSSymbol>();
519*bbb1b6f9SApple OSS Distributions
520*bbb1b6f9SApple OSS Distributions /*
521*bbb1b6f9SApple OSS Distributions * can't use OSString::initWithCStringNoCopy because it calls
522*bbb1b6f9SApple OSS Distributions * OSObject::init() which tries to enrol in IOTracking if it's on.
523*bbb1b6f9SApple OSS Distributions */
524*bbb1b6f9SApple OSS Distributions newSymb->flags = kOSStringNoCopy;
525*bbb1b6f9SApple OSS Distributions newSymb->length = (uint32_t)(key.smrk_len + 1);
526*bbb1b6f9SApple OSS Distributions newSymb->string = const_cast<char *>(cString);
527*bbb1b6f9SApple OSS Distributions pool.insertSymbol(/* inout */ newSymb, key, permanent);
528*bbb1b6f9SApple OSS Distributions
529*bbb1b6f9SApple OSS Distributions return os::move(newSymb); // return the newly created & inserted symbol.
530*bbb1b6f9SApple OSS Distributions }
531*bbb1b6f9SApple OSS Distributions
532*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol>
existingSymbolForString(const OSString * aString)533*bbb1b6f9SApple OSS Distributions OSSymbol::existingSymbolForString(const OSString *aString)
534*bbb1b6f9SApple OSS Distributions {
535*bbb1b6f9SApple OSS Distributions if (!aString) {
536*bbb1b6f9SApple OSS Distributions return NULL;
537*bbb1b6f9SApple OSS Distributions }
538*bbb1b6f9SApple OSS Distributions if (OSDynamicCast(OSSymbol, aString)) {
539*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> aStringNew((const OSSymbol *)aString, OSRetain);
540*bbb1b6f9SApple OSS Distributions return aStringNew;
541*bbb1b6f9SApple OSS Distributions }
542*bbb1b6f9SApple OSS Distributions
543*bbb1b6f9SApple OSS Distributions smrh_key_t key = {
544*bbb1b6f9SApple OSS Distributions .smrk_string = aString->getCStringNoCopy(),
545*bbb1b6f9SApple OSS Distributions .smrk_len = aString->getLength(),
546*bbb1b6f9SApple OSS Distributions };
547*bbb1b6f9SApple OSS Distributions return OSSymbolPool::instance().findSymbol(key);
548*bbb1b6f9SApple OSS Distributions }
549*bbb1b6f9SApple OSS Distributions
550*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol>
existingSymbolForCString(const char * cString)551*bbb1b6f9SApple OSS Distributions OSSymbol::existingSymbolForCString(const char *cString)
552*bbb1b6f9SApple OSS Distributions {
553*bbb1b6f9SApple OSS Distributions smrh_key_t key = {
554*bbb1b6f9SApple OSS Distributions .smrk_string = cString,
555*bbb1b6f9SApple OSS Distributions .smrk_len = strlen(cString),
556*bbb1b6f9SApple OSS Distributions };
557*bbb1b6f9SApple OSS Distributions return OSSymbolPool::instance().findSymbol(key);
558*bbb1b6f9SApple OSS Distributions }
559*bbb1b6f9SApple OSS Distributions
560*bbb1b6f9SApple OSS Distributions void
checkForPageUnload(void * startAddr,void * endAddr)561*bbb1b6f9SApple OSS Distributions OSSymbol::checkForPageUnload(void *startAddr, void *endAddr)
562*bbb1b6f9SApple OSS Distributions {
563*bbb1b6f9SApple OSS Distributions OSSymbolPool::instance().checkForPageUnload(startAddr, endAddr);
564*bbb1b6f9SApple OSS Distributions }
565*bbb1b6f9SApple OSS Distributions
566*bbb1b6f9SApple OSS Distributions void
taggedRetain(const void * tag) const567*bbb1b6f9SApple OSS Distributions OSSymbol::taggedRetain(const void *tag) const
568*bbb1b6f9SApple OSS Distributions {
569*bbb1b6f9SApple OSS Distributions if ((flags & kOSSSymbolPermanent) == 0) {
570*bbb1b6f9SApple OSS Distributions super::taggedRetain(tag);
571*bbb1b6f9SApple OSS Distributions }
572*bbb1b6f9SApple OSS Distributions }
573*bbb1b6f9SApple OSS Distributions
574*bbb1b6f9SApple OSS Distributions void
taggedRelease(const void * tag) const575*bbb1b6f9SApple OSS Distributions OSSymbol::taggedRelease(const void *tag) const
576*bbb1b6f9SApple OSS Distributions {
577*bbb1b6f9SApple OSS Distributions if ((flags & kOSSSymbolPermanent) == 0) {
578*bbb1b6f9SApple OSS Distributions super::taggedRelease(tag);
579*bbb1b6f9SApple OSS Distributions }
580*bbb1b6f9SApple OSS Distributions }
581*bbb1b6f9SApple OSS Distributions
582*bbb1b6f9SApple OSS Distributions void
taggedRelease(const void * tag,const int when) const583*bbb1b6f9SApple OSS Distributions OSSymbol::taggedRelease(const void *tag, const int when) const
584*bbb1b6f9SApple OSS Distributions {
585*bbb1b6f9SApple OSS Distributions if ((flags & kOSSSymbolPermanent) == 0) {
586*bbb1b6f9SApple OSS Distributions super::taggedRelease(tag, when);
587*bbb1b6f9SApple OSS Distributions }
588*bbb1b6f9SApple OSS Distributions }
589*bbb1b6f9SApple OSS Distributions
590*bbb1b6f9SApple OSS Distributions void *
operator new(size_t size __unused)591*bbb1b6f9SApple OSS Distributions OSSymbol::operator new(size_t size __unused)
592*bbb1b6f9SApple OSS Distributions {
593*bbb1b6f9SApple OSS Distributions return zalloc_smr(OSSymbol_zone, Z_WAITOK_ZERO_NOFAIL);
594*bbb1b6f9SApple OSS Distributions }
595*bbb1b6f9SApple OSS Distributions
596*bbb1b6f9SApple OSS Distributions void
operator delete(void * mem,size_t size)597*bbb1b6f9SApple OSS Distributions OSSymbol::operator delete(void *mem, size_t size)
598*bbb1b6f9SApple OSS Distributions {
599*bbb1b6f9SApple OSS Distributions /*
600*bbb1b6f9SApple OSS Distributions * OSSymbol dying is this sequence:
601*bbb1b6f9SApple OSS Distributions *
602*bbb1b6f9SApple OSS Distributions * OSSymbol::taggedRelease() hits 0,
603*bbb1b6f9SApple OSS Distributions * which calls OSSymbol::free(),
604*bbb1b6f9SApple OSS Distributions * which calls zfree_smr().
605*bbb1b6f9SApple OSS Distributions *
606*bbb1b6f9SApple OSS Distributions * At this stage, the memory of the OSSymbol is on a deferred
607*bbb1b6f9SApple OSS Distributions * reclamation queue.
608*bbb1b6f9SApple OSS Distributions *
609*bbb1b6f9SApple OSS Distributions * When the memory is being recycled by zalloc, OSSymbol::smr_free()
610*bbb1b6f9SApple OSS Distributions * is called which terminates with a delete call and only needs
611*bbb1b6f9SApple OSS Distributions * to zero said memory given that the memory has already been
612*bbb1b6f9SApple OSS Distributions * returned to the allocator.
613*bbb1b6f9SApple OSS Distributions */
614*bbb1b6f9SApple OSS Distributions bzero(mem, size);
615*bbb1b6f9SApple OSS Distributions }
616*bbb1b6f9SApple OSS Distributions
617*bbb1b6f9SApple OSS Distributions void
smr_free()618*bbb1b6f9SApple OSS Distributions OSSymbol::smr_free()
619*bbb1b6f9SApple OSS Distributions {
620*bbb1b6f9SApple OSS Distributions /*
621*bbb1b6f9SApple OSS Distributions * This is called when the object is getting reused
622*bbb1b6f9SApple OSS Distributions */
623*bbb1b6f9SApple OSS Distributions
624*bbb1b6f9SApple OSS Distributions if (!(flags & kOSStringNoCopy) && string) {
625*bbb1b6f9SApple OSS Distributions kfree_data(string, length);
626*bbb1b6f9SApple OSS Distributions }
627*bbb1b6f9SApple OSS Distributions
628*bbb1b6f9SApple OSS Distributions /*
629*bbb1b6f9SApple OSS Distributions * Note: we do not call super::free() on purpose because
630*bbb1b6f9SApple OSS Distributions * it would call OSObject::free() which tries to support
631*bbb1b6f9SApple OSS Distributions * iotracking. iotracking is fundamentally incompatible
632*bbb1b6f9SApple OSS Distributions * with SMR, so we on purpose do not call into these.
633*bbb1b6f9SApple OSS Distributions *
634*bbb1b6f9SApple OSS Distributions * to debug OSSymbol leaks etc, the zone logging feature
635*bbb1b6f9SApple OSS Distributions * can be used instead on the iokit.OSSymbol zone.
636*bbb1b6f9SApple OSS Distributions */
637*bbb1b6f9SApple OSS Distributions OSSymbol::gMetaClass.instanceDestructed();
638*bbb1b6f9SApple OSS Distributions
639*bbb1b6f9SApple OSS Distributions delete this;
640*bbb1b6f9SApple OSS Distributions }
641*bbb1b6f9SApple OSS Distributions
642*bbb1b6f9SApple OSS Distributions void
free()643*bbb1b6f9SApple OSS Distributions OSSymbol::free()
644*bbb1b6f9SApple OSS Distributions {
645*bbb1b6f9SApple OSS Distributions bool freeNow = true;
646*bbb1b6f9SApple OSS Distributions
647*bbb1b6f9SApple OSS Distributions if (flags & kOSSSymbolHashed) {
648*bbb1b6f9SApple OSS Distributions OSSymbolPool::instance().removeSymbol(this);
649*bbb1b6f9SApple OSS Distributions freeNow = OSSymbol_is_huge(length);
650*bbb1b6f9SApple OSS Distributions }
651*bbb1b6f9SApple OSS Distributions
652*bbb1b6f9SApple OSS Distributions if (freeNow && !(flags & kOSStringNoCopy) && string) {
653*bbb1b6f9SApple OSS Distributions /*
654*bbb1b6f9SApple OSS Distributions * If the element isn't in the hash, it was a failed insertion
655*bbb1b6f9SApple OSS Distributions * racing, and no one will every do a hazardous access,
656*bbb1b6f9SApple OSS Distributions * so we can clean up the string right away.
657*bbb1b6f9SApple OSS Distributions *
658*bbb1b6f9SApple OSS Distributions * If it is huge, then it is not looked up via SMR but under
659*bbb1b6f9SApple OSS Distributions * locks, so we can free right now (actually _must_ because
660*bbb1b6f9SApple OSS Distributions * this free is not preemption disabled safe and can't be done
661*bbb1b6f9SApple OSS Distributions * in smr_free())
662*bbb1b6f9SApple OSS Distributions */
663*bbb1b6f9SApple OSS Distributions kfree_data(string, length);
664*bbb1b6f9SApple OSS Distributions assert(string == nullptr); /* kfree_data nils out */
665*bbb1b6f9SApple OSS Distributions }
666*bbb1b6f9SApple OSS Distributions
667*bbb1b6f9SApple OSS Distributions (zfree_smr)(OSSymbol_zone, this);
668*bbb1b6f9SApple OSS Distributions }
669*bbb1b6f9SApple OSS Distributions
670*bbb1b6f9SApple OSS Distributions uint32_t
hash() const671*bbb1b6f9SApple OSS Distributions OSSymbol::hash() const
672*bbb1b6f9SApple OSS Distributions {
673*bbb1b6f9SApple OSS Distributions assert(!OSSymbol_is_huge(length));
674*bbb1b6f9SApple OSS Distributions return os_hash_jenkins(string, length - 1);
675*bbb1b6f9SApple OSS Distributions }
676*bbb1b6f9SApple OSS Distributions
677*bbb1b6f9SApple OSS Distributions bool
isEqualTo(const char * aCString) const678*bbb1b6f9SApple OSS Distributions OSSymbol::isEqualTo(const char *aCString) const
679*bbb1b6f9SApple OSS Distributions {
680*bbb1b6f9SApple OSS Distributions return super::isEqualTo(aCString);
681*bbb1b6f9SApple OSS Distributions }
682*bbb1b6f9SApple OSS Distributions
683*bbb1b6f9SApple OSS Distributions bool
isEqualTo(const OSSymbol * aSymbol) const684*bbb1b6f9SApple OSS Distributions OSSymbol::isEqualTo(const OSSymbol *aSymbol) const
685*bbb1b6f9SApple OSS Distributions {
686*bbb1b6f9SApple OSS Distributions return aSymbol == this;
687*bbb1b6f9SApple OSS Distributions }
688*bbb1b6f9SApple OSS Distributions
689*bbb1b6f9SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const690*bbb1b6f9SApple OSS Distributions OSSymbol::isEqualTo(const OSMetaClassBase *obj) const
691*bbb1b6f9SApple OSS Distributions {
692*bbb1b6f9SApple OSS Distributions OSSymbol * sym;
693*bbb1b6f9SApple OSS Distributions OSString * str;
694*bbb1b6f9SApple OSS Distributions
695*bbb1b6f9SApple OSS Distributions if ((sym = OSDynamicCast(OSSymbol, obj))) {
696*bbb1b6f9SApple OSS Distributions return isEqualTo(sym);
697*bbb1b6f9SApple OSS Distributions } else if ((str = OSDynamicCast(OSString, obj))) {
698*bbb1b6f9SApple OSS Distributions return super::isEqualTo(str);
699*bbb1b6f9SApple OSS Distributions } else {
700*bbb1b6f9SApple OSS Distributions return false;
701*bbb1b6f9SApple OSS Distributions }
702*bbb1b6f9SApple OSS Distributions }
703*bbb1b6f9SApple OSS Distributions
704*bbb1b6f9SApple OSS Distributions unsigned int
bsearch(const void * key,const void * array,unsigned int arrayCount,size_t memberSize)705*bbb1b6f9SApple OSS Distributions OSSymbol::bsearch(
706*bbb1b6f9SApple OSS Distributions const void * key,
707*bbb1b6f9SApple OSS Distributions const void * array,
708*bbb1b6f9SApple OSS Distributions unsigned int arrayCount,
709*bbb1b6f9SApple OSS Distributions size_t memberSize)
710*bbb1b6f9SApple OSS Distributions {
711*bbb1b6f9SApple OSS Distributions const void **p;
712*bbb1b6f9SApple OSS Distributions unsigned int baseIdx = 0;
713*bbb1b6f9SApple OSS Distributions unsigned int lim;
714*bbb1b6f9SApple OSS Distributions
715*bbb1b6f9SApple OSS Distributions for (lim = arrayCount; lim; lim >>= 1) {
716*bbb1b6f9SApple OSS Distributions p = (typeof(p))(((uintptr_t) array) + (baseIdx + (lim >> 1)) * memberSize);
717*bbb1b6f9SApple OSS Distributions if (key == *p) {
718*bbb1b6f9SApple OSS Distributions return baseIdx + (lim >> 1);
719*bbb1b6f9SApple OSS Distributions }
720*bbb1b6f9SApple OSS Distributions if (key > *p) {
721*bbb1b6f9SApple OSS Distributions // move right
722*bbb1b6f9SApple OSS Distributions baseIdx += (lim >> 1) + 1;
723*bbb1b6f9SApple OSS Distributions lim--;
724*bbb1b6f9SApple OSS Distributions }
725*bbb1b6f9SApple OSS Distributions // else move left
726*bbb1b6f9SApple OSS Distributions }
727*bbb1b6f9SApple OSS Distributions // not found, insertion point here
728*bbb1b6f9SApple OSS Distributions return baseIdx + (lim >> 1);
729*bbb1b6f9SApple OSS Distributions }
730*bbb1b6f9SApple OSS Distributions
731*bbb1b6f9SApple OSS Distributions #if DEBUG || DEVELOPMENT
732*bbb1b6f9SApple OSS Distributions static int
iokit_symbol_basic_test(int64_t size,int64_t * out)733*bbb1b6f9SApple OSS Distributions iokit_symbol_basic_test(int64_t size, int64_t *out)
734*bbb1b6f9SApple OSS Distributions {
735*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> sym1;
736*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> sym2;
737*bbb1b6f9SApple OSS Distributions char *data;
738*bbb1b6f9SApple OSS Distributions
739*bbb1b6f9SApple OSS Distributions data = (char *)kalloc_data(size, Z_WAITOK);
740*bbb1b6f9SApple OSS Distributions if (!data) {
741*bbb1b6f9SApple OSS Distributions return ENOMEM;
742*bbb1b6f9SApple OSS Distributions }
743*bbb1b6f9SApple OSS Distributions
744*bbb1b6f9SApple OSS Distributions memset(data, 'A', size - 1);
745*bbb1b6f9SApple OSS Distributions data[size - 1] = '\0';
746*bbb1b6f9SApple OSS Distributions
747*bbb1b6f9SApple OSS Distributions sym1 = OSSymbol::withCString(data);
748*bbb1b6f9SApple OSS Distributions if (sym1 == nullptr) {
749*bbb1b6f9SApple OSS Distributions return ENOMEM;
750*bbb1b6f9SApple OSS Distributions }
751*bbb1b6f9SApple OSS Distributions assert(sym1->getLength() == size - 1);
752*bbb1b6f9SApple OSS Distributions
753*bbb1b6f9SApple OSS Distributions sym2 = OSSymbol::withCString(data);
754*bbb1b6f9SApple OSS Distributions assert(sym1 == sym2);
755*bbb1b6f9SApple OSS Distributions
756*bbb1b6f9SApple OSS Distributions sym2.reset();
757*bbb1b6f9SApple OSS Distributions sym1.reset();
758*bbb1b6f9SApple OSS Distributions
759*bbb1b6f9SApple OSS Distributions *out = 1;
760*bbb1b6f9SApple OSS Distributions return 0;
761*bbb1b6f9SApple OSS Distributions }
762*bbb1b6f9SApple OSS Distributions SYSCTL_TEST_REGISTER(iokit_symbol_basic, iokit_symbol_basic_test);
763*bbb1b6f9SApple OSS Distributions #endif /* DEBUG || DEVELOPMENT */
764