Rev 2869 | Rev 3431 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2869 | Rev 3015 | ||
---|---|---|---|
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 <mm/frame.h> |
56 | #include <mm/frame.h> |
57 | #include <ddi/ddi.h> |
57 | #include <ddi/ddi.h> |
58 | #include <udebug/udebug.h> |
58 | #include <udebug/udebug.h> |
59 | 59 | ||
60 | /* Pointer to variable with uptime */ |
60 | /* Pointer to variable with uptime */ |
61 | uptime_t *uptime; |
61 | uptime_t *uptime; |
62 | 62 | ||
63 | /** Physical memory area of the real time clock */ |
63 | /** Physical memory area of the real time clock */ |
64 | static parea_t clock_parea; |
64 | static parea_t clock_parea; |
65 | 65 | ||
66 | /* Variable holding fragment of second, so that we would update |
66 | /* Variable holding fragment of second, so that we would update |
67 | * seconds correctly |
67 | * seconds correctly |
68 | */ |
68 | */ |
69 | static unative_t secfrag = 0; |
69 | static unative_t secfrag = 0; |
70 | 70 | ||
71 | /** Initialize realtime clock counter |
71 | /** Initialize realtime clock counter |
72 | * |
72 | * |
73 | * The applications (and sometimes kernel) need to access accurate |
73 | * The applications (and sometimes kernel) need to access accurate |
74 | * information about realtime data. We allocate 1 page with these |
74 | * information about realtime data. We allocate 1 page with these |
75 | * data and update it periodically. |
75 | * data and update it periodically. |
76 | */ |
76 | */ |
77 | void clock_counter_init(void) |
77 | void clock_counter_init(void) |
78 | { |
78 | { |
79 | void *faddr; |
79 | void *faddr; |
80 | 80 | ||
81 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC); |
81 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC); |
82 | if (!faddr) |
82 | if (!faddr) |
83 | panic("Cannot allocate page for clock"); |
83 | panic("Cannot allocate page for clock"); |
84 | 84 | ||
85 | uptime = (uptime_t *) PA2KA(faddr); |
85 | uptime = (uptime_t *) PA2KA(faddr); |
86 | 86 | ||
87 | uptime->seconds1 = 0; |
87 | uptime->seconds1 = 0; |
88 | uptime->seconds2 = 0; |
88 | uptime->seconds2 = 0; |
89 | uptime->useconds = 0; |
89 | uptime->useconds = 0; |
90 | 90 | ||
91 | clock_parea.pbase = (uintptr_t) faddr; |
91 | clock_parea.pbase = (uintptr_t) faddr; |
92 | clock_parea.vbase = (uintptr_t) uptime; |
92 | clock_parea.vbase = (uintptr_t) uptime; |
93 | clock_parea.frames = 1; |
93 | clock_parea.frames = 1; |
94 | clock_parea.cacheable = true; |
94 | clock_parea.cacheable = true; |
95 | ddi_parea_register(&clock_parea); |
95 | ddi_parea_register(&clock_parea); |
96 | 96 | ||
97 | /* |
97 | /* |
98 | * Prepare information for the userspace so that it can successfully |
98 | * Prepare information for the userspace so that it can successfully |
99 | * physmem_map() the clock_parea. |
99 | * physmem_map() the clock_parea. |
100 | */ |
100 | */ |
101 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true); |
101 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true); |
102 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr); |
102 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr); |
103 | } |
103 | } |
104 | 104 | ||
105 | 105 | ||
106 | /** Update public counters |
106 | /** Update public counters |
107 | * |
107 | * |
108 | * Update it only on first processor |
108 | * Update it only on first processor |
109 | * TODO: Do we really need so many write barriers? |
109 | * TODO: Do we really need so many write barriers? |
110 | */ |
110 | */ |
111 | static void clock_update_counters(void) |
111 | static void clock_update_counters(void) |
112 | { |
112 | { |
113 | if (CPU->id == 0) { |
113 | if (CPU->id == 0) { |
114 | secfrag += 1000000 / HZ; |
114 | secfrag += 1000000 / HZ; |
115 | if (secfrag >= 1000000) { |
115 | if (secfrag >= 1000000) { |
116 | secfrag -= 1000000; |
116 | secfrag -= 1000000; |
117 | uptime->seconds1++; |
117 | uptime->seconds1++; |
118 | write_barrier(); |
118 | write_barrier(); |
119 | uptime->useconds = secfrag; |
119 | uptime->useconds = secfrag; |
120 | write_barrier(); |
120 | write_barrier(); |
121 | uptime->seconds2 = uptime->seconds1; |
121 | uptime->seconds2 = uptime->seconds1; |
122 | } else |
122 | } else |
123 | uptime->useconds += 1000000 / HZ; |
123 | uptime->useconds += 1000000 / HZ; |
124 | } |
124 | } |
125 | } |
125 | } |
126 | 126 | ||
127 | /** Clock routine |
127 | /** Clock routine |
128 | * |
128 | * |
129 | * Clock routine executed from clock interrupt handler |
129 | * Clock routine executed from clock interrupt handler |
130 | * (assuming interrupts_disable()'d). Runs expired timeouts |
130 | * (assuming interrupts_disable()'d). Runs expired timeouts |
131 | * and preemptive scheduling. |
131 | * and preemptive scheduling. |
132 | * |
132 | * |
133 | */ |
133 | */ |
134 | void clock(void) |
134 | void clock(void) |
135 | { |
135 | { |
136 | link_t *l; |
136 | link_t *l; |
137 | timeout_t *h; |
137 | timeout_t *h; |
138 | timeout_handler_t f; |
138 | timeout_handler_t f; |
139 | void *arg; |
139 | void *arg; |
140 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
140 | count_t missed_clock_ticks = CPU->missed_clock_ticks; |
141 | unsigned int i; |
141 | unsigned int i; |
142 | 142 | ||
143 | /* |
143 | /* |
144 | * To avoid lock ordering problems, |
144 | * To avoid lock ordering problems, |
145 | * run all expired timeouts as you visit them. |
145 | * run all expired timeouts as you visit them. |
146 | */ |
146 | */ |
147 | for (i = 0; i <= missed_clock_ticks; i++) { |
147 | for (i = 0; i <= missed_clock_ticks; i++) { |
148 | clock_update_counters(); |
148 | clock_update_counters(); |
149 | spinlock_lock(&CPU->timeoutlock); |
149 | spinlock_lock(&CPU->timeoutlock); |
150 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
150 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
151 | h = list_get_instance(l, timeout_t, link); |
151 | h = list_get_instance(l, timeout_t, link); |
152 | spinlock_lock(&h->lock); |
152 | spinlock_lock(&h->lock); |
153 | if (h->ticks-- != 0) { |
153 | if (h->ticks-- != 0) { |
154 | spinlock_unlock(&h->lock); |
154 | spinlock_unlock(&h->lock); |
155 | break; |
155 | break; |
156 | } |
156 | } |
157 | list_remove(l); |
157 | list_remove(l); |
158 | f = h->handler; |
158 | f = h->handler; |
159 | arg = h->arg; |
159 | arg = h->arg; |
160 | timeout_reinitialize(h); |
160 | timeout_reinitialize(h); |
161 | spinlock_unlock(&h->lock); |
161 | spinlock_unlock(&h->lock); |
162 | spinlock_unlock(&CPU->timeoutlock); |
162 | spinlock_unlock(&CPU->timeoutlock); |
163 | 163 | ||
164 | f(arg); |
164 | f(arg); |
165 | 165 | ||
166 | spinlock_lock(&CPU->timeoutlock); |
166 | spinlock_lock(&CPU->timeoutlock); |
167 | } |
167 | } |
168 | spinlock_unlock(&CPU->timeoutlock); |
168 | spinlock_unlock(&CPU->timeoutlock); |
169 | } |
169 | } |
170 | CPU->missed_clock_ticks = 0; |
170 | CPU->missed_clock_ticks = 0; |
171 | 171 | ||
172 | /* |
172 | /* |
173 | * Do CPU usage accounting and find out whether to preempt THREAD. |
173 | * Do CPU usage accounting and find out whether to preempt THREAD. |
174 | */ |
174 | */ |
175 | 175 | ||
176 | if (THREAD) { |
176 | if (THREAD) { |
177 | uint64_t ticks; |
177 | uint64_t ticks; |
178 | 178 | ||
179 | spinlock_lock(&CPU->lock); |
179 | spinlock_lock(&CPU->lock); |
180 | CPU->needs_relink += 1 + missed_clock_ticks; |
180 | CPU->needs_relink += 1 + missed_clock_ticks; |
181 | spinlock_unlock(&CPU->lock); |
181 | spinlock_unlock(&CPU->lock); |
182 | 182 | ||
183 | spinlock_lock(&THREAD->lock); |
183 | spinlock_lock(&THREAD->lock); |
184 | if ((ticks = THREAD->ticks)) { |
184 | if ((ticks = THREAD->ticks)) { |
185 | if (ticks >= 1 + missed_clock_ticks) |
185 | if (ticks >= 1 + missed_clock_ticks) |
186 | THREAD->ticks -= 1 + missed_clock_ticks; |
186 | THREAD->ticks -= 1 + missed_clock_ticks; |
187 | else |
187 | else |
188 | THREAD->ticks = 0; |
188 | THREAD->ticks = 0; |
189 | } |
189 | } |
190 | spinlock_unlock(&THREAD->lock); |
190 | spinlock_unlock(&THREAD->lock); |
191 | 191 | ||
192 | if (!ticks && !PREEMPTION_DISABLED) { |
192 | if (!ticks && !PREEMPTION_DISABLED) { |
193 | /* |
- | |
194 | * Make thread stoppable when preempted. |
- | |
195 | * Necessary to be able to stop CPU-bound threads |
- | |
196 | * that don't do any syscalls. |
- | |
197 | */ |
- | |
198 | udebug_stoppable_begin(); |
- | |
199 | 193 | ||
200 | scheduler(); |
194 | scheduler(); |
201 | 195 | ||
- | 196 | /* |
|
- | 197 | * Give udebug chance to stop the thread |
|
- | 198 | * before it begins executing. |
|
- | 199 | */ |
|
202 | udebug_stoppable_end(); |
200 | udebug_before_thread_runs(); |
203 | } |
201 | } |
204 | } |
202 | } |
205 | 203 | ||
206 | } |
204 | } |
207 | 205 | ||
208 | /** @} |
206 | /** @} |
209 | */ |
207 | */ |
210 | 208 |