1/*
2 * Copyright (c) 2000-2007 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 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30 * support for mandatory and extensible security protections.  This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
32 * Version 2.0.
33 */
34
35#ifndef	_MACH_KMOD_H_
36#define	_MACH_KMOD_H_
37
38#include <mach/kern_return.h>
39
40#include <sys/cdefs.h>
41
42/***********************************************************************
43* kmod_control() commands. 1-5 are long-established. 6-8 are new in
44* Leopard and used to reliably get and verify symbol information needed
45* to link kexts against the running kernel, or to disable kmod loading
46* if such symbol information cannot be found.
47***********************************************************************/
48#define KMOD_CNTL_START		1	// call kmod's start routine
49#define KMOD_CNTL_STOP		2	// call kmod's stop routine
50#define KMOD_CNTL_RETAIN	3	// increase a kmod's reference count
51#define KMOD_CNTL_RELEASE	4	// decrease a kmod's reference count
52#define KMOD_CNTL_GET_CMD	5	// get kmod load cmd from kernel
53
54#define KMOD_CNTL_GET_KERNEL_SYMBOLS  6  // get symfile as data buffer
55#define KMOD_CNTL_FREE_LINKEDIT_DATA  7  // refuse to create new kmods
56#define KMOD_CNTL_GET_KERNEL_UUID     8  // LC_UUID load command payload
57#define KMOD_CNTL_GET_UUID            8  // LC_UUID load command payload
58#define KMOD_CNTL_DISABLE_LOAD        9  // refuse to create new kmods
59
60#define KMOD_PACK_IDS(from, to)	(((unsigned long)from << 16) | (unsigned long)to)
61#define KMOD_UNPACK_FROM_ID(i)	((unsigned long)i >> 16)
62#define KMOD_UNPACK_TO_ID(i)	((unsigned long)i & 0xffff)
63
64typedef int kmod_t;
65typedef int kmod_control_flavor_t;
66typedef void* kmod_args_t;
67
68#define KMOD_MAX_NAME	64
69
70#pragma pack(4)
71
72/* LP64todo - not 64-bit safe */
73typedef struct kmod_reference {
74	struct kmod_reference	*next;
75	struct kmod_info	*info;
76} kmod_reference_t;
77
78#pragma pack()
79
80/**************************************************************************************/
81/*	 warning any changes to this structure affect the following macros.	      */
82/**************************************************************************************/
83
84#define KMOD_RETURN_SUCCESS	KERN_SUCCESS
85#define KMOD_RETURN_FAILURE	KERN_FAILURE
86
87typedef kern_return_t kmod_start_func_t(struct kmod_info *ki, void *data);
88typedef kern_return_t kmod_stop_func_t(struct kmod_info *ki, void *data);
89
90#pragma pack(4)
91
92/* LP64todo - not 64-bit safe */
93
94typedef struct kmod_info {
95	struct kmod_info 	*next;
96	int			info_version;		// version of this structure
97	int			id;
98	char			name[KMOD_MAX_NAME];
99	char			version[KMOD_MAX_NAME];
100	int			reference_count;	// # refs to this
101	kmod_reference_t	*reference_list;	// who this refs
102	vm_address_t		address;		// starting address
103	vm_size_t		size;			// total size
104	vm_size_t		hdr_size;		// unwired hdr size
105        kmod_start_func_t	*start;
106        kmod_stop_func_t	*stop;
107} kmod_info_t;
108
109#pragma pack()
110
111typedef kmod_info_t *kmod_info_array_t;
112
113#define KMOD_INFO_NAME 		kmod_info
114#define KMOD_INFO_VERSION	1
115
116#define KMOD_DECL(name, version)							\
117	static kmod_start_func_t name ## _module_start;					\
118	static kmod_stop_func_t  name ## _module_stop;					\
119	kmod_info_t KMOD_INFO_NAME = { 0, KMOD_INFO_VERSION, -1,			\
120				       { #name }, { version }, -1, 0, 0, 0, 0,		\
121			               name ## _module_start,		\
122			               name ## _module_stop };
123
124#define KMOD_EXPLICIT_DECL(name, version, start, stop)					\
125	kmod_info_t KMOD_INFO_NAME = { 0, KMOD_INFO_VERSION, -1,			\
126				       { #name }, { version }, -1, 0, 0, 0, 0,		\
127			               start, stop };
128
129// the following is useful for libaries that don't need their own start and stop functions
130#define KMOD_LIB_DECL(name, version)							\
131	kmod_info_t KMOD_INFO_NAME = { 0, KMOD_INFO_VERSION, -1,			\
132				       { #name }, { version }, -1, 0, 0, 0, 0,		\
133			               kmod_default_start,				\
134				       kmod_default_stop };
135
136
137// *************************************************************************************
138// kmod kernel to user commands
139// *************************************************************************************
140
141#define KMOD_LOAD_EXTENSION_PACKET		1
142#define KMOD_LOAD_WITH_DEPENDENCIES_PACKET	2
143
144// for generic packets
145#define KMOD_IOKIT_START_RANGE_PACKET		0x1000
146#define KMOD_IOKIT_END_RANGE_PACKET		0x1fff
147
148typedef struct kmod_load_extension_cmd {
149	int	type;
150	char	name[KMOD_MAX_NAME];
151} kmod_load_extension_cmd_t;
152
153typedef struct kmod_load_with_dependencies_cmd {
154	int	type;
155	char	name[KMOD_MAX_NAME];
156	char	dependencies[1][KMOD_MAX_NAME];
157} kmod_load_with_dependencies_cmd_t;
158
159typedef struct kmod_generic_cmd {
160	int	type;
161	char	data[1];
162} kmod_generic_cmd_t;
163
164#ifdef	KERNEL_PRIVATE
165
166extern kmod_info_t *kmod_lookupbyname(const char * name);
167extern kmod_info_t *kmod_lookupbyid(kmod_t id);
168extern kmod_info_t *kmod_lookupbyaddress(vm_address_t address);
169extern int kmod_lookupidbyaddress_locked(vm_address_t address);
170
171extern kmod_info_t *kmod_lookupbyname_locked(const char * name);
172extern kmod_info_t *kmod_lookupbyid_locked(kmod_t id);
173extern kmod_start_func_t kmod_default_start;
174extern kmod_stop_func_t  kmod_default_stop;
175
176__BEGIN_DECLS
177extern void kmod_init(void) __attribute__((section("__TEXT, initcode")));
178
179extern kern_return_t kmod_create_fake(const char *name, const char *version);
180extern kern_return_t kmod_create_fake_with_address(const char *name, const char *version,
181                                                    vm_address_t address, vm_size_t size,
182                                                    int * return_id);
183extern kern_return_t kmod_destroy_fake(kmod_t id);
184
185extern kern_return_t kmod_load_extension(char *name);
186extern kern_return_t kmod_load_extension_with_dependencies(char *name, char **dependencies);
187extern kern_return_t kmod_send_generic(int type, void *data, int size);
188
189extern kern_return_t kmod_initialize_cpp(kmod_info_t *info);
190extern kern_return_t kmod_finalize_cpp(kmod_info_t *info);
191
192void record_kext_unload(kmod_t kmod_id);
193void dump_kext_info(int (*printf_func)(const char *fmt, ...));
194
195extern void kmod_dump(vm_offset_t *addr, unsigned int dump_cnt);
196__END_DECLS
197
198#endif	/* KERNEL_PRIVATE */
199
200#endif	/* _MACH_KMOD_H_ */
201