2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
Kernel+LibC: Add adjtime(2)
Most systems (Linux, OpenBSD) adjust 0.5 ms per second, or 0.5 us per
1 ms tick. That is, the clock is sped up or slowed down by at most
0.05%. This means adjusting the clock by 1 s takes 2000 s, and the
clock an be adjusted by at most 1.8 s per hour.
FreeBSD adjusts 5 ms per second if the remaining time adjustment is
>= 1 s (0.5%) , else it adjusts by 0.5 ms as well. This allows adjusting
by (almost) 18 s per hour.
Since Serenity OS can lose more than 22 s per hour (#3429), this
picks an adjustment rate up to 1% for now. This allows us to
adjust up to 36s per hour, which should be sufficient to adjust
the clock fast enough to keep up with how much time the clock
currently loses. Once we have a fancier NTP implementation that can
adjust tick rate in addition to offset, we can think about reducing
this.
adjtime is a bit old-school and most current POSIX-y OSs instead
implement adjtimex/ntp_adjtime, but a) we have to start somewhere
b) ntp_adjtime() is a fairly gnarly API. OpenBSD's adjfreq looks
like it might provide similar functionality with a nicer API. But
before worrying about all this, it's probably a good idea to get
to a place where the kernel APIs are (barely) good enough so that
we can write an ntp service, and once we have that we should write
a way to automatically evaluate how well it keeps the time adjusted,
and only then should we add improvements ot the adjustment mechanism.
2020-11-05 21:00:51 +00:00
|
|
|
#include <AK/Time.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Time/TimeManagement.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2020-08-09 19:42:51 +00:00
|
|
|
int Process::sys$clock_gettime(clockid_t clock_id, Userspace<timespec*> user_ts)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
auto ts = TimeManagement::the().current_time(clock_id);
|
|
|
|
if (ts.is_error())
|
|
|
|
return ts.error();
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
if (!copy_to_user(user_ts, &ts.value()))
|
2020-09-12 03:11:07 +00:00
|
|
|
return -EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-09 19:48:49 +00:00
|
|
|
int Process::sys$clock_settime(clockid_t clock_id, Userspace<const timespec*> user_ts)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(settime);
|
|
|
|
|
|
|
|
if (!is_superuser())
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
timespec ts;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(&ts, user_ts))
|
2020-07-30 21:38:15 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
switch (clock_id) {
|
|
|
|
case CLOCK_REALTIME:
|
2020-09-06 23:52:24 +00:00
|
|
|
TimeManagement::the().set_epoch_time(ts);
|
2020-07-30 21:38:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-05 05:24:45 +00:00
|
|
|
int Process::sys$clock_nanosleep(Userspace<const Syscall::SC_clock_nanosleep_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
|
|
|
|
Syscall::SC_clock_nanosleep_params params;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2020-07-30 21:38:15 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
timespec requested_sleep;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(&requested_sleep, params.requested_sleep))
|
2020-07-30 21:38:15 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2021-02-12 17:49:16 +00:00
|
|
|
bool is_absolute;
|
|
|
|
switch (params.flags) {
|
|
|
|
case 0:
|
|
|
|
is_absolute = false;
|
|
|
|
break;
|
|
|
|
case TIMER_ABSTIME:
|
|
|
|
is_absolute = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-12-04 05:12:50 +00:00
|
|
|
if (!TimeManagement::is_valid_clock_id(params.clock_id))
|
2020-07-30 21:38:15 +00:00
|
|
|
return -EINVAL;
|
2020-12-04 05:12:50 +00:00
|
|
|
|
|
|
|
bool was_interrupted;
|
|
|
|
if (is_absolute) {
|
|
|
|
was_interrupted = Thread::current()->sleep_until(params.clock_id, requested_sleep).was_interrupted();
|
|
|
|
} else {
|
|
|
|
timespec remaining_sleep;
|
|
|
|
was_interrupted = Thread::current()->sleep(params.clock_id, requested_sleep, &remaining_sleep).was_interrupted();
|
|
|
|
if (was_interrupted && params.remaining_sleep && !copy_to_user(params.remaining_sleep, &remaining_sleep))
|
|
|
|
return -EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-12-04 05:12:50 +00:00
|
|
|
if (was_interrupted)
|
|
|
|
return -EINTR;
|
|
|
|
return 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
Kernel+LibC: Add adjtime(2)
Most systems (Linux, OpenBSD) adjust 0.5 ms per second, or 0.5 us per
1 ms tick. That is, the clock is sped up or slowed down by at most
0.05%. This means adjusting the clock by 1 s takes 2000 s, and the
clock an be adjusted by at most 1.8 s per hour.
FreeBSD adjusts 5 ms per second if the remaining time adjustment is
>= 1 s (0.5%) , else it adjusts by 0.5 ms as well. This allows adjusting
by (almost) 18 s per hour.
Since Serenity OS can lose more than 22 s per hour (#3429), this
picks an adjustment rate up to 1% for now. This allows us to
adjust up to 36s per hour, which should be sufficient to adjust
the clock fast enough to keep up with how much time the clock
currently loses. Once we have a fancier NTP implementation that can
adjust tick rate in addition to offset, we can think about reducing
this.
adjtime is a bit old-school and most current POSIX-y OSs instead
implement adjtimex/ntp_adjtime, but a) we have to start somewhere
b) ntp_adjtime() is a fairly gnarly API. OpenBSD's adjfreq looks
like it might provide similar functionality with a nicer API. But
before worrying about all this, it's probably a good idea to get
to a place where the kernel APIs are (barely) good enough so that
we can write an ntp service, and once we have that we should write
a way to automatically evaluate how well it keeps the time adjusted,
and only then should we add improvements ot the adjustment mechanism.
2020-11-05 21:00:51 +00:00
|
|
|
int Process::sys$adjtime(Userspace<const timeval*> user_delta, Userspace<timeval*> user_old_delta)
|
|
|
|
{
|
|
|
|
if (user_old_delta) {
|
|
|
|
timespec old_delta_ts = TimeManagement::the().remaining_epoch_time_adjustment();
|
|
|
|
timeval old_delta;
|
|
|
|
timespec_to_timeval(old_delta_ts, old_delta);
|
|
|
|
if (!copy_to_user(user_old_delta, &old_delta))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_delta) {
|
|
|
|
REQUIRE_PROMISE(settime);
|
|
|
|
if (!is_superuser())
|
|
|
|
return -EPERM;
|
|
|
|
timeval delta;
|
|
|
|
if (!copy_from_user(&delta, user_delta))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (delta.tv_usec < 0 || delta.tv_usec >= 1'000'000)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
timespec delta_ts;
|
|
|
|
timeval_to_timespec(delta, delta_ts);
|
|
|
|
TimeManagement::the().set_remaining_epoch_time_adjustment(delta_ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-05 05:18:51 +00:00
|
|
|
int Process::sys$gettimeofday(Userspace<timeval*> user_tv)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
auto tv = kgettimeofday();
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(user_tv, &tv))
|
|
|
|
return -EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|