vserver 1.9.3
[linux-2.6.git] / kernel / time.c
index 142a4bd..167a72a 100644 (file)
@@ -22,6 +22,9 @@
  *     "A Kernel Model for Precision Timekeeping" by Dave Mills
  *     Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
  *     (Even though the technical memorandum forbids it)
+ * 2004-07-14   Christoph Lameter
+ *     Added getnstimeofday to allow the posix timer functions to return
+ *     with nanosecond accuracy
  */
 
 #include <linux/module.h>
@@ -29,6 +32,7 @@
 #include <linux/errno.h>
 #include <linux/smp_lock.h>
 #include <asm/uaccess.h>
+#include <asm/unistd.h>
 
 /* 
  * The timezone where the local system is located.  Used as a default by some
@@ -38,7 +42,7 @@ struct timezone sys_tz;
 
 EXPORT_SYMBOL(sys_tz);
 
-#if !defined(__alpha__) && !defined(__ia64__)
+#ifdef __ARCH_WANT_SYS_TIME
 
 /*
  * sys_time() can be implemented in user-level using
@@ -48,7 +52,7 @@ EXPORT_SYMBOL(sys_tz);
  *
  * XXX This function is NOT 64-bit clean!
  */
-asmlinkage long sys_time(int * tloc)
+asmlinkage long sys_time(int __user * tloc)
 {
        int i;
        struct timeval tv;
@@ -70,7 +74,7 @@ asmlinkage long sys_time(int * tloc)
  * architectures that need it).
  */
  
-asmlinkage long sys_stime(time_t *tptr)
+asmlinkage long sys_stime(time_t __user *tptr)
 {
        struct timespec tv;
 
@@ -84,7 +88,7 @@ asmlinkage long sys_stime(time_t *tptr)
        return 0;
 }
 
-#endif
+#endif /* __ARCH_WANT_SYS_TIME */
 
 asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz)
 {
@@ -122,7 +126,7 @@ inline static void warp_clock(void)
        write_seqlock_irq(&xtime_lock);
        wall_to_monotonic.tv_sec -= sys_tz.tz_minuteswest * 60;
        xtime.tv_sec += sys_tz.tz_minuteswest * 60;
-       time_interpolator_update(sys_tz.tz_minuteswest * 60 * NSEC_PER_SEC);
+       time_interpolator_reset();
        write_sequnlock_irq(&xtime_lock);
        clock_was_set();
 }
@@ -420,6 +424,103 @@ struct timespec current_kernel_time(void)
 
 EXPORT_SYMBOL(current_kernel_time);
 
+#ifdef CONFIG_TIME_INTERPOLATION
+void getnstimeofday (struct timespec *tv)
+{
+       unsigned long seq,sec,nsec;
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               sec = xtime.tv_sec;
+               nsec = xtime.tv_nsec+time_interpolator_get_offset();
+       } while (unlikely(read_seqretry(&xtime_lock, seq)));
+
+       while (unlikely(nsec >= NSEC_PER_SEC)) {
+               nsec -= NSEC_PER_SEC;
+               ++sec;
+       }
+       tv->tv_sec = sec;
+       tv->tv_nsec = nsec;
+}
+
+int do_settimeofday (struct timespec *tv)
+{
+       time_t wtm_sec, sec = tv->tv_sec;
+       long wtm_nsec, nsec = tv->tv_nsec;
+
+       if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
+               return -EINVAL;
+
+       write_seqlock_irq(&xtime_lock);
+       {
+               /*
+                * This is revolting. We need to set "xtime" correctly. However, the value
+                * in this location is the value at the most recent update of wall time.
+                * Discover what correction gettimeofday would have done, and then undo
+                * it!
+                */
+               nsec -= time_interpolator_get_offset();
+
+               wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
+               wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
+
+               set_normalized_timespec(&xtime, sec, nsec);
+               set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
+
+               time_adjust = 0;                /* stop active adjtime() */
+               time_status |= STA_UNSYNC;
+               time_maxerror = NTP_PHASE_LIMIT;
+               time_esterror = NTP_PHASE_LIMIT;
+               time_interpolator_reset();
+       }
+       write_sequnlock_irq(&xtime_lock);
+       clock_was_set();
+       return 0;
+}
+
+EXPORT_SYMBOL(do_settimeofday);
+
+void do_gettimeofday (struct timeval *tv)
+{
+       unsigned long seq, nsec, usec, sec, offset;
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               offset = time_interpolator_get_offset();
+               sec = xtime.tv_sec;
+               nsec = xtime.tv_nsec;
+       } while (unlikely(read_seqretry(&xtime_lock, seq)));
+
+       usec = (nsec + offset) / 1000;
+
+       while (unlikely(usec >= USEC_PER_SEC)) {
+               usec -= USEC_PER_SEC;
+               ++sec;
+       }
+
+       tv->tv_sec = sec;
+       tv->tv_usec = usec;
+}
+
+EXPORT_SYMBOL(do_gettimeofday);
+
+
+#else
+/*
+ * Simulate gettimeofday using do_gettimeofday which only allows a timeval
+ * and therefore only yields usec accuracy
+ */
+void getnstimeofday(struct timespec *tv)
+{
+       struct timeval x;
+
+       do_gettimeofday(&x);
+       tv->tv_sec = x.tv_sec;
+       tv->tv_nsec = x.tv_usec * NSEC_PER_USEC;
+}
+#endif
+
+EXPORT_SYMBOL(getnstimeofday);
+
 #if (BITS_PER_LONG < 64)
 u64 get_jiffies_64(void)
 {