Rev 1434 | Rev 1457 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1434 | Rev 1438 | ||
---|---|---|---|
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 | /** |
29 | /** |
30 | * @file clock.c |
30 | * @file clock.c |
31 | * @brief High-level clock interrupt handler. |
31 | * @brief High-level clock interrupt handler. |
32 | * |
32 | * |
33 | * This file contains the clock() function which is the source |
33 | * This file contains the clock() function which is the source |
34 | * of preemption. It is also responsible for executing expired |
34 | * of preemption. It is also responsible for executing expired |
35 | * timeouts. |
35 | * timeouts. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <time/clock.h> |
38 | #include <time/clock.h> |
39 | #include <time/timeout.h> |
39 | #include <time/timeout.h> |
40 | #include <arch/types.h> |
40 | #include <arch/types.h> |
41 | #include <config.h> |
41 | #include <config.h> |
42 | #include <synch/spinlock.h> |
42 | #include <synch/spinlock.h> |
43 | #include <synch/waitq.h> |
43 | #include <synch/waitq.h> |
44 | #include <func.h> |
44 | #include <func.h> |
45 | #include <proc/scheduler.h> |
45 | #include <proc/scheduler.h> |
46 | #include <cpu.h> |
46 | #include <cpu.h> |
47 | #include <arch.h> |
47 | #include <arch.h> |
48 | #include <adt/list.h> |
48 | #include <adt/list.h> |
49 | #include <atomic.h> |
49 | #include <atomic.h> |
50 | #include <proc/thread.h> |
50 | #include <proc/thread.h> |
51 | #include <sysinfo/sysinfo.h> |
51 | #include <sysinfo/sysinfo.h> |
52 | #include <arch/barrier.h> |
52 | #include <arch/barrier.h> |
53 | 53 | ||
54 | /* Pointers to public variables with time */ |
54 | /* Pointers to public variables with time */ |
55 | struct ptime { |
55 | struct ptime { |
56 | __native seconds; |
56 | __native seconds1; |
57 | __native useconds; |
57 | __native useconds; |
58 | __native useconds2; |
58 | __native seconds2; |
59 | }; |
59 | }; |
60 | struct ptime *public_time; |
60 | struct ptime *public_time; |
61 | /* Variable holding fragment of second, so that we would update |
61 | /* Variable holding fragment of second, so that we would update |
62 | * seconds correctly |
62 | * seconds correctly |
63 | */ |
63 | */ |
64 | static __native secfrag = 0; |
64 | static __native secfrag = 0; |
65 | 65 | ||
66 | /** Initialize realtime clock counter |
66 | /** Initialize realtime clock counter |
67 | * |
67 | * |
68 | * The applications (and sometimes kernel) need to access accurate |
68 | * The applications (and sometimes kernel) need to access accurate |
69 | * information about realtime data. We allocate 1 page with these |
69 | * information about realtime data. We allocate 1 page with these |
70 | * data and update it periodically. |
70 | * data and update it periodically. |
71 | * |
71 | * |
72 | * |
72 | * |
73 | */ |
73 | */ |
74 | void clock_counter_init(void) |
74 | void clock_counter_init(void) |
75 | { |
75 | { |
76 | void *faddr; |
76 | void *faddr; |
77 | 77 | ||
78 | faddr = (void *)PFN2ADDR(frame_alloc(0, FRAME_ATOMIC)); |
78 | faddr = (void *)PFN2ADDR(frame_alloc(0, FRAME_ATOMIC)); |
79 | if (!faddr) |
79 | if (!faddr) |
80 | panic("Cannot allocate page for clock"); |
80 | panic("Cannot allocate page for clock"); |
81 | 81 | ||
82 | public_time = (struct ptime *)PA2KA(faddr); |
82 | public_time = (struct ptime *)PA2KA(faddr); |
83 | 83 | ||
84 | /* TODO: We would need some arch dependent settings here */ |
84 | /* TODO: We would need some arch dependent settings here */ |
- | 85 | public_time->seconds1 = 0; |
|
85 | public_time->seconds = 0; |
86 | public_time->seconds2 = 0; |
86 | public_time->useconds = 0; |
87 | public_time->useconds = 0; |
87 | 88 | ||
88 | sysinfo_set_item_val("clock.faddr", NULL, (__native)faddr); |
89 | sysinfo_set_item_val("clock.faddr", NULL, (__native)faddr); |
89 | } |
90 | } |
90 | 91 | ||
91 | 92 | ||
92 | /** Update public counters |
93 | /** Update public counters |
93 | * |
94 | * |
94 | * Update it only on first processor |
95 | * Update it only on first processor |
95 | * TODO: Do we really need so many write barriers? |
96 | * TODO: Do we really need so many write barriers? |
96 | */ |
97 | */ |
97 | static void clock_update_counters(void) |
98 | static void clock_update_counters(void) |
98 | { |
99 | { |
99 | if (CPU->id == 0) { |
100 | if (CPU->id == 0) { |
100 | secfrag += 1000000/HZ; |
101 | secfrag += 1000000/HZ; |
101 | if (secfrag >= 1000000) { |
102 | if (secfrag >= 1000000) { |
- | 103 | secfrag -= 1000000; |
|
102 | public_time->useconds = 0; |
104 | public_time->seconds1++; |
- | 105 | write_barrier(); |
|
- | 106 | public_time->useconds = secfrag; |
|
103 | write_barrier(); |
107 | write_barrier(); |
104 | public_time->seconds++; |
108 | public_time->seconds2 = public_time->seconds1; |
105 | secfrag = 0; |
- | |
106 | } else |
109 | } else |
107 | public_time->useconds += 1000000/HZ; |
110 | public_time->useconds += 1000000/HZ; |
108 | write_barrier(); |
- | |
109 | public_time->useconds2 = public_time->useconds; |
- | |
110 | write_barrier(); |
- | |
111 | } |
111 | } |
112 | } |
112 | } |
113 | 113 | ||
114 | /** Clock routine |
114 | /** Clock routine |
115 | * |
115 | * |
116 | * Clock routine executed from clock interrupt handler |
116 | * Clock routine executed from clock interrupt handler |
117 | * (assuming interrupts_disable()'d). Runs expired timeouts |
117 | * (assuming interrupts_disable()'d). Runs expired timeouts |
118 | * and preemptive scheduling. |
118 | * and preemptive scheduling. |
119 | * |
119 | * |
120 | */ |
120 | */ |
121 | void clock(void) |
121 | void clock(void) |
122 | { |
122 | { |
123 | link_t *l; |
123 | link_t *l; |
124 | timeout_t *h; |
124 | timeout_t *h; |
125 | timeout_handler_t f; |
125 | timeout_handler_t f; |
126 | void *arg; |
126 | void *arg; |
127 | int i; |
127 | int i; |
128 | 128 | ||
129 | /* |
129 | /* |
130 | * To avoid lock ordering problems, |
130 | * To avoid lock ordering problems, |
131 | * run all expired timeouts as you visit them. |
131 | * run all expired timeouts as you visit them. |
132 | */ |
132 | */ |
133 | for (i = 0; i <= CPU->missed_clock_ticks; i++) { |
133 | for (i = 0; i <= CPU->missed_clock_ticks; i++) { |
134 | clock_update_counters(); |
134 | clock_update_counters(); |
135 | spinlock_lock(&CPU->timeoutlock); |
135 | spinlock_lock(&CPU->timeoutlock); |
136 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
136 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) { |
137 | h = list_get_instance(l, timeout_t, link); |
137 | h = list_get_instance(l, timeout_t, link); |
138 | spinlock_lock(&h->lock); |
138 | spinlock_lock(&h->lock); |
139 | if (h->ticks-- != 0) { |
139 | if (h->ticks-- != 0) { |
140 | spinlock_unlock(&h->lock); |
140 | spinlock_unlock(&h->lock); |
141 | break; |
141 | break; |
142 | } |
142 | } |
143 | list_remove(l); |
143 | list_remove(l); |
144 | f = h->handler; |
144 | f = h->handler; |
145 | arg = h->arg; |
145 | arg = h->arg; |
146 | timeout_reinitialize(h); |
146 | timeout_reinitialize(h); |
147 | spinlock_unlock(&h->lock); |
147 | spinlock_unlock(&h->lock); |
148 | spinlock_unlock(&CPU->timeoutlock); |
148 | spinlock_unlock(&CPU->timeoutlock); |
149 | 149 | ||
150 | f(arg); |
150 | f(arg); |
151 | 151 | ||
152 | spinlock_lock(&CPU->timeoutlock); |
152 | spinlock_lock(&CPU->timeoutlock); |
153 | } |
153 | } |
154 | spinlock_unlock(&CPU->timeoutlock); |
154 | spinlock_unlock(&CPU->timeoutlock); |
155 | } |
155 | } |
156 | CPU->missed_clock_ticks = 0; |
156 | CPU->missed_clock_ticks = 0; |
157 | 157 | ||
158 | /* |
158 | /* |
159 | * Do CPU usage accounting and find out whether to preempt THREAD. |
159 | * Do CPU usage accounting and find out whether to preempt THREAD. |
160 | */ |
160 | */ |
161 | 161 | ||
162 | if (THREAD) { |
162 | if (THREAD) { |
163 | __u64 ticks; |
163 | __u64 ticks; |
164 | 164 | ||
165 | spinlock_lock(&CPU->lock); |
165 | spinlock_lock(&CPU->lock); |
166 | CPU->needs_relink++; |
166 | CPU->needs_relink++; |
167 | spinlock_unlock(&CPU->lock); |
167 | spinlock_unlock(&CPU->lock); |
168 | 168 | ||
169 | spinlock_lock(&THREAD->lock); |
169 | spinlock_lock(&THREAD->lock); |
170 | if ((ticks = THREAD->ticks)) |
170 | if ((ticks = THREAD->ticks)) |
171 | THREAD->ticks--; |
171 | THREAD->ticks--; |
172 | spinlock_unlock(&THREAD->lock); |
172 | spinlock_unlock(&THREAD->lock); |
173 | 173 | ||
174 | if (!ticks && !PREEMPTION_DISABLED) { |
174 | if (!ticks && !PREEMPTION_DISABLED) { |
175 | scheduler(); |
175 | scheduler(); |
176 | } |
176 | } |
177 | } |
177 | } |
178 | 178 | ||
179 | } |
179 | } |
180 | 180 |