forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 11
/
time.c
328 lines (291 loc) · 9.17 KB
/
time.c
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
#ifdef __linux__
#define _GNU_SOURCE
#include <sys/resource.h>
#endif
#include "debug.h"
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include "kernel/calls.h"
#include "kernel/errno.h"
#include "kernel/resource.h"
#include "kernel/time.h"
#include "fs/poll.h"
dword_t sys_time(addr_t time_out) {
dword_t now = time(NULL);
if (time_out != 0)
if (user_put(time_out, now))
return _EFAULT;
return now;
}
dword_t sys_stime(addr_t UNUSED(time)) {
return _EPERM;
}
dword_t sys_clock_gettime(dword_t clock, addr_t tp) {
STRACE("clock_gettime(%d, 0x%x)", clock, tp);
struct timespec ts;
if (clock == CLOCK_PROCESS_CPUTIME_ID_) {
// FIXME this is thread usage, not process usage
struct rusage_ rusage = rusage_get_current();
ts.tv_sec = rusage.utime.sec;
ts.tv_nsec = rusage.utime.usec * 1000;
} else {
clockid_t clock_id;
switch (clock) {
case CLOCK_REALTIME_: clock_id = CLOCK_REALTIME; break;
case CLOCK_MONOTONIC_: clock_id = CLOCK_MONOTONIC; break;
default: return _EINVAL;
}
int err = clock_gettime(clock_id, &ts);
if (err < 0)
return errno_map();
}
struct timespec_ t;
t.sec = ts.tv_sec;
t.nsec = ts.tv_nsec;
if (user_put(tp, t))
return _EFAULT;
STRACE(" {%lds %ldns}", t.sec, t.nsec);
return 0;
}
dword_t sys_clock_getres(dword_t clock, addr_t res_addr) {
STRACE("clock_getres(%d, %#x)", clock, res_addr);
clockid_t clock_id;
switch (clock) {
case CLOCK_REALTIME_: clock_id = CLOCK_REALTIME; break;
case CLOCK_MONOTONIC_: clock_id = CLOCK_MONOTONIC; break;
default: return _EINVAL;
}
struct timespec res;
int err = clock_getres(clock_id, &res);
if (err < 0)
return errno_map();
struct timespec_ t;
t.sec = res.tv_sec;
t.nsec = res.tv_nsec;
if (user_put(res_addr, t))
return _EFAULT;
return 0;
}
dword_t sys_clock_settime(dword_t UNUSED(clock), addr_t UNUSED(tp)) {
return _EPERM;
}
static void itimer_notify(struct task *task) {
struct siginfo_ info = {
.code = SI_TIMER_,
};
send_signal(task, SIGALRM_, info);
}
static int itimer_set(struct tgroup *group, int which, struct timer_spec spec, struct timer_spec *old_spec) {
if (which != ITIMER_REAL_) {
FIXME("unimplemented setitimer %d", which);
return _EINVAL;
}
if (!group->timer) {
struct timer *timer = timer_new(CLOCK_REALTIME, (timer_callback_t) itimer_notify, current);
if (IS_ERR(timer))
return PTR_ERR(timer);
group->timer = timer;
}
return timer_set(group->timer, spec, old_spec);
}
int_t sys_setitimer(int_t which, addr_t new_val_addr, addr_t old_val_addr) {
struct itimerval_ val;
if (user_get(new_val_addr, val))
return _EFAULT;
STRACE("setitimer(%d, {%ds %dus, %ds %dus}, 0x%x)", which, val.value.sec, val.value.usec, val.interval.sec, val.interval.usec, old_val_addr);
struct timer_spec spec = {
.interval.tv_sec = val.interval.sec,
.interval.tv_nsec = val.interval.usec * 1000,
.value.tv_sec = val.value.sec,
.value.tv_nsec = val.value.usec * 1000,
};
struct timer_spec old_spec;
struct tgroup *group = current->group;
lock(&group->lock);
int err = itimer_set(group, which, spec, &old_spec);
unlock(&group->lock);
if (err < 0)
return err;
if (old_val_addr != 0) {
struct itimerval_ old_val;
old_val.interval.sec = old_spec.interval.tv_sec;
old_val.interval.usec = old_spec.interval.tv_nsec / 1000;
old_val.value.sec = old_spec.value.tv_sec;
old_val.value.usec = old_spec.value.tv_nsec / 1000;
if (user_put(old_val_addr, old_val))
return _EFAULT;
}
return 0;
}
uint_t sys_alarm(uint_t seconds) {
STRACE("alarm(%d)", seconds);
struct timer_spec spec = {
.value.tv_sec = seconds,
};
struct timer_spec old_spec;
struct tgroup *group = current->group;
lock(&group->lock);
int err = itimer_set(group, ITIMER_REAL_, spec, &old_spec);
unlock(&group->lock);
if (err < 0)
return err;
// Round up, and make sure to not return 0 if old_spec is > 0
seconds = old_spec.value.tv_sec;
if (old_spec.value.tv_nsec >= 500000000)
seconds++;
if (seconds == 0 && !timespec_is_zero(old_spec.value))
seconds = 1;
return seconds;
}
dword_t sys_nanosleep(addr_t req_addr, addr_t rem_addr) {
struct timespec_ req_ts;
if (user_get(req_addr, req_ts))
return _EFAULT;
STRACE("nanosleep({%d, %d}, 0x%x", req_ts.sec, req_ts.nsec, rem_addr);
struct timespec req;
req.tv_sec = req_ts.sec;
req.tv_nsec = req_ts.nsec;
struct timespec rem;
if (nanosleep(&req, &rem) < 0)
return errno_map();
if (rem_addr != 0) {
struct timespec_ rem_ts;
rem_ts.sec = rem.tv_sec;
rem_ts.nsec = rem.tv_nsec;
if (user_put(rem_addr, rem_ts))
return _EFAULT;
}
return 0;
}
dword_t sys_times(addr_t tbuf) {
STRACE("times(0x%x)", tbuf);
if (tbuf) {
struct tms_ tmp;
struct rusage_ rusage = rusage_get_current();
tmp.tms_utime = clock_from_timeval(rusage.utime);
tmp.tms_stime = clock_from_timeval(rusage.stime);
tmp.tms_cutime = tmp.tms_utime;
tmp.tms_cstime = tmp.tms_stime;
if (user_put(tbuf, tmp))
return _EFAULT;
}
return 0;
}
dword_t sys_gettimeofday(addr_t tv, addr_t tz) {
STRACE("gettimeofday(0x%x, 0x%x)", tv, tz);
struct timeval timeval;
struct timezone timezone;
if (gettimeofday(&timeval, &timezone) < 0) {
return errno_map();
}
struct timeval_ tv_;
struct timezone_ tz_;
tv_.sec = timeval.tv_sec;
tv_.usec = timeval.tv_usec;
tz_.minuteswest = timezone.tz_minuteswest;
tz_.dsttime = timezone.tz_dsttime;
if ((tv && user_put(tv, tv_)) || (tz && user_put(tz, tz_))) {
return _EFAULT;
}
return 0;
}
dword_t sys_settimeofday(addr_t UNUSED(tv), addr_t UNUSED(tz)) {
return _EPERM;
}
static struct fd_ops timerfd_ops;
static void timerfd_callback(struct fd *fd) {
lock(&fd->lock);
fd->timerfd.expirations++;
notify(&fd->cond);
unlock(&fd->lock);
poll_wakeup(fd);
}
fd_t sys_timerfd_create(int_t clockid, int_t flags) {
STRACE("timerfd_create(%d, %#x)", clockid, flags);
clockid_t real_clockid;
switch (clockid) {
case CLOCK_REALTIME_: real_clockid = CLOCK_REALTIME; break;
case CLOCK_MONOTONIC_: real_clockid = CLOCK_MONOTONIC; break;
default: FIXME("timerfd %d", clockid); return _EINVAL;
}
struct fd *fd = adhoc_fd_create(&timerfd_ops);
if (fd == NULL)
return _ENOMEM;
fd->timerfd.timer = timer_new(real_clockid, (timer_callback_t) timerfd_callback, fd);
return f_install(fd, flags);
}
#define TFD_TIMER_ABSTIME_ (1 << 0)
int_t sys_timerfd_settime(fd_t f, int_t flags, addr_t new_value_addr, addr_t old_value_addr) {
STRACE("timerfd_settime(%d, %d, %#x, %#x)", f, flags, new_value_addr, old_value_addr);
if (flags & ~(TFD_TIMER_ABSTIME_))
return _EINVAL;
struct fd *fd = f_get(f);
if (fd == NULL)
return _EBADF;
if (fd->ops != &timerfd_ops)
return _EINVAL;
struct itimerspec_ value;
if (user_get(new_value_addr, value))
return _EFAULT;
struct timer_spec spec = {
.value.tv_sec = value.value.sec,
.value.tv_nsec = value.value.nsec,
.interval.tv_sec = value.interval.sec,
.interval.tv_nsec = value.interval.nsec,
};
struct timer_spec old_spec;
if (flags & TFD_TIMER_ABSTIME_) {
struct timespec now = timespec_now(fd->timerfd.timer->clockid);
spec.value = timespec_subtract(spec.value, now);
}
lock(&fd->lock);
int err = timer_set(fd->timerfd.timer, spec, &old_spec);
unlock(&fd->lock);
if (err < 0)
return err;
if (old_value_addr) {
struct itimerspec_ old_value = {
.value.sec = old_spec.value.tv_sec,
.value.nsec = old_spec.value.tv_nsec,
.interval.sec = old_spec.interval.tv_sec,
.interval.nsec = old_spec.interval.tv_nsec,
};
if (user_put(old_value_addr, old_value))
return _EFAULT;
}
return 0;
}
static ssize_t timerfd_read(struct fd *fd, void *buf, size_t bufsize) {
if (bufsize < sizeof(uint64_t))
return _EINVAL;
lock(&fd->lock);
while (fd->timerfd.expirations == 0) {
if (fd->flags & O_NONBLOCK_) {
unlock(&fd->lock);
return _EAGAIN;
}
wait_for(&fd->cond, &fd->lock, NULL);
}
*(uint64_t *) buf = fd->timerfd.expirations;
fd->timerfd.expirations = 0;
unlock(&fd->lock);
return sizeof(uint64_t);
}
static int timerfd_poll(struct fd *fd) {
int res = 0;
lock(&fd->lock);
if (fd->timerfd.expirations != 0)
res |= POLL_READ;
unlock(&fd->lock);
return res;
}
static int timerfd_close(struct fd *fd) {
timer_free(fd->timerfd.timer);
return 0;
}
static struct fd_ops timerfd_ops = {
.read = timerfd_read,
.poll = timerfd_poll,
.close = timerfd_close,
};