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