/* * Copyright 2010 Stefan Lankes, Chair for Operating Systems, * RWTH Aachen University * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This file is part of MetalSVM. */ #include #include #include #include #include #include #include #include #include #include #include #include typedef struct { uint8_t active; uint64_t timeout; } timer_t; static timer_t timers[MAX_TASKS] = {[0 ... MAX_TASKS-1] = {0, 0}}; static spinlock_irqsave_t timers_lock = SPINLOCK_IRQSAVE_INIT; /* * This will keep track of how many ticks the system * has been running for */ static volatile uint64_t timer_ticks = 0; uint64_t get_clock_tick(void) { return timer_ticks; } int sys_times(struct tms* buffer, clock_t* clock) { if (BUILTIN_EXPECT(!buffer, 0)) return -EINVAL; if (BUILTIN_EXPECT(!clock, 0)) return -EINVAL; memset(buffer, 0x00, sizeof(struct tms)); *clock = buffer->tms_utime = (clock_t) ((timer_ticks - per_core(current_task)->start_tick) * CLOCKS_PER_SEC / TIMER_FREQ); return 0; } /* * Handles the timer. In this case, it's very simple: We * increment the 'timer_ticks' variable every time the * timer fires. */ static void timer_handler(struct state *s) { uint32_t i; /* Increment our 'tick counter' */ #if MAX_CORES > 1 if (smp_id() == 0) #endif { timer_ticks++; spinlock_irqsave_lock(&timers_lock); for(i=1; i= timers[i].timeout)) { timers[i].active = 0; wakeup_task(i); } } spinlock_irqsave_unlock(&timers_lock); } /* * Every TIMER_FREQ clocks (approximately 1 second), we will * display a message on the screen */ /*if (timer_ticks % TIMER_FREQ == 0) { vga_puts("One second has passed\n"); }*/ } /* * This will continuously loop until the given time has * been reached */ void timer_wait(unsigned int ticks) { uint64_t eticks = timer_ticks + ticks; task_t* curr_task = per_core(current_task); // Task 0 is always an idle task // Perhaps, the status is not set correctly... if ((curr_task->status == TASK_IDLE) || (curr_task->id == 0)) { while (timer_ticks < eticks) { check_workqueues(); // recheck break condition if (timer_ticks >= eticks) break; HALT; } } else if (timer_ticks < eticks) { check_workqueues(); spinlock_irqsave_lock(&timers_lock); if (timer_ticks < eticks) { timers[curr_task->id].active = 1; timers[curr_task->id].timeout = eticks; block_task(curr_task->id); spinlock_irqsave_unlock(&timers_lock); reschedule(); } else spinlock_irqsave_unlock(&timers_lock); } } #define LATCH(f) ((CLOCK_TICK_RATE + f/2) / f) /* * Sets up the system clock by installing the timer handler * into IRQ0 */ int timer_init(void) { uint64_t start; /* * Installs 'timer_handler' for the PIC and APIC timer, * only one handler will be later used. */ irq_install_handler(32, timer_handler); irq_install_handler(123, timer_handler); /* * The Rock Creek processor doesn't posseess an tradional PIC. * Therefore, we have not to configure the PIC timer. */ #ifndef CONFIG_ROCKCREEK outportb(0x43, 0x34); /* before we write to 0x40, we wait some time */ start = rdtsc(); while(rdtsc() - start < 1000000) ; outportb(0x40, LATCH(TIMER_FREQ) & 0xFF); /* low byte */ /* before we write to 0x40, we wait some time */ start = rdtsc(); while(rdtsc() - start < 1000000) ; outportb(0x40, LATCH(TIMER_FREQ) >> 8); /* high byte */ #endif return 0; }