xref: /xnu-11215.41.3/osfmk/kern/ipc_clock.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2000 Apple Computer, 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  * @OSF_COPYRIGHT@
30  */
31 /*
32  *	File:		kern/ipc_clock.c
33  *	Purpose:	Routines to support ipc semantics of new kernel
34  *			alarm clock facility.
35  */
36 
37 #include <mach/message.h>
38 #include <kern/host.h>
39 #include <kern/processor.h>
40 #include <kern/task.h>
41 #include <kern/thread.h>
42 #include <kern/ipc_host.h>
43 #include <kern/ipc_kobject.h>
44 #include <kern/clock.h>
45 #include <kern/misc_protos.h>
46 #include <ipc/ipc_port.h>
47 #include <ipc/ipc_space.h>
48 
49 IPC_KOBJECT_DEFINE(IKOT_CLOCK,
50     .iko_op_stable    = true,
51     .iko_op_permanent = true);
52 
53 /*
54  *	Routine:	ipc_clock_init
55  *	Purpose:
56  *		Initialize ipc control of a clock.
57  */
58 void
ipc_clock_init(clock_t clock)59 ipc_clock_init(clock_t clock)
60 {
61 	clock->cl_service = ipc_kobject_alloc_port(clock, IKOT_CLOCK,
62 	    IPC_KOBJECT_ALLOC_NONE);
63 }
64 
65 /*
66  *	Routine:	convert_port_to_clock
67  *	Purpose:
68  *		Convert from a port to a clock.
69  *		Doesn't consume the port ref
70  *		which may be null.
71  *	Conditions:
72  *		Nothing locked.
73  */
74 clock_t
convert_port_to_clock(ipc_port_t port)75 convert_port_to_clock(ipc_port_t port)
76 {
77 	clock_t clock = CLOCK_NULL;
78 
79 	if (IP_VALID(port)) {
80 		clock = ipc_kobject_get_stable(port, IKOT_CLOCK);
81 	}
82 
83 	return clock;
84 }
85 
86 /*
87  *	Routine:	convert_clock_to_port
88  *	Purpose:
89  *		Convert from a clock to a port.
90  *		Produces a naked send right which may be invalid.
91  *	Conditions:
92  *		Nothing locked.
93  */
94 ipc_port_t
convert_clock_to_port(clock_t clock)95 convert_clock_to_port(clock_t clock)
96 {
97 	return ipc_kobject_make_send(clock->cl_service, clock, IKOT_CLOCK);
98 }
99 
100 /*
101  *	Routine:	port_name_to_clock
102  *	Purpose:
103  *		Convert from a clock name to a clock pointer.
104  */
105 clock_t
port_name_to_clock(mach_port_name_t clock_name)106 port_name_to_clock(mach_port_name_t clock_name)
107 {
108 	clock_t         clock = CLOCK_NULL;
109 	ipc_space_t     space;
110 	ipc_port_t      port;
111 
112 	if (clock_name == 0) {
113 		return clock;
114 	}
115 	space = current_space();
116 	if (ipc_port_translate_send(space, clock_name, &port) != KERN_SUCCESS) {
117 		return clock;
118 	}
119 	clock = (clock_t)ipc_kobject_get_stable(port, IKOT_CLOCK);
120 	ip_mq_unlock(port);
121 	return clock;
122 }
123