2011-08-05 08:25:00 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
time.h
|
|
|
|
Handles interrupts whenever some time has passed and provides useful
|
|
|
|
time-related functions to the kernel.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "time.h"
|
2011-09-07 12:45:07 -04:00
|
|
|
#include "interrupt.h"
|
2011-09-21 14:52:29 -04:00
|
|
|
#include "process.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "scheduler.h"
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/log.h>
|
2011-09-21 14:52:29 -04:00
|
|
|
#include "sound.h"
|
2011-11-28 10:28:56 -05:00
|
|
|
#include "syscall.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-08 05:10:41 -04:00
|
|
|
#ifdef PLATFORM_SERIAL
|
2011-11-20 09:58:42 -05:00
|
|
|
#include "serialterminal.h"
|
2011-09-08 05:10:41 -04:00
|
|
|
#endif
|
|
|
|
|
2011-08-05 08:25:00 -04:00
|
|
|
#if !defined(PLATFORM_X86_FAMILY)
|
|
|
|
#error No time subsystem is available for this CPU
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
namespace Time
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
uintmax_t ticks;
|
|
|
|
uintmax_t microsecondssinceboot;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
const uint32_t Frequency = 100; // 100 Hz
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
uintmax_t MicrosecondsSinceBoot()
|
|
|
|
{
|
|
|
|
return microsecondssinceboot;
|
|
|
|
}
|
|
|
|
|
2012-04-30 11:23:43 -04:00
|
|
|
extern "C" void RequestIRQ0()
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
|
|
|
// The value we send to the PIT is the value to divide it's input clock
|
|
|
|
// (1193180 Hz) by, to get our required frequency. Important to note is
|
|
|
|
// that the divisor must be small enough to fit into 16-bits.
|
|
|
|
const uint32_t Divisor = 1193180 / Frequency;
|
|
|
|
|
|
|
|
// Send the command byte.
|
|
|
|
CPU::OutPortB(0x43, 0x36);
|
|
|
|
|
|
|
|
// Divisor has to be sent byte-wise, so split here into upper/lower bytes.
|
|
|
|
uint8_t L = (uint8_t) (Divisor & 0xFF);
|
|
|
|
uint8_t H = (uint8_t) ((Divisor>>8) & 0xFF);
|
|
|
|
|
|
|
|
// Send the frequency divisor.
|
|
|
|
CPU::OutPortB(0x40, L);
|
|
|
|
CPU::OutPortB(0x40, H);
|
|
|
|
}
|
|
|
|
|
2011-08-28 10:59:07 -04:00
|
|
|
bool didUglyIRQ0Hack;
|
|
|
|
|
2011-12-04 15:27:21 -05:00
|
|
|
int SysUptime(uintmax_t* usecssinceboot)
|
2011-11-28 10:28:56 -05:00
|
|
|
{
|
2011-12-04 15:27:21 -05:00
|
|
|
// TODO: Validate that usecssinceboot is a valid user-space pointer.
|
|
|
|
*usecssinceboot = microsecondssinceboot;
|
2011-11-28 10:28:56 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-28 10:59:07 -04:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
// Initialize our variables.
|
2011-09-21 14:52:29 -04:00
|
|
|
ticks = 0;
|
|
|
|
microsecondssinceboot = 0;
|
2011-08-28 10:59:07 -04:00
|
|
|
|
|
|
|
// First, register our timer callback.
|
2012-01-15 12:32:42 -05:00
|
|
|
Interrupt::RegisterHandler(Interrupt::IRQ0, &OnIRQ0, NULL);
|
2011-08-28 10:59:07 -04:00
|
|
|
|
2011-11-28 10:28:56 -05:00
|
|
|
Syscall::Register(SYSCALL_UPTIME, (void*) SysUptime);
|
|
|
|
|
2011-08-28 10:59:07 -04:00
|
|
|
didUglyIRQ0Hack = false;
|
|
|
|
|
2012-04-30 11:23:43 -04:00
|
|
|
RequestIRQ0();
|
2011-08-28 10:59:07 -04:00
|
|
|
}
|
|
|
|
|
2012-01-15 12:32:42 -05:00
|
|
|
void OnIRQ0(CPU::InterruptRegisters* Regs, void* /*user*/)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-08 05:10:41 -04:00
|
|
|
#ifdef PLATFORM_SERIAL
|
2011-11-20 09:58:42 -05:00
|
|
|
SerialTerminal::OnTick();
|
2011-09-08 05:10:41 -04:00
|
|
|
#endif
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
ticks++;
|
|
|
|
microsecondssinceboot += (1000*1000)/Frequency;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
// Let the scheduler switch to the next task.
|
|
|
|
// TODO: Let the scheduler know how long has passed.
|
2011-09-21 14:52:29 -04:00
|
|
|
Scheduler::Switch(Regs);
|
2011-08-28 10:59:07 -04:00
|
|
|
|
|
|
|
// TODO: There is a horrible bug that causes Sortix to only receive
|
2012-03-21 19:52:29 -04:00
|
|
|
// one IRQ0 on my laptop, but it works in virtual machines. But
|
2011-08-28 10:59:07 -04:00
|
|
|
// re-requesting an addtional time seems to work. Hacky and ugly.
|
2012-04-30 11:23:43 -04:00
|
|
|
if ( !didUglyIRQ0Hack ) { RequestIRQ0(); didUglyIRQ0Hack = true; }
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-11-28 10:28:56 -05:00
|
|
|
// TODO: Implement all the other useful functions regarding time.
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|