]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/sys_work_interval.c
xnu-4570.51.1.tar.gz
[apple/xnu.git] / bsd / kern / sys_work_interval.c
CommitLineData
3e170ce0
A
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>
a39ff7e2 36#include <kern/task.h>
5ba3f43e 37#include <kern/work_interval.h>
39037602 38
3e170ce0
A
39#include <libkern/libkern.h>
40
41int
5ba3f43e
A
42work_interval_ctl(__unused proc_t p, struct work_interval_ctl_args *uap,
43 __unused int32_t *retval)
3e170ce0 44{
5ba3f43e
A
45 uint32_t operation = uap->operation;
46 int error = 0;
47 kern_return_t kret = KERN_SUCCESS;
48 struct work_interval_notification notification;
49
5ba3f43e
A
50 struct work_interval_create_params create_params;
51 struct kern_work_interval_create_args create_args;
3e170ce0
A
52
53 switch (operation) {
54 case WORK_INTERVAL_OPERATION_CREATE:
5ba3f43e
A
55 return ENOTSUP;
56 case WORK_INTERVAL_OPERATION_CREATE2:
57 if (uap->arg == USER_ADDR_NULL || uap->work_interval_id != 0)
58 return EINVAL;
59 if (uap->len < sizeof(create_params))
3e170ce0 60 return EINVAL;
3e170ce0 61
a39ff7e2 62 if ((error = copyin(uap->arg, &create_params, sizeof(create_params))))
5ba3f43e 63 return error;
3e170ce0 64
a39ff7e2 65 if ((error = priv_check_cred(kauth_cred_get(), PRIV_WORK_INTERVAL, 0)) != 0) {
5ba3f43e 66 return error;
a39ff7e2 67 }
5ba3f43e
A
68
69 create_args = (struct kern_work_interval_create_args) {
70 .wica_id = create_params.wicp_id,
71 .wica_port = create_params.wicp_port,
72 .wica_create_flags = create_params.wicp_create_flags,
73 };
74
75 kret = kern_work_interval_create(current_thread(), &create_args);
76
77 /* thread already has a work interval */
78 if (kret == KERN_FAILURE)
79 return EALREADY;
80
81 /* port copyout failed */
82 if (kret == KERN_RESOURCE_SHORTAGE)
83 return ENOMEM;
84
85 /* some other failure */
86 if (kret != KERN_SUCCESS)
87 return EINVAL;
88
89 create_params = (struct work_interval_create_params) {
90 .wicp_id = create_args.wica_id,
91 .wicp_port = create_args.wica_port,
92 .wicp_create_flags = create_args.wica_create_flags,
93 };
94
a39ff7e2
A
95 if ((error = copyout(&create_params, uap->arg, sizeof(create_params)))) {
96 kern_work_interval_destroy(current_thread(), create_args.wica_id);
5ba3f43e 97 return error;
a39ff7e2 98 }
3e170ce0
A
99 break;
100 case WORK_INTERVAL_OPERATION_DESTROY:
101 if (uap->arg != USER_ADDR_NULL || uap->work_interval_id == 0) {
102 return EINVAL;
103 }
104
105 /*
106 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
107 * operation would have allocated a work interval ID for the current
108 * thread, which the scheduler will validate.
109 */
5ba3f43e
A
110 kret = kern_work_interval_destroy(current_thread(), uap->work_interval_id);
111 if (kret != KERN_SUCCESS)
112 return EINVAL;
3e170ce0
A
113
114 break;
115 case WORK_INTERVAL_OPERATION_NOTIFY:
5ba3f43e 116 if (uap->arg == USER_ADDR_NULL || uap->work_interval_id == 0)
3e170ce0 117 return EINVAL;
5ba3f43e
A
118
119 if (uap->len < sizeof(notification))
3e170ce0 120 return EINVAL;
3e170ce0
A
121
122 /*
123 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
124 * operation would have allocated a work interval ID for the current
125 * thread, which the scheduler will validate.
126 */
5ba3f43e
A
127 if ((error = copyin(uap->arg, &notification, sizeof(notification))))
128 return error;
3e170ce0 129
5ba3f43e
A
130 struct kern_work_interval_args kwi_args = {
131 .work_interval_id = uap->work_interval_id,
132 .start = notification.start,
133 .finish = notification.finish,
134 .deadline = notification.deadline,
135 .next_start = notification.next_start,
136 .notify_flags = notification.notify_flags,
137 .create_flags = notification.create_flags,
138 };
139
140 kret = kern_work_interval_notify(current_thread(), &kwi_args);
141 if (kret != KERN_SUCCESS)
142 return EINVAL;
143
144 break;
145 case WORK_INTERVAL_OPERATION_JOIN:
146 if (uap->arg != USER_ADDR_NULL) {
147 return EINVAL;
3e170ce0
A
148 }
149
5ba3f43e
A
150 /*
151 * No privilege check, because the work interval port
152 * is a capability.
153 */
154 kret = kern_work_interval_join(current_thread(),
155 (mach_port_name_t)uap->work_interval_id);
156 if (kret != KERN_SUCCESS)
157 return EINVAL;
158
3e170ce0 159 break;
5ba3f43e 160
3e170ce0 161 default:
5ba3f43e 162 return ENOTSUP;
3e170ce0
A
163 }
164
165 return (error);
166}
5ba3f43e 167