Refactor kernel process.h and thread.h headers.
This commit is contained in:
parent
7aa061e50e
commit
1bc470624f
35 changed files with 380 additions and 388 deletions
|
@ -33,8 +33,7 @@
|
|||
#include <sortix/kernel/copy.h>
|
||||
#include <sortix/kernel/timer.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
|
||||
#include "process.h"
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
namespace Sortix {
|
||||
namespace Alarm {
|
||||
|
|
|
@ -30,13 +30,13 @@
|
|||
#include <sortix/kernel/descriptor.h>
|
||||
#include <sortix/kernel/interlock.h>
|
||||
#include <sortix/kernel/interrupt.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/stat.h>
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include "signal.h"
|
||||
#include "com.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include <sortix/kernel/descriptor.h>
|
||||
#include <sortix/kernel/fsfunc.h>
|
||||
#include <sortix/kernel/string.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/dirent.h>
|
||||
#include <sortix/fcntl.h>
|
||||
#include <sortix/seek.h>
|
||||
|
@ -38,7 +39,6 @@
|
|||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
|
|
|
@ -27,12 +27,12 @@
|
|||
#include <sortix/kernel/refcount.h>
|
||||
#include <sortix/kernel/descriptor.h>
|
||||
#include <sortix/kernel/dtable.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/fcntl.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
|
|
|
@ -24,13 +24,13 @@
|
|||
|
||||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/mman.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include "elf.h"
|
||||
#include <sortix/kernel/memorymanagement.h>
|
||||
#include <sortix/kernel/panic.h>
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
|
|
|
@ -49,8 +49,7 @@
|
|||
#include <sortix/kernel/vnode.h>
|
||||
#include <sortix/kernel/mtable.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
|
||||
#include "../process.h"
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
|
|
|
@ -27,8 +27,8 @@
|
|||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/kernel/kthread.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
#include "process.h"
|
||||
#include "identity.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
197
sortix/include/sortix/kernel/process.h
Normal file
197
sortix/include/sortix/kernel/process.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
sortix/kernel/process.h
|
||||
A named collection of threads.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef INCLUDE_SORTIX_KERNEL_PROCESS_H
|
||||
#define INCLUDE_SORTIX_KERNEL_PROCESS_H
|
||||
|
||||
#include <sortix/fork.h>
|
||||
|
||||
#include <sortix/kernel/clock.h>
|
||||
#include <sortix/kernel/kthread.h>
|
||||
#include <sortix/kernel/refcount.h>
|
||||
#include <sortix/kernel/time.h>
|
||||
#include <sortix/kernel/timer.h>
|
||||
#include <sortix/kernel/user-timer.h>
|
||||
#include <sortix/kernel/cpu.h>
|
||||
|
||||
#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<Descriptor> root;
|
||||
Ref<Descriptor> cwd;
|
||||
Ref<MountTable> mtable;
|
||||
Ref<DescriptorTable> dtable;
|
||||
|
||||
public:
|
||||
void BootstrapTables(Ref<DescriptorTable> dtable, Ref<MountTable> mtable);
|
||||
void BootstrapDirectories(Ref<Descriptor> root);
|
||||
Ref<MountTable> GetMTable();
|
||||
Ref<DescriptorTable> GetDTable();
|
||||
Ref<Descriptor> GetRoot();
|
||||
Ref<Descriptor> GetCWD();
|
||||
Ref<Descriptor> GetDescriptor(int fd);
|
||||
// TODO: This should be removed, don't call it.
|
||||
Ref<Descriptor> Open(ioctx_t* ctx, const char* path, int flags, mode_t mode = 0);
|
||||
void SetCWD(Ref<Descriptor> 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
|
145
sortix/include/sortix/kernel/thread.h
Normal file
145
sortix/include/sortix/kernel/thread.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
sortix/kernel/thread.h
|
||||
Describes a thread belonging to a process.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef INCLUDE_SORTIX_KERNEL_THREAD_H
|
||||
#define INCLUDE_SORTIX_KERNEL_THREAD_H
|
||||
|
||||
#include <sortix/signal.h>
|
||||
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
|
||||
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
|
|
@ -27,6 +27,7 @@
|
|||
#include <sortix/kernel/interrupt.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
@ -34,7 +35,6 @@
|
|||
|
||||
#include "x86-family/idt.h"
|
||||
#include "calltrace.h"
|
||||
#include "process.h"
|
||||
|
||||
#include "sound.h" // Hack for SIGSEGV
|
||||
|
||||
|
|
|
@ -31,6 +31,8 @@
|
|||
#include <sortix/kernel/string.h>
|
||||
#include <sortix/kernel/kthread.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/seek.h>
|
||||
#include <sortix/dirent.h>
|
||||
|
@ -42,8 +44,6 @@
|
|||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include "process.h"
|
||||
#include "io.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
|
@ -25,8 +25,7 @@
|
|||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/kernel/ioctx.h>
|
||||
#include <sortix/kernel/copy.h>
|
||||
|
||||
#include "process.h"
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
|
|
|
@ -49,6 +49,8 @@
|
|||
#include <sortix/kernel/string.h>
|
||||
#include <sortix/kernel/user-timer.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/fcntl.h>
|
||||
#include <sortix/stat.h>
|
||||
|
@ -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"
|
||||
|
|
|
@ -29,8 +29,7 @@
|
|||
#include <sortix/kernel/worker.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
|
|
|
@ -27,12 +27,12 @@
|
|||
#include <sortix/kernel/refcount.h>
|
||||
#include <sortix/kernel/textbuffer.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/vga.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "../thread.h"
|
||||
#include "vga.h"
|
||||
#include "lfbtextbuffer.h"
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include <sortix/kernel/keyboard.h>
|
||||
#include <sortix/kernel/poll.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
#include <sortix/fcntl.h>
|
||||
#include <sortix/termmode.h>
|
||||
|
@ -44,7 +45,6 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "utf8.h"
|
||||
#include "process.h"
|
||||
#include "logterminal.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
|
@ -46,8 +46,7 @@
|
|||
#include <sortix/kernel/mtable.h>
|
||||
#include <sortix/kernel/pipe.h>
|
||||
#include <sortix/kernel/poll.h>
|
||||
|
||||
#include "../process.h"
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
#include "fs.h"
|
||||
|
||||
|
|
|
@ -45,6 +45,8 @@
|
|||
#include <sortix/kernel/pipe.h>
|
||||
#include <sortix/kernel/poll.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/signal.h>
|
||||
#include <sortix/stat.h>
|
||||
|
@ -53,8 +55,6 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include "process.h"
|
||||
#include "pipe.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
|
@ -41,8 +41,8 @@
|
|||
#include <sortix/kernel/ioctx.h>
|
||||
#include <sortix/kernel/poll.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
#include "process.h"
|
||||
#include "poll.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
|
@ -36,6 +36,8 @@
|
|||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/sortedlist.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/clock.h>
|
||||
#include <sortix/signal.h>
|
||||
|
@ -50,8 +52,6 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include "process.h"
|
||||
#include "initrd.h"
|
||||
#include "elf.h"
|
||||
|
||||
|
|
196
sortix/process.h
196
sortix/process.h
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
process.h
|
||||
A named collection of threads.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef SORTIX_PROCESS_H
|
||||
#define SORTIX_PROCESS_H
|
||||
|
||||
#include <sortix/fork.h>
|
||||
|
||||
#include <sortix/kernel/clock.h>
|
||||
#include <sortix/kernel/kthread.h>
|
||||
#include <sortix/kernel/refcount.h>
|
||||
#include <sortix/kernel/time.h>
|
||||
#include <sortix/kernel/timer.h>
|
||||
#include <sortix/kernel/user-timer.h>
|
||||
#include <sortix/kernel/cpu.h>
|
||||
|
||||
#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<Descriptor> root;
|
||||
Ref<Descriptor> cwd;
|
||||
Ref<MountTable> mtable;
|
||||
Ref<DescriptorTable> dtable;
|
||||
|
||||
public:
|
||||
void BootstrapTables(Ref<DescriptorTable> dtable, Ref<MountTable> mtable);
|
||||
void BootstrapDirectories(Ref<Descriptor> root);
|
||||
Ref<MountTable> GetMTable();
|
||||
Ref<DescriptorTable> GetDTable();
|
||||
Ref<Descriptor> GetRoot();
|
||||
Ref<Descriptor> GetCWD();
|
||||
Ref<Descriptor> GetDescriptor(int fd);
|
||||
// TODO: This should be removed, don't call it.
|
||||
Ref<Descriptor> Open(ioctx_t* ctx, const char* path, int flags, mode_t mode = 0);
|
||||
void SetCWD(Ref<Descriptor> 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
|
|
@ -39,11 +39,11 @@
|
|||
#include <sortix/kernel/time.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include "x86-family/gdt.h"
|
||||
#include "x86-family/float.h"
|
||||
#include "thread.h"
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix {
|
||||
namespace Scheduler {
|
||||
|
|
|
@ -28,12 +28,11 @@
|
|||
#include <sortix/kernel/panic.h>
|
||||
#include <sortix/kernel/interrupt.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "thread.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
// A per-cpu value whether a signal is pending in the running task.
|
||||
|
|
|
@ -27,9 +27,8 @@
|
|||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
|
||||
#include "process.h"
|
||||
#include "thread.h"
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
namespace Sortix {
|
||||
namespace Syscall {
|
||||
|
|
|
@ -29,6 +29,8 @@
|
|||
#include <sortix/kernel/interrupt.h>
|
||||
#include <sortix/kernel/time.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <sortix/mman.h>
|
||||
#include <sortix/signal.h>
|
||||
|
@ -37,9 +39,6 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "process.h"
|
||||
#include "thread.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
Thread::Thread()
|
||||
|
|
144
sortix/thread.h
144
sortix/thread.h
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
thread.h
|
||||
Describes a thread belonging to a process.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef SORTIX_THREAD_H
|
||||
#define SORTIX_THREAD_H
|
||||
|
||||
#include <sortix/signal.h>
|
||||
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/signal.h>
|
||||
|
||||
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
|
|
@ -38,7 +38,6 @@
|
|||
#include <sortix/kernel/time.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
|
||||
#include "process.h"
|
||||
#include "sound.h"
|
||||
|
||||
#ifdef PLATFORM_SERIAL
|
||||
|
|
|
@ -38,8 +38,7 @@
|
|||
#include <sortix/kernel/time.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/user-timer.h>
|
||||
|
||||
#include "process.h"
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
// TODO: Memset all user timers in process constructor.
|
||||
|
||||
|
|
|
@ -31,13 +31,13 @@
|
|||
#include <sortix/kernel/interlock.h>
|
||||
#include <sortix/kernel/syscall.h>
|
||||
#include <sortix/kernel/scheduler.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "fs/util.h"
|
||||
#include "vga.h"
|
||||
#include "process.h"
|
||||
|
||||
#define TEST_VGAFONT 0
|
||||
|
||||
|
|
|
@ -28,10 +28,10 @@
|
|||
#include <sortix/kernel/inode.h>
|
||||
#include <sortix/kernel/vnode.h>
|
||||
#include <sortix/kernel/mtable.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/mount.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
||||
|
|
|
@ -23,9 +23,9 @@
|
|||
*******************************************************************************/
|
||||
|
||||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/fork.h>
|
||||
#include <string.h>
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
*******************************************************************************/
|
||||
|
||||
#include <sortix/kernel/platform.h>
|
||||
#include "thread.h"
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
|
|
|
@ -24,10 +24,10 @@
|
|||
|
||||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/kernel/interrupt.h>
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../thread.h"
|
||||
#include "float.h"
|
||||
|
||||
namespace Sortix {
|
||||
|
|
|
@ -23,9 +23,9 @@
|
|||
*******************************************************************************/
|
||||
|
||||
#include <sortix/kernel/platform.h>
|
||||
#include <sortix/kernel/process.h>
|
||||
#include <sortix/fork.h>
|
||||
#include <string.h>
|
||||
#include "process.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
*******************************************************************************/
|
||||
|
||||
#include <sortix/kernel/platform.h>
|
||||
#include "thread.h"
|
||||
#include <sortix/kernel/thread.h>
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue