Subversion Repositories HelenOS-historic

Rev

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

Rev 1704 Rev 1705
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 generic   
29
 /** @addtogroup generic   
36
 * @{
30
 * @{
37
 */
31
 */
38
 
32
 
39
/**
33
/**
40
 * @file
34
 * @file
41
 * @brief   High-level clock interrupt handler.
35
 * @brief   High-level clock interrupt handler.
42
 *
36
 *
43
 * This file contains the clock() function which is the source
37
 * This file contains the clock() function which is the source
44
 * of preemption. It is also responsible for executing expired
38
 * of preemption. It is also responsible for executing expired
45
 * timeouts.
39
 * timeouts.
46
 */
40
 */
47
 
41
 
48
#include <time/clock.h>
42
#include <time/clock.h>
49
#include <time/timeout.h>
43
#include <time/timeout.h>
50
#include <arch/types.h>
44
#include <arch/types.h>
51
#include <config.h>
45
#include <config.h>
52
#include <synch/spinlock.h>
46
#include <synch/spinlock.h>
53
#include <synch/waitq.h>
47
#include <synch/waitq.h>
54
#include <func.h>
48
#include <func.h>
55
#include <proc/scheduler.h>
49
#include <proc/scheduler.h>
56
#include <cpu.h>
50
#include <cpu.h>
57
#include <arch.h>
51
#include <arch.h>
58
#include <adt/list.h>
52
#include <adt/list.h>
59
#include <atomic.h>
53
#include <atomic.h>
60
#include <proc/thread.h>
54
#include <proc/thread.h>
61
#include <sysinfo/sysinfo.h>
55
#include <sysinfo/sysinfo.h>
62
#include <arch/barrier.h>
56
#include <arch/barrier.h>
63
 
57
 
64
/* Pointers to public variables with time */
58
/* Pointers to public variables with time */
65
struct ptime {
59
struct ptime {
66
    __native seconds1;
60
    __native seconds1;
67
    __native useconds;
61
    __native useconds;
68
    __native seconds2;
62
    __native seconds2;
69
};
63
};
70
struct ptime *public_time;
64
struct ptime *public_time;
71
/* Variable holding fragment of second, so that we would update
65
/* Variable holding fragment of second, so that we would update
72
 * seconds correctly
66
 * seconds correctly
73
 */
67
 */
74
static __native secfrag = 0;
68
static __native secfrag = 0;
75
 
69
 
76
/** Initialize realtime clock counter
70
/** Initialize realtime clock counter
77
 *
71
 *
78
 * The applications (and sometimes kernel) need to access accurate
72
 * The applications (and sometimes kernel) need to access accurate
79
 * information about realtime data. We allocate 1 page with these
73
 * information about realtime data. We allocate 1 page with these
80
 * data and update it periodically.
74
 * data and update it periodically.
81
 *
75
 *
82
 *
76
 *
83
 */
77
 */
84
void clock_counter_init(void)
78
void clock_counter_init(void)
85
{
79
{
86
    void *faddr;
80
    void *faddr;
87
 
81
 
88
    faddr = (void *)PFN2ADDR(frame_alloc(0, FRAME_ATOMIC));
82
    faddr = (void *)PFN2ADDR(frame_alloc(0, FRAME_ATOMIC));
89
    if (!faddr)
83
    if (!faddr)
90
        panic("Cannot allocate page for clock");
84
        panic("Cannot allocate page for clock");
91
   
85
   
92
    public_time = (struct ptime *)PA2KA(faddr);
86
    public_time = (struct ptime *)PA2KA(faddr);
93
 
87
 
94
        /* TODO: We would need some arch dependent settings here */
88
        /* TODO: We would need some arch dependent settings here */
95
    public_time->seconds1 = 0;
89
    public_time->seconds1 = 0;
96
    public_time->seconds2 = 0;
90
    public_time->seconds2 = 0;
97
    public_time->useconds = 0;
91
    public_time->useconds = 0;
98
 
92
 
99
    sysinfo_set_item_val("clock.faddr", NULL, (__native)faddr);
93
    sysinfo_set_item_val("clock.faddr", NULL, (__native)faddr);
100
}
94
}
101
 
95
 
102
 
96
 
103
/** Update public counters
97
/** Update public counters
104
 *
98
 *
105
 * Update it only on first processor
99
 * Update it only on first processor
106
 * TODO: Do we really need so many write barriers?
100
 * TODO: Do we really need so many write barriers?
107
 */
101
 */
108
static void clock_update_counters(void)
102
static void clock_update_counters(void)
109
{
103
{
110
    if (CPU->id == 0) {
104
    if (CPU->id == 0) {
111
        secfrag += 1000000/HZ;
105
        secfrag += 1000000/HZ;
112
        if (secfrag >= 1000000) {
106
        if (secfrag >= 1000000) {
113
            secfrag -= 1000000;
107
            secfrag -= 1000000;
114
            public_time->seconds1++;
108
            public_time->seconds1++;
115
            write_barrier();
109
            write_barrier();
116
            public_time->useconds = secfrag;
110
            public_time->useconds = secfrag;
117
            write_barrier();
111
            write_barrier();
118
            public_time->seconds2 = public_time->seconds1;
112
            public_time->seconds2 = public_time->seconds1;
119
        } else
113
        } else
120
            public_time->useconds += 1000000/HZ;
114
            public_time->useconds += 1000000/HZ;
121
    }
115
    }
122
}
116
}
123
 
