[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[e88eb48] | 29 | /** @addtogroup kernel_time
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf26ba9] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[da1bafb] | 35 | * @brief High-level clock interrupt handler.
|
---|
[cf26ba9] | 36 | *
|
---|
| 37 | * This file contains the clock() function which is the source
|
---|
| 38 | * of preemption. It is also responsible for executing expired
|
---|
| 39 | * timeouts.
|
---|
[da1bafb] | 40 | *
|
---|
[cf26ba9] | 41 | */
|
---|
[da1bafb] | 42 |
|
---|
[f761f1eb] | 43 | #include <time/clock.h>
|
---|
| 44 | #include <time/timeout.h>
|
---|
| 45 | #include <config.h>
|
---|
| 46 | #include <synch/spinlock.h>
|
---|
| 47 | #include <synch/waitq.h>
|
---|
[b2e121a] | 48 | #include <halt.h>
|
---|
[f761f1eb] | 49 | #include <proc/scheduler.h>
|
---|
| 50 | #include <cpu.h>
|
---|
| 51 | #include <arch.h>
|
---|
[5c9a08b] | 52 | #include <adt/list.h>
|
---|
[23684b7] | 53 | #include <atomic.h>
|
---|
[1084a784] | 54 | #include <proc/thread.h>
|
---|
[d6e5cbc] | 55 | #include <sysinfo/sysinfo.h>
|
---|
[05882233] | 56 | #include <barrier.h>
|
---|
[f8ddd17] | 57 | #include <mm/frame.h>
|
---|
| 58 | #include <ddi/ddi.h>
|
---|
[d0c82c5] | 59 | #include <arch/cycle.h>
|
---|
[f8ddd17] | 60 |
|
---|
[4b662f8c] | 61 | /* Pointer to variable with uptime */
|
---|
| 62 | uptime_t *uptime;
|
---|
| 63 |
|
---|
| 64 | /** Physical memory area of the real time clock */
|
---|
[f8ddd17] | 65 | static parea_t clock_parea;
|
---|
[d6e5cbc] | 66 |
|
---|
[da1bafb] | 67 | /** Fragment of second
|
---|
| 68 | *
|
---|
| 69 | * For updating seconds correctly.
|
---|
| 70 | *
|
---|
[d6e5cbc] | 71 | */
|
---|
[96b02eb9] | 72 | static sysarg_t secfrag = 0;
|
---|
[d6e5cbc] | 73 |
|
---|
| 74 | /** Initialize realtime clock counter
|
---|
| 75 | *
|
---|
| 76 | * The applications (and sometimes kernel) need to access accurate
|
---|
[1b20da0] | 77 | * information about realtime data. We allocate 1 page with these
|
---|
[d6e5cbc] | 78 | * data and update it periodically.
|
---|
[da1bafb] | 79 | *
|
---|
[d6e5cbc] | 80 | */
|
---|
| 81 | void clock_counter_init(void)
|
---|
| 82 | {
|
---|
[482f968] | 83 | uintptr_t faddr = frame_alloc(1, FRAME_LOWMEM | FRAME_ATOMIC, 0);
|
---|
[8cbf1c3] | 84 | if (faddr == 0)
|
---|
[f651e80] | 85 | panic("Cannot allocate page for clock.");
|
---|
[a35b458] | 86 |
|
---|
[4b662f8c] | 87 | uptime = (uptime_t *) PA2KA(faddr);
|
---|
[a35b458] | 88 |
|
---|
[4b662f8c] | 89 | uptime->seconds1 = 0;
|
---|
| 90 | uptime->seconds2 = 0;
|
---|
[9dae191e] | 91 | uptime->useconds = 0;
|
---|
[a35b458] | 92 |
|
---|
[6f7071b] | 93 | ddi_parea_init(&clock_parea);
|
---|
[8cbf1c3] | 94 | clock_parea.pbase = faddr;
|
---|
[f8ddd17] | 95 | clock_parea.frames = 1;
|
---|
[d7533c7] | 96 | clock_parea.unpriv = true;
|
---|
[b366a6f4] | 97 | clock_parea.mapped = false;
|
---|
[f8ddd17] | 98 | ddi_parea_register(&clock_parea);
|
---|
[a35b458] | 99 |
|
---|
[f8ddd17] | 100 | /*
|
---|
| 101 | * Prepare information for the userspace so that it can successfully
|
---|
| 102 | * physmem_map() the clock_parea.
|
---|
[da1bafb] | 103 | *
|
---|
[f8ddd17] | 104 | */
|
---|
[96b02eb9] | 105 | sysinfo_set_item_val("clock.faddr", NULL, (sysarg_t) faddr);
|
---|
[d6e5cbc] | 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Update public counters
|
---|
| 109 | *
|
---|
| 110 | * Update it only on first processor
|
---|
[da1bafb] | 111 | * TODO: Do we really need so many write barriers?
|
---|
| 112 | *
|
---|
[d6e5cbc] | 113 | */
|
---|
| 114 | static void clock_update_counters(void)
|
---|
| 115 | {
|
---|
| 116 | if (CPU->id == 0) {
|
---|
[4b662f8c] | 117 | secfrag += 1000000 / HZ;
|
---|
[d6e5cbc] | 118 | if (secfrag >= 1000000) {
|
---|
[fd8302d] | 119 | secfrag -= 1000000;
|
---|
[4b662f8c] | 120 | uptime->seconds1++;
|
---|
[d6e5cbc] | 121 | write_barrier();
|
---|
[4b662f8c] | 122 | uptime->useconds = secfrag;
|
---|
[fd8302d] | 123 | write_barrier();
|
---|
[4b662f8c] | 124 | uptime->seconds2 = uptime->seconds1;
|
---|
[d6e5cbc] | 125 | } else
|
---|
[4b662f8c] | 126 | uptime->useconds += 1000000 / HZ;
|
---|
[d6e5cbc] | 127 | }
|
---|
| 128 | }
|
---|
[f761f1eb] | 129 |
|
---|
[d0c82c5] | 130 | static void cpu_update_accounting(void)
|
---|
| 131 | {
|
---|
| 132 | irq_spinlock_lock(&CPU->lock, false);
|
---|
| 133 | uint64_t now = get_cycle();
|
---|
| 134 | CPU->busy_cycles += now - CPU->last_cycle;
|
---|
| 135 | CPU->last_cycle = now;
|
---|
| 136 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[70527f1] | 139 | /** Clock routine
|
---|
| 140 | *
|
---|
| 141 | * Clock routine executed from clock interrupt handler
|
---|
[22f7769] | 142 | * (assuming interrupts_disable()'d). Runs expired timeouts
|
---|
[70527f1] | 143 | * and preemptive scheduling.
|
---|
| 144 | *
|
---|
[f761f1eb] | 145 | */
|
---|
| 146 | void clock(void)
|
---|
| 147 | {
|
---|
[98000fb] | 148 | size_t missed_clock_ticks = CPU->missed_clock_ticks;
|
---|
[a35b458] | 149 |
|
---|
[d0c82c5] | 150 | /* Account CPU usage */
|
---|
| 151 | cpu_update_accounting();
|
---|
[a35b458] | 152 |
|
---|
[f761f1eb] | 153 | /*
|
---|
| 154 | * To avoid lock ordering problems,
|
---|
| 155 | * run all expired timeouts as you visit them.
|
---|
[da1bafb] | 156 | *
|
---|
[f761f1eb] | 157 | */
|
---|
[da1bafb] | 158 | size_t i;
|
---|
[8cf8ee6] | 159 | for (i = 0; i <= missed_clock_ticks; i++) {
|
---|
[d0c82c5] | 160 | /* Update counters and accounting */
|
---|
[d6e5cbc] | 161 | clock_update_counters();
|
---|
[d0c82c5] | 162 | cpu_update_accounting();
|
---|
[a35b458] | 163 |
|
---|
[da1bafb] | 164 | irq_spinlock_lock(&CPU->timeoutlock, false);
|
---|
[a35b458] | 165 |
|
---|
[da1bafb] | 166 | link_t *cur;
|
---|
[55b77d9] | 167 | while ((cur = list_first(&CPU->timeout_active_list)) != NULL) {
|
---|
| 168 | timeout_t *timeout = list_get_instance(cur, timeout_t,
|
---|
| 169 | link);
|
---|
[a35b458] | 170 |
|
---|
[da1bafb] | 171 | irq_spinlock_lock(&timeout->lock, false);
|
---|
| 172 | if (timeout->ticks-- != 0) {
|
---|
| 173 | irq_spinlock_unlock(&timeout->lock, false);
|
---|
[c93e805] | 174 | break;
|
---|
| 175 | }
|
---|
[a35b458] | 176 |
|
---|
[da1bafb] | 177 | list_remove(cur);
|
---|
| 178 | timeout_handler_t handler = timeout->handler;
|
---|
| 179 | void *arg = timeout->arg;
|
---|
| 180 | timeout_reinitialize(timeout);
|
---|
[a35b458] | 181 |
|
---|
[da1bafb] | 182 | irq_spinlock_unlock(&timeout->lock, false);
|
---|
| 183 | irq_spinlock_unlock(&CPU->timeoutlock, false);
|
---|
[a35b458] | 184 |
|
---|
[da1bafb] | 185 | handler(arg);
|
---|
[a35b458] | 186 |
|
---|
[da1bafb] | 187 | irq_spinlock_lock(&CPU->timeoutlock, false);
|
---|
[c93e805] | 188 | }
|
---|
[a35b458] | 189 |
|
---|
[da1bafb] | 190 | irq_spinlock_unlock(&CPU->timeoutlock, false);
|
---|
[f761f1eb] | 191 | }
|
---|
[c93e805] | 192 | CPU->missed_clock_ticks = 0;
|
---|
[a35b458] | 193 |
|
---|
[f761f1eb] | 194 | /*
|
---|
[43114c5] | 195 | * Do CPU usage accounting and find out whether to preempt THREAD.
|
---|
[da1bafb] | 196 | *
|
---|
[f761f1eb] | 197 | */
|
---|
[a35b458] | 198 |
|
---|
[43114c5] | 199 | if (THREAD) {
|
---|
[7f1c620] | 200 | uint64_t ticks;
|
---|
[a35b458] | 201 |
|
---|
[da1bafb] | 202 | irq_spinlock_lock(&CPU->lock, false);
|
---|
[8cf8ee6] | 203 | CPU->needs_relink += 1 + missed_clock_ticks;
|
---|
[da1bafb] | 204 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
[a35b458] | 205 |
|
---|
[da1bafb] | 206 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[8cf8ee6] | 207 | if ((ticks = THREAD->ticks)) {
|
---|
| 208 | if (ticks >= 1 + missed_clock_ticks)
|
---|
| 209 | THREAD->ticks -= 1 + missed_clock_ticks;
|
---|
| 210 | else
|
---|
| 211 | THREAD->ticks = 0;
|
---|
| 212 | }
|
---|
[da1bafb] | 213 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[a35b458] | 214 |
|
---|
[057e77f] | 215 | if (ticks == 0 && PREEMPTION_ENABLED) {
|
---|
[f761f1eb] | 216 | scheduler();
|
---|
[3ff2b54] | 217 | #ifdef CONFIG_UDEBUG
|
---|
| 218 | /*
|
---|
| 219 | * Give udebug chance to stop the thread
|
---|
[5d9430d7] | 220 | * before it begins executing userspace code.
|
---|
[3ff2b54] | 221 | */
|
---|
[da1bafb] | 222 | istate_t *istate = THREAD->udebug.uspace_state;
|
---|
| 223 | if ((istate) && (istate_from_uspace(istate)))
|
---|
[3ff2b54] | 224 | udebug_before_thread_runs();
|
---|
| 225 | #endif
|
---|
[f761f1eb] | 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
[b45c443] | 229 |
|
---|
[1bb2e7a] | 230 | /** @}
|
---|
[b45c443] | 231 | */
|
---|