]> git.saurik.com Git - apple/xnu.git/blob - osfmk/chud/chud_osfmk_callback.c
xnu-1228.tar.gz
[apple/xnu.git] / osfmk / chud / chud_osfmk_callback.c
1 /*
2 * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <stdint.h>
30 #include <mach/boolean.h>
31 #include <mach/mach_types.h>
32
33 #include <kern/kern_types.h>
34 #include <kern/processor.h>
35 #include <kern/timer_call.h>
36 #include <kern/thread_call.h>
37 #include <kern/kalloc.h>
38 #include <kern/thread.h>
39
40 #include <libkern/OSAtomic.h>
41
42 #include <machine/machine_routines.h>
43 #include <machine/cpu_data.h>
44
45 #include <chud/chud_xnu.h>
46 #include <chud/chud_xnu_private.h>
47
48 #pragma mark **** timer ****
49
50 __private_extern__ chud_timer_t
51 chudxnu_timer_alloc(chudxnu_timer_callback_func_t func, uint32_t param0)
52 {
53 return (chud_timer_t)thread_call_allocate((thread_call_func_t)func, (thread_call_param_t)param0);
54 }
55
56 __private_extern__ kern_return_t
57 chudxnu_timer_callback_enter(
58 chud_timer_t timer,
59 uint32_t param1,
60 uint32_t time,
61 uint32_t units)
62 {
63 uint64_t t_delay;
64 clock_interval_to_deadline(time, units, &t_delay);
65 thread_call_enter1_delayed((thread_call_t)timer, (thread_call_param_t)param1, t_delay);
66 return KERN_SUCCESS;
67 }
68
69 __private_extern__ kern_return_t
70 chudxnu_timer_callback_cancel(chud_timer_t timer)
71 {
72 thread_call_cancel((thread_call_t)timer);
73 return KERN_SUCCESS;
74 }
75
76 __private_extern__ kern_return_t
77 chudxnu_timer_free(chud_timer_t timer)
78 {
79 thread_call_cancel((thread_call_t)timer);
80 thread_call_free((thread_call_t)timer);
81 return KERN_SUCCESS;
82 }
83
84 static chudxnu_dtrace_callback_t
85 dtrace_callback = (chudxnu_dtrace_callback_t) NULL;
86
87 kern_return_t
88 chudxnu_dtrace_callback(uint64_t selector, uint64_t *args, uint32_t count)
89 {
90 /* it's not an error if no callback is hooked up */
91 kern_return_t ret = KERN_SUCCESS;
92
93 /* Make a local stack copy of the function ptr */
94 chudxnu_dtrace_callback_t fn = dtrace_callback;
95
96 if(fn) {
97 ret = fn(selector, args, count);
98 }
99
100 return ret;
101 }
102
103 __private_extern__ void
104 chudxnu_dtrace_callback_enter(chudxnu_dtrace_callback_t fn)
105 {
106 chudxnu_dtrace_callback_t old_fn = dtrace_callback;
107
108 /* Atomically clear the call back */
109 while(!OSCompareAndSwap((UInt32)old_fn, (UInt32)fn,
110 (volatile UInt32 *) &dtrace_callback)) {
111 old_fn = dtrace_callback;
112 }
113 }
114
115 __private_extern__ void
116 chudxnu_dtrace_callback_cancel(void)
117 {
118 chudxnu_dtrace_callback_enter(NULL);
119 }
120