1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * Copyright (c) 2022 Jiří Zárevúcky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup kernel_time
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | * @brief High-level clock interrupt handler.
|
---|
37 | *
|
---|
38 | * This file contains the clock() function which is the source
|
---|
39 | * of preemption. It is also responsible for executing expired
|
---|
40 | * timeouts.
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 | #include <time/clock.h>
|
---|
45 | #include <time/timeout.h>
|
---|
46 | #include <config.h>
|
---|
47 | #include <synch/spinlock.h>
|
---|
48 | #include <synch/waitq.h>
|
---|
49 | #include <halt.h>
|
---|
50 | #include <proc/scheduler.h>
|
---|
51 | #include <cpu.h>
|
---|
52 | #include <arch.h>
|
---|
53 | #include <adt/list.h>
|
---|
54 | #include <atomic.h>
|
---|
55 | #include <proc/thread.h>
|
---|
56 | #include <sysinfo/sysinfo.h>
|
---|
57 | #include <barrier.h>
|
---|
58 | #include <mm/frame.h>
|
---|
59 | #include <ddi/ddi.h>
|
---|
60 | #include <arch/cycle.h>
|
---|
61 |
|
---|
62 | /* Pointer to variable with uptime */
|
---|
63 | uptime_t *uptime;
|
---|
64 |
|
---|
65 | /** Physical memory area of the real time clock */
|
---|
66 | static parea_t clock_parea;
|
---|
67 |
|
---|
68 | /** Initialize realtime clock counter
|
---|
69 | *
|
---|
70 | * The applications (and sometimes kernel) need to access accurate
|
---|
71 | * information about realtime data. We allocate 1 page with these
|
---|
72 | * data and update it periodically.
|
---|
73 | *
|
---|
74 | */
|
---|
75 | void clock_counter_init(void)
|
---|
76 | {
|
---|
77 | uintptr_t faddr = frame_alloc(1, FRAME_LOWMEM | FRAME_ATOMIC, 0);
|
---|
78 | if (faddr == 0)
|
---|
79 | panic("Cannot allocate page for clock.");
|
---|
80 |
|
---|
81 | uptime = (uptime_t *) PA2KA(faddr);
|
---|
82 |
|
---|
83 | uptime->seconds1 = 0;
|
---|
84 | uptime->seconds2 = 0;
|
---|
85 | uptime->useconds = 0;
|
---|
86 |
|
---|
87 | ddi_parea_init(&clock_parea);
|
---|
88 | clock_parea.pbase = faddr;
|
---|
89 | clock_parea.frames = 1;
|
---|
90 | clock_parea.unpriv = true;
|
---|
91 | clock_parea.mapped = false;
|
---|
92 | ddi_parea_register(&clock_parea);
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Prepare information for the userspace so that it can successfully
|
---|
96 | * physmem_map() the clock_parea.
|
---|
97 | *
|
---|
98 | */
|
---|
99 | sysinfo_set_item_val("clock.faddr", NULL, (sysarg_t) faddr);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Update public counters
|
---|
103 | *
|
---|
104 | * Update it only on first processor
|
---|
105 | */
|
---|
106 | static void clock_update_counters(uint64_t current_tick)
|
---|
107 | {
|
---|
108 | if (CPU->id == 0) {
|
---|
109 | uint64_t usec = (1000000 / HZ) * current_tick;
|
---|
110 |
|
---|
111 | sysarg_t secs = usec / 1000000;
|
---|
112 | sysarg_t usecs = usec % 1000000;
|
---|
113 |
|
---|
114 | uptime->seconds1 = secs;
|
---|
115 | write_barrier();
|
---|
116 | uptime->useconds = usecs;
|
---|
117 | write_barrier();
|
---|
118 | uptime->seconds2 = secs;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void cpu_update_accounting(void)
|
---|
123 | {
|
---|
124 | irq_spinlock_lock(&CPU->lock, false);
|
---|
125 | uint64_t now = get_cycle();
|
---|
126 | CPU->busy_cycles += now - CPU->last_cycle;
|
---|
127 | CPU->last_cycle = now;
|
---|
128 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Clock routine
|
---|
132 | *
|
---|
133 | * Clock routine executed from clock interrupt handler
|
---|
134 | * (assuming interrupts_disable()'d). Runs expired timeouts
|
---|
135 | * and preemptive scheduling.
|
---|
136 | *
|
---|
137 | */
|
---|
138 | void clock(void)
|
---|
139 | {
|
---|
140 | size_t missed_clock_ticks = CPU->missed_clock_ticks;
|
---|
141 | CPU->missed_clock_ticks = 0;
|
---|
142 |
|
---|
143 | CPU->current_clock_tick += missed_clock_ticks + 1;
|
---|
144 | uint64_t current_clock_tick = CPU->current_clock_tick;
|
---|
145 | clock_update_counters(current_clock_tick);
|
---|
146 |
|
---|
147 | /* Account CPU usage */
|
---|
148 | cpu_update_accounting();
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * To avoid lock ordering problems,
|
---|
152 | * run all expired timeouts as you visit them.
|
---|
153 | *
|
---|
154 | */
|
---|
155 |
|
---|
156 | irq_spinlock_lock(&CPU->timeoutlock, false);
|
---|
157 |
|
---|
158 | link_t *cur;
|
---|
159 | while ((cur = list_first(&CPU->timeout_active_list)) != NULL) {
|
---|
160 | timeout_t *timeout = list_get_instance(cur, timeout_t, link);
|
---|
161 |
|
---|
162 | if (current_clock_tick <= timeout->deadline) {
|
---|
163 | break;
|
---|
164 | }
|
---|
165 |
|
---|
166 | list_remove(cur);
|
---|
167 | timeout_handler_t handler = timeout->handler;
|
---|
168 | void *arg = timeout->arg;
|
---|
169 |
|
---|
170 | irq_spinlock_unlock(&CPU->timeoutlock, false);
|
---|
171 |
|
---|
172 | handler(arg);
|
---|
173 |
|
---|
174 | irq_spinlock_lock(&CPU->timeoutlock, false);
|
---|
175 | }
|
---|
176 |
|
---|
177 | irq_spinlock_unlock(&CPU->timeoutlock, false);
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Do CPU usage accounting and find out whether to preempt THREAD.
|
---|
181 | *
|
---|
182 | */
|
---|
183 |
|
---|
184 | if (THREAD) {
|
---|
185 | uint64_t ticks;
|
---|
186 |
|
---|
187 | irq_spinlock_lock(&CPU->lock, false);
|
---|
188 | CPU->needs_relink += 1 + missed_clock_ticks;
|
---|
189 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
190 |
|
---|
191 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
192 | if ((ticks = THREAD->ticks)) {
|
---|
193 | if (ticks >= 1 + missed_clock_ticks)
|
---|
194 | THREAD->ticks -= 1 + missed_clock_ticks;
|
---|
195 | else
|
---|
196 | THREAD->ticks = 0;
|
---|
197 | }
|
---|
198 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
199 |
|
---|
200 | if (ticks == 0 && PREEMPTION_ENABLED) {
|
---|
201 | scheduler();
|
---|
202 | #ifdef CONFIG_UDEBUG
|
---|
203 | /*
|
---|
204 | * Give udebug chance to stop the thread
|
---|
205 | * before it begins executing userspace code.
|
---|
206 | */
|
---|
207 | istate_t *istate = THREAD->udebug.uspace_state;
|
---|
208 | if ((istate) && (istate_from_uspace(istate)))
|
---|
209 | udebug_before_thread_runs();
|
---|
210 | #endif
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** @}
|
---|
216 | */
|
---|