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