Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: subr_time.c,v 1.43 2026/01/04 03:21:11 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     32  *	@(#)kern_time.c 8.4 (Berkeley) 5/26/95
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.43 2026/01/04 03:21:11 riastradh Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 
     41 #include <sys/intr.h>
     42 #include <sys/kauth.h>
     43 #include <sys/kernel.h>
     44 #include <sys/lwp.h>
     45 #include <sys/proc.h>
     46 #include <sys/sdt.h>
     47 #include <sys/time.h>
     48 #include <sys/timetc.h>
     49 #include <sys/timex.h>
     50 
     51 /*
     52  * Compute number of hz until specified time.  Used to compute second
     53  * argument to callout_reset() from an absolute time.
     54  */
     55 int
     56 tvhzto(const struct timeval *tvp)
     57 {
     58 	struct timeval now, tv;
     59 
     60 	tv = *tvp;	/* Don't modify original tvp. */
     61 	getmicrotime(&now);
     62 	timersub(&tv, &now, &tv);
     63 	return tvtohz(&tv);
     64 }
     65 
     66 int
     67 tshzto(const struct timespec *tsp)
     68 {
     69 	struct timespec now, ts;
     70 
     71 	ts = *tsp;	/* Don't modify original tsp. */
     72 	getnanotime(&now);
     73 	timespecsub(&ts, &now, &ts);
     74 	return tstohz(&ts);
     75 }
     76 
     77 int
     78 tshztoup(const struct timespec *tsp)
     79 {
     80 	struct timespec now, ts;
     81 
     82 	ts = *tsp;	/* Don't modify original tsp. */
     83 	getnanouptime(&now);
     84 	timespecsub(&ts, &now, &ts);
     85 	return tstohz(&ts);
     86 }
     87 
     88 int
     89 inittimeleft(struct timespec *ts, struct timespec *sleepts)
     90 {
     91 
     92 	if (itimespecfix(ts)) {
     93 		return -1;
     94 	}
     95 	KASSERT(ts->tv_sec >= 0);
     96 	getnanouptime(sleepts);
     97 	return 0;
     98 }
     99 
    100 int
    101 gettimeleft(struct timespec *ts, struct timespec *sleepts)
    102 {
    103 	struct timespec now, sleptts;
    104 
    105 	KASSERT(ts->tv_sec >= 0);
    106 
    107 	/*
    108 	 * Reduce ts by elapsed time based on monotonic time scale.
    109 	 */
    110 	getnanouptime(&now);
    111 	KASSERT(timespeccmp(sleepts, &now, <=));
    112 	timespecsub(&now, sleepts, &sleptts);
    113 	*sleepts = now;
    114 
    115 	if (timespeccmp(ts, &sleptts, <=)) { /* timed out */
    116 		timespecclear(ts);
    117 		return 0;
    118 	}
    119 	timespecsub(ts, &sleptts, ts);
    120 
    121 	return tstohz(ts);
    122 }
    123 
    124 void
    125 clock_timeleft(clockid_t clockid, struct timespec *ts, struct timespec *sleepts)
    126 {
    127 	struct timespec sleptts;
    128 
    129 	clock_gettime1(clockid, &sleptts);
    130 	timespecadd(ts, sleepts, ts);
    131 	timespecsub(ts, &sleptts, ts);
    132 	*sleepts = sleptts;
    133 }
    134 
    135 int
    136 clock_gettime1(clockid_t clock_id, struct timespec *ts)
    137 {
    138 	int error;
    139 	struct proc *p;
    140 
    141 #define CPUCLOCK_ID_MASK (~(CLOCK_THREAD_CPUTIME_ID|CLOCK_PROCESS_CPUTIME_ID))
    142 	if (clock_id & CLOCK_PROCESS_CPUTIME_ID) {
    143 		pid_t pid = clock_id & CPUCLOCK_ID_MASK;
    144 		struct timeval cputime;
    145 
    146 		mutex_enter(&proc_lock);
    147 		p = pid == 0 ? curproc : proc_find(pid);
    148 		if (p == NULL) {
    149 			mutex_exit(&proc_lock);
    150 			return SET_ERROR(ESRCH);
    151 		}
    152 		mutex_enter(p->p_lock);
    153 		calcru(p, /*usertime*/NULL, /*systime*/NULL, /*intrtime*/NULL,
    154 		    &cputime);
    155 		mutex_exit(p->p_lock);
    156 		mutex_exit(&proc_lock);
    157 
    158 		// XXX: Perhaps create a special kauth type
    159 		error = kauth_authorize_process(kauth_cred_get(),
    160 		    KAUTH_PROCESS_PTRACE, p,
    161 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    162 		if (error)
    163 			return error;
    164 
    165 		TIMEVAL_TO_TIMESPEC(&cputime, ts);
    166 		return 0;
    167 	} else if (clock_id & CLOCK_THREAD_CPUTIME_ID) {
    168 		struct lwp *l;
    169 		lwpid_t lid = clock_id & CPUCLOCK_ID_MASK;
    170 		struct bintime tm = {0, 0};
    171 
    172 		p = curproc;
    173 		mutex_enter(p->p_lock);
    174 		l = lid == 0 ? curlwp : lwp_find(p, lid);
    175 		if (l == NULL) {
    176 			mutex_exit(p->p_lock);
    177 			return SET_ERROR(ESRCH);
    178 		}
    179 		addrulwp(l, &tm);
    180 		mutex_exit(p->p_lock);
    181 
    182 		bintime2timespec(&tm, ts);
    183 		return 0;
    184 	}
    185 
    186 	switch (clock_id) {
    187 	case CLOCK_REALTIME:
    188 		nanotime(ts);
    189 		break;
    190 	case CLOCK_MONOTONIC:
    191 		nanouptime(ts);
    192 		break;
    193 	default:
    194 		return SET_ERROR(EINVAL);
    195 	}
    196 
    197 	return 0;
    198 }
    199 
    200 /*
    201  * Calculate delta and convert from struct timespec to the ticks.
    202  */
    203 int
    204 ts2timo(clockid_t clock_id, int flags, struct timespec *ts,
    205     int *timo, struct timespec *start)
    206 {
    207 	int error;
    208 	struct timespec tsd;
    209 
    210 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000L)
    211 		return SET_ERROR(EINVAL);
    212 
    213 	if ((flags & TIMER_ABSTIME) != 0 || start != NULL) {
    214 		error = clock_gettime1(clock_id, &tsd);
    215 		if (error != 0)
    216 			return error;
    217 		if (start != NULL)
    218 			*start = tsd;
    219 	}
    220 
    221 	if ((flags & TIMER_ABSTIME) != 0) {
    222 		if (!timespecsubok(ts, &tsd))
    223 			return SET_ERROR(EINVAL);
    224 		timespecsub(ts, &tsd, &tsd);
    225 		ts = &tsd;
    226 	}
    227 
    228 	error = itimespecfix(ts);
    229 	if (error != 0)
    230 		return error;
    231 
    232 	if (ts->tv_sec == 0 && ts->tv_nsec == 0)
    233 		return SET_ERROR(ETIMEDOUT);
    234 
    235 	*timo = tstohz(ts);
    236 	KASSERT(*timo > 0);
    237 
    238 	return 0;
    239 }
    240