vgettimeofday.c 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2012 Tilera Corporation. All Rights Reserved.
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation, version 2.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 *   NON INFRINGEMENT.  See the GNU General Public License for
 *   more details.
 */

#define VDSO_BUILD  /* avoid some shift warnings for -m32 in <asm/page.h> */
#include <linux/time.h>
#include <asm/timex.h>
18
#include <asm/unistd.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <asm/vdso.h>

#if CHIP_HAS_SPLIT_CYCLE()
static inline cycles_t get_cycles_inline(void)
{
	unsigned int high = __insn_mfspr(SPR_CYCLE_HIGH);
	unsigned int low = __insn_mfspr(SPR_CYCLE_LOW);
	unsigned int high2 = __insn_mfspr(SPR_CYCLE_HIGH);

	while (unlikely(high != high2)) {
		low = __insn_mfspr(SPR_CYCLE_LOW);
		high = high2;
		high2 = __insn_mfspr(SPR_CYCLE_HIGH);
	}

	return (((cycles_t)high) << 32) | low;
}
#define get_cycles get_cycles_inline
#endif

39 40 41 42 43
struct syscall_return_value {
	long value;
	long error;
};

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * Find out the vDSO data page address in the process address space.
 */
inline unsigned long get_datapage(void)
{
	unsigned long ret;

	/* vdso data page located in the 2nd vDSO page. */
	asm volatile ("lnk %0" : "=r"(ret));
	ret &= ~(PAGE_SIZE - 1);
	ret += PAGE_SIZE;

	return ret;
}

59 60 61 62 63 64 65 66 67 68 69
static inline u64 vgetsns(struct vdso_data *vdso)
{
	return ((get_cycles() - vdso->cycle_last) & vdso->mask) * vdso->mult;
}

static inline int do_realtime(struct vdso_data *vdso, struct timespec *ts)
{
	unsigned count;
	u64 ns;

	do {
70
		count = raw_read_seqcount_begin(&vdso->tb_seq);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		ts->tv_sec = vdso->wall_time_sec;
		ns = vdso->wall_time_snsec;
		ns += vgetsns(vdso);
		ns >>= vdso->shift;
	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));

	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
	ts->tv_nsec = ns;

	return 0;
}

static inline int do_monotonic(struct vdso_data *vdso, struct timespec *ts)
{
	unsigned count;
	u64 ns;

	do {
89
		count = raw_read_seqcount_begin(&vdso->tb_seq);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		ts->tv_sec = vdso->monotonic_time_sec;
		ns = vdso->monotonic_time_snsec;
		ns += vgetsns(vdso);
		ns >>= vdso->shift;
	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));

	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
	ts->tv_nsec = ns;

	return 0;
}

static inline int do_realtime_coarse(struct vdso_data *vdso,
				     struct timespec *ts)
{
	unsigned count;

	do {
108
		count = raw_read_seqcount_begin(&vdso->tb_seq);
109 110 111 112 113 114 115 116 117
		ts->tv_sec = vdso->wall_time_coarse_sec;
		ts->tv_nsec = vdso->wall_time_coarse_nsec;
	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));

	return 0;
}

static inline int do_monotonic_coarse(struct vdso_data *vdso,
				      struct timespec *ts)
118
{
119
	unsigned count;
120 121

	do {
122
		count = raw_read_seqcount_begin(&vdso->tb_seq);
123 124 125 126 127 128 129 130 131 132 133 134
		ts->tv_sec = vdso->monotonic_time_coarse_sec;
		ts->tv_nsec = vdso->monotonic_time_coarse_nsec;
	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));

	return 0;
}

struct syscall_return_value __vdso_gettimeofday(struct timeval *tv,
						struct timezone *tz)
{
	struct syscall_return_value ret = { 0, 0 };
	unsigned count;
135
	struct vdso_data *vdso = (struct vdso_data *)get_datapage();
136 137 138

	/* The use of the timezone is obsolete, normally tz is NULL. */
	if (unlikely(tz != NULL)) {
139
		do {
140
			count = raw_read_seqcount_begin(&vdso->tz_seq);
141 142 143
			tz->tz_minuteswest = vdso->tz_minuteswest;
			tz->tz_dsttime = vdso->tz_dsttime;
		} while (unlikely(read_seqcount_retry(&vdso->tz_seq, count)));
144 145 146
	}

	if (unlikely(tv == NULL))
147
		return ret;
148

149 150
	do_realtime(vdso, (struct timespec *)tv);
	tv->tv_usec /= 1000;
151

152
	return ret;
153 154 155 156
}

int gettimeofday(struct timeval *tv, struct timezone *tz)
	__attribute__((weak, alias("__vdso_gettimeofday")));
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

static struct syscall_return_value vdso_fallback_gettime(long clock,
							 struct timespec *ts)
{
	struct syscall_return_value ret;
	__asm__ __volatile__ (
		"swint1"
		: "=R00" (ret.value), "=R01" (ret.error)
		: "R10" (__NR_clock_gettime), "R00" (clock), "R01" (ts)
		: "r2", "r3", "r4", "r5", "r6", "r7",
		"r8",  "r9", "r11", "r12", "r13", "r14", "r15",
		"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
		"r24", "r25", "r26", "r27", "r28", "r29", "memory");
	return ret;
}

struct syscall_return_value __vdso_clock_gettime(clockid_t clock,
						 struct timespec *ts)
{
	struct vdso_data *vdso = (struct vdso_data *)get_datapage();
	struct syscall_return_value ret = { 0, 0 };

	switch (clock) {
	case CLOCK_REALTIME:
		do_realtime(vdso, ts);
		return ret;
	case CLOCK_MONOTONIC:
		do_monotonic(vdso, ts);
		return ret;
	case CLOCK_REALTIME_COARSE:
		do_realtime_coarse(vdso, ts);
		return ret;
	case CLOCK_MONOTONIC_COARSE:
		do_monotonic_coarse(vdso, ts);
		return ret;
	default:
		return vdso_fallback_gettime(clock, ts);
	}
}

int clock_gettime(clockid_t clock, struct timespec *ts)
	__attribute__((weak, alias("__vdso_clock_gettime")));