uml: track and make up lost ticks
[pandora-kernel.git] / arch / um / os-Linux / time.c
index e492805..bee98f4 100644 (file)
@@ -9,7 +9,9 @@
 #include <time.h>
 #include <sys/time.h>
 #include "kern_constants.h"
+#include "kern_util.h"
 #include "os.h"
+#include "process.h"
 #include "user.h"
 
 int set_interval(void)
@@ -58,12 +60,17 @@ static inline long long timeval_to_ns(const struct timeval *tv)
 long long disable_timer(void)
 {
        struct itimerval time = ((struct itimerval) { { 0, 0 }, { 0, 0 } });
+       int remain, max = UM_NSEC_PER_SEC / UM_HZ;
 
        if (setitimer(ITIMER_VIRTUAL, &time, &time) < 0)
                printk(UM_KERN_ERR "disable_timer - setitimer failed, "
                       "errno = %d\n", errno);
 
-       return timeval_to_ns(&time.it_value);
+       remain = timeval_to_ns(&time.it_value);
+       if (remain > max)
+               remain = max;
+
+       return remain;
 }
 
 long long os_nsecs(void)
@@ -79,7 +86,44 @@ static int after_sleep_interval(struct timespec *ts)
 {
        return 0;
 }
+
+static void deliver_alarm(void)
+{
+       alarm_handler(SIGVTALRM, NULL);
+}
+
+static unsigned long long sleep_time(unsigned long long nsecs)
+{
+       return nsecs;
+}
+
 #else
+unsigned long long last_tick;
+unsigned long long skew;
+
+static void deliver_alarm(void)
+{
+       unsigned long long this_tick = os_nsecs();
+       int one_tick = UM_NSEC_PER_SEC / UM_HZ;
+
+       if (last_tick == 0)
+               last_tick = this_tick - one_tick;
+
+       skew += this_tick - last_tick;
+
+       while (skew >= one_tick) {
+               alarm_handler(SIGVTALRM, NULL);
+               skew -= one_tick;
+       }
+
+       last_tick = this_tick;
+}
+
+static unsigned long long sleep_time(unsigned long long nsecs)
+{
+       return nsecs > skew ? nsecs - skew : 0;
+}
+
 static inline long long timespec_to_us(const struct timespec *ts)
 {
        return ((long long) ts->tv_sec * UM_USEC_PER_SEC) +
@@ -102,6 +146,8 @@ static int after_sleep_interval(struct timespec *ts)
         */
        if (start_usecs > usec)
                start_usecs = usec;
+
+       start_usecs -= skew / UM_NSEC_PER_USEC;
        tv = ((struct timeval) { .tv_sec  = start_usecs / UM_USEC_PER_SEC,
                                 .tv_usec = start_usecs % UM_USEC_PER_SEC });
        interval = ((struct itimerval) { { 0, usec }, tv });
@@ -113,8 +159,6 @@ static int after_sleep_interval(struct timespec *ts)
 }
 #endif
 
-extern void alarm_handler(int sig, struct sigcontext *sc);
-
 void idle_sleep(unsigned long long nsecs)
 {
        struct timespec ts;
@@ -126,10 +170,12 @@ void idle_sleep(unsigned long long nsecs)
         */
        if (nsecs == 0)
                nsecs = UM_NSEC_PER_SEC / UM_HZ;
+
+       nsecs = sleep_time(nsecs);
        ts = ((struct timespec) { .tv_sec       = nsecs / UM_NSEC_PER_SEC,
                                  .tv_nsec      = nsecs % UM_NSEC_PER_SEC });
 
        if (nanosleep(&ts, &ts) == 0)
-               alarm_handler(SIGVTALRM, NULL);
+               deliver_alarm();
        after_sleep_interval(&ts);
 }