Subversion Repositories HelenOS-historic

Rev

Rev 1457 | Rev 1704 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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