Subversion Repositories HelenOS

Rev

Rev 4377 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1731 jermar 29
/** @addtogroup time
1702 cejka 30
 * @{
31
 */
32
 
1264 jermar 33
/**
1702 cejka 34
 * @file
1264 jermar 35
 * @brief   High-level clock interrupt handler.
36
 *
37
 * This file contains the clock() function which is the source
38
 * of preemption. It is also responsible for executing expired
39
 * timeouts.
40
 */
41
 
1 jermar 42
#include <time/clock.h>
43
#include <time/timeout.h>
44
#include <config.h>
45
#include <synch/spinlock.h>
46
#include <synch/waitq.h>
47
#include <func.h>
48
#include <proc/scheduler.h>
49
#include <cpu.h>
50
#include <arch.h>
788 jermar 51
#include <adt/list.h>
1104 jermar 52
#include <atomic.h>
391 jermar 53
#include <proc/thread.h>
1434 palkovsky 54
#include <sysinfo/sysinfo.h>
55
#include <arch/barrier.h>
3610 svoboda 56
#include <arch/interrupt.h>
2015 jermar 57
#include <mm/frame.h>
58
#include <ddi/ddi.h>
2869 svoboda 59
#include <udebug/udebug.h>
1 jermar 60
 
2275 decky 61
/* Pointer to variable with uptime */
62
uptime_t *uptime;
63
 
64
/** Physical memory area of the real time clock */
2015 jermar 65
static parea_t clock_parea;
66
 
1434 palkovsky 67
/* Variable holding fragment of second, so that we would update
68
 * seconds correctly
69
 */
1780 jermar 70
static unative_t secfrag = 0;
1434 palkovsky 71
 
72
/** Initialize realtime clock counter
73
 *
74
 * The applications (and sometimes kernel) need to access accurate
75
 * information about realtime data. We allocate 1 page with these
76
 * data and update it periodically.
77
 */
78
void clock_counter_init(void)
79
{
80
    void *faddr;
81
 
2015 jermar 82
    faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
1434 palkovsky 83
    if (!faddr)
4377 svoboda 84
        panic("Cannot allocate page for clock.");
1434 palkovsky 85
 
2275 decky 86
    uptime = (uptime_t *) PA2KA(faddr);
87
 
88
    uptime->seconds1 = 0;
89
    uptime->seconds2 = 0;
90
    uptime->useconds = 0;
1434 palkovsky 91
 
2015 jermar 92
    clock_parea.pbase = (uintptr_t) faddr;
93
    clock_parea.frames = 1;
94
    ddi_parea_register(&clock_parea);
95
 
96
    /*
97
     * Prepare information for the userspace so that it can successfully
98
     * physmem_map() the clock_parea.
99
     */
100
    sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true);
101
    sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr);
1434 palkovsky 102
}
103
 
104
 
105
/** Update public counters
106
 *
107
 * Update it only on first processor
108
 * TODO: Do we really need so many write barriers?
109
 */
110
static void clock_update_counters(void)
111
{
112
    if (CPU->id == 0) {
2275 decky 113
        secfrag += 1000000 / HZ;
1434 palkovsky 114
        if (secfrag >= 1000000) {
1438 palkovsky 115
            secfrag -= 1000000;
2275 decky 116
            uptime->seconds1++;
1434 palkovsky 117
            write_barrier();
2275 decky 118
            uptime->useconds = secfrag;
1438 palkovsky 119
            write_barrier();
2275 decky 120
            uptime->seconds2 = uptime->seconds1;
1434 palkovsky 121
        } else
2275 decky 122
            uptime->useconds += 1000000 / HZ;
1434 palkovsky 123
    }
124
}
125
 
107 decky 126
/** Clock routine
127
 *
128
 * Clock routine executed from clock interrupt handler
413 jermar 129
 * (assuming interrupts_disable()'d). Runs expired timeouts
107 decky 130
 * and preemptive scheduling.
131
 *
1 jermar 132
 */
133
void clock(void)
134
{
135
    link_t *l;
136
    timeout_t *h;
411 jermar 137
    timeout_handler_t f;
1 jermar 138
    void *arg;
4692 svoboda 139
    size_t missed_clock_ticks = CPU->missed_clock_ticks;
2745 decky 140
    unsigned int i;
1 jermar 141
 
142
    /*
143
     * To avoid lock ordering problems,
144
     * run all expired timeouts as you visit them.
145
     */
1457 jermar 146
    for (i = 0; i <= missed_clock_ticks; i++) {
1434 palkovsky 147
        clock_update_counters();
1431 jermar 148
        spinlock_lock(&CPU->timeoutlock);
149
        while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
150
            h = list_get_instance(l, timeout_t, link);
151
            spinlock_lock(&h->lock);
152
            if (h->ticks-- != 0) {
153
                spinlock_unlock(&h->lock);
154
                break;
155
            }
156
            list_remove(l);
157
            f = h->handler;
158
            arg = h->arg;
159
            timeout_reinitialize(h);
160
            spinlock_unlock(&h->lock); 
161
            spinlock_unlock(&CPU->timeoutlock);
162
 
163
            f(arg);
164
 
165
            spinlock_lock(&CPU->timeoutlock);
1 jermar 166
        }
15 jermar 167
        spinlock_unlock(&CPU->timeoutlock);
1 jermar 168
    }
1431 jermar 169
    CPU->missed_clock_ticks = 0;
1 jermar 170
 
171
    /*
15 jermar 172
     * Do CPU usage accounting and find out whether to preempt THREAD.
1 jermar 173
     */
174
 
15 jermar 175
    if (THREAD) {
1780 jermar 176
        uint64_t ticks;
221 jermar 177
 
15 jermar 178
        spinlock_lock(&CPU->lock);
1457 jermar 179
        CPU->needs_relink += 1 + missed_clock_ticks;
15 jermar 180
        spinlock_unlock(&CPU->lock);   
1 jermar 181
 
15 jermar 182
        spinlock_lock(&THREAD->lock);
1457 jermar 183
        if ((ticks = THREAD->ticks)) {
184
            if (ticks >= 1 + missed_clock_ticks)
185
                THREAD->ticks -= 1 + missed_clock_ticks;
186
            else
187
                THREAD->ticks = 0;
188
        }
221 jermar 189
        spinlock_unlock(&THREAD->lock);
190
 
191
        if (!ticks && !PREEMPTION_DISABLED) {
3684 svoboda 192
#ifdef CONFIG_UDEBUG
193
            istate_t *istate;
194
#endif
1 jermar 195
            scheduler();
2869 svoboda 196
 
3431 svoboda 197
#ifdef CONFIG_UDEBUG
3015 svoboda 198
            /*
199
             * Give udebug chance to stop the thread
3684 svoboda 200
             * before it begins executing userspace code.
3015 svoboda 201
             */
3684 svoboda 202
            istate = THREAD->udebug.uspace_state;
203
            if (istate && istate_from_uspace(istate))
3610 svoboda 204
                udebug_before_thread_runs();
3431 svoboda 205
#endif
1 jermar 206
        }
207
    }
208
 
209
}
1702 cejka 210
 
1731 jermar 211
/** @}
1702 cejka 212
 */