xref: /xnu-8796.141.3/bsd/kern/sys_work_interval.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2015 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions #include <sys/param.h>
29*1b191cb5SApple OSS Distributions #include <sys/kernel.h>
30*1b191cb5SApple OSS Distributions #include <sys/kernel_types.h>
31*1b191cb5SApple OSS Distributions #include <sys/sysproto.h>
32*1b191cb5SApple OSS Distributions #include <sys/priv.h>
33*1b191cb5SApple OSS Distributions #include <sys/work_interval.h>
34*1b191cb5SApple OSS Distributions #include <kern/sched_prim.h>
35*1b191cb5SApple OSS Distributions #include <kern/thread.h>
36*1b191cb5SApple OSS Distributions #include <kern/task.h>
37*1b191cb5SApple OSS Distributions #include <kern/work_interval.h>
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions #include <libkern/libkern.h>
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions int
work_interval_ctl(__unused proc_t p,struct work_interval_ctl_args * uap,__unused int32_t * retval)42*1b191cb5SApple OSS Distributions work_interval_ctl(__unused proc_t p, struct work_interval_ctl_args *uap,
43*1b191cb5SApple OSS Distributions     __unused int32_t *retval)
44*1b191cb5SApple OSS Distributions {
45*1b191cb5SApple OSS Distributions 	uint32_t        operation = uap->operation;
46*1b191cb5SApple OSS Distributions 	int             error = 0;
47*1b191cb5SApple OSS Distributions 	kern_return_t   kret = KERN_SUCCESS;
48*1b191cb5SApple OSS Distributions 	struct work_interval_notification notification;
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions 	struct work_interval_create_params create_params;
51*1b191cb5SApple OSS Distributions 	struct kern_work_interval_create_args create_args;
52*1b191cb5SApple OSS Distributions 	struct work_interval_workload_id_params workload_id_params;
53*1b191cb5SApple OSS Distributions 	struct kern_work_interval_workload_id_args workload_id_args;
54*1b191cb5SApple OSS Distributions 	mach_port_name_t port_name;
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions 	switch (operation) {
57*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_CREATE:
58*1b191cb5SApple OSS Distributions 		return ENOTSUP;
59*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_CREATE2:
60*1b191cb5SApple OSS Distributions 		if (uap->arg == USER_ADDR_NULL || uap->work_interval_id != 0) {
61*1b191cb5SApple OSS Distributions 			return EINVAL;
62*1b191cb5SApple OSS Distributions 		}
63*1b191cb5SApple OSS Distributions 		if (uap->len < sizeof(create_params)) {
64*1b191cb5SApple OSS Distributions 			return EINVAL;
65*1b191cb5SApple OSS Distributions 		}
66*1b191cb5SApple OSS Distributions 
67*1b191cb5SApple OSS Distributions 		if ((error = copyin(uap->arg, &create_params, sizeof(create_params)))) {
68*1b191cb5SApple OSS Distributions 			return error;
69*1b191cb5SApple OSS Distributions 		}
70*1b191cb5SApple OSS Distributions 
71*1b191cb5SApple OSS Distributions 		if ((error = priv_check_cred(kauth_cred_get(), PRIV_WORK_INTERVAL, 0)) != 0) {
72*1b191cb5SApple OSS Distributions 			return error;
73*1b191cb5SApple OSS Distributions 		}
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 		create_args = (struct kern_work_interval_create_args) {
76*1b191cb5SApple OSS Distributions 			.wica_id            = create_params.wicp_id,
77*1b191cb5SApple OSS Distributions 			.wica_port          = create_params.wicp_port,
78*1b191cb5SApple OSS Distributions 			.wica_create_flags  = create_params.wicp_create_flags,
79*1b191cb5SApple OSS Distributions 		};
80*1b191cb5SApple OSS Distributions 
81*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_create(current_thread(), &create_args);
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions 		/* thread already has a work interval */
84*1b191cb5SApple OSS Distributions 		if (kret == KERN_FAILURE) {
85*1b191cb5SApple OSS Distributions 			return EALREADY;
86*1b191cb5SApple OSS Distributions 		}
87*1b191cb5SApple OSS Distributions 
88*1b191cb5SApple OSS Distributions 		/* port copyout failed */
89*1b191cb5SApple OSS Distributions 		if (kret == KERN_RESOURCE_SHORTAGE) {
90*1b191cb5SApple OSS Distributions 			return ENOMEM;
91*1b191cb5SApple OSS Distributions 		}
92*1b191cb5SApple OSS Distributions 
93*1b191cb5SApple OSS Distributions 		/* some other failure */
94*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
95*1b191cb5SApple OSS Distributions 			return EINVAL;
96*1b191cb5SApple OSS Distributions 		}
97*1b191cb5SApple OSS Distributions 
98*1b191cb5SApple OSS Distributions 		create_params = (struct work_interval_create_params) {
99*1b191cb5SApple OSS Distributions 			.wicp_id = create_args.wica_id,
100*1b191cb5SApple OSS Distributions 			.wicp_port = create_args.wica_port,
101*1b191cb5SApple OSS Distributions 			.wicp_create_flags = create_args.wica_create_flags,
102*1b191cb5SApple OSS Distributions 		};
103*1b191cb5SApple OSS Distributions 
104*1b191cb5SApple OSS Distributions 		if ((error = copyout(&create_params, uap->arg, sizeof(create_params)))) {
105*1b191cb5SApple OSS Distributions 			kern_work_interval_destroy(current_thread(), create_args.wica_id);
106*1b191cb5SApple OSS Distributions 			return error;
107*1b191cb5SApple OSS Distributions 		}
108*1b191cb5SApple OSS Distributions 		break;
109*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_GET_FLAGS:
110*1b191cb5SApple OSS Distributions 		if (uap->arg == USER_ADDR_NULL || uap->len < sizeof(create_params)) {
111*1b191cb5SApple OSS Distributions 			return EINVAL;
112*1b191cb5SApple OSS Distributions 		}
113*1b191cb5SApple OSS Distributions 
114*1b191cb5SApple OSS Distributions 		port_name = (mach_port_name_t) uap->work_interval_id;
115*1b191cb5SApple OSS Distributions 		if (!MACH_PORT_VALID(port_name)) {
116*1b191cb5SApple OSS Distributions 			return EINVAL;
117*1b191cb5SApple OSS Distributions 		}
118*1b191cb5SApple OSS Distributions 
119*1b191cb5SApple OSS Distributions 		create_params = (struct work_interval_create_params) {
120*1b191cb5SApple OSS Distributions 			.wicp_port = port_name
121*1b191cb5SApple OSS Distributions 		};
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_get_flags_from_port(port_name, &create_params.wicp_create_flags);
124*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
125*1b191cb5SApple OSS Distributions 			return EINVAL;
126*1b191cb5SApple OSS Distributions 		}
127*1b191cb5SApple OSS Distributions 
128*1b191cb5SApple OSS Distributions 		if ((error = copyout(&create_params, uap->arg, sizeof(create_params)))) {
129*1b191cb5SApple OSS Distributions 			return error;
130*1b191cb5SApple OSS Distributions 		}
131*1b191cb5SApple OSS Distributions 		break;
132*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_SET_NAME:
133*1b191cb5SApple OSS Distributions 		if (uap->arg == USER_ADDR_NULL || uap->len < WORK_INTERVAL_NAME_MAX) {
134*1b191cb5SApple OSS Distributions 			return EINVAL;
135*1b191cb5SApple OSS Distributions 		}
136*1b191cb5SApple OSS Distributions 		port_name = (mach_port_name_t) uap->work_interval_id;
137*1b191cb5SApple OSS Distributions 		if (!MACH_PORT_VALID(port_name)) {
138*1b191cb5SApple OSS Distributions 			return EINVAL;
139*1b191cb5SApple OSS Distributions 		}
140*1b191cb5SApple OSS Distributions 		size_t wi_name_len = 0;
141*1b191cb5SApple OSS Distributions 		char wi_name[WORK_INTERVAL_NAME_MAX];
142*1b191cb5SApple OSS Distributions 		if ((error = copyinstr(uap->arg, wi_name, sizeof(wi_name), &wi_name_len)) != 0) {
143*1b191cb5SApple OSS Distributions 			return error;
144*1b191cb5SApple OSS Distributions 		}
145*1b191cb5SApple OSS Distributions 
146*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_set_name(port_name, wi_name, wi_name_len);
147*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
148*1b191cb5SApple OSS Distributions 			return EINVAL;
149*1b191cb5SApple OSS Distributions 		}
150*1b191cb5SApple OSS Distributions 		break;
151*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_SET_WORKLOAD_ID:
152*1b191cb5SApple OSS Distributions 		if (uap->arg == USER_ADDR_NULL ||
153*1b191cb5SApple OSS Distributions 		    uap->len < sizeof(struct work_interval_workload_id_params)) {
154*1b191cb5SApple OSS Distributions 			return EINVAL;
155*1b191cb5SApple OSS Distributions 		}
156*1b191cb5SApple OSS Distributions 		port_name = (mach_port_name_t) uap->work_interval_id;
157*1b191cb5SApple OSS Distributions 		if (!MACH_PORT_VALID(port_name)) {
158*1b191cb5SApple OSS Distributions 			return EINVAL;
159*1b191cb5SApple OSS Distributions 		}
160*1b191cb5SApple OSS Distributions 		if ((error = copyin(uap->arg, &workload_id_params,
161*1b191cb5SApple OSS Distributions 		    sizeof(workload_id_params)))) {
162*1b191cb5SApple OSS Distributions 			return error;
163*1b191cb5SApple OSS Distributions 		}
164*1b191cb5SApple OSS Distributions 
165*1b191cb5SApple OSS Distributions 		size_t wlid_name_len = 0;
166*1b191cb5SApple OSS Distributions 		char wlid_name[WORK_INTERVAL_WORKLOAD_ID_NAME_MAX] = {};
167*1b191cb5SApple OSS Distributions 		user_addr_t wlidp_name = CAST_USER_ADDR_T(workload_id_params.wlidp_name);
168*1b191cb5SApple OSS Distributions 		if (wlidp_name != USER_ADDR_NULL) {
169*1b191cb5SApple OSS Distributions 			if ((error = copyinstr(wlidp_name, wlid_name, sizeof(wlid_name),
170*1b191cb5SApple OSS Distributions 			    &wlid_name_len)) != 0) {
171*1b191cb5SApple OSS Distributions 				return error;
172*1b191cb5SApple OSS Distributions 			}
173*1b191cb5SApple OSS Distributions 		}
174*1b191cb5SApple OSS Distributions 
175*1b191cb5SApple OSS Distributions 		workload_id_args = (struct kern_work_interval_workload_id_args) {
176*1b191cb5SApple OSS Distributions 			.wlida_flags = workload_id_params.wlidp_flags,
177*1b191cb5SApple OSS Distributions 			.wlida_wicreate_flags = workload_id_params.wlidp_wicreate_flags,
178*1b191cb5SApple OSS Distributions 			.wlida_name = wlid_name,
179*1b191cb5SApple OSS Distributions 		};
180*1b191cb5SApple OSS Distributions 
181*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_set_workload_id(port_name, &workload_id_args);
182*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
183*1b191cb5SApple OSS Distributions 			return EINVAL;
184*1b191cb5SApple OSS Distributions 		}
185*1b191cb5SApple OSS Distributions 
186*1b191cb5SApple OSS Distributions 		workload_id_params = (struct work_interval_workload_id_params) {
187*1b191cb5SApple OSS Distributions 			.wlidp_flags = workload_id_args.wlida_flags,
188*1b191cb5SApple OSS Distributions 			.wlidp_wicreate_flags = workload_id_args.wlida_wicreate_flags,
189*1b191cb5SApple OSS Distributions 			.wlidp_syscall_mask = {
190*1b191cb5SApple OSS Distributions 				[0] = workload_id_args.wlida_syscall_mask[0],
191*1b191cb5SApple OSS Distributions 				[1] = workload_id_args.wlida_syscall_mask[1],
192*1b191cb5SApple OSS Distributions 			},
193*1b191cb5SApple OSS Distributions 		};
194*1b191cb5SApple OSS Distributions 
195*1b191cb5SApple OSS Distributions 		if ((error = copyout(&workload_id_params, uap->arg,
196*1b191cb5SApple OSS Distributions 		    sizeof(workload_id_params)))) {
197*1b191cb5SApple OSS Distributions 			return error;
198*1b191cb5SApple OSS Distributions 		}
199*1b191cb5SApple OSS Distributions 		break;
200*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_DESTROY:
201*1b191cb5SApple OSS Distributions 		if (uap->arg != USER_ADDR_NULL || uap->work_interval_id == 0) {
202*1b191cb5SApple OSS Distributions 			return EINVAL;
203*1b191cb5SApple OSS Distributions 		}
204*1b191cb5SApple OSS Distributions 
205*1b191cb5SApple OSS Distributions 		/*
206*1b191cb5SApple OSS Distributions 		 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
207*1b191cb5SApple OSS Distributions 		 * operation would have allocated a work interval ID for the current
208*1b191cb5SApple OSS Distributions 		 * thread, which the scheduler will validate.
209*1b191cb5SApple OSS Distributions 		 */
210*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_destroy(current_thread(), uap->work_interval_id);
211*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
212*1b191cb5SApple OSS Distributions 			return EINVAL;
213*1b191cb5SApple OSS Distributions 		}
214*1b191cb5SApple OSS Distributions 
215*1b191cb5SApple OSS Distributions 		break;
216*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_NOTIFY:
217*1b191cb5SApple OSS Distributions 		if (uap->arg == USER_ADDR_NULL || uap->work_interval_id == 0) {
218*1b191cb5SApple OSS Distributions 			return EINVAL;
219*1b191cb5SApple OSS Distributions 		}
220*1b191cb5SApple OSS Distributions 
221*1b191cb5SApple OSS Distributions 		if (uap->len < sizeof(notification)) {
222*1b191cb5SApple OSS Distributions 			return EINVAL;
223*1b191cb5SApple OSS Distributions 		}
224*1b191cb5SApple OSS Distributions 
225*1b191cb5SApple OSS Distributions 		/*
226*1b191cb5SApple OSS Distributions 		 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
227*1b191cb5SApple OSS Distributions 		 * operation would have allocated a work interval ID for the current
228*1b191cb5SApple OSS Distributions 		 * thread, which the scheduler will validate.
229*1b191cb5SApple OSS Distributions 		 */
230*1b191cb5SApple OSS Distributions 		if ((error = copyin(uap->arg, &notification, sizeof(notification)))) {
231*1b191cb5SApple OSS Distributions 			return error;
232*1b191cb5SApple OSS Distributions 		}
233*1b191cb5SApple OSS Distributions 
234*1b191cb5SApple OSS Distributions 
235*1b191cb5SApple OSS Distributions 		struct kern_work_interval_args kwi_args = {
236*1b191cb5SApple OSS Distributions 			.work_interval_id   = uap->work_interval_id,
237*1b191cb5SApple OSS Distributions 			.start              = notification.start,
238*1b191cb5SApple OSS Distributions 			.finish             = notification.finish,
239*1b191cb5SApple OSS Distributions 			.deadline           = notification.deadline,
240*1b191cb5SApple OSS Distributions 			.next_start         = notification.next_start,
241*1b191cb5SApple OSS Distributions 			.notify_flags       = notification.notify_flags,
242*1b191cb5SApple OSS Distributions 			.create_flags       = notification.create_flags,
243*1b191cb5SApple OSS Distributions 		};
244*1b191cb5SApple OSS Distributions 
245*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_notify(current_thread(), &kwi_args);
246*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
247*1b191cb5SApple OSS Distributions 			return EINVAL;
248*1b191cb5SApple OSS Distributions 		}
249*1b191cb5SApple OSS Distributions 
250*1b191cb5SApple OSS Distributions 		break;
251*1b191cb5SApple OSS Distributions 	case WORK_INTERVAL_OPERATION_JOIN:
252*1b191cb5SApple OSS Distributions 		if (uap->arg != USER_ADDR_NULL) {
253*1b191cb5SApple OSS Distributions 			return EINVAL;
254*1b191cb5SApple OSS Distributions 		}
255*1b191cb5SApple OSS Distributions 
256*1b191cb5SApple OSS Distributions 		/*
257*1b191cb5SApple OSS Distributions 		 * No privilege check, because the work interval port
258*1b191cb5SApple OSS Distributions 		 * is a capability.
259*1b191cb5SApple OSS Distributions 		 */
260*1b191cb5SApple OSS Distributions 		kret = kern_work_interval_join(current_thread(),
261*1b191cb5SApple OSS Distributions 		    (mach_port_name_t)uap->work_interval_id);
262*1b191cb5SApple OSS Distributions 		if (kret != KERN_SUCCESS) {
263*1b191cb5SApple OSS Distributions 			return EINVAL;
264*1b191cb5SApple OSS Distributions 		}
265*1b191cb5SApple OSS Distributions 
266*1b191cb5SApple OSS Distributions 		break;
267*1b191cb5SApple OSS Distributions 
268*1b191cb5SApple OSS Distributions 	default:
269*1b191cb5SApple OSS Distributions 		return ENOTSUP;
270*1b191cb5SApple OSS Distributions 	}
271*1b191cb5SApple OSS Distributions 
272*1b191cb5SApple OSS Distributions 	return error;
273*1b191cb5SApple OSS Distributions }
274