[PATCH] uml: move libc-dependent time code
authorGennady Sharapov <Gennady.V.Sharapov@intel.com>
Thu, 19 Jan 2006 01:42:42 +0000 (17:42 -0800)
committerLinus Torvalds <torvalds@g5.osdl.org>
Thu, 19 Jan 2006 03:20:19 +0000 (19:20 -0800)
The serial UML OS-abstraction layer patch (um/kernel dir).

This moves all systemcalls from time.c file under os-Linux dir and joins
time.c and tine_kernel.c files

Signed-off-by: Gennady Sharapov <Gennady.V.Sharapov@intel.com>
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
16 files changed:
arch/um/include/os.h
arch/um/include/time_user.h [deleted file]
arch/um/kernel/Makefile
arch/um/kernel/exec_kern.c
arch/um/kernel/process_kern.c
arch/um/kernel/skas/process.c
arch/um/kernel/skas/process_kern.c
arch/um/kernel/syscall.c
arch/um/kernel/time_kern.c
arch/um/kernel/tt/exec_kern.c
arch/um/kernel/tt/process_kern.c
arch/um/os-Linux/main.c
arch/um/os-Linux/signal.c
arch/um/os-Linux/start_up.c
arch/um/os-Linux/time.c
arch/um/os-Linux/tt.c

index 7f27dfe0ecac7ad4db8ddaa0a71f0551ca191640..624938ad9e14bfc55ff092a3b84207624926ac5d 100644 (file)
@@ -190,7 +190,6 @@ extern int os_protect_memory(void *addr, unsigned long len,
                             int r, int w, int x);
 extern int os_unmap_memory(void *addr, int len);
 extern void os_flush_stdout(void);
-extern unsigned long long os_usecs(void);
 
 /* tt.c
  * for tt mode only (will be deleted in future...)
@@ -245,4 +244,15 @@ extern void setup_machinename(char *machine_out);
 extern void setup_hostinfo(void);
 extern int setjmp_wrapper(void (*proc)(void *, void *), ...);
 
+/* time.c */
+#define BILLION (1000 * 1000 * 1000)
+
+extern void switch_timers(int to_real);
+extern void idle_sleep(int secs);
+extern void enable_timer(void);
+extern void disable_timer(void);
+extern void user_time_init(void);
+extern void uml_idle_timer(void);
+extern unsigned long long os_nsecs(void);
+
 #endif
diff --git a/arch/um/include/time_user.h b/arch/um/include/time_user.h
deleted file mode 100644 (file)
index 17d7ef2..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/* 
- * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
- * Licensed under the GPL
- */
-
-#ifndef __TIME_USER_H__
-#define __TIME_USER_H__
-
-extern void timer(void);
-extern void switch_timers(int to_real);
-extern void idle_sleep(int secs);
-extern void enable_timer(void);
-extern void prepare_timer(void * ptr);
-extern void disable_timer(void);
-extern unsigned long time_lock(void);
-extern void time_unlock(unsigned long);
-extern void user_time_init(void);
-
-#endif
index 165c8639c9f682c9cd528a012b04ce29a1bd8b43..693018ba80f1f956d8c7d13fd863847d433514e3 100644 (file)
@@ -9,7 +9,7 @@ clean-files :=
 obj-y = config.o exec_kern.o exitcode.o \
        init_task.o irq.o irq_user.o ksyms.o mem.o physmem.o \
        process_kern.o ptrace.o reboot.o resource.o sigio_user.o sigio_kern.o \
-       signal_kern.o smp.o syscall_kern.o sysrq.o time.o \
+       signal_kern.o smp.o syscall_kern.o sysrq.o \
        time_kern.o tlb.o trap_kern.o uaccess.o um_arch.o umid.o
 
 obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o
@@ -23,7 +23,7 @@ obj-$(CONFIG_MODE_SKAS) += skas/
 
 user-objs-$(CONFIG_TTY_LOG) += tty_log.o
 
-USER_OBJS := $(user-objs-y) config.o time.o tty_log.o
+USER_OBJS := $(user-objs-y) config.o tty_log.o
 
 include arch/um/scripts/Makefile.rules
 
index efd222ffe20e1df22fa2e04a4c61cdd5d6a3a419..569fe8b9b0535d362d20e3b14d215f46aaae67cb 100644 (file)
@@ -17,7 +17,6 @@
 #include "irq_user.h"
 #include "tlb.h"
 #include "os.h"
-#include "time_user.h"
 #include "choose-mode.h"
 #include "mode_kern.h"
 
index 7f13b85d26564609d2f1c2fca954024a3bf28a4f..e167cf0a71f4657d7bd0e52d7144fab1a839f147 100644 (file)
@@ -39,7 +39,6 @@
 #include "init.h"
 #include "irq_user.h"
 #include "mem_user.h"
