1

switch cpu methods to static

This commit is contained in:
2022-07-24 00:29:21 +02:00
parent a5c14da0de
commit 605d4e8c8d
11 changed files with 52 additions and 62 deletions

View File

@ -33,7 +33,6 @@
#include "kernel/Allocator.h" #include "kernel/Allocator.h"
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include <stddef.h>
#define MEM_SIZE_DEF 8 * 1024 * 1024 // Groesse des Speichers = 8 MB #define MEM_SIZE_DEF 8 * 1024 * 1024 // Groesse des Speichers = 8 MB

View File

@ -319,7 +319,7 @@ void BIOS::Int(int inter) {
// Interrupt-Nummer in 16-Bit Code-Segment schreiben (unschoen, aber ...) // Interrupt-Nummer in 16-Bit Code-Segment schreiben (unschoen, aber ...)
*(ptr + 48) = (unsigned char)inter; *(ptr + 48) = (unsigned char)inter;
cpu.disable_int(); // Interrupts abschalten CPU::disable_int(); // Interrupts abschalten
bios_call(); bios_call();
cpu.enable_int(); CPU::enable_int();
} }

View File

@ -10,43 +10,37 @@
#ifndef __BIOS_include__ #ifndef __BIOS_include__
#define __BIOS_include__ #define __BIOS_include__
// Speicherseite fuer Rueckgabewerte von BIOS-Aufrufen // Speicherseite fuer Rueckgabewerte von BIOS-Aufrufen
#define RETURN_MEM 0x9F000 #define RETURN_MEM 0x9F000
// Struktur fuer Parameteruebergabe fuer einen BIOS-Aufruf // Struktur fuer Parameteruebergabe fuer einen BIOS-Aufruf
struct BIOScall_params { struct BIOScall_params {
unsigned short DS; unsigned short DS;
unsigned short ES; unsigned short ES;
unsigned short FS; unsigned short FS;
unsigned short Flags; unsigned short Flags;
unsigned int DI; unsigned int DI;
unsigned int SI; unsigned int SI;
unsigned int BP; unsigned int BP;
unsigned int SP; unsigned int SP;
unsigned int BX; unsigned int BX;
unsigned int DX; unsigned int DX;
unsigned int CX; unsigned int CX;
unsigned int AX; unsigned int AX;
} __attribute__((packed)); } __attribute__((packed));
// kein Auffuellen von bytes auf Wortgrenzen // kein Auffuellen von bytes auf Wortgrenzen
// Zeiger auf Speichbereich fuer Parameter fuer BIOS-Aufruf // Zeiger auf Speichbereich fuer Parameter fuer BIOS-Aufruf
extern struct BIOScall_params* BC_params; extern struct BIOScall_params* BC_params;
class BIOS { class BIOS {
private: private:
BIOS(const BIOS &copy); // Verhindere Kopieren BIOS(const BIOS& copy); // Verhindere Kopieren
public: public:
// Initialisierung: manuelles Anlegen einer Funktion // Initialisierung: manuelles Anlegen einer Funktion
BIOS(); BIOS();
// BIOS-Aufruf, per Software-Interrupt // BIOS-Aufruf, per Software-Interrupt
void Int(int inter); void Int(int inter);
}; };

View File

