diff --git a/sortix/ata.cpp b/sortix/ata.cpp index 0d201002..9949679a 100644 --- a/sortix/ata.cpp +++ b/sortix/ata.cpp @@ -29,12 +29,12 @@ #include #include #include +#include #include #include #include #include #include "ata.h" -#include "cpu.h" // TODO: Use the PCI to detect ATA devices instead of relying on them being on // standard locations. diff --git a/sortix/bga.cpp b/sortix/bga.cpp index a6a62cb3..50907f51 100644 --- a/sortix/bga.cpp +++ b/sortix/bga.cpp @@ -36,12 +36,12 @@ #include #include #include +#include #include #include "x86-family/memorymanagement.h" #include "lfbtextbuffer.h" -#include "cpu.h" #include "bga.h" namespace Sortix { diff --git a/sortix/cpu.h b/sortix/cpu.h deleted file mode 100644 index 7c9274d1..00000000 --- a/sortix/cpu.h +++ /dev/null @@ -1,51 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. - - This file is part of Sortix. - - Sortix is free software: you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation, either version 3 of the License, or (at your option) any later - version. - - Sortix is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. - - You should have received a copy of the GNU General Public License along with - Sortix. If not, see . - - cpu.h - Includes CPU-specific headers. - -*******************************************************************************/ - -#ifndef SORTIX_CPU_H -#define SORTIX_CPU_H - - // Include some x86 headers. - #ifdef PLATFORM_X86 - #include "x86/x86.h" - #endif - - // Include some x64 headers. - #ifdef PLATFORM_X64 - #include "x64/x64.h" - #endif - -namespace Sortix { -namespace CPU { - -extern "C" void load_registers(InterruptRegisters* regs, size_t size) SORTIX_NORETURN; -inline void LoadRegisters(InterruptRegisters* regs) SORTIX_NORETURN; -inline void LoadRegisters(InterruptRegisters* regs) -{ - load_registers(regs, sizeof(*regs)); -} - -} // namespace CPU -} // namespace CPU - -#endif diff --git a/sortix/include/sortix/kernel/cpu.h b/sortix/include/sortix/kernel/cpu.h new file mode 100644 index 00000000..cd437639 --- /dev/null +++ b/sortix/include/sortix/kernel/cpu.h @@ -0,0 +1,131 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + sortix/kernel/cpu.h + CPU-specific declarations. + +*******************************************************************************/ + +#ifndef INCLUDE_SORTIX_KERNEL_CPU_H +#define INCLUDE_SORTIX_KERNEL_CPU_H + +#include + +namespace Sortix { + +// Functions for 32-bit and 64-bit x86. +#if defined(__i386__) || defined(__x86_64__) +namespace CPU { +void OutPortB(uint16_t Port, uint8_t Value); +void OutPortW(uint16_t Port, uint16_t Value); +void OutPortL(uint16_t Port, uint32_t Value); +uint8_t InPortB(uint16_t Port); +uint16_t InPortW(uint16_t Port); +uint32_t InPortL(uint16_t Port); +void Reboot(); +void ShutDown(); +} // namespace CPU +#endif + +// CPU flag register bits for 32-bit and 64-bit x86. +#if defined(__i386__) || defined(__x86_64__) +const size_t FLAGS_CARRY = 1<<0; // 0x000001 +const size_t FLAGS_RESERVED1 = 1<<1; // 0x000002, read as one +const size_t FLAGS_PARITY = 1<<2; // 0x000004 +const size_t FLAGS_RESERVED2 = 1<<3; // 0x000008 +const size_t FLAGS_AUX = 1<<4; // 0x000010 +const size_t FLAGS_RESERVED3 = 1<<5; // 0x000020 +const size_t FLAGS_ZERO = 1<<6; // 0x000040 +const size_t FLAGS_SIGN = 1<<7; // 0x000080 +const size_t FLAGS_TRAP = 1<<8; // 0x000100 +const size_t FLAGS_INTERRUPT = 1<<9; // 0x000200 +const size_t FLAGS_DIRECTION = 1<<10; // 0x000400 +const size_t FLAGS_OVERFLOW = 1<<11; // 0x000800 +const size_t FLAGS_IOPRIVLEVEL = 1<<12 | 1<<13; +const size_t FLAGS_NESTEDTASK = 1<<14; // 0x004000 +const size_t FLAGS_RESERVED4 = 1<<15; // 0x008000 +const size_t FLAGS_RESUME = 1<<16; // 0x010000 +const size_t FLAGS_VIRTUAL8086 = 1<<17; // 0x020000 +const size_t FLAGS_ALIGNCHECK = 1<<18; // 0x040000 +const size_t FLAGS_VIRTINTR = 1<<19; // 0x080000 +const size_t FLAGS_VIRTINTRPEND = 1<<20; // 0x100000 +const size_t FLAGS_ID = 1<<21; // 0x200000 +#endif + +// x86 interrupt registers structure. +#if defined(__i386__) +namespace X86 { +struct InterruptRegisters +{ + uint32_t signal_pending, kerrno, cr2; + uint32_t ds; // Data segment selector + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha. + uint32_t int_no, err_code; // Interrupt number and error code (if applicable) + uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically. + +public: + void LogRegisters() const; + bool InUserspace() const { return (cs & 0x3) != 0; } + +}; +} // namespace X86 +#endif + +// x86_64 interrupt +#if defined(__x86_64__) +namespace X64 { +struct InterruptRegisters +{ + uint64_t signal_pending, kerrno, cr2; + uint64_t ds; // Data segment selector + uint64_t rdi, rsi, rbp, rsp, rbx, rdx, rcx, rax; + uint64_t r8, r9, r10, r11, r12, r13, r14, r15; + uint64_t int_no, err_code; // Interrupt number and error code (if applicable) + uint64_t rip, cs, rflags, userrsp, ss; // Pushed by the processor automatically. + +public: + void LogRegisters() const; + bool InUserspace() const { return (cs & 0x3) != 0; } + +}; +} // namespace X64 +#endif + +// Segment values for 32-bit and 64-bit x86. +#if defined(__i386__) || defined(__x86_64__) +const uint64_t KCS = 0x08; +const uint64_t KDS = 0x10; +const uint64_t KRPL = 0x0; +const uint64_t UCS = 0x18; +const uint64_t UDS = 0x20; +const uint64_t URPL = 0x3; +#endif + +// Portable functions for loading registers. +namespace CPU { +extern "C" void load_registers(InterruptRegisters* regs, size_t size) SORTIX_NORETURN; +SORTIX_NORETURN inline void LoadRegisters(InterruptRegisters* regs) +{ + load_registers(regs, sizeof(*regs)); +} +} // namespace CPU + +} // namespace Sortix + +#endif diff --git a/sortix/kb/ps2.cpp b/sortix/kb/ps2.cpp index 59ee22ce..9b2b7068 100644 --- a/sortix/kb/ps2.cpp +++ b/sortix/kb/ps2.cpp @@ -25,13 +25,13 @@ #include #include #include +#include #include #include #include -#include "../cpu.h" #include "ps2.h" namespace Sortix diff --git a/sortix/pci.cpp b/sortix/pci.cpp index 9d284485..685bb55b 100644 --- a/sortix/pci.cpp +++ b/sortix/pci.cpp @@ -25,8 +25,8 @@ #include #include #include +#include #include -#include "cpu.h" // TODO: Put this in some // TODO: Verify that the endian conversions in this file actually works. I have // a sneaking suspicion that they won't work on non-little endian platforms. diff --git a/sortix/process.h b/sortix/process.h index 1a5b4c6f..9f5138d2 100644 --- a/sortix/process.h +++ b/sortix/process.h @@ -33,8 +33,7 @@ #include #include #include - -#include "cpu.h" +#include #define PROCESS_TIMER_NUM_MAX 32 diff --git a/sortix/signal.h b/sortix/signal.h index 63b36f71..c56f7c9a 100644 --- a/sortix/signal.h +++ b/sortix/signal.h @@ -27,7 +27,7 @@ #include -#include "cpu.h" +#include namespace Sortix { diff --git a/sortix/sound.cpp b/sortix/sound.cpp index a7cee97a..c20d22c1 100644 --- a/sortix/sound.cpp +++ b/sortix/sound.cpp @@ -25,8 +25,8 @@ #include #include #include +#include -#include "cpu.h" #include "sound.h" namespace Sortix diff --git a/sortix/uart.cpp b/sortix/uart.cpp index df302907..5b4851bc 100644 --- a/sortix/uart.cpp +++ b/sortix/uart.cpp @@ -23,7 +23,8 @@ *******************************************************************************/ #include -#include "cpu.h" +#include + #include #include "vga.h" #include "uart.h" diff --git a/sortix/x64/x64.cpp b/sortix/x64/x64.cpp index 664572ad..aadbcfc7 100644 --- a/sortix/x64/x64.cpp +++ b/sortix/x64/x64.cpp @@ -23,7 +23,7 @@ *******************************************************************************/ #include -#include "x64.h" +#include #include namespace Sortix diff --git a/sortix/x64/x64.h b/sortix/x64/x64.h deleted file mode 100644 index 1e456607..00000000 --- a/sortix/x64/x64.h +++ /dev/null @@ -1,58 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. - - This file is part of Sortix. - - Sortix is free software: you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation, either version 3 of the License, or (at your option) any later - version. - - Sortix is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. - - You should have received a copy of the GNU General Public License along with - Sortix. If not, see . - - x64/x64.h - CPU stuff for the x64 platform. - -*******************************************************************************/ - -#ifndef SORTIX_X64_H -#define SORTIX_X64_H - -#include "../x86-family/x86-family.h" - -namespace Sortix -{ - namespace X64 - { - struct InterruptRegisters - { - uint64_t signal_pending, kerrno, cr2; - uint64_t ds; // Data segment selector - uint64_t rdi, rsi, rbp, rsp, rbx, rdx, rcx, rax; - uint64_t r8, r9, r10, r11, r12, r13, r14, r15; - uint64_t int_no, err_code; // Interrupt number and error code (if applicable) - uint64_t rip, cs, rflags, userrsp, ss; // Pushed by the processor automatically. - - public: - void LogRegisters() const; - bool InUserspace() const { return (cs & 0x3) != 0; } - - }; - } - - const uint64_t KCS = 0x08; - const uint64_t KDS = 0x10; - const uint64_t KRPL = 0x0; - const uint64_t UCS = 0x18; - const uint64_t UDS = 0x20; - const uint64_t URPL = 0x3; -} - -#endif diff --git a/sortix/x86-family/time.cpp b/sortix/x86-family/time.cpp index 67e1d30d..258fdc0b 100644 --- a/sortix/x86-family/time.cpp +++ b/sortix/x86-family/time.cpp @@ -33,8 +33,7 @@ #include #include #include - -#include "../cpu.h" +#include namespace Sortix { namespace Time { diff --git a/sortix/x86/x86.cpp b/sortix/x86/x86.cpp index 83f1d8ec..4cb03e4c 100644 --- a/sortix/x86/x86.cpp +++ b/sortix/x86/x86.cpp @@ -23,7 +23,7 @@ *******************************************************************************/ #include -#include "x86.h" +#include #include namespace Sortix diff --git a/sortix/x86/x86.h b/sortix/x86/x86.h deleted file mode 100644 index c48ffa72..00000000 --- a/sortix/x86/x86.h +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. - - This file is part of Sortix. - - Sortix is free software: you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation, either version 3 of the License, or (at your option) any later - version. - - Sortix is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. - - You should have received a copy of the GNU General Public License along with - Sortix. If not, see . - - x86/x86.h - CPU stuff for the x86 platform. - -*******************************************************************************/ - -#ifndef SORTIX_X86_H -#define SORTIX_X86_H - -#include "../x86-family/x86-family.h" - -namespace Sortix -{ - namespace X86 - { - struct InterruptRegisters - { - uint32_t signal_pending, kerrno, cr2; - uint32_t ds; // Data segment selector - uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha. - uint32_t int_no, err_code; // Interrupt number and error code (if applicable) - uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically. - - public: - void LogRegisters() const; - bool InUserspace() const { return (cs & 0x3) != 0; } - - }; - } - - const uint64_t KCS = 0x08; - const uint64_t KDS = 0x10; - const uint64_t KRPL = 0x0; - const uint64_t UCS = 0x18; - const uint64_t UDS = 0x20; - const uint64_t URPL = 0x3; -} - -#endif