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));
226 extern vm_offset_t sectPRELINKB
;
227 extern int sectSizePRELINK
;
230 kmod_create_internal(kmod_info_t
*info
, kmod_t
*id
)
235 if (!info
) return KERN_INVALID_ADDRESS
;
237 // double check for page alignment
238 if ((info
->address
| info
->hdr_size
) & (PAGE_SIZE
- 1)) {
239 return KERN_INVALID_ADDRESS
;
242 isPrelink
= ((info
->address
>= sectPRELINKB
) && (info
->address
< (sectPRELINKB
+ sectSizePRELINK
)));
244 rc
= vm_map_wire(kernel_map
, info
->address
+ info
->hdr_size
,
245 info
->address
+ info
->size
, VM_PROT_DEFAULT
, FALSE
);
246 if (rc
!= KERN_SUCCESS
) {
250 #if WRITE_PROTECT_MODULE_TEXT
252 struct section
* sect
= getsectbynamefromheader(
253 (struct mach_header
*) info
->address
, "__TEXT", "__text");
256 (void) vm_map_protect(kernel_map
, round_page(sect
->addr
), trunc_page(sect
->addr
+ sect
->size
),
257 VM_PROT_READ
|VM_PROT_EXECUTE
, TRUE
);
260 #endif /* WRITE_PROTECT_MODULE_TEXT */
262 simple_lock(&kmod_lock
);
264 // check to see if already loaded
265 if (kmod_lookupbyname(info
->name
)) {
266 simple_unlock(&kmod_lock
);
268 rc
= vm_map_unwire(kernel_map
, info
->address
+ info
->hdr_size
,
269 info
->address
+ info
->size
, FALSE
);
270 assert(rc
== KERN_SUCCESS
);
272 return KERN_INVALID_ARGUMENT
;
275 info
->id
= kmod_index
++;
276 info
->reference_count
= 0;
283 simple_unlock(&kmod_lock
);
286 printf("kmod_create: %s (id %d), %d pages loaded at 0x%x, header size 0x%x\n",
287 info
->name
, info
->id
, info
->size
/ PAGE_SIZE
, info
->address
, info
->hdr_size
);
295 kmod_create(host_priv_t host_priv
,
299 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
300 return kmod_create_internal(info
, id
);
304 kmod_create_fake(const char *name
, const char *version
)
308 if (!name
|| ! version
||
309 (1 + strlen(name
) > KMOD_MAX_NAME
) ||
310 (1 + strlen(version
) > KMOD_MAX_NAME
)) {
312 return KERN_INVALID_ARGUMENT
;
315 info
= (kmod_info_t
*)kalloc(sizeof(kmod_info_t
));
317 return KERN_RESOURCE_SHORTAGE
;
321 info
->info_version
= KMOD_INFO_VERSION
;
322 bcopy(name
, info
->name
, 1 + strlen(name
));
323 bcopy(version
, info
->version
, 1 + strlen(version
)); //NIK fixed this part
324 info
->reference_count
= 1; // keep it from unloading, starting, stopping
325 info
->reference_list
= 0;
326 info
->address
= info
->size
= info
->hdr_size
= 0;
327 info
->start
= info
->stop
= 0;
329 simple_lock(&kmod_lock
);
331 // check to see if already "loaded"
332 if (kmod_lookupbyname(info
->name
)) {
333 simple_unlock(&kmod_lock
);
334 return KERN_INVALID_ARGUMENT
;
337 info
->id
= kmod_index
++;
342 simple_unlock(&kmod_lock
);
348 kmod_destroy_internal(kmod_t id
)
354 simple_lock(&kmod_lock
);
359 kmod_reference_t
*r
, *t
;
361 if (k
->reference_count
!= 0) {
362 simple_unlock(&kmod_lock
);
363 return KERN_INVALID_ARGUMENT
;
366 if (k
== p
) { // first element
371 simple_unlock(&kmod_lock
);
373 r
= k
->reference_list
;
375 r
->info
->reference_count
--;
378 kfree((vm_offset_t
)t
, sizeof(struct kmod_reference
));
382 printf("kmod_destroy: %s (id %d), deallocating %d pages starting at 0x%x\n",
383 k
->name
, k
->id
, k
->size
/ PAGE_SIZE
, k
->address
);
386 if( (k
->address
>= sectPRELINKB
) && (k
->address
< (sectPRELINKB
+ sectSizePRELINK
)))
389 virt
= ml_static_ptovirt(k
->address
);
391 ml_static_mfree( virt
, k
->size
);
396 rc
= vm_map_unwire(kernel_map
, k
->address
+ k
->hdr_size
,
397 k
->address
+ k
->size
, FALSE
);
398 assert(rc
== KERN_SUCCESS
);
400 rc
= vm_deallocate(kernel_map
, k
->address
, k
->size
);
401 assert(rc
== KERN_SUCCESS
);
409 simple_unlock(&kmod_lock
);
411 return KERN_INVALID_ARGUMENT
;
416 kmod_destroy(host_priv_t host_priv
,
419 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
420 return kmod_destroy_internal(id
);
429 mach_msg_type_number_t
*dataCount
)
431 kern_return_t rc
= KERN_SUCCESS
;
432 void * user_data
= 0;
433 kern_return_t (*func
)();
436 simple_lock(&kmod_lock
);
438 k
= kmod_lookupbyid(id
);
439 if (!k
|| k
->reference_count
) {
440 simple_unlock(&kmod_lock
);
441 rc
= KERN_INVALID_ARGUMENT
;
446 func
= (void *)k
->start
;
448 func
= (void *)k
->stop
;
451 simple_unlock(&kmod_lock
);
454 // call kmod entry point
456 if (data
&& dataCount
&& *data
&& *dataCount
) {
457 vm_map_copyout(kernel_map
, (vm_offset_t
*)&user_data
, (vm_map_copy_t
)*data
);
460 rc
= (*func
)(k
, user_data
);
465 (void) vm_deallocate(kernel_map
, (vm_offset_t
)user_data
, *dataCount
);
468 if (dataCount
) *dataCount
= 0;
475 * The retain and release calls take no user data, but the caller
476 * may have sent some in error (the MIG definition allows it).
477 * If this is the case, they will just return that same data
478 * right back to the caller (since they never touch the *data and
479 * *dataCount fields).
482 kmod_retain(kmod_t id
)
484 kern_return_t rc
= KERN_SUCCESS
;
486 kmod_info_t
*t
; // reference to
487 kmod_info_t
*f
; // reference from
488 kmod_reference_t
*r
= 0;
490 r
= (kmod_reference_t
*)kalloc(sizeof(struct kmod_reference
));
492 rc
= KERN_RESOURCE_SHORTAGE
;
496 simple_lock(&kmod_lock
);
498 t
= kmod_lookupbyid(KMOD_UNPACK_TO_ID(id
));
499 f
= kmod_lookupbyid(KMOD_UNPACK_FROM_ID(id
));
501 simple_unlock(&kmod_lock
);
502 if (r
) kfree((vm_offset_t
)r
, sizeof(struct kmod_reference
));
503 rc
= KERN_INVALID_ARGUMENT
;
507 r
->next
= f
->reference_list
;
509 f
->reference_list
= r
;
510 t
->reference_count
++;
512 simple_unlock(&kmod_lock
);
521 kmod_release(kmod_t id
)
523 kern_return_t rc
= KERN_INVALID_ARGUMENT
;
525 kmod_info_t
*t
; // reference to
526 kmod_info_t
*f
; // reference from
527 kmod_reference_t
*r
= 0;
528 kmod_reference_t
* p
;
530 simple_lock(&kmod_lock
);
532 t
= kmod_lookupbyid(KMOD_UNPACK_TO_ID(id
));
533 f
= kmod_lookupbyid(KMOD_UNPACK_FROM_ID(id
));
535 rc
= KERN_INVALID_ARGUMENT
;
539 p
= r
= f
->reference_list
;
542 if (p
== r
) { // first element
543 f
->reference_list
= r
->next
;
547 r
->info
->reference_count
--;
549 simple_unlock(&kmod_lock
);
550 kfree((vm_offset_t
)r
, sizeof(struct kmod_reference
));
558 simple_unlock(&kmod_lock
);
567 kmod_control(host_priv_t host_priv
,
569 kmod_control_flavor_t flavor
,
571 mach_msg_type_number_t
*dataCount
)
573 kern_return_t rc
= KERN_SUCCESS
;
575 if (host_priv
== HOST_PRIV_NULL
) return KERN_INVALID_HOST
;
579 case KMOD_CNTL_START
:
582 rc
= kmod_start_or_stop(id
, (flavor
== KMOD_CNTL_START
),
587 case KMOD_CNTL_RETAIN
:
589 rc
= kmod_retain(id
);
593 case KMOD_CNTL_RELEASE
:
595 rc
= kmod_release(id
);
599 case KMOD_CNTL_GET_CMD
:
602 cmd_queue_entry_t
*e
;
605 * Throw away any data the user may have sent in error.
606 * We must do this, because we are likely to return to
607 * some data for these commands (thus causing a leak of
608 * whatever data the user sent us in error).
610 if (*data
&& *dataCount
) {
611 vm_map_copy_discard(*data
);
616 simple_lock(&kmod_queue_lock
);
618 if (queue_empty(&kmod_cmd_queue
)) {
621 res
= thread_sleep_simple_lock((event_t
)&kmod_cmd_queue
,
624 if (queue_empty(&kmod_cmd_queue
)) {
625 // we must have been interrupted!
626 simple_unlock(&kmod_queue_lock
);
627 assert(res
== THREAD_INTERRUPTED
);
631 e
= (cmd_queue_entry_t
*)dequeue_head(&kmod_cmd_queue
);
633 simple_unlock(&kmod_queue_lock
);
635 rc
= vm_map_copyin(kernel_map
, e
->data
, e
->size
, TRUE
, (vm_map_copy_t
*)data
);
637 simple_lock(&kmod_queue_lock
);
638 enqueue_head(&kmod_cmd_queue
, (queue_entry_t
)e
);
639 simple_unlock(&kmod_queue_lock
);
644 *dataCount
= e
->size
;
646 kfree((vm_offset_t
)e
, sizeof(struct cmd_queue_entry
));
652 rc
= KERN_INVALID_ARGUMENT
;
660 kmod_get_info(host_t host
,
661 kmod_info_array_t
*kmods
,
662 mach_msg_type_number_t
*kmodCount
)
666 kmod_reference_t
*r
, *p2
;
669 kern_return_t rc
= KERN_SUCCESS
;
675 simple_lock(&kmod_lock
);
679 size
+= sizeof(kmod_info_t
);
680 r
= k
->reference_list
;
682 size
+=sizeof(kmod_reference_t
);
687 simple_unlock(&kmod_lock
);
688 if (!size
) return KERN_SUCCESS
;
690 rc
= kmem_alloc(kernel_map
, &data
, size
);
693 // copy kmod into data, retry if kmod's size has changed (grown)
694 // the copied out data is tweeked to figure what's what at user level
695 // change the copied out k->next pointers to point to themselves
696 // change the k->reference into a count, tack the references on
697 // the end of the data packet in the order they are found
699 simple_lock(&kmod_lock
);
700 k
= kmod
; p1
= (kmod_info_t
*)data
;
702 if ((p1
+ 1) > (kmod_info_t
*)(data
+ size
)) {
703 simple_unlock(&kmod_lock
);
704 kmem_free(kernel_map
, data
, size
);
709 if (k
->next
) p1
->next
= k
;
713 p2
= (kmod_reference_t
*)p1
;
714 k
= kmod
; p1
= (kmod_info_t
*)data
;
716 r
= k
->reference_list
; ref_count
= 0;
718 if ((p2
+ 1) > (kmod_reference_t
*)(data
+ size
)) {
719 simple_unlock(&kmod_lock
);
720 kmem_free(kernel_map
, data
, size
);
723 // note the last 'k' in the chain has its next == 0
724 // since there can only be one like that,
725 // this case is handled by the caller
727 p2
++; r
= r
->next
; ref_count
++;
729 p1
->reference_list
= (kmod_reference_t
*)ref_count
;
732 simple_unlock(&kmod_lock
);
734 rc
= vm_map_copyin(kernel_map
, data
, size
, TRUE
, (vm_map_copy_t
*)kmods
);
736 kmem_free(kernel_map
, data
, size
);
747 kmod_call_funcs_in_section(struct mach_header
*header
, const char *sectName
)
749 typedef void (*Routine
)(void);
753 if (header
->magic
!= MH_MAGIC
) {
754 return KERN_INVALID_ARGUMENT
;
757 routines
= (Routine
*) getsectdatafromheader(header
, SEG_TEXT
, (char *) sectName
, &size
);
758 if (!routines
) return KERN_SUCCESS
;
760 size
/= sizeof(Routine
);
761 for (i
= 0; i
< size
; i
++) {
769 kmod_initialize_cpp(kmod_info_t
*info
)
771 return kmod_call_funcs_in_section((struct mach_header
*)info
->address
, "__constructor");
775 kmod_finalize_cpp(kmod_info_t
*info
)
777 return kmod_call_funcs_in_section((struct mach_header
*)info
->address
, "__destructor");
781 kmod_default_start(struct kmod_info
*ki
, void *data
)
783 return KMOD_RETURN_SUCCESS
;
787 kmod_default_stop(struct kmod_info
*ki
, void *data
)
789 return KMOD_RETURN_SUCCESS
;
793 kmod_dump(vm_offset_t
*addr
, unsigned int cnt
)
795 vm_offset_t
* kscan_addr
= 0;
796 vm_offset_t
* rscan_addr
= 0;
798 kmod_reference_t
* r
;
801 int kmod_scan_stopped
= 0;
802 kmod_info_t
* stop_kmod
= 0;
803 int ref_scan_stopped
= 0;
804 kmod_reference_t
* stop_ref
= 0;
806 for (k
= kmod
; k
; k
= k
->next
) {
808 continue; // skip fake entries for built-in kernel components
810 if (pmap_find_phys(kernel_pmap
, (addr64_t
)((uintptr_t)k
)) == 0) {
811 kdb_printf(" kmod scan stopped due to missing "
812 "kmod page: %08x\n", stop_kmod
);
815 for (i
= 0, kscan_addr
= addr
; i
< cnt
; i
++, kscan_addr
++) {
816 if ((*kscan_addr
>= k
->address
) &&
817 (*kscan_addr
< (k
->address
+ k
->size
))) {
820 kdb_printf(" Kernel loadable modules in backtrace "
821 "(with dependencies):\n");
824 kdb_printf(" %s(%s)@0x%x\n",
825 k
->name
, k
->version
, k
->address
);
827 for (r
= k
->reference_list
; r
; r
= r
->next
) {
830 if (pmap_find_phys(kernel_pmap
, (addr64_t
)((uintptr_t)r
)) == 0) {
831 kdb_printf(" kmod dependency scan stopped "
832 "due to missing dependency page: %08x\n", r
);
838 if (!rinfo
->address
) {
839 continue; // skip fake entries for built-ins
842 if (pmap_find_phys(kernel_pmap
, (addr64_t
)((uintptr_t)rinfo
)) == 0) {
843 kdb_printf(" kmod dependency scan stopped "
844 "due to missing kmod page: %08x\n", rinfo
);
848 kdb_printf(" dependency: %s(%s)@0x%x\n",
849 rinfo
->name
, rinfo
->version
, rinfo
->address
);
852 break; // only report this kmod for one backtrace address