[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 |
|
---|
[1bb2e7a] | 29 | /** @addtogroup time
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf26ba9] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[cf26ba9] | 35 | * @brief High-level clock interrupt handler.
|
---|
| 36 | *
|
---|
| 37 | * This file contains the clock() function which is the source
|
---|
| 38 | * of preemption. It is also responsible for executing expired
|
---|
| 39 | * timeouts.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
[f761f1eb] | 42 | #include <time/clock.h>
|
---|
| 43 | #include <time/timeout.h>
|
---|
| 44 | #include <arch/types.h>
|
---|
| 45 | #include <config.h>
|
---|
| 46 | #include <synch/spinlock.h>
|
---|
| 47 | #include <synch/waitq.h>
|
---|
| 48 | #include <func.h>
|
---|
| 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>
|
---|
| 56 | #include <arch/barrier.h>
|
---|
[f8ddd17] | 57 | #include <mm/frame.h>
|
---|
| 58 | #include <ddi/ddi.h>
|
---|
| 59 |
|
---|
| 60 | /** Physical memory area of the real time clock. */
|
---|
| 61 | static parea_t clock_parea;
|
---|
[d6e5cbc] | 62 |
|
---|
| 63 | /* Pointers to public variables with time */
|
---|
| 64 | struct ptime {
|
---|
[7f1c620] | 65 | unative_t seconds1;
|
---|
| 66 | unative_t useconds;
|
---|
| 67 | unative_t seconds2;
|
---|
[d6e5cbc] | 68 | };
|
---|
| 69 | struct ptime *public_time;
|
---|
| 70 | /* Variable holding fragment of second, so that we would update
|
---|
| 71 | * seconds correctly
|
---|
| 72 | */
|
---|
[7f1c620] | 73 | static unative_t secfrag = 0;
|
---|
[d6e5cbc] | 74 |
|
---|
| 75 | /** Initialize realtime clock counter
|
---|
| 76 | *
|
---|
| 77 | * The applications (and sometimes kernel) need to access accurate
|
---|
| 78 | * information about realtime data. We allocate 1 page with these
|
---|
| 79 | * data and update it periodically.
|
---|
| 80 | */
|
---|
| 81 | void clock_counter_init(void)
|
---|
| 82 | {
|
---|
| 83 | void *faddr;
|
---|
| 84 |
|
---|
[f8ddd17] | 85 | faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
|
---|
[d6e5cbc] | 86 | if (!faddr)
|
---|
| 87 | panic("Cannot allocate page for clock");
|
---|
| 88 |
|
---|
[f8ddd17] | 89 | public_time = (struct ptime *) PA2KA(faddr);
|
---|
[d6e5cbc] | 90 |
|
---|
| 91 | /* TODO: We would need some arch dependent settings here */
|
---|
[fd8302d] | 92 | public_time->seconds1 = 0;
|
---|
| 93 | public_time->seconds2 = 0;
|
---|
[d6e5cbc] | 94 | public_time->useconds = 0;
|
---|
| 95 |
|
---|
[f8ddd17] | 96 | clock_parea.pbase = (uintptr_t) faddr;
|
---|
| 97 | clock_parea.vbase = (uintptr_t) public_time;
|
---|
| 98 | clock_parea.frames = 1;
|
---|
| 99 | clock_parea.cacheable = true;
|
---|
| 100 | ddi_parea_register(&clock_parea);
|
---|
| 101 |
|
---|
| 102 | /*
|
---|
| 103 | * Prepare information for the userspace so that it can successfully
|
---|
| 104 | * physmem_map() the clock_parea.
|
---|
| 105 | */
|
---|
| 106 | sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true);
|
---|
| 107 | sysinfo_set_item_val("clock.fcolor", NULL, (unative_t)
|
---|
| 108 | PAGE_COLOR(clock_parea.vbase));
|
---|
| 109 | sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr);
|
---|
[d6e5cbc] | 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | /** Update public counters
|
---|
| 114 | *
|
---|
| 115 | * Update it only on first processor
|
---|
| 116 | * TODO: Do we really need so many write barriers?
|
---|
| 117 | */
|
---|
| 118 | static void clock_update_counters(void)
|
---|
| 119 | {
|
---|
| 120 | if (CPU->id == 0) {
|
---|
| 121 | secfrag += 1000000/HZ;
|
---|
| 122 | if (secfrag >= 1000000) {
|
---|
[fd8302d] | 123 | secfrag -= 1000000;
|
---|
| 124 | public_time->seconds1++;
|
---|
[d6e5cbc] | 125 | write_barrier();
|
---|
[fd8302d] | 126 | public_time->useconds = secfrag;
|
---|
| 127 | write_barrier();
|
---|
| 128 | public_time->seconds2 = public_time->seconds1;
|
---|
[d6e5cbc] | 129 | } else
|
---|
| 130 | public_time->useconds += 1000000/HZ;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
[f761f1eb] | 133 |
|
---|
[70527f1] | 134 | /** Clock routine
|
---|
| 135 | *
|
---|
| 136 | * Clock routine executed from clock interrupt handler
|
---|
[22f7769] | 137 | * (assuming interrupts_disable()'d). Runs expired timeouts
|
---|
[70527f1] | 138 | * and preemptive scheduling.
|
---|
| 139 | *
|
---|
[f761f1eb] | 140 | */
|
---|
| 141 | void clock(void)
|
---|
| 142 | {
|
---|
| 143 | link_t *l;
|
---|
| 144 | timeout_t *h;
|
---|
[ba1b2194] | 145 | timeout_handler_t f;
|
---|
[f761f1eb] | 146 | void *arg;
|
---|
[8cf8ee6] | 147 | count_t missed_clock_ticks = CPU->missed_clock_ticks;
|
---|
[c93e805] | 148 | int i;
|
---|
[f761f1eb] | 149 |
|
---|
| 150 | /*
|
---|
| 151 | * To avoid lock ordering problems,
|
---|
| 152 | * run all expired timeouts as you visit them.
|
---|
| 153 | */
|
---|
[8cf8ee6] | 154 | for (i = 0; i <= missed_clock_ticks; i++) {
|
---|
[d6e5cbc] | 155 | clock_update_counters();
|
---|
[c93e805] | 156 | spinlock_lock(&CPU->timeoutlock);
|
---|
| 157 | while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
|
---|
| 158 | h = list_get_instance(l, timeout_t, link);
|
---|
| 159 | spinlock_lock(&h->lock);
|
---|
| 160 | if (h->ticks-- != 0) {
|
---|
| 161 | spinlock_unlock(&h->lock);
|
---|
| 162 | break;
|
---|
| 163 | }
|
---|
| 164 | list_remove(l);
|
---|
| 165 | f = h->handler;
|
---|
| 166 | arg = h->arg;
|
---|
| 167 | timeout_reinitialize(h);
|
---|
| 168 | spinlock_unlock(&h->lock);
|
---|
| 169 | spinlock_unlock(&CPU->timeoutlock);
|
---|
[f761f1eb] | 170 |
|
---|
[c93e805] | 171 | f(arg);
|
---|
[f761f1eb] | 172 |
|
---|
[c93e805] | 173 | spinlock_lock(&CPU->timeoutlock);
|
---|
| 174 | }
|
---|
| 175 | spinlock_unlock(&CPU->timeoutlock);
|
---|
[f761f1eb] | 176 | }
|
---|
[c93e805] | 177 | CPU->missed_clock_ticks = 0;
|
---|
[f761f1eb] | 178 |
|
---|
| 179 | /*
|
---|
[43114c5] | 180 | * Do CPU usage accounting and find out whether to preempt THREAD.
|
---|
[f761f1eb] | 181 | */
|
---|
| 182 |
|
---|
[43114c5] | 183 | if (THREAD) {
|
---|
[7f1c620] | 184 | uint64_t ticks;
|
---|
[dbe9ff0] | 185 |
|
---|
[43114c5] | 186 | spinlock_lock(&CPU->lock);
|
---|
[8cf8ee6] | 187 | CPU->needs_relink += 1 + missed_clock_ticks;
|
---|
[43114c5] | 188 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 189 |
|
---|
[43114c5] | 190 | spinlock_lock(&THREAD->lock);
|
---|
[8cf8ee6] | 191 | if ((ticks = THREAD->ticks)) {
|
---|
| 192 | if (ticks >= 1 + missed_clock_ticks)
|
---|
| 193 | THREAD->ticks -= 1 + missed_clock_ticks;
|
---|
| 194 | else
|
---|
| 195 | THREAD->ticks = 0;
|
---|
| 196 | }
|
---|
[dbe9ff0] | 197 | spinlock_unlock(&THREAD->lock);
|
---|
| 198 |
|
---|
| 199 | if (!ticks && !PREEMPTION_DISABLED) {
|
---|
[f761f1eb] | 200 | scheduler();
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | }
|
---|
[b45c443] | 205 |
|
---|
[1bb2e7a] | 206 | /** @}
|
---|
[b45c443] | 207 | */
|
---|