@ -13,7 +13,6 @@
#define __CPU_include__ #define __CPU_include__
class CPU { class CPU {
private: private:
CPU(const CPU& copy); // Verhindere Kopieren CPU(const CPU& copy); // Verhindere Kopieren
@ -21,29 +20,29 @@ public:
CPU() {} CPU() {}
// Erlauben von (Hardware-)Interrupts // Erlauben von (Hardware-)Interrupts
inline void enable_int() { static inline void enable_int() {
asm volatile("sti"); asm volatile("sti");
} }
// Interrupts werden ignoriert/verboten // Interrupts werden ignoriert/verboten
inline void disable_int() { static inline void disable_int() {
asm volatile("cli"); asm volatile("cli");
} }
// Prozessor bis zum naechsten Interrupt anhalten // Prozessor bis zum naechsten Interrupt anhalten
inline void idle() { static inline void idle() {
asm volatile("sti;" asm volatile("sti;"
"hlt"); "hlt");
} }
// Prozessor anhalten // Prozessor anhalten
inline void halt() { static inline void halt() {
asm volatile("cli;" asm volatile("cli;"
"hlt"); "hlt");
} }
// Time-Stamp-Counter auslesen // Time-Stamp-Counter auslesen
inline unsigned long long int rdtsc() { static inline unsigned long long int rdtsc() {
unsigned long long int ret; unsigned long long int ret;
asm volatile("rdtsc" asm volatile("rdtsc"
: "=A"(ret)); : "=A"(ret));

View File

@ -10,7 +10,6 @@
#include "kernel/Globals.h" #include "kernel/Globals.h"
CPU cpu; // CPU-spezifische Funktionen
CGA_Stream kout; // Ausgabe-Strom fuer Kernel CGA_Stream kout; // Ausgabe-Strom fuer Kernel
BIOS bios; // Schnittstelle zum 16-Bit BIOS BIOS bios; // Schnittstelle zum 16-Bit BIOS
VESA vesa; // VESA-Treiber VESA vesa; // VESA-Treiber

View File

@ -22,13 +22,13 @@
#include "kernel/CPU.h" #include "kernel/CPU.h"
#include "kernel/interrupts/IntDispatcher.h" #include "kernel/interrupts/IntDispatcher.h"
#include "kernel/interrupts/PIC.h" #include "kernel/interrupts/PIC.h"
#include "kernel/Paging.h"
#include "kernel/threads/Scheduler.h" #include "kernel/threads/Scheduler.h"
#include "user/devices/SerialOut.h" #include "user/devices/SerialOut.h"
#include "user/event/KeyEventManager.h" #include "user/event/KeyEventManager.h"
// I wanted to make more of these singletons but there were problems with atexit missing because of nostdlib I guess // I wanted to make more of these singletons but there were problems with atexit missing because of nostdlib I guess
extern CPU cpu; // CPU-spezifische Funktionen
extern CGA_Stream kout; // Ausgabe-Strom fuer Kernel extern CGA_Stream kout; // Ausgabe-Strom fuer Kernel
extern BIOS bios; // Schnittstelle zum 16-Bit BIOS extern BIOS bios; // Schnittstelle zum 16-Bit BIOS
extern VESA vesa; // VESA-Treiber extern VESA vesa; // VESA-Treiber

View File

@ -35,13 +35,13 @@ void int_disp(unsigned int vector) {
if (vector < 32) { if (vector < 32) {
bs_dump(vector); bs_dump(vector);
cpu.halt(); CPU::halt();
} }
if (intdis.report(vector) < 0) { if (intdis.report(vector) < 0) {
kout << "Panic: unexpected interrupt " << vector; kout << "Panic: unexpected interrupt " << vector;
kout << " - processor halted." << endl; kout << " - processor halted." << endl;
cpu.halt(); CPU::halt();
} }
} }

View File

@ -8,7 +8,7 @@
* * * *
* Der Scheduler wird mit 'schedule' gestartet. Neue Threads* * Der Scheduler wird mit 'schedule' gestartet. Neue Threads*
* können mit 'ready' hinzugefügt werden. Ein Thread muss * * können mit 'ready' hinzugefügt werden. Ein Thread muss *
* die CPU freiwillig mit 'yield' abgeben, damit andere auch* * die CPU::freiwillig mit 'yield' abgeben, damit andere auch*
* rechnen koennen. Ein Thread kann sich selbst mit 'exit' * * rechnen koennen. Ein Thread kann sich selbst mit 'exit' *
* terminieren. Ein Thread kann einen anderen Thread mit * * terminieren. Ein Thread kann einen anderen Thread mit *
* 'kill' beenden. Ein erzwungener Threadwechsel erfolgt * * 'kill' beenden. Ein erzwungener Threadwechsel erfolgt *
@ -33,7 +33,7 @@ constexpr const bool INSANE_TRACE = false;
* Beschreibung: Auf den active thread wechseln. * * Beschreibung: Auf den active thread wechseln. *
* * * *
* Parameter: * * Parameter: *
* next Thread der die CPU erhalten soll. * * next Thread der die CPU::erhalten soll. *
*****************************************************************************/ *****************************************************************************/
void Scheduler::start(bse::vector<bse::unique_ptr<Thread>>::iterator next) { void Scheduler::start(bse::vector<bse::unique_ptr<Thread>>::iterator next) {
active = next; active = next;
@ -84,10 +84,10 @@ void Scheduler::schedule() {
* Beschreibung: Thread in readyQueue eintragen. * * Beschreibung: Thread in readyQueue eintragen. *
*****************************************************************************/ *****************************************************************************/
void Scheduler::ready(bse::unique_ptr<Thread>&& thread) { void Scheduler::ready(bse::unique_ptr<Thread>&& thread) {
cpu.disable_int(); CPU::disable_int();
log.debug() << "Adding to ready_queue, ID: " << dec << thread->tid << endl; log.debug() << "Adding to ready_queue, ID: " << dec << thread->tid << endl;
ready_queue.push_back(std::move(thread)); ready_queue.push_back(std::move(thread));
cpu.enable_int(); CPU::enable_int();
} }
/***************************************************************************** /*****************************************************************************
@ -103,11 +103,11 @@ void Scheduler::exit() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern // Thread-Wechsel durch PIT verhindern
cpu.disable_int(); CPU::disable_int();
if (ready_queue.size() == 1) { if (ready_queue.size() == 1) {
log.error() << "Can't exit last thread, active ID: " << dec << (*active)->tid << endl; log.error() << "Can't exit last thread, active ID: " << dec << (*active)->tid << endl;
cpu.enable_int(); CPU::enable_int();
return; return;
} }
@ -132,7 +132,7 @@ void Scheduler::exit() {
* that Zu terminierender Thread * * that Zu terminierender Thread *
*****************************************************************************/ *****************************************************************************/
void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) { void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
cpu.disable_int(); CPU::disable_int();
unsigned int prev_tid = (*active)->tid; unsigned int prev_tid = (*active)->tid;
@ -151,7 +151,7 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
block_queue.erase(it); block_queue.erase(it);
log.info() << "Killed thread from block_queue with id: " << tid << endl; log.info() << "Killed thread from block_queue with id: " << tid << endl;
cpu.enable_int(); CPU::enable_int();
return; return;
} }
} }
@ -159,7 +159,7 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
// Ready queue, can't kill last one // Ready queue, can't kill last one
if (ready_queue.size() == 1) { if (ready_queue.size() == 1) {
log.error() << "Kill: Can't kill last thread in ready_queue with id: " << tid << endl; log.error() << "Kill: Can't kill last thread in ready_queue with id: " << tid << endl;
cpu.enable_int(); CPU::enable_int();
return; return;
} }
@ -185,26 +185,26 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
ready_queue.erase(it); ready_queue.erase(it);
log.info() << "Killed thread from ready_queue with id: " << tid << endl; log.info() << "Killed thread from ready_queue with id: " << tid << endl;
cpu.enable_int(); CPU::enable_int();
return; return;
} }
} }
log.error() << "Kill: Couldn't find thread with id: " << tid << " in ready- or block-queue" << endl; log.error() << "Kill: Couldn't find thread with id: " << tid << " in ready- or block-queue" << endl;
cpu.enable_int(); CPU::enable_int();
} }
// TODO: Can't retrive the thread right now because it's not clear when it's finished, // TODO: Can't retrive the thread right now because it's not clear when it's finished,
// maybe introduce a exited_queue and get it from there // maybe introduce a exited_queue and get it from there
void Scheduler::nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) { void Scheduler::nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
cpu.disable_int(); CPU::disable_int();
for (bse::unique_ptr<Thread>& thread : block_queue) { for (bse::unique_ptr<Thread>& thread : block_queue) {
if (thread->tid == tid) { if (thread->tid == tid) {
thread->suicide(); thread->suicide();
log.info() << "Nice killed thread in block_queue with id: " << tid << endl; log.info() << "Nice killed thread in block_queue with id: " << tid << endl;
deblock(tid); deblock(tid);
cpu.enable_int(); CPU::enable_int();
return; return;
} }
} }
@ -213,19 +213,19 @@ void Scheduler::nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
if (thread->tid == tid) { if (thread->tid == tid) {
thread->suicide(); thread->suicide();
log.info() << "Nice killed thread in ready_queue with id: " << tid << endl; log.info() << "Nice killed thread in ready_queue with id: " << tid << endl;
cpu.enable_int(); CPU::enable_int();
return; return;
} }
} }
log.error() << "Can't nice kill thread (not found) with id: " << tid << endl; log.error() << "Can't nice kill thread (not found) with id: " << tid << endl;
cpu.enable_int(); CPU::enable_int();
} }
/***************************************************************************** /*****************************************************************************
* Methode: Scheduler::yield * * Methode: Scheduler::yield *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
* Beschreibung: CPU freiwillig abgeben und Auswahl des naechsten Threads.* * Beschreibung: CPU::freiwillig abgeben und Auswahl des naechsten Threads.*
* Naechsten Thread aus der readyQueue holen, den aktuellen * * Naechsten Thread aus der readyQueue holen, den aktuellen *
* in die readyQueue wieder eintragen. Das Umschalten soll * * in die readyQueue wieder eintragen. Das Umschalten soll *
* mithilfe des Dispatchers erfolgen. * * mithilfe des Dispatchers erfolgen. *
@ -238,13 +238,13 @@ void Scheduler::yield() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern // Thread-Wechsel durch PIT verhindern
cpu.disable_int(); CPU::disable_int();
if (ready_queue.size() == 1) { if (ready_queue.size() == 1) {
if constexpr (INSANE_TRACE) { if constexpr (INSANE_TRACE) {
log.trace() << "Skipping yield as no thread is waiting, active ID: " << dec << (*active)->tid << endl; log.trace() << "Skipping yield as no thread is waiting, active ID: " << dec << (*active)->tid << endl;
} }
cpu.enable_int(); CPU::enable_int();
return; return;
} }
if constexpr (INSANE_TRACE) { if constexpr (INSANE_TRACE) {
@ -264,7 +264,7 @@ void Scheduler::preempt() {
/* Hier muss Code eingefuegt werden */ /* Hier muss Code eingefuegt werden */
cpu.disable_int(); CPU::disable_int();
yield(); yield();
} }
@ -282,11 +282,11 @@ void Scheduler::block() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
cpu.disable_int(); CPU::disable_int();
if (ready_queue.size() == 1) { if (ready_queue.size() == 1) {
log.error() << "Can't block last thread, active ID: " << dec << (*active)->tid << endl; log.error() << "Can't block last thread, active ID: " << dec << (*active)->tid << endl;
cpu.enable_int(); CPU::enable_int();
return; return;
} }
@ -316,7 +316,7 @@ void Scheduler::deblock(unsigned int tid) {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
cpu.disable_int(); CPU::disable_int();
for (bse::vector<bse::unique_ptr<Thread>>::iterator it = block_queue.begin(); it != block_queue.end(); ++it) { for (bse::vector<bse::unique_ptr<Thread>>::iterator it = block_queue.begin(); it != block_queue.end(); ++it) {
if ((*it)->tid == tid) { if ((*it)->tid == tid) {
@ -329,11 +329,11 @@ void Scheduler::deblock(unsigned int tid) {
if constexpr (INSANE_TRACE) { if constexpr (INSANE_TRACE) {
log.trace() << "Deblocked thread with id: " << tid << endl; log.trace() << "Deblocked thread with id: " << tid << endl;
} }
cpu.enable_int(); CPU::enable_int();
return; return;
} }
} }
log.error() << "Couldn't deblock thread with id: " << tid << endl; log.error() << "Couldn't deblock thread with id: " << tid << endl;
cpu.enable_int(); CPU::enable_int();
} }

