From 755e855c083683520ab5670e3099eac758e5d04c Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 5 Aug 2012 17:35:19 +0200 Subject: [PATCH] Renamed sforkr(2) to tfork(2). It's a much better name if you think of it as task-fork or thread-fork in the sense that it either modifies this task or creates a new one. This call will be used to provide user-space threads as well as fork(2). --- libmaxsi/include/unistd.h | 2 +- libmaxsi/process.cpp | 12 ++++++------ libmaxsi/x64/fork.s | 14 +++++++------- libmaxsi/x86/fork.s | 14 +++++++------- sortix/include/sortix/fork.h | 28 +++++++++++++++++----------- sortix/include/sortix/syscallnum.h | 2 +- sortix/include/sortix/x64/fork.h | 4 ++-- sortix/include/sortix/x86/fork.h | 2 +- sortix/process.cpp | 6 +++--- sortix/process.h | 2 +- sortix/x64/process.cpp | 2 +- sortix/x86/process.cpp | 2 +- 12 files changed, 48 insertions(+), 42 deletions(-) diff --git a/libmaxsi/include/unistd.h b/libmaxsi/include/unistd.h index 89b841a4..656ad1f9 100644 --- a/libmaxsi/include/unistd.h +++ b/libmaxsi/include/unistd.h @@ -194,7 +194,7 @@ size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off) size_t readall(int fd, void* buf, size_t count); size_t readleast(int fd, void* buf, size_t least, size_t max); pid_t sfork(int flags); -pid_t sforkr(int flags, sforkregs_t* regs); +pid_t tfork(int flags, tforkregs_t* regs); int uptime(uintmax_t* usecssinceboot); int vexecl(const char*, va_list args); int vexecle(const char*, va_list args); diff --git a/libmaxsi/process.cpp b/libmaxsi/process.cpp index 0c1a7c00..f81cd750 100644 --- a/libmaxsi/process.cpp +++ b/libmaxsi/process.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of LibMaxsi. @@ -39,7 +39,7 @@ namespace Maxsi { DEFN_SYSCALL1_VOID(SysExit, SYSCALL_EXIT, int); DEFN_SYSCALL3(int, SysExecVE, SYSCALL_EXEC, const char*, char* const*, char* const*); - DEFN_SYSCALL2(pid_t, SysSForkR, SYSCALL_SFORKR, int, sforkregs_t*); + DEFN_SYSCALL2(pid_t, SysTFork, SYSCALL_TFORK, int, tforkregs_t*); DEFN_SYSCALL0(pid_t, SysGetPID, SYSCALL_GETPID); DEFN_SYSCALL0(pid_t, SysGetParentPID, SYSCALL_GETPPID); DEFN_SYSCALL3(pid_t, SysWait, SYSCALL_WAIT, pid_t, int*, int); @@ -184,16 +184,16 @@ namespace Maxsi _exit(status); } - extern "C" pid_t sforkr(int flags, sforkregs_t* regs) + extern "C" pid_t tfork(int flags, tforkregs_t* regs) { - return SysSForkR(flags, regs); + return SysTFork(flags, regs); } - extern "C" pid_t __call_sforkr_with_regs(int flags); + extern "C" pid_t __call_tfork_with_regs(int flags); extern "C" pid_t sfork(int flags) { - return __call_sforkr_with_regs(flags); + return __call_tfork_with_regs(flags); } DUAL_FUNCTION(pid_t, fork, Fork, ()) diff --git a/libmaxsi/x64/fork.s b/libmaxsi/x64/fork.s index 96e1ea21..73e8f4a4 100644 --- a/libmaxsi/x64/fork.s +++ b/libmaxsi/x64/fork.s @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012. This file is part of LibMaxsi. @@ -24,13 +24,13 @@ .section .text -.globl __call_sforkr_with_regs -.type __call_sforkr_with_regs, @function -__call_sforkr_with_regs: +.globl __call_tfork_with_regs +.type __call_tfork_with_regs, @function +__call_tfork_with_regs: pushq %rbp movq %rsp, %rbp - # The actual system call expects a struct sforkregs_x64 containing the state + # The actual system call expects a struct tforkregs_x64 containing the state # of each register in the child. Since we create an identical copy, we # simply set each member of the structure to our own state. Note that since # the stack goes downwards, we create it in the reverse order. @@ -53,10 +53,10 @@ __call_sforkr_with_regs: pushq $0 # rax, result of sfork is 0 for the child. pushq $after_fork # rip, child will start execution from here. - # Call sforkr with a nice pointer to our structure. Note that %rdi contains + # Call tfork with a nice pointer to our structure. Note that %rdi contains # the flag parameter that this function accepted. movq %rsp, %rsi - call sforkr + call tfork after_fork: # The value in %rax determines whether we are child or parent. There is no diff --git a/libmaxsi/x86/fork.s b/libmaxsi/x86/fork.s index e40b0851..7cd69550 100644 --- a/libmaxsi/x86/fork.s +++ b/libmaxsi/x86/fork.s @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012. This file is part of LibMaxsi. @@ -24,15 +24,15 @@ .section .text -.globl __call_sforkr_with_regs -.type __call_sforkr_with_regs, @function -__call_sforkr_with_regs: +.globl __call_tfork_with_regs +.type __call_tfork_with_regs, @function +__call_tfork_with_regs: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx # flags parameter, edx need not be preserved. - # The actual system call expects a struct sforkregs_x86 containing the state + # The actual system call expects a struct tforkregs_x86 containing the state # of each register in the child. Since we create an identical copy, we # simply set each member of the structure to our own state. Note that since # the stack goes downwards, we create it in the reverse order. @@ -47,11 +47,11 @@ __call_sforkr_with_regs: pushl $0 # rax, result of sfork is 0 for the child. pushl $after_fork # rip, child will start execution from here. - # Call sforkr with a nice pointer to our structure. Note that %edi contains + # Call tfork with a nice pointer to our structure. Note that %edi contains # the flag parameter that this function accepted. pushl %esp pushl %edx - call sforkr + call tfork after_fork: # The value in %eax determines whether we are child or parent. There is no diff --git a/sortix/include/sortix/fork.h b/sortix/include/sortix/fork.h index bd26217b..29d83b40 100644 --- a/sortix/include/sortix/fork.h +++ b/sortix/include/sortix/fork.h @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012. This file is part of Sortix. @@ -31,6 +31,7 @@ __BEGIN_DECLS +/* TODO: These doesn't belong in this header! */ #define R_OK 4 /* Test for read permission. */ #define W_OK 2 /* Test for write permission. */ #define X_OK 1 /* Test for execute permission. */ @@ -43,9 +44,9 @@ __BEGIN_DECLS you wish to fork certain items simply set the proper flags. Note that since flags may be added from time to time, you should use various compound flags defined below such as SFFORK and SFALL. It can be useful do combine these - compount flags with bitoperations, for instance "I want traditional fork, + compound flags with bit operations, for instance "I want traditional fork, except share the working dir pointer" is sfork(SFFORK & ~SFCWD). */ -#define SFPROC (1<<0) /* Creates child, otherwise affect current process. */ +#define SFPROC (1<<0) /* Creates child, otherwise affect current task. */ #define SFPID (1<<1) /* Allocates new PID. */ #define SFFD (1<<2) /* Fork file descriptor table. */ #define SFMEM (1<<3) /* Forks address space. */ @@ -56,10 +57,11 @@ __BEGIN_DECLS #define SFCSIG (1<<8) /* Child will have no pending signals, like fork(2). */ /* Creates a new thread in this process. Beware that it will share the stack of - the parent thread and that various threading featues may not have been set up - properly. You should use the standard threading API unless you know what you - are doing; remember that you can always sfork more stuff after the standard - threading API returns control to you. */ + the parent thread and that various threading features may not have been set + up properly. You should use the standard threading API unless you know what + you are doing; remember that you can always sfork more stuff after the + standard threading API returns control to you. This is useful combined with + the tfork System call that lets you control the registers of the new task. */ #define SFTHREAD (SFPROC | SFCSIG) /* Provides traditional fork(2) behavior; use this instead of the above values @@ -75,15 +77,19 @@ __BEGIN_DECLS are reserved and must not be set. */ #define SFALL ((1<<20)-1) +/* This structure tells tfork the initial values of the registers in the new + task. It is ignored if no new task is created. sfork works by recording its + own state into such a structure and calling tfork. Note that this structure + is highly platform specific, portable code should use the standard threading + facilities combined with sfork if possible. */ #ifdef PLATFORM_X86 -typedef struct sforkregs_x86 sforkregs_t; +typedef struct tforkregs_x86 tforkregs_t; #elif defined(PLATFORM_X64) -typedef struct sforkregs_x64 sforkregs_t; +typedef struct tforkregs_x64 tforkregs_t; #else -#warning No sforkresgs_cpu structure declared +#warning No tforkregs_cpu structure declared #endif __END_DECLS #endif - diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h index 9ae864de..5ccd54fb 100644 --- a/sortix/include/sortix/syscallnum.h +++ b/sortix/include/sortix/syscallnum.h @@ -72,7 +72,7 @@ #define SYSCALL_KERNELINFO 48 #define SYSCALL_PREAD 49 #define SYSCALL_PWRITE 50 -#define SYSCALL_SFORKR 51 +#define SYSCALL_TFORK 51 #define SYSCALL_TCGETWINSIZE 52 #define SYSCALL_RAISE 53 #define SYSCALL_MAX_NUM 54 /* index of highest constant + 1 */ diff --git a/sortix/include/sortix/x64/fork.h b/sortix/include/sortix/x64/fork.h index 0fc267cd..44d63b52 100644 --- a/sortix/include/sortix/x64/fork.h +++ b/sortix/include/sortix/x64/fork.h @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012. This file is part of Sortix. @@ -29,7 +29,7 @@ __BEGIN_DECLS -struct sforkregs_x64 +struct tforkregs_x64 { uint64_t rip; uint64_t rax; diff --git a/sortix/include/sortix/x86/fork.h b/sortix/include/sortix/x86/fork.h index 4e60faf3..259f1a8c 100644 --- a/sortix/include/sortix/x86/fork.h +++ b/sortix/include/sortix/x86/fork.h @@ -29,7 +29,7 @@ __BEGIN_DECLS -struct sforkregs_x86 +struct tforkregs_x86 { uint32_t eip; uint32_t eax; diff --git a/sortix/process.cpp b/sortix/process.cpp index c00905a7..761f1238 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -661,11 +661,11 @@ namespace Sortix return result; } - pid_t SysSForkR(int flags, sforkregs_t* regs) + pid_t SysTFork(int flags, tforkregs_t* regs) { if ( Signal::IsPending() ) { Error::Set(EINTR); return -1; } - // TODO: Properly support sforkr(2). + // TODO: Properly support tfork(2). if ( flags != SFFORK ) { Error::Set(ENOSYS); return -1; } CPU::InterruptRegisters cpuregs; @@ -832,7 +832,7 @@ namespace Sortix void Process::Init() { Syscall::Register(SYSCALL_EXEC, (void*) SysExecVE); - Syscall::Register(SYSCALL_SFORKR, (void*) SysSForkR); + Syscall::Register(SYSCALL_TFORK, (void*) SysTFork); Syscall::Register(SYSCALL_GETPID, (void*) SysGetPID); Syscall::Register(SYSCALL_GETPPID, (void*) SysGetParentPID); Syscall::Register(SYSCALL_EXIT, (void*) SysExit); diff --git a/sortix/process.h b/sortix/process.h index 5e059d13..b0d5658d 100644 --- a/sortix/process.h +++ b/sortix/process.h @@ -147,7 +147,7 @@ namespace Sortix }; void InitializeThreadRegisters(CPU::InterruptRegisters* regs, - const sforkregs_t* requested); + const tforkregs_t* requested); Process* CurrentProcess(); } diff --git a/sortix/x64/process.cpp b/sortix/x64/process.cpp index dd74a3cb..33dbe597 100644 --- a/sortix/x64/process.cpp +++ b/sortix/x64/process.cpp @@ -51,7 +51,7 @@ namespace Sortix } void InitializeThreadRegisters(CPU::InterruptRegisters* regs, - const sforkregs_t* requested) + const tforkregs_t* requested) { Maxsi::Memory::Set(regs, 0, sizeof(*regs)); regs->rip = requested->rip; diff --git a/sortix/x86/process.cpp b/sortix/x86/process.cpp index a9e39d0f..a78fad7e 100644 --- a/sortix/x86/process.cpp +++ b/sortix/x86/process.cpp @@ -47,7 +47,7 @@ namespace Sortix } void InitializeThreadRegisters(CPU::InterruptRegisters* regs, - const sforkregs_t* requested) + const tforkregs_t* requested) { Maxsi::Memory::Set(regs, 0, sizeof(*regs)); regs->eip = requested->eip;