newlib: Try to unify wrappers. And get rid of kos32sys.h

git-svn-id: svn://kolibrios.org@9874 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2022-09-01 19:19:27 +00:00
parent f86f8feb93
commit 6c347146ee
7 changed files with 78 additions and 94 deletions

View File

@ -21,7 +21,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include <errno.h>
#include <kos32sys.h>
#include <stdint.h>
#include <sys/ksys.h>
#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,11 +148,7 @@ 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));
_ksys_futex_wait(mutex->handle, 2 , 0);
}
return 0;
}
@ -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;
}

View File

@ -22,6 +22,10 @@
#include <stddef.h>
#include <stdint.h>
#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_

View File

@ -6,7 +6,7 @@
#include <setjmp.h>
#include <envz.h>
#include <kos32sys.h>
#include <sys/ksys.h>
#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;

View File

@ -7,7 +7,7 @@
#include <setjmp.h>
#include <envz.h>
#include <kos32sys.h>
#include <sys/ksys.h>
#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;
};

View File

@ -3,7 +3,7 @@
#include <_ansi.h>
#include <string.h>
#include <sys/reent.h>
#include <kos32sys.h>
#include <sys/ksys.h>
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);

View File

@ -36,7 +36,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ksys.h>
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 -------------------- */

View File

@ -20,14 +20,14 @@
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#include <kos32sys.h>
#include <sys/ksys.h>
/* 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;