-#include "time_user.h"
 #include "tlb.h"
 #include "frame_kern.h"
 #include "sigcontext.h"
index 3b3955d844070f781f7d3b1ffe69ef969ae395ff..eea1c9c4bb0fcd531803dbd6f9bfb468ed87055f 100644 (file)
@@ -18,7 +18,6 @@
 #include <asm/types.h>
 #include "user.h"
 #include "ptrace_user.h"
-#include "time_user.h"
 #include "sysdep/ptrace.h"
 #include "user_util.h"
 #include "kern_util.h"
index fcddee76c7c08f57e0e328294d7b33d29df6f06e..a340006dec47938425b4191f50a3a7cebfdf2097 100644 (file)
@@ -13,7 +13,6 @@
 #include "asm/uaccess.h"
 #include "asm/atomic.h"
 #include "kern_util.h"
-#include "time_user.h"
 #include "skas.h"
 #include "os.h"
 #include "user_util.h"
index 1429c131879d2c0f4a7ea548713e6078d313960b..1731d90e68507e1316cfa4a3c6c3d15f510f316b 100644 (file)
@@ -25,12 +25,12 @@ int record_syscall_start(int syscall)
        syscall_record[index].syscall = syscall;
        syscall_record[index].pid = current_pid();
        syscall_record[index].result = 0xdeadbeef;
-       syscall_record[index].start = os_usecs();
+       syscall_record[index].start = os_nsecs();
        return(index);
 }
 
 void record_syscall_end(int index, long result)
 {
        syscall_record[index].result = result;
-       syscall_record[index].end = os_usecs();
+       syscall_record[index].end = os_nsecs();
 }
index 020ca79b8d33200b3c6f4c29c582e4528cf5a00b..6712ffad0242c55e3ce1c33e340e55ee09c818c8 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
  * Licensed under the GPL
  */
 #include "linux/interrupt.h"
 #include "linux/init.h"
 #include "linux/delay.h"
+#include "linux/hrtimer.h"
 #include "asm/irq.h"
 #include "asm/param.h"
 #include "asm/current.h"
 #include "kern_util.h"
 #include "user_util.h"
-#include "time_user.h"
 #include "mode.h"
 #include "os.h"
 
@@ -39,7 +39,7 @@ unsigned long long sched_clock(void)
 int timer_irq_inited = 0;
 
 static int first_tick;
-static unsigned long long prev_usecs;
+static unsigned long long prev_nsecs;
 #ifdef CONFIG_UML_REAL_TIME_CLOCK
 static long long delta;                /* Deviation per interval */
 #endif
@@ -58,23 +58,23 @@ void timer_irq(union uml_pt_regs *regs)
        if(first_tick){
 #ifdef CONFIG_UML_REAL_TIME_CLOCK
                /* We've had 1 tick */
-               unsigned long long usecs = os_usecs();
+               unsigned long long nsecs = os_nsecs();
 
-               delta += usecs - prev_usecs;
-               prev_usecs = usecs;
+               delta += nsecs - prev_nsecs;
+               prev_nsecs = nsecs;
 
                /* Protect against the host clock being set backwards */
                if(delta < 0)
                        delta = 0;
 
-               ticks += (delta * HZ) / MILLION;
-               delta -= (ticks * MILLION) / HZ;
+               ticks += (delta * HZ) / BILLION;
+               delta -= (ticks * BILLION) / HZ;
 #else
                ticks = 1;
 #endif
        }
        else {
-               prev_usecs = os_usecs();
+               prev_nsecs = os_nsecs();
                first_tick = 1;
        }
 
