sortix-mirror/sortix/time.cpp

96 lines
2.3 KiB
C++
Raw Normal View History

/*******************************************************************************
2011-08-05 12:25:00 +00:00
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
2011-08-05 12:25:00 +00:00
This file is part of Sortix.
2011-08-05 12:25:00 +00: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 12:25:00 +00: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 12:25:00 +00: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 12:25:00 +00:00
time.cpp
Handles interrupts whenever some time has passed and provides useful
time-related functions to the kernel.
2011-08-05 12:25:00 +00:00
*******************************************************************************/
2011-08-05 12:25:00 +00:00
#include <sys/types.h>
#include <errno.h>
#include <timespec.h>
#include <sortix/clock.h>
#include <sortix/kernel/platform.h>
2013-01-08 23:41:35 +00:00
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/copy.h>
2013-01-09 22:30:36 +00:00
#include <sortix/kernel/interrupt.h>
2013-10-13 21:56:58 +00:00
#include <sortix/kernel/clock.h>
#include <sortix/kernel/time.h>
2013-01-09 09:47:22 +00:00
#include <sortix/kernel/scheduler.h>
2013-01-08 23:41:35 +00: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 18:52:29 +00:00
#include "sound.h"
2011-08-05 12:25:00 +00:00
#ifdef PLATFORM_SERIAL
2011-11-20 14:58:42 +00:00
#include "serialterminal.h"
#endif
namespace Sortix {
namespace Time {
2011-08-05 12:25:00 +00:00
2013-10-13 21:56:58 +00:00
void CPUInit();
2011-08-05 12:25:00 +00:00
2013-10-13 21:56:58 +00:00
Clock* realtime_clock;
Clock* uptime_clock;
2013-10-13 21:56:58 +00:00
Clock* GetClock(clockid_t clock)
{
2013-10-13 21:56:58 +00:00
switch ( clock )
{
2013-10-13 21:56:58 +00:00
case CLOCK_REALTIME: return realtime_clock;
case CLOCK_BOOT: return uptime_clock;
case CLOCK_INIT: return uptime_clock;
case CLOCK_MONOTONIC: return uptime_clock;
default: return errno = ENOTSUP, (Clock*) NULL;
}
}
2013-10-13 21:56:58 +00:00
struct timespec Get(clockid_t clockid)
{
2013-10-13 21:56:58 +00:00
Clock* clock = GetClock(clockid);
if ( clock )
{
2013-10-13 21:56:58 +00:00
struct timespec now;
clock->Get(&now, NULL);
return now;
}
2013-10-13 21:56:58 +00:00
return timespec_nul();
}
2013-10-13 21:56:58 +00:00
void OnTick(struct timespec tick_period)
{
2013-10-13 21:56:58 +00:00
realtime_clock->Advance(tick_period);
uptime_clock->Advance(tick_period);
}
2011-08-05 12:25:00 +00:00
void Init()
{
2013-10-13 21:56:58 +00:00
if ( !(realtime_clock = new Clock()) )
Panic("Unable to allocate realtime clock");
if ( !(uptime_clock = new Clock()) )
Panic("Unable to allocate uptime clock");
CPUInit();
2011-08-05 12:25:00 +00:00
}
} // namespace Time
} // namespace Sortix