2020-07-30 21:38:15 +00:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
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
# include <AK/Time.h>
2023-02-24 17:45:37 +00:00
# include <Kernel/Tasks/Process.h>
2020-07-30 21:38:15 +00:00
# include <Kernel/Time/TimeManagement.h>
namespace Kernel {
2021-11-07 23:51:39 +00:00
ErrorOr < FlatPtr > Process : : sys $ map_time_page ( )
2021-08-10 11:44:01 +00:00
{
2023-04-04 11:02:39 +00:00
VERIFY_NO_PROCESS_BIG_LOCK ( this ) ;
2021-12-29 09:11:45 +00:00
TRY ( require_promise ( Pledge : : stdio ) ) ;
2021-08-10 11:44:01 +00:00
auto & vmobject = TimeManagement : : the ( ) . time_page_vmobject ( ) ;
2022-08-23 15:58:05 +00:00
return address_space ( ) . with ( [ & ] ( auto & space ) - > ErrorOr < FlatPtr > {
auto * region = TRY ( space - > allocate_region_with_vmobject ( Memory : : RandomizeVirtualAddress : : Yes , { } , PAGE_SIZE , PAGE_SIZE , vmobject , 0 , " Kernel time page " sv , PROT_READ , true ) ) ;
return region - > vaddr ( ) . get ( ) ;
} ) ;
2021-08-10 11:44:01 +00:00
}
2021-11-07 23:51:39 +00:00
ErrorOr < FlatPtr > Process : : sys $ clock_gettime ( clockid_t clock_id , Userspace < timespec * > user_ts )
2020-07-30 21:38:15 +00:00
{
2021-08-06 11:49:52 +00:00
VERIFY_NO_PROCESS_BIG_LOCK ( this ) ;
2021-12-29 09:11:45 +00:00
TRY ( require_promise ( Pledge : : stdio ) ) ;
2020-07-30 21:38:15 +00:00
2022-02-21 21:53:43 +00:00
TRY ( TimeManagement : : validate_clock_id ( clock_id ) ) ;
2020-07-30 21:38:15 +00:00
2021-05-05 16:51:06 +00:00
auto ts = TimeManagement : : the ( ) . current_time ( clock_id ) . to_timespec ( ) ;
2021-11-07 23:51:39 +00:00
TRY ( copy_to_user ( user_ts , & ts ) ) ;
return 0 ;
2020-07-30 21:38:15 +00:00
}
2022-04-01 17:58:27 +00:00
ErrorOr < FlatPtr > Process : : sys $ clock_settime ( clockid_t clock_id , Userspace < timespec const * > user_ts )
2020-07-30 21:38:15 +00:00
{
2022-04-03 22:12:57 +00:00
VERIFY_NO_PROCESS_BIG_LOCK ( this ) ;
2021-12-29 09:11:45 +00:00
TRY ( require_promise ( Pledge : : settime ) ) ;
2020-07-30 21:38:15 +00:00
2022-08-20 22:21:01 +00:00
auto credentials = this - > credentials ( ) ;
if ( ! credentials - > is_superuser ( ) )
2021-03-01 12:49:16 +00:00
return EPERM ;
2020-07-30 21:38:15 +00:00
2023-03-13 21:11:13 +00:00
auto time = UnixDateTime : : epoch ( ) + TRY ( copy_time_from_user ( user_ts ) ) ;
2020-07-30 21:38:15 +00:00
switch ( clock_id ) {
case CLOCK_REALTIME :
2021-09-06 20:22:14 +00:00
TimeManagement : : the ( ) . set_epoch_time ( time ) ;
2020-07-30 21:38:15 +00:00
break ;
default :
2021-03-01 12:49:16 +00:00
return EINVAL ;
2020-07-30 21:38:15 +00:00
}
return 0 ;
}
2022-04-01 17:58:27 +00:00
ErrorOr < FlatPtr > Process : : sys $ clock_nanosleep ( Userspace < Syscall : : SC_clock_nanosleep_params const * > user_params )
2020-07-30 21:38:15 +00:00
{
2021-08-06 11:54:34 +00:00
VERIFY_NO_PROCESS_BIG_LOCK ( this ) ;
2021-12-29 09:11:45 +00:00
TRY ( require_promise ( Pledge : : stdio ) ) ;
2021-09-05 15:51:37 +00:00
auto params = TRY ( copy_typed_from_user ( user_params ) ) ;
2020-07-30 21:38:15 +00:00
2021-09-06 20:22:14 +00:00
auto requested_sleep = TRY ( copy_time_from_user ( params . requested_sleep ) ) ;
2020-07-30 21:38:15 +00:00
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 :
2021-03-01 12:49:16 +00:00
return EINVAL ;
2021-02-12 17:49:16 +00:00
}
2020-07-30 21:38:15 +00:00
2022-02-21 21:53:43 +00:00
TRY ( TimeManagement : : validate_clock_id ( params . clock_id ) ) ;
2020-12-04 05:12:50 +00:00
bool was_interrupted ;
if ( is_absolute ) {
2021-09-06 20:22:14 +00:00
was_interrupted = Thread : : current ( ) - > sleep_until ( params . clock_id , requested_sleep ) . was_interrupted ( ) ;
2020-12-04 05:12:50 +00:00
} else {
2023-03-13 15:30:34 +00:00
Duration remaining_sleep ;
2021-09-06 20:22:14 +00:00
was_interrupted = Thread : : current ( ) - > sleep ( params . clock_id , requested_sleep , & remaining_sleep ) . was_interrupted ( ) ;
2021-02-27 22:56:16 +00:00
timespec remaining_sleep_ts = remaining_sleep . to_timespec ( ) ;
2021-09-05 15:38:37 +00:00
if ( was_interrupted & & params . remaining_sleep ) {
TRY ( copy_to_user ( params . remaining_sleep , & remaining_sleep_ts ) ) ;
}
2020-07-30 21:38:15 +00:00
}
2020-12-04 05:12:50 +00:00
if ( was_interrupted )
2021-03-01 12:49:16 +00:00
return EINTR ;
2020-12-04 05:12:50 +00:00
return 0 ;
2020-07-30 21:38:15 +00:00
}
2022-07-25 11:09:30 +00:00
ErrorOr < FlatPtr > Process : : sys $ clock_getres ( Userspace < Syscall : : SC_clock_getres_params const * > user_params )
{
VERIFY_NO_PROCESS_BIG_LOCK ( this ) ;
auto params = TRY ( copy_typed_from_user ( user_params ) ) ;
2023-02-20 21:06:29 +00:00
TRY ( TimeManagement : : validate_clock_id ( params . clock_id ) ) ;
auto ts = TimeManagement : : the ( ) . clock_resolution ( ) . to_timespec ( ) ;
TRY ( copy_to_user ( params . result , & ts ) ) ;
2022-07-25 11:09:30 +00:00
return 0 ;
}
2022-04-01 17:58:27 +00:00
ErrorOr < FlatPtr > Process : : sys $ adjtime ( Userspace < timeval const * > user_delta , Userspace < timeval * > user_old_delta )
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
{
2022-04-03 22:39:43 +00:00
VERIFY_NO_PROCESS_BIG_LOCK ( this ) ;
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
if ( user_old_delta ) {
2023-03-13 21:11:13 +00:00
auto old_delta_duration = TimeManagement : : the ( ) . remaining_epoch_time_adjustment ( ) ;
auto old_delta = old_delta_duration . to_timeval ( ) ;
2021-09-05 15:38:37 +00:00
TRY ( copy_to_user ( user_old_delta , & old_delta ) ) ;
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
}
if ( user_delta ) {
2021-12-29 09:11:45 +00:00
TRY ( require_promise ( Pledge : : settime ) ) ;
2022-08-20 22:21:01 +00:00
auto credentials = this - > credentials ( ) ;
if ( ! credentials - > is_superuser ( ) )
2021-03-01 12:49:16 +00:00
return EPERM ;
2021-09-06 20:22:14 +00:00
auto delta = TRY ( copy_time_from_user ( user_delta ) ) ;
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
2023-03-13 21:11:13 +00:00
TimeManagement : : the ( ) . set_remaining_epoch_time_adjustment ( delta ) ;
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
}
return 0 ;
}
2020-07-30 21:38:15 +00:00
}