@@ -88,45 +88,99 @@ void boot_timer_handler(int sig)
 {
        struct pt_regs regs;
 
-       CHOOSE_MODE((void) 
+       CHOOSE_MODE((void)
                    (UPT_SC(&regs.regs) = (struct sigcontext *) (&sig + 1)),
                    (void) (regs.regs.skas.is_user = 0));
        do_timer(&regs);
 }
 
+static DEFINE_SPINLOCK(timer_spinlock);
+
+static unsigned long long local_offset = 0;
+
+static inline unsigned long long get_time(void)
+{
+       unsigned long long nsecs;
+       unsigned long flags;
+
+       spin_lock_irqsave(&timer_spinlock, flags);
+       nsecs = os_nsecs();
+       nsecs += local_offset;
+       spin_unlock_irqrestore(&timer_spinlock, flags);
+
+       return nsecs;
+}
+
 irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
 {
+       unsigned long long nsecs;
        unsigned long flags;
 
        do_timer(regs);
+
        write_seqlock_irqsave(&xtime_lock, flags);
-       timer();
+       nsecs = get_time() + local_offset;
+       xtime.tv_sec = nsecs / NSEC_PER_SEC;
+       xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
        write_sequnlock_irqrestore(&xtime_lock, flags);
+
        return(IRQ_HANDLED);
 }
 
 long um_time(int __user *tloc)
 {
-       struct timeval now;
+       long ret = get_time() / NSEC_PER_SEC;
 
-       do_gettimeofday(&now);
-       if (tloc) {
-               if (put_user(now.tv_sec, tloc))
-                       now.tv_sec = -EFAULT;
-       }
-       return now.tv_sec;
+       if((tloc != NULL) && put_user(ret, tloc))
+               return -EFAULT;
+
+       return ret;
+}
+
+void do_gettimeofday(struct timeval *tv)
+{
+       unsigned long long nsecs = get_time();
+
+       tv->tv_sec = nsecs / NSEC_PER_SEC;
+       /* Careful about calculations here - this was originally done as
+        * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
+        * which gave bogus (> 1000000) values.  Dunno why, suspect gcc
+        * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
+        * problem that I missed.
+        */
+       nsecs -= tv->tv_sec * NSEC_PER_SEC;
+       tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
+}
+
+static inline void set_time(unsigned long long nsecs)
+{
+       unsigned long long now;
+       unsigned long flags;
+
+       spin_lock_irqsave(&timer_spinlock, flags);
+       now = os_nsecs();
+       local_offset = nsecs - now;
+       spin_unlock_irqrestore(&timer_spinlock, flags);
+
+       clock_was_set();
 }
 
 long um_stime(int __user *tptr)
 {
        int value;
-       struct timespec new;
 
        if (get_user(value, tptr))
                 return -EFAULT;
-       new.tv_sec = value;
-       new.tv_nsec = 0;
-       do_settimeofday(&new);
+
+       set_time((unsigned long long) value * NSEC_PER_SEC);
+
+       return 0;
+}
+
+int do_settimeofday(struct timespec *tv)
+{
+       set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
+
        return 0;
 }
 
@@ -142,21 +196,6 @@ void timer_handler(int sig, union uml_pt_regs *regs)
                timer_irq(regs);
 }
 
-static DEFINE_SPINLOCK(timer_spinlock);
-
-unsigned long time_lock(void)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&timer_spinlock, flags);
-       return(flags);
-}
-
-void time_unlock(unsigned long flags)
-{
-       spin_unlock_irqrestore(&timer_spinlock, flags);
-}
-
 int __init timer_init(void)
 {
        int err;
@@ -171,14 +210,3 @@ int __init timer_init(void)
 }
 
 __initcall(timer_init);
-
-/*
- * Overrides for Emacs so that we follow Linus's tabbing style.
- * Emacs will notice this stuff at the end of the file and automatically
- * adjust the settings for this buffer only.  This must remain at the end
- * of the file.
- * ---------------------------------------------------------------------------
- * Local variables:
- * c-file-style: "linux"
- * End:
- */
index 8f40e4838736988645c1960b8e302d7ca52f48db..5c1e4cc1c0493aa85bd86e7e1bc41dff1ea2c086 100644 (file)
@@ -13,7 +13,6 @@
 #include "user_util.h"
 #include "kern_util.h"
 #include "irq_user.h"
-#include "time_user.h"
 #include "mem_user.h"
 #include "os.h"
 #include "tlb.h"
index 62535303aa277233241650a8c0a8686d1a6fb9c8..295c1ac817b34cc6f8d04aeab783539b5004060a 100644 (file)
@@ -18,7 +18,6 @@
 #include "os.h"
 #include "kern.h"
 #include "sigcontext.h"
-#include "time_user.h"
 #include "mem_user.h"
 #include "tlb.h"
 #include "mode.h"
index 172c8474453c9c4baa229d66c0d619ba243d40f3..98becd18f2112175ea4c37d17cf30a9cd36ca6a6 100644 (file)
@@ -16,7 +16,6 @@
 #include "user_util.h"
 #include "kern_util.h"
 #include "mem_user.h"
-#include "time_user.h"
 #include "irq_user.h"
 #include "user.h"
 #include "init.h"
index c1f46a0fef13d14c8740d80091fb18a2cf0690d7..da474a797fbf23b0ff6f621ae2b24115fc01cff3 100644 (file)
@@ -18,8 +18,8 @@
 #include "sysdep/sigcontext.h"
 #include "sysdep/signal.h"
 #include "sigcontext.h"
-#include "time_user.h"
 #include "mode.h"
