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