2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
27 * 1999 Mar 29 rsulack created.
30 #include <mach/mach_types.h>
31 #include <mach/vm_types.h>
32 #include <mach/kern_return.h>
33 #include <kern/kern_types.h>
34 #include <vm/vm_kern.h>
35 #include <kern/thread.h>
36 #include <mach-o/mach_header.h>
38 #include <mach_host.h>
40 #define WRITE_PROTECT_MODULE_TEXT (0)
42 kmod_info_t
*kmod
= 0;
43 static int kmod_index
= 1;
45 decl_simple_lock_data(,kmod_lock
)
46 decl_simple_lock_data(,kmod_queue_lock
)
48 typedef struct cmd_queue_entry
{
54 queue_head_t kmod_cmd_queue
;
59 simple_lock_init(&kmod_lock
, ETAP_MISC_Q
);
60 simple_lock_init(&kmod_queue_lock
, ETAP_MISC_Q
);
61 queue_init(&kmod_cmd_queue
);
65 kmod_lookupbyid(kmod_t id
)
71 if (k
->id
== id
) break;
79 kmod_lookupbyname(const char * name
)
85 if (!strcmp(k
->name
, name
)) break;
93 kmod_lookupbyid_locked(kmod_t id
)
98 kc
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
101 simple_lock(&kmod_queue_lock
);
102 k
= kmod_lookupbyid(id
);
104 bcopy((char*)k
, (char *)kc
, sizeof(kmod_info_t
));
107 simple_unlock(&kmod_queue_lock
);
110 kfree((vm_offset_t
)kc
, sizeof(kmod_info_t
));
117 kmod_lookupbyname_locked(const char * name
)
122 kc
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
125 simple_lock(&kmod_queue_lock
);
126 k
= kmod_lookupbyname(name
);
128 bcopy((char *)k
, (char *)kc
, sizeof(kmod_info_t
));
131 simple_unlock(&kmod_queue_lock
);
134 kfree((vm_offset_t
)kc
, sizeof(kmod_info_t
));
140 // XXX add a nocopy flag??
143 kmod_queue_cmd(vm_address_t data
, vm_size_t size
)
146 cmd_queue_entry_t
*e
= (cmd_queue_entry_t
*)kalloc(sizeof(struct cmd_queue_entry
));
147 if (!e
) return KERN_RESOURCE_SHORTAGE
;
149 rc
= kmem_alloc(kernel_map
, &e
->data
, size
);
150 if (rc
!= KERN_SUCCESS
) {
151 kfree((vm_offset_t
)e
, sizeof(struct cmd_queue_entry
));
155 bcopy((void *)data
, (void *)e
->data
, size
);
157 simple_lock(&kmod_queue_lock
);
158 enqueue_tail(&kmod_cmd_queue
, (queue_entry_t
)e
);
159 simple_unlock(&kmod_queue_lock
);
161 thread_wakeup_one((event_t
)&kmod_cmd_queue
);
167 kmod_load_extension(char *name
)
169 kmod_load_extension_cmd_t
*data
;
172 size
= sizeof(kmod_load_extension_cmd_t
);
173 data
= (kmod_load_extension_cmd_t
*)kalloc(size
);
174 if (!data
) return KERN_RESOURCE_SHORTAGE
;
176 data
->type
= KMOD_LOAD_EXTENSION_PACKET
;
177 strncpy(data
->name
, name
, KMOD_MAX_NAME
);
179 return kmod_queue_cmd((vm_address_t
)data
, size
);
183 kmod_load_extension_with_dependencies(char *name
, char **dependencies
)
185 kmod_load_with_dependencies_cmd_t
*data
;
196 size
= sizeof(int) + KMOD_MAX_NAME
* (count
+ 1) + 1;
197 data
= (kmod_load_with_dependencies_cmd_t
*)kalloc(size
);
198 if (!data
) return KERN_RESOURCE_SHORTAGE
;
200 data
->type
= KMOD_LOAD_WITH_DEPENDENCIES_PACKET
;
201 strncpy(data
->name
, name
, KMOD_MAX_NAME
);
204 for (i
=0; i
< count
; i
++) {
205 strncpy(data
->dependencies
[i
], *c
, KMOD_MAX_NAME
);
208 data
->dependencies
[count
][0] = 0;
210 return kmod_queue_cmd((vm_address_t
)data
, size
);
213 kmod_send_generic(int type
, void *generic_data
, int size
)
215 kmod_generic_cmd_t
*data
;
217 data
= (kmod_generic_cmd_t
*)kalloc(size
+ sizeof(int));
218 if (!data
) return KERN_RESOURCE_SHORTAGE
;
221 bcopy(data
->data
, generic_data
, size
);
223 return kmod_queue_cmd((vm_address_t
)data
, size
+ sizeof(int));
227 kmod_create_internal(kmod_info_t
*info
, kmod_t
*id
)
231 if (!info
) return KERN_INVALID_ADDRESS
;
233 // double check for page alignment
234 if ((info
->address
| info
->hdr_size
) & (PAGE_SIZE
- 1)) {
235 return KERN_INVALID_ADDRESS
;
238 rc
= vm_map_wire(kernel_map
, info
->address
+ info
->hdr_size
,
239 info
->address
+ info
->size
, VM_PROT_DEFAULT
, FALSE
);
240 if (rc
!= KERN_SUCCESS
) {
243 #if WRITE_PROTECT_MODULE_TEXT
245 struct section
* sect
= getsectbynamefromheader(
246 (struct mach_header
*) info
->address
, "__TEXT", "__text");
249 (void) vm_map_protect(kernel_map
, round_page(sect
->addr
), trunc_page(sect
->addr
+ sect
->size
),
250 VM_PROT_READ
|VM_PROT_EXECUTE
, TRUE
);
255 simple_lock(&kmod_lock
);
257 // check to see if already loaded
258 if (kmod_lookupbyname(info
->name
)) {
259 simple_unlock(&kmod_lock
);
260 rc
= vm_map_unwire(kernel_map
, info
->address
+ info
->hdr_size
,
261 info
->address
+ info
->size
, FALSE
);
262 assert(rc
== KERN_SUCCESS
);
263 return KERN_INVALID_ARGUMENT
;
266 info
->id
= kmod_index
++;
267 info
->reference_count
= 0;
274 simple_unlock(&kmod_lock
);
277 printf("kmod_create: %s (id %d), %d pages loaded at 0x%x, header size 0x%x\n",
278 info
->name
, info
->id
, info
->size
/ PAGE_SIZE
, info
->address
, info
->hdr_size
);
286 kmod_create(host_priv_t host_priv
,
290 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
291 return kmod_create_internal(info
, id
);
295 kmod_create_fake(const char *name
, const char *version
)
299 if (!name
|| ! version
||
300 (1 + strlen(name
) > KMOD_MAX_NAME
) ||
301 (1 + strlen(version
) > KMOD_MAX_NAME
)) {
303 return KERN_INVALID_ARGUMENT
;
306 info
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
308 return KERN_RESOURCE_SHORTAGE
;
312 info
->info_version
= KMOD_INFO_VERSION
;
313 bcopy(name
, info
->name
, 1 + strlen(name
));
314 bcopy(version
, info
->version
, 1 + strlen(version
)); //NIK fixed this part
315 info
->reference_count
= 1; // keep it from unloading, starting, stopping
316 info
->reference_list
= 0;
317 info
->address
= info
->size
= info
->hdr_size
= 0;
318 info
->start
= info
->stop
= 0;
320 simple_lock(&kmod_lock
);
322 // check to see if already "loaded"
323 if (kmod_lookupbyname(info
->name
)) {
324 simple_unlock(&kmod_lock
);
325 return KERN_INVALID_ARGUMENT
;
328 info
->id
= kmod_index
++;
333 simple_unlock(&kmod_lock
);
339 kmod_destroy_internal(kmod_t id
)
345 simple_lock(&kmod_lock
);
350 kmod_reference_t
*r
, *t
;
352 if (k
->reference_count
!= 0) {
353 simple_unlock(&kmod_lock
);
354 return KERN_INVALID_ARGUMENT
;
357 if (k
== p
) { // first element
362 simple_unlock(&kmod_lock
);
364 r
= k
->reference_list
;
366 r
->info
->reference_count
--;
369 kfree((vm_offset_t
)t
, sizeof(struct kmod_reference
));
373 printf("kmod_destroy: %s (id %d), deallocating %d pages starting at 0x%x\n",
374 k
->name
, k
->id
, k
->size
/ PAGE_SIZE
, k
->address
);
377 rc
= vm_map_unwire(kernel_map
, k
->address
+ k
->hdr_size
,
378 k
->address
+ k
->size
, FALSE
);
379 assert(rc
== KERN_SUCCESS
);
381 rc
= vm_deallocate(kernel_map
, k
->address
, k
->size
);
382 assert(rc
== KERN_SUCCESS
);
390 simple_unlock(&kmod_lock
);
392 return KERN_INVALID_ARGUMENT
;
397 kmod_destroy(host_priv_t host_priv
,
400 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
401 return kmod_destroy_internal(id
);
410 mach_msg_type_number_t
*dataCount
)
412 kern_return_t rc
= KERN_SUCCESS
;
413 void * user_data
= 0;
414 kern_return_t (*func
)();
417 simple_lock(&kmod_lock
);
419 k
= kmod_lookupbyid(id
);
420 if (!k
|| k
->reference_count
) {
421 simple_unlock(&kmod_lock
);
422 rc
= KERN_INVALID_ARGUMENT
;
427 func
= (void *)k
->start
;
429 func
= (void *)k
->stop
;
432 simple_unlock(&kmod_lock
);
435 // call kmod entry point
437 if (data
&& dataCount
&& *data
&& *dataCount
) {
438 vm_map_copyout(kernel_map
, (vm_offset_t
*)&user_data
, (vm_map_copy_t
)*data
);
441 rc
= (*func
)(k
, user_data
);
446 (void) vm_deallocate(kernel_map
, (vm_offset_t
)user_data
, *dataCount
);
449 if (dataCount
) *dataCount
= 0;
456 * The retain and release calls take no user data, but the caller
457 * may have sent some in error (the MIG definition allows it).
458 * If this is the case, they will just return that same data
459 * right back to the caller (since they never touch the *data and
460 * *dataCount fields).
463 kmod_retain(kmod_t id
)
465 kern_return_t rc
= KERN_SUCCESS
;
467 kmod_info_t
*t
; // reference to
468 kmod_info_t
*f
; // reference from
469 kmod_reference_t
*r
= 0;
471 r
= (kmod_reference_t
*)kalloc(sizeof(struct kmod_reference
));
473 rc
= KERN_RESOURCE_SHORTAGE
;
477 simple_lock(&kmod_lock
);
479 t
= kmod_lookupbyid(KMOD_UNPACK_TO_ID(id
));
480 f
= kmod_lookupbyid(KMOD_UNPACK_FROM_ID(id
));
482 simple_unlock(&kmod_lock
);
483 if (r
) kfree((vm_offset_t
)r
, sizeof(struct kmod_reference
));
484 rc
= KERN_INVALID_ARGUMENT
;
488 r
->next
= f
->reference_list
;
490 f
->reference_list
= r
;
491 t
->reference_count
++;
493 simple_unlock(&kmod_lock
);
502 kmod_release(kmod_t id
)
504 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
506 kmod_info_t
*t
; // reference to
507 kmod_info_t
*f
; // reference from
508 kmod_reference_t
*r
= 0;
509 kmod_reference_t
* p
;
511 simple_lock(&kmod_lock
);
513 t
= kmod_lookupbyid(KMOD_UNPACK_TO_ID(id
));
514 f
= kmod_lookupbyid(KMOD_UNPACK_FROM_ID(id
));
516 rc
= KERN_INVALID_ARGUMENT
;
520 p
= r
= f
->reference_list
;
523 if (p
== r
) { // first element
524 f
->reference_list
= r
->next
;
528 r
->info
->reference_count
--;
530 simple_unlock(&kmod_lock
);
531 kfree((vm_offset_t
)r
, sizeof(struct kmod_reference
));
539 simple_unlock(&kmod_lock
);
548 kmod_control(host_priv_t host_priv
,
550 kmod_control_flavor_t flavor
,
552 mach_msg_type_number_t
*dataCount
)
554 kern_return_t rc
= KERN_SUCCESS
;
556 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
560 case KMOD_CNTL_START
:
563 rc
= kmod_start_or_stop(id
, (flavor
== KMOD_CNTL_START
),
568 case KMOD_CNTL_RETAIN
:
570 rc
= kmod_retain(id
);
574 case KMOD_CNTL_RELEASE
:
576 rc
= kmod_release(id
);
580 case KMOD_CNTL_GET_CMD
:
583 cmd_queue_entry_t
*e
;
586 * Throw away any data the user may have sent in error.
587 * We must do this, because we are likely to return to
588 * some data for these commands (thus causing a leak of
589 * whatever data the user sent us in error).
591 if (*data
&& *dataCount
) {
592 vm_map_copy_discard(*data
);
597 simple_lock(&kmod_queue_lock
);
599 if (queue_empty(&kmod_cmd_queue
)) {
602 res
= thread_sleep_simple_lock((event_t
)&kmod_cmd_queue
,
605 if (queue_empty(&kmod_cmd_queue
)) {
606 // we must have been interrupted!
607 simple_unlock(&kmod_queue_lock
);
608 assert(res
== THREAD_INTERRUPTED
);
612 e
= (cmd_queue_entry_t
*)dequeue_head(&kmod_cmd_queue
);
614 simple_unlock(&kmod_queue_lock
);
616 rc
= vm_map_copyin(kernel_map
, e
->data
, e
->size
, TRUE
, (vm_map_copy_t
*)data
);
618 simple_lock(&kmod_queue_lock
);
619 enqueue_head(&kmod_cmd_queue
, (queue_entry_t
)e
);
620 simple_unlock(&kmod_queue_lock
);
625 *dataCount
= e
->size
;
627 kfree((vm_offset_t
)e
, sizeof(struct cmd_queue_entry
));
633 rc
= KERN_INVALID_ARGUMENT
;
641 kmod_get_info(host_t host
,
642 kmod_info_array_t
*kmods
,
643 mach_msg_type_number_t
*kmodCount
)
647 kmod_reference_t
*r
, *p2
;
650 kern_return_t rc
= KERN_SUCCESS
;
656 simple_lock(&kmod_lock
);
660 size
+= sizeof(kmod_info_t
);
661 r
= k
->reference_list
;
663 size
+=sizeof(kmod_reference_t
);
668 simple_unlock(&kmod_lock
);
669 if (!size
) return KERN_SUCCESS
;
671 rc
= kmem_alloc(kernel_map
, &data
, size
);
674 // copy kmod into data, retry if kmod's size has changed (grown)
675 // the copied out data is tweeked to figure what's what at user level
676 // change the copied out k->next pointers to point to themselves
677 // change the k->reference into a count, tack the references on
678 // the end of the data packet in the order they are found
680 simple_lock(&kmod_lock
);
681 k
= kmod
; p1
= (kmod_info_t
*)data
;
683 if ((p1
+ 1) > (kmod_info_t
*)(data
+ size
)) {
684 simple_unlock(&kmod_lock
);
685 kmem_free(kernel_map
, data
, size
);
690 if (k
->next
) p1
->next
= k
;
694 p2
= (kmod_reference_t
*)p1
;
695 k
= kmod
; p1
= (kmod_info_t
*)data
;
697 r
= k
->reference_list
; ref_count
= 0;
699 if ((p2
+ 1) > (kmod_reference_t
*)(data
+ size
)) {
700 simple_unlock(&kmod_lock
);
701 kmem_free(kernel_map
, data
, size
);
704 // note the last 'k' in the chain has its next == 0
705 // since there can only be one like that,
706 // this case is handled by the caller
708 p2
++; r
= r
->next
; ref_count
++;
710 p1
->reference_list
= (kmod_reference_t
*)ref_count
;
713 simple_unlock(&kmod_lock
);
715 rc
= vm_map_copyin(kernel_map
, data
, size
, TRUE
, (vm_map_copy_t
*)kmods
);
717 kmem_free(kernel_map
, data
, size
);
728 kmod_call_funcs_in_section(struct mach_header
*header
, const char *sectName
)
730 typedef void (*Routine
)(void);
734 if (header
->magic
!= MH_MAGIC
) {
735 return KERN_INVALID_ARGUMENT
;
738 routines
= (Routine
*) getsectdatafromheader(header
, SEG_TEXT
, (char *) sectName
, &size
);
739 if (!routines
) return KERN_SUCCESS
;
741 size
/= sizeof(Routine
);
742 for (i
= 0; i
< size
; i
++) {
750 kmod_initialize_cpp(kmod_info_t
*info
)
752 return kmod_call_funcs_in_section((struct mach_header
*)info
->address
, "__constructor");
756 kmod_finalize_cpp(kmod_info_t
*info
)
758 return kmod_call_funcs_in_section((struct mach_header
*)info
->address
, "__destructor");
762 kmod_default_start(struct kmod_info
*ki
, void *data
)
764 return KMOD_RETURN_SUCCESS
;
768 kmod_default_stop(struct kmod_info
*ki
, void *data
)
770 return KMOD_RETURN_SUCCESS
;
774 kmod_dump(vm_offset_t
*addr
, unsigned int cnt
)
776 vm_offset_t
* kscan_addr
= 0;
777 vm_offset_t
* rscan_addr
= 0;
779 kmod_reference_t
* r
;
782 int kmod_scan_stopped
= 0;
783 kmod_info_t
* stop_kmod
= 0;
784 int ref_scan_stopped
= 0;
785 kmod_reference_t
* stop_ref
= 0;
787 for (k
= kmod
; k
; k
= k
->next
) {
789 continue; // skip fake entries for built-in kernel components
791 if (pmap_extract(kernel_pmap
, (vm_offset_t
)k
) == 0) {
792 kdb_printf(" kmod scan stopped due to missing "
793 "kmod page: %08x\n", stop_kmod
);
796 for (i
= 0, kscan_addr
= addr
; i
< cnt
; i
++, kscan_addr
++) {
797 if ((*kscan_addr
>= k
->address
) &&
798 (*kscan_addr
< (k
->address
+ k
->size
))) {
801 kdb_printf(" Kernel loadable modules in backtrace "
802 "(with dependencies):\n");
805 kdb_printf(" %s(%s)@0x%x\n",
806 k
->name
, k
->version
, k
->address
);
808 for (r
= k
->reference_list
; r
; r
= r
->next
) {
811 if (pmap_extract(kernel_pmap
, (vm_offset_t
)r
) == 0) {
812 kdb_printf(" kmod dependency scan stopped "
813 "due to missing dependency page: %08x\n", r
);
819 if (!rinfo
->address
) {
820 continue; // skip fake entries for built-ins
823 if (pmap_extract(kernel_pmap
, (vm_offset_t
)rinfo
) == 0) {
824 kdb_printf(" kmod dependency scan stopped "
825 "due to missing kmod page: %08x\n", rinfo
);
829 kdb_printf(" dependency: %s(%s)@0x%x\n",
830 rinfo
->name
, rinfo
->version
, rinfo
->address
);
833 break; // only report this kmod for one backtrace address