root/kernel/time.c

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

DEFINITIONS

This source file includes following definitions.
  1. mktime
  2. time_init
  3. sys_time
  4. sys_stime
  5. do_gettimeoffset
  6. do_gettimeofday
  7. sys_gettimeofday
  8. warp_clock
  9. sys_settimeofday
  10. sys_adjtimex
  11. set_rtc_mmss

   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  * 02 Sep 93    Philip Gladstone
  14  *      Created file with time related functions from sched.c and adjtimex() 
  15  * 08 Oct 93    Torsten Duwe
  16  *      adjtime interface update and CMOS clock write code
  17  * 02 Jul 94    Alan Modra
  18  *      fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  19  */
  20 
  21 #include <linux/config.h>
  22 #include <linux/errno.h>
  23 #include <linux/sched.h>
  24 #include <linux/kernel.h>
  25 #include <linux/param.h>
  26 #include <linux/string.h>
  27 
  28 #include <asm/segment.h>
  29 #include <asm/io.h>
  30 
  31 #include <linux/mc146818rtc.h>
  32 #define RTC_ALWAYS_BCD 1
  33 
  34 #include <linux/timex.h>
  35 
  36 /* converts date to days since 1/1/1970
  37  * assumes year,mon,day in normal date format
  38  * ie. 1/1/1970 => year=1970, mon=1, day=1
  39  *
  40  * For the Julian calendar (which was used in Russia before 1917,
  41  * Britain & colonies before 1752, anywhere else before 1582,
  42  * and is still in use by some communities) leave out the
  43  * -year/100+year/400 terms, and add 10.
  44  *
  45  * This algorithm was first published by Gauss (I think).
  46  */
  47 static inline unsigned long mktime(unsigned int year, unsigned int mon,
     /* [previous][next][first][last][top][bottom][index][help] */
  48         unsigned int day, unsigned int hour,
  49         unsigned int min, unsigned int sec)
  50 {
  51         if (0 >= (int) (mon -= 2)) {    /* 1..12 -> 11,12,1..10 */
  52                 mon += 12;      /* Puts Feb last since it has leap day */
  53                 year -= 1;
  54         }
  55         return (((
  56             (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
  57               year*365 - 719499
  58             )*24 + hour /* now have hours */
  59            )*60 + min /* now have minutes */
  60           )*60 + sec; /* finally seconds */
  61 }
  62 
  63 void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65         unsigned int year, mon, day, hour, min, sec;
  66         int i;
  67 
  68         /* checking for Update-In-Progress could be done more elegantly
  69          * (using the "update finished"-interrupt for example), but that
  70          * would require excessive testing. promise I'll do that when I find
  71          * the time.                    - Torsten
  72          */
  73         /* read RTC exactly on falling edge of update flag */
  74         for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
  75                 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
  76                         break;
  77         for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms*/
  78                 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  79                         break;
  80         do { /* Isn't this overkill ? UIP above should guarantee consistency */
  81                 sec = CMOS_READ(RTC_SECONDS);
  82                 min = CMOS_READ(RTC_MINUTES);
  83                 hour = CMOS_READ(RTC_HOURS);
  84                 day = CMOS_READ(RTC_DAY_OF_MONTH);
  85                 mon = CMOS_READ(RTC_MONTH);
  86                 year = CMOS_READ(RTC_YEAR);
  87         } while (sec != CMOS_READ(RTC_SECONDS));
  88         if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  89           {
  90             BCD_TO_BIN(sec);
  91             BCD_TO_BIN(min);
  92             BCD_TO_BIN(hour);
  93             BCD_TO_BIN(day);
  94             BCD_TO_BIN(mon);
  95             BCD_TO_BIN(year);
  96           }
  97         if ((year += 1900) < 1970)
  98                 year += 100;
  99         xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
 100 }
 101 /* 
 102  * The timezone where the local system is located.  Used as a default by some
 103  * programs who obtain this value by using gettimeofday.
 104  */
 105 struct timezone sys_tz = { 0, 0};
 106 
 107 asmlinkage int sys_time(long * tloc)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         int i, error;
 110 
 111         i = CURRENT_TIME;
 112         if (tloc) {
 113                 error = verify_area(VERIFY_WRITE, tloc, 4);
 114                 if (error)
 115                         return error;
 116                 put_fs_long(i,(unsigned long *)tloc);
 117         }
 118         return i;
 119 }
 120 
 121 asmlinkage int sys_stime(long * tptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123         if (!suser())
 124                 return -EPERM;
 125         cli();
 126         xtime.tv_sec = get_fs_long((unsigned long *) tptr);
 127         xtime.tv_usec = 0;
 128         time_status = TIME_BAD;
 129         time_maxerror = 0x70000000;
 130         time_esterror = 0x70000000;
 131         sti();
 132         return 0;
 133 }
 134 
 135 /* This function must be called with interrupts disabled 
 136  * It was inspired by Steve McCanne's microtime-i386 for BSD.  -- jrs
 137  * 
 138  * However, the pc-audio speaker driver changes the divisor so that
 139  * it gets interrupted rather more often - it loads 64 into the
 140  * counter rather than 11932! This has an adverse impact on
 141  * do_gettimeoffset() -- it stops working! What is also not
 142  * good is that the interval that our timer function gets called
 143  * is no longer 10.0002 msecs, but 9.9767 msec. To get around this
 144  * would require using a different timing source. Maybe someone
 145  * could use the RTC - I know that this can interrupt at frequencies
 146  * ranging from 8192Hz to 2Hz. If I had the energy, I'd somehow fix
 147  * it so that at startup, the timer code in sched.c would select
 148  * using either the RTC or the 8253 timer. The decision would be
 149  * based on whether there was any other device around that needed
 150  * to trample on the 8253. I'd set up the RTC to interrupt at 1024Hz,
 151  * and then do some jiggery to have a version of do_timer that 
 152  * advanced the clock by 1/1024 sec. Every time that reached over 1/100
 153  * of a second, then do all the old code. If the time was kept correct
 154  * then do_gettimeoffset could just return 0 - there is no low order
 155  * divider that can be accessed.
 156  *
 157  * Ideally, you would be able to use the RTC for the speaker driver,
 158  * but it appears that the speaker driver really needs interrupt more
 159  * often than every 120us or so.
 160  *
 161  * Anyway, this needs more thought....          pjsg (28 Aug 93)
 162  * 
 163  * If you are really that interested, you should be reading
 164  * comp.protocols.time.ntp!
 165  */
 166 
 167 #define TICK_SIZE tick
 168 
 169 static inline unsigned long do_gettimeoffset(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171         int count;
 172         unsigned long offset = 0;
 173 
 174         /* timer count may underflow right here */
 175         outb_p(0x00, 0x43);     /* latch the count ASAP */
 176         count = inb_p(0x40);    /* read the latched count */
 177         count |= inb(0x40) << 8;
 178         /* we know probability of underflow is always MUCH less than 1% */
 179         if (count > (LATCH - LATCH/100)) {
 180                 /* check for pending timer interrupt */
 181                 outb_p(0x0a, 0x20);
 182                 if (inb(0x20) & 1)
 183                         offset = TICK_SIZE;
 184         }
 185         count = ((LATCH-1) - count) * TICK_SIZE;
 186         count = (count + LATCH/2) / LATCH;
 187         return offset + count;
 188 }
 189 
 190 /*
 191  * This version of gettimeofday has near microsecond resolution.
 192  */
 193 static inline void do_gettimeofday(struct timeval *tv)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195 #ifdef __i386__
 196         cli();
 197         *tv = xtime;
 198         tv->tv_usec += do_gettimeoffset();
 199         if (tv->tv_usec >= 1000000) {
 200                 tv->tv_usec -= 1000000;
 201                 tv->tv_sec++;
 202         }
 203         sti();
 204 #else /* not __i386__ */
 205         cli();
 206         *tv = xtime;
 207         sti();
 208 #endif /* not __i386__ */
 209 }
 210 
 211 asmlinkage int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
     /* [previous][next][first][last][top][bottom][index][help] */
 212 {
 213         int error;
 214 
 215         if (tv) {
 216                 struct timeval ktv;
 217                 error = verify_area(VERIFY_WRITE, tv, sizeof *tv);
 218                 if (error)
 219                         return error;
 220                 do_gettimeofday(&ktv);
 221                 put_fs_long(ktv.tv_sec, (unsigned long *) &tv->tv_sec);
 222                 put_fs_long(ktv.tv_usec, (unsigned long *) &tv->tv_usec);
 223         }
 224         if (tz) {
 225                 error = verify_area(VERIFY_WRITE, tz, sizeof *tz);
 226                 if (error)
 227                         return error;
 228                 put_fs_long(sys_tz.tz_minuteswest, (unsigned long *) tz);
 229                 put_fs_long(sys_tz.tz_dsttime, ((unsigned long *) tz)+1);
 230         }
 231         return 0;
 232 }
 233 
 234 /*
 235  * Adjust the time obtained from the CMOS to be GMT time instead of
 236  * local time.
 237  * 
 238  * This is ugly, but preferable to the alternatives.  Otherwise we
 239  * would either need to write a program to do it in /etc/rc (and risk
 240  * confusion if the program gets run more than once; it would also be 
 241  * hard to make the program warp the clock precisely n hours)  or
 242  * compile in the timezone information into the kernel.  Bad, bad....
 243  *
 244  * XXX Currently does not adjust for daylight savings time.  May not
 245  * need to do anything, depending on how smart (dumb?) the BIOS
 246  * is.  Blast it all.... the best thing to do not depend on the CMOS
 247  * clock at all, but get the time via NTP or timed if you're on a 
 248  * network....                          - TYT, 1/1/92
 249  */
 250 inline static void warp_clock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 251 {
 252         cli();
 253         xtime.tv_sec += sys_tz.tz_minuteswest * 60;
 254         sti();
 255 }
 256 
 257 /*
 258  * The first time we set the timezone, we will warp the clock so that
 259  * it is ticking GMT time instead of local time.  Presumably, 
 260  * if someone is setting the timezone then we are running in an
 261  * environment where the programs understand about timezones.
 262  * This should be done at boot time in the /etc/rc script, as
 263  * soon as possible, so that the clock can be set right.  Otherwise,
 264  * various programs will get confused when the clock gets warped.
 265  */
 266 asmlinkage int sys_settimeofday(struct timeval *tv, struct timezone *tz)
     /* [previous][next][first][last][top][bottom][index][help] */
 267 {
 268         static int      firsttime = 1;
 269 
 270         if (!suser())
 271                 return -EPERM;
 272         if (tz) {
 273                 sys_tz.tz_minuteswest = get_fs_long((unsigned long *) tz);
 274                 sys_tz.tz_dsttime = get_fs_long(((unsigned long *) tz)+1);
 275                 if (firsttime) {
 276                         firsttime = 0;
 277                         if (!tv)
 278                                 warp_clock();
 279                 }
 280         }
 281         if (tv) {
 282                 int sec, usec;
 283 
 284                 sec = get_fs_long((unsigned long *)tv);
 285                 usec = get_fs_long(((unsigned long *)tv)+1);
 286         
 287                 cli();
 288                 /* This is revolting. We need to set the xtime.tv_usec
 289                  * correctly. However, the value in this location is
 290                  * is value at the last tick.
 291                  * Discover what correction gettimeofday
 292                  * would have done, and then undo it!
 293                  */
 294                 usec -= do_gettimeoffset();
 295 
 296                 if (usec < 0)
 297                 {
 298                         usec += 1000000;
 299                         sec--;
 300                 }
 301                 xtime.tv_sec = sec;
 302                 xtime.tv_usec = usec;
 303                 time_status = TIME_BAD;
 304                 time_maxerror = 0x70000000;
 305                 time_esterror = 0x70000000;
 306                 sti();
 307         }
 308         return 0;
 309 }
 310 
 311 /* adjtimex mainly allows reading (and writing, if superuser) of
 312  * kernel time-keeping variables. used by xntpd.
 313  */
 314 asmlinkage int sys_adjtimex(struct timex *txc_p)
     /* [previous][next][first][last][top][bottom][index][help] */
 315 {
 316         long ltemp, mtemp, save_adjust;
 317         int error;
 318 
 319         /* Local copy of parameter */
 320         struct timex txc;
 321 
 322         error = verify_area(VERIFY_WRITE, txc_p, sizeof(struct timex));
 323         if (error)
 324           return error;
 325 
 326         /* Copy the user data space into the kernel copy
 327          * structure. But bear in mind that the structures
 328          * may change
 329          */
 330         memcpy_fromfs(&txc, txc_p, sizeof(struct timex));
 331 
 332         /* In order to modify anything, you gotta be super-user! */
 333         if (txc.mode && !suser())
 334                 return -EPERM;
 335 
 336         /* Now we validate the data before disabling interrupts
 337          */
 338 
 339         if (txc.mode != ADJ_OFFSET_SINGLESHOT && (txc.mode & ADJ_OFFSET))
 340           /* Microsec field limited to -131000 .. 131000 usecs */
 341           if (txc.offset <= -(1 << (31 - SHIFT_UPDATE))
 342               || txc.offset >= (1 << (31 - SHIFT_UPDATE)))
 343             return -EINVAL;
 344 
 345         /* time_status must be in a fairly small range */
 346         if (txc.mode & ADJ_STATUS)
 347           if (txc.status < TIME_OK || txc.status > TIME_BAD)
 348             return -EINVAL;
 349 
 350         /* if the quartz is off by more than 10% something is VERY wrong ! */
 351         if (txc.mode & ADJ_TICK)
 352           if (txc.tick < 900000/HZ || txc.tick > 1100000/HZ)
 353             return -EINVAL;
 354 
 355         cli();
 356 
 357         /* Save for later - semantics of adjtime is to return old value */
 358         save_adjust = time_adjust;
 359 
 360         /* If there are input parameters, then process them */
 361         if (txc.mode)
 362         {
 363             if (time_status == TIME_BAD)
 364                 time_status = TIME_OK;
 365 
 366             if (txc.mode & ADJ_STATUS)
 367                 time_status = txc.status;
 368 
 369             if (txc.mode & ADJ_FREQUENCY)
 370                 time_freq = txc.frequency << (SHIFT_KF - 16);
 371 
 372             if (txc.mode & ADJ_MAXERROR)
 373                 time_maxerror = txc.maxerror;
 374 
 375             if (txc.mode & ADJ_ESTERROR)
 376                 time_esterror = txc.esterror;
 377 
 378             if (txc.mode & ADJ_TIMECONST)
 379                 time_constant = txc.time_constant;
 380 
 381             if (txc.mode & ADJ_OFFSET)
 382               if (txc.mode == ADJ_OFFSET_SINGLESHOT)
 383                 {
 384                   time_adjust = txc.offset;
 385                 }
 386               else /* XXX should give an error if other bits set */
 387                 {
 388                   time_offset = txc.offset << SHIFT_UPDATE;
 389                   mtemp = xtime.tv_sec - time_reftime;
 390                   time_reftime = xtime.tv_sec;
 391                   if (mtemp > (MAXSEC+2) || mtemp < 0)
 392                     mtemp = 0;
 393 
 394                   if (txc.offset < 0)
 395                     time_freq -= (-txc.offset * mtemp) >>
 396                       (time_constant + time_constant);
 397                   else
 398                     time_freq += (txc.offset * mtemp) >>
 399                       (time_constant + time_constant);
 400 
 401                   ltemp = time_tolerance << SHIFT_KF;
 402 
 403                   if (time_freq > ltemp)
 404                     time_freq = ltemp;
 405                   else if (time_freq < -ltemp)
 406                     time_freq = -ltemp;
 407                 }
 408             if (txc.mode & ADJ_TICK)
 409               tick = txc.tick;
 410 
 411         }
 412         txc.offset         = save_adjust;
 413         txc.frequency      = ((time_freq+1) >> (SHIFT_KF - 16));
 414         txc.maxerror       = time_maxerror;
 415         txc.esterror       = time_esterror;
 416         txc.status         = time_status;
 417         txc.time_constant  = time_constant;
 418         txc.precision      = time_precision;
 419         txc.tolerance      = time_tolerance;
 420         txc.time           = xtime;
 421         txc.tick           = tick;
 422 
 423         sti();
 424 
 425         memcpy_tofs(txc_p, &txc, sizeof(struct timex));
 426         return time_status;
 427 }
 428 
 429 int set_rtc_mmss(unsigned long nowtime)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {
 431   int retval = 0;
 432   int real_seconds, real_minutes, cmos_minutes;
 433   unsigned char save_control, save_freq_select;
 434 
 435   save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
 436   CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
 437 
 438   save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
 439   CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
 440 
 441   cmos_minutes = CMOS_READ(RTC_MINUTES);
 442   if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
 443     BCD_TO_BIN(cmos_minutes);
 444 
 445   /* since we're only adjusting minutes and seconds,
 446    * don't interfere with hour overflow. This avoids
 447    * messing with unknown time zones but requires your
 448    * RTC not to be off by more than 15 minutes
 449    */
 450   real_seconds = nowtime % 60;
 451   real_minutes = nowtime / 60;
 452   if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
 453     real_minutes += 30;         /* correct for half hour time zone */
 454   real_minutes %= 60;
 455 
 456   if (abs(real_minutes - cmos_minutes) < 30)
 457     {
 458       if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
 459         {
 460           BIN_TO_BCD(real_seconds);
 461           BIN_TO_BCD(real_minutes);
 462         }
 463       CMOS_WRITE(real_seconds,RTC_SECONDS);
 464       CMOS_WRITE(real_minutes,RTC_MINUTES);
 465     }
 466   else
 467     retval = -1;
 468 
 469   CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
 470   CMOS_WRITE(save_control, RTC_CONTROL);
 471   return retval;
 472 }

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