1
0
Fork 0
mirror of https://github.com/hermitcore/libhermit.git synced 2025-03-09 00:00:03 +01:00
libhermit/arch/x86_64/kernel/timer.c

217 lines
5.6 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) 2010, Stefan Lankes, RWTH Aachen University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <hermit/stdio.h>
#include <hermit/string.h>
#include <hermit/processor.h>
#include <hermit/time.h>
#include <hermit/tasks.h>
#include <hermit/errno.h>
#include <hermit/spinlock.h>
#include <hermit/logging.h>
#include <asm/irq.h>
#include <asm/irqflags.h>
#include <asm/io.h>
/*
* This will keep track of how many ticks the system
* has been running for
*/
DEFINE_PER_CORE(uint64_t, timer_ticks, 0);
extern uint32_t cpu_freq;
2015-07-25 00:21:57 +02:00
extern int32_t boot_processor;
#ifdef DYNAMIC_TICKS
DEFINE_PER_CORE(uint64_t, last_rdtsc, 0);
uint64_t boot_tsc __attribute__ ((section(".data"))) = 0;
2015-07-25 00:21:57 +02:00
void check_ticks(void)
{
// do we already know the cpu frequency? => if not, ignore this check
if (!cpu_freq)
return;
const uint64_t curr_rdtsc = rdtsc();
mb();
const uint64_t diff_cycles = curr_rdtsc - per_core(last_rdtsc);
const uint64_t cpu_freq_hz = 1000000ULL * (uint64_t) get_cpu_frequency();
const uint64_t diff_ticks = (diff_cycles * (uint64_t) TIMER_FREQ) / cpu_freq_hz;
if (diff_ticks > 0) {
set_per_core(timer_ticks, per_core(timer_ticks) + diff_ticks);
set_per_core(last_rdtsc, curr_rdtsc);
mb();
2015-07-25 00:21:57 +02:00
}
}
uint64_t get_uptime(void)
{
2018-08-07 09:53:22 +02:00
if (!cpu_freq)
return 0;
const uint64_t cpu_freq_hz = 1000000ULL * (uint64_t) get_cpu_frequency();
const uint64_t curr_tsc = rdtsc();
mb();
2018-08-07 09:53:22 +02:00
uint64_t diff = curr_tsc - boot_tsc;
return (1000ULL*diff) / cpu_freq_hz;
}
#else
uint64_t get_uptime(void)
{
return (get_clock_tick() * 1000) / TIMER_FREQ;
}
#endif
/*
* 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)
{
#ifndef DYNAMIC_TICKS
/* Increment our 'tick counter' */
set_per_core(timer_ticks, per_core(timer_ticks)+1);
#else
// do we already know the cpu frequency? => if not, use timer interrupt to count the ticks
if (!cpu_freq)
set_per_core(timer_ticks, per_core(timer_ticks)+1);
#endif
#if 0
/*
* Every TIMER_FREQ clocks (approximately 1 second), we will
* display a message on the screen
*/
if (timer_ticks % TIMER_FREQ == 0) {
LOG_INFO("One second has passed %d\n", CORE_ID);
}
#endif
}
int timer_wait(unsigned int ticks)
{
#ifdef DYNAMIC_TICKS
check_ticks();
#endif
uint64_t eticks = per_core(timer_ticks) + ticks;
task_t* curr_task = per_core(current_task);
if (curr_task->status == TASK_IDLE)
{
/*
* This will continuously loop until the given time has
* been reached
*/
while (per_core(timer_ticks) < eticks) {
check_workqueues();
// recheck break condition
if (per_core(timer_ticks) >= eticks)
break;
PAUSE;
}
} else if (per_core(timer_ticks) < eticks) {
set_timer(eticks);
reschedule();
}
return 0;
}
#define LATCH(f) ((CLOCK_TICK_RATE + f/2) / f)
2015-08-17 12:59:39 +02:00
#define WAIT_SOME_TIME() do { uint64_t start = rdtsc(); mb(); \
while(rdtsc() - start < 1000000) ; \
} while (0)
static int pit_init(void)
{
/*
* Port 0x43 is for initializing the PIT:
*
* 0x34 means the following:
* 0b... (step-by-step binary representation)
* ... 00 - channel 0
* ... 11 - write two values to counter register:
* first low-, then high-byte
* ... 010 - mode number 2: "rate generator" / frequency divider
* ... 0 - binary counter (the alternative is BCD)
*/
outportb(0x43, 0x34);
WAIT_SOME_TIME();
/* Port 0x40 is for the counter register of channel 0 */
outportb(0x40, LATCH(TIMER_FREQ) & 0xFF); /* low byte */
WAIT_SOME_TIME();
outportb(0x40, LATCH(TIMER_FREQ) >> 8); /* high byte */
return 0;
}
int clock_init(void)
{
#ifdef DYNAMIC_TICKS
if (!boot_tsc)
2018-08-07 09:53:22 +02:00
boot_tsc = rdtsc();
#endif
return 0;
}
/*
* Sets up the system clock by installing the timer handler
* into IRQ0
*/
int timer_init(void)
{
#ifdef DYNAMIC_TICKS
2018-08-07 09:53:22 +02:00
if (!per_core(last_rdtsc))
set_per_core(last_rdtsc, rdtsc());
#endif
/*
* 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);
if (cpu_freq) // do we need to configure the timer?
return 0;
return pit_init();
}