117
 
124
/** Clock routine
118
/** Clock routine
125
 *
119
 *
126
 * Clock routine executed from clock interrupt handler
120
 * Clock routine executed from clock interrupt handler
127
 * (assuming interrupts_disable()'d). Runs expired timeouts
121
 * (assuming interrupts_disable()'d). Runs expired timeouts
128
 * and preemptive scheduling.
122
 * and preemptive scheduling.
129
 *
123
 *
130
 */
124
 */
131
void clock(void)
125
void clock(void)
132
{
126
{
133
    link_t *l;
127
    link_t *l;
134
    timeout_t *h;
128
    timeout_t *h;
135
    timeout_handler_t f;
129
    timeout_handler_t f;
136
    void *arg;
130
    void *arg;
137
    count_t missed_clock_ticks = CPU->missed_clock_ticks;
131
    count_t missed_clock_ticks = CPU->missed_clock_ticks;
138
    int i;
132
    int i;
139
 
133
 
140
    /*
134
    /*
141
     * To avoid lock ordering problems,
135
     * To avoid lock ordering problems,
142
     * run all expired timeouts as you visit them.
136
     * run all expired timeouts as you visit them.
143
     */
137
     */
144
    for (i = 0; i <= missed_clock_ticks; i++) {
138
    for (i = 0; i <= missed_clock_ticks; i++) {
145
        clock_update_counters();
139
        clock_update_counters();
146
        spinlock_lock(&CPU->timeoutlock);
140
        spinlock_lock(&CPU->timeoutlock);
147
        while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
141
        while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
148
            h = list_get_instance(l, timeout_t, link);
142
            h = list_get_instance(l, timeout_t, link);
149
            spinlock_lock(&h->lock);
143
            spinlock_lock(&h->lock);
150
            if (h->ticks-- != 0) {
144
            if (h->ticks-- != 0) {
151
                spinlock_unlock(&h->lock);
145
                spinlock_unlock(&h->lock);
152
                break;
146
                break;
153
            }
147
            }
154
            list_remove(l);
148
            list_remove(l);
155
            f = h->handler;
149
            f = h->handler;
156
            arg = h->arg;
150
            arg = h->arg;
157
            timeout_reinitialize(h);
151
            timeout_reinitialize(h);
158
            spinlock_unlock(&h->lock); 
152
            spinlock_unlock(&h->lock); 
159
            spinlock_unlock(&CPU->timeoutlock);
153
            spinlock_unlock(&CPU->timeoutlock);
160
 
154
 
161
            f(arg);
155
            f(arg);
162
 
156
 
163
            spinlock_lock(&CPU->timeoutlock);
157
            spinlock_lock(&CPU->timeoutlock);
164
        }
158
        }
165
        spinlock_unlock(&CPU->timeoutlock);
159
        spinlock_unlock(&CPU->timeoutlock);
166
    }
160
    }
167
    CPU->missed_clock_ticks = 0;
161
    CPU->missed_clock_ticks = 0;
168
 
162
 
169
    /*
163
    /*
170
     * Do CPU usage accounting and find out whether to preempt THREAD.
164
     * Do CPU usage accounting and find out whether to preempt THREAD.
171
     */
165
     */
172
 
166
 
173
    if (THREAD) {
167
    if (THREAD) {
174
        __u64 ticks;
168
        __u64 ticks;
175
       
169
       
176
        spinlock_lock(&CPU->lock);
170
        spinlock_lock(&CPU->lock);
177
        CPU->needs_relink += 1 + missed_clock_ticks;
171
        CPU->needs_relink += 1 + missed_clock_ticks;
178
        spinlock_unlock(&CPU->lock);   
172
        spinlock_unlock(&CPU->lock);   
179
   
173
   
180
        spinlock_lock(&THREAD->lock);
174
        spinlock_lock(&THREAD->lock);
181
        if ((ticks = THREAD->ticks)) {
175
        if ((ticks = THREAD->ticks)) {
182
            if (ticks >= 1 + missed_clock_ticks)
176
            if (ticks >= 1 + missed_clock_ticks)
183
                THREAD->ticks -= 1 + missed_clock_ticks;
177
                THREAD->ticks -= 1 + missed_clock_ticks;
184
            else
178
            else
185
                THREAD->ticks = 0;
179
                THREAD->ticks = 0;
186
        }
180
        }
187
        spinlock_unlock(&THREAD->lock);
181
        spinlock_unlock(&THREAD->lock);
188
       
182
       
189
        if (!ticks && !PREEMPTION_DISABLED) {
183
        if (!ticks && !PREEMPTION_DISABLED) {
190
            scheduler();
184
            scheduler();
191
        }
185
        }
192
    }
186
    }
193
 
187
 
194
}
188
}
195
 
189
 
196
 /** @}
190
 /** @}
197
 */
191
 */
198
 
192
 
199
 
193