1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/* $OpenBSD: linux_time.c,v 1.5 2013/10/25 04:51:39 guenther Exp $ */
/*
* Copyright (c) 2010, 2011 Paul Irofti <pirofti@openbsd.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <sys/signal.h>
#include <sys/stdint.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/syscallargs.h>
#include <compat/linux/linux_types.h>
#include <compat/linux/linux_fcntl.h>
#include <compat/linux/linux_misc.h>
#include <compat/linux/linux_mmap.h>
#include <compat/linux/linux_sched.h>
#include <compat/linux/linux_signal.h>
#include <compat/linux/linux_syscallargs.h>
#include <compat/linux/linux_util.h>
#include <compat/linux/linux_dirent.h>
#include <compat/linux/linux_emuldata.h>
#include <compat/linux/linux_time.h>
#define LINUX_CLOCK_REALTIME 0
#define LINUX_CLOCK_MONOTONIC 1
#define LINUX_CLOCK_PROCESS_CPUTIME_ID 2
#define LINUX_CLOCK_THREAD_CPUTIME_ID 3
int
bsd_to_linux_timespec(struct linux_timespec *ltp, const struct timespec *ntp)
{
if (ntp->tv_sec > LINUX_TIME_MAX)
return EOVERFLOW;
ltp->tv_sec = ntp->tv_sec;
ltp->tv_nsec = ntp->tv_nsec;
return 0;
}
void
linux_to_bsd_timespec(struct timespec *ntp, const struct linux_timespec *ltp)
{
ntp->tv_sec = ltp->tv_sec;
ntp->tv_nsec = ltp->tv_nsec;
}
int
bsd_to_linux_itimerval(struct linux_itimerval *ltp,
const struct itimerval *ntp)
{
int error;
error = bsd_to_linux_timeval(<p->it_interval, &ntp->it_interval);
if (error)
return (error);
return (bsd_to_linux_timeval(<p->it_value, &ntp->it_value));
}
void
linux_to_bsd_itimerval(struct itimerval *ntp,
const struct linux_itimerval *ltp)
{
linux_to_bsd_timeval(&ntp->it_interval, <p->it_interval);
linux_to_bsd_timeval(&ntp->it_value, <p->it_value);
}
int
linux_to_bsd_clockid(clockid_t *n, clockid_t l)
{
switch (l) {
case LINUX_CLOCK_REALTIME:
*n = CLOCK_REALTIME;
break;
case LINUX_CLOCK_MONOTONIC:
*n = CLOCK_MONOTONIC;
break;
case LINUX_CLOCK_PROCESS_CPUTIME_ID:
*n = CLOCK_PROCESS_CPUTIME_ID;
break;
case LINUX_CLOCK_THREAD_CPUTIME_ID:
*n = CLOCK_THREAD_CPUTIME_ID;
break;
default:
return (EINVAL);
break;
}
return (0);
}
int
linux_sys_clock_getres(struct proc *p, void *v, register_t *retval)
{
struct linux_sys_clock_getres_args *uap = v;
struct linux_timespec ltp;
clockid_t clockid;
int error;
if (SCARG(uap, tp) == NULL)
return 0;
error = linux_to_bsd_clockid(&clockid, SCARG(uap, which));
if (error != 0)
return error;
/* ahhh, just give a good guess */
ltp.tv_sec = 0;
ltp.tv_nsec = 1000000000 / hz;
return (copyout(<p, SCARG(uap, tp), sizeof ltp));
}
int
linux_sys_clock_gettime(struct proc *p, void *v, register_t *retval)
{
struct linux_sys_clock_gettime_args *uap = v;
struct timespec tp;
struct linux_timespec ltp;
clockid_t clockid;
int error;
error = linux_to_bsd_clockid(&clockid, SCARG(uap, which));
if (error != 0)
return error;
error = clock_gettime(p, clockid, &tp);
if (error != 0)
return error;
error = bsd_to_linux_timespec(<p, &tp);
if (error != 0)
return error;
return (copyout(<p, SCARG(uap, tp), sizeof ltp));
}
int
linux_sys_nanosleep(struct proc *p, void *v, register_t *retval)
{
static int nanowait;
struct linux_sys_nanosleep_args /* {
syscallarg(const struct linux_timespec *) rqtp;
syscallarg(struct linux_timespec *) rmtp;
} */ *uap = v;
struct linux_timespec lts;
struct timespec rqt, rmt;
struct timespec sts, ets;
struct linux_timespec *rmtp;
struct timeval tv;
int error, error1;
rmtp = SCARG(uap, rmtp);
error = copyin(SCARG(uap, rqtp), <s, sizeof(lts));
if (error)
return (error);
linux_to_bsd_timespec(&rqt, <s);
TIMESPEC_TO_TIMEVAL(&tv, &rqt);
if (itimerfix(&tv))
return (EINVAL);
if (rmtp)
getnanouptime(&sts);
error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep",
MAX(1, tvtohz(&tv)));
if (error == ERESTART)
error = EINTR;
if (error == EWOULDBLOCK)
error = 0;
if (rmtp) {
getnanouptime(&ets);
timespecsub(&ets, &sts, &sts);
timespecsub(&rqt, &sts, &rmt);
if (rmt.tv_sec < 0)
timespecclear(&rmt);
if ((error1 = bsd_to_linux_timespec(<s, &rmt)) ||
(error1 = copyout(<s, rmtp, sizeof(lts))))
error = error1;
}
return error;
}
int
linux_sys_gettimeofday(struct proc *p, void *v, register_t *retval)
{
struct linux_sys_gettimeofday_args /* {
syscallarg(struct linux_timeval *) tp;
syscallarg(struct timezone *) tzp;
} */ *uap = v;
struct timeval atv;
struct linux_timeval latv;
struct linux_timeval *tp;
struct timezone *tzp;
int error = 0;
tp = SCARG(uap, tp);
tzp = SCARG(uap, tzp);
if (tp) {
microtime(&atv);
if ((error = bsd_to_linux_timeval(&latv, &atv)) ||
(error = copyout(&latv, tp, sizeof (latv))))
return (error);
}
if (tzp)
error = copyout(&tz, tzp, sizeof (tz));
return (error);
}
int
linux_sys_getitimer(struct proc *p, void *v, register_t *retval)
{
struct linux_sys_getitimer_args /* {
syscallarg(int) which;
syscallarg(struct linux_itimerval *) itv;
} */ *uap = v;
struct itimerval aitv;
struct linux_itimerval laitv;
int s, which, error;
which = SCARG(uap, which);
if (which < ITIMER_REAL || which > ITIMER_PROF)
return (EINVAL);
s = splclock();
aitv = p->p_p->ps_timer[which];
if (which == ITIMER_REAL) {
struct timeval now;
getmicrouptime(&now);
/*
* Convert from absolute to relative time in .it_value
* part of real time timer. If time for real time timer
* has passed return 0, else return difference between
* current time and time for the timer to go off.
*/
if (timerisset(&aitv.it_value)) {
if (timercmp(&aitv.it_value, &now, <))
timerclear(&aitv.it_value);
else
timersub(&aitv.it_value, &now,
&aitv.it_value);
}
}
splx(s);
if ((error = bsd_to_linux_itimerval(&laitv, &aitv)))
return error;
return (copyout(&laitv, SCARG(uap, itv), sizeof(laitv)));
}
int
linux_sys_setitimer(struct proc *p, void *v, register_t *retval)
{
struct linux_sys_setitimer_args /* {
syscallarg(int) which;
syscallarg(const struct linux_itimerval *) itv;
syscallarg(struct linux_itimerval *) oitv;
} */ *uap = v;
struct linux_sys_getitimer_args getargs;
struct itimerval aitv;
struct linux_itimerval laitv;
const struct linux_itimerval *itvp;
struct linux_itimerval *oitv;
struct process *pr = p->p_p;
int error;
int timo;
int which;
which = SCARG(uap, which);
itvp = SCARG(uap, itv);
oitv = SCARG(uap, oitv);
if (which < ITIMER_REAL || which > ITIMER_PROF)
return (EINVAL);
if (itvp && (error = copyin(itvp, &laitv, sizeof(laitv))))
return (error);
if (oitv != NULL) {
SCARG(&getargs, which) = which;
SCARG(&getargs, itv) = oitv;
if ((error = linux_sys_getitimer(p, &getargs, retval)))
return (error);
}
if (itvp == 0)
return (0);
linux_to_bsd_itimerval(&aitv, &laitv);
if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
return (EINVAL);
if (which == ITIMER_REAL) {
struct timeval ctv;
timeout_del(&pr->ps_realit_to);
getmicrouptime(&ctv);
if (timerisset(&aitv.it_value)) {
timo = tvtohz(&aitv.it_value);
timeout_add(&pr->ps_realit_to, timo);
timeradd(&aitv.it_value, &ctv, &aitv.it_value);
}
pr->ps_timer[ITIMER_REAL] = aitv;
} else {
int s;
itimerround(&aitv.it_interval);
s = splclock();
pr->ps_timer[which] = aitv;
splx(s);
}
return (0);
}
|