GlobalTtyManager

This commit is contained in:
2023-11-26 22:11:29 +01:00
parent 84ae1aa6e5
commit 4749bb5c09
19 changed files with 172 additions and 119 deletions

View File

@@ -2,7 +2,6 @@
target_sources(kernel.elf PRIVATE
limine_mm.cpp
task.asm
tty.cpp
kmem.cpp
kmain.asm
paging.asm
@@ -11,6 +10,7 @@ target_sources(kernel.elf PRIVATE
limine_fb.cpp
idt.cpp
serial.cpp
SerialTty.cpp
idt.asm
globals.cpp
memman.cpp

View File

@@ -0,0 +1,16 @@
//
// Created by Stepan Usatiuk on 26.11.2023.
//
#include "SerialTty.hpp"
#include "LockGuard.hpp"
void SerialTty::putchar(char c) {
LockGuard guard(mutex);
write_serial(c);
}
void SerialTty::putstr(const char *str) {
LockGuard guard(mutex);
writestr(str);
}

View File

@@ -0,0 +1,19 @@
//
// Created by Stepan Usatiuk on 26.11.2023.
//
#ifndef OS2_SERIALTTY_HPP
#define OS2_SERIALTTY_HPP
#include "Tty.hpp"
class SerialTty : public Tty {
Mutex mutex;
public:
void putchar(char c) override;
void putstr(const char *str) override;
};
#endif//OS2_SERIALTTY_HPP

View File

@@ -4,9 +4,11 @@
#include <cstddef>
#include "LockGuard.hpp"
#include "SerialTty.hpp"
#include "SkipList.hpp"
#include "String.hpp"
#include "TestTemplates.hpp"
#include "TtyManager.hpp"
#include "VMA.hpp"
#include "asserts.hpp"
#include "globals.hpp"
@@ -21,7 +23,6 @@
#include "syscalls_interface.h"
#include "task.hpp"
#include "timer.hpp"
#include "tty.hpp"
void ktask();
@@ -69,20 +70,20 @@ void freeprinter() {
buf += "Free mem: ";
buf += get_free() * 1024;
buf += "\n";
all_tty_putstr(buf.c_str());
GlobalTtyManager.all_tty_putstr(buf.c_str());
buf = "";
buf += "Heap allocated: ";
buf += get_heap_allocated();
buf += "\n";
all_tty_putstr(buf.c_str());
GlobalTtyManager.all_tty_putstr(buf.c_str());
buf = "";
buf += "Heap used: ";
buf += get_heap_used();
buf += "\n";
buf += "=====\n";
all_tty_putstr(buf.c_str());
GlobalTtyManager.all_tty_putstr(buf.c_str());
sleep_self(1000000);
}
}
@@ -112,7 +113,7 @@ void statprinter() {
buf += " usage: ";
buf += (((f->data.second - t.data.second) * 100ULL) / slice);
buf += "%\n";
all_tty_putstr(buf.c_str());
GlobalTtyManager.all_tty_putstr(buf.c_str());
} else {
String buf;
buf += "PID: ";
@@ -120,7 +121,7 @@ void statprinter() {
buf += " ";
buf += t.data.first;
buf += " dead \n";
all_tty_putstr(buf.c_str());
GlobalTtyManager.all_tty_putstr(buf.c_str());
}
}
}
@@ -131,30 +132,30 @@ static Mutex testmutex;
void mtest1() {
{
LockGuard l(testmutex);
all_tty_putstr("Locked1\n");
GlobalTtyManager.all_tty_putstr("Locked1\n");
sleep_self(100000);
}
all_tty_putstr("Unlocked1\n");
GlobalTtyManager.all_tty_putstr("Unlocked1\n");
remove_self();
}
void mtest2() {
{
LockGuard l(testmutex);
all_tty_putstr("Locked2\n");
GlobalTtyManager.all_tty_putstr("Locked2\n");
sleep_self(100000);
}
all_tty_putstr("Unlocked2\n");
GlobalTtyManager.all_tty_putstr("Unlocked2\n");
remove_self();
}
void mtest3() {
{
LockGuard l(testmutex);
all_tty_putstr("Locked3\n");
GlobalTtyManager.all_tty_putstr("Locked3\n");
sleep_self(100000);
}
all_tty_putstr("Unlocked3\n");
GlobalTtyManager.all_tty_putstr("Unlocked3\n");
remove_self();
}
@@ -167,17 +168,17 @@ void stress() {
char buf[69];
itoa(curi, buf, 10);
// all_tty_putstr("stress ");
// all_tty_putstr(buf);
// all_tty_putstr("\n");
// GlobalTtyManager.all_tty_putstr("stress ");
// GlobalTtyManager.all_tty_putstr(buf);
// GlobalTtyManager.all_tty_putstr("\n");
remove_self();
}
void templates_tester() {
all_tty_putstr("Testing templates\n");
GlobalTtyManager.all_tty_putstr("Testing templates\n");
for (int i = 0; i < 2000; i++)
test_templates();
all_tty_putstr("Testing templates OK\n");
GlobalTtyManager.all_tty_putstr("Testing templates OK\n");
remove_self();
}
@@ -186,7 +187,7 @@ void stress_tester() {
for (int i = 0; i < 2000; i++)
new_ktask(stress, "stress");
all_tty_putstr("Finished stress\n");
GlobalTtyManager.all_tty_putstr("Finished stress\n");
remove_self();
}
@@ -202,8 +203,7 @@ void user_task() {
}
void ktask_main() {
struct tty_funcs serial_tty = {.putchar = write_serial};
add_tty(serial_tty);
GlobalTtyManager.add_tty(new SerialTty());
new_ktask(ktask, "one");
new_ktask(freeprinter, "freeprinter");
@@ -216,9 +216,9 @@ void ktask_main() {
new_ktask(stress_tester, "stress_tester");
for (int i = 0; i < saved_modules_size; i++) {
all_tty_putstr("Starting ");
all_tty_putstr(saved_modules_names[i]);
all_tty_putchar('\n');
GlobalTtyManager.all_tty_putstr("Starting ");
GlobalTtyManager.all_tty_putstr(saved_modules_names[i]);
GlobalTtyManager.all_tty_putchar('\n');
Task *utask = new_utask((void (*)()) 0x00020000, saved_modules_names[i]);
assert(saved_modules_size > 0);

View File

@@ -57,6 +57,17 @@ void write_serial(char a) {
outb(PORT, a);
}
void write_serial_no_yield(char a) {
while (is_transmit_empty() == 0) {
__builtin_ia32_pause();
}
outb(PORT, a);
}
void writestr(const char *a) {
while (*a != '\0') write_serial(*a++);
}
void writestr_no_yield(const char *a) {
while (*a != '\0') write_serial_no_yield(*a++);
}

View File

@@ -15,5 +15,7 @@ char read_serial();
int is_transmit_empty();
void write_serial(char a);
void writestr(const char *a);
void write_serial_no_yield(char a);
void writestr_no_yield(const char *a);
#endif//OS1_SERIAL_H

View File

@@ -7,10 +7,10 @@
#include <cstdint>
#include "TtyManager.hpp"
#include "asserts.hpp"
#include "gdt.hpp"
#include "misc.hpp"
#include "tty.hpp"
// Don't forget the correct order
// Shockingly, it doesn't immediately break and even something simple as putchar works
@@ -46,7 +46,7 @@ void setup_syscalls() {
}
uint64_t syscall_putchar(char c) {
all_tty_putchar(c);
GlobalTtyManager.all_tty_putchar(c);
return 0;
}

View File

@@ -6,6 +6,7 @@
#include "LockGuard.hpp"
#include "SkipList.hpp"
#include "Spinlock.hpp"
#include "TtyManager.hpp"
#include "VMA.hpp"
#include "asserts.hpp"
#include "gdt.hpp"
@@ -16,7 +17,6 @@
#include "paging.hpp"
#include "string.h"
#include "timer.hpp"
#include "tty.hpp"
char temp_fxsave[512] __attribute__((aligned(16)));

View File

@@ -1,49 +0,0 @@
//
// Created by Stepan Usatiuk on 25.08.2023.
//
#include "tty.hpp"
#include "LockGuard.hpp"
#include "Vector.hpp"
#include "asserts.hpp"
#include "kmem.hpp"
#include "mutex.hpp"
static Mutex ttysMutex;
Vector<tty> ttys;
void add_tty(tty_funcs funcs) {
LockGuard l(ttysMutex);
ttys.emplace_back(ttys.size(), funcs);
}
void tty_putchar(struct tty *tty, char c) {
LockGuard l(tty->lock);
tty->funcs.putchar(c);
}
void tty_putstr(struct tty *tty, const char *str) {
LockGuard l(tty->lock);
while (*str != '\0') tty->funcs.putchar(*str++);
}
void all_tty_putchar(char c) {
for (unsigned i = 0; i < get_num_ttys(); i++) { tty_putchar(get_tty(i), c); }
}
void all_tty_putstr(const char *str) {
for (unsigned i = 0; i < get_num_ttys(); i++) { tty_putstr(get_tty(i), str); }
}
unsigned get_num_ttys() {
return ttys.size();
}
struct tty *get_tty(unsigned n) {
if (n < get_num_ttys()) return &ttys[n];
else
return NULL;
}

View File

@@ -1,32 +0,0 @@
//
// Created by Stepan Usatiuk on 25.08.2023.
//
#ifndef OS1_TTY_H
#define OS1_TTY_H
#include "mutex.hpp"
struct tty_funcs {
void (*putchar)(char);
};
struct tty {
Mutex lock;
unsigned id;
tty_funcs funcs;
tty(unsigned id, tty_funcs funcs) : id(id), funcs(funcs) {}
};
void add_tty(struct tty_funcs);
void tty_putchar(struct tty *tty, char c);
void tty_putstr(struct tty *tty, const char *str);
void all_tty_putchar(char c);
void all_tty_putstr(const char *str);
unsigned get_num_ttys();
struct tty *get_tty(unsigned n);
#endif//OS1_TTY_H

View File

@@ -1,5 +1,5 @@
target_include_directories(kernel.elf PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_sources(kernel.elf PRIVATE mutex.cpp cppsupport.cpp Spinlock.cpp LockGuard.cpp rand.cpp VMA.cpp asserts.cpp)
target_sources(kernel.elf PRIVATE mutex.cpp cppsupport.cpp Spinlock.cpp LockGuard.cpp rand.cpp VMA.cpp asserts.cpp TtyManager.cpp Tty.cpp)
add_subdirectory(templates)

5
src/kernel/Tty.cpp Normal file
View File

@@ -0,0 +1,5 @@
//
// Created by Stepan Usatiuk on 26.11.2023.
//
#include "Tty.hpp"

17
src/kernel/Tty.hpp Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by Stepan Usatiuk on 26.11.2023.
//
#ifndef OS2_TTY_HPP
#define OS2_TTY_HPP
#include "mutex.hpp"
class Tty {
public:
virtual void putchar(char c) = 0;
virtual void putstr(const char *str) = 0;
};
#endif//OS2_TTY_HPP

36
src/kernel/TtyManager.cpp Normal file
View File

@@ -0,0 +1,36 @@
//
// Created by Stepan Usatiuk on 25.08.2023.
//
#include "TtyManager.hpp"
#include "LockGuard.hpp"
#include "Vector.hpp"
#include "mutex.hpp"
TtyManager GlobalTtyManager;
Vector<TtyManager> ttys;
void TtyManager::add_tty(Tty *tty) {
LockGuard l(lock);
ttys.emplace_back(tty);
}
void TtyManager::all_tty_putchar(char c) {
for (unsigned i = 0; i < get_num_ttys(); i++) { get_tty(i)->putchar(c); }
}
void TtyManager::all_tty_putstr(const char *str) {
for (unsigned i = 0; i < get_num_ttys(); i++) { get_tty(i)->putstr(str); }
}
unsigned TtyManager::get_num_ttys() {
return ttys.size();
}
Tty *TtyManager::get_tty(unsigned n) {
if (n < get_num_ttys()) return ttys[n];
else
return NULL;
}

28
src/kernel/TtyManager.hpp Normal file
View File

@@ -0,0 +1,28 @@
//
// Created by Stepan Usatiuk on 25.08.2023.
//
#ifndef OS1_TTY_H
#define OS1_TTY_H
#include "Spinlock.hpp"
#include "Tty.hpp"
#include "Vector.hpp"
class TtyManager {
Spinlock lock;
Vector<Tty *> ttys;
public:
void add_tty(Tty *tty);
void all_tty_putchar(char c);
void all_tty_putstr(const char *str);
unsigned get_num_ttys();
Tty *get_tty(unsigned n);
};
extern TtyManager GlobalTtyManager;
#endif//OS1_TTY_H

View File

@@ -11,7 +11,7 @@
extern "C" {
static inline void _assert2(int val, const char *msg) {
if (!val) {
writestr(msg);
writestr_no_yield(msg);
_hcf();
}
}

View File

@@ -79,7 +79,7 @@ void *__dso_handle = nullptr;
// Do we really care about destructors at kernel exit?
int __cxa_atexit(void (*f)(void *), void *objptr, void *dso) {
writestr("Something registered\n");
writestr_no_yield("Something registered\n");
return 0;
};
};

View File

@@ -63,7 +63,7 @@ void Mutex::lock() {
void Mutex::unlock() {
bool expected = true;
if (!locked.compare_exchange_strong(expected, false))
writestr("Unlocking an unlocked mutex!\n");
assert2(false, "Unlocking an unlocked mutex!\n");
List<Task *>::Node *t = nullptr;
{
LockGuard l(waiters_lock);

View File

@@ -13,7 +13,7 @@
#include "Vector.hpp"
#include "asserts.hpp"
#include "tty.hpp"
#include "TtyManager.hpp"
class SharedPtrTester {
private:
@@ -36,7 +36,7 @@ public:
assert(test2->size() == 2);
assert((*test2)[1] == "Thingy2");
// all_tty_putstr("SharedPtr tests ok!\n");
// GlobalTtyManager.all_tty_putstr("SharedPtr tests ok!\n");
return true;
}
};
@@ -80,7 +80,7 @@ public:
assert(test12.ptr.get() != test1.ptr.get());
assert(test22.ptr.get() != test2.ptr.get());
// all_tty_putstr("COWPointer tests ok!\n");
// GlobalTtyManager.all_tty_putstr("COWPointer tests ok!\n");
return true;
}
};
@@ -120,7 +120,7 @@ public:
assert(testv1[2] == "H6");
assert(testv2[2] == "H5");
// all_tty_putstr("Vector tests ok!\n");
// GlobalTtyManager.all_tty_putstr("Vector tests ok!\n");
return true;
}
};
@@ -140,7 +140,7 @@ public:
str2 = "abcd";
assert(str1 <= str2);
// all_tty_putstr("String tests ok!\n");
// GlobalTtyManager.all_tty_putstr("String tests ok!\n");
return true;
}
};
@@ -170,7 +170,7 @@ public:
test1.add(78, "test78", true);
assert(test1.find(78)->data == "test78");
// all_tty_putstr("SkipList tests ok!\n");
// GlobalTtyManager.all_tty_putstr("SkipList tests ok!\n");
return true;
}
};
@@ -200,7 +200,7 @@ public:
test1.add(78, true);
assert(test1.find(78)->key == 78);
// all_tty_putstr("SkipListSet tests ok!\n");
// GlobalTtyManager.all_tty_putstr("SkipListSet tests ok!\n");
return true;
}
};