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