Update files

This commit is contained in:
Alex 2022-10-26 21:55:31 +03:00
parent f82aa2c444
commit 581638c256
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
9 changed files with 202 additions and 86 deletions

View File

@ -61,8 +61,8 @@ namespace ACPI
else if (Event & ACPI_POWER_BUTTON) else if (Event & ACPI_POWER_BUTTON)
{ {
this->Shutdown(); this->Shutdown();
Time tm = ReadClock(); Time::Clock tm = Time::ReadClock();
while (tm.Second == ReadClock().Second) while (tm.Second == Time::ReadClock().Second)
; ;
outw(0xB004, 0x2000); outw(0xB004, 0x2000);
outw(0x604, 0x2000); outw(0x604, 0x2000);

View File

@ -239,7 +239,7 @@ namespace APIC
} }
this->Write(APIC_TPR, 0x0); this->Write(APIC_TPR, 0x0);
this->Write(APIC_SVR, this->Read(APIC_SVR) | 0x100); // 0x1FF or 0x100 ? on https://wiki.osdev.org/APIC is 0x100 // this->Write(APIC_SVR, this->Read(APIC_SVR) | 0x100); // 0x1FF or 0x100 ? on https://wiki.osdev.org/APIC is 0x100
if (!this->x2APICSupported) if (!this->x2APICSupported)
{ {
@ -292,8 +292,8 @@ namespace APIC
LVTTimer timer = {.raw = 0}; LVTTimer timer = {.raw = 0};
timer.Vector = Vector; timer.Vector = Vector;
timer.TimerMode = 0; timer.TimerMode = 0;
this->lapic->Write(APIC_TDCR, 0x0); this->lapic->Write(APIC_TDCR, DivideBy16);
this->lapic->Write(APIC_TICR, (Ticks / 10) * Miliseconds); this->lapic->Write(APIC_TICR, Ticks * Miliseconds);
this->lapic->Write(APIC_TIMER, timer.raw); this->lapic->Write(APIC_TIMER, timer.raw);
} }
@ -301,6 +301,8 @@ namespace APIC
{ {
SmartCriticalSection(APICLock); SmartCriticalSection(APICLock);
this->lapic = apic; this->lapic = apic;
LVTTimerDivide Divider = DivideBy16;
trace("Initializing APIC timer on CPU %d", GetCurrentCPU()->ID); trace("Initializing APIC timer on CPU %d", GetCurrentCPU()->ID);
// Setup the spurrious interrupt vector // Setup the spurrious interrupt vector
@ -309,51 +311,13 @@ namespace APIC
Spurious.Software = 1; Spurious.Software = 1;
this->lapic->Write(APIC_SVR, Spurious.raw); this->lapic->Write(APIC_SVR, Spurious.raw);
this->lapic->Write(APIC_TDCR, 0x0); this->lapic->Write(APIC_TDCR, Divider);
this->lapic->Write(APIC_TICR, 0xFFFFFFFF); this->lapic->Write(APIC_TICR, 0xFFFFFFFF);
// After PIT sleep is completed. Mask the timer. TimeManager->Sleep(10);
LVTTimer masktimer = {.raw = 0x10000};
// // PIT Config
// int Count = 10000; /* µs */
// int Ticks = 1193180 / (Count / 100);
// // https://wiki.osdev.org/Programmable_Interval_Timer#I.2FO_Ports
// // PIT Prepare
// int IOIn = inb(0x61);
// IOIn = (IOIn & 0xFD) | 1;
// outb(0x61, IOIn);
// outb(0x43, 178);
// outb(0x40, Ticks & 0xff);
// inb(0x60);
// outb(0x40, Ticks >> 8);
// // PIT Start
// IOIn = inb(0x61);
// IOIn = (IOIn & 0xFC);
// outb(0x61, IOIn);
// IOIn |= 1;
// outb(0x61, IOIn);
// uint32_t Loop = 0;
// while ((inb(0x61) & 0x20) != 0)
// ++Loop;
// 10000ms
for (int i = 0; i < 10000; i++)
inb(0x80); // 1µs
// Mask the timer // Mask the timer
this->lapic->Write(APIC_TIMER, masktimer.raw); this->lapic->Write(APIC_TIMER, 0x10000 /* LVTTimer.Mask flag */);
// // Disable the PIT
// outb(0x43, 0x28);
// outb(0x40, 0x0);
// outb(0x21, 0xFF);
// outb(0xA1, 0xFF);
Ticks = 0xFFFFFFFF - this->lapic->Read(APIC_TCCR); Ticks = 0xFFFFFFFF - this->lapic->Read(APIC_TCCR);
// Config for IRQ0 timer // Config for IRQ0 timer
@ -363,7 +327,7 @@ namespace APIC
timer.TimerMode = Periodic; timer.TimerMode = Periodic;
// Initialize APIC timer // Initialize APIC timer
this->lapic->Write(APIC_TDCR, 0x0); this->lapic->Write(APIC_TDCR, Divider);
this->lapic->Write(APIC_TICR, Ticks); this->lapic->Write(APIC_TICR, Ticks);
this->lapic->Write(APIC_TIMER, timer.raw); this->lapic->Write(APIC_TIMER, timer.raw);
trace("%d APIC Timer %d ticks in.", GetCurrentCPU()->ID, Ticks); trace("%d APIC Timer %d ticks in.", GetCurrentCPU()->ID, Ticks);

View File

@ -1,9 +1,11 @@
#include <time.hpp> #include <time.hpp>
#include <io.h> #include <io.h>
Time ReadClock() namespace Time
{ {
Time tm; Clock ReadClock()
{
Clock tm;
#if defined(__amd64__) || defined(__i386__) #if defined(__amd64__) || defined(__i386__)
uint32_t t = 0; uint32_t t = 0;
outb(0x70, 0x00); outb(0x70, 0x00);
@ -35,4 +37,5 @@ Time ReadClock()
tm.Counter = 0; tm.Counter = 0;
#endif #endif
return tm; return tm;
}
} }

55
Core/Timer.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <time.hpp>
#include <memory.hpp>
#include <debug.h>
#include <io.h>
#if defined(__amd64__)
#include "../Architecture/amd64/acpi.hpp"
#elif defined(__i386__)
#elif defined(__aarch64__)
#endif
namespace Time
{
void time::Sleep(uint64_t Milliseconds)
{
uint64_t Target = mminq(&((HPET *)hpet)->MainCounterValue) + (Milliseconds * 1000000000) / clk;
while (mminq(&((HPET *)hpet)->MainCounterValue) < Target)
;
}
time::time(void *_acpi)
{
if (_acpi)
{
#if defined(__amd64__)
this->acpi = _acpi;
ACPI::ACPI *acpi = (ACPI::ACPI *)this->acpi;
if (acpi->HPET)
{
Memory::Virtual().Map((void *)acpi->HPET->Address.Address,
(void *)acpi->HPET->Address.Address,
Memory::PTFlag::RW | Memory::PTFlag::PCD);
this->hpet = (void *)acpi->HPET->Address.Address;
HPET *hpet = (HPET *)this->hpet;
trace("%s timer is at address %016p", acpi->HPET->Header.OEMID, (void *)acpi->HPET->Address.Address);
clk = hpet->GeneralCapabilities >> 32;
mmoutq(&hpet->GeneralConfiguration, 0);
mmoutq(&hpet->MainCounterValue, 0);
mmoutq(&hpet->GeneralConfiguration, 1);
}
else
{
trace("HPET not found");
}
#elif defined(__i386__)
#elif defined(__aarch64__)
#endif
}
}
time::~time()
{
}
}

View File

@ -6,7 +6,6 @@
#include <convert.h> #include <convert.h>
#include <printf.h> #include <printf.h>
#include <lock.hpp> #include <lock.hpp>
#include <time.hpp>
#include <debug.h> #include <debug.h>
#include <smp.hpp> #include <smp.hpp>
#include <cargs.h> #include <cargs.h>
@ -20,9 +19,10 @@ SymbolResolver::Symbols *KernelSymbolTable = nullptr;
Power::Power *PowerManager = nullptr; Power::Power *PowerManager = nullptr;
PCI::PCI *PCIManager = nullptr; PCI::PCI *PCIManager = nullptr;
Tasking::Task *TaskManager = nullptr; Tasking::Task *TaskManager = nullptr;
Time::time *TimeManager = nullptr;
KernelConfig Config; KernelConfig Config;
Time BootClock; Time::Clock BootClock;
// For the Display class. Printing on first buffer as default. // For the Display class. Printing on first buffer as default.
extern "C" void putchar(char c) { Display->Print(c, 0); } extern "C" void putchar(char c) { Display->Print(c, 0); }
@ -30,7 +30,7 @@ extern "C" void putchar(char c) { Display->Print(c, 0); }
EXTERNC void KPrint(const char *Format, ...) EXTERNC void KPrint(const char *Format, ...)
{ {
SmartLock(KernelLock); SmartLock(KernelLock);
Time tm = ReadClock(); Time::Clock tm = Time::ReadClock();
printf_("\eCCCCCC[\e00AEFF%02ld:%02ld:%02ld\eCCCCCC] ", tm.Hour, tm.Minute, tm.Second); printf_("\eCCCCCC[\e00AEFF%02ld:%02ld:%02ld\eCCCCCC] ", tm.Hour, tm.Minute, tm.Second);
va_list args; va_list args;
va_start(args, Format); va_start(args, Format);
@ -44,7 +44,7 @@ EXTERNC void Entry(BootInfo *Info)
{ {
trace("Hello, World!"); trace("Hello, World!");
InitializeMemoryManagement(Info); InitializeMemoryManagement(Info);
BootClock = ReadClock(); BootClock = Time::ReadClock();
bInfo = (BootInfo *)KernelAllocator.RequestPages(TO_PAGES(sizeof(BootInfo))); bInfo = (BootInfo *)KernelAllocator.RequestPages(TO_PAGES(sizeof(BootInfo)));
memcpy(bInfo, Info, sizeof(BootInfo)); memcpy(bInfo, Info, sizeof(BootInfo));
debug("BootInfo structure is at %p", bInfo); debug("BootInfo structure is at %p", bInfo);
@ -78,6 +78,14 @@ EXTERNC void Entry(BootInfo *Info)
} }
KPrint("Enabling Interrupts on Bootstrap Processor"); KPrint("Enabling Interrupts on Bootstrap Processor");
Interrupts::Enable(0); Interrupts::Enable(0);
KPrint("Initializing Timers");
#if defined(__amd64__)
TimeManager = new Time::time(PowerManager->GetACPI());
#elif defined(__i386__)
TimeManager = new Time::time(PowerManager->GetACPI());
#elif defined(__aarch64__)
TimeManager = new Time::time(nullptr);
#endif
KPrint("Initializing Bootstrap Processor Timer"); KPrint("Initializing Bootstrap Processor Timer");
Interrupts::InitializeTimer(0); Interrupts::InitializeTimer(0);
KPrint("Initializing SMP"); KPrint("Initializing SMP");

View File

@ -6,6 +6,8 @@
#include <interrupts.hpp> #include <interrupts.hpp>
#include <vector.hpp> #include <vector.hpp>
#include <memory.hpp> #include <memory.hpp>
#include <hashmap.hpp>
#include <ipc.hpp>
namespace Tasking namespace Tasking
{ {
@ -99,6 +101,11 @@ namespace Tasking
break; break;
} }
} }
void SetPriority(int priority)
{
Info.Priority = priority;
}
}; };
struct PCB struct PCB
@ -113,6 +120,7 @@ namespace Tasking
Vector<TCB *> Threads; Vector<TCB *> Threads;
Vector<PCB *> Children; Vector<PCB *> Children;
Memory::PageTable *PageTable; Memory::PageTable *PageTable;
HashMap<InterProcessCommunication::IPCPort, uint64_t> *IPCHandles;
}; };
enum TokenTrustLevel enum TokenTrustLevel

