X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/378393581903b274cb7a4d18e0d978071a6b592d..4bd07ac2140668789aa3ee8ec4dde4a3e0a3bba5:/osfmk/device/iokit_rpc.c diff --git a/osfmk/device/iokit_rpc.c b/osfmk/device/iokit_rpc.c index c1e4c2d51..1412f4115 100644 --- a/osfmk/device/iokit_rpc.c +++ b/osfmk/device/iokit_rpc.c @@ -1,35 +1,38 @@ /* - * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ -#include #include -#include - #include #include #include #include #include #include -#include +//#include #include #include /* spl definitions */ @@ -57,10 +60,7 @@ #include -#ifdef __ppc__ -#include -#endif -#ifdef __i386 +#if defined(__i386__) || defined(__x86_64__) #include #endif #include @@ -88,6 +88,50 @@ iokit_client_memory_for_type( vm_address_t * address, vm_size_t * size ); + +extern ppnum_t IOGetLastPageNumber(void); + +/* + * Functions imported by iokit:IOUserClient.cpp + */ + +extern ipc_port_t iokit_alloc_object_port( io_object_t obj, + ipc_kobject_type_t type ); + +extern kern_return_t iokit_destroy_object_port( ipc_port_t port ); + +extern mach_port_name_t iokit_make_send_right( task_t task, + io_object_t obj, ipc_kobject_type_t type ); + +extern kern_return_t iokit_mod_send_right( task_t task, mach_port_name_t name, mach_port_delta_t delta ); + +extern io_object_t iokit_lookup_connect_ref(io_object_t clientRef, ipc_space_t task); + +extern io_object_t iokit_lookup_connect_ref_current_task(io_object_t clientRef); + +extern void iokit_retain_port( ipc_port_t port ); +extern void iokit_release_port( ipc_port_t port ); +extern void iokit_release_port_send( ipc_port_t port ); + +extern void iokit_lock_port(ipc_port_t port); +extern void iokit_unlock_port(ipc_port_t port); + +extern kern_return_t iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type ); + +/* + * Functions imported by iokit:IOMemoryDescriptor.cpp + */ + +extern kern_return_t IOMapPages(vm_map_t map, mach_vm_address_t va, mach_vm_address_t pa, + mach_vm_size_t length, unsigned int mapFlags); + +extern kern_return_t IOUnmapPages(vm_map_t map, mach_vm_address_t va, mach_vm_size_t length); + +extern kern_return_t IOProtectCacheMode(vm_map_t map, mach_vm_address_t va, + mach_vm_size_t length, unsigned int options); + +extern unsigned int IODefaultCacheBits(addr64_t pa); + /* * Lookup a device by its port. * Doesn't consume the naked send right; produces a device reference. @@ -101,7 +145,7 @@ iokit_lookup_object_port( if (!IP_VALID(port)) return (NULL); - ip_lock(port); + iokit_lock_port(port); if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_OBJECT)) { obj = (io_object_t) port->ip_kobject; iokit_add_reference( obj ); @@ -109,7 +153,7 @@ iokit_lookup_object_port( else obj = NULL; - ip_unlock(port); + iokit_unlock_port(port); return( obj ); } @@ -123,7 +167,7 @@ iokit_lookup_connect_port( if (!IP_VALID(port)) return (NULL); - ip_lock(port); + iokit_lock_port(port); if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) { obj = (io_object_t) port->ip_kobject; iokit_add_reference( obj ); @@ -131,7 +175,7 @@ iokit_lookup_connect_port( else obj = NULL; - ip_unlock(port); + iokit_unlock_port(port); return( obj ); } @@ -141,21 +185,26 @@ iokit_lookup_connect_ref(io_object_t connectRef, ipc_space_t space) { io_object_t obj = NULL; - if (connectRef && MACH_PORT_VALID((mach_port_name_t)connectRef)) { + if (connectRef && MACH_PORT_VALID(CAST_MACH_PORT_TO_NAME(connectRef))) { ipc_port_t port; kern_return_t kr; - kr = ipc_object_translate(space, (mach_port_name_t)connectRef, MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port); + kr = ipc_object_translate(space, CAST_MACH_PORT_TO_NAME(connectRef), MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port); if (kr == KERN_SUCCESS) { - assert(IP_VALID(port)); - - if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) { - obj = (io_object_t) port->ip_kobject; - iokit_add_reference(obj); - } - - ip_unlock(port); + assert(IP_VALID(port)); + + ip_reference(port); + ip_unlock(port); + + iokit_lock_port(port); + if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) { + obj = (io_object_t) port->ip_kobject; + iokit_add_reference(obj); + } + iokit_unlock_port(port); + + ip_release(port); } } @@ -180,6 +229,26 @@ iokit_release_port( ipc_port_t port ) ipc_port_release( port ); } +EXTERN void +iokit_release_port_send( ipc_port_t port ) +{ + ipc_port_release_send( port ); +} + +extern lck_mtx_t iokit_obj_to_port_binding_lock; + +EXTERN void +iokit_lock_port( __unused ipc_port_t port ) +{ + lck_mtx_lock(&iokit_obj_to_port_binding_lock); +} + +EXTERN void +iokit_unlock_port( __unused ipc_port_t port ) +{ + lck_mtx_unlock(&iokit_obj_to_port_binding_lock); +} + /* * Get the port for a device. * Consumes a device reference; produces a naked send right. @@ -228,10 +297,6 @@ iokit_make_connect_port( return( sendPort); } - -EXTERN ipc_port_t -iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type ); - int gIOKitPortCount; EXTERN ipc_port_t @@ -252,9 +317,10 @@ iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type ) ipc_kobject_set( port, (ipc_kobject_t) obj, type); /* Request no-senders notifications on the port. */ - notify = ipc_port_make_sonce( port); ip_lock( port); + notify = ipc_port_make_sonce_locked( port); ipc_port_nsrequest( port, 1, notify, ¬ify); + /* port unlocked */ assert( notify == IP_NULL); gIOKitPortCount++; @@ -267,10 +333,12 @@ iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type ) EXTERN kern_return_t iokit_destroy_object_port( ipc_port_t port ) { + + iokit_lock_port(port); ipc_kobject_set( port, IKO_NULL, IKOT_NONE); // iokit_remove_reference( obj ); - + iokit_unlock_port(port); ipc_port_dealloc_kernel( port); gIOKitPortCount--; @@ -280,7 +348,9 @@ iokit_destroy_object_port( ipc_port_t port ) EXTERN kern_return_t iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type ) { + iokit_lock_port(port); ipc_kobject_set( port, (ipc_kobject_t) obj, type); + iokit_unlock_port(port); return( KERN_SUCCESS); } @@ -290,7 +360,7 @@ iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type ) { ipc_port_t port; ipc_port_t sendPort; - mach_port_name_t name; + mach_port_name_t name = 0; if( obj == NULL) return MACH_PORT_NULL; @@ -306,8 +376,10 @@ iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type ) kern_return_t kr; kr = ipc_object_copyout( task->itk_space, (ipc_object_t) sendPort, MACH_MSG_TYPE_PORT_SEND, TRUE, &name); - if ( kr != KERN_SUCCESS) - name = MACH_PORT_NULL; + if ( kr != KERN_SUCCESS) { + ipc_port_release_send( sendPort ); + name = MACH_PORT_NULL; + } } else if ( sendPort == IP_NULL) name = MACH_PORT_NULL; else if ( sendPort == IP_DEAD) @@ -335,14 +407,14 @@ iokit_no_senders( mach_no_senders_notification_t * notification ) { ipc_port_t port; io_object_t obj = NULL; - ipc_kobject_type_t type; + ipc_kobject_type_t type = IKOT_NONE; ipc_port_t notify; port = (ipc_port_t) notification->not_header.msgh_remote_port; // convert a port to io_object_t. if( IP_VALID(port)) { - ip_lock(port); + iokit_lock_port(port); if( ip_active(port)) { obj = (io_object_t) port->ip_kobject; type = ip_kotype( port ); @@ -352,7 +424,7 @@ iokit_no_senders( mach_no_senders_notification_t * notification ) else obj = NULL; } - ip_unlock(port); + iokit_unlock_port(port); if( obj ) { @@ -360,11 +432,17 @@ iokit_no_senders( mach_no_senders_notification_t * notification ) if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount )) { - /* Re-request no-senders notifications on the port. */ - notify = ipc_port_make_sonce( port); - ip_lock( port); - ipc_port_nsrequest( port, mscount + 1, notify, ¬ify); - assert( notify == IP_NULL); + /* Re-request no-senders notifications on the port (if still active) */ + ip_lock(port); + if (ip_active(port)) { + notify = ipc_port_make_sonce_locked(port); + ipc_port_nsrequest( port, mscount + 1, notify, ¬ify); + /* port unlocked */ + if ( notify != IP_NULL) + ipc_port_release_sonce(notify); + } else { + ip_unlock(port); + } } iokit_remove_reference( obj ); } @@ -386,7 +464,7 @@ iokit_notify( mach_msg_header_t * msg ) case MACH_NOTIFY_SEND_ONCE: case MACH_NOTIFY_DEAD_NAME: default: - printf("iokit_notify: strange notification %ld\n", msg->msgh_id); + printf("iokit_notify: strange notification %d\n", msg->msgh_id); return FALSE; } } @@ -394,43 +472,22 @@ iokit_notify( mach_msg_header_t * msg ) /* need to create a pmap function to generalize */ unsigned int IODefaultCacheBits(addr64_t pa) { - unsigned int flags; -#ifndef i386 - struct phys_entry * pp; - - // Find physical address - if ((pp = pmap_find_physentry(pa >> 12))) { - // Use physical attributes as default - // NOTE: DEVICE_PAGER_FLAGS are made to line up - flags = VM_MEM_COHERENT; /* We only support coherent memory */ - if(pp->ppLink & ppG) flags |= VM_MEM_GUARDED; /* Add in guarded if it is */ - if(pp->ppLink & ppI) flags |= VM_MEM_NOT_CACHEABLE; /* Add in cache inhibited if so */ - } else - // If no physical, just hard code attributes - flags = VM_WIMG_IO; -#else - extern pmap_paddr_t avail_end; - - if (pa < avail_end) - flags = VM_WIMG_COPYBACK; - else - flags = VM_WIMG_IO; -#endif - - return flags; + return(pmap_cache_attributes((ppnum_t)(pa >> PAGE_SHIFT))); } -kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa, - vm_size_t length, unsigned int options) +kern_return_t IOMapPages(vm_map_t map, mach_vm_address_t va, mach_vm_address_t pa, + mach_vm_size_t length, unsigned int options) { - vm_size_t off; - vm_prot_t prot; + vm_prot_t prot; unsigned int flags; + ppnum_t pagenum; pmap_t pmap = map->pmap; prot = (options & kIOMapReadOnly) ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE); + pagenum = (ppnum_t)atop_64(pa); + switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */ case kIOMapDefaultCache: @@ -446,31 +503,30 @@ kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa, flags = VM_WIMG_WTHRU; break; - case kIOWriteCombineCache: + case kIOMapWriteCombineCache: flags = VM_WIMG_WCOMB; break; case kIOMapCopybackCache: flags = VM_WIMG_COPYBACK; break; + case kIOMapCopybackInnerCache: + flags = VM_WIMG_INNERWBACK; + break; } -#if __ppc__ - // Set up a block mapped area - pmap_map_block(pmap, (addr64_t)va, (ppnum_t)(pa >> 12), (uint32_t)(length >> 12), prot, flags, 0); + pmap_set_cache_attributes(pagenum, flags); -#else -// enter each page's physical address in the target map + vm_map_set_cache_attr(map, (vm_map_offset_t)va); - for (off = 0; off < length; off += page_size) - pmap_enter(pmap, va + off, (pa + off) >> 12, prot, flags, TRUE); -#endif + // Set up a block mapped area + pmap_map_block(pmap, va, pagenum, (uint32_t) atop_64(round_page_64(length)), prot, 0, 0); return( KERN_SUCCESS ); } -kern_return_t IOUnmapPages(vm_map_t map, vm_offset_t va, vm_size_t length) +kern_return_t IOUnmapPages(vm_map_t map, mach_vm_address_t va, mach_vm_size_t length) { pmap_t pmap = map->pmap; @@ -479,8 +535,88 @@ kern_return_t IOUnmapPages(vm_map_t map, vm_offset_t va, vm_size_t length) return( KERN_SUCCESS ); } +kern_return_t IOProtectCacheMode(vm_map_t __unused map, mach_vm_address_t __unused va, + mach_vm_size_t __unused length, unsigned int __unused options) +{ + mach_vm_size_t off; + vm_prot_t prot; + unsigned int flags; + pmap_t pmap = map->pmap; + pmap_flush_context pmap_flush_context_storage; + boolean_t delayed_pmap_flush = FALSE; + + prot = (options & kIOMapReadOnly) + ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE); + + switch (options & kIOMapCacheMask) + { + // what cache mode do we need? + case kIOMapDefaultCache: + default: + return (KERN_INVALID_ARGUMENT); + + case kIOMapInhibitCache: + flags = VM_WIMG_IO; + break; + + case kIOMapWriteThruCache: + flags = VM_WIMG_WTHRU; + break; + + case kIOMapWriteCombineCache: + flags = VM_WIMG_WCOMB; + break; + + case kIOMapCopybackCache: + flags = VM_WIMG_COPYBACK; + break; + } + + pmap_flush_context_init(&pmap_flush_context_storage); + delayed_pmap_flush = FALSE; + + // enter each page's physical address in the target map + for (off = 0; off < length; off += page_size) + { + ppnum_t ppnum = pmap_find_phys(pmap, va + off); + if (ppnum) { + pmap_enter_options(pmap, va + off, ppnum, prot, VM_PROT_NONE, flags, TRUE, + PMAP_OPTIONS_NOFLUSH, (void *)&pmap_flush_context_storage); + delayed_pmap_flush = TRUE; + } + } + if (delayed_pmap_flush == TRUE) + pmap_flush(&pmap_flush_context_storage); + + return (KERN_SUCCESS); +} + +ppnum_t IOGetLastPageNumber(void) +{ +#if __i386__ || __x86_64__ + ppnum_t lastPage, highest = 0; + unsigned int idx; + + for (idx = 0; idx < pmap_memory_region_count; idx++) + { + lastPage = pmap_memory_regions[idx].end - 1; + if (lastPage > highest) + highest = lastPage; + } + return (highest); +#else +#error unknown arch +#endif +} + + void IOGetTime( mach_timespec_t * clock_time); void IOGetTime( mach_timespec_t * clock_time) { - clock_get_system_nanotime(&clock_time->tv_sec, &clock_time->tv_nsec); + clock_sec_t sec; + clock_nsec_t nsec; + clock_get_system_nanotime(&sec, &nsec); + clock_time->tv_sec = (typeof(clock_time->tv_sec))sec; + clock_time->tv_nsec = nsec; } +