1 /*
2 * linux/kernel/timer.c
3 *
4 * Kernel internal timers, kernel timekeeping, basic process system calls
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 */
17
18 #include <linux/config.h>
19 #include <linux/mm.h>
20 #include <linux/timex.h>
21 #include <linux/delay.h>
22 #include <linux/smp_lock.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel_stat.h>
25
26 #include <asm/uaccess.h>
27
28 /*
29 * Timekeeping variables
30 */
31
32 long tick = (1000000 + HZ/2) / HZ; /* timer interrupt period */
33
34 /* The current time */
35 volatile struct timeval xtime __attribute__ ((aligned (16)));
36
37 /* Don't completely fail for HZ > 500. */
38 int tickadj = 500/HZ ? : 1; /* microsecs */
39
40 DECLARE_TASK_QUEUE(tq_timer);
41 DECLARE_TASK_QUEUE(tq_immediate);
42
43 /*
44 * phase-lock loop variables
45 */
46 /* TIME_ERROR prevents overwriting the CMOS clock */
47 int time_state = TIME_OK; /* clock synchronization status */
48 int time_status = STA_UNSYNC; /* clock status bits */
49 long time_offset; /* time adjustment (us) */
50 long time_constant = 2; /* pll time constant */
51 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
52 long time_precision = 1; /* clock precision (us) */
53 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
54 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
55 long time_phase; /* phase offset (scaled us) */
56 long time_freq = ((1000000 + HZ/2) % HZ - HZ/2) << SHIFT_USEC;
57 /* frequency offset (scaled ppm)*/
58 long time_adj; /* tick adjust (scaled 1 / HZ) */
59 long time_reftime; /* time at last adjustment (s) */
60
61 long time_adjust;
62 long time_adjust_step;
63
64 unsigned long event;
65
66 extern int do_setitimer(int, struct itimerval *, struct itimerval *);
67
68 unsigned long volatile jiffies;
69
70 unsigned int * prof_buffer;
71 unsigned long prof_len;
72 unsigned long prof_shift;
73
74 /*
75 * Event timer code
76 */
77 #define TVN_BITS 6
78 #define TVR_BITS 8
79 #define TVN_SIZE (1 << TVN_BITS)
80 #define TVR_SIZE (1 << TVR_BITS)
81 #define TVN_MASK (TVN_SIZE - 1)
82 #define TVR_MASK (TVR_SIZE - 1)
83
84 struct timer_vec {
85 int index;
86 struct list_head vec[TVN_SIZE];
87 };
88
89 struct timer_vec_root {
90 int index;
91 struct list_head vec[TVR_SIZE];
92 };
93
94 static struct timer_vec tv5;
95 static struct timer_vec tv4;
96 static struct timer_vec tv3;
97 static struct timer_vec tv2;
98 static struct timer_vec_root tv1;
99
100 static struct timer_vec * const tvecs[] = {
101 (struct timer_vec *)&tv1, &tv2, &tv3, &tv4, &tv5
102 };
103
104 #define NOOF_TVECS (sizeof(tvecs) / sizeof(tvecs[0]))
105
106 void init_timervecs (void)
107 {
108 int i;
109
110 for (i = 0; i < TVN_SIZE; i++) {
111 INIT_LIST_HEAD(tv5.vec + i);
112 INIT_LIST_HEAD(tv4.vec + i);
113 INIT_LIST_HEAD(tv3.vec + i);
114 INIT_LIST_HEAD(tv2.vec + i);
115 }
116 for (i = 0; i < TVR_SIZE; i++)
117 INIT_LIST_HEAD(tv1.vec + i);
118 }
119
120 static unsigned long timer_jiffies;
121
122 static inline void internal_add_timer(struct timer_list *timer)
123 {
124 /*
125 * must be cli-ed when calling this
126 */
127 unsigned long expires = timer->expires;
128 unsigned long idx = expires - timer_jiffies;
129 struct list_head * vec;
130
131 if (idx < TVR_SIZE) {
132 int i = expires & TVR_MASK;
133 vec = tv1.vec + i;
134 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
135 int i = (expires >> TVR_BITS) & TVN_MASK;
136 vec = tv2.vec + i;
137 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
138 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
139 vec = tv3.vec + i;
140 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
141 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
142 vec = tv4.vec + i;
143 } else if ((signed long) idx < 0) {
144 /* can happen if you add a timer with expires == jiffies,
145 * or you set a timer to go off in the past
146 */
147 vec = tv1.vec + tv1.index;
148 } else if (idx <= 0xffffffffUL) {
149 int i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
150 vec = tv5.vec + i;
151 } else {
152 /* Can only get here on architectures with 64-bit jiffies */
153 INIT_LIST_HEAD(&timer->list);
154 return;
155 }
156 /*
157 * Timers are FIFO!
158 */
159 list_add(&timer->list, vec->prev);
160 }
161
162 /* Initialize both explicitly - let's try to have them in the same cache line */
163 spinlock_t timerlist_lock = SPIN_LOCK_UNLOCKED;
164
165 #ifdef CONFIG_SMP
166 volatile struct timer_list * volatile running_timer;
167 #define timer_enter(t) do { running_timer = t; mb(); } while (0)
168 #define timer_exit() do { running_timer = NULL; } while (0)
169 #define timer_is_running(t) (running_timer == t)
170 #define timer_synchronize(t) while (timer_is_running(t)) barrier()
171 #else
172 #define timer_enter(t) do { } while (0)
173 #define timer_exit() do { } while (0)
174 #endif
175
176 void add_timer(struct timer_list *timer)
177 {
178 unsigned long flags;
179
180 spin_lock_irqsave(&timerlist_lock, flags);
181 if (timer_pending(timer))
182 goto bug;
183 internal_add_timer(timer);
184 spin_unlock_irqrestore(&timerlist_lock, flags);
185 return;
186 bug:
187 spin_unlock_irqrestore(&timerlist_lock, flags);
188 printk("bug: kernel timer added twice at %p.\n",
189 __builtin_return_address(0));
190 }
191
192 static inline int detach_timer (struct timer_list *timer)
193 {
194 if (!timer_pending(timer))
195 return 0;
196 list_del(&timer->list);
197 return 1;
198 }
199
200 int mod_timer(struct timer_list *timer, unsigned long expires)
201 {
202 int ret;
203 unsigned long flags;
204
205 spin_lock_irqsave(&timerlist_lock, flags);
206 timer->expires = expires;
207 ret = detach_timer(timer);
208 internal_add_timer(timer);
209 spin_unlock_irqrestore(&timerlist_lock, flags);
210 return ret;
211 }
212
213 int del_timer(struct timer_list * timer)
214 {
215 int ret;
216 unsigned long flags;
217
218 spin_lock_irqsave(&timerlist_lock, flags);
219 ret = detach_timer(timer);
220 timer->list.next = timer->list.prev = NULL;
221 spin_unlock_irqrestore(&timerlist_lock, flags);
222 return ret;
223 }
224
225 #ifdef CONFIG_SMP
226 void sync_timers(void)
227 {
228 spin_unlock_wait(&global_bh_lock);
229 }
230
231 /*
232 * SMP specific function to delete periodic timer.
233 * Caller must disable by some means restarting the timer
234 * for new. Upon exit the timer is not queued and handler is not running
235 * on any CPU. It returns number of times, which timer was deleted
236 * (for reference counting).
237 */
238
239 int del_timer_sync(struct timer_list * timer)
240 {
241 int ret = 0;
242
243 for (;;) {
244 unsigned long flags;
245 int running;
246
247 spin_lock_irqsave(&timerlist_lock, flags);
248 ret += detach_timer(timer);
249 timer->list.next = timer->list.prev = 0;
250 running = timer_is_running(timer);
251 spin_unlock_irqrestore(&timerlist_lock, flags);
252
253 if (!running)
254 break;
255
256 timer_synchronize(timer);
257 }
258
259 return ret;
260 }
261 #endif
262
263
264 static inline void cascade_timers(struct timer_vec *tv)
265 {
266 /* cascade all the timers from tv up one level */
267 struct list_head *head, *curr, *next;
268
269 head = tv->vec + tv->index;
270 curr = head->next;
271 /*
272 * We are removing _all_ timers from the list, so we don't have to
273 * detach them individually, just clear the list afterwards.
274 */
275 while (curr != head) {
276 struct timer_list *tmp;
277
278 tmp = list_entry(curr, struct timer_list, list);
279 next = curr->next;
280 list_del(curr); // not needed
281 internal_add_timer(tmp);
282 curr = next;
283 }
284 INIT_LIST_HEAD(head);
285 tv->index = (tv->index + 1) & TVN_MASK;
286 }
287
288 static inline void run_timer_list(void)
289 {
290 spin_lock_irq(&timerlist_lock);
291 while ((long)(jiffies - timer_jiffies) >= 0) {
292 struct list_head *head, *curr;
293 if (!tv1.index) {
294 int n = 1;
295 do {
296 cascade_timers(tvecs[n]);
297 } while (tvecs[n]->index == 1 && ++n < NOOF_TVECS);
298 }
299 repeat:
300 head = tv1.vec + tv1.index;
301 curr = head->next;
302 if (curr != head) {
303 struct timer_list *timer;
304 void (*fn)(unsigned long);
305 unsigned long data;
306
307 timer = list_entry(curr, struct timer_list, list);
308 fn = timer->function;
309 data= timer->data;
310
311 detach_timer(timer);
312 timer->list.next = timer->list.prev = NULL;
313 timer_enter(timer);
314 spin_unlock_irq(&timerlist_lock);
315 fn(data);
316 spin_lock_irq(&timerlist_lock);
317 timer_exit();
318 goto repeat;
319 }
320 ++timer_jiffies;
321 tv1.index = (tv1.index + 1) & TVR_MASK;
322 }
323 spin_unlock_irq(&timerlist_lock);
324 }
325
326 spinlock_t tqueue_lock = SPIN_LOCK_UNLOCKED;
327
328 void tqueue_bh(void)
329 {
330 run_task_queue(&tq_timer);
331 }
332
333 void immediate_bh(void)
334 {
335 run_task_queue(&tq_immediate);
336 }
337
338 /*
339 * this routine handles the overflow of the microsecond field
340 *
341 * The tricky bits of code to handle the accurate clock support
342 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
343 * They were originally developed for SUN and DEC kernels.
344 * All the kudos should go to Dave for this stuff.
345 *
346 */
347 static void second_overflow(void)
348 {
349 long ltemp;
350
351 /* Bump the maxerror field */
352 time_maxerror += time_tolerance >> SHIFT_USEC;
353 if ( time_maxerror > NTP_PHASE_LIMIT ) {
354 time_maxerror = NTP_PHASE_LIMIT;
355 time_status |= STA_UNSYNC;
356 }
357
358 /*
359 * Leap second processing. If in leap-insert state at
360 * the end of the day, the system clock is set back one
361 * second; if in leap-delete state, the system clock is
362 * set ahead one second. The microtime() routine or
363 * external clock driver will insure that reported time
364 * is always monotonic. The ugly divides should be
365 * replaced.
366 */
367 switch (time_state) {
368
369 case TIME_OK:
370 if (time_status & STA_INS)
371 time_state = TIME_INS;
372 else if (time_status & STA_DEL)
373 time_state = TIME_DEL;
374 break;
375
376 case TIME_INS:
377 if (xtime.tv_sec % 86400 == 0) {
378 xtime.tv_sec--;
379 time_state = TIME_OOP;
380 printk(KERN_NOTICE "Clock: inserting leap second 23:59:60 UTC\n");
381 }
382 break;
383
384 case TIME_DEL:
385 if ((xtime.tv_sec + 1) % 86400 == 0) {
386 xtime.tv_sec++;
387 time_state = TIME_WAIT;
388 printk(KERN_NOTICE "Clock: deleting leap second 23:59:59 UTC\n");
389 }
390 break;
391
392 case TIME_OOP:
393 time_state = TIME_WAIT;
394 break;
395
396 case TIME_WAIT:
397 if (!(time_status & (STA_INS | STA_DEL)))
398 time_state = TIME_OK;
399 }
400
401 /*
402 * Compute the phase adjustment for the next second. In
403 * PLL mode, the offset is reduced by a fixed factor
404 * times the time constant. In FLL mode the offset is
405 * used directly. In either mode, the maximum phase
406 * adjustment for each second is clamped so as to spread
407 * the adjustment over not more than the number of
408 * seconds between updates.
409 */
410 if (time_offset < 0) {
411 ltemp = -time_offset;
412 if (!(time_status & STA_FLL))
413 ltemp >>= SHIFT_KG + time_constant;
414 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
415 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
416 time_offset += ltemp;
417 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
418 } else {
419 ltemp = time_offset;
420 if (!(time_status & STA_FLL))
421 ltemp >>= SHIFT_KG + time_constant;
422 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
423 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
424 time_offset -= ltemp;
425 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
426 }
427
428 /*
429 * Compute the frequency estimate and additional phase
430 * adjustment due to frequency error for the next
431 * second. When the PPS signal is engaged, gnaw on the
432 * watchdog counter and update the frequency computed by
433 * the pll and the PPS signal.
434 */
435 pps_valid++;
436 if (pps_valid == PPS_VALID) { /* PPS signal lost */
437 pps_jitter = MAXTIME;
438 pps_stabil = MAXFREQ;
439 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
440 STA_PPSWANDER | STA_PPSERROR);
441 }
442 ltemp = time_freq + pps_freq;
443 if (ltemp < 0)
444 time_adj -= -ltemp >>
445 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
446 else
447 time_adj += ltemp >>
448 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
449
450 #if HZ == 100
451 /* Compensate for (HZ==100) != (1 << SHIFT_HZ).
452 * Add 25% and 3.125% to get 128.125; => only 0.125% error (p. 14)
453 */
454 if (time_adj < 0)
455 time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
456 else
457 time_adj += (time_adj >> 2) + (time_adj >> 5);
458 #endif
459 }
460
461 /* in the NTP reference this is called "hardclock()" */
462 static void update_wall_time_one_tick(void)
463 {
464 if ( (time_adjust_step = time_adjust) != 0 ) {
465 /* We are doing an adjtime thing.
466 *
467 * Prepare time_adjust_step to be within bounds.
468 * Note that a positive time_adjust means we want the clock
469 * to run faster.
470 *
471 * Limit the amount of the step to be in the range
472 * -tickadj .. +tickadj
473 */
474 if (time_adjust > tickadj)
475 time_adjust_step = tickadj;
476 else if (time_adjust < -tickadj)
477 time_adjust_step = -tickadj;
478
479 /* Reduce by this step the amount of time left */
480 time_adjust -= time_adjust_step;
481 }
482 xtime.tv_usec += tick + time_adjust_step;
483 /*
484 * Advance the phase, once it gets to one microsecond, then
485 * advance the tick more.
486 */
487 time_phase += time_adj;
488 if (time_phase <= -FINEUSEC) {
489 long ltemp = -time_phase >> SHIFT_SCALE;
490 time_phase += ltemp << SHIFT_SCALE;
491 xtime.tv_usec -= ltemp;
492 }
493 else if (time_phase >= FINEUSEC) {
494 long ltemp = time_phase >> SHIFT_SCALE;
495 time_phase -= ltemp << SHIFT_SCALE;
496 xtime.tv_usec += ltemp;
497 }
498 }
499
500 /*
501 * Using a loop looks inefficient, but "ticks" is
502 * usually just one (we shouldn't be losing ticks,
503 * we're doing this this way mainly for interrupt
504 * latency reasons, not because we think we'll
505 * have lots of lost timer ticks
506 */
507 static void update_wall_time(unsigned long ticks)
508 {
509 do {
510 ticks--;
511 update_wall_time_one_tick();
512 } while (ticks);
513
514 if (xtime.tv_usec >= 1000000) {
515 xtime.tv_usec -= 1000000;
516 xtime.tv_sec++;
517 second_overflow();
518 }
519 }
520
521 static inline void do_process_times(struct task_struct *p,
522 unsigned long user, unsigned long system)
523 {
524 unsigned long psecs;
525
526 psecs = (p->times.tms_utime += user);
527 psecs += (p->times.tms_stime += system);
528 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
529 /* Send SIGXCPU every second.. */
530 if (!(psecs % HZ))
531 send_sig(SIGXCPU, p, 1);
532 /* and SIGKILL when we go over max.. */
533 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_max)
534 send_sig(SIGKILL, p, 1);
535 }
536 }
537
538 static inline void do_it_virt(struct task_struct * p, unsigned long ticks)
539 {
540 unsigned long it_virt = p->it_virt_value;
541
542 if (it_virt) {
543 it_virt -= ticks;
544 if (!it_virt) {
545 it_virt = p->it_virt_incr;
546 send_sig(SIGVTALRM, p, 1);
547 }
548 p->it_virt_value = it_virt;
549 }
550 }
551
552 static inline void do_it_prof(struct task_struct *p)
553 {
554 unsigned long it_prof = p->it_prof_value;
555
556 if (it_prof) {
557 if (--it_prof == 0) {
558 it_prof = p->it_prof_incr;
559 send_sig(SIGPROF, p, 1);
560 }
561 p->it_prof_value = it_prof;
562 }
563 }
564
565 void update_one_process(struct task_struct *p, unsigned long user,
566 unsigned long system, int cpu)
567 {
568 p->per_cpu_utime[cpu] += user;
569 p->per_cpu_stime[cpu] += system;
570 do_process_times(p, user, system);
571 do_it_virt(p, user);
572 do_it_prof(p);
573 }
574
575 /*
576 * Called from the timer interrupt handler to charge one tick to the current
577 * process. user_tick is 1 if the tick is user time, 0 for system.
578 */
579 void update_process_times(int user_tick)
580 {
581 struct task_struct *p = current;
582 int cpu = smp_processor_id(), system = user_tick ^ 1;
583
584 update_one_process(p, user_tick, system, cpu);
585 if (p->pid) {
586 if (--p->counter <= 0) {
587 p->counter = 0;
588 p->need_resched = 1;
589 }
590 if (p->nice > 0)
591 kstat.per_cpu_nice[cpu] += user_tick;
592 else
593 kstat.per_cpu_user[cpu] += user_tick;
594 kstat.per_cpu_system[cpu] += system;
595 } else if (local_bh_count(cpu) || local_irq_count(cpu) > 1)
596 kstat.per_cpu_system[cpu] += system;
597 }
598
599 /*
600 * Nr of active tasks - counted in fixed-point numbers
601 */
602 static unsigned long count_active_tasks(void)
603 {
604 struct task_struct *p;
605 unsigned long nr = 0;
606
607 read_lock(&tasklist_lock);
608 for_each_task(p) {
609 if ((p->state == TASK_RUNNING ||
610 (p->state & TASK_UNINTERRUPTIBLE)))
611 nr += FIXED_1;
612 }
613 read_unlock(&tasklist_lock);
614 return nr;
615 }
616
617 /*
618 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
619 * imply that avenrun[] is the standard name for this kind of thing.
620 * Nothing else seems to be standardized: the fractional size etc
621 * all seem to differ on different machines.
622 */
623 unsigned long avenrun[3];
624
625 static inline void calc_load(unsigned long ticks)
626 {
627 unsigned long active_tasks; /* fixed-point */
628 static int count = LOAD_FREQ;
629
630 count -= ticks;
631 if (count < 0) {
632 count += LOAD_FREQ;
633 active_tasks = count_active_tasks();
634 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
635 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
636 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
637 }
638 }
639
640 /* jiffies at the most recent update of wall time */
641 unsigned long wall_jiffies;
642
643 /*
644 * This spinlock protect us from races in SMP while playing with xtime. -arca
645 */
646 rwlock_t xtime_lock = RW_LOCK_UNLOCKED;
647
648 static inline void update_times(void)
649 {
650 unsigned long ticks;
651
652 /*
653 * update_times() is run from the raw timer_bh handler so we
654 * just know that the irqs are locally enabled and so we don't
655 * need to save/restore the flags of the local CPU here. -arca
656 */
657 write_lock_irq(&xtime_lock);
658
659 ticks = jiffies - wall_jiffies;
660 if (ticks) {
661 wall_jiffies += ticks;
662 update_wall_time(ticks);
663 }
664 write_unlock_irq(&xtime_lock);
665 calc_load(ticks);
666 }
667
668 void timer_bh(void)
669 {
670 update_times();
671 run_timer_list();
672 }
673
674 void do_timer(struct pt_regs *regs)
675 {
676 (*(unsigned long *)&jiffies)++;
677 #ifndef CONFIG_SMP
678 /* SMP process accounting uses the local APIC timer */
679
680 update_process_times(user_mode(regs));
681 #endif
682 mark_bh(TIMER_BH);
683 if (TQ_ACTIVE(tq_timer))
684 mark_bh(TQUEUE_BH);
685 }
686
687 #if !defined(__alpha__) && !defined(__ia64__)
688
689 /*
690 * For backwards compatibility? This can be done in libc so Alpha
691 * and all newer ports shouldn't need it.
692 */
693 asmlinkage unsigned long sys_alarm(unsigned int seconds)
694 {
695 struct itimerval it_new, it_old;
696 unsigned int oldalarm;
697
698 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
699 it_new.it_value.tv_sec = seconds;
700 it_new.it_value.tv_usec = 0;
701 do_setitimer(ITIMER_REAL, &it_new, &it_old);
702 oldalarm = it_old.it_value.tv_sec;
703 /* ehhh.. We can't return 0 if we have an alarm pending.. */
704 /* And we'd better return too much than too little anyway */
705 if (it_old.it_value.tv_usec)
706 oldalarm++;
707 return oldalarm;
708 }
709
710 #endif
711
712 #ifndef __alpha__
713
714 /*
715 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
716 * should be moved into arch/i386 instead?
717 */
718
719 asmlinkage long sys_getpid(void)
720 {
721 /* This is SMP safe - current->pid doesn't change */
722 return current->tgid;
723 }
724
725 /*
726 * This is not strictly SMP safe: p_opptr could change
727 * from under us. However, rather than getting any lock
728 * we can use an optimistic algorithm: get the parent
729 * pid, and go back and check that the parent is still
730 * the same. If it has changed (which is extremely unlikely
731 * indeed), we just try again..
732 *
733 * NOTE! This depends on the fact that even if we _do_
734 * get an old value of "parent", we can happily dereference
735 * the pointer: we just can't necessarily trust the result
736 * until we know that the parent pointer is valid.
737 *
738 * The "mb()" macro is a memory barrier - a synchronizing
739 * event. It also makes sure that gcc doesn't optimize
740 * away the necessary memory references.. The barrier doesn't
741 * have to have all that strong semantics: on x86 we don't
742 * really require a synchronizing instruction, for example.
743 * The barrier is more important for code generation than
744 * for any real memory ordering semantics (even if there is
745 * a small window for a race, using the old pointer is
746 * harmless for a while).
747 */
748 asmlinkage long sys_getppid(void)
749 {
750 int pid;
751 struct task_struct * me = current;
752 struct task_struct * parent;
753
754 parent = me->p_opptr;
755 for (;;) {
756 pid = parent->pid;
757 #if CONFIG_SMP
758 {
759 struct task_struct *old = parent;
760 mb();
761 parent = me->p_opptr;
762 if (old != parent)
763 continue;
764 }
765 #endif
766 break;
767 }
768 return pid;
769 }
770
771 asmlinkage long sys_getuid(void)
772 {
773 /* Only we change this so SMP safe */
774 return current->uid;
775 }
776
777 asmlinkage long sys_geteuid(void)
778 {
779 /* Only we change this so SMP safe */
780 return current->euid;
781 }
782
783 asmlinkage long sys_getgid(void)
784 {
785 /* Only we change this so SMP safe */
786 return current->gid;
787 }
788
789 asmlinkage long sys_getegid(void)
790 {
791 /* Only we change this so SMP safe */
792 return current->egid;
793 }
794
795 #endif
796
797 asmlinkage long sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp)
798 {
799 struct timespec t;
800 unsigned long expire;
801
802 if(copy_from_user(&t, rqtp, sizeof(struct timespec)))
803 return -EFAULT;
804
805 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0 || t.tv_sec < 0)
806 return -EINVAL;
807
808
809 if (t.tv_sec == 0 && t.tv_nsec <= 2000000L &&
810 current->policy != SCHED_OTHER)
811 {
812 /*
813 * Short delay requests up to 2 ms will be handled with
814 * high precision by a busy wait for all real-time processes.
815 *
816 * Its important on SMP not to do this holding locks.
817 */
818 udelay((t.tv_nsec + 999) / 1000);
819 return 0;
820 }
821
822 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
823
824 current->state = TASK_INTERRUPTIBLE;
825 expire = schedule_timeout(expire);
826
827 if (expire) {
828 if (rmtp) {
829 jiffies_to_timespec(expire, &t);
830 if (copy_to_user(rmtp, &t, sizeof(struct timespec)))
831 return -EFAULT;
832 }
833 return -EINTR;
834 }
835 return 0;
836 }
837
838
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.