View File

@ -3,12 +3,41 @@
#include <types.h> #include <types.h>
struct Time namespace Time
{ {
struct Clock
{
uint64_t Year, Month, Day, Hour, Minute, Second; uint64_t Year, Month, Day, Hour, Minute, Second;
uint64_t Counter; uint64_t Counter;
}; };
Time ReadClock(); Clock ReadClock();
class time
{
private:
struct HPET
{
uint64_t GeneralCapabilities;
uint64_t Reserved0;
uint64_t GeneralConfiguration;
uint64_t Reserved1;
uint64_t GeneralIntStatus;
uint64_t Reserved2;
uint64_t Reserved3[24];
uint64_t MainCounterValue;
uint64_t Reserved4;
};
void *acpi;
void *hpet;
uint32_t clk = 0;
public:
void Sleep(uint64_t Milliseconds);
time(void *acpi);
~time();
};
}
#endif // !__FENNIX_KERNEL_TIME_H__ #endif // !__FENNIX_KERNEL_TIME_H__

View File

@ -54,6 +54,53 @@ typedef __builtin_va_list va_list;
#define VPOKE(type, address) (*((volatile type *)(address))) #define VPOKE(type, address) (*((volatile type *)(address)))
#define POKE(type, address) (*((type *)(address))) #define POKE(type, address) (*((type *)(address)))
#ifndef __cplusplus
#ifdef __STDC__
#ifdef __STDC_VERSION__
#if (__STDC_VERSION__ >= 201710L)
#define C_LANGUAGE_STANDARD 2018
#elif (__STDC_VERSION__ >= 201112L)
#define C_LANGUAGE_STANDARD 2011
#elif (__STDC_VERSION__ >= 199901L)
#define C_LANGUAGE_STANDARD 1999
#elif (__STDC_VERSION__ >= 199409L)
#define C_LANGUAGE_STANDARD 1995
#endif
#else
#define C_LANGUAGE_STANDARD 1990
#endif
#else
#define C_LANGUAGE_STANDARD 1972
#endif
#else
#ifdef __STDC__
#ifdef __cplusplus
#if (__cplusplus >= 202100L)
#define CPP_LANGUAGE_STANDARD 2023
#elif (__cplusplus >= 202002L)
#define CPP_LANGUAGE_STANDARD 2020
#elif (__cplusplus >= 201703L)
#define CPP_LANGUAGE_STANDARD 2017
#elif (__cplusplus >= 201402L)
#define CPP_LANGUAGE_STANDARD 2014
#elif (__cplusplus >= 201103L)
#define CPP_LANGUAGE_STANDARD 2011
#elif (__cplusplus >= 199711L)
#define CPP_LANGUAGE_STANDARD 1998
#endif
#else
#define CPP_LANGUAGE_STANDARD __cplusplus
#endif
#else
#define CPP_LANGUAGE_STANDARD __cplusplus
#endif
#endif // __cplusplus
typedef __INT8_TYPE__ int8_t; typedef __INT8_TYPE__ int8_t;
typedef __INT16_TYPE__ int16_t; typedef __INT16_TYPE__ int16_t;
typedef __INT32_TYPE__ int32_t; typedef __INT32_TYPE__ int32_t;

View File

@ -10,6 +10,7 @@
#include <kconfig.hpp> #include <kconfig.hpp>
#include <power.hpp> #include <power.hpp>
#include <task.hpp> #include <task.hpp>
#include <time.hpp>
#include <pci.hpp> #include <pci.hpp>
#endif #endif
@ -21,6 +22,7 @@ extern Power::Power *PowerManager;
extern PCI::PCI *PCIManager; extern PCI::PCI *PCIManager;
extern KernelConfig Config; extern KernelConfig Config;
extern Tasking::Task *TaskManager; extern Tasking::Task *TaskManager;
extern Time::time *TimeManager;
#endif #endif
EXTERNC void KPrint(const char *format, ...); EXTERNC void KPrint(const char *format, ...);