Compare commits

...

10 Commits

Author SHA1 Message Date
Simon 6951311c9c foo 4 months ago
Simon 9d525a9602 hopefully added semaphores 4 months ago
Simon 42f2dd6028 fooo 4 months ago
Eggert Jung 68cb30210f :wq 4 months ago
Eggert Jung 370e76bfc4 Merge branch 'test2' of gitlab.ibr.cs.tu-bs.de:vss/teaching/ss25/v_bsb1/Gruppe_018 into test2 4 months ago
Eggert Jung fd021c98a7 more or less works (with kill) 4 months ago
Simon 5611b7f886 killing all bugs 4 months ago
Simon c175c49945 works mostly 4 months ago
Simon 9023944e92 remove second timer 4 months ago
Eggert Jung cc412f4902 test2 4 months ago

@ -69,7 +69,8 @@ enum Vector {
// Interrupts
KEYBOARD=32,
PANIC=33,
TIMER=34
TIMER=34,
ASSASSIN=35
};
constexpr size_t VECTORS = 256;

@ -129,21 +129,23 @@ void set(uint32_t counter, uint8_t divide, uint8_t vector, bool periodic,
LAPIC::write(TIMER_DIVIDE_CONFIGURATION, getClockDiv(divide));
LAPIC::write(TIMER_INITIAL_COUNTER, counter);
}
uint64_t time_tick;
uint8_t dividender;
bool setup(uint32_t us) {
uint64_t timer_ticks = (static_cast<uint64_t>(ticks()) * us) / 1000ULL;
time_tick = (static_cast<uint64_t>(ticks()) * us) / 1000ULL;
uint8_t divisor = 1;
while (timer_ticks > UINT32_MAX) {
dividender = 1;
while (time_tick > UINT32_MAX) {
// While timer_ticks is to large to fit in 32bits.
timer_ticks >>= 1;
divisor <<= 1;
time_tick >>= 1;
dividender <<= 1;
}
if (divisor > 128)
if (dividender > 128)
return false; // Timer interval is to large.
// Setup Masked interrupts to effectively disable the timer.
set(static_cast<uint32_t>(timer_ticks), divisor, Core::Interrupt::TIMER, true, true);
return true;
}
@ -156,15 +158,15 @@ uint32_t interval() {
}
void activate() {
uint32_t timer_ticks = LAPIC::read(TIMER_INITIAL_COUNTER);
// Disable Counter to avoid spouriose interrupts.
LAPIC::write(TIMER_INITIAL_COUNTER, 0);
set(static_cast<uint32_t>(time_tick), dividender, Core::Interrupt::TIMER, true, true);
// Activate Timer interrupts.
setMasked(false);
// enable counter with correct value.
LAPIC::write(TIMER_INITIAL_COUNTER, timer_ticks);
LAPIC::write(TIMER_INITIAL_COUNTER, time_tick);
}
void setMasked(bool masked) {

@ -3,6 +3,7 @@
#include "../arch/system.h"
#include "../arch/core_interrupt.h"
#include "../arch/ioport.h"
#include "../arch/ioapic.h"
#include "../compiler/fix.h"
#include "../debug/output.h"
#include "keydecoder.h"
@ -106,6 +107,10 @@ void init() {
// Set to maximum speed & minimum delay
setRepeatRate(SPEED_30_0CPS, DELAY_250MS);
IOAPIC::config(1, Core::Interrupt::KEYBOARD);
IOAPIC::allow(1);
PS2Controller::drainBuffer();
}
bool fetch(Key &pressed) {

@ -1,17 +1,23 @@
#include "epilogues.h"
#include "../debug/output.h"
#include "../arch/core.h"
#include "../arch/lapic.h"
#include "guard.h"
#include "../thread/scheduler.h"
extern Key kout_key;
#include "../user/app1/appl.h"
extern Application application1;
namespace Epilogues {
void keyboard(Vault& v) {
v.kout.setPos(0,0);
v.kout << kout_key.ascii() << flush ;
if(kout_key.scancode == Key::KEY_K)
v.sch.kill(v.sch.dispatcher.lifePointer[3]);
}
void timer(Vault& v) {
@ -19,15 +25,17 @@ void timer(Vault& v) {
int x, y;
if(Core::getID() == 0) {
v.kout.getPos(x, y);
v.kout.setPos(65U, 0U);
v.kout << counter++ << " " << flush;
v.kout.setPos(65, 0);
v.kout << counter++ << flush;
v.kout.setPos(x, y);
}
DBG << "timer C:" << dec << Core::getID() << " L:" << LAPIC::getID() << "\n" << flush;
v.sch.resume(true);
}
void assassin(Vault& v) {
DBG << "assassin epilog\n"<< endl;
if (v.sch.active()->kill_flag) {
v.sch.exit();
}

@ -1,5 +1,4 @@
#include "guard.h"
#include "../arch/core.h"
#include "../debug/output.h"
#include "../object/bbuffer.h"
@ -24,9 +23,11 @@ Vault::Vault() {
Guarded::~Guarded() { Guard::leave(); }
Guarded Guard::enter() {
bool status = Core::Interrupt::disable();
epi_flag FOR_CURRENT_CORE = true;
//Core::Interrupt::enable();
Core::Interrupt::restore(status);
global_lock.lock();
Bellringer bellringer;
return Guarded(global_vault);
}
@ -57,7 +58,7 @@ void Guard::relay(Epilogue handler) {
Core::Interrupt::enable(); // goto level 0.5
if(epi_flag FOR_CURRENT_CORE){
epilogue_queue->produce(handler);
epilogue_queue FOR_CURRENT_CORE.produce(handler);
}
else{
epi_flag FOR_CURRENT_CORE = true;

@ -3,6 +3,7 @@
*/
//#pragma once
#include "sync/bellringer.h"
#include "../object/bbuffer.h"
#include "../object/key.h"
#include "../types.h"
@ -18,6 +19,9 @@ struct Vault {
// no copy
Vault(const Vault&) = delete;
Vault& operator=(const Vault&) = delete;
uint8_t counter;
Bellringer bellringer;
};
/*! \brief Lock guard that provides access to the epilogue \ref Vault

@ -10,9 +10,11 @@
#include "../device/ps2controller.h"
#include "../sync/ticketlock.h"
#include "../arch/core_interrupt.h"
#include "epilogues.h"
#include "guard.h"
Key kout_key = Key();
void printContext(const InterruptContext *context) {
@ -77,6 +79,7 @@ void handle_keyboard() {
LAPIC::endOfInterrupt();
if (kout_key.ctrl() && kout_key.alt() && kout_key.scancode == Key::KEY_DEL)
System::reboot();
Guard::relay(Epilogues::keyboard);
}
else
@ -98,6 +101,7 @@ void handle_keyboard() {
[[gnu::interrupt]] void handle_assassin(InterruptContext *context) {
(void)context;
LAPIC::endOfInterrupt();
DBG << "assassin handler\n"<< endl;
Guard::relay(Epilogues::assassin);
}
[[gnu::interrupt]] void handle_wakeup(InterruptContext *context) {
@ -126,6 +130,8 @@ void initInterruptHandlers() {
IDT::InterruptDescriptor::Returning(handle_keyboard_asm));
IDT::set(Core::Interrupt::Vector::TIMER,
IDT::InterruptDescriptor::Returning(handle_timer));
IDT::set(Core::Interrupt::Vector::ASSASSIN,
IDT::InterruptDescriptor::Returning(handle_assassin));
// Load the idt pointer
IDT::load();
}

@ -20,7 +20,7 @@
#include "arch/context.h"
#include "thread/thread.h"
///TextStream kout = TextStream(0, 80, 0, 10, true);
TextStream kout = TextStream(0, 80, 0, 10, true);
Ticketlock koutlock;
//Scheduler sch;
@ -121,24 +121,14 @@ extern "C" int main() {
}
LAPIC::Timer::setup(1000000);
ApplicationProcessor::boot();
PS2Controller::init();
IOAPIC::init();
IOAPIC::config(1, Core::Interrupt::KEYBOARD);
IOAPIC::allow(1);
PS2Controller::init();
ApplicationProcessor::boot();
Core::Interrupt::enable();
LAPIC::Timer::activate();
PS2Controller::drainBuffer();
DBG << "Main CPU " << static_cast<int>(LAPIC::getID()) << endl << flush;
{
Guarded g = Guard::enter();
g.vault().sch.schedule();
@ -156,11 +146,12 @@ extern "C" int main() {
// Main function for application processors
extern "C" int main_ap() {
DBG_VERBOSE << "CPU core " << static_cast<int>(Core::getID()) << " / LAPIC "
DBG << "CPU core " << static_cast<int>(Core::getID()) << " / LAPIC "
<< static_cast<int>(LAPIC::getID()) << " in main_ap()" << endl;
Core::Interrupt::enable();
DBG << "App CPU " << static_cast<int>(Core::getID()) << endl << flush;
Core::Interrupt::enable();
LAPIC::Timer::activate();
{
Guarded g = Guard::enter();

@ -1,16 +1,38 @@
#include "./bellringer.h"
#include "../interrupt/guard.h"
// check: Checks whether bells are running out of time and rings them if
// necessary
void Bellringer::check(Vault &vault) { (void)vault; }
void Bellringer::check(Vault &vault) {
auto first = vault.bellringer.bells.dequeue();
first->counter--;
if (vault.bellringer.bellPending()){
vault.bellringer.bells.insertFirst(*first);
}
if (vault.bellringer.bellPending()){
if (first->counter < 1) {
vault.sch.ready(vault.bellringer.bells.dequeue()->thread);
}
}
}
// job: Give a bell to the bellringer & ring it when the specified time ran out.
void Bellringer::sleep(Vault &vault, unsigned int ms) {
(void)vault;
(void)ms;
Bell bell;
bell.thread = vault.sch.dispatcher.active();
auto tmp = vault.bellringer.bells.dequeue();
vault.bellringer.bells.insertFirst(*tmp);
do{
ms-=tmp->counter;
} while ((tmp=vault.bellringer.bells.next(*tmp)) != nullptr );
bell.counter = ms;
vault.sch.resume(false);
}
// Are there bells in the queue?
bool Bellringer::bellPending() const { return false; }
bool Bellringer::bellPending() const {
if (bells.is_empty())
return false;
else
return true;
}

@ -1,7 +1,30 @@
#include "./semaphore.h"
#include "../interrupt/guard.h"
#include "../thread/scheduler.h"
uint8_t counter;
Queue<Thread> waiting;
Semaphore::Semaphore(unsigned c) {
counter=c;
}
Semaphore::Semaphore(unsigned c) { (void)c; }
void Semaphore::p(Vault &vault) { (void)vault; }
void Semaphore::p(Vault &vault) {
//resource is frei, läuft direkt weiter
if (vault.counter > 0)
vault.counter--;
else
//thread block, zu aktueller queue hinzufügen und
waiting.enqueue(*vault.sch.active()); //was is der calling thread
//
}
//release
void Semaphore::v(Vault &vault) {
Thread* first = waiting.dequeue();
if (first != nullptr)
vault.sch.ready(first); //wakeup "first" thread
else
vault.counter++;
}
void Semaphore::v(Vault &vault) { (void)vault; }

@ -27,6 +27,7 @@ void Dispatcher::go(Thread *first) {
}
void Dispatcher::dispatch(Thread *next) {
//lifePointer[Core::getID()] = next;
lifePointer[Core::getID()]->resume(next);
Thread* tmp = lifePointer[Core::getID()];
lifePointer[Core::getID()] = next;
tmp->resume(next);
}

@ -1,6 +1,10 @@
// vim: set noet ts=4 sw=4:
#include "scheduler.h"
#include "../arch/lapic.h"
#include "dispatcher.h"
#include "../interrupt/guard.h"
#include "../debug/output.h"
Queue<Thread> readyList = Queue<Thread>();
@ -39,6 +43,17 @@ void Scheduler::exit() {
void Scheduler::kill(Thread* that) {
readyList.remove(that);
that->kill_flag = true;
DBG << "kill..." << flush;
for(uint8_t i=0;i<Core::MAX; i++)
if(dispatcher.lifePointer[i] == that){
LAPIC::IPI::send(i, Core::Interrupt::Vector::ASSASSIN);
DBG << "found thread on Core" << dec << static_cast<int>(i) << "! killing...\n" << flush;
return;
}
DBG << "not found\n" << flush;
}
bool Scheduler::isActive(const Thread* thread, unsigned int* cpu) {
@ -47,6 +62,9 @@ bool Scheduler::isActive(const Thread* thread, unsigned int* cpu) {
return false;
}
bool Scheduler::isEmpty() const { return false; }
void Scheduler::setIdle(IdleThread* that) { (void)that; }

@ -18,11 +18,10 @@ Thread::Thread(void* tos) {
#include "../thread/scheduler.h"
void Thread::resume(Thread* next) {
Guarded g = Guard::enter();
Context *from = &g.vault().sch.active()->context;
Context *from = &this->context;
Context *to = &next->context;
g.vault().sch.dispatcher.lifePointer[Core::getID()] = next;
//g.vault().sch.dispatcher.lifePointer[Core::getID()] = next;
DBG << "from: " << hex << from << endl;
DBG << "to : " << hex << to << endl << flush;
context_switch(to, from);

@ -9,9 +9,9 @@
#include "../../interrupt/guard.h"
#include "../../debug/output.h"
#include "../../arch/context.h"
#include "../../sync/semaphore.h"
static uint8_t appl_cnt = 0;
extern TextStream kout;
char text[] = "Ich mag\n\
Saftige Pflaumen voller Aroma\n\
Ich knuddel jede Oma ins Koma\n\
@ -25,9 +25,6 @@ Ich bin Großmuttersniffer\n\
Und wacht sie aus'm Koma auf, kriegt sie von mir 'n Sticker\n\
\n";
extern Ticketlock koutlock;
extern Context* test2; extern Context* test1;
extern uint8_t test1_stack[], test2_stack[];
void activeWaitDelay(uint64_t cycles) {
uint64_t counter = 0; // Use volatile to prevent optimization
for (uint64_t i = 0; i < cycles; ++i) {
@ -36,18 +33,23 @@ void activeWaitDelay(uint64_t cycles) {
//Core::pause();
}
}
Semaphore foo= Semaphore(1);
void Application::action() { // NOLINT
uint16_t cnt = 0;
uint8_t row = appl_cnt++;
while (1) {
//koutlock.lock();
{
Guarded g = Guard::enter();
//g.vault();
g.vault().kout.setPos((unsigned)10*row,(unsigned)/*Core::getID()*2+*/1);
g.vault().kout << cnt++ << flush;
Guarded g= Guard::enter();
//g.vault();
foo.p(g.vault());
kout.setPos((unsigned)8*row,(unsigned)/*Core::getID()*2+*/1);
kout << cnt++ << flush;
foo.v(g.vault());
g.vault().bellringer.sleep(g.vault(),6666);
//g.vault().kout << endl << flush;
//Guard::leave();
//koutlock.unlock();

Loading…
Cancel
Save