Subversion Repositories HelenOS

Rev

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

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