From 6c347146ee2682e33b9e201a3966a20a1355360c Mon Sep 17 00:00:00 2001 From: turbocat Date: Thu, 1 Sep 2022 19:19:27 +0000 Subject: [PATCH] newlib: Try to unify wrappers. And get rid of kos32sys.h git-svn-id: svn://kolibrios.org@9874 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../sdk/sources/newlib/libc/crt/gthr-kos32.c | 98 +++++++++---------- .../sources/newlib/libc/include/sys/ksys.h | 10 +- .../sdk/sources/newlib/libc/pe/crtloader.c | 10 +- contrib/sdk/sources/newlib/libc/pe/loader.c | 15 +-- .../sdk/sources/newlib/libc/reent/getreent.c | 4 +- .../sdk/sources/newlib/libc/stdlib/mallocr.c | 31 +----- .../sources/newlib/libc/sys/clock_gettime.c | 4 +- 7 files changed, 78 insertions(+), 94 deletions(-) diff --git a/contrib/sdk/sources/newlib/libc/crt/gthr-kos32.c b/contrib/sdk/sources/newlib/libc/crt/gthr-kos32.c index ddbed39534..9f7f833581 100644 --- a/contrib/sdk/sources/newlib/libc/crt/gthr-kos32.c +++ b/contrib/sdk/sources/newlib/libc/crt/gthr-kos32.c @@ -21,7 +21,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see . */ #include -#include +#include +#include #include "gthr-kos32.h" #define FUTEX_INIT 0 @@ -35,6 +36,42 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #define exchange_release(ptr, new) \ __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE) +#define TLS_KEY_PID 0 +#define TLS_KEY_TID 4 +#define TLS_KEY_LOW_STACK 8 +#define TLS_KEY_HIGH_STACK 12 +#define TLS_KEY_LIBC 16 + +unsigned int tls_alloc(void); +int tls_free(unsigned int key); + +static inline int tls_set(unsigned int key, void *val) +{ + int ret = -1; + if(key < 4096) + { + __asm__ __volatile__( + "movl %0, %%fs:(%1)" + ::"r"(val),"r"(key)); + ret = 0; + } + return ret; +}; + +static inline void *tls_get(unsigned int key) +{ + void *val = (void*)-1; + if(key < 4096) + { + __asm__ __volatile__( + "movl %%fs:(%1), %0" + :"=r"(val) + :"r"(key)); + }; + return val; +} + + int __gthr_kos32_once (__gthread_once_t *once, void (*func) (void)) { if (once == NULL || func == NULL) @@ -50,7 +87,7 @@ int __gthr_kos32_once (__gthread_once_t *once, void (*func) (void)) else { while (! once->done) - yield(); + _ksys_thread_yield(); } } return 0; @@ -92,25 +129,14 @@ int __gthr_kos32_setspecific (__gthread_key_t key, const void *ptr) void __gthr_kos32_mutex_init_function (__gthread_mutex_t *mutex) { - int handle; - mutex->lock = 0; - - __asm__ volatile( - "int $0x40\t" - :"=a"(handle) - :"a"(77),"b"(FUTEX_INIT),"c"(mutex)); - mutex->handle = handle; + mutex->handle = _ksys_futex_create(mutex); } void __gthr_kos32_mutex_destroy (__gthread_mutex_t *mutex) { int retval; - - __asm__ volatile( - "int $0x40\t" - :"=a"(retval) - :"a"(77),"b"(FUTEX_DESTROY),"c"(mutex->handle)); + _ksys_futex_destroy(mutex->handle); } int __gthr_kos32_mutex_lock (__gthread_mutex_t *mutex) @@ -122,13 +148,9 @@ int __gthr_kos32_mutex_lock (__gthread_mutex_t *mutex) while (exchange_acquire (&mutex->lock, 2) != 0) { - __asm__ volatile( - "int $0x40\t\n" - :"=a"(tmp) - :"a"(77),"b"(FUTEX_WAIT), - "c"(mutex->handle),"d"(2),"S"(0)); - } - return 0; + _ksys_futex_wait(mutex->handle, 2 , 0); + } + return 0; } int __gthr_kos32_mutex_trylock (__gthread_mutex_t *mutex) @@ -146,11 +168,7 @@ int __gthr_kos32_mutex_unlock (__gthread_mutex_t *mutex) if (prev != 1) { - __asm__ volatile( - "int $0x40\t" - :"=a"(prev) - :"a"(77),"b"(FUTEX_WAKE), - "c"(mutex->handle),"d"(1)); + _ksys_futex_wake(mutex->handle, 1); }; return 0; } @@ -158,14 +176,9 @@ int __gthr_kos32_mutex_unlock (__gthread_mutex_t *mutex) void __gthr_kos32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex) { int handle; - mutex->lock = 0; - __asm__ volatile( - "int $0x40\t" - :"=a"(handle) - :"a"(77),"b"(FUTEX_INIT),"c"(mutex)); - mutex->handle = handle; + mutex->handle = _ksys_futex_create(mutex); mutex->depth = 0; mutex->owner = 0; @@ -190,11 +203,7 @@ int __gthr_kos32_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex) } else while (exchange_acquire (&mutex->lock, 2) != 0) { - __asm__ volatile( - "int $0x40\t\n" - :"=a"(tmp) - :"a"(77),"b"(FUTEX_WAIT), - "c"(mutex->handle),"d"(2),"S"(0)); + _ksys_futex_wait(mutex->handle, 2, 0); mutex->depth = 1; mutex->owner = me; }; @@ -232,11 +241,7 @@ int __gthr_kos32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex) if (prev != 1) { - __asm__ volatile( - "int $0x40\t" - :"=a"(prev) - :"a"(77),"b"(FUTEX_WAKE), - "c"(mutex->handle),"d"(1)); + _ksys_futex_wake(mutex->handle, 1); }; mutex->owner = 0; }; @@ -247,12 +252,7 @@ int __gthr_kos32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex) int __gthr_kos32_recursive_mutex_destroy (__gthread_recursive_mutex_t *mutex) { int retval; - - __asm__ volatile( - "int $0x40\t" - :"=a"(retval) - :"a"(77),"b"(FUTEX_DESTROY),"c"(mutex->handle)); - + _ksys_futex_destroy(mutex->handle); return 0; } diff --git a/contrib/sdk/sources/newlib/libc/include/sys/ksys.h b/contrib/sdk/sources/newlib/libc/include/sys/ksys.h index 8fa44cfbfd..b4ca6914ab 100644 --- a/contrib/sdk/sources/newlib/libc/include/sys/ksys.h +++ b/contrib/sdk/sources/newlib/libc/include/sys/ksys.h @@ -22,6 +22,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define KOSAPI static inline #define asm_inline __asm__ __volatile__ @@ -1617,7 +1621,7 @@ KOSAPI void _ksys_set_window_title(const char* title) /*============= Function 77, subfunction 0 - create futex object =============*/ -KOSAPI uint32_t _ksys_futex_create(int* futex_ctrl) +KOSAPI uint32_t _ksys_futex_create(void* futex_ctrl) { uint32_t futex_desc; asm_inline( @@ -1722,4 +1726,8 @@ static inline int _ksys_file_write_file(const char* name, unsigned long long off return res.status; } +#ifdef __cplusplus +} +#endif + #endif // _KSYS_H_ diff --git a/contrib/sdk/sources/newlib/libc/pe/crtloader.c b/contrib/sdk/sources/newlib/libc/pe/crtloader.c index 4063598039..64e687d209 100644 --- a/contrib/sdk/sources/newlib/libc/pe/crtloader.c +++ b/contrib/sdk/sources/newlib/libc/pe/crtloader.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include "list.h" #include "pe.h" @@ -97,7 +97,7 @@ void* create_image(void *raw) dos = (PIMAGE_DOS_HEADER)raw; nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); - img_base = user_alloc(nt->OptionalHeader.SizeOfImage); + img_base = _ksys_alloc(nt->OptionalHeader.SizeOfImage); if(unlikely(img_base == NULL)) return 0; @@ -196,9 +196,9 @@ void* load_libc() void *raw_img; size_t raw_size; void *img_base = NULL; - ufile_t uf; + ksys_ufile_t uf; - uf = load_file("/kolibrios/lib/libc.dll"); + uf = _ksys_load_file("/kolibrios/lib/libc.dll"); raw_img = uf.data; raw_size = uf.size; @@ -214,7 +214,7 @@ void* load_libc() img_base = create_image(raw_img); }; - user_free(raw_img); + _ksys_free(raw_img); return img_base; diff --git a/contrib/sdk/sources/newlib/libc/pe/loader.c b/contrib/sdk/sources/newlib/libc/pe/loader.c index 7cbcf7d159..9c691eab58 100644 --- a/contrib/sdk/sources/newlib/libc/pe/loader.c +++ b/contrib/sdk/sources/newlib/libc/pe/loader.c @@ -7,7 +7,7 @@ #include #include -#include +#include #include "list.h" #include "pe.h" @@ -22,6 +22,7 @@ void init_loader(void *libc_image); void* create_image(void *raw); int link_image(void *img_base, PIMAGE_IMPORT_DESCRIPTOR imp); +void* load_library(const char *name); extern char* __appenv; extern int __appenv_size; @@ -266,7 +267,7 @@ void* create_image(void *raw) dos = (PIMAGE_DOS_HEADER)raw; nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); - img_base = user_alloc(nt->OptionalHeader.SizeOfImage); + img_base = _ksys_alloc(nt->OptionalHeader.SizeOfImage); if(unlikely(img_base == NULL)) return 0; @@ -651,12 +652,12 @@ static void *load_lib_internal(const char *path) PIMAGE_NT_HEADERS32 nt; PIMAGE_EXPORT_DIRECTORY exp; - ufile_t uf; + ksys_ufile_t uf; void *raw_img; size_t raw_size; void *img_base = NULL; - uf = load_file(path); + uf = _ksys_load_file(path); raw_img = uf.data; raw_size = uf.size; @@ -666,12 +667,12 @@ static void *load_lib_internal(const char *path) if( validate_pe(raw_img, raw_size, 0) == 0) { printf("invalide module %s\n", path); - user_free(raw_img); + _ksys_free(raw_img); return NULL; }; img_base = create_image(raw_img); - user_free(raw_img); + _ksys_free(raw_img); if( unlikely(img_base == NULL) ) printf("cannot create image %s\n",path); @@ -797,7 +798,7 @@ err2: free(module->img_path); free(module); err1: - user_free(img_base); + _ksys_free(img_base); return NULL; }; diff --git a/contrib/sdk/sources/newlib/libc/reent/getreent.c b/contrib/sdk/sources/newlib/libc/reent/getreent.c index 0987729471..2cbdbedcc4 100644 --- a/contrib/sdk/sources/newlib/libc/reent/getreent.c +++ b/contrib/sdk/sources/newlib/libc/reent/getreent.c @@ -3,7 +3,7 @@ #include <_ansi.h> #include #include -#include +#include extern _VOID _EXFUN(__sinit,(struct _reent *)); @@ -11,7 +11,7 @@ void init_reent() { struct _reent *ent; - ent = user_alloc(sizeof(struct _reent)); + ent = _ksys_alloc(sizeof(struct _reent)); _REENT_INIT_PTR_ZEROED(ent); diff --git a/contrib/sdk/sources/newlib/libc/stdlib/mallocr.c b/contrib/sdk/sources/newlib/libc/stdlib/mallocr.c index dd8746540e..55d7b92c9f 100644 --- a/contrib/sdk/sources/newlib/libc/stdlib/mallocr.c +++ b/contrib/sdk/sources/newlib/libc/stdlib/mallocr.c @@ -36,7 +36,7 @@ #include #include #include - +#include struct malloc_chunk { @@ -60,33 +60,8 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* The maximum possible size_t value has all bits set */ #define MAX_SIZE_T (~(size_t)0) -void *user_alloc(size_t size) -{ - void *val; - -// __asm__("int3"); - - __asm__ __volatile__( - "int $0x40" - :"=a"(val) - :"a"(68),"b"(12),"c"(size)); - return val; -} - -static inline -int user_free(void *mem) -{ - int val; - -// __asm__("int3"); - - __asm__ __volatile__( - "int $0x40" - :"=a"(val) - :"a"(68),"b"(13),"c"(mem)); - return val; -} - +#define user_alloc(s) _ksys_alloc(s) +#define user_free _ksys_free /* ------------------- size_t and alignment properties -------------------- */ diff --git a/contrib/sdk/sources/newlib/libc/sys/clock_gettime.c b/contrib/sdk/sources/newlib/libc/sys/clock_gettime.c index f5860d0376..f5eab820ed 100644 --- a/contrib/sdk/sources/newlib/libc/sys/clock_gettime.c +++ b/contrib/sdk/sources/newlib/libc/sys/clock_gettime.c @@ -20,14 +20,14 @@ #include #include #include -#include +#include /* Get current value of CLOCK and store it in TP. */ int clock_gettime (clockid_t clock_id, struct timespec *tp) { uint64_t tsc; - tsc = get_ns_count(); + tsc = _ksys_get_ns_count(); tp->tv_sec = tsc / 1000000000; tp->tv_nsec = tsc % 1000000000;