From 1bc470624f6f3343f02a6488bd6c75a3204dff22 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 13 May 2013 00:41:30 +0200 Subject: [PATCH] Refactor kernel process.h and thread.h headers. --- sortix/alarm.cpp | 3 +- sortix/com.cpp | 4 +- sortix/descriptor.cpp | 2 +- sortix/dtable.cpp | 2 +- sortix/elf.cpp | 2 +- sortix/fs/user.cpp | 3 +- sortix/identity.cpp | 2 +- sortix/include/sortix/kernel/process.h | 197 +++++++++++++++++++++++++ sortix/include/sortix/kernel/thread.h | 145 ++++++++++++++++++ sortix/interrupt.cpp | 2 +- sortix/io.cpp | 4 +- sortix/ioctx.cpp | 3 +- sortix/kernel.cpp | 4 +- sortix/kthread.cpp | 3 +- sortix/lfbtextbuffer.cpp | 2 +- sortix/logterminal.cpp | 2 +- sortix/net/fs.cpp | 3 +- sortix/pipe.cpp | 4 +- sortix/poll.cpp | 2 +- sortix/process.cpp | 4 +- sortix/process.h | 196 ------------------------ sortix/scheduler.cpp | 4 +- sortix/signal.cpp | 3 +- sortix/syscall.cpp | 5 +- sortix/thread.cpp | 5 +- sortix/thread.h | 144 ------------------ sortix/time.cpp | 1 - sortix/user-timer.cpp | 3 +- sortix/vga.cpp | 2 +- sortix/vnode.cpp | 2 +- sortix/x64/process.cpp | 2 +- sortix/x64/thread.cpp | 2 +- sortix/x86-family/float.cpp | 2 +- sortix/x86/process.cpp | 2 +- sortix/x86/thread.cpp | 2 +- 35 files changed, 380 insertions(+), 388 deletions(-) create mode 100644 sortix/include/sortix/kernel/process.h create mode 100644 sortix/include/sortix/kernel/thread.h delete mode 100644 sortix/process.h delete mode 100644 sortix/thread.h diff --git a/sortix/alarm.cpp b/sortix/alarm.cpp index 3d8b3135..ce09f3dd 100644 --- a/sortix/alarm.cpp +++ b/sortix/alarm.cpp @@ -33,8 +33,7 @@ #include #include #include - -#include "process.h" +#include namespace Sortix { namespace Alarm { diff --git a/sortix/com.cpp b/sortix/com.cpp index 0e02d90d..25ff963f 100644 --- a/sortix/com.cpp +++ b/sortix/com.cpp @@ -30,13 +30,13 @@ #include #include #include +#include +#include #include #include -#include "thread.h" -#include "signal.h" #include "com.h" namespace Sortix { diff --git a/sortix/descriptor.cpp b/sortix/descriptor.cpp index db916848..2c15fb35 100644 --- a/sortix/descriptor.cpp +++ b/sortix/descriptor.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,6 @@ #include #include #include -#include "process.h" namespace Sortix { diff --git a/sortix/dtable.cpp b/sortix/dtable.cpp index c5ce9ad4..5a1ba073 100644 --- a/sortix/dtable.cpp +++ b/sortix/dtable.cpp @@ -27,12 +27,12 @@ #include #include #include +#include #include #include #include #include #include -#include "process.h" namespace Sortix { diff --git a/sortix/elf.cpp b/sortix/elf.cpp index 6832d1a1..1cd36d54 100644 --- a/sortix/elf.cpp +++ b/sortix/elf.cpp @@ -24,13 +24,13 @@ #include #include +#include #include #include #include #include "elf.h" #include #include -#include "process.h" namespace Sortix { diff --git a/sortix/fs/user.cpp b/sortix/fs/user.cpp index b3048826..ba715014 100644 --- a/sortix/fs/user.cpp +++ b/sortix/fs/user.cpp @@ -49,8 +49,7 @@ #include #include #include - -#include "../process.h" +#include namespace Sortix { diff --git a/sortix/identity.cpp b/sortix/identity.cpp index 5ec11063..c795009f 100644 --- a/sortix/identity.cpp +++ b/sortix/identity.cpp @@ -27,8 +27,8 @@ #include #include #include +#include -#include "process.h" #include "identity.h" namespace Sortix { diff --git a/sortix/include/sortix/kernel/process.h b/sortix/include/sortix/kernel/process.h new file mode 100644 index 00000000..988602d1 --- /dev/null +++ b/sortix/include/sortix/kernel/process.h @@ -0,0 +1,197 @@ +/******************************************************************************* + + 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/process.h + A named collection of threads. + +*******************************************************************************/ + +#ifndef INCLUDE_SORTIX_KERNEL_PROCESS_H +#define INCLUDE_SORTIX_KERNEL_PROCESS_H + +#include + +#include +#include +#include +#include +#include +#include +#include + +#define PROCESS_TIMER_NUM_MAX 32 + +namespace Sortix { + +class Thread; +class Process; +class Descriptor; +class DescriptorTable; +class MountTable; +struct ProcessSegment; +struct ProcessTimer; +struct ioctx_struct; +typedef struct ioctx_struct ioctx_t; + +const int SEG_NONE = 0; +const int SEG_TEXT = 1; +const int SEG_DATA = 2; +const int SEG_STACK = 3; +const int SEG_OTHER = 4; + +struct ProcessSegment +{ +public: + ProcessSegment() : prev(NULL), next(NULL) { } + +public: + ProcessSegment* prev; + ProcessSegment* next; + addr_t position; + size_t size; + int type; + +public: + bool Intersects(ProcessSegment* segments); + ProcessSegment* Fork(); + +}; + +class Process +{ +friend void Process__OnLastThreadExit(void*); + +public: + Process(); + ~Process(); + +public: + static void Init(); + +private: + static pid_t AllocatePID(); + +public: + char* program_image_path; + addr_t addrspace; + pid_t pid; + +public: + kthread_mutex_t idlock; + uid_t uid, euid; + gid_t gid, egid; + +private: + kthread_mutex_t ptrlock; + Ref root; + Ref cwd; + Ref mtable; + Ref dtable; + +public: + void BootstrapTables(Ref dtable, Ref mtable); + void BootstrapDirectories(Ref root); + Ref GetMTable(); + Ref GetDTable(); + Ref GetRoot(); + Ref GetCWD(); + Ref GetDescriptor(int fd); + // TODO: This should be removed, don't call it. + Ref Open(ioctx_t* ctx, const char* path, int flags, mode_t mode = 0); + void SetCWD(Ref newcwd); + +private: +// A process may only access its parent if parentlock is locked. A process +// may only use its list of children if childlock is locked. A process may +// not access its sibling processes. + Process* parent; + Process* prevsibling; + Process* nextsibling; + Process* firstchild; + Process* zombiechild; + kthread_mutex_t childlock; + kthread_mutex_t parentlock; + kthread_cond_t zombiecond; + size_t zombiewaiting; + bool iszombie; + bool nozombify; + addr_t mmapfrom; + int exitstatus; + +public: + Thread* firstthread; + kthread_mutex_t threadlock; + +public: + ProcessSegment* segments; + +public: + kthread_mutex_t user_timers_lock; + UserTimer user_timers[PROCESS_TIMER_NUM_MAX]; + Timer alarm_timer; + +public: + int Execute(const char* programname, const uint8_t* program, + size_t programsize, int argc, const char* const* argv, + int envc, const char* const* envp, + CPU::InterruptRegisters* regs); + void ResetAddressSpace(); + void Exit(int status); + pid_t Wait(pid_t pid, int* status, int options); + bool DeliverSignal(int signum); + void OnThreadDestruction(Thread* thread); + int GetParentProcessId(); + void AddChildProcess(Process* child); + void ScheduleDeath(); + void AbortConstruction(); + +public: + Process* Fork(); + +private: + void ExecuteCPU(int argc, char** argv, int envc, char** envp, + addr_t stackpos, addr_t entry, + CPU::InterruptRegisters* regs); + void OnLastThreadExit(); + void LastPrayer(); + void NotifyChildExit(Process* child, bool zombify); + void NotifyNewZombies(); + void DeleteTimers(); + +public: + void ResetForExecute(); + addr_t AllocVirtualAddr(size_t size); + +public: + static Process* Get(pid_t pid); + static pid_t HackGetForegroundProcess(); + +private: + static bool Put(Process* process); + static void Remove(Process* process); + +}; + +void InitializeThreadRegisters(CPU::InterruptRegisters* regs, + const tforkregs_t* requested); +Process* CurrentProcess(); + +} // namespace Sortix + +#endif diff --git a/sortix/include/sortix/kernel/thread.h b/sortix/include/sortix/kernel/thread.h new file mode 100644 index 00000000..1e5c3d74 --- /dev/null +++ b/sortix/include/sortix/kernel/thread.h @@ -0,0 +1,145 @@ +/******************************************************************************* + + 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/thread.h + Describes a thread belonging to a process. + +*******************************************************************************/ + +#ifndef INCLUDE_SORTIX_KERNEL_THREAD_H +#define INCLUDE_SORTIX_KERNEL_THREAD_H + +#include + +#include +#include + +typedef struct multiboot_info multiboot_info_t; + +namespace Sortix { + +class Process; +class Thread; + +extern "C" void KernelInit(unsigned long magic, multiboot_info_t* bootinfo); + +typedef void (*ThreadEntry)(void* user); + +// Simply exits the kernel thread. +void KernelThreadExit() SORTIX_NORETURN; + +// Internally used as a kernel thread entry point that exits the thread +// upon the actual thread entry returning. +extern "C" void BootstrapKernelThread(void* user, ThreadEntry entry) SORTIX_NORETURN; + +// These functions create a new kernel process but doesn't start it. +Thread* CreateKernelThread(Process* process, CPU::InterruptRegisters* regs); +Thread* CreateKernelThread(Process* process, ThreadEntry entry, void* user, + size_t stacksize = 0); +Thread* CreateKernelThread(ThreadEntry entry, void* user, size_t stacksize = 0); + +// This function can be used to start a thread from the above functions. +void StartKernelThread(Thread* thread); + +// Alternatively, these functions both create and start the thread. +Thread* RunKernelThread(Process* process, CPU::InterruptRegisters* regs); +Thread* RunKernelThread(Process* process, ThreadEntry entry, void* user, + size_t stacksize = 0); +Thread* RunKernelThread(ThreadEntry entry, void* user, size_t stacksize = 0); + +void SetupKernelThreadRegs(CPU::InterruptRegisters* regs, ThreadEntry entry, + void* user, addr_t stack, size_t stacksize); + +extern "C" void Thread__OnSigKill(Thread* thread); + +typedef void (*sighandler_t)(int); + +class Thread +{ +friend Thread* CreateKernelThread(Process* process, + CPU::InterruptRegisters* regs); +friend void KernelInit(unsigned long magic, multiboot_info_t* bootinfo); +friend void Thread__OnSigKill(Thread* thread); + +public: + static void Init(); + +private: + Thread(); + +public: + ~Thread(); + +public: + size_t id; + Process* process; + bool terminated; + Thread* prevsibling; + Thread* nextsibling; + +// These are some things used internally by the scheduler and should not be +// touched by anything but it. Consider it private. +public: + Thread* schedulerlistprev; + Thread* schedulerlistnext; + volatile ThreadState state; + uint8_t fpuenv[512UL + 16UL]; + uint8_t* fpuenvaligned; + bool fpuinitialized; + +public: + addr_t addrspace; + addr_t stackpos; + size_t stacksize; + sighandler_t sighandler; + addr_t kernelstackpos; + size_t kernelstacksize; + bool kernelstackmalloced; + +private: + CPU::InterruptRegisters registers; + Signal::Queue signalqueue; + int currentsignal; + int siglevel; + int signums[SIG_NUM_LEVELS]; + CPU::InterruptRegisters sigregs[SIG_NUM_LEVELS]; + +public: + void SaveRegisters(const CPU::InterruptRegisters* src); + void LoadRegisters(CPU::InterruptRegisters* dest); + void HandleSignal(CPU::InterruptRegisters* regs); + void HandleSigreturn(CPU::InterruptRegisters* regs); + bool DeliverSignal(int signum); + addr_t SwitchAddressSpace(addr_t newaddrspace); + +private: + void GotoOnSigKill(CPU::InterruptRegisters* regs); + void OnSigKill() SORTIX_NORETURN; + void LastPrayer(); + void SetHavePendingSignals(); + void HandleSignalFixupRegsCPU(CPU::InterruptRegisters* regs); + void HandleSignalCPU(CPU::InterruptRegisters* regs); + +}; + +Thread* CurrentThread(); + +} // namespace Sortix + +#endif diff --git a/sortix/interrupt.cpp b/sortix/interrupt.cpp index d128d664..f4ebdb55 100644 --- a/sortix/interrupt.cpp +++ b/sortix/interrupt.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -34,7 +35,6 @@ #include "x86-family/idt.h" #include "calltrace.h" -#include "process.h" #include "sound.h" // Hack for SIGSEGV diff --git a/sortix/io.cpp b/sortix/io.cpp index ba54a028..25a4e972 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include @@ -42,8 +44,6 @@ #include #include -#include "thread.h" -#include "process.h" #include "io.h" namespace Sortix { diff --git a/sortix/ioctx.cpp b/sortix/ioctx.cpp index 3d1e5751..ccb5e866 100644 --- a/sortix/ioctx.cpp +++ b/sortix/ioctx.cpp @@ -25,8 +25,7 @@ #include #include #include - -#include "process.h" +#include namespace Sortix { diff --git a/sortix/kernel.cpp b/sortix/kernel.cpp index ef2ef80d..ea532db1 100644 --- a/sortix/kernel.cpp +++ b/sortix/kernel.cpp @@ -49,6 +49,8 @@ #include #include #include +#include +#include #include #include @@ -63,8 +65,6 @@ #include "x86-family/gdt.h" #include "x86-family/float.h" #include "multiboot.h" -#include "thread.h" -#include "process.h" #include "alarm.h" #include "ata.h" #include "com.h" diff --git a/sortix/kthread.cpp b/sortix/kthread.cpp index 1e2ee989..6c824593 100644 --- a/sortix/kthread.cpp +++ b/sortix/kthread.cpp @@ -29,8 +29,7 @@ #include #include #include - -#include "thread.h" +#include namespace Sortix { diff --git a/sortix/lfbtextbuffer.cpp b/sortix/lfbtextbuffer.cpp index a215d217..98eacb74 100644 --- a/sortix/lfbtextbuffer.cpp +++ b/sortix/lfbtextbuffer.cpp @@ -27,12 +27,12 @@ #include #include #include +#include #include #include -#include "../thread.h" #include "vga.h" #include "lfbtextbuffer.h" diff --git a/sortix/logterminal.cpp b/sortix/logterminal.cpp index 60b92444..6ff11baf 100644 --- a/sortix/logterminal.cpp +++ b/sortix/logterminal.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -44,7 +45,6 @@ #include #include "utf8.h" -#include "process.h" #include "logterminal.h" namespace Sortix { diff --git a/sortix/net/fs.cpp b/sortix/net/fs.cpp index b856a78e..212c8b07 100644 --- a/sortix/net/fs.cpp +++ b/sortix/net/fs.cpp @@ -46,8 +46,7 @@ #include #include #include - -#include "../process.h" +#include #include "fs.h" diff --git a/sortix/pipe.cpp b/sortix/pipe.cpp index 667fed64..c143104c 100644 --- a/sortix/pipe.cpp +++ b/sortix/pipe.cpp @@ -45,6 +45,8 @@ #include #include #include +#include +#include #include #include @@ -53,8 +55,6 @@ #include #include -#include "thread.h" -#include "process.h" #include "pipe.h" namespace Sortix { diff --git a/sortix/poll.cpp b/sortix/poll.cpp index 9cb4244d..08c3234b 100644 --- a/sortix/poll.cpp +++ b/sortix/poll.cpp @@ -41,8 +41,8 @@ #include #include #include +#include -#include "process.h" #include "poll.h" namespace Sortix { diff --git a/sortix/process.cpp b/sortix/process.cpp index 187895cf..cf79dfde 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include #include @@ -50,8 +52,6 @@ #include #include -#include "thread.h" -#include "process.h" #include "initrd.h" #include "elf.h" diff --git a/sortix/process.h b/sortix/process.h deleted file mode 100644 index 9f5138d2..00000000 --- a/sortix/process.h +++ /dev/null @@ -1,196 +0,0 @@ -/******************************************************************************* - - 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 . - - process.h - A named collection of threads. - -*******************************************************************************/ - -#ifndef SORTIX_PROCESS_H -#define SORTIX_PROCESS_H - -#include - -#include -#include -#include -#include -#include -#include -#include - -#define PROCESS_TIMER_NUM_MAX 32 - -namespace Sortix -{ - class Thread; - class Process; - class Descriptor; - class DescriptorTable; - class MountTable; - struct ProcessSegment; - struct ProcessTimer; - struct ioctx_struct; - typedef struct ioctx_struct ioctx_t; - - const int SEG_NONE = 0; - const int SEG_TEXT = 1; - const int SEG_DATA = 2; - const int SEG_STACK = 3; - const int SEG_OTHER = 4; - - struct ProcessSegment - { - public: - ProcessSegment() : prev(NULL), next(NULL) { } - - public: - ProcessSegment* prev; - ProcessSegment* next; - addr_t position; - size_t size; - int type; - - public: - bool Intersects(ProcessSegment* segments); - ProcessSegment* Fork(); - - }; - - class Process - { - friend void Process__OnLastThreadExit(void*); - - public: - Process(); - ~Process(); - - public: - static void Init(); - - private: - static pid_t AllocatePID(); - - public: - char* program_image_path; - addr_t addrspace; - pid_t pid; - - public: - kthread_mutex_t idlock; - uid_t uid, euid; - gid_t gid, egid; - - private: - kthread_mutex_t ptrlock; - Ref root; - Ref cwd; - Ref mtable; - Ref dtable; - - public: - void BootstrapTables(Ref dtable, Ref mtable); - void BootstrapDirectories(Ref root); - Ref GetMTable(); - Ref GetDTable(); - Ref GetRoot(); - Ref GetCWD(); - Ref GetDescriptor(int fd); - // TODO: This should be removed, don't call it. - Ref Open(ioctx_t* ctx, const char* path, int flags, mode_t mode = 0); - void SetCWD(Ref newcwd); - - private: - // A process may only access its parent if parentlock is locked. A process - // may only use its list of children if childlock is locked. A process may - // not access its sibling processes. - Process* parent; - Process* prevsibling; - Process* nextsibling; - Process* firstchild; - Process* zombiechild; - kthread_mutex_t childlock; - kthread_mutex_t parentlock; - kthread_cond_t zombiecond; - size_t zombiewaiting; - bool iszombie; - bool nozombify; - addr_t mmapfrom; - int exitstatus; - - public: - Thread* firstthread; - kthread_mutex_t threadlock; - - public: - ProcessSegment* segments; - - public: - kthread_mutex_t user_timers_lock; - UserTimer user_timers[PROCESS_TIMER_NUM_MAX]; - Timer alarm_timer; - - public: - int Execute(const char* programname, const uint8_t* program, - size_t programsize, int argc, const char* const* argv, - int envc, const char* const* envp, - CPU::InterruptRegisters* regs); - void ResetAddressSpace(); - void Exit(int status); - pid_t Wait(pid_t pid, int* status, int options); - bool DeliverSignal(int signum); - void OnThreadDestruction(Thread* thread); - int GetParentProcessId(); - void AddChildProcess(Process* child); - void ScheduleDeath(); - void AbortConstruction(); - - public: - Process* Fork(); - - private: - void ExecuteCPU(int argc, char** argv, int envc, char** envp, - addr_t stackpos, addr_t entry, - CPU::InterruptRegisters* regs); - void OnLastThreadExit(); - void LastPrayer(); - void NotifyChildExit(Process* child, bool zombify); - void NotifyNewZombies(); - void DeleteTimers(); - - public: - void ResetForExecute(); - addr_t AllocVirtualAddr(size_t size); - - public: - static Process* Get(pid_t pid); - static pid_t HackGetForegroundProcess(); - - private: - static bool Put(Process* process); - static void Remove(Process* process); - - }; - - void InitializeThreadRegisters(CPU::InterruptRegisters* regs, - const tforkregs_t* requested); - Process* CurrentProcess(); -} - -#endif diff --git a/sortix/scheduler.cpp b/sortix/scheduler.cpp index e507fc31..a60ca56f 100644 --- a/sortix/scheduler.cpp +++ b/sortix/scheduler.cpp @@ -39,11 +39,11 @@ #include #include #include +#include +#include #include "x86-family/gdt.h" #include "x86-family/float.h" -#include "thread.h" -#include "process.h" namespace Sortix { namespace Scheduler { diff --git a/sortix/signal.cpp b/sortix/signal.cpp index 3e917ee6..d9d28791 100644 --- a/sortix/signal.cpp +++ b/sortix/signal.cpp @@ -28,12 +28,11 @@ #include #include #include +#include #include #include -#include "thread.h" - namespace Sortix { // A per-cpu value whether a signal is pending in the running task. diff --git a/sortix/syscall.cpp b/sortix/syscall.cpp index c5fbf7b4..ae2c9508 100644 --- a/sortix/syscall.cpp +++ b/sortix/syscall.cpp @@ -27,9 +27,8 @@ #include #include #include - -#include "process.h" -#include "thread.h" +#include +#include namespace Sortix { namespace Syscall { diff --git a/sortix/thread.cpp b/sortix/thread.cpp index e4bd513e..58a25420 100644 --- a/sortix/thread.cpp +++ b/sortix/thread.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include @@ -37,9 +39,6 @@ #include #include -#include "process.h" -#include "thread.h" - namespace Sortix { Thread::Thread() diff --git a/sortix/thread.h b/sortix/thread.h deleted file mode 100644 index 4384e2a9..00000000 --- a/sortix/thread.h +++ /dev/null @@ -1,144 +0,0 @@ -/******************************************************************************* - - 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 . - - thread.h - Describes a thread belonging to a process. - -*******************************************************************************/ - -#ifndef SORTIX_THREAD_H -#define SORTIX_THREAD_H - -#include - -#include -#include - -typedef struct multiboot_info multiboot_info_t; - -namespace Sortix -{ - class Process; - class Thread; - - extern "C" void KernelInit(unsigned long magic, multiboot_info_t* bootinfo); - - typedef void (*ThreadEntry)(void* user); - - // Simply exits the kernel thread. - void KernelThreadExit() SORTIX_NORETURN; - - // Internally used as a kernel thread entry point that exits the thread - // upon the actual thread entry returning. - extern "C" void BootstrapKernelThread(void* user, ThreadEntry entry) SORTIX_NORETURN; - - // These functions create a new kernel process but doesn't start it. - Thread* CreateKernelThread(Process* process, CPU::InterruptRegisters* regs); - Thread* CreateKernelThread(Process* process, ThreadEntry entry, void* user, - size_t stacksize = 0); - Thread* CreateKernelThread(ThreadEntry entry, void* user, size_t stacksize = 0); - - // This function can be used to start a thread from the above functions. - void StartKernelThread(Thread* thread); - - // Alternatively, these functions both create and start the thread. - Thread* RunKernelThread(Process* process, CPU::InterruptRegisters* regs); - Thread* RunKernelThread(Process* process, ThreadEntry entry, void* user, - size_t stacksize = 0); - Thread* RunKernelThread(ThreadEntry entry, void* user, size_t stacksize = 0); - - void SetupKernelThreadRegs(CPU::InterruptRegisters* regs, ThreadEntry entry, - void* user, addr_t stack, size_t stacksize); - - extern "C" void Thread__OnSigKill(Thread* thread); - - typedef void (*sighandler_t)(int); - - class Thread - { - friend Thread* CreateKernelThread(Process* process, - CPU::InterruptRegisters* regs); - friend void KernelInit(unsigned long magic, multiboot_info_t* bootinfo); - friend void Thread__OnSigKill(Thread* thread); - - public: - static void Init(); - - private: - Thread(); - - public: - ~Thread(); - - public: - size_t id; - Process* process; - bool terminated; - Thread* prevsibling; - Thread* nextsibling; - - // These are some things used internally by the scheduler and should not be - // touched by anything but it. Consider it private. - public: - Thread* schedulerlistprev; - Thread* schedulerlistnext; - volatile ThreadState state; - uint8_t fpuenv[512UL + 16UL]; - uint8_t* fpuenvaligned; - bool fpuinitialized; - - public: - addr_t addrspace; - addr_t stackpos; - size_t stacksize; - sighandler_t sighandler; - addr_t kernelstackpos; - size_t kernelstacksize; - bool kernelstackmalloced; - - private: - CPU::InterruptRegisters registers; - Signal::Queue signalqueue; - int currentsignal; - int siglevel; - int signums[SIG_NUM_LEVELS]; - CPU::InterruptRegisters sigregs[SIG_NUM_LEVELS]; - - public: - void SaveRegisters(const CPU::InterruptRegisters* src); - void LoadRegisters(CPU::InterruptRegisters* dest); - void HandleSignal(CPU::InterruptRegisters* regs); - void HandleSigreturn(CPU::InterruptRegisters* regs); - bool DeliverSignal(int signum); - addr_t SwitchAddressSpace(addr_t newaddrspace); - - private: - void GotoOnSigKill(CPU::InterruptRegisters* regs); - void OnSigKill() SORTIX_NORETURN; - void LastPrayer(); - void SetHavePendingSignals(); - void HandleSignalFixupRegsCPU(CPU::InterruptRegisters* regs); - void HandleSignalCPU(CPU::InterruptRegisters* regs); - - }; - - Thread* CurrentThread(); -} - -#endif diff --git a/sortix/time.cpp b/sortix/time.cpp index 50bb267c..e0f578f1 100644 --- a/sortix/time.cpp +++ b/sortix/time.cpp @@ -38,7 +38,6 @@ #include #include -#include "process.h" #include "sound.h" #ifdef PLATFORM_SERIAL diff --git a/sortix/user-timer.cpp b/sortix/user-timer.cpp index fcaffc99..26396145 100644 --- a/sortix/user-timer.cpp +++ b/sortix/user-timer.cpp @@ -38,8 +38,7 @@ #include #include #include - -#include "process.h" +#include // TODO: Memset all user timers in process constructor. diff --git a/sortix/vga.cpp b/sortix/vga.cpp index ad7a0966..2379a612 100644 --- a/sortix/vga.cpp +++ b/sortix/vga.cpp @@ -31,13 +31,13 @@ #include #include #include +#include #include #include #include "fs/util.h" #include "vga.h" -#include "process.h" #define TEST_VGAFONT 0 diff --git a/sortix/vnode.cpp b/sortix/vnode.cpp index 22a14a16..fef23370 100644 --- a/sortix/vnode.cpp +++ b/sortix/vnode.cpp @@ -28,10 +28,10 @@ #include #include #include +#include #include #include #include -#include "process.h" namespace Sortix { diff --git a/sortix/x64/process.cpp b/sortix/x64/process.cpp index f8d604fd..f100e9d7 100644 --- a/sortix/x64/process.cpp +++ b/sortix/x64/process.cpp @@ -23,9 +23,9 @@ *******************************************************************************/ #include +#include #include #include -#include "process.h" namespace Sortix { diff --git a/sortix/x64/thread.cpp b/sortix/x64/thread.cpp index 43722c56..c33482ee 100644 --- a/sortix/x64/thread.cpp +++ b/sortix/x64/thread.cpp @@ -23,7 +23,7 @@ *******************************************************************************/ #include -#include "thread.h" +#include namespace Sortix { diff --git a/sortix/x86-family/float.cpp b/sortix/x86-family/float.cpp index d33757b7..5852e1b8 100644 --- a/sortix/x86-family/float.cpp +++ b/sortix/x86-family/float.cpp @@ -24,10 +24,10 @@ #include #include +#include #include -#include "../thread.h" #include "float.h" namespace Sortix { diff --git a/sortix/x86/process.cpp b/sortix/x86/process.cpp index 4bcf168f..842ea392 100644 --- a/sortix/x86/process.cpp +++ b/sortix/x86/process.cpp @@ -23,9 +23,9 @@ *******************************************************************************/ #include +#include #include #include -#include "process.h" namespace Sortix { diff --git a/sortix/x86/thread.cpp b/sortix/x86/thread.cpp index 791c27af..39ec31bd 100644 --- a/sortix/x86/thread.cpp +++ b/sortix/x86/thread.cpp @@ -23,7 +23,7 @@ *******************************************************************************/ #include -#include "thread.h" +#include namespace Sortix {