diff --git a/libc/errno/errno.cpp b/libc/errno/errno.cpp index b0dda399..54780d57 100644 --- a/libc/errno/errno.cpp +++ b/libc/errno/errno.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014. This file is part of the Sortix C Library. @@ -25,20 +25,16 @@ #define __SORTIX_STDLIB_REDIRECTS 0 #include #include -#if !defined(__is_sortix_kernel) -#include -#endif + +#if __STDC_HOSTED__ + +extern "C" { int __thread errno = 0; } + +#else extern "C" { int global_errno = 0; } extern "C" { errno_location_func_t errno_location_func = NULL; } -#if !defined(__is_sortix_kernel) -extern "C" void init_error_functions() -{ - global_errno = 0; -} -#endif - extern "C" int* get_errno_location(void) { if ( errno_location_func ) { return errno_location_func(); } @@ -49,3 +45,5 @@ extern "C" void set_errno_location_func(errno_location_func_t func) { errno_location_func = func; } + +#endif diff --git a/libc/include/errno.h b/libc/include/errno.h index 3b602e41..9ad0f589 100644 --- a/libc/include/errno.h +++ b/libc/include/errno.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. This file is part of the Sortix C Library. @@ -107,6 +107,13 @@ __BEGIN_DECLS #define EOPNOTSUPP ENOTSUP +#if __STDC_HOSTED__ + +extern __thread int errno; +#define errno errno + +#else + /* Returns the address of the errno variable for this thread. */ int* get_errno_location(void); @@ -117,6 +124,8 @@ void set_errno_location_func(errno_location_func_t func); #define errno (*get_errno_location()) +#endif + extern char* program_invocation_name; extern char* program_invocation_short_name; diff --git a/libc/init/init.cpp b/libc/init/init.cpp index 0a2d78a0..685ff7e8 100644 --- a/libc/init/init.cpp +++ b/libc/init/init.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. This file is part of the Sortix C Library. @@ -30,7 +30,6 @@ extern "C" { char* program_invocation_name; } extern "C" { char* program_invocation_short_name; } -extern "C" void init_error_functions(); extern "C" void init_stdio(); extern "C" void init_signal(); @@ -54,9 +53,6 @@ extern "C" void initialize_standard_library(int argc, char* argv[]) program_invocation_name = (char*) argv0; program_invocation_short_name = find_last_elem((char*) argv0); - // Initialize stuff such as errno. - init_error_functions(); - // It's probably best to initialize the Unix signals early on. init_signal(); diff --git a/libc/x64/syscall.S b/libc/x64/syscall.S index 10622859..4b1bea90 100644 --- a/libc/x64/syscall.S +++ b/libc/x64/syscall.S @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2014. This file is part of the Sortix C Library. @@ -37,23 +37,10 @@ asm_syscall: /* syscall num in %rax. */ mov %rsp, %rbp int $0x80 test %ecx, %ecx - jnz 1f - pop %rbp - ret + jz 1f + mov %fs:0, %rsi + mov %ecx, errno@tpoff(%rsi) 1: - push %rbx - push %r12 - push %r13 - mov %ecx, %ebx # preserve: ret_errno - mov %rax, %r12 # preserve: ret_low - mov %rdx, %r13 # preserve: ret_high - call get_errno_location # get_errno_location() - mov %ebx, (%rax) # *get_errno_location() = ret_errno - mov %r12, %rax # restore: ret_low - mov %r13, %rdx # restore: ret_high - pop %r13 - pop %r12 - pop %rbx pop %rbp ret .size asm_syscall, .-asm_syscall diff --git a/libc/x86/syscall.S b/libc/x86/syscall.S index 046248e7..0d804e67 100644 --- a/libc/x86/syscall.S +++ b/libc/x86/syscall.S @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2014. This file is part of the Sortix C Library. @@ -49,13 +49,8 @@ asm_syscall: /* syscall num in %eax. */ int $0x80 test %ecx, %ecx # ret_errno & ret_errno jz 1f # if ( !(ret_errno & ret_errno) ) - mov %eax, %ebx # preserve: ret_low - mov %edx, %edi # preserve: ret_high - mov %ecx, %esi # preserve: ret_errno - call get_errno_location # get_errno_location() - mov %esi, (%eax) # *get_errno_location() = ret_errno - mov %ebx, %eax # restore: ret_low - mov %edi, %edx # restore: ret_high + mov %gs:0, %ebx + mov %ecx, errno@ntpoff(%ebx) 1: pop %esi pop %edi