2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2022 Jiří Zárevúcky
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
30 /** @addtogroup kernel_time
36 * @brief High-level clock interrupt handler.
38 * This file contains the clock() function which is the source
39 * of preemption. It is also responsible for executing expired
44 #include <time/clock.h>
45 #include <time/timeout.h>
47 #include <synch/spinlock.h>
48 #include <synch/waitq.h>
50 #include <proc/scheduler.h>
55 #include <proc/thread.h>
56 #include <sysinfo/sysinfo.h>
60 #include <arch/cycle.h>
61 #include <preemption.h>
63 /* Pointer to variable with uptime */
66 /** Physical memory area of the real time clock */
67 static parea_t clock_parea
;
69 /** Initialize realtime clock counter
71 * The applications (and sometimes kernel) need to access accurate
72 * information about realtime data. We allocate 1 page with these
73 * data and update it periodically.
76 void clock_counter_init(void)
78 uintptr_t faddr
= frame_alloc(1, FRAME_LOWMEM
| FRAME_ATOMIC
, 0);
80 panic("Cannot allocate page for clock.");
82 uptime
= (uptime_t
*) PA2KA(faddr
);
88 ddi_parea_init(&clock_parea
);
89 clock_parea
.pbase
= faddr
;
90 clock_parea
.frames
= 1;
91 clock_parea
.unpriv
= true;
92 clock_parea
.mapped
= false;
93 ddi_parea_register(&clock_parea
);
96 * Prepare information for the userspace so that it can successfully
97 * physmem_map() the clock_parea.
100 sysinfo_set_item_val("clock.faddr", NULL
, (sysarg_t
) faddr
);
103 /** Update public counters
105 * Update it only on first processor
107 static void clock_update_counters(uint64_t current_tick
)
110 uint64_t usec
= (1000000 / HZ
) * current_tick
;
112 sysarg_t secs
= usec
/ 1000000;
113 sysarg_t usecs
= usec
% 1000000;
115 uptime
->seconds1
= secs
;
117 uptime
->useconds
= usecs
;
119 uptime
->seconds2
= secs
;
123 static void cpu_update_accounting(void)
125 irq_spinlock_lock(&CPU
->lock
, false);
126 uint64_t now
= get_cycle();
127 atomic_time_increment(&CPU
->busy_cycles
, now
- CPU
->last_cycle
);
128 CPU
->last_cycle
= now
;
129 irq_spinlock_unlock(&CPU
->lock
, false);
134 * Clock routine executed from clock interrupt handler
135 * (assuming interrupts_disable()'d). Runs expired timeouts
136 * and preemptive scheduling.
141 size_t missed_clock_ticks
= CPU
->missed_clock_ticks
;
142 CPU
->missed_clock_ticks
= 0;
144 CPU
->current_clock_tick
+= missed_clock_ticks
+ 1;
145 uint64_t current_clock_tick
= CPU
->current_clock_tick
;
146 clock_update_counters(current_clock_tick
);
148 /* Account CPU usage */
149 cpu_update_accounting();
152 * To avoid lock ordering problems,
153 * run all expired timeouts as you visit them.
157 irq_spinlock_lock(&CPU
->timeoutlock
, false);
160 while ((cur
= list_first(&CPU
->timeout_active_list
)) != NULL
) {
161 timeout_t
*timeout
= list_get_instance(cur
, timeout_t
, link
);
163 if (current_clock_tick
<= timeout
->deadline
) {
168 timeout_handler_t handler
= timeout
->handler
;
169 void *arg
= timeout
->arg
;
170 atomic_bool
*finished
= &timeout
->finished
;
172 irq_spinlock_unlock(&CPU
->timeoutlock
, false);
176 /* Signal that the handler is finished. */
177 atomic_store_explicit(finished
, true, memory_order_release
);
179 irq_spinlock_lock(&CPU
->timeoutlock
, false);
182 irq_spinlock_unlock(&CPU
->timeoutlock
, false);
185 * Do CPU usage accounting and find out whether to preempt THREAD.
190 if (current_clock_tick
>= CPU
->preempt_deadline
&& PREEMPTION_ENABLED
) {
194 * Give udebug chance to stop the thread
195 * before it begins executing userspace code.
197 istate_t
*istate
= THREAD
->udebug
.uspace_state
;
198 if ((istate
) && (istate_from_uspace(istate
)))
199 udebug_before_thread_runs();