Subversion Repositories HelenOS-historic

Rev

Rev 1438 | Rev 1704 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1264 jermar 29
/**
30
 * @file    clock.c
31
 * @brief   High-level clock interrupt handler.
32
 *
33
 * This file contains the clock() function which is the source
34
 * of preemption. It is also responsible for executing expired
35
 * timeouts.
36
 */
37
 
1 jermar 38
#include <time/clock.h>
39
#include <time/timeout.h>
40
#include <arch/types.h>
41
#include <config.h>
42
#include <synch/spinlock.h>
43
#include <synch/waitq.h>
44
#include <func.h>
45
#include <proc/scheduler.h>
46
#include <cpu.h>
47
#include <arch.h>
788 jermar 48
#include <adt/list.h>
1104 jermar 49
#include <atomic.h>
391 jermar 50
#include <proc/thread.h>
1434 palkovsky 51
#include <sysinfo/sysinfo.h>
52
#include <arch/barrier.h>
1 jermar 53
 
1434 palkovsky 54
/* Pointers to public variables with time */
55
struct ptime {
1438 palkovsky 56
    __native seconds1;
1434 palkovsky 57
    __native useconds;
1438 palkovsky 58
    __native seconds2;
1434 palkovsky 59
};
60
struct ptime *public_time;
61
/* Variable holding fragment of second, so that we would update
62
 * seconds correctly
63
 */
64
static __native secfrag = 0;
65
 
66
/** Initialize realtime clock counter
67
 *
68
 * The applications (and sometimes kernel) need to access accurate
69
 * information about realtime data. We allocate 1 page with these
70
 * data and update it periodically.
71
 *
72
 *
73
 */
74
void clock_counter_init(void)
75
{
76
    void *faddr;
77
 
78
    faddr = (void *)PFN2ADDR(frame_alloc(0, FRAME_ATOMIC));
79
    if (!faddr)
80
        panic("Cannot allocate page for clock");
81
 
82
    public_time = (struct ptime *)PA2KA(faddr);
83
 
84
        /* TODO: We would need some arch dependent settings here */
1438 palkovsky 85
    public_time->seconds1 = 0;
86
    public_time->seconds2 = 0;
1434 palkovsky 87
    public_time->useconds = 0;
88
 
89
    sysinfo_set_item_val("clock.faddr", NULL, (__native)faddr);
90
}
91
 
92
 
93
/** Update public counters
94
 *
95
 * Update it only on first processor
96
 * TODO: Do we really need so many write barriers?
97
 */
98
static void clock_update_counters(void)
99
{
100
    if (CPU->id == 0) {
101
        secfrag += 1000000/HZ;
102
        if (secfrag >= 1000000) {
1438 palkovsky 103
            secfrag -= 1000000;
104
            public_time->seconds1++;
1434 palkovsky 105
            write_barrier();
1438 palkovsky 106
            public_time->useconds = secfrag;
107
            write_barrier();
108
            public_time->seconds2 = public_time->seconds1;
1434 palkovsky 109
        } else
110
            public_time->useconds += 1000000/HZ;
111
    }
112
}
113
 
107 decky 114
/** Clock routine
115
 *
116
 * Clock routine executed from clock interrupt handler
413 jermar 117
 * (assuming interrupts_disable()'d). Runs expired timeouts
107 decky 118
 * and preemptive scheduling.
119
 *
1 jermar 120
 */
121
void clock(void)
122
{
123
    link_t *l;
124
    timeout_t *h;
411 jermar 125
    timeout_handler_t f;
1 jermar 126
    void *arg;
1457 jermar 127
    count_t missed_clock_ticks = CPU->missed_clock_ticks;
1431 jermar 128
    int i;
1 jermar 129
 
130
    /*
131
     * To avoid lock ordering problems,
132
     * run all expired timeouts as you visit them.
133
     */
1457 jermar 134
    for (i = 0; i <= missed_clock_ticks; i++) {
1434 palkovsky 135
        clock_update_counters();
1431 jermar 136
        spinlock_lock(&CPU->timeoutlock);
137
        while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
138
            h = list_get_instance(l, timeout_t, link);
139
            spinlock_lock(&h->lock);
140
            if (h->ticks-- != 0) {
141
                spinlock_unlock(&h->lock);
142
                break;
143
            }
144
            list_remove(l);
145
            f = h->handler;
146
            arg = h->arg;
147
            timeout_reinitialize(h);
148
            spinlock_unlock(&h->lock); 
149
            spinlock_unlock(&CPU->timeoutlock);
150
 
151
            f(arg);
152
 
153
            spinlock_lock(&CPU->timeoutlock);
1 jermar 154
        }
15 jermar 155
        spinlock_unlock(&CPU->timeoutlock);
1 jermar 156
    }
1431 jermar 157
    CPU->missed_clock_ticks = 0;
1 jermar 158
 
159
    /*
15 jermar 160
     * Do CPU usage accounting and find out whether to preempt THREAD.
1 jermar 161
     */
162
 
15 jermar 163
    if (THREAD) {
221 jermar 164
        __u64 ticks;
165
 
15 jermar 166
        spinlock_lock(&CPU->lock);
1457 jermar 167
        CPU->needs_relink += 1 + missed_clock_ticks;
15 jermar 168
        spinlock_unlock(&CPU->lock);   
1 jermar 169
 
15 jermar 170
        spinlock_lock(&THREAD->lock);
1457 jermar 171
        if ((ticks = THREAD->ticks)) {
172
            if (ticks >= 1 + missed_clock_ticks)
173
                THREAD->ticks -= 1 + missed_clock_ticks;
174
            else
175
                THREAD->ticks = 0;
176
        }
221 jermar 177
        spinlock_unlock(&THREAD->lock);
178
 
179
        if (!ticks && !PREEMPTION_DISABLED) {
1 jermar 180
            scheduler();
181
        }
182
    }
183
 
184
}