View File

@ -13,7 +13,7 @@ void Semaphore::p() {
// Block and manage thread in semaphore queue until it's woken up by v() again // Block and manage thread in semaphore queue until it's woken up by v() again
this->wait_queue.push_back(scheduler.get_active()); this->wait_queue.push_back(scheduler.get_active());
cpu.disable_int(); // Make sure the block() comes through after releasing the lock CPU::disable_int(); // Make sure the block() comes through after releasing the lock
this->lock.release(); this->lock.release();
scheduler.block(); // Moves to next thread, enables int scheduler.block(); // Moves to next thread, enables int
} }
@ -27,7 +27,7 @@ void Semaphore::v() {
unsigned int tid = this->wait_queue.front(); unsigned int tid = this->wait_queue.front();
this->wait_queue.erase(wait_queue.begin()); this->wait_queue.erase(wait_queue.begin());
cpu.disable_int(); // Make sure the deblock() comes through after releasing the lock CPU::disable_int(); // Make sure the deblock() comes through after releasing the lock
this->lock.release(); this->lock.release();
scheduler.deblock(tid); // Enables int scheduler.deblock(tid); // Enables int
} else { } else {

View File

@ -13,7 +13,6 @@
#define __SpinLock_include__ #define __SpinLock_include__
class SpinLock { class SpinLock {
private: private:
SpinLock(const SpinLock& copy) = delete; // Verhindere Kopieren SpinLock(const SpinLock& copy) = delete; // Verhindere Kopieren

View File

@ -53,7 +53,7 @@ int main() {
pit.plugin(); pit.plugin();
// Interrupts erlauben (Tastatur, PIT) // Interrupts erlauben (Tastatur, PIT)
cpu.enable_int(); CPU::enable_int();
// Activate paging // Activate paging
// This has to happen after the allocator is initialized but before the scheduler is started // This has to happen after the allocator is initialized but before the scheduler is started