12c2b5e8Patrick Mooney/*
22c2b5e8Patrick Mooney * This file and its contents are supplied under the terms of the
32c2b5e8Patrick Mooney * Common Development and Distribution License ("CDDL"), version 1.0.
42c2b5e8Patrick Mooney * You may only use this file in accordance with the terms of version
52c2b5e8Patrick Mooney * 1.0 of the CDDL.
62c2b5e8Patrick Mooney *
72c2b5e8Patrick Mooney * A full copy of the text of the CDDL should have accompanied this
82c2b5e8Patrick Mooney * source.  A copy of the CDDL is also available via the Internet at
92c2b5e8Patrick Mooney * http://www.illumos.org/license/CDDL.
102c2b5e8Patrick Mooney */
112c2b5e8Patrick Mooney
122c2b5e8Patrick Mooney/*
132c2b5e8Patrick Mooney * Copyright (c) 2017, Joyent, Inc.
142c2b5e8Patrick Mooney */
152c2b5e8Patrick Mooney
162c2b5e8Patrick Mooney
172c2b5e8Patrick Mooney#include "thr_uberdata.h"
182c2b5e8Patrick Mooney#include <cp_defs.h>
192c2b5e8Patrick Mooney
202c2b5e8Patrick Mooney#pragma weak _gettimeofday = gettimeofday
212c2b5e8Patrick Mooney
222c2b5e8Patrick Mooneyextern int __clock_gettime_sys(clockid_t, timespec_t *);
232c2b5e8Patrick Mooney
242c2b5e8Patrick Mooneyint
252c2b5e8Patrick Mooneygettimeofday(struct timeval *tv, void *tz)
262c2b5e8Patrick Mooney{
272c2b5e8Patrick Mooney	comm_page_t *cp = (comm_page_t *)__uberdata.ub_comm_page;
282c2b5e8Patrick Mooney
292c2b5e8Patrick Mooney	/*
302c2b5e8Patrick Mooney	 * Perform a NULL check before attempting to store the result directly.
312c2b5e8Patrick Mooney	 * The old fasttrap logic would perform this same check, but after the
322c2b5e8Patrick Mooney	 * call into hrestime().
332c2b5e8Patrick Mooney	 */
342c2b5e8Patrick Mooney	if (tv == NULL) {
352c2b5e8Patrick Mooney		return (0);
362c2b5e8Patrick Mooney	}
372c2b5e8Patrick Mooney
382c2b5e8Patrick Mooney	/*
392c2b5e8Patrick Mooney	 * Since timeval and timespec structs feature the same effective types
402c2b5e8Patrick Mooney	 * and layout of their members, the conversion can be done in-place.
412c2b5e8Patrick Mooney	 */
422c2b5e8Patrick Mooney	if (cp != NULL && __cp_can_gettime(cp) != 0) {
4393dc830Toomas Soome		(void) __cp_clock_gettime_realtime(cp, (struct timespec *)tv);
442c2b5e8Patrick Mooney	} else {
4593dc830Toomas Soome		(void) __clock_gettime_sys(CLOCK_REALTIME,
4693dc830Toomas Soome		    (struct timespec *)tv);
472c2b5e8Patrick Mooney	}
482c2b5e8Patrick Mooney	/* Convert from tv_nsec to tv_usec */
492c2b5e8Patrick Mooney	tv->tv_usec /= 1000;
502c2b5e8Patrick Mooney	return (0);
512c2b5e8Patrick Mooney}