From fe979e8774d87b3f5679d84bece876fae2c217d4 Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Fri, 15 Apr 2016 22:04:01 +0200 Subject: [PATCH] [x86] Did some various fixes to the ATA driver [dummy] Removed useless code files from dummy [x86] Moved Tasks to ring 3. --- kernel/hal/dummy/asm/snippets.S | 0 kernel/hal/dummy/boot/boot.S | 15 ------- kernel/hal/dummy/include/base.hpp | 2 + kernel/hal/dummy/include/blockdev.hpp | 20 ++++++++++ kernel/hal/dummy/init/Multitasking.cpp | 36 ----------------- kernel/hal/dummy/init/init.cpp | 32 --------------- kernel/hal/dummy/io/keyboard.cpp | 20 ---------- kernel/hal/dummy/io/output.cpp | 39 ------------------- kernel/hal/dummy/io/serial.cpp | 19 --------- kernel/hal/dummy/io/textDISP.cpp | 32 --------------- kernel/hal/dummy/string.c | 12 ------ kernel/hal/x86/asm/snippets.S | 12 +++++- .../{Multitasking.h => Multitasking.hpp} | 5 ++- .../x86/init/{MTGosHAL => }/Multitasking.cpp | 38 ++++++++++++------ kernel/hal/x86/init/init.cpp | 9 +++-- kernel/hal/x86/mm/pmm.cpp | 4 +- kernel/kernel/init.cpp | 4 +- 17 files changed, 73 insertions(+), 226 deletions(-) delete mode 100644 kernel/hal/dummy/asm/snippets.S delete mode 100644 kernel/hal/dummy/boot/boot.S create mode 100644 kernel/hal/dummy/include/blockdev.hpp delete mode 100644 kernel/hal/dummy/init/Multitasking.cpp delete mode 100644 kernel/hal/dummy/init/init.cpp delete mode 100644 kernel/hal/dummy/io/keyboard.cpp delete mode 100644 kernel/hal/dummy/io/output.cpp delete mode 100644 kernel/hal/dummy/io/serial.cpp delete mode 100644 kernel/hal/dummy/io/textDISP.cpp delete mode 100644 kernel/hal/dummy/string.c rename kernel/hal/x86/include/{Multitasking.h => Multitasking.hpp} (73%) rename kernel/hal/x86/init/{MTGosHAL => }/Multitasking.cpp (60%) diff --git a/kernel/hal/dummy/asm/snippets.S b/kernel/hal/dummy/asm/snippets.S deleted file mode 100644 index e69de29..0000000 diff --git a/kernel/hal/dummy/boot/boot.S b/kernel/hal/dummy/boot/boot.S deleted file mode 100644 index 60d416a..0000000 --- a/kernel/hal/dummy/boot/boot.S +++ /dev/null @@ -1,15 +0,0 @@ -.section multiboot -#define MB_MAGIC 0x1BADB002 -#define MB_FLAGS 0x0 -#define MB_CHECKSUM -(MB_MAGIC + MB_FLAGS) -.align 4 -.int MB_MAGIC -.int MB_FLAGS -.int MB_CHECKSUM -.section .text -.extern init -.global _start -_start: -.section .bss -.space 8192 -kernel_stack: diff --git a/kernel/hal/dummy/include/base.hpp b/kernel/hal/dummy/include/base.hpp index 60c1883..747af2e 100644 --- a/kernel/hal/dummy/include/base.hpp +++ b/kernel/hal/dummy/include/base.hpp @@ -8,6 +8,7 @@ namespace MTGosHAL { class Screen; class Keyboard; class Multitasking; + class BlockDevice; enum class BG_color: uint16_t; enum class FG_color: uint16_t; extern Serial debug; @@ -15,5 +16,6 @@ namespace MTGosHAL { extern Screen err; extern Keyboard in; extern Multitasking tasks; + extern BlockDevice disk; } #endif diff --git a/kernel/hal/dummy/include/blockdev.hpp b/kernel/hal/dummy/include/blockdev.hpp new file mode 100644 index 0000000..dbb6518 --- /dev/null +++ b/kernel/hal/dummy/include/blockdev.hpp @@ -0,0 +1,20 @@ +#ifndef __HAL_BLOCKDEV_HPP +#define __HAL_BLOCKDEV_HPP +#include +namespace MTGosHAL { + class BlockDevice { + private: + uint8_t numDevices; + public: + BlockDevice(); + ~BlockDevice(); + auto getDriveCnt() -> int32_t; + auto getDriveNumByName(const char *) -> int32_t; //Returns -1 if device is not existent + auto readSector(int32_t drv, uint64_t sectorNum, uint8_t *buf) -> void; //Has to be at least 512 bytes big! + auto readSectors(int32_t drv, uint64_t sectorNum, uint32_t num, uint8_t *buf) -> void; //Has to be at least num*512 bytes big! + + }; +} + + +#endif /* end of include guard: __HAL_BLOCKDEV_HPP */ diff --git a/kernel/hal/dummy/init/Multitasking.cpp b/kernel/hal/dummy/init/Multitasking.cpp deleted file mode 100644 index 98f1a09..0000000 --- a/kernel/hal/dummy/init/Multitasking.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -#include -/*auto schedule(struct cpu_state* cpu) -> struct cpu_state* { - return MTGosHAL::tasks.schedule(cpu); -}*/ -namespace MTGosHAL { - -Multitasking::Multitasking(): current_task(-1), num_tasks(2) -{ - task_states[0] = initTask(stack_a, task_a); - task_states[1] = initTask(stack_b, task_b); -} -auto Multitasking::initTask(uint8_t* stck, void(* entry)()) -> struct cpu_state* -{ - return nullptr; -} - -auto Multitasking::task_a() -> void -{ - while(true) - out << "A"; -} - -auto Multitasking::task_b() -> void -{ - while(true) - out << "B"; -} -auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state* -{ - return nullptr; -} - -} // namespace MTGosHAL diff --git a/kernel/hal/dummy/init/init.cpp b/kernel/hal/dummy/init/init.cpp deleted file mode 100644 index 58e110f..0000000 --- a/kernel/hal/dummy/init/init.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include -#include -#include -extern "C" void intr_stub_0(void); -namespace MTGosHAL { - Serial debug; - Screen out; - Screen err; - Keyboard in; - Multitasking tasks; - void main() { - out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; - err << BG_color::BLACK << FG_color::RED; - debug << "Hello debugger! This is MTGos v00r01\nThese logs are probably very long, so please redirect the output to a file.\n"; - for(;;); - } -} -typedef void (*constructor)(); -extern "C" constructor start_ctors; -extern "C" constructor end_ctors; -extern "C" void init() { - for(constructor* i = &start_ctors; i != &end_ctors; ++i) - (*i)(); - MTGosHAL::main(); -} -extern "C" void __cxa_pure_virtual() { - MTGosHAL::debug << "A pure virtual function just got called.\n"; -} - diff --git a/kernel/hal/dummy/io/keyboard.cpp b/kernel/hal/dummy/io/keyboard.cpp deleted file mode 100644 index 1a8c77b..0000000 --- a/kernel/hal/dummy/io/keyboard.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include -#include -auto handleIRQ(struct cpu_state* cpu) -> struct cpu_state* { - return cpu; -} -namespace MTGosHAL { - auto Keyboard::getChar() -> char { - return '\0'; - } - auto Keyboard::sendCommand(uint8_t command) -> void { - } - auto Keyboard::handleIRQ1(struct cpu_state* cpu) -> struct cpu_state* { - return cpu; - } - Keyboard::Keyboard(): numlock(true), capslock(false), scrolllock(false), response(false) { - } -} diff --git a/kernel/hal/dummy/io/output.cpp b/kernel/hal/dummy/io/output.cpp deleted file mode 100644 index dda21a7..0000000 --- a/kernel/hal/dummy/io/output.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include -#include -namespace MTGosHAL { - auto Output::puts(const char* s) -> void { - int i=0; - while(s[i]!='\0') - putChar(s[i++]); - } - template <> - auto Output::operator<<(Base output) -> Output & { - base=static_cast(output); - return *this; - } - template <> - auto Output::operator<<(int output) -> Output & { - const char* chars="0123456789ABCDEF"; - char buf[33]; - buf[32]='\0'; - char* ptr=buf+31; - do { - *(ptr--)=chars[output%base]; - output/=base; - } while(output && (ptr!=buf)); - puts(ptr+1); - return *this; - } - template <> - auto Output::operator<<(char output) -> Output & { - putChar(output); - return *this; - } - template <> - auto Output::operator<<(char* output) -> Output & { - puts(output); - return *this; - } -} diff --git a/kernel/hal/dummy/io/serial.cpp b/kernel/hal/dummy/io/serial.cpp deleted file mode 100644 index c5e0d08..0000000 --- a/kernel/hal/dummy/io/serial.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -namespace MTGosHAL { - auto Serial::isTransmitEmpty() -> int { - return 0; - } - auto Serial::putChar(char chr) -> void { - } - auto Serial::serial_received() -> int { - return 0; - } - auto Serial::getChar() -> char { - return '\0'; - } - Serial::Serial(): works(true) { - } - auto Serial::debug() -> void { - } -} diff --git a/kernel/hal/dummy/io/textDISP.cpp b/kernel/hal/dummy/io/textDISP.cpp deleted file mode 100644 index 26c2d7d..0000000 --- a/kernel/hal/dummy/io/textDISP.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -int x=0, y=0; -namespace MTGosHAL { - auto Screen::putChar(char c) -> void { - } - auto Screen::clrscr() -> void { - } - auto Screen::scroll() -> void { - } - template <> - auto Screen::operator<<(FG_color fg) -> Screen &{ - this->fg=fg; - return *this; - } - template <> - auto Screen::operator<<(BG_color bg) -> Screen &{ - this->bg=bg; - return *this; - } - auto Screen::setColor(FG_color fg) -> Screen &{ - this->fg=fg; - return *this; - } - auto Screen::setColor(BG_color bg) -> Screen &{ - this->bg=bg; - return *this; - } - auto Screen::setColor(FG_color fg, BG_color bg) -> Screen &{ - return (*this).setColor(fg).setColor(bg); - } -} diff --git a/kernel/hal/dummy/string.c b/kernel/hal/dummy/string.c deleted file mode 100644 index a9031c7..0000000 --- a/kernel/hal/dummy/string.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -__attribute__((optimize(0))) void memmove(void* dst, void* src, uint32_t size) { - uint8_t* from=(uint8_t*)src; - uint8_t* to=(uint8_t*)dst; - if((srcdst)) { - for(int i=size-1;i>=0;i--) - to[i]=from[i]; //This would get optimized by gcc to memmove(dst, src, size); if optimizations are enabled. - } else if(src != dst) { - for(int i=0;i struct cpu_state*; + uint32_t tss[32]; protected: private: - auto initTask(uint8_t* stck, void(*entry)()) -> struct cpu_state*; + auto initTask(uint8_t* stck, uint8_t* user_stck, void(*entry)()) -> struct cpu_state*; static auto task_a() -> void; static auto task_b() -> void; uint8_t stack_a[4096]; uint8_t stack_b[4096]; + uint8_t user_stack_a[4096]; + uint8_t user_stack_b[4096]; struct cpu_state* task_states[2]; int current_task, num_tasks; }; diff --git a/kernel/hal/x86/init/MTGosHAL/Multitasking.cpp b/kernel/hal/x86/init/Multitasking.cpp similarity index 60% rename from kernel/hal/x86/init/MTGosHAL/Multitasking.cpp rename to kernel/hal/x86/init/Multitasking.cpp index 063e726..b694b84 100644 --- a/kernel/hal/x86/init/MTGosHAL/Multitasking.cpp +++ b/kernel/hal/x86/init/Multitasking.cpp @@ -1,7 +1,8 @@ #include #include -#include +#include #include +#include auto schedule(struct cpu_state* cpu) -> struct cpu_state* { return MTGosHAL::tasks.schedule(cpu); } @@ -9,15 +10,21 @@ namespace MTGosHAL { Multitasking::Multitasking(): current_task(-1), num_tasks(2) { - task_states[0] = initTask(stack_a, task_a); - task_states[1] = initTask(stack_b, task_b); + for(int i=0;i<32;i++) { + if(i==2) + continue; + tss[i]=0; + } + tss[2]=0x10; + task_states[0] = initTask(stack_a, user_stack_a, task_a); + task_states[1] = initTask(stack_b, user_stack_b, task_b); if(!idt.request(0x20,::schedule)) { err << "Could not start multitasking\nFatal error; Kernel halted!\n"; while(true); asm volatile("cli; hlt"); } } -auto Multitasking::initTask(uint8_t* stck, void(* entry)()) -> struct cpu_state* +auto Multitasking::initTask(uint8_t* stck, uint8_t* user_stck, void(* entry)()) -> struct cpu_state* { struct cpu_state new_state = { 0, //EAX @@ -30,10 +37,10 @@ auto Multitasking::initTask(uint8_t* stck, void(* entry)()) -> struct cpu_state* 0, //INTR 0, //ERROR (uint32_t) entry, //EIP - 0x08, //CS + 0x18 | 0x03, //CS 0x202, // EFLAGS - (uint32_t) stck, //ESP - 0x10 //SS + (uint32_t) user_stck+4096, //ESP + 0x20 | 0x03 //SS }; struct cpu_state* state = (struct cpu_state*)(stck+4096-sizeof(new_state)); *state = new_state; @@ -42,14 +49,22 @@ auto Multitasking::initTask(uint8_t* stck, void(* entry)()) -> struct cpu_state* auto Multitasking::task_a() -> void { - while(true) - out << "A"; + while(true) { + char arr[513]; + arr[512]=0; + disk.readSector(disk.getDriveNumByName("ATA0m"),0,(uint8_t*)arr); + out << arr; + } } auto Multitasking::task_b() -> void { - while(true) - out << "B"; + while(true) { + char arr[513]; + arr[512]=0; + disk.readSector(disk.getDriveNumByName("ATA0m"),1,(uint8_t*)arr); + out << arr; + } } auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state* { @@ -59,6 +74,7 @@ auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state* current_task++; current_task%= num_tasks; cpu=task_states[current_task]; + tss[1]=(uint32_t)(cpu+1); return cpu; } diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index b8e20e3..0ff8fd0 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include extern "C" void intr_stub_0(void); @@ -30,9 +30,10 @@ namespace MTGosHAL { gdt.setEntry(2, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); gdt.setEntry(3, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT | GDT_FLAG_RING3); gdt.setEntry(4, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT | GDT_FLAG_RING3); - gdt.setEntry(5, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_TSS | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); - gdt.setEntry(6, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_TSS | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); + gdt.setEntry(5, (uint32_t)tasks.tss, sizeof(tasks.tss), GDT_FLAG_RING3 | GDT_FLAG_TSS | GDT_FLAG_PRESENT); + gdt.setEntry(6, 0, 0xfffff, GDT_FLAG_RING3 | GDT_FLAG_TSS | GDT_FLAG_PRESENT); gdt.apply(); + asm volatile("ltr %%ax" : : "a"(5<<3)); debug << "We are now creating the IDT.\n"; for(int i=0;i<256;i++) { idt.setEntry(i, (void *)((uint32_t)&intr_stub_0+i*16), SEG_KERNEL, IDT_INTERRUPT_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); @@ -40,8 +41,8 @@ namespace MTGosHAL { idt.setEntry(48, (void *)((uint32_t)&intr_stub_0+768), SEG_KERNEL, IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); idt.setEntry(8, (void *)((uint32_t)&intr_stub_0+128), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); idt.apply(); - sti(); ::main(); + sti(); for(;;); } } diff --git a/kernel/hal/x86/mm/pmm.cpp b/kernel/hal/x86/mm/pmm.cpp index 8249bd3..adc121c 100644 --- a/kernel/hal/x86/mm/pmm.cpp +++ b/kernel/hal/x86/mm/pmm.cpp @@ -10,7 +10,7 @@ PMM::PMM(struct multiboot_info * mb_info) { struct multiboot_mmap_entry* mmap_end = (struct multiboot_mmap_entry*) ((unsigned int) mb_info->mmap_addr + mb_info->mmap_length); while (mmap < mmap_end) { if (mmap->type == 1) { - // Der Speicherbereich ist frei, entsprechend markieren + // Memory is free uintptr_t addr = mmap->addr; uintptr_t end_addr = addr + mmap->len; while (addr < end_addr) { @@ -57,4 +57,4 @@ auto PMM::operator << (const void * addr) -> PMM & { bitmap[index]|=bit; return *this; } -} \ No newline at end of file +} diff --git a/kernel/kernel/init.cpp b/kernel/kernel/init.cpp index a7c077c..376eec3 100644 --- a/kernel/kernel/init.cpp +++ b/kernel/kernel/init.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +using namespace MTGosHAL; void main() { MTGosHAL::out << "Initializing Kernel!\n"; -} \ No newline at end of file +}