/* * 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 /* * This will keep track of how many ticks the system * has been running for */ static volatile uint64_t timer_ticks = 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) { /* Increment our 'tick counter' */ timer_ticks++; /* * 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; while (timer_ticks < eticks) reschedule(); } #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' to IRQ0 */ irq_install_handler(0, timer_handler); if (!has_apic()) { /* set default timer */ outportb(0x43, 0x34); start = rdtsc(); while(rdtsc() - start < 1000000) ; outportb(0x40, LATCH(TIMER_FREQ) & 0xFF); /* low byte */ start = rdtsc(); while(rdtsc() - start < 1000000) ; outportb(0x40, LATCH(TIMER_FREQ) >> 8); /* high byte */ } return 0; }