2005-04-17 06:20:36 +08:00
|
|
|
#ifndef _LINUX_TIME_H
|
|
|
|
#define _LINUX_TIME_H
|
|
|
|
|
2007-07-21 19:37:37 +08:00
|
|
|
# include <linux/cache.h>
|
2006-01-10 12:52:26 +08:00
|
|
|
# include <linux/seqlock.h>
|
2008-06-12 16:47:56 +08:00
|
|
|
# include <linux/math64.h>
|
2014-07-17 05:03:58 +08:00
|
|
|
# include <linux/time64.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-16 13:02:06 +08:00
|
|
|
extern struct timezone sys_tz;
|
|
|
|
|
2017-06-25 02:45:02 +08:00
|
|
|
int get_timespec64(struct timespec64 *ts,
|
|
|
|
const struct timespec __user *uts);
|
|
|
|
int put_timespec64(const struct timespec64 *ts,
|
|
|
|
struct timespec __user *uts);
|
2017-06-25 02:45:03 +08:00
|
|
|
int get_itimerspec64(struct itimerspec64 *it,
|
|
|
|
const struct itimerspec __user *uit);
|
|
|
|
int put_itimerspec64(const struct itimerspec64 *it,
|
|
|
|
struct itimerspec __user *uit);
|
2017-06-25 02:45:02 +08:00
|
|
|
|
2008-08-31 23:09:53 +08:00
|
|
|
#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
|
|
|
|
|
2007-07-17 19:03:53 +08:00
|
|
|
static inline int timespec_equal(const struct timespec *a,
|
|
|
|
const struct timespec *b)
|
2006-01-10 12:52:26 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
|
2006-01-10 12:52:26 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-02-12 09:55:52 +08:00
|
|
|
/*
|
|
|
|
* lhs < rhs: return <0
|
|
|
|
* lhs == rhs: return 0
|
|
|
|
* lhs > rhs: return >0
|
|
|
|
*/
|
2007-02-10 17:45:49 +08:00
|
|
|
static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
|
2006-02-12 09:55:52 +08:00
|
|
|
{
|
|
|
|
if (lhs->tv_sec < rhs->tv_sec)
|
|
|
|
return -1;
|
|
|
|
if (lhs->tv_sec > rhs->tv_sec)
|
|
|
|
return 1;
|
|
|
|
return lhs->tv_nsec - rhs->tv_nsec;
|
|
|
|
}
|
|
|
|
|
2007-02-10 17:45:49 +08:00
|
|
|
static inline int timeval_compare(const struct timeval *lhs, const struct timeval *rhs)
|
2006-02-12 09:55:52 +08:00
|
|
|
{
|
|
|
|
if (lhs->tv_sec < rhs->tv_sec)
|
|
|
|
return -1;
|
|
|
|
if (lhs->tv_sec > rhs->tv_sec)
|
|
|
|
return 1;
|
|
|
|
return lhs->tv_usec - rhs->tv_usec;
|
|
|
|
}
|
|
|
|
|
2014-11-18 19:15:18 +08:00
|
|
|
extern time64_t mktime64(const unsigned int year, const unsigned int mon,
|
|
|
|
const unsigned int day, const unsigned int hour,
|
|
|
|
const unsigned int min, const unsigned int sec);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated. Use mktime64().
|
|
|
|
*/
|
|
|
|
static inline unsigned long mktime(const unsigned int year,
|
|
|
|
const unsigned int mon, const unsigned int day,
|
|
|
|
const unsigned int hour, const unsigned int min,
|
|
|
|
const unsigned int sec)
|
|
|
|
{
|
|
|
|
return mktime64(year, mon, day, hour, min, sec);
|
|
|
|
}
|
2006-01-10 12:52:23 +08:00
|
|
|
|
2009-09-15 05:37:40 +08:00
|
|
|
extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
|
2010-07-14 08:56:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* timespec_add_safe assumes both values are positive and checks
|
|
|
|
* for overflow. It will return TIME_T_MAX if the reutrn would be
|
|
|
|
* smaller then either of the arguments.
|
|
|
|
*/
|
2008-08-31 23:09:53 +08:00
|
|
|
extern struct timespec timespec_add_safe(const struct timespec lhs,
|
|
|
|
const struct timespec rhs);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-07-14 08:56:19 +08:00
|
|
|
|
|
|
|
static inline struct timespec timespec_add(struct timespec lhs,
|
|
|
|
struct timespec rhs)
|
|
|
|
{
|
|
|
|
struct timespec ts_delta;
|
|
|
|
set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
|
|
|
|
lhs.tv_nsec + rhs.tv_nsec);
|
|
|
|
return ts_delta;
|
|
|
|
}
|
|
|
|
|
2006-07-14 15:24:36 +08:00
|
|
|
/*
|
|
|
|
* sub = lhs - rhs, in normalized form
|
|
|
|
*/
|
|
|
|
static inline struct timespec timespec_sub(struct timespec lhs,
|
|
|
|
struct timespec rhs)
|
|
|
|
{
|
|
|
|
struct timespec ts_delta;
|
|
|
|
set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
|
|
|
|
lhs.tv_nsec - rhs.tv_nsec);
|
|
|
|
return ts_delta;
|
|
|
|
}
|
|
|
|
|
2006-01-10 12:52:29 +08:00
|
|
|
/*
|
|
|
|
* Returns true if the timespec is norm, false if denorm:
|
|
|
|
*/
|
2012-08-09 03:36:20 +08:00
|
|
|
static inline bool timespec_valid(const struct timespec *ts)
|
|
|
|
{
|
|
|
|
/* Dates before 1970 are bogus */
|
|
|
|
if (ts->tv_sec < 0)
|
|
|
|
return false;
|
|
|
|
/* Can't have more nanoseconds then a second */
|
|
|
|
if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
|
|
|
|
return false;
|
2012-09-01 01:30:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool timespec_valid_strict(const struct timespec *ts)
|
|
|
|
{
|
|
|
|
if (!timespec_valid(ts))
|
|
|
|
return false;
|
2012-08-09 03:36:20 +08:00
|
|
|
/* Disallow values that could overflow ktime_t */
|
|
|
|
if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2006-01-10 12:52:29 +08:00
|
|
|
|
2014-12-04 08:22:48 +08:00
|
|
|
static inline bool timeval_valid(const struct timeval *tv)
|
|
|
|
{
|
|
|
|
/* Dates before 1970 are bogus */
|
|
|
|
if (tv->tv_sec < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Can't have more microseconds then a second */
|
|
|
|
if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 05:04:02 +08:00
|
|
|
extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-12-04 11:09:31 +08:00
|
|
|
/*
|
|
|
|
* Validates if a timespec/timeval used to inject a time offset is valid.
|
|
|
|
* Offsets can be postive or negative. The value of the timeval/timespec
|
|
|
|
* is the sum of its fields, but *NOTE*: the field tv_usec/tv_nsec must
|
|
|
|
* always be non-negative.
|
|
|
|
*/
|
|
|
|
static inline bool timeval_inject_offset_valid(const struct timeval *tv)
|
|
|
|
{
|
|
|
|
/* We don't check the tv_sec as it can be positive or negative */
|
|
|
|
|
|
|
|
/* Can't have more microseconds then a second */
|
|
|
|
if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool timespec_inject_offset_valid(const struct timespec *ts)
|
|
|
|
{
|
|
|
|
/* We don't check the tv_sec as it can be positive or negative */
|
|
|
|
|
|
|
|
/* Can't have more nanoseconds then a second */
|
|
|
|
if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-02 04:10:26 +08:00
|
|
|
/* Some architectures do not supply their own clocksource.
|
|
|
|
* This is mainly the case in architectures that get their
|
|
|
|
* inter-tick times by reading the counter on their interval
|
|
|
|
* timer. Since these timers wrap every tick, they're not really
|
|
|
|
* useful as clocksources. Wrapping them to act like one is possible
|
|
|
|
* but not very efficient. So we provide a callout these arches
|
|
|
|
* can implement for use with the jiffies clocksource to provide
|
|
|
|
* finer then tick granular time.
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
|
time: convert arch_gettimeoffset to a pointer
Currently, whenever CONFIG_ARCH_USES_GETTIMEOFFSET is enabled, each
arch core provides a single implementation of arch_gettimeoffset(). In
many cases, different sub-architectures, different machines, or
different timer providers exist, and so the arch ends up implementing
arch_gettimeoffset() as a call-through-pointer anyway. Examples are
ARM, Cris, M68K, and it's arguable that the remaining architectures,
M32R and Blackfin, should be doing this anyway.
Modify arch_gettimeoffset so that it itself is a function pointer, which
the arch initializes. This will allow later changes to move the
initialization of this function into individual machine support or timer
drivers. This is particularly useful for code in drivers/clocksource
which should rely on an arch-independant mechanism to register their
implementation of arch_gettimeoffset().
This patch also converts the Cris architecture to set arch_gettimeoffset
directly to the final implementation in time_init(), because Cris already
had separate time_init() functions per sub-architecture. M68K and ARM
are converted to set arch_gettimeoffset to the final implementation in
later patches, because they already have function pointers in place for
this purpose.
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Mike Frysinger <vapier@gentoo.org>
Cc: Mikael Starvik <starvik@axis.com>
Cc: Hirokazu Takata <takata@linux-m32r.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Jesper Nilsson <jesper.nilsson@axis.com>
Acked-by: John Stultz <johnstul@us.ibm.com>
Signed-off-by: Stephen Warren <swarren@nvidia.com>
2012-11-08 08:58:54 +08:00
|
|
|
extern u32 (*arch_gettimeoffset)(void);
|
2009-05-02 04:10:26 +08:00
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
struct itimerval;
|
2006-01-10 12:52:26 +08:00
|
|
|
extern int do_setitimer(int which, struct itimerval *value,
|
|
|
|
struct itimerval *ovalue);
|
2005-04-17 06:20:36 +08:00
|
|
|
extern int do_getitimer(int which, struct itimerval *value);
|
|
|
|
|
2014-07-17 05:04:02 +08:00
|
|
|
extern long do_utimes(int dfd, const char __user *filename, struct timespec *times, int flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-09-24 06:56:10 +08:00
|
|
|
/*
|
|
|
|
* Similar to the struct tm in userspace <time.h>, but it needs to be here so
|
|
|
|
* that the kernel source is self contained.
|
|
|
|
*/
|
|
|
|
struct tm {
|
|
|
|
/*
|
|
|
|
* the number of seconds after the minute, normally in the range
|
|
|
|
* 0 to 59, but can be up to 60 to allow for leap seconds
|
|
|
|
*/
|
|
|
|
int tm_sec;
|
|
|
|
/* the number of minutes after the hour, in the range 0 to 59*/
|
|
|
|
int tm_min;
|
|
|
|
/* the number of hours past midnight, in the range 0 to 23 */
|
|
|
|
int tm_hour;
|
|
|
|
/* the day of the month, in the range 1 to 31 */
|
|
|
|
int tm_mday;
|
|
|
|
/* the number of months since January, in the range 0 to 11 */
|
|
|
|
int tm_mon;
|
|
|
|
/* the number of years since 1900 */
|
|
|
|
long tm_year;
|
|
|
|
/* the number of days since Sunday, in the range 0 to 6 */
|
|
|
|
int tm_wday;
|
|
|
|
/* the number of days since January 1, in the range 0 to 365 */
|
|
|
|
int tm_yday;
|
|
|
|
};
|
|
|
|
|
2016-06-09 13:04:59 +08:00
|
|
|
void time64_to_tm(time64_t totalsecs, int offset, struct tm *result);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* time_to_tm - converts the calendar time to local broken-down time
|
|
|
|
*
|
|
|
|
* @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970,
|
|
|
|
* Coordinated Universal Time (UTC).
|
|
|
|
* @offset offset seconds adding to totalsecs.
|
|
|
|
* @result pointer to struct tm variable to receive broken-down time
|
|
|
|
*/
|
|
|
|
static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result)
|
|
|
|
{
|
|
|
|
time64_to_tm(totalsecs, offset, result);
|
|
|
|
}
|
2009-09-24 06:56:10 +08:00
|
|
|
|
2006-01-10 12:52:30 +08:00
|
|
|
/**
|
|
|
|
* timespec_to_ns - Convert timespec to nanoseconds
|
|
|
|
* @ts: pointer to the timespec variable to be converted
|
|
|
|
*
|
|
|
|
* Returns the scalar nanosecond representation of the timespec
|
|
|
|
* parameter.
|
|
|
|
*/
|
2006-03-26 17:38:11 +08:00
|
|
|
static inline s64 timespec_to_ns(const struct timespec *ts)
|
2006-01-10 12:52:30 +08:00
|
|
|
{
|
2006-03-26 17:38:11 +08:00
|
|
|
return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
|
2006-01-10 12:52:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timeval_to_ns - Convert timeval to nanoseconds
|
|
|
|
* @ts: pointer to the timeval variable to be converted
|
|
|
|
*
|
|
|
|
* Returns the scalar nanosecond representation of the timeval
|
|
|
|
* parameter.
|
|
|
|
*/
|
2006-03-26 17:38:11 +08:00
|
|
|
static inline s64 timeval_to_ns(const struct timeval *tv)
|
2006-01-10 12:52:30 +08:00
|
|
|
{
|
2006-03-26 17:38:11 +08:00
|
|
|
return ((s64) tv->tv_sec * NSEC_PER_SEC) +
|
2006-01-10 12:52:30 +08:00
|
|
|
tv->tv_usec * NSEC_PER_USEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ns_to_timespec - Convert nanoseconds to timespec
|
|
|
|
* @nsec: the nanoseconds value to be converted
|
|
|
|
*
|
|
|
|
* Returns the timespec representation of the nsec parameter.
|
|
|
|
*/
|
2006-03-26 17:38:11 +08:00
|
|
|
extern struct timespec ns_to_timespec(const s64 nsec);
|
2006-01-10 12:52:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ns_to_timeval - Convert nanoseconds to timeval
|
|
|
|
* @nsec: the nanoseconds value to be converted
|
|
|
|
*
|
|
|
|
* Returns the timeval representation of the nsec parameter.
|
|
|
|
*/
|
2006-03-26 17:38:11 +08:00
|
|
|
extern struct timeval ns_to_timeval(const s64 nsec);
|
2006-01-10 12:52:30 +08:00
|
|
|
|
2006-06-26 15:25:08 +08:00
|
|
|
/**
|
|
|
|
* timespec_add_ns - Adds nanoseconds to a timespec
|
|
|
|
* @a: pointer to timespec to be incremented
|
|
|
|
* @ns: unsigned nanoseconds value to be added
|
2008-06-12 16:48:00 +08:00
|
|
|
*
|
|
|
|
* This must always be inlined because its used from the x86-64 vdso,
|
|
|
|
* which cannot call other kernel functions.
|
2006-06-26 15:25:08 +08:00
|
|
|
*/
|
2008-06-12 16:48:00 +08:00
|
|
|
static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
|
2006-06-26 15:25:08 +08:00
|
|
|
{
|
2008-06-12 16:48:00 +08:00
|
|
|
a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
|
2006-06-26 15:25:08 +08:00
|
|
|
a->tv_nsec = ns;
|
|
|
|
}
|
2012-02-08 13:09:05 +08:00
|
|
|
|
2017-06-25 02:45:03 +08:00
|
|
|
static inline bool itimerspec64_valid(const struct itimerspec64 *its)
|
|
|
|
{
|
|
|
|
if (!timespec64_valid(&(its->it_interval)) ||
|
|
|
|
!timespec64_valid(&(its->it_value)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-31 23:09:45 +08:00
|
|
|
/**
|
|
|
|
* time_after32 - compare two 32-bit relative times
|
|
|
|
* @a: the time which may be after @b
|
|
|
|
* @b: the time which may be before @a
|
|
|
|
*
|
|
|
|
* time_after32(a, b) returns true if the time @a is after time @b.
|
|
|
|
* time_before32(b, a) returns true if the time @b is before time @a.
|
|
|
|
*
|
|
|
|
* Similar to time_after(), compare two 32-bit timestamps for relative
|
|
|
|
* times. This is useful for comparing 32-bit seconds values that can't
|
|
|
|
* be converted to 64-bit values (e.g. due to disk format or wire protocol
|
|
|
|
* issues) when it is known that the times are less than 68 years apart.
|
|
|
|
*/
|
|
|
|
#define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
|
|
|
|
#define time_before32(b, a) time_after32(a, b)
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|