Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /* * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. * * @APPLE_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 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. * * @APPLE_LICENSE_HEADER_END@ */ /* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * * HISTORY * * 29 June 2000 (debo) * Created. */ #include <mach/mach_types.h> #include <mach/mach_traps.h> #include <mach/mach_port_server.h> #include <mach/mk_timer.h> #include <ipc/ipc_space.h> #include <kern/mk_timer.h> #include <kern/thread_call.h> static zone_t mk_timer_zone; static mach_port_qos_t mk_timer_qos = { FALSE, TRUE, 0, sizeof (mk_timer_expire_msg_t) }; static void mk_timer_expire( void *p0, void *p1); mach_port_name_t mk_timer_create_trap( __unused struct mk_timer_create_trap_args *args) { mk_timer_t timer; ipc_space_t myspace = current_space(); mach_port_name_t name = MACH_PORT_NULL; ipc_port_t port; kern_return_t result; timer = (mk_timer_t)zalloc(mk_timer_zone); if (timer == NULL) return (MACH_PORT_NULL); result = mach_port_allocate_qos(myspace, MACH_PORT_RIGHT_RECEIVE, &mk_timer_qos, &name); if (result == KERN_SUCCESS) result = ipc_port_translate_receive(myspace, name, &port); if (result != KERN_SUCCESS) { zfree(mk_timer_zone, timer); return (MACH_PORT_NULL); } simple_lock_init(&timer->lock, 0); call_entry_setup(&timer->call_entry, mk_timer_expire, timer); timer->is_armed = timer->is_dead = FALSE; timer->active = 0; timer->port = port; ipc_kobject_set_atomically(port, (ipc_kobject_t)timer, IKOT_TIMER); port->ip_srights++; ip_reference(port); ip_unlock(port); return (name); } void mk_timer_port_destroy( ipc_port_t port) { mk_timer_t timer = NULL; ip_lock(port); if (ip_kotype(port) == IKOT_TIMER) { timer = (mk_timer_t)port->ip_kobject; assert(timer != NULL); ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE); simple_lock(&timer->lock); assert(timer->port == port); } ip_unlock(port); if (timer != NULL) { if (thread_call_cancel(&timer->call_entry)) timer->active--; timer->is_armed = FALSE; timer->is_dead = TRUE; if (timer->active == 0) { simple_unlock(&timer->lock); zfree(mk_timer_zone, timer); ipc_port_release_send(port); return; } simple_unlock(&timer->lock); } } void mk_timer_init(void) { int s = sizeof (mk_timer_data_t); assert(!(mk_timer_zone != NULL)); mk_timer_zone = zinit(s, (4096 * s), (16 * s), "mk_timer"); } static void mk_timer_expire( void *p0, __unused void *p1) { mk_timer_t timer = p0; ipc_port_t port; simple_lock(&timer->lock); if (timer->active > 1) { timer->active--; simple_unlock(&timer->lock); return; } port = timer->port; assert(port != IP_NULL); assert(timer->active == 1); while (timer->is_armed && timer->active == 1) { mk_timer_expire_msg_t msg; timer->is_armed = FALSE; simple_unlock(&timer->lock); msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); msg.header.msgh_remote_port = port; msg.header.msgh_local_port = MACH_PORT_NULL; msg.header.msgh_reserved = msg.header.msgh_id = 0; msg.unused[0] = msg.unused[1] = msg.unused[2] = 0; (void) mach_msg_send_from_kernel(&msg.header, sizeof (msg)); simple_lock(&timer->lock); } if (--timer->active == 0 && timer->is_dead) { simple_unlock(&timer->lock); zfree(mk_timer_zone, timer); ipc_port_release_send(port); return; } simple_unlock(&timer->lock); } kern_return_t mk_timer_destroy_trap( struct mk_timer_destroy_trap_args *args) { mach_port_name_t name = args->name; ipc_space_t myspace = current_space(); ipc_port_t port; kern_return_t result; result = ipc_port_translate_receive(myspace, name, &port); if (result != KERN_SUCCESS) return (result); if (ip_kotype(port) == IKOT_TIMER) { ip_unlock(port); result = mach_port_destroy(myspace, name); } else { ip_unlock(port); result = KERN_INVALID_ARGUMENT; } return (result); } kern_return_t mk_timer_arm_trap( struct mk_timer_arm_trap_args *args) { mach_port_name_t name = args->name; uint64_t expire_time = args->expire_time; mk_timer_t timer; ipc_space_t myspace = current_space(); ipc_port_t port; kern_return_t result; result = ipc_port_translate_receive(myspace, name, &port); if (result != KERN_SUCCESS) return (result); if (ip_kotype(port) == IKOT_TIMER) { timer = (mk_timer_t)port->ip_kobject; assert(timer != NULL); simple_lock(&timer->lock); assert(timer->port == port); ip_unlock(port); if (!timer->is_dead) { timer->is_armed = TRUE; if (!thread_call_enter_delayed(&timer->call_entry, expire_time)) timer->active++; } simple_unlock(&timer->lock); } else { ip_unlock(port); result = KERN_INVALID_ARGUMENT; } return (result); } kern_return_t mk_timer_cancel_trap( struct mk_timer_cancel_trap_args *args) { mach_port_name_t name = args->name; mach_vm_address_t result_time_addr = args->result_time; uint64_t armed_time = 0; mk_timer_t timer; ipc_space_t myspace = current_space(); ipc_port_t port; kern_return_t result; result = ipc_port_translate_receive(myspace, name, &port); if (result != KERN_SUCCESS) return (result); if (ip_kotype(port) == IKOT_TIMER) { timer = (mk_timer_t)port->ip_kobject; assert(timer != NULL); simple_lock(&timer->lock); assert(timer->port == port); ip_unlock(port); if (timer->is_armed) { armed_time = timer->call_entry.deadline; if (thread_call_cancel(&timer->call_entry)) timer->active--; timer->is_armed = FALSE; } simple_unlock(&timer->lock); } else { ip_unlock(port); result = KERN_INVALID_ARGUMENT; } if (result == KERN_SUCCESS) if ( result_time_addr != 0 && copyout((void *)&armed_time, result_time_addr, sizeof (armed_time)) != 0 ) result = KERN_FAILURE; return (result); } |