From 9b986798850b9c9a0c41d67ff5d8ddde2a4cdfce Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 2 May 2016 18:19:11 +0200 Subject: [PATCH] Clean up errno. --- kernel/libk.cpp | 9 +++++++++ kernel/x64/interrupt.S | 4 ++-- kernel/x64/syscall.S | 4 ++-- kernel/x86/interrupt.S | 4 ++-- kernel/x86/syscall.S | 4 ++-- libc/errno/errno.c | 20 +------------------- libc/include/errno.h | 25 ++++++++----------------- 7 files changed, 26 insertions(+), 44 deletions(-) diff --git a/kernel/libk.cpp b/kernel/libk.cpp index 46fdb95a..0de6f683 100644 --- a/kernel/libk.cpp +++ b/kernel/libk.cpp @@ -169,4 +169,13 @@ void libk_munmap(void* ptr, size_t size) FreeKernelAddress(&addralloc); } +#undef errno +extern "C" { int errno; } + +extern "C" +int* libk_get_errno_location(void) +{ + return &errno; +} + } // namespace Sortix diff --git a/kernel/x64/interrupt.S b/kernel/x64/interrupt.S index 8bc6ee06..5d6d6ed4 100644 --- a/kernel/x64/interrupt.S +++ b/kernel/x64/interrupt.S @@ -372,7 +372,7 @@ interrupt_handler_prepare: pushq %rbp # Push the current kernel errno value. - movl global_errno, %ebp + movl errno, %ebp pushq %rbp # Push whether a signal is pending. @@ -396,7 +396,7 @@ load_interrupted_registers: # Restore the previous kernel errno. popq %rbp - movl %ebp, global_errno + movl %ebp, errno # Remove CR2 from the stack. addq $8, %rsp diff --git a/kernel/x64/syscall.S b/kernel/x64/syscall.S index 50983f6c..2ad76f54 100644 --- a/kernel/x64/syscall.S +++ b/kernel/x64/syscall.S @@ -24,7 +24,7 @@ .section .text .type syscall_handler, @function syscall_handler: - movl $0, global_errno # Reset errno + movl $0, errno pushq %rbp movq %rsp, %rbp @@ -45,7 +45,7 @@ syscall_handler: # Return to user-space, system call result in %rax:%rdx, errno in %ecx. popq %rbp - movl global_errno, %ecx + movl errno, %ecx # Zero registers to avoid information leaks. # rax is return value. diff --git a/kernel/x86/interrupt.S b/kernel/x86/interrupt.S index 53a3c5b1..ad35f5ae 100644 --- a/kernel/x86/interrupt.S +++ b/kernel/x86/interrupt.S @@ -369,7 +369,7 @@ fixup_relocate_stack_complete: pushl %ebp # Push the current kernel errno value. - movl global_errno, %ebp + movl errno, %ebp pushl %ebp # Push whether a signal is pending. @@ -394,7 +394,7 @@ load_interrupted_registers: # Restore the previous kernel errno. popl %ebp - movl %ebp, global_errno + movl %ebp, errno # Remove CR2 from the stack. addl $4, %esp diff --git a/kernel/x86/syscall.S b/kernel/x86/syscall.S index 1a3fd91c..6aae706a 100644 --- a/kernel/x86/syscall.S +++ b/kernel/x86/syscall.S @@ -25,7 +25,7 @@ .type syscall_handler, @function syscall_handler: /* -- stack is 12 bytes from being 16-byte aligned -- */ - movl $0, global_errno # Reset errno + movl $0, errno pushl %ebp /* -- stack is 8 bytes from being 16-byte aligned -- */ @@ -69,7 +69,7 @@ syscall_handler: # Return to user-space, system call result in %eax:%edx, errno in %ecx. popl %ebp - movl global_errno, %ecx + movl errno, %ecx # Zero registers to avoid information leaks. # eax is the return value. diff --git a/libc/errno/errno.c b/libc/errno/errno.c index 77c1500b..f9b49cd1 100644 --- a/libc/errno/errno.c +++ b/libc/errno/errno.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2011, 2012, 2014, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,27 +18,9 @@ */ #include -#include #ifndef __is_sortix_libk __thread int errno = 0; -#else - -int global_errno = 0; -errno_location_func_t errno_location_func = NULL; - -int* get_errno_location(void) -{ - if ( errno_location_func ) - return errno_location_func(); - return &global_errno; -} - -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 7894a0ac..63b06109 100644 --- a/libc/include/errno.h +++ b/libc/include/errno.h @@ -22,10 +22,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - #define ENOTBLK 12 #define ENODEV 13 #define EWOULDBLOCK 14 @@ -117,6 +113,10 @@ extern "C" { #define EOPNOTSUPP ENOTSUP +#ifdef __cplusplus +extern "C" { +#endif + #if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) extern __thread int errno; @@ -124,24 +124,15 @@ extern __thread int errno; #else -/* Returns the address of the errno variable for this thread. */ -int* get_errno_location(void); - -/* get_errno_location will forward the request to a user-specified function if - specified, or if NULL, will return the global thread-shared errno value. */ -typedef int* (*errno_location_func_t)(void); -void set_errno_location_func(errno_location_func_t func); - -#define errno (*get_errno_location()) +int* libk_get_errno_location(void); +#define errno (*libk_get_errno_location()) #endif +#if __USE_SORTIX extern char* program_invocation_name; extern char* program_invocation_short_name; - -/* Satisfy broken programs that expect these to be macros. */ -#define program_invocation_name program_invocation_name -#define program_invocation_short_name program_invocation_short_name +#endif #ifdef __cplusplus } /* extern "C" */