Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: subr_time.c,v 1.42 2025/10/05 18:51:50 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.42 2025/10/05 18:51:50 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/time.h>
     47 #include <sys/timetc.h>
     48 #include <sys/timex.h>
     49 
     50 /*
     51  * Compute number of hz until specified time.  Used to compute second
     52  * argument to callout_reset() from an absolute time.
     53  */
     54 int
     55 tvhzto(const struct timeval *tvp)
     56 {
     57 	struct timeval now, tv;
     58 
     59 	tv = *tvp;	/* Don't modify original tvp. */
     60 	getmicrotime(&now);
     61 	timersub(&tv, &now, &tv);
     62 	return tvtohz(&tv);
     63 }
     64 
     65 int
     66 tshzto(const struct timespec *tsp)
     67 {
     68 	struct timespec now, ts;
     69 
     70 	ts = *tsp;	/* Don't modify original tsp. */
     71 	getnanotime(&now);
     72 	timespecsub(&ts, &now, &ts);
     73 	return tstohz(&ts);
     74 }
     75 
     76 int
     77 tshztoup(const struct timespec *tsp)
     78 {
     79 	struct timespec now, ts;
     80 
     81 	ts = *tsp;	/* Don't modify original tsp. */
     82 	getnanouptime(&now);
     83 	timespecsub(&ts, &now, &ts);
     84 	return tstohz(&ts);
     85 }
     86 
     87 int
     88 inittimeleft(struct timespec *ts, struct timespec *sleepts)
     89 {
     90 
     91 	if (itimespecfix(ts)) {
     92 		return -1;
     93 	}
     94 	KASSERT(ts->tv_sec >= 0);
     95 	getnanouptime(sleepts);
     96 	return 0;
     97 }
     98 
     99 int
    100 gettimeleft(struct timespec *ts, struct timespec *sleepts)
    101 {
    102 	struct timespec now, sleptts;
    103 
    104 	KASSERT(ts->tv_sec >= 0);
    105 
    106 	/*
    107 	 * Reduce ts by elapsed time based on monotonic time scale.
    108 	 */
    109 	getnanouptime(&now);
    110 	KASSERT(timespeccmp(sleepts, &now, <=));
    111 	timespecsub(&now, sleepts, &sleptts);
    112 	*sleepts = now;
    113 
    114 	if (timespeccmp(ts, &sleptts, <=)) { /* timed out */
    115 		timespecclear(ts);
    116 		return 0;
    117 	}
    118 	timespecsub(ts, &sleptts, ts);
    119 
    120 	return tstohz(ts);
    121 }
    122 
    123 void
    124 clock_timeleft(clockid_t clockid, struct timespec *ts, struct timespec *sleepts)
    125 {
    126 	struct timespec sleptts;
    127 
    128 	clock_gettime1(clockid, &sleptts);
    129 	timespecadd(ts, sleepts, ts);
    130 	timespecsub(ts, &sleptts, ts);
    131 	*sleepts = sleptts;
    132 }
    133 
    134 int
    135 clock_gettime1(clockid_t clock_id, struct timespec *ts)
    136 {
    137 	int error;
    138 	struct proc *p;
    139 
    140 #define CPUCLOCK_ID_MASK (~(CLOCK_THREAD_CPUTIME_ID|CLOCK_PROCESS_CPUTIME_ID))
    141 	if (clock_id & CLOCK_PROCESS_CPUTIME_ID) {
    142 		pid_t pid = clock_id & CPUCLOCK_ID_MASK;
    143 		struct timeval cputime;
    144 
    145 		mutex_enter(&proc_lock);
    146 		p = pid == 0 ? curproc : proc_find(pid);
    147 		if (p == NULL) {
    148 			mutex_exit(&proc_lock);
    149 			return ESRCH;
    150 		}
    151 		mutex_enter(p->p_lock);
    152 		calcru(p, /*usertime*/NULL, /*systime*/NULL, /*intrtime*/NULL,
    153 		    &cputime);
    154 		mutex_exit(p->p_lock);
    155 		mutex_exit(&proc_lock);
    156 
    157 		// XXX: Perhaps create a special kauth type
    158 		error = kauth_authorize_process(kauth_cred_get(),
    159 		    KAUTH_PROCESS_PTRACE, p,
    160 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    161 		if (error)
    162 			return error;
    163 
    164 		TIMEVAL_TO_TIMESPEC(&cputime, ts);
    165 		return 0;
    166 	} else if (clock_id & CLOCK_THREAD_CPUTIME_ID) {
    167 		struct lwp *l;
    168 		lwpid_t lid = clock_id & CPUCLOCK_ID_MASK;
    169 		struct bintime tm = {0, 0};
    170 
    171 		p = curproc;
    172 		mutex_enter(p->p_lock);
    173 		l = lid == 0 ? curlwp : lwp_find(p, lid);
    174 		if (l == NULL) {
    175 			mutex_exit(p->p_lock);
    176 			return ESRCH;
    177 		}
    178 		addrulwp(l, &tm);
    179 		mutex_exit(p->p_lock);
    180 
    181 		bintime2timespec(&tm, ts);
    182 		return 0;
    183 	}
    184 
    185 	switch (clock_id) {
    186 	case CLOCK_REALTIME:
    187 		nanotime(ts);
    188 		break;
    189 	case CLOCK_MONOTONIC:
    190 		nanouptime(ts);
    191 		break;
    192 	default:
    193 		return EINVAL;
    194 	}
    195 
    196 	return 0;
    197 }
    198 
    199 /*
    200  * Calculate delta and convert from struct timespec to the ticks.
    201  */
    202 int
    203 ts2timo(clockid_t clock_id, int flags, struct timespec *ts,
    204     int *timo, struct timespec *start)
    205 {
    206 	int error;
    207 	struct timespec tsd;
    208 
    209 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000L)
    210 		return EINVAL;
    211 
    212 	if ((flags & TIMER_ABSTIME) != 0 || start != NULL) {
    213 		error = clock_gettime1(clock_id, &tsd);
    214 		if (error != 0)
    215 			return error;
    216 		if (start != NULL)
    217 			*start = tsd;
    218 	}
    219 
    220 	if ((flags & TIMER_ABSTIME) != 0) {
    221 		if (!timespecsubok(ts, &tsd))
    222 			return EINVAL;
    223 		timespecsub(ts, &tsd, &tsd);
    224 		ts = &tsd;
    225 	}
    226 
    227 	error = itimespecfix(ts);
    228 	if (error != 0)
    229 		return error;
    230 
    231 	if (ts->tv_sec == 0 && ts->tv_nsec == 0)
    232 		return ETIMEDOUT;
    233 
    234 	*timo = tstohz(ts);
    235 	KASSERT(*timo > 0);
    236 
    237 	return 0;
    238 }
    239