root/kernel/time.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_time
  2. sys_stime
  3. sys_gettimeofday
  4. warp_clock
  5. sys_settimeofday
  6. sys_adjtimex

   1 /*
   2  *  linux/kernel/time.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  *
   6  *  This file contains the interface functions for the various
   7  *  time related system calls: time, stime, gettimeofday, settimeofday,
   8  *                             adjtime
   9  */
  10 /*
  11  * Modification history kernel/time.c
  12  * 
  13  * 1993-09-02    Philip Gladstone
  14  *      Created file with time related functions from sched.c and adjtimex() 
  15  * 1993-10-08    Torsten Duwe
  16  *      adjtime interface update and CMOS clock write code
  17  * 1995-08-13    Torsten Duwe
  18  *      kernel PLL updated to 1994-12-13 specs (rfc-1489)
  19  */
  20 
  21 #include <linux/errno.h>
  22 #include <linux/sched.h>
  23 #include <linux/kernel.h>
  24 #include <linux/param.h>
  25 #include <linux/string.h>
  26 #include <linux/mm.h>
  27 #include <linux/timex.h>
  28 
  29 /* 
  30  * The timezone where the local system is located.  Used as a default by some
  31  * programs who obtain this value by using gettimeofday.
  32  */
  33 struct timezone sys_tz = { 0, 0};
  34 
  35 asmlinkage int sys_time(int * tloc)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37         int i;
  38 
  39         i = CURRENT_TIME;
  40         if (tloc) {
  41                 int error = verify_area(VERIFY_WRITE, tloc, sizeof(*tloc));
  42                 if (error)
  43                         return error;
  44                 put_user(i,tloc);
  45         }
  46         return i;
  47 }
  48 
  49 asmlinkage int sys_stime(int * tptr)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51         int error, value;
  52 
  53         if (!suser())
  54                 return -EPERM;
  55         error = verify_area(VERIFY_READ, tptr, sizeof(*tptr));
  56         if (error)
  57                 return error;
  58         value = get_user(tptr);
  59         cli();
  60         xtime.tv_sec = value;
  61         xtime.tv_usec = 0;
  62         time_state = TIME_BAD;
  63         time_maxerror = 0x70000000;
  64         time_esterror = 0x70000000;
  65         sti();
  66         return 0;
  67 }
  68 
  69 asmlinkage int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
     /* [previous][next][first][last][top][bottom][index][help] */
  70 {
  71         int error;
  72 
  73         if (tv) {
  74                 struct timeval ktv;
  75                 error = verify_area(VERIFY_WRITE, tv, sizeof *tv);
  76                 if (error)
  77                         return error;
  78                 do_gettimeofday(&ktv);
  79                 memcpy_tofs(tv, &ktv, sizeof(ktv));
  80         }
  81         if (tz) {
  82                 error = verify_area(VERIFY_WRITE, tz, sizeof *tz);
  83                 if (error)
  84                         return error;
  85                 memcpy_tofs(tz, &sys_tz, sizeof(sys_tz));
  86         }
  87         return 0;
  88 }
  89 
  90 /*
  91  * Adjust the time obtained from the CMOS to be UTC time instead of
  92  * local time.
  93  * 
  94  * This is ugly, but preferable to the alternatives.  Otherwise we
  95  * would either need to write a program to do it in /etc/rc (and risk
  96  * confusion if the program gets run more than once; it would also be 
  97  * hard to make the program warp the clock precisely n hours)  or
  98  * compile in the timezone information into the kernel.  Bad, bad....
  99  *
 100  *                                              - TYT, 1992-01-01
 101  *
 102  * The best thing to do is to keep the CMOS clock in universal time (UTC)
 103  * as real UNIX machines always do it. This avoids all headaches about
 104  * daylight saving times and warping kernel clocks.
 105  */
 106 inline static void warp_clock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108         cli();
 109         xtime.tv_sec += sys_tz.tz_minuteswest * 60;
 110         sti();
 111 }
 112 
 113 /*
 114  * In case for some reason the CMOS clock has not already been running
 115  * in UTC, but in some local time: The first time we set the timezone,
 116  * we will warp the clock so that it is ticking UTC time instead of
 117  * local time. Presumably, if someone is setting the timezone then we
 118  * are running in an environment where the programs understand about
 119  * timezones. This should be done at boot time in the /etc/rc script,
 120  * as soon as possible, so that the clock can be set right. Otherwise,
 121  * various programs will get confused when the clock gets warped.
 122  */
 123 asmlinkage int sys_settimeofday(struct timeval *tv, struct timezone *tz)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         static int      firsttime = 1;
 126         struct timeval  new_tv;
 127         struct timezone new_tz;
 128 
 129         if (!suser())
 130                 return -EPERM;
 131         if (tv) {
 132                 int error = verify_area(VERIFY_READ, tv, sizeof(*tv));
 133                 if (error)
 134                         return error;
 135                 memcpy_fromfs(&new_tv, tv, sizeof(*tv));
 136         }
 137         if (tz) {
 138                 int error = verify_area(VERIFY_READ, tz, sizeof(*tz));
 139                 if (error)
 140                         return error;
 141                 memcpy_fromfs(&new_tz, tz, sizeof(*tz));
 142         }
 143         if (tz) {
 144                 sys_tz = new_tz;
 145                 if (firsttime) {
 146                         firsttime = 0;
 147                         if (!tv)
 148                                 warp_clock();
 149                 }
 150         }
 151         if (tv)
 152                 do_settimeofday(&new_tv);
 153         return 0;
 154 }
 155 
 156 long pps_offset = 0;            /* pps time offset (us) */
 157 long pps_jitter = MAXTIME;      /* time dispersion (jitter) (us) */
 158 
 159 long pps_freq = 0;              /* frequency offset (scaled ppm) */
 160 long pps_stabil = MAXFREQ;      /* frequency dispersion (scaled ppm) */
 161 
 162 long pps_valid = PPS_VALID;     /* pps signal watchdog counter */
 163 
 164 int pps_shift = PPS_SHIFT;      /* interval duration (s) (shift) */
 165 
 166 long pps_jitcnt = 0;            /* jitter limit exceeded */
 167 long pps_calcnt = 0;            /* calibration intervals */
 168 long pps_errcnt = 0;            /* calibration errors */
 169 long pps_stbcnt = 0;            /* stability limit exceeded */
 170 
 171 /* hook for a loadable hardpps kernel module */
 172 void (*hardpps_ptr)(struct timeval *) = (void (*)(struct timeval *))0;
 173 
 174 /* adjtimex mainly allows reading (and writing, if superuser) of
 175  * kernel time-keeping variables. used by xntpd.
 176  */
 177 asmlinkage int sys_adjtimex(struct timex *txc_p)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179         long ltemp, mtemp, save_adjust;
 180         int error;
 181 
 182         /* Local copy of parameter */
 183         struct timex txc;
 184 
 185         error = verify_area(VERIFY_WRITE, txc_p, sizeof(struct timex));
 186         if (error)
 187           return error;
 188 
 189         /* Copy the user data space into the kernel copy
 190          * structure. But bear in mind that the structures
 191          * may change
 192          */
 193         memcpy_fromfs(&txc, txc_p, sizeof(struct timex));
 194 
 195         /* In order to modify anything, you gotta be super-user! */
 196         if (txc.modes && !suser())
 197                 return -EPERM;
 198 
 199         /* Now we validate the data before disabling interrupts
 200          */
 201 
 202         if (txc.modes != ADJ_OFFSET_SINGLESHOT && (txc.modes & ADJ_OFFSET))
 203           /* adjustment Offset limited to +- .512 seconds */
 204           if (txc.offset <= - MAXPHASE || txc.offset >= MAXPHASE )
 205             return -EINVAL;
 206 
 207         /* if the quartz is off by more than 10% something is VERY wrong ! */
 208         if (txc.modes & ADJ_TICK)
 209           if (txc.tick < 900000/HZ || txc.tick > 1100000/HZ)
 210             return -EINVAL;
 211 
 212         cli();
 213 
 214         /* Save for later - semantics of adjtime is to return old value */
 215         save_adjust = time_adjust;
 216 
 217         /* If there are input parameters, then process them */
 218         if (txc.modes)
 219         {
 220             if (time_state == TIME_BAD)
 221                 time_state = TIME_OK;
 222 
 223             if (txc.modes & ADJ_STATUS)
 224                 time_status = txc.status;
 225 
 226             if (txc.modes & ADJ_FREQUENCY)
 227                 time_freq = txc.freq;
 228 
 229             if (txc.modes & ADJ_MAXERROR)
 230                 time_maxerror = txc.maxerror;
 231 
 232             if (txc.modes & ADJ_ESTERROR)
 233                 time_esterror = txc.esterror;
 234 
 235             if (txc.modes & ADJ_TIMECONST)
 236                 time_constant = txc.constant;
 237 
 238             if (txc.modes & ADJ_OFFSET)
 239               if ((txc.modes == ADJ_OFFSET_SINGLESHOT)
 240                   || !(time_status & STA_PLL))
 241                 {
 242                   time_adjust = txc.offset;
 243                 }
 244               else if ((time_status & STA_PLL)||(time_status & STA_PPSTIME))
 245                 {
 246                   ltemp = (time_status & STA_PPSTIME &&
 247                            time_status & STA_PPSSIGNAL) ?
 248                     pps_offset : txc.offset;
 249 
 250                   /*
 251                    * Scale the phase adjustment and
 252                    * clamp to the operating range.
 253                    */
 254                   if (ltemp > MAXPHASE)
 255                     time_offset = MAXPHASE << SHIFT_UPDATE;
 256                   else if (ltemp < -MAXPHASE)
 257                     time_offset = -(MAXPHASE << SHIFT_UPDATE);
 258                   else
 259                     time_offset = ltemp << SHIFT_UPDATE;
 260 
 261                   /*
 262                    * Select whether the frequency is to be controlled and in which
 263                    * mode (PLL or FLL). Clamp to the operating range. Ugly
 264                    * multiply/divide should be replaced someday.
 265                    */
 266 
 267                   if (time_status & STA_FREQHOLD || time_reftime == 0)
 268                     time_reftime = xtime.tv_sec;
 269                   mtemp = xtime.tv_sec - time_reftime;
 270                   time_reftime = xtime.tv_sec;
 271                   if (time_status & STA_FLL)
 272                     {
 273                       if (mtemp >= MINSEC)
 274                         {
 275                           ltemp = ((time_offset / mtemp) << (SHIFT_USEC -
 276                                                              SHIFT_UPDATE));
 277                           if (ltemp < 0)
 278                             time_freq -= -ltemp >> SHIFT_KH;
 279                           else
 280                             time_freq += ltemp >> SHIFT_KH;
 281                         }
 282                     } 
 283                   else 
 284                     {
 285                       if (mtemp < MAXSEC)
 286                         {
 287                           ltemp *= mtemp;
 288                           if (ltemp < 0)
 289                             time_freq -= -ltemp >> (time_constant +
 290                                                     time_constant + SHIFT_KF -
 291                                                     SHIFT_USEC);
 292                           else
 293                             time_freq += ltemp >> (time_constant +
 294                                                    time_constant + SHIFT_KF -
 295                                                    SHIFT_USEC);
 296                         }
 297                     }
 298                   if (time_freq > time_tolerance)
 299                     time_freq = time_tolerance;
 300                   else if (time_freq < -time_tolerance)
 301                     time_freq = -time_tolerance;
 302                 }
 303             if (txc.modes & ADJ_TICK)
 304               tick = txc.tick;
 305 
 306         }
 307         txc.offset         = save_adjust;
 308         txc.freq           = time_freq;
 309         txc.maxerror       = time_maxerror;
 310         txc.esterror       = time_esterror;
 311         txc.status         = time_status;
 312         txc.constant       = time_constant;
 313         txc.precision      = time_precision;
 314         txc.tolerance      = time_tolerance;
 315         txc.time           = xtime;
 316         txc.tick           = tick;
 317         txc.ppsfreq        = pps_freq;
 318         txc.jitter         = pps_jitter;
 319         txc.shift          = pps_shift;
 320         txc.stabil         = pps_stabil;
 321         txc.jitcnt         = pps_jitcnt;
 322         txc.calcnt         = pps_calcnt;
 323         txc.errcnt         = pps_errcnt;
 324         txc.stbcnt         = pps_stbcnt;
 325 
 326         sti();
 327 
 328         memcpy_tofs(txc_p, &txc, sizeof(struct timex));
 329         return time_state;
 330 }

/* [previous][next][first][last][top][bottom][index][help] */