2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
30 * 1999 Mar 29 rsulack created.
33 #include <mach/mach_types.h>
34 #include <mach/vm_types.h>
35 #include <mach/kern_return.h>
36 #include <kern/kern_types.h>
37 #include <vm/vm_kern.h>
38 #include <kern/thread.h>
39 #include <mach-o/mach_header.h>
41 #include <mach_host.h>
43 #define WRITE_PROTECT_MODULE_TEXT (0)
45 kmod_info_t
*kmod
= 0;
46 static int kmod_index
= 1;
48 decl_simple_lock_data(,kmod_lock
)
49 decl_simple_lock_data(,kmod_queue_lock
)
51 typedef struct cmd_queue_entry
{
57 queue_head_t kmod_cmd_queue
;
62 simple_lock_init(&kmod_lock
, ETAP_MISC_Q
);
63 simple_lock_init(&kmod_queue_lock
, ETAP_MISC_Q
);
64 queue_init(&kmod_cmd_queue
);
68 kmod_lookupbyid(kmod_t id
)
74 if (k
->id
== id
) break;
82 kmod_lookupbyname(const char * name
)
88 if (!strcmp(k
->name
, name
)) break;
96 kmod_lookupbyid_locked(kmod_t id
)
101 kc
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
104 simple_lock(&kmod_queue_lock
);
105 k
= kmod_lookupbyid(id
);
107 bcopy((char*)k
, (char *)kc
, sizeof(kmod_info_t
));
110 simple_unlock(&kmod_queue_lock
);
113 kfree((vm_offset_t
)kc
, sizeof(kmod_info_t
));
120 kmod_lookupbyname_locked(const char * name
)
125 kc
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
128 simple_lock(&kmod_queue_lock
);
129 k
= kmod_lookupbyname(name
);
131 bcopy((char *)k
, (char *)kc
, sizeof(kmod_info_t
));
134 simple_unlock(&kmod_queue_lock
);
137 kfree((vm_offset_t
)kc
, sizeof(kmod_info_t
));
143 // XXX add a nocopy flag??
146 kmod_queue_cmd(vm_address_t data
, vm_size_t size
)
149 cmd_queue_entry_t
*e
= (cmd_queue_entry_t
*)kalloc(sizeof(struct cmd_queue_entry
));
150 if (!e
) return KERN_RESOURCE_SHORTAGE
;
152 rc
= kmem_alloc(kernel_map
, &e
->data
, size
);
153 if (rc
!= KERN_SUCCESS
) {
154 kfree((vm_offset_t
)e
, sizeof(struct cmd_queue_entry
));
158 bcopy((void *)data
, (void *)e
->data
, size
);
160 simple_lock(&kmod_queue_lock
);
161 enqueue_tail(&kmod_cmd_queue
, (queue_entry_t
)e
);
162 simple_unlock(&kmod_queue_lock
);
164 thread_wakeup_one((event_t
)&kmod_cmd_queue
);
170 kmod_load_extension(char *name
)
172 kmod_load_extension_cmd_t
*data
;
175 size
= sizeof(kmod_load_extension_cmd_t
);
176 data
= (kmod_load_extension_cmd_t
*)kalloc(size
);
177 if (!data
) return KERN_RESOURCE_SHORTAGE
;
179 data
->type
= KMOD_LOAD_EXTENSION_PACKET
;
180 strncpy(data
->name
, name
, KMOD_MAX_NAME
);
182 return kmod_queue_cmd((vm_address_t
)data
, size
);
186 kmod_load_extension_with_dependencies(char *name
, char **dependencies
)
188 kmod_load_with_dependencies_cmd_t
*data
;
199 size
= sizeof(int) + KMOD_MAX_NAME
* (count
+ 1) + 1;
200 data
= (kmod_load_with_dependencies_cmd_t
*)kalloc(size
);
201 if (!data
) return KERN_RESOURCE_SHORTAGE
;
203 data
->type
= KMOD_LOAD_WITH_DEPENDENCIES_PACKET
;
204 strncpy(data
->name
, name
, KMOD_MAX_NAME
);
207 for (i
=0; i
< count
; i
++) {
208 strncpy(data
->dependencies
[i
], *c
, KMOD_MAX_NAME
);
211 data
->dependencies
[count
][0] = 0;
213 return kmod_queue_cmd((vm_address_t
)data
, size
);
216 kmod_send_generic(int type
, void *generic_data
, int size
)
218 kmod_generic_cmd_t
*data
;
220 data
= (kmod_generic_cmd_t
*)kalloc(size
+ sizeof(int));
221 if (!data
) return KERN_RESOURCE_SHORTAGE
;
224 bcopy(data
->data
, generic_data
, size
);
226 return kmod_queue_cmd((vm_address_t
)data
, size
+ sizeof(int));
230 kmod_create_internal(kmod_info_t
*info
, kmod_t
*id
)
234 if (!info
) return KERN_INVALID_ADDRESS
;
236 // double check for page alignment
237 if ((info
->address
| info
->hdr_size
) & (PAGE_SIZE
- 1)) {
238 return KERN_INVALID_ADDRESS
;
241 rc
= vm_map_wire(kernel_map
, info
->address
+ info
->hdr_size
,
242 info
->address
+ info
->size
, VM_PROT_DEFAULT
, FALSE
);
243 if (rc
!= KERN_SUCCESS
) {
246 #if WRITE_PROTECT_MODULE_TEXT
248 struct section
* sect
= getsectbynamefromheader(
249 (struct mach_header
*) info
->address
, "__TEXT", "__text");
252 (void) vm_map_protect(kernel_map
, round_page(sect
->addr
), trunc_page(sect
->addr
+ sect
->size
),
253 VM_PROT_READ
|VM_PROT_EXECUTE
, TRUE
);
258 simple_lock(&kmod_lock
);
260 // check to see if already loaded
261 if (kmod_lookupbyname(info
->name
)) {
262 simple_unlock(&kmod_lock
);
263 rc
= vm_map_unwire(kernel_map
, info
->address
+ info
->hdr_size
,
264 info
->address
+ info
->size
, FALSE
);
265 assert(rc
== KERN_SUCCESS
);
266 return KERN_INVALID_ARGUMENT
;
269 info
->id
= kmod_index
++;
270 info
->reference_count
= 0;
277 simple_unlock(&kmod_lock
);
280 printf("kmod_create: %s (id %d), %d pages loaded at 0x%x, header size 0x%x\n",
281 info
->name
, info
->id
, info
->size
/ PAGE_SIZE
, info
->address
, info
->hdr_size
);
289 kmod_create(host_priv_t host_priv
,
293 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
294 return kmod_create_internal(info
, id
);
298 kmod_create_fake(const char *name
, const char *version
)
302 if (!name
|| ! version
||
303 (1 + strlen(name
) > KMOD_MAX_NAME
) ||
304 (1 + strlen(version
) > KMOD_MAX_NAME
)) {
306 return KERN_INVALID_ARGUMENT
;
309 info
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
311 return KERN_RESOURCE_SHORTAGE
;
315 info
->info_version
= KMOD_INFO_VERSION
;
316 bcopy(name
, info
->name
, 1 + strlen(name
));
317 bcopy(version
, info
->version
, 1 + strlen(version
)); //NIK fixed this part
318 info
->reference_count
= 1; // keep it from unloading, starting, stopping
319 info
->reference_list
= 0;
320 info
->address
= info
->size
= info
->hdr_size
= 0;
321 info
->start
= info
->stop
= 0;
323 simple_lock(&kmod_lock
);
325 // check to see if already "loaded"
326 if (kmod_lookupbyname(info
->name
)) {
327 simple_unlock(&kmod_lock
);
328 return KERN_INVALID_ARGUMENT
;
331 info
->id
= kmod_index
++;
336 simple_unlock(&kmod_lock
);
342 kmod_destroy_internal(kmod_t id
)
348 simple_lock(&kmod_lock
);
353 kmod_reference_t
*r
, *t
;
355 if (k
->reference_count
!= 0) {
356 simple_unlock(&kmod_lock
);
357 return KERN_INVALID_ARGUMENT
;
360 if (k
== p
) { // first element
365 simple_unlock(&kmod_lock
);
367 r
= k
->reference_list
;
369 r
->info
->reference_count
--;
372 kfree((vm_offset_t
)t
, sizeof(struct kmod_reference
));
376 printf("kmod_destroy: %s (id %d), deallocating %d pages starting at 0x%x\n",
377 k
->name
, k
->id
, k
->size
/ PAGE_SIZE
, k
->address
);
380 rc
= vm_map_unwire(kernel_map
, k
->address
+ k
->hdr_size
,
381 k
->address
+ k
->size
, FALSE
);
382 assert(rc
== KERN_SUCCESS
);
384 rc
= vm_deallocate(kernel_map
, k
->address
, k
->size
);
385 assert(rc
== KERN_SUCCESS
);
393 simple_unlock(&kmod_lock
);
395 return KERN_INVALID_ARGUMENT
;
400 kmod_destroy(host_priv_t host_priv
,
403 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
404 return kmod_destroy_internal(id
);
413 mach_msg_type_number_t
*dataCount
)
415 kern_return_t rc
= KERN_SUCCESS
;
416 void * user_data
= 0;
417 kern_return_t (*func
)();
420 simple_lock(&kmod_lock
);
422 k
= kmod_lookupbyid(id
);
423 if (!k
|| k
->reference_count
) {
424 simple_unlock(&kmod_lock
);
425 rc
= KERN_INVALID_ARGUMENT
;
430 func
= (void *)k
->start
;
432 func
= (void *)k
->stop
;
435 simple_unlock(&kmod_lock
);
438 // call kmod entry point
440 if (data
&& dataCount
&& *data
&& *dataCount
) {
441 vm_map_copyout(kernel_map
, (vm_offset_t
*)&user_data
, (vm_map_copy_t
)*data
);
444 rc
= (*func
)(k
, user_data
);
449 (void) vm_deallocate(kernel_map
, (vm_offset_t
)user_data
, *dataCount
);
452 if (dataCount
) *dataCount
= 0;
459 * The retain and release calls take no user data, but the caller
460 * may have sent some in error (the MIG definition allows it).
461 * If this is the case, they will just return that same data
462 * right back to the caller (since they never touch the *data and
463 * *dataCount fields).
466 kmod_retain(kmod_t id
)
468 kern_return_t rc
= KERN_SUCCESS
;
470 kmod_info_t
*t
; // reference to
471 kmod_info_t
*f
; // reference from
472 kmod_reference_t
*r
= 0;
474 r
= (kmod_reference_t
*)kalloc(sizeof(struct kmod_reference
));
476 rc
= KERN_RESOURCE_SHORTAGE
;
480 simple_lock(&kmod_lock
);
482 t
= kmod_lookupbyid(KMOD_UNPACK_TO_ID(id
));
483 f
= kmod_lookupbyid(KMOD_UNPACK_FROM_ID(id
));
485 simple_unlock(&kmod_lock
);
486 if (r
) kfree((vm_offset_t
)r
, sizeof(struct kmod_reference
));
487 rc
= KERN_INVALID_ARGUMENT
;
491 r
->next
= f
->reference_list
;
493 f
->reference_list
= r
;
494 t
->reference_count
++;
496 simple_unlock(&kmod_lock
);
505 kmod_release(kmod_t id
)
507 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
509 kmod_info_t
*t
; // reference to
510 kmod_info_t
*f
; // reference from
511 kmod_reference_t
*r
= 0;
512 kmod_reference_t
* p
;
514 simple_lock(&kmod_lock
);
516 t
= kmod_lookupbyid(KMOD_UNPACK_TO_ID(id
));
517 f
= kmod_lookupbyid(KMOD_UNPACK_FROM_ID(id
));
519 rc
= KERN_INVALID_ARGUMENT
;
523 p
= r
= f
->reference_list
;
526 if (p
== r
) { // first element
527 f
->reference_list
= r
->next
;
531 r
->info
->reference_count
--;
533 simple_unlock(&kmod_lock
);
534 kfree((vm_offset_t
)r
, sizeof(struct kmod_reference
));
542 simple_unlock(&kmod_lock
);
551 kmod_control(host_priv_t host_priv
,
553 kmod_control_flavor_t flavor
,
555 mach_msg_type_number_t
*dataCount
)
557 kern_return_t rc
= KERN_SUCCESS
;
559 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
563 case KMOD_CNTL_START
:
566 rc
= kmod_start_or_stop(id
, (flavor
== KMOD_CNTL_START
),
571 case KMOD_CNTL_RETAIN
:
573 rc
= kmod_retain(id
);
577 case KMOD_CNTL_RELEASE
:
579 rc
= kmod_release(id
);
583 case KMOD_CNTL_GET_CMD
:
586 cmd_queue_entry_t
*e
;
589 * Throw away any data the user may have sent in error.
590 * We must do this, because we are likely to return to
591 * some data for these commands (thus causing a leak of
592 * whatever data the user sent us in error).
594 if (*data
&& *dataCount
) {
595 vm_map_copy_discard(*data
);
600 simple_lock(&kmod_queue_lock
);
602 if (queue_empty(&kmod_cmd_queue
)) {
605 res
= thread_sleep_simple_lock((event_t
)&kmod_cmd_queue
,
608 if (queue_empty(&kmod_cmd_queue
)) {
609 // we must have been interrupted!
610 simple_unlock(&kmod_queue_lock
);
611 assert(res
== THREAD_INTERRUPTED
);
615 e
= (cmd_queue_entry_t
*)dequeue_head(&kmod_cmd_queue
);
617 simple_unlock(&kmod_queue_lock
);
619 rc
= vm_map_copyin(kernel_map
, e
->data
, e
->size
, TRUE
, (vm_map_copy_t
*)data
);
621 simple_lock(&kmod_queue_lock
);
622 enqueue_head(&kmod_cmd_queue
, (queue_entry_t
)e
);
623 simple_unlock(&kmod_queue_lock
);
628 *dataCount
= e
->size
;
630 kfree((vm_offset_t
)e
, sizeof(struct cmd_queue_entry
));
636 rc
= KERN_INVALID_ARGUMENT
;
644 kmod_get_info(host_t host
,
645 kmod_info_array_t
*kmods
,
646 mach_msg_type_number_t
*kmodCount
)
650 kmod_reference_t
*r
, *p2
;
653 kern_return_t rc
= KERN_SUCCESS
;
659 simple_lock(&kmod_lock
);
663 size
+= sizeof(kmod_info_t
);
664 r
= k
->reference_list
;
666 size
+=sizeof(kmod_reference_t
);
671 simple_unlock(&kmod_lock
);
672 if (!size
) return KERN_SUCCESS
;
674 rc
= kmem_alloc(kernel_map
, &data
, size
);
677 // copy kmod into data, retry if kmod's size has changed (grown)
678 // the copied out data is tweeked to figure what's what at user level
679 // change the copied out k->next pointers to point to themselves
680 // change the k->reference into a count, tack the references on
681 // the end of the data packet in the order they are found
683 simple_lock(&kmod_lock
);
684 k
= kmod
; p1
= (kmod_info_t
*)data
;
686 if ((p1
+ 1) > (kmod_info_t
*)(data
+ size
)) {
687 simple_unlock(&kmod_lock
);
688 kmem_free(kernel_map
, data
, size
);
693 if (k
->next
) p1
->next
= k
;
697 p2
= (kmod_reference_t
*)p1
;
698 k
= kmod
; p1
= (kmod_info_t
*)data
;
700 r
= k
->reference_list
; ref_count
= 0;
702 if ((p2
+ 1) > (kmod_reference_t
*)(data
+ size
)) {
703 simple_unlock(&kmod_lock
);
704 kmem_free(kernel_map
, data
, size
);
707 // note the last 'k' in the chain has its next == 0
708 // since there can only be one like that,
709 // this case is handled by the caller
711 p2
++; r
= r
->next
; ref_count
++;
713 p1
->reference_list
= (kmod_reference_t
*)ref_count
;
716 simple_unlock(&kmod_lock
);
718 rc
= vm_map_copyin(kernel_map
, data
, size
, TRUE
, (vm_map_copy_t
*)kmods
);
720 kmem_free(kernel_map
, data
, size
);
731 kmod_call_funcs_in_section(struct mach_header
*header
, const char *sectName
)
733 typedef void (*Routine
)(void);
737 if (header
->magic
!= MH_MAGIC
) {
738 return KERN_INVALID_ARGUMENT
;
741 routines
= (Routine
*) getsectdatafromheader(header
, SEG_TEXT
, (char *) sectName
, &size
);
742 if (!routines
) return KERN_SUCCESS
;
744 size
/= sizeof(Routine
);
745 for (i
= 0; i
< size
; i
++) {
753 kmod_initialize_cpp(kmod_info_t
*info
)
755 return kmod_call_funcs_in_section((struct mach_header
*)info
->address
, "__constructor");
759 kmod_finalize_cpp(kmod_info_t
*info
)
761 return kmod_call_funcs_in_section((struct mach_header
*)info
->address
, "__destructor");
765 kmod_default_start(struct kmod_info
*ki
, void *data
)
767 return KMOD_RETURN_SUCCESS
;
771 kmod_default_stop(struct kmod_info
*ki
, void *data
)
773 return KMOD_RETURN_SUCCESS
;
777 kmod_dump(vm_offset_t
*addr
, unsigned int cnt
)
779 vm_offset_t
* kscan_addr
= 0;
780 vm_offset_t
* rscan_addr
= 0;
782 kmod_reference_t
* r
;
785 int kmod_scan_stopped
= 0;
786 kmod_info_t
* stop_kmod
= 0;
787 int ref_scan_stopped
= 0;
788 kmod_reference_t
* stop_ref
= 0;
790 for (k
= kmod
; k
; k
= k
->next
) {
792 continue; // skip fake entries for built-in kernel components
794 if (pmap_extract(kernel_pmap
, (vm_offset_t
)k
) == 0) {
795 kdb_printf(" kmod scan stopped due to missing "
796 "kmod page: %08x\n", stop_kmod
);
799 for (i
= 0, kscan_addr
= addr
; i
< cnt
; i
++, kscan_addr
++) {
800 if ((*kscan_addr
>= k
->address
) &&
801 (*kscan_addr
< (k
->address
+ k
->size
))) {
804 kdb_printf(" Kernel loadable modules in backtrace "
805 "(with dependencies):\n");
808 kdb_printf(" %s(%s)@0x%x\n",
809 k
->name
, k
->version
, k
->address
);
811 for (r
= k
->reference_list
; r
; r
= r
->next
) {
814 if (pmap_extract(kernel_pmap
, (vm_offset_t
)r
) == 0) {
815 kdb_printf(" kmod dependency scan stopped "
816 "due to missing dependency page: %08x\n", r
);
822 if (!rinfo
->address
) {
823 continue; // skip fake entries for built-ins
826 if (pmap_extract(kernel_pmap
, (vm_offset_t
)rinfo
) == 0) {
827 kdb_printf(" kmod dependency scan stopped "
828 "due to missing kmod page: %08x\n", rinfo
);
832 kdb_printf(" dependency: %s(%s)@0x%x\n",
833 rinfo
->name
, rinfo
->version
, rinfo
->address
);
836 break; // only report this kmod for one backtrace address