+#include "os.h"
 
 void sig_handler(ARCH_SIGHDLR_PARAM)
 {
index b47e5e71d1a5d14d0ebefef0589ceef269a20851..6c5b17ed59e1170baca1f67854211d2c127e869a 100644 (file)
@@ -29,7 +29,6 @@
 #include "irq_user.h"
 #include "ptrace_user.h"
 #include "mem_user.h"
-#include "time_user.h"
 #include "init.h"
 #include "os.h"
 #include "uml-config.h"
index cf30a39bc4841a58ec27d0a1863732c4a4ee1ebd..6f7626775acb983e7f2e663d2312533d80c1a792 100644 (file)
+/*
+ * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
+ * Licensed under the GPL
+ */
+
+#include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
 #include <sys/time.h>
+#include <signal.h>
+#include <errno.h>
+#include "user_util.h"
+#include "kern_util.h"
+#include "user.h"
+#include "process.h"
+#include "kern_constants.h"
+#include "os.h"
+
+/* XXX This really needs to be declared and initialized in a kernel file since
+ * it's in <linux/time.h>
+ */
+extern struct timespec wall_to_monotonic;
+
+static void set_interval(int timer_type)
+{
+       int usec = 1000000/hz();
+       struct itimerval interval = ((struct itimerval) { { 0, usec },
+                                                         { 0, usec } });
+
+       if(setitimer(timer_type, &interval, NULL) == -1)
+               panic("setitimer failed - errno = %d\n", errno);
+}
+
+void enable_timer(void)
+{
+       set_interval(ITIMER_VIRTUAL);
+}
+
+void disable_timer(void)
+{
+       struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
+       if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
+          (setitimer(ITIMER_REAL, &disable, NULL) < 0))
+               printk("disnable_timer - setitimer failed, errno = %d\n",
+                      errno);
+       /* If there are signals already queued, after unblocking ignore them */
+       set_handler(SIGALRM, SIG_IGN, 0, -1);
+       set_handler(SIGVTALRM, SIG_IGN, 0, -1);
+}
+
+void switch_timers(int to_real)
+{
+       struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
+       struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
+                                                       { 0, 1000000/hz() }});
+       int old, new;
+
+       if(to_real){
+               old = ITIMER_VIRTUAL;
+               new = ITIMER_REAL;
+       }
+       else {
+               old = ITIMER_REAL;
+               new = ITIMER_VIRTUAL;
+       }
+
+       if((setitimer(old, &disable, NULL) < 0) ||
+          (setitimer(new, &enable, NULL)))
+               printk("switch_timers - setitimer failed, errno = %d\n",
+                      errno);
+}
 
-unsigned long long os_usecs(void)
+void uml_idle_timer(void)
+{
+       if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
+               panic("Couldn't unset SIGVTALRM handler");
+
+       set_handler(SIGALRM, (__sighandler_t) alarm_handler,
+                   SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
+       set_interval(ITIMER_REAL);
+}
+
+extern void ktime_get_ts(struct timespec *ts);
+#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
+
+void time_init(void)
+{
+       struct timespec now;
+
+       if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
+               panic("Couldn't set SIGVTALRM handler");
+       set_interval(ITIMER_VIRTUAL);
+
+       do_posix_clock_monotonic_gettime(&now);
+       wall_to_monotonic.tv_sec = -now.tv_sec;
+       wall_to_monotonic.tv_nsec = -now.tv_nsec;
+}
+
+unsigned long long os_nsecs(void)
 {
        struct timeval tv;
 
        gettimeofday(&tv, NULL);
-       return((unsigned long long) tv.tv_sec * 1000000 + tv.tv_usec);
+       return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
 }
 
-/*
- * Overrides for Emacs so that we follow Linus's tabbing style.
- * Emacs will notice this stuff at the end of the file and automatically
- * adjust the settings for this buffer only.  This must remain at the end
- * of the file.
- * ---------------------------------------------------------------------------
- * Local variables:
- * c-file-style: "linux"
- * End:
- */
+void idle_sleep(int secs)
+{
+       struct timespec ts;
+
+       ts.tv_sec = secs;
+       ts.tv_nsec = 0;
+       nanosleep(&ts, NULL);
+}
+
+/* XXX This partly duplicates init_irq_signals */
+
+void user_time_init(void)
+{
+       set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
+                   SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
+                   SIGALRM, SIGUSR2, -1);
+       set_handler(SIGALRM, (__sighandler_t) alarm_handler,
+                   SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
+                   SIGVTALRM, SIGUSR2, -1);
+       set_interval(ITIMER_VIRTUAL);
+}
index 404bb63a74a9ae24274d94e7231ccb69dc36bd32..919d19f11537172cf6817e3682909497eb73bd8c 100644 (file)
@@ -27,7 +27,6 @@
 #include "sysdep/sigcontext.h"
 #include "irq_user.h"
 #include "ptrace_user.h"
-#include "time_user.h"
 #include "init.h"
 #include "os.h"
 #include "uml-config.h"