Subversion Repositories HelenOS

Rev

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

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