1/*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source.  A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12/*
13 * Copyright (c) 2017, Joyent, Inc.
14 */
15
16
17#include "thr_uberdata.h"
18#include <cp_defs.h>
19
20#pragma weak _gettimeofday = gettimeofday
21
22extern int __clock_gettime_sys(clockid_t, timespec_t *);
23
24int
25gettimeofday(struct timeval *tv, void *tz)
26{
27	comm_page_t *cp = (comm_page_t *)__uberdata.ub_comm_page;
28
29	/*
30	 * Perform a NULL check before attempting to store the result directly.
31	 * The old fasttrap logic would perform this same check, but after the
32	 * call into hrestime().
33	 */
34	if (tv == NULL) {
35		return (0);
36	}
37
38	/*
39	 * Since timeval and timespec structs feature the same effective types
40	 * and layout of their members, the conversion can be done in-place.
41	 */
42	if (cp != NULL && __cp_can_gettime(cp) != 0) {
43		(void) __cp_clock_gettime_realtime(cp, (struct timespec *)tv);
44	} else {
45		(void) __clock_gettime_sys(CLOCK_REALTIME,
46		    (struct timespec *)tv);
47	}
48	/* Convert from tv_nsec to tv_usec */
49	tv->tv_usec /= 1000;
50	return (0);
51}
52