1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/sortix/time.cpp

177 lines
4.6 KiB
C++
Raw Normal View History

/*******************************************************************************
2011-08-05 08:25:00 -04:00
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
2011-08-05 08:25:00 -04:00
This file is part of Sortix.
2011-08-05 08:25:00 -04:00
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.
2011-08-05 08:25:00 -04:00
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.
2011-08-05 08:25:00 -04:00
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
2011-08-05 08:25:00 -04:00
time.cpp
Handles interrupts whenever some time has passed and provides useful
time-related functions to the kernel.
2011-08-05 08:25:00 -04:00
*******************************************************************************/
2011-08-05 08:25:00 -04:00
#include <sys/types.h>
#include <errno.h>
#include <timespec.h>
#include <sortix/clock.h>
#include <sortix/timespec.h>
#include <sortix/kernel/platform.h>
2013-01-08 18:41:35 -05:00
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/copy.h>
2013-01-09 17:30:36 -05:00
#include <sortix/kernel/interrupt.h>
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/time.h>
2013-01-09 04:47:22 -05:00
#include <sortix/kernel/scheduler.h>
2013-01-08 18:41:35 -05:00
Implemented the fork() system call and what it needed to work properly. This commit got completely out of control. Added the fork(), getpid(), getppid(), sleep(), usleep() system calls, and aliases in the Maxsi:: namespace. Fixed a bug where zero-byte allocation would fail. Worked on the DescriptorTable class which now works and can fork. Got rid of some massive print-registers statements and replaced them with the portable InterruptRegisters::LogRegisters() function. Removed the SysExecuteOld function and replaced it with Process::Execute(). Rewrote the boot sequence in kernel.cpp such that it now loads the system idle process 'idle' as PID 0, and the initization process 'init' as PID 1. Rewrote the SIGINT hack. Processes now maintain a family-tree structure and keep track of their threads. PIDs are now allocated using a simple hack. Virtual memory per-process can now be allocated using a simple hack. Processes can now be forked. Fixed the Process::Execute function such that it now resets the stack pointer to where the stack actually is - not just a magic value. Removed the old and ugly Process::_endcodesection hack. Rewrote the scheduler into a much cleaner and faster version. Debug code is now moved to designated functions. The noop kernel-thread has been replaced by a simple user-space infinite-loop program 'idle'. The Thread class has been seperated from the Scheduler except in Scheduler- related code. Thread::{Save,Load}Registers has been improved and has been moved to $(CPU)/thread.cpp. Threads can now be forked. A new CreateThread function creates threads properly and portably. Added a MicrosecondsSinceBoot() function. Fixed a crucial bug in MemoryManagement::Fork(). Added an 'idle' user-space program that is a noop infinite loop, which is used by the scheduler when there is nothing to do. Rewrote the 'init' program such that it now forks off a shell, instead of becoming the shell. Added the $$ (current PID) and $PPID (parent PPID) variables to the shell.
2011-09-21 14:52:29 -04:00
#include "process.h"
#include "sound.h"
2011-08-05 08:25:00 -04:00
#ifdef PLATFORM_SERIAL
2011-11-20 09:58:42 -05:00
#include "serialterminal.h"
#endif
#include "cpu.h"
2011-08-05 08:25:00 -04:00
#if !defined(PLATFORM_X86_FAMILY)
#error Provide a time implementation for this CPU.
2011-08-05 08:25:00 -04:00
#endif
namespace Sortix {
namespace Time {
2011-08-05 08:25:00 -04:00
struct timespec since_boot;
struct timespec since_boot_period;
uint16_t since_boot_divisor;
2011-08-05 08:25:00 -04:00
static uint16_t DivisorOfFrequency(long frequency)
{
// The value we send to the PIT is the value to divide it's input clock
// (1193180 Hz) by, to get our required frequency. Note that the divisor
// must be small enough to fit into 16 bits.
return 1193180 / frequency;
}
static long FrequencyOfDivisor(uint16_t divisor)
{
return 1193180 / divisor;
}
2011-11-28 10:28:56 -05:00
static long RealFrequencyOfFrequency(long frequency)
{
return FrequencyOfDivisor(DivisorOfFrequency(frequency));
}
static struct timespec PeriodOfFrequency(long frequency)
{
long period_ns = 1000000000L / frequency;
return timespec_make(0, period_ns);
}
static void RequestIRQ0(uint16_t divisor)
{
CPU::OutPortB(0x43, 0x36);
CPU::OutPortB(0x40, divisor >> 0 & 0xFF);
CPU::OutPortB(0x40, divisor >> 8 & 0xFF);
}
2011-11-28 10:28:56 -05:00
static void InitializeTimer(long frequency)
{
long real_frequence = RealFrequencyOfFrequency(frequency);
since_boot_divisor = DivisorOfFrequency(frequency);
since_boot = timespec_nul();
since_boot_period = PeriodOfFrequency(real_frequence);
RequestIRQ0(since_boot_divisor);
}
static void OnIRQ0(CPU::InterruptRegisters* regs, void* /*user*/)
{
since_boot = timespec_add(since_boot, since_boot_period);
Scheduler::Switch(regs);
// TODO: There is a horrible bug that causes Sortix to only receive
// one IRQ0 on my laptop, but it works in virtual machines. But
// re-requesting an addtional time seems to work. Hacky and ugly.
static bool did_ugly_irq0_hack = false;
if ( !did_ugly_irq0_hack )
{
RequestIRQ0(since_boot_divisor);
did_ugly_irq0_hack = true;
}
}
bool TryGet(clockid_t clock, struct timespec* time, struct timespec* res)
{
switch ( clock )
{
case CLOCK_REALTIME:
return errno = ENOTSUP, false;
case CLOCK_MONOTONIC:
case CLOCK_BOOT:
case CLOCK_INIT:
// TODO: Is is possible to implement the below as atomic operations?
Interrupt::Disable();
if ( time )
*time = since_boot;
if ( res )
*res = since_boot_period;
Interrupt::Enable();
return true;
default:
return errno = EINVAL, false;
}
}
struct timespec Get(clockid_t clock, struct timespec* res)
{
struct timespec ret;
if ( !TryGet(clock, &ret, res) )
return timespec_nul();
return ret;
}
2011-08-05 08:25:00 -04:00
uintmax_t MicrosecondsOfTimespec(struct timespec time)
{
uintmax_t seconds = time.tv_sec;
uintmax_t nano_seconds = time.tv_nsec;
return seconds * 1000000 + nano_seconds / 1000;
}
// TODO: This system call is for compatibility. Provide user-space with better
// facilities such as sys_clock_gettime.
static int sys_uptime(uintmax_t* usecssinceboot)
{
struct timespec now;
if ( !TryGet(CLOCK_BOOT, &now, NULL) )
return -1;
uintmax_t ret = MicrosecondsOfTimespec(now);
if ( !CopyToUser(usecssinceboot, &ret, sizeof(ret)) )
return -1;
return 0;
}
2011-08-05 08:25:00 -04:00
void Init()
{
Interrupt::RegisterHandler(Interrupt::IRQ0, &OnIRQ0, NULL);
Syscall::Register(SYSCALL_UPTIME, (void*) sys_uptime);
InitializeTimer(100/*Hz*/);
2011-08-05 08:25:00 -04:00
}
} // namespace Time
} // namespace Sortix