From e559af2008e73292216159c383be6b5a494dd8cc Mon Sep 17 00:00:00 2001 From: Liav A Date: Sun, 9 Feb 2020 16:47:15 +0200 Subject: [PATCH] Kernel: Apply changes to use LibBareMetal definitions --- Kernel/ACPI/ACPIStaticParser.cpp | 4 +- Kernel/ACPI/DMIDecoder.cpp | 2 +- Kernel/Arch/i386/APIC.cpp | 49 +++--- Kernel/Arch/i386/CPU.h | 2 +- Kernel/Arch/i386/PIC.cpp | 3 +- Kernel/Arch/i386/PIT.cpp | 2 +- Kernel/Assertions.h | 2 +- Kernel/CMOS.cpp | 2 +- Kernel/Console.cpp | 93 ---------- Kernel/Console.h | 64 ------- Kernel/Devices/BXVGADevice.cpp | 2 +- Kernel/Devices/DebugLogDevice.cpp | 2 +- Kernel/Devices/FloppyDiskDevice.cpp | 2 +- Kernel/Devices/KeyboardDevice.cpp | 2 +- Kernel/Devices/PATAChannel.cpp | 2 +- Kernel/Devices/PCSpeaker.cpp | 2 +- Kernel/Devices/PS2MouseDevice.cpp | 2 +- Kernel/Devices/SB16.cpp | 2 +- Kernel/Devices/SerialDevice.cpp | 6 +- Kernel/Devices/VMWareBackdoor.cpp | 2 +- Kernel/FileSystem/ProcFS.cpp | 4 +- Kernel/Heap/kmalloc.cpp | 5 +- Kernel/IO.h | 100 ----------- Kernel/Makefile | 8 +- Kernel/Net/E1000NetworkAdapter.cpp | 2 +- Kernel/Net/LocalSocket.cpp | 2 +- Kernel/Net/MACAddress.h | 4 +- Kernel/Net/NetworkAdapter.cpp | 2 +- Kernel/Net/RTL8139NetworkAdapter.cpp | 2 +- Kernel/PCI/IOAccess.cpp | 2 +- Kernel/PCI/Initializer.cpp | 2 +- Kernel/PCI/MMIOAccess.cpp | 7 +- Kernel/Process.cpp | 6 +- Kernel/StdLib.cpp | 254 --------------------------- Kernel/StdLib.h | 76 -------- Kernel/TTY/VirtualConsole.cpp | 6 +- Kernel/TTY/VirtualConsole.h | 2 +- Kernel/TestModule.cpp | 2 +- Kernel/VM/MemoryManager.cpp | 2 +- Kernel/VM/RangeAllocator.cpp | 20 ++- Kernel/init.cpp | 2 +- Kernel/kprintf.cpp | 163 ----------------- Kernel/kstdio.h | 56 ------ 43 files changed, 84 insertions(+), 892 deletions(-) delete mode 100644 Kernel/Console.cpp delete mode 100644 Kernel/Console.h delete mode 100644 Kernel/IO.h delete mode 100644 Kernel/StdLib.cpp delete mode 100644 Kernel/StdLib.h delete mode 100644 Kernel/kprintf.cpp delete mode 100644 Kernel/kstdio.h diff --git a/Kernel/ACPI/ACPIStaticParser.cpp b/Kernel/ACPI/ACPIStaticParser.cpp index e944182490..de88f69a5c 100644 --- a/Kernel/ACPI/ACPIStaticParser.cpp +++ b/Kernel/ACPI/ACPIStaticParser.cpp @@ -25,9 +25,9 @@ */ #include -#include -#include #include +#include +#include //#define ACPI_DEBUG diff --git a/Kernel/ACPI/DMIDecoder.cpp b/Kernel/ACPI/DMIDecoder.cpp index 801dc4e18c..a73f694116 100644 --- a/Kernel/ACPI/DMIDecoder.cpp +++ b/Kernel/ACPI/DMIDecoder.cpp @@ -25,8 +25,8 @@ */ #include -#include #include +#include static DMIDecoder* s_dmi_decoder; diff --git a/Kernel/Arch/i386/APIC.cpp b/Kernel/Arch/i386/APIC.cpp index 752bd9c56a..1990f1272e 100644 --- a/Kernel/Arch/i386/APIC.cpp +++ b/Kernel/Arch/i386/APIC.cpp @@ -26,10 +26,10 @@ #include #include -#include #include -#include +#include #include +#include #define IRQ_APIC_SPURIOUS 0x1f @@ -56,12 +56,11 @@ asm( namespace APIC { -class ICRReg -{ - u32 m_reg{0}; +class ICRReg { + u32 m_reg { 0 }; + public: - enum DeliveryMode - { + enum DeliveryMode { Fixed = 0x0, LowPriority = 0x1, SMI = 0x2, @@ -69,31 +68,27 @@ public: INIT = 0x5, StartUp = 0x6, }; - enum DestinationMode - { + enum DestinationMode { Physical = 0x0, Logical = 0x0, }; - enum Level - { + enum Level { DeAssert = 0x0, Assert = 0x1 }; - enum class TriggerMode - { + enum class TriggerMode { Edge = 0x0, Level = 0x1, }; - enum DestinationShorthand - { + enum DestinationShorthand { NoShorthand = 0x0, Self = 0x1, AllIncludingSelf = 0x2, AllExcludingSelf = 0x3, }; - ICRReg(u8 vector, DeliveryMode delivery_mode, DestinationMode destination_mode, Level level, TriggerMode trigger_mode, DestinationShorthand destination): - m_reg(vector | (delivery_mode << 8) | (destination_mode << 11) | (level << 14) | (static_cast(trigger_mode) << 15) | (destination << 18)) + ICRReg(u8 vector, DeliveryMode delivery_mode, DestinationMode destination_mode, Level level, TriggerMode trigger_mode, DestinationShorthand destination) + : m_reg(vector | (delivery_mode << 8) | (destination_mode << 11) | (level << 14) | (static_cast(trigger_mode) << 15) | (destination << 18)) { } @@ -180,38 +175,40 @@ bool init() void enable(u32 cpu) { kprintf("Enabling local APIC for cpu #%u\n", cpu); - + // set spurious interrupt vector apic_write(APIC_REG_SIV, apic_read(APIC_REG_SIV) | 0x100); - + // local destination mode (flat mode) apic_write(APIC_REG_DF, 0xf000000); - + // set destination id (note that this limits it to 8 cpus) apic_write(APIC_REG_LD, (1 << cpu) << 24); - + register_interrupt_handler(IRQ_APIC_SPURIOUS, apic_spurious_interrupt_entry); - + apic_write(APIC_REG_LVT_TIMER, APIC_LVT(0xff, 0) | APIC_LVT_MASKED); apic_write(APIC_REG_LVT_THERMAL, APIC_LVT(0xff, 0) | APIC_LVT_MASKED); apic_write(APIC_REG_LVT_PERFORMANCE_COUNTER, APIC_LVT(0xff, 0) | APIC_LVT_MASKED); apic_write(APIC_REG_LVT_LINT0, APIC_LVT(0x1f, 7) | APIC_LVT_MASKED); apic_write(APIC_REG_LVT_LINT1, APIC_LVT(0xff, 4) | APIC_LVT_TRIGGER_LEVEL); // nmi apic_write(APIC_REG_LVT_ERR, APIC_LVT(0xe3, 0) | APIC_LVT_MASKED); - + if (cpu == 0) { static volatile u32 foo = 0; - + // INIT apic_write_icr(ICRReg(0, ICRReg::INIT, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf)); - for (foo = 0; foo < 0x800000; foo++); // TODO: 10 millisecond delay + for (foo = 0; foo < 0x800000; foo++) + ; // TODO: 10 millisecond delay for (int i = 0; i < 2; i++) { // SIPI apic_write_icr(ICRReg(0x08, ICRReg::StartUp, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf)); // start execution at P8000 - for (foo = 0; foo < 0x80000; foo++); // TODO: 200 microsecond delay + for (foo = 0; foo < 0x80000; foo++) + ; // TODO: 200 microsecond delay } } } diff --git a/Kernel/Arch/i386/CPU.h b/Kernel/Arch/i386/CPU.h index a9c93d51d4..53bd135b8a 100644 --- a/Kernel/Arch/i386/CPU.h +++ b/Kernel/Arch/i386/CPU.h @@ -30,7 +30,7 @@ #include #include #include -#include +#include #define PAGE_SIZE 4096 #define PAGE_MASK ((uintptr_t)0xfffff000u) diff --git a/Kernel/Arch/i386/PIC.cpp b/Kernel/Arch/i386/PIC.cpp index 2b62494b5a..abffcbc484 100644 --- a/Kernel/Arch/i386/PIC.cpp +++ b/Kernel/Arch/i386/PIC.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include // The slave 8259 is connected to the master's IRQ2 line. // This is really only to enhance clarity. @@ -137,4 +137,3 @@ u16 get_irr() } } - diff --git a/Kernel/Arch/i386/PIT.cpp b/Kernel/Arch/i386/PIT.cpp index 5f1df7f809..6806744a04 100644 --- a/Kernel/Arch/i386/PIT.cpp +++ b/Kernel/Arch/i386/PIT.cpp @@ -27,8 +27,8 @@ #include #include #include -#include #include +#include #define IRQ_TIMER 0 diff --git a/Kernel/Assertions.h b/Kernel/Assertions.h index 4251072026..9f2753e3dc 100644 --- a/Kernel/Assertions.h +++ b/Kernel/Assertions.h @@ -27,7 +27,7 @@ #pragma once #include -#include +#include #ifdef DEBUG [[noreturn]] void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); diff --git a/Kernel/CMOS.cpp b/Kernel/CMOS.cpp index 621e77471d..328d52dd81 100644 --- a/Kernel/CMOS.cpp +++ b/Kernel/CMOS.cpp @@ -25,7 +25,7 @@ */ #include -#include +#include namespace CMOS { diff --git a/Kernel/Console.cpp b/Kernel/Console.cpp deleted file mode 100644 index 9d277e6961..0000000000 --- a/Kernel/Console.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include - -// Bytes output to 0xE9 end up on the Bochs console. It's very handy. -#define CONSOLE_OUT_TO_E9 - -static Console* s_the; - -Console& Console::the() -{ - ASSERT(s_the); - return *s_the; -} - -bool Console::is_initialized() -{ - return s_the != nullptr; -} - -Console::Console() - : CharacterDevice(5, 1) -{ - s_the = this; -} - -Console::~Console() -{ -} - -bool Console::can_read(const FileDescription&) const -{ - return false; -} - -ssize_t Console::read(FileDescription&, u8*, ssize_t) -{ - // FIXME: Implement reading from the console. - // Maybe we could use a ring buffer for this device? - return 0; -} - -ssize_t Console::write(FileDescription&, const u8* data, ssize_t size) -{ - if (!size) - return 0; - if (!m_implementation) - return 0; - for (ssize_t i = 0; i < size; ++i) - put_char(data[i]); - return size; -} - -void Console::put_char(char ch) -{ -#ifdef CONSOLE_OUT_TO_E9 - //if (ch != 27) - IO::out8(0xe9, ch); -#endif - m_logbuffer.enqueue(ch); - if (m_implementation) - m_implementation->on_sysconsole_receive(ch); -} - -ConsoleImplementation::~ConsoleImplementation() -{ -} diff --git a/Kernel/Console.h b/Kernel/Console.h deleted file mode 100644 index 32a5d9ae68..0000000000 --- a/Kernel/Console.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include -#include -#include - -class ConsoleImplementation { -public: - virtual ~ConsoleImplementation(); - virtual void on_sysconsole_receive(u8) = 0; -}; - -class Console final : public CharacterDevice { - AK_MAKE_ETERNAL -public: - static Console& the(); - static bool is_initialized(); - - Console(); - virtual ~Console() override; - - // ^CharacterDevice - virtual bool can_read(const FileDescription&) const override; - virtual bool can_write(const FileDescription&) const override { return true; } - virtual ssize_t read(FileDescription&, u8*, ssize_t) override; - virtual ssize_t write(FileDescription&, const u8*, ssize_t) override; - virtual const char* class_name() const override { return "Console"; } - - void set_implementation(ConsoleImplementation* implementation) { m_implementation = implementation; } - - void put_char(char); - - const CircularQueue& logbuffer() const { return m_logbuffer; } - -private: - ConsoleImplementation* m_implementation { nullptr }; - CircularQueue m_logbuffer; -}; diff --git a/Kernel/Devices/BXVGADevice.cpp b/Kernel/Devices/BXVGADevice.cpp index 474b8a897f..1503c0d57b 100644 --- a/Kernel/Devices/BXVGADevice.cpp +++ b/Kernel/Devices/BXVGADevice.cpp @@ -25,11 +25,11 @@ */ #include -#include #include #include #include #include +#include #include #include diff --git a/Kernel/Devices/DebugLogDevice.cpp b/Kernel/Devices/DebugLogDevice.cpp index 9e4222a8c0..41646a59b7 100644 --- a/Kernel/Devices/DebugLogDevice.cpp +++ b/Kernel/Devices/DebugLogDevice.cpp @@ -25,7 +25,7 @@ */ #include -#include +#include static DebugLogDevice* s_the; diff --git a/Kernel/Devices/FloppyDiskDevice.cpp b/Kernel/Devices/FloppyDiskDevice.cpp index 3e9e4a3128..ca8ab87d41 100644 --- a/Kernel/Devices/FloppyDiskDevice.cpp +++ b/Kernel/Devices/FloppyDiskDevice.cpp @@ -27,9 +27,9 @@ #include #include #include -#include #include #include +#include // Uncomment me for a LOT of output //#define FLOPPY_DEBUG diff --git a/Kernel/Devices/KeyboardDevice.cpp b/Kernel/Devices/KeyboardDevice.cpp index 86c862057e..092aba10dc 100644 --- a/Kernel/Devices/KeyboardDevice.cpp +++ b/Kernel/Devices/KeyboardDevice.cpp @@ -29,8 +29,8 @@ #include #include #include -#include #include +#include //#define KEYBOARD_DEBUG diff --git a/Kernel/Devices/PATAChannel.cpp b/Kernel/Devices/PATAChannel.cpp index b57fe42c1c..7808fb8fca 100644 --- a/Kernel/Devices/PATAChannel.cpp +++ b/Kernel/Devices/PATAChannel.cpp @@ -28,9 +28,9 @@ #include #include #include -#include #include #include +#include #define PATA_PRIMARY_IRQ 14 #define PATA_SECONDARY_IRQ 15 diff --git a/Kernel/Devices/PCSpeaker.cpp b/Kernel/Devices/PCSpeaker.cpp index 0cec54cc27..c76bd9f3c0 100644 --- a/Kernel/Devices/PCSpeaker.cpp +++ b/Kernel/Devices/PCSpeaker.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include void PCSpeaker::tone_on(int frequency) { diff --git a/Kernel/Devices/PS2MouseDevice.cpp b/Kernel/Devices/PS2MouseDevice.cpp index 01e8e2f91c..3a4a005dbd 100644 --- a/Kernel/Devices/PS2MouseDevice.cpp +++ b/Kernel/Devices/PS2MouseDevice.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #define IRQ_MOUSE 12 #define I8042_BUFFER 0x60 diff --git a/Kernel/Devices/SB16.cpp b/Kernel/Devices/SB16.cpp index f015adcc10..7e12691217 100644 --- a/Kernel/Devices/SB16.cpp +++ b/Kernel/Devices/SB16.cpp @@ -25,10 +25,10 @@ */ #include -#include #include #include #include +#include //#define SB16_DEBUG diff --git a/Kernel/Devices/SerialDevice.cpp b/Kernel/Devices/SerialDevice.cpp index 96fecd7ab0..c84d4ded06 100644 --- a/Kernel/Devices/SerialDevice.cpp +++ b/Kernel/Devices/SerialDevice.cpp @@ -25,7 +25,7 @@ */ #include -#include +#include SerialDevice::SerialDevice(int base_addr, unsigned minor) : CharacterDevice(4, minor) @@ -95,8 +95,8 @@ void SerialDevice::set_baud(Baud baud) m_baud = baud; IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) | 0x80); // turn on DLAB - IO::out8(m_base_addr + 0, ((char)(baud)) >> 2); // lower half of divisor - IO::out8(m_base_addr + 1, ((char)(baud)) & 0xff); // upper half of divisor + IO::out8(m_base_addr + 0, ((char)(baud)) >> 2); // lower half of divisor + IO::out8(m_base_addr + 1, ((char)(baud)) & 0xff); // upper half of divisor IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & 0x7f); // turn off DLAB } diff --git a/Kernel/Devices/VMWareBackdoor.cpp b/Kernel/Devices/VMWareBackdoor.cpp index 3a7621e890..3467ac0a36 100644 --- a/Kernel/Devices/VMWareBackdoor.cpp +++ b/Kernel/Devices/VMWareBackdoor.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #define VMWARE_CMD_GETVERSION 0x0a diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 8c7ded1ab2..add5f7db7c 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -25,11 +25,9 @@ */ #include "ProcFS.h" -#include "Console.h" #include "KSyms.h" #include "Process.h" #include "Scheduler.h" -#include "StdLib.h" #include #include #include @@ -52,6 +50,8 @@ #include #include #include +#include +#include #include enum ProcParentDirectory { diff --git a/Kernel/Heap/kmalloc.cpp b/Kernel/Heap/kmalloc.cpp index 83f1348f5d..9c2a259e40 100644 --- a/Kernel/Heap/kmalloc.cpp +++ b/Kernel/Heap/kmalloc.cpp @@ -32,11 +32,11 @@ #include #include #include +#include #include #include #include -#include -#include +#include #define SANITIZE_KMALLOC @@ -231,4 +231,3 @@ void* operator new[](size_t size) { return kmalloc(size); } - diff --git a/Kernel/IO.h b/Kernel/IO.h deleted file mode 100644 index 0d59496d55..0000000000 --- a/Kernel/IO.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include -#include -#include - -namespace IO { - -inline u8 in8(u16 port) -{ - u8 value; - asm volatile("inb %1, %0" - : "=a"(value) - : "Nd"(port)); - return value; -} - -inline u16 in16(u16 port) -{ - u16 value; - asm volatile("inw %1, %0" - : "=a"(value) - : "Nd"(port)); - return value; -} - -inline u32 in32(u16 port) -{ - u32 value; - asm volatile("inl %1, %0" - : "=a"(value) - : "Nd"(port)); - return value; -} - -inline void repeated_in16(u16 port, u8* buffer, int buffer_size) -{ - asm volatile("rep insw" - : "+D"(buffer), "+c"(buffer_size) - : "d"(port) - : "memory"); -} - -inline void out8(u16 port, u8 value) -{ - asm volatile("outb %0, %1" ::"a"(value), "Nd"(port)); -} - -inline void out16(u16 port, u16 value) -{ - asm volatile("outw %0, %1" ::"a"(value), "Nd"(port)); -} - -inline void out32(u16 port, u32 value) -{ - asm volatile("outl %0, %1" ::"a"(value), "Nd"(port)); -} - -inline void repeated_out16(u16 port, const u8* data, int data_size) -{ - asm volatile("rep outsw" - : "+S"(data), "+c"(data_size) - : "d"(port)); -} - -inline void delay() -{ - // ~3 microsecs - for (auto i = 0; i < 32; i++) { - IO::in8(0x80); - } -} - -} diff --git a/Kernel/Makefile b/Kernel/Makefile index 4e154527b7..835646a4d0 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -9,12 +9,14 @@ OBJS = \ ../AK/StringView.o \ ../Libraries/LibELF/ELFImage.o \ ../Libraries/LibELF/ELFLoader.o \ + ../Libraries/LibBareMetal/Output/Console.o \ + ../Libraries/LibBareMetal/Output/kprintf.o \ + ../Libraries/LibBareMetal/StdLib.o \ Arch/i386/APIC.o \ Arch/i386/CPU.o \ Arch/i386/PIC.o \ Arch/i386/PIT.o \ CMOS.o \ - Console.o \ Devices/BXVGADevice.o \ Devices/BlockDevice.o \ Devices/CharacterDevice.o \ @@ -83,7 +85,6 @@ OBJS = \ Random.o \ Scheduler.o \ SharedBuffer.o \ - StdLib.o \ Syscall.o \ TimerQueue.o \ TTY/MasterPTY.o \ @@ -107,8 +108,7 @@ OBJS = \ ACPI/ACPIDynamicParser.o \ ACPI/DMIDecoder.o \ WaitQueue.o \ - init.o \ - kprintf.o + init.o OBJ_SUFFIX = .kernel diff --git a/Kernel/Net/E1000NetworkAdapter.cpp b/Kernel/Net/E1000NetworkAdapter.cpp index d4f05871c3..57ad91620b 100644 --- a/Kernel/Net/E1000NetworkAdapter.cpp +++ b/Kernel/Net/E1000NetworkAdapter.cpp @@ -24,9 +24,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include +#include //#define E1000_DEBUG diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index a364336799..c1dbbe05b0 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -29,8 +29,8 @@ #include #include #include -#include #include +#include #include //#define DEBUG_LOCAL_SOCKET diff --git a/Kernel/Net/MACAddress.h b/Kernel/Net/MACAddress.h index 38383597aa..e6456a6677 100644 --- a/Kernel/Net/MACAddress.h +++ b/Kernel/Net/MACAddress.h @@ -26,10 +26,10 @@ #pragma once -#include #include +#include #include -#include +#include class [[gnu::packed]] MACAddress { diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp index a82f7edb84..ff34fe03eb 100644 --- a/Kernel/Net/NetworkAdapter.cpp +++ b/Kernel/Net/NetworkAdapter.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include static Lockable>& all_adapters() { diff --git a/Kernel/Net/RTL8139NetworkAdapter.cpp b/Kernel/Net/RTL8139NetworkAdapter.cpp index d4686e7dcc..407c42bc66 100644 --- a/Kernel/Net/RTL8139NetworkAdapter.cpp +++ b/Kernel/Net/RTL8139NetworkAdapter.cpp @@ -24,8 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include +#include //#define RTL8139_DEBUG diff --git a/Kernel/PCI/IOAccess.cpp b/Kernel/PCI/IOAccess.cpp index 04a698dc6e..d2465cb0dd 100644 --- a/Kernel/PCI/IOAccess.cpp +++ b/Kernel/PCI/IOAccess.cpp @@ -24,8 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include +#include void PCI::IOAccess::initialize() { diff --git a/Kernel/PCI/Initializer.cpp b/Kernel/PCI/Initializer.cpp index 0c6c97cf63..a65554c194 100644 --- a/Kernel/PCI/Initializer.cpp +++ b/Kernel/PCI/Initializer.cpp @@ -25,13 +25,13 @@ */ #include -#include #include #include #include #include #include #include +#include static PCI::Initializer* s_pci_initializer; diff --git a/Kernel/PCI/MMIOAccess.cpp b/Kernel/PCI/MMIOAccess.cpp index c182101921..7fc1ca857d 100644 --- a/Kernel/PCI/MMIOAccess.cpp +++ b/Kernel/PCI/MMIOAccess.cpp @@ -25,7 +25,6 @@ */ #include -#include #include #include @@ -60,11 +59,11 @@ PCI::MMIOAccess::MMIOAccess(ACPI_RAW::MCFG& raw_mcfg) kprintf("PCI: Using MMIO Mechanism for PCI Configuartion Space Access\n"); m_mmio_window_region = MM.allocate_kernel_region(PAGE_ROUND_UP(PCI_MMIO_CONFIG_SPACE_SIZE), "PCI MMIO", Region::Access::Read | Region::Access::Write); - auto checkup_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)&raw_mcfg)),(PAGE_SIZE * 2), "PCI MCFG Checkup", Region::Access::Read | Region::Access::Write); + auto checkup_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)&raw_mcfg)), (PAGE_SIZE * 2), "PCI MCFG Checkup", Region::Access::Read | Region::Access::Write); #ifdef PCI_DEBUG dbgprintf("PCI: Checking MCFG Table length to choose the correct mapping size\n"); #endif - + ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)checkup_region->vaddr().offset(offset_in_page((u32)&raw_mcfg)).as_ptr(); u32 length = sdt->length; u8 revision = sdt->revision; @@ -114,7 +113,7 @@ void PCI::MMIOAccess::map_device(Address address) #ifdef PCI_DEBUG dbgprintf("PCI: Mapping device @ pci (%w:%b:%b.%b), V 0x%x, P 0x%x\n", address.seg(), address.bus(), address.slot(), address.function(), m_mmio_window_region->vaddr().get(), device_physical_mmio_space.get()); #endif - m_mmio_window_region->vmobject().physical_pages()[0] = PhysicalPage::create(device_physical_mmio_space,false,false); + m_mmio_window_region->vmobject().physical_pages()[0] = PhysicalPage::create(device_physical_mmio_space, false, false); m_mmio_window_region->remap(); m_mapped_address = address; } diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 240cc83947..6ee08c341a 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -48,7 +47,6 @@ #include #include #include -#include #include #include #include @@ -62,12 +60,14 @@ #include #include #include -#include #include #include #include #include #include +#include +#include +#include #include #include #include diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp deleted file mode 100644 index 6c4d671899..0000000000 --- a/Kernel/StdLib.cpp +++ /dev/null @@ -1,254 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -String copy_string_from_user(const char* user_str, size_t user_str_size) -{ - SmapDisabler disabler; - size_t length = strnlen(user_str, user_str_size); - return String(user_str, length); -} - -extern "C" { - -void copy_to_user(void* dest_ptr, const void* src_ptr, size_t n) -{ - ASSERT(is_user_range(VirtualAddress(dest_ptr), n)); - SmapDisabler disabler; - memcpy(dest_ptr, src_ptr, n); -} - -void copy_from_user(void* dest_ptr, const void* src_ptr, size_t n) -{ - ASSERT(is_user_range(VirtualAddress(src_ptr), n)); - SmapDisabler disabler; - memcpy(dest_ptr, src_ptr, n); -} - -void* memcpy(void* dest_ptr, const void* src_ptr, size_t n) -{ - size_t dest = (size_t)dest_ptr; - size_t src = (size_t)src_ptr; - // FIXME: Support starting at an unaligned address. - if (!(dest & 0x3) && !(src & 0x3) && n >= 12) { - size_t size_ts = n / sizeof(size_t); - asm volatile( - "rep movsl\n" - : "=S"(src), "=D"(dest) - : "S"(src), "D"(dest), "c"(size_ts) - : "memory"); - n -= size_ts * sizeof(size_t); - if (n == 0) - return dest_ptr; - } - asm volatile( - "rep movsb\n" ::"S"(src), "D"(dest), "c"(n) - : "memory"); - return dest_ptr; -} - -void* memmove(void* dest, const void* src, size_t n) -{ - if (dest < src) - return memcpy(dest, src, n); - - u8* pd = (u8*)dest; - const u8* ps = (const u8*)src; - for (pd += n, ps += n; n--;) - *--pd = *--ps; - return dest; -} - -char* strcpy(char* dest, const char* src) -{ - auto* dest_ptr = dest; - auto* src_ptr = src; - while ((*dest_ptr++ = *src_ptr++) != '\0') - ; - return dest; -} - -char* strncpy(char* dest, const char* src, size_t n) -{ - size_t i; - for (i = 0; i < n && src[i] != '\0'; ++i) - dest[i] = src[i]; - for (; i < n; ++i) - dest[i] = '\0'; - return dest; -} - -void memset_user(void* dest_ptr, int c, size_t n) -{ - ASSERT(is_user_range(VirtualAddress(dest_ptr), n)); - SmapDisabler disabler; - memset(dest_ptr, c, n); -} - -void* memset(void* dest_ptr, int c, size_t n) -{ - size_t dest = (size_t)dest_ptr; - // FIXME: Support starting at an unaligned address. - if (!(dest & 0x3) && n >= 12) { - size_t size_ts = n / sizeof(size_t); - size_t expanded_c = (u8)c; - expanded_c |= expanded_c << 8; - expanded_c |= expanded_c << 16; - asm volatile( - "rep stosl\n" - : "=D"(dest) - : "D"(dest), "c"(size_ts), "a"(expanded_c) - : "memory"); - n -= size_ts * sizeof(size_t); - if (n == 0) - return dest_ptr; - } - asm volatile( - "rep stosb\n" - : "=D"(dest), "=c"(n) - : "0"(dest), "1"(n), "a"(c) - : "memory"); - return dest_ptr; -} - -char* strrchr(const char* str, int ch) -{ - char* last = nullptr; - char c; - for (; (c = *str); ++str) { - if (c == ch) - last = const_cast(str); - } - return last; -} - -size_t strlen(const char* str) -{ - size_t len = 0; - while (*(str++)) - ++len; - return len; -} - -size_t strnlen(const char* str, size_t maxlen) -{ - size_t len = 0; - for (; len < maxlen && *str; str++) - len++; - return len; -} - -int strcmp(const char* s1, const char* s2) -{ - for (; *s1 == *s2; ++s1, ++s2) { - if (*s1 == 0) - return 0; - } - return *(const u8*)s1 < *(const u8*)s2 ? -1 : 1; -} - -char* strdup(const char* str) -{ - size_t len = strlen(str); - char* new_str = (char*)kmalloc(len + 1); - strcpy(new_str, str); - return new_str; -} - -int memcmp(const void* v1, const void* v2, size_t n) -{ - auto* s1 = (const u8*)v1; - auto* s2 = (const u8*)v2; - while (n-- > 0) { - if (*s1++ != *s2++) - return s1[-1] < s2[-1] ? -1 : 1; - } - return 0; -} - -int strncmp(const char* s1, const char* s2, size_t n) -{ - if (!n) - return 0; - do { - if (*s1 != *s2++) - return *(const unsigned char*)s1 - *(const unsigned char*)--s2; - if (*s1++ == 0) - break; - } while (--n); - return 0; -} - -char* strstr(const char* haystack, const char* needle) -{ - char nch; - char hch; - - if ((nch = *needle++) != 0) { - size_t len = strlen(needle); - do { - do { - if ((hch = *haystack++) == 0) - return nullptr; - } while (hch != nch); - } while (strncmp(haystack, needle, len) != 0); - --haystack; - } - return const_cast(haystack); -} - -[[noreturn]] void __cxa_pure_virtual() -{ - ASSERT_NOT_REACHED(); -} - -void* realloc(void* p, size_t s) -{ - return krealloc(p, s); -} - -void free(void* p) -{ - return kfree(p); -} - -[[noreturn]] void __stack_chk_fail() -{ - ASSERT_NOT_REACHED(); -} - -[[noreturn]] void __stack_chk_fail_local() -{ - ASSERT_NOT_REACHED(); -} -} diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h deleted file mode 100644 index 3d2063f617..0000000000 --- a/Kernel/StdLib.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -namespace AK { -class String; -} - -namespace Syscall { -struct StringArgument; -} - -AK::String copy_string_from_user(const char*, size_t); - -extern "C" { - -static_assert(sizeof(size_t) == 4); - -void copy_to_user(void*, const void*, size_t); -void copy_from_user(void*, const void*, size_t); -void memset_user(void*, int, size_t); - -void* memcpy(void*, const void*, size_t); -char* strcpy(char*, const char*); -char* strncpy(char*, const char*, size_t); -int strncmp(const char* s1, const char* s2, size_t n); -int strcmp(char const*, const char*); -size_t strlen(const char*); -size_t strnlen(const char*, size_t); -void* memset(void*, int, size_t); -char* strdup(const char*); -int memcmp(const void*, const void*, size_t); -char* strrchr(const char* str, int ch); -void* memmove(void* dest, const void* src, size_t n); - -inline u16 ntohs(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); } -inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); } -} - -template -inline void copy_from_user(T* dest, const T* src) -{ - copy_from_user(dest, src, sizeof(T)); -} - -template -inline void copy_to_user(T* dest, const T* src) -{ - copy_to_user(dest, src, sizeof(T)); -} diff --git a/Kernel/TTY/VirtualConsole.cpp b/Kernel/TTY/VirtualConsole.cpp index c113d6bf67..7fdea47553 100644 --- a/Kernel/TTY/VirtualConsole.cpp +++ b/Kernel/TTY/VirtualConsole.cpp @@ -25,12 +25,12 @@ */ #include "VirtualConsole.h" -#include "IO.h" -#include "StdLib.h" -#include #include #include #include +#include +#include +#include static u8* s_vga_buffer; static VirtualConsole* s_consoles[6]; diff --git a/Kernel/TTY/VirtualConsole.h b/Kernel/TTY/VirtualConsole.h index 5398f9d885..7f173eb39b 100644 --- a/Kernel/TTY/VirtualConsole.h +++ b/Kernel/TTY/VirtualConsole.h @@ -26,9 +26,9 @@ #pragma once -#include "Console.h" #include #include +#include class VirtualConsole final : public TTY , public KeyboardClient diff --git a/Kernel/TestModule.cpp b/Kernel/TestModule.cpp index b37d93a67a..b15ae5ce3a 100644 --- a/Kernel/TestModule.cpp +++ b/Kernel/TestModule.cpp @@ -24,8 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include +#include extern "C" const char module_name[] = "TestModule"; diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp index aa50e71757..9fb49e59fc 100644 --- a/Kernel/VM/MemoryManager.cpp +++ b/Kernel/VM/MemoryManager.cpp @@ -26,7 +26,6 @@ #include "CMOS.h" #include "Process.h" -#include "StdLib.h" #include #include #include @@ -36,6 +35,7 @@ #include #include #include +#include //#define MM_DEBUG //#define PAGE_FAULT_DEBUG diff --git a/Kernel/VM/RangeAllocator.cpp b/Kernel/VM/RangeAllocator.cpp index d28faa6c06..232f40c7ec 100644 --- a/Kernel/VM/RangeAllocator.cpp +++ b/Kernel/VM/RangeAllocator.cpp @@ -27,9 +27,9 @@ #include #include #include -#include -#include #include +#include +#include //#define VRA_DEBUG #define VM_GUARD_PAGES @@ -162,18 +162,22 @@ void RangeAllocator::deallocate(Range range) ASSERT(!m_available_ranges.is_empty()); int nearby_index = 0; - auto* existing_range = binary_search(m_available_ranges.data(), m_available_ranges.size(), range, [](auto& a, auto& b) { - return a.base().get() - b.end().get(); - }, &nearby_index); + auto* existing_range = binary_search( + m_available_ranges.data(), m_available_ranges.size(), range, [](auto& a, auto& b) { + return a.base().get() - b.end().get(); + }, + &nearby_index); int inserted_index = 0; if (existing_range) { existing_range->m_size += range.size(); inserted_index = nearby_index; } else { - m_available_ranges.insert_before_matching(Range(range), [&](auto& entry) { - return entry.base() >= range.end(); - }, nearby_index, &inserted_index); + m_available_ranges.insert_before_matching( + Range(range), [&](auto& entry) { + return entry.base() >= range.end(); + }, + nearby_index, &inserted_index); } if (inserted_index < (m_available_ranges.size() - 1)) { diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 122ec63eba..76c6268340 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -29,7 +29,6 @@ #include "Process.h" #include "RTC.h" #include "Scheduler.h" -#include "kstdio.h" #include #include #include @@ -71,6 +70,7 @@ #include #include #include +#include [[noreturn]] static void init_stage2(); static void setup_serial_debug(); diff --git a/Kernel/kprintf.cpp b/Kernel/kprintf.cpp deleted file mode 100644 index 2701aafe82..0000000000 --- a/Kernel/kprintf.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -static bool serial_debug; - -void set_serial_debug(bool on_or_off) -{ - serial_debug = on_or_off; -} - -int get_serial_debug() -{ - return serial_debug; -} - -static void color_on() -{ - IO::out8(0xe9, 0x1b); - IO::out8(0xe9, '['); - IO::out8(0xe9, '3'); - IO::out8(0xe9, '6'); - IO::out8(0xe9, 'm'); -} - -static void color_off() -{ - IO::out8(0xe9, 0x1b); - IO::out8(0xe9, '['); - IO::out8(0xe9, '0'); - IO::out8(0xe9, 'm'); -} - -static void serial_putch(char ch) -{ - static bool serial_ready = false; - static bool was_cr = false; - - if (!serial_ready) { - IO::out8(0x3F8 + 1, 0x00); - IO::out8(0x3F8 + 3, 0x80); - IO::out8(0x3F8 + 0, 0x02); - IO::out8(0x3F8 + 1, 0x00); - IO::out8(0x3F8 + 3, 0x03); - IO::out8(0x3F8 + 2, 0xC7); - IO::out8(0x3F8 + 4, 0x0B); - - serial_ready = true; - } - - while ((IO::in8(0x3F8 + 5) & 0x20) == 0) - ; - - if (ch == '\n' && !was_cr) - IO::out8(0x3F8, '\r'); - - IO::out8(0x3F8, ch); - - if (ch == '\r') - was_cr = true; - else - was_cr = false; -} - -static void console_putch(char*&, char ch) -{ - if (serial_debug) - serial_putch(ch); - - // It would be bad to reach the assert in Console()::the() and do a stack overflow - - if (Console::is_initialized()) { - Console::the().put_char(ch); - } else { - IO::out8(0xe9, ch); - } -} - -int kprintf(const char* fmt, ...) -{ - color_on(); - va_list ap; - va_start(ap, fmt); - int ret = printf_internal(console_putch, nullptr, fmt, ap); - va_end(ap); - color_off(); - return ret; -} - -static void buffer_putch(char*& bufptr, char ch) -{ - *bufptr++ = ch; -} - -int sprintf(char* buffer, const char* fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - int ret = printf_internal(buffer_putch, buffer, fmt, ap); - buffer[ret] = '\0'; - va_end(ap); - return ret; -} - -static void debugger_out(char ch) -{ - if (serial_debug) - serial_putch(ch); - IO::out8(0xe9, ch); -} - -static void debugger_putch(char*&, char ch) -{ - debugger_out(ch); -} - -extern "C" int dbgputstr(const char* characters, int length) -{ - for (int i = 0; i < length; ++i) - debugger_out(characters[i]); - return 0; -} - -extern "C" int dbgprintf(const char* fmt, ...) -{ - color_on(); - va_list ap; - va_start(ap, fmt); - int ret = printf_internal(debugger_putch, nullptr, fmt, ap); - va_end(ap); - color_off(); - return ret; -} diff --git a/Kernel/kstdio.h b/Kernel/kstdio.h deleted file mode 100644 index 0c99196235..0000000000 --- a/Kernel/kstdio.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include - -extern "C" { -int dbgprintf(const char* fmt, ...); -int dbgputstr(const char*, int); -int kprintf(const char* fmt, ...); -int sprintf(char* buf, const char* fmt, ...); -void set_serial_debug(bool on_or_off); -int get_serial_debug(); -} - -#ifdef KERNEL -# define printf dbgprintf -#endif - -#ifndef __serenity__ -#define dbgprintf printf -#endif - -#ifdef __cplusplus - -template -inline int dbgputstr(const char (&array)[N]) -{ - return ::dbgputstr(array, N); -} - -#endif