1/*
2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1998 Apple Inc. All rights reserved.
30 *
31 * HISTORY
32 *
33 */
34
35
36#ifndef _IOKIT_IOCATALOGUE_H
37#define _IOKIT_IOCATALOGUE_H
38
39#include <libkern/c++/OSObject.h>
40#include <libkern/c++/OSCollectionIterator.h>
41#include <libkern/c++/OSArray.h>
42#include <libkern/c++/OSDictionary.h>
43#include <IOKit/IOLocks.h>
44#include <sys/cdefs.h>
45
46#include <IOKit/IOKitServer.h>
47
48class IOService;
49
50/*!
51 @class IOCatalogue
52 @abstract In-kernel database for IOKit driver personalities.
53 @discussion The IOCatalogue is a database which contains all IOKit driver personalities. IOService uses this resource when matching devices to their associated drivers.
54*/
55class IOCatalogue : public OSObject
56{
57 OSDeclareDefaultStructors(IOCatalogue)
58
59private:
60 IORWLock * lock;
61 SInt32 generation;
62 OSDictionary * personalities;
63 OSArray * arrayForPersonality(OSDictionary * dict);
64 void addPersonality(OSDictionary * dict);
65
66public:
67 /*!
68 @function initialize
69 @abstract Creates and initializes the database object and poputates it with in-kernel driver personalities.
70 */
71 static void initialize( void );
72
73 /*!
74 @function init
75 @abstract Initializes the database object.
76 @param initArray The initial array of driver personalities to populate the database.
77 */
78 bool init( OSArray * initArray );
79
80 /*!
81 @function free
82 @abstract Cleans up the database and deallocates memory allocated at initialization. This is never called in normal operation of the system.
83 */
84 void free( void ) APPLE_KEXT_OVERRIDE;
85
86 /*!
87 @function findDrivers
88 @abstract This is the primary entry point for IOService.
89 @param service The service
90 @param generationCount Returns a reference to the generation count of the database. The generation count increases only when personalities are added to the database *and* IOService matching has been initiated.
91 @result Returns an ordered set of driver personalities ranked on probe-scores. The ordered set must be released by the receiver.
92 */
93 OSOrderedSet * findDrivers( IOService * service, SInt32 * generationCount );
94
95 /*!
96 @function findDrivers
97 @abstract A more general purpose interface which allows one to retreive driver personalities based the intersection of the 'matching' dictionary and the personality's own property list.
98 @param matching A dictionary containing only keys and values which are to be used for matching. For example, a matching dictionary containing 'IOProviderClass'='IOPCIDevice' will return all personalities with an IOProviderClass key and a value of IOPCIDevice.
99 @param generationCount Returns a reference to the current generation of the database. The generation count increases only when personalities are added to the database *and* IOService matching has been initiated.
100 @result Returns an ordered set of driver personalities ranked on probe-scores. The ordered set must be released by the receiver.
101 */
102 OSOrderedSet * findDrivers( OSDictionary * matching, SInt32 * generationCount );
103
104 /*!
105 @function addDrivers
106 @abstract Adds an array of driver personalities to the database.
107 @param array Array of driver personalities to be added to the database.
108 @param doNubMatching Start matching process after personalities have been added.
109 @result Returns true if driver personality was added to the database successfully. Failure is due to a memory allocation failure.
110 */
111 bool addDrivers( OSArray * array, bool doNubMatching = true );
112
113 /*!
114 @function removeDrivers
115 @abstract Remove driver personalities from the database based on matching information provided.
116 @param matching A dictionary whose keys and values are used for matching personalities in the database. For example, a matching dictionary containing a 'IOProviderClass' key with the value 'IOPCIDevice' will remove all personalities which have the key 'IOProviderClass' equal to 'IOPCIDevice'.
117 @param doNubMatching Start matching process after personalities have been removed. Matching criteria is based on IOProviderClass of those personalities which were removed. This is to allow drivers which haven't been matched to match against NUB's which were blocked by the previous personalities.
118 @result Returns true if personality was removed successfully. Failure is due to a memory allocation failure.
119 */
120 bool removeDrivers( OSDictionary * matching, bool doNubMatching = true );
121
122 /*!
123 @function getGenerationCount
124 @abstract Get the current generation count of the database.
125 */
126 SInt32 getGenerationCount( void ) const;
127
128 /*!
129 @function isModuleLoaded
130 @abstract Reports if a kernel module has been loaded.
131 @param moduleName Name of the module.
132 @result Returns true if the associated kernel module has been loaded into the kernel.
133 */
134 bool isModuleLoaded( OSString * moduleName ) const;
135
136 /*!
137 @function isModuleLoaded
138 @abstract Reports if a kernel module has been loaded.
139 @param moduleName Name of the module.
140 @result Returns true if the associated kernel module has been loaded into the kernel.
141 */
142 bool isModuleLoaded( const char * moduleName ) const;
143
144 /*!
145 @function isModuleLoaded
146 @abstract Reports if a kernel module has been loaded for a particular personality.
147 @param driver A driver personality's property list.
148 @result Returns true if the associated kernel module has been loaded into the kernel for a particular driver personality on which it depends.
149 */
150 bool isModuleLoaded( OSDictionary * driver ) const;
151
152 /*!
153 @function moduleHasLoaded
154 @abstract Callback function called after a IOKit dependent kernel module is loaded.
155 @param name Name of the kernel module.
156 */
157 void moduleHasLoaded( OSString * name );
158
159 /*!
160 @function moduleHasLoaded
161 @abstract Callback function called after a IOKit dependent kernel module is loaded.
162 @param name Name of the kernel module.
163 */
164 void moduleHasLoaded( const char * name );
165
166 /*!
167 @function terminateDrivers
168 @abstract Terminates all instances of a driver which match the contents of the matching dictionary. Does not unload module.
169 @param matching A dictionary whose keys and values are used for matching personalities in the database. For example, a matching dictionary containing a 'IOProviderClass' key with the value 'IOPCIDevice' will cause termination for all instances whose personalities have the key 'IOProviderClass' equal to 'IOPCIDevice'.
170 */
171 IOReturn terminateDrivers( OSDictionary * matching );
172
173 /*!
174 @function terminateDriversForModule
175 @abstract Terminates all instances of a driver which depends on a particular module and unloads the module.
176 @param moduleName Name of the module which is used to determine which driver instances to terminate and unload.
177 @param unload Flag to cause the actual unloading of the module.
178 */
179 IOReturn terminateDriversForModule( OSString * moduleName, bool unload = true);
180
181 /*!
182 @function terminateDriversForModule
183 @abstract Terminates all instances of a driver which depends on a particular module and unloads the module.
184 @param moduleName Name of the module which is used to determine which driver instances to terminate and unload.
185 @param unload Flag to cause the actual unloading of the module.
186 */
187 IOReturn terminateDriversForModule( const char * moduleName, bool unload = true);
188
189 /*!
190 @function startMatching
191 @abstract Starts an IOService matching thread where matching keys and values are provided by the matching dictionary.
192 @param matching A dictionary whose keys and values are used for matching personalities in the database. For example, a matching dictionary containing a 'IOProviderClass' key with the value 'IOPCIDevice' will start matching for all personalities which have the key 'IOProviderClass' equal to 'IOPCIDevice'.
193 */
194 bool startMatching( OSDictionary * matching );
195
196 /*!
197 @function reset
198 @abstract Return the Catalogue to its initial state.
199 @discussion
200 Should only be used by kextd just before it sends all kext personalities down during a rescan.
201 */
202 void reset(void);
203
204 /*!
205 @function resetAndAddDrivers
206 @abstract Replace personalities in IOCatalog with those provided.
207 @discussion
208 Resets the catalogue with a new set of drivers, preserving matching originals to keep wired memory usage down.
209 */
210 bool resetAndAddDrivers(OSArray * drivers, bool doNubMatching = true);
211
212 /*!
213 @function serialize
214 @abstract Serializes the catalog for transport to the user.
215 @param s The serializer object.
216 @result Returns false if unable to serialize database, most likely due to memory shortage.
217 */
218 virtual bool serialize(OSSerialize * s) const APPLE_KEXT_OVERRIDE;
219
220 bool serializeData(IOOptionBits kind, OSSerialize * s) const;
221
222/* This stuff is no longer used at all we keep it around for i386
223 * binary compatibility only. Symbols are no longer exported.
224 */
225
226private:
227
228 /*!
229 @function unloadModule
230 @abstract Unloads the reqested module if no driver instances are currently depending on it.
231 @param moduleName An OSString containing the name of the module to unload.
232 */
233 IOReturn unloadModule( OSString * moduleName ) const;
234
235 IOReturn _removeDrivers(OSDictionary * matching);
236 IOReturn _terminateDrivers(OSDictionary * matching);
237};
238
239extern const OSSymbol * gIOClassKey;
240extern const OSSymbol * gIOProbeScoreKey;
241extern IOCatalogue * gIOCatalogue;
242
243#endif /* ! _IOKIT_IOCATALOGUE_H */
244