]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_work_interval.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / bsd / kern / sys_work_interval.c
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/policy_internal.h>
37
38 #include <libkern/libkern.h>
39
40 int
41 work_interval_ctl(__unused proc_t p, struct work_interval_ctl_args *uap, __unused int32_t *retval)
42 {
43 uint32_t operation = uap->operation;
44 int error = 0;
45 kern_return_t kret = KERN_SUCCESS;
46 uint64_t work_interval_id;
47 struct work_interval_notification notification;
48
49 switch (operation) {
50 case WORK_INTERVAL_OPERATION_CREATE:
51 if (uap->arg == USER_ADDR_NULL || uap->work_interval_id != 0) {
52 return EINVAL;
53 }
54 if (uap->len < sizeof(work_interval_id)) {
55 return ERANGE;
56 }
57
58 /*
59 * Privilege check performed up-front, and then the work
60 * ID is allocated for use by the thread
61 */
62 error = priv_check_cred(kauth_cred_get(), PRIV_WORK_INTERVAL, 0);
63 if (error) {
64 return (error);
65 }
66
67 kret = thread_policy_create_work_interval(current_thread(),
68 &work_interval_id);
69 if (kret == KERN_SUCCESS) {
70 error = copyout(&work_interval_id, uap->arg, sizeof(work_interval_id));
71 } else {
72 error = EINVAL;
73 }
74
75 break;
76 case WORK_INTERVAL_OPERATION_DESTROY:
77 if (uap->arg != USER_ADDR_NULL || uap->work_interval_id == 0) {
78 return EINVAL;
79 }
80
81 /*
82 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
83 * operation would have allocated a work interval ID for the current
84 * thread, which the scheduler will validate.
85 */
86 kret = thread_policy_destroy_work_interval(current_thread(),
87 uap->work_interval_id);
88 if (kret != KERN_SUCCESS) {
89 error = EINVAL;
90 }
91
92 break;
93 case WORK_INTERVAL_OPERATION_NOTIFY:
94 if (uap->arg == USER_ADDR_NULL || uap->work_interval_id == 0) {
95 return EINVAL;
96 }
97 if (uap->len < sizeof(notification)) {
98 return EINVAL;
99 }
100
101 /*
102 * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE
103 * operation would have allocated a work interval ID for the current
104 * thread, which the scheduler will validate.
105 */
106 error = copyin(uap->arg, &notification, sizeof(notification));
107 if (error) {
108 break;
109 }
110
111 kret = sched_work_interval_notify(current_thread(),
112 uap->work_interval_id,
113 notification.start,
114 notification.finish,
115 notification.deadline,
116 notification.next_start,
117 notification.flags);
118 if (kret != KERN_SUCCESS) {
119 error = EINVAL;
120 break;
121 }
122
123 break;
124 default:
125 error = ENOTSUP;
126 break;
127 }
128
129 return (error);
130 }