xref: /xnu-8020.140.41/bsd/kern/sys_work_interval.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * Copyright (c) 2015 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 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/kernel_types.h>
31 #include <sys/sysproto.h>
32 #include <sys/priv.h>
33 #include <sys/work_interval.h>
34 #include <kern/sched_prim.h>
35 #include <kern/thread.h>
36 #include <kern/task.h>
37 #include <kern/work_interval.h>
38 
39 #include <libkern/libkern.h>
40 
41 int
work_interval_ctl(__unused proc_t p,struct work_interval_ctl_args * uap,__unused int32_t * retval)42 work_interval_ctl(__unused proc_t p, struct work_interval_ctl_args *uap,
43     __unused int32_t *retval)
44 {
45 	uint32_t        operation = uap->operation;
46 	int             error = 0;
47 	kern_return_t   kret = KERN_SUCCESS;
48 	struct work_interval_notification notification;
49 
50 	struct work_interval_create_params create_params;
51 	struct kern_work_interval_create_args create_args;
52 	mach_port_name_t port_name;
53 
54 	switch (operation) {
55 	case WORK_INTERVAL_OPERATION_CREATE:
56 		return ENOTSUP;
57 	case WORK_INTERVAL_OPERATION_CREATE2:
58 		if (uap->arg == USER_ADDR_NULL || uap->work_interval_id != 0) {
59 			return EINVAL;
60 		}
61 		if (uap->len < sizeof(create_params)) {
62 			return EINVAL;
63 		}
64 
65 		if ((error = copyin(uap->arg, &create_params, sizeof(create_params)))) {
66 			return error;
67 		}
68 
69 		if ((error = priv_check_cred(kauth_cred_get(), PRIV_WORK_INTERVAL, 0)) != 0) {
70 			return error;
71 		}
72 
73 		create_args = (struct kern_work_interval_create_args) {
74 			.wica_id            = create_params.wicp_id,
75 			.wica_port          = create_params.wicp_port,
76 			.wica_create_flags  = create_params.wicp_create_flags,
77 		};
78 
79 		kret = kern_work_interval_create(current_thread(), &create_args);
80 
81 		/* thread already has a work interval */
82 		if (kret == KERN_FAILURE) {
83 			return EALREADY;
84 		}
85 
86 		/* port copyout failed */
87 		if (kret == KERN_RESOURCE_SHORTAGE) {
88 			return ENOMEM;
89 		}
90 
91 		/* some other failure */
92 		if (kret != KERN_SUCCESS) {
93 			return EINVAL;
94 		}
95 
96 		create_params = (struct work_interval_create_params) {
97 			.wicp_id = create_args.wica_id,
98 			.wicp_port = create_args.wica_port,
99 			.wicp_create_flags = create_args.wica_create_flags,
100 		};
101 
102 		if ((error = copyout(&create_params, uap->arg, sizeof(create_params)))) {
103 			kern_work_interval_destroy(current_thread(), create_args.wica_id);
104 			return error;
105 		}
106 		break;
107 	case WORK_INTERVAL_OPERATION_GET_FLAGS:
108 		if (uap->arg == USER_ADDR_NULL || uap->len < sizeof(create_params)) {
109 			return EINVAL;
110 		}
111 
112 		port_name = (mach_port_name_t) uap->work_interval_id;
113 		if (!MACH_PORT_VALID(port_name)) {
114 			return EINVAL;
115 		}
116 
117 		create_params = (struct work_interval_create_params) {
118 			.wicp_port = port_name
119 		};
120 
121 		kret = kern_work_interval_get_flags_from_port(port_name, &create_params.wicp_create_flags);
122 		if (kret != KERN_SUCCESS) {
123 			return EINVAL;
124 		}
125 
126 		if ((error = copyout(&create_params, uap->arg, sizeof(create_params)))) {
127 			return error;
128 		}
129 		break;
130 	case WORK_INTERVAL_OPERATION_DESTROY:
131 		if (uap->arg != USER_ADDR_NULL || uap->work_interval_id == 0) {
132 			return EINVAL;
133 		}
134 
135 		/*
136 		 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
137 		 * operation would have allocated a work interval ID for the current
138 		 * thread, which the scheduler will validate.
139 		 */
140 		kret = kern_work_interval_destroy(current_thread(), uap->work_interval_id);
141 		if (kret != KERN_SUCCESS) {
142 			return EINVAL;
143 		}
144 
145 		break;
146 	case WORK_INTERVAL_OPERATION_NOTIFY:
147 		if (uap->arg == USER_ADDR_NULL || uap->work_interval_id == 0) {
148 			return EINVAL;
149 		}
150 
151 		if (uap->len < sizeof(notification)) {
152 			return EINVAL;
153 		}
154 
155 		/*
156 		 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
157 		 * operation would have allocated a work interval ID for the current
158 		 * thread, which the scheduler will validate.
159 		 */
160 		if ((error = copyin(uap->arg, &notification, sizeof(notification)))) {
161 			return error;
162 		}
163 
164 
165 		struct kern_work_interval_args kwi_args = {
166 			.work_interval_id   = uap->work_interval_id,
167 			.start              = notification.start,
168 			.finish             = notification.finish,
169 			.deadline           = notification.deadline,
170 			.next_start         = notification.next_start,
171 			.notify_flags       = notification.notify_flags,
172 			.create_flags       = notification.create_flags,
173 		};
174 
175 		kret = kern_work_interval_notify(current_thread(), &kwi_args);
176 		if (kret != KERN_SUCCESS) {
177 			return EINVAL;
178 		}
179 
180 		break;
181 	case WORK_INTERVAL_OPERATION_JOIN:
182 		if (uap->arg != USER_ADDR_NULL) {
183 			return EINVAL;
184 		}
185 
186 		/*
187 		 * No privilege check, because the work interval port
188 		 * is a capability.
189 		 */
190 		kret = kern_work_interval_join(current_thread(),
191 		    (mach_port_name_t)uap->work_interval_id);
192 		if (kret != KERN_SUCCESS) {
193 			return EINVAL;
194 		}
195 
196 		break;
197 
198 	default:
199 		return ENOTSUP;
200 	}
201 
202 	return error;
203 }
204