Subversion Repositories HelenOS

Rev

Rev 3610 | Rev 4377 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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