xref: /xnu-12377.61.12/osfmk/kern/ipc_clock.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
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 
47 IPC_KOBJECT_DEFINE(IKOT_CLOCK,
48     .iko_op_movable_send = true,
49     .iko_op_stable    = true,
50     .iko_op_permanent = true);
51 
52 /*
53  *	Routine:	ipc_clock_init
54  *	Purpose:
55  *		Initialize ipc control of a clock.
56  */
57 void
ipc_clock_init(clock_t clock)58 ipc_clock_init(clock_t clock)
59 {
60 	clock->cl_service = ipc_kobject_alloc_port(clock, IKOT_CLOCK,
61 	    IPC_KOBJECT_ALLOC_NONE);
62 }
63 
64 /*
65  *	Routine:	convert_port_to_clock
66  *	Purpose:
67  *		Convert from a port to a clock.
68  *		Doesn't consume the port ref
69  *		which may be null.
70  *	Conditions:
71  *		Nothing locked.
72  */
73 clock_t
convert_port_to_clock(ipc_port_t port)74 convert_port_to_clock(ipc_port_t port)
75 {
76 	clock_t clock = CLOCK_NULL;
77 
78 	if (IP_VALID(port)) {
79 		clock = ipc_kobject_get_stable(port, IKOT_CLOCK);
80 	}
81 
82 	return clock;
83 }
84 
85 /*
86  *	Routine:	convert_clock_to_port
87  *	Purpose:
88  *		Convert from a clock to a port.
89  *		Produces a naked send right which may be invalid.
90  *	Conditions:
91  *		Nothing locked.
92  */
93 ipc_port_t
convert_clock_to_port(clock_t clock)94 convert_clock_to_port(clock_t clock)
95 {
96 	return ipc_kobject_make_send(clock->cl_service, clock, IKOT_CLOCK);
97 }
98 
99 /*
100  *	Routine:	port_name_to_clock
101  *	Purpose:
102  *		Convert from a clock name to a clock pointer.
103  */
104 clock_t
port_name_to_clock(mach_port_name_t clock_name)105 port_name_to_clock(mach_port_name_t clock_name)
106 {
107 	clock_t         clock = CLOCK_NULL;
108 	ipc_space_t     space;
109 	ipc_port_t      port;
110 
111 	if (clock_name == 0) {
112 		return clock;
113 	}
114 	space = current_space();
115 	if (ipc_port_translate_send(space, clock_name, &port) != KERN_SUCCESS) {
116 		return clock;
117 	}
118 	clock = (clock_t)ipc_kobject_get_stable(port, IKOT_CLOCK);
119 	ip_mq_unlock(port);
120 	return clock;
121 }
122