Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
This commit is contained in:
126
core/shared/platform/zephyr/COPYRIGHT
Normal file
126
core/shared/platform/zephyr/COPYRIGHT
Normal file
@ -0,0 +1,126 @@
|
||||
# $FreeBSD$
|
||||
# @(#)COPYRIGHT 8.2 (Berkeley) 3/21/94
|
||||
|
||||
The compilation of software known as FreeBSD is distributed under the
|
||||
following terms:
|
||||
|
||||
Copyright (c) 1992-2019 The FreeBSD Project.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGE.
|
||||
|
||||
The 4.4BSD and 4.4BSD-Lite software is distributed under the following
|
||||
terms:
|
||||
|
||||
All of the documentation and software included in the 4.4BSD and 4.4BSD-Lite
|
||||
Releases is copyrighted by The Regents of the University of California.
|
||||
|
||||
Copyright 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
|
||||
The Regents of the University of California. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. All advertising materials mentioning features or use of this software
|
||||
must display the following acknowledgement:
|
||||
This product includes software developed by the University of
|
||||
California, Berkeley and its contributors.
|
||||
4. Neither the name of the University nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGE.
|
||||
|
||||
The Institute of Electrical and Electronics Engineers and the American
|
||||
National Standards Committee X3, on Information Processing Systems have
|
||||
given us permission to reprint portions of their documentation.
|
||||
|
||||
In the following statement, the phrase ``this text'' refers to portions
|
||||
of the system documentation.
|
||||
|
||||
Portions of this text are reprinted and reproduced in electronic form in
|
||||
the second BSD Networking Software Release, from IEEE Std 1003.1-1988, IEEE
|
||||
Standard Portable Operating System Interface for Computer Environments
|
||||
(POSIX), copyright C 1988 by the Institute of Electrical and Electronics
|
||||
Engineers, Inc. In the event of any discrepancy between these versions
|
||||
and the original IEEE Standard, the original IEEE Standard is the referee
|
||||
document.
|
||||
|
||||
In the following statement, the phrase ``This material'' refers to portions
|
||||
of the system documentation.
|
||||
|
||||
This material is reproduced with permission from American National
|
||||
Standards Committee X3, on Information Processing Systems. Computer and
|
||||
Business Equipment Manufacturers Association (CBEMA), 311 First St., NW,
|
||||
Suite 500, Washington, DC 20001-2178. The developmental work of
|
||||
Programming Language C was completed by the X3J11 Technical Committee.
|
||||
|
||||
The views and conclusions contained in the software and documentation are
|
||||
those of the authors and should not be interpreted as representing official
|
||||
policies, either expressed or implied, of the Regents of the University
|
||||
of California.
|
||||
|
||||
|
||||
NOTE: The copyright of UC Berkeley's Berkeley Software Distribution ("BSD")
|
||||
source has been updated. The copyright addendum may be found at
|
||||
ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change and is
|
||||
included below.
|
||||
|
||||
July 22, 1999
|
||||
|
||||
To All Licensees, Distributors of Any Version of BSD:
|
||||
|
||||
As you know, certain of the Berkeley Software Distribution ("BSD") source
|
||||
code files require that further distributions of products containing all or
|
||||
portions of the software, acknowledge within their advertising materials
|
||||
that such products contain software developed by UC Berkeley and its
|
||||
contributors.
|
||||
|
||||
Specifically, the provision reads:
|
||||
|
||||
" * 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors."
|
||||
|
||||
Effective immediately, licensees and distributors are no longer required to
|
||||
include the acknowledgement within advertising materials. Accordingly, the
|
||||
foregoing paragraph of those BSD Unix files containing it is hereby deleted
|
||||
in its entirety.
|
||||
|
||||
William Hoskins
|
||||
Director, Office of Technology Licensing
|
||||
University of California, Berkeley
|
||||
4
core/shared/platform/zephyr/Makefile
Normal file
4
core/shared/platform/zephyr/Makefile
Normal file
@ -0,0 +1,4 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
obj-y += bh_assert.o bh_definition.o bh_memory.o bh_platform_log.o bh_thread.o bh_time.o
|
||||
58
core/shared/platform/zephyr/bh_assert.c
Normal file
58
core/shared/platform/zephyr/bh_assert.c
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef BH_TEST
|
||||
#include <setjmp.h>
|
||||
#endif
|
||||
|
||||
#ifdef BH_TEST
|
||||
/* for exception throwing */
|
||||
jmp_buf bh_test_jb;
|
||||
#endif
|
||||
extern void abort(void);
|
||||
void bh_assert_internal(int v, const char *file_name, int line_number,
|
||||
const char *expr_string)
|
||||
{
|
||||
if (v)
|
||||
return;
|
||||
|
||||
if (!file_name)
|
||||
file_name = "NULL FILENAME";
|
||||
if (!expr_string)
|
||||
expr_string = "NULL EXPR_STRING";
|
||||
|
||||
printk("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
|
||||
file_name, line_number);
|
||||
|
||||
#ifdef BH_TEST
|
||||
longjmp(bh_test_jb, 1);
|
||||
#endif
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
|
||||
...)
|
||||
{
|
||||
#ifndef JEFF_TEST_VERIFIER
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
bh_assert(file_name);
|
||||
|
||||
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
|
||||
vprintf(fmt, args);
|
||||
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
53
core/shared/platform/zephyr/bh_definition.c
Normal file
53
core/shared/platform/zephyr/bh_definition.c
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#define RSIZE_MAX 0x7FFFFFFF
|
||||
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
|
||||
{
|
||||
char *dest = (char*) s1;
|
||||
char *src = (char*) s2;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s1 == NULL || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
if (s2 == NULL || n > s1max) {
|
||||
memset(dest, 0, s1max);
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcat_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL == s1 || NULL == s2
|
||||
|| s1max < (strlen(s1) + strlen(s2) + 1)
|
||||
|| s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcat(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL == s1|| NULL == s2
|
||||
|| s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcpy(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
861
core/shared/platform/zephyr/bh_math.c
Normal file
861
core/shared/platform/zephyr/bh_math.c
Normal file
@ -0,0 +1,861 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
||||
*
|
||||
* Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#define __FDLIBM_STDC__
|
||||
|
||||
typedef uint32_t u_int32_t;
|
||||
typedef uint64_t u_int64_t;
|
||||
|
||||
typedef union u32double_tag {
|
||||
int *pint;
|
||||
double *pdouble;
|
||||
} U32DOUBLE;
|
||||
|
||||
static inline int *
|
||||
pdouble2pint(double *pdouble)
|
||||
{
|
||||
U32DOUBLE u;
|
||||
u.pdouble = pdouble;
|
||||
return u.pint;
|
||||
}
|
||||
|
||||
typedef union
|
||||
{
|
||||
double value;
|
||||
struct
|
||||
{
|
||||
u_int32_t lsw;
|
||||
u_int32_t msw;
|
||||
} parts;
|
||||
struct
|
||||
{
|
||||
u_int64_t w;
|
||||
} xparts;
|
||||
} ieee_double_shape_type_little;
|
||||
|
||||
typedef union
|
||||
{
|
||||
double value;
|
||||
struct
|
||||
{
|
||||
u_int32_t msw;
|
||||
u_int32_t lsw;
|
||||
} parts;
|
||||
struct
|
||||
{
|
||||
u_int64_t w;
|
||||
} xparts;
|
||||
} ieee_double_shape_type_big;
|
||||
|
||||
typedef union {
|
||||
double d;
|
||||
struct {
|
||||
unsigned int manl :32;
|
||||
unsigned int manh :20;
|
||||
unsigned int exp :11;
|
||||
unsigned int sign :1;
|
||||
} bits;
|
||||
} IEEEd2bits_L;
|
||||
|
||||
typedef union {
|
||||
double d;
|
||||
struct {
|
||||
unsigned int sign :1;
|
||||
unsigned int exp :11;
|
||||
unsigned int manh :20;
|
||||
unsigned int manl :32;
|
||||
} bits;
|
||||
} IEEEd2bits_B;
|
||||
|
||||
typedef union {
|
||||
float f;
|
||||
struct {
|
||||
unsigned int man :23;
|
||||
unsigned int exp :8;
|
||||
unsigned int sign :1;
|
||||
} bits;
|
||||
} IEEEf2bits_L;
|
||||
|
||||
typedef union {
|
||||
float f;
|
||||
struct {
|
||||
unsigned int sign :1;
|
||||
unsigned int exp :8;
|
||||
unsigned int man :23;
|
||||
} bits;
|
||||
} IEEEf2bits_B;
|
||||
|
||||
static union {
|
||||
int a;
|
||||
char b;
|
||||
} __ue = { .a = 1 };
|
||||
|
||||
#define is_little_endian() (__ue.b == 1)
|
||||
|
||||
#define __HIL(x) *(1+pdouble2pint(&x))
|
||||
#define __LOL(x) *(pdouble2pint(&x))
|
||||
#define __HIB(x) *(int*)&x
|
||||
#define __LOB(x) *(1+(int*)&x)
|
||||
|
||||
/* Get two 32 bit ints from a double. */
|
||||
|
||||
#define EXTRACT_WORDS_L(ix0,ix1,d) \
|
||||
do { \
|
||||
ieee_double_shape_type_little ew_u; \
|
||||
ew_u.value = (d); \
|
||||
(ix0) = ew_u.parts.msw; \
|
||||
(ix1) = ew_u.parts.lsw; \
|
||||
} while (0)
|
||||
|
||||
/* Set a double from two 32 bit ints. */
|
||||
|
||||
#define INSERT_WORDS_L(d,ix0,ix1) \
|
||||
do { \
|
||||
ieee_double_shape_type_little iw_u; \
|
||||
iw_u.parts.msw = (ix0); \
|
||||
iw_u.parts.lsw = (ix1); \
|
||||
(d) = iw_u.value; \
|
||||
} while (0)
|
||||
|
||||
/* Get two 32 bit ints from a double. */
|
||||
|
||||
#define EXTRACT_WORDS_B(ix0,ix1,d) \
|
||||
do { \
|
||||
ieee_double_shape_type_big ew_u; \
|
||||
ew_u.value = (d); \
|
||||
(ix0) = ew_u.parts.msw; \
|
||||
(ix1) = ew_u.parts.lsw; \
|
||||
} while (0)
|
||||
|
||||
/* Set a double from two 32 bit ints. */
|
||||
|
||||
#define INSERT_WORDS_B(d,ix0,ix1) \
|
||||
do { \
|
||||
ieee_double_shape_type_big iw_u; \
|
||||
iw_u.parts.msw = (ix0); \
|
||||
iw_u.parts.lsw = (ix1); \
|
||||
(d) = iw_u.value; \
|
||||
} while (0)
|
||||
|
||||
/* Get the more significant 32 bit int from a double. */
|
||||
#define GET_HIGH_WORD_L(i,d) \
|
||||
do { \
|
||||
ieee_double_shape_type_little gh_u; \
|
||||
gh_u.value = (d); \
|
||||
(i) = gh_u.parts.msw; \
|
||||
} while (0)
|
||||
|
||||
/* Get the more significant 32 bit int from a double. */
|
||||
#define GET_HIGH_WORD_B(i,d) \
|
||||
do { \
|
||||
ieee_double_shape_type_big gh_u; \
|
||||
gh_u.value = (d); \
|
||||
(i) = gh_u.parts.msw; \
|
||||
} while (0)
|
||||
|
||||
/* Set the more significant 32 bits of a double from an int. */
|
||||
#define SET_HIGH_WORD_L(d,v) \
|
||||
do { \
|
||||
ieee_double_shape_type_little sh_u; \
|
||||
sh_u.value = (d); \
|
||||
sh_u.parts.msw = (v); \
|
||||
(d) = sh_u.value; \
|
||||
} while (0)
|
||||
|
||||
/* Set the more significant 32 bits of a double from an int. */
|
||||
#define SET_HIGH_WORD_B(d,v) \
|
||||
do { \
|
||||
ieee_double_shape_type_big sh_u; \
|
||||
sh_u.value = (d); \
|
||||
sh_u.parts.msw = (v); \
|
||||
(d) = sh_u.value; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* A union which permits us to convert between a float and a 32 bit
|
||||
* int.
|
||||
*/
|
||||
typedef union
|
||||
{
|
||||
float value;
|
||||
/* FIXME: Assumes 32 bit int. */
|
||||
unsigned int word;
|
||||
} ieee_float_shape_type;
|
||||
|
||||
/* Get a 32 bit int from a float. */
|
||||
#define GET_FLOAT_WORD(i,d) \
|
||||
do { \
|
||||
ieee_float_shape_type gf_u; \
|
||||
gf_u.value = (d); \
|
||||
(i) = gf_u.word; \
|
||||
} while (0)
|
||||
|
||||
/* Set a float from a 32 bit int. */
|
||||
#define SET_FLOAT_WORD(d,i) \
|
||||
do { \
|
||||
ieee_float_shape_type sf_u; \
|
||||
sf_u.word = (i); \
|
||||
(d) = sf_u.value; \
|
||||
} while (0)
|
||||
|
||||
/* Macro wrappers. */
|
||||
#define EXTRACT_WORDS(ix0,ix1,d) do { \
|
||||
if (is_little_endian()) \
|
||||
EXTRACT_WORDS_L(ix0,ix1,d); \
|
||||
else \
|
||||
EXTRACT_WORDS_B(ix0,ix1,d); \
|
||||
} while (0)
|
||||
|
||||
#define INSERT_WORDS(d,ix0,ix1) do { \
|
||||
if (is_little_endian()) \
|
||||
INSERT_WORDS_L(d,ix0,ix1); \
|
||||
else \
|
||||
INSERT_WORDS_B(d,ix0,ix1); \
|
||||
} while (0)
|
||||
|
||||
#define GET_HIGH_WORD(i,d) \
|
||||
do { \
|
||||
if (is_little_endian()) \
|
||||
GET_HIGH_WORD_L(i,d); \
|
||||
else \
|
||||
GET_HIGH_WORD_B(i,d); \
|
||||
} while (0)
|
||||
|
||||
#define SET_HIGH_WORD(d,v) \
|
||||
do { \
|
||||
if (is_little_endian()) \
|
||||
SET_HIGH_WORD_L(d,v); \
|
||||
else \
|
||||
SET_HIGH_WORD_B(d,v); \
|
||||
} while (0)
|
||||
|
||||
#define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x))
|
||||
|
||||
#define __LO(x) (is_little_endian() ? __LOL(x) : __LOB(x))
|
||||
|
||||
/*
|
||||
* Attempt to get strict C99 semantics for assignment with non-C99 compilers.
|
||||
*/
|
||||
#if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
|
||||
#define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
|
||||
#else
|
||||
#define STRICT_ASSIGN(type, lval, rval) do { \
|
||||
volatile type __lval; \
|
||||
\
|
||||
if (sizeof(type) >= sizeof(long double)) \
|
||||
(lval) = (rval); \
|
||||
else { \
|
||||
__lval = (rval); \
|
||||
(lval) = __lval; \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifdef __FDLIBM_STDC__
|
||||
static const double huge = 1.0e300;
|
||||
#else
|
||||
static double huge = 1.0e300;
|
||||
#endif
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
tiny = 1.0e-300;
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
one= 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
TWO52[2]={
|
||||
4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
|
||||
-4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
|
||||
};
|
||||
|
||||
static double freebsd_sqrt(double x);
|
||||
static double freebsd_floor(double x);
|
||||
static double freebsd_ceil(double x);
|
||||
static double freebsd_fabs(double x);
|
||||
static double freebsd_rint(double x);
|
||||
static int freebsd_isnan(double x);
|
||||
|
||||
static double freebsd_sqrt(double x) /* wrapper sqrt */
|
||||
{
|
||||
double z;
|
||||
int32_t sign = (int)0x80000000;
|
||||
int32_t ix0,s0,q,m,t,i;
|
||||
u_int32_t r,t1,s1,ix1,q1;
|
||||
|
||||
EXTRACT_WORDS(ix0,ix1,x);
|
||||
|
||||
/* take care of Inf and NaN */
|
||||
if((ix0&0x7ff00000)==0x7ff00000) {
|
||||
return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
|
||||
sqrt(-inf)=sNaN */
|
||||
}
|
||||
/* take care of zero */
|
||||
if(ix0<=0) {
|
||||
if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */
|
||||
else if(ix0<0)
|
||||
return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
|
||||
}
|
||||
/* normalize x */
|
||||
m = (ix0>>20);
|
||||
if(m==0) { /* subnormal x */
|
||||
while(ix0==0) {
|
||||
m -= 21;
|
||||
ix0 |= (ix1>>11); ix1 <<= 21;
|
||||
}
|
||||
for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;
|
||||
m -= i-1;
|
||||
ix0 |= (ix1>>(32-i));
|
||||
ix1 <<= i;
|
||||
}
|
||||
m -= 1023; /* unbias exponent */
|
||||
ix0 = (ix0&0x000fffff)|0x00100000;
|
||||
if(m&1){ /* odd m, double x to make it even */
|
||||
ix0 += ix0 + ((ix1&sign)>>31);
|
||||
ix1 += ix1;
|
||||
}
|
||||
m >>= 1; /* m = [m/2] */
|
||||
|
||||
/* generate sqrt(x) bit by bit */
|
||||
ix0 += ix0 + ((ix1&sign)>>31);
|
||||
ix1 += ix1;
|
||||
q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
|
||||
r = 0x00200000; /* r = moving bit from right to left */
|
||||
|
||||
while(r!=0) {
|
||||
t = s0+r;
|
||||
if(t<=ix0) {
|
||||
s0 = t+r;
|
||||
ix0 -= t;
|
||||
q += r;
|
||||
}
|
||||
ix0 += ix0 + ((ix1&sign)>>31);
|
||||
ix1 += ix1;
|
||||
r>>=1;
|
||||
}
|
||||
|
||||
r = sign;
|
||||
while(r!=0) {
|
||||
t1 = s1+r;
|
||||
t = s0;
|
||||
if((t<ix0)||((t==ix0)&&(t1<=ix1))) {
|
||||
s1 = t1+r;
|
||||
if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;
|
||||
ix0 -= t;
|
||||
if (ix1 < t1) ix0 -= 1;
|
||||
ix1 -= t1;
|
||||
q1 += r;
|
||||
}
|
||||
ix0 += ix0 + ((ix1&sign)>>31);
|
||||
ix1 += ix1;
|
||||
r>>=1;
|
||||
}
|
||||
|
||||
/* use floating add to find out rounding direction */
|
||||
if((ix0|ix1)!=0) {
|
||||
z = one-tiny; /* trigger inexact flag */
|
||||
if (z>=one) {
|
||||
z = one+tiny;
|
||||
if (q1==(u_int32_t)0xffffffff) { q1=0; q += 1;}
|
||||
else if (z>one) {
|
||||
if (q1==(u_int32_t)0xfffffffe) q+=1;
|
||||
q1+=2;
|
||||
} else
|
||||
q1 += (q1&1);
|
||||
}
|
||||
}
|
||||
ix0 = (q>>1)+0x3fe00000;
|
||||
ix1 = q1>>1;
|
||||
if ((q&1)==1) ix1 |= sign;
|
||||
ix0 += (m <<20);
|
||||
|
||||
INSERT_WORDS(z,ix0,ix1);
|
||||
|
||||
return z;
|
||||
}
|
||||
|
||||
static double freebsd_floor(double x)
|
||||
{
|
||||
int32_t i0,i1,j0;
|
||||
u_int32_t i,j;
|
||||
|
||||
EXTRACT_WORDS(i0,i1,x);
|
||||
|
||||
j0 = ((i0>>20)&0x7ff)-0x3ff;
|
||||
if(j0<20) {
|
||||
if(j0<0) { /* raise inexact if x != 0 */
|
||||
if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
|
||||
if(i0>=0) {i0=i1=0;}
|
||||
else if(((i0&0x7fffffff)|i1)!=0)
|
||||
{ i0=0xbff00000;i1=0;}
|
||||
}
|
||||
} else {
|
||||
i = (0x000fffff)>>j0;
|
||||
if(((i0&i)|i1)==0) return x; /* x is integral */
|
||||
if(huge+x>0.0) { /* raise inexact flag */
|
||||
if(i0<0) i0 += (0x00100000)>>j0;
|
||||
i0 &= (~i); i1=0;
|
||||
}
|
||||
}
|
||||
} else if (j0>51) {
|
||||
if(j0==0x400) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
} else {
|
||||
i = ((u_int32_t)(0xffffffff))>>(j0-20);
|
||||
if((i1&i)==0) return x; /* x is integral */
|
||||
if(huge+x>0.0) { /* raise inexact flag */
|
||||
if(i0<0) {
|
||||
if(j0==20) i0+=1;
|
||||
else {
|
||||
j = i1+(1<<(52-j0));
|
||||
if(j<i1) i0 +=1 ; /* got a carry */
|
||||
i1=j;
|
||||
}
|
||||
}
|
||||
i1 &= (~i);
|
||||
}
|
||||
}
|
||||
|
||||
INSERT_WORDS(x,i0,i1);
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
static double freebsd_ceil(double x)
|
||||
{
|
||||
int32_t i0,i1,j0;
|
||||
u_int32_t i,j;
|
||||
EXTRACT_WORDS(i0,i1,x);
|
||||
j0 = ((i0>>20)&0x7ff)-0x3ff;
|
||||
if(j0<20) {
|
||||
if(j0<0) { /* raise inexact if x != 0 */
|
||||
if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
|
||||
if(i0<0) {i0=0x80000000;i1=0;}
|
||||
else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
|
||||
}
|
||||
} else {
|
||||
i = (0x000fffff)>>j0;
|
||||
if(((i0&i)|i1)==0) return x; /* x is integral */
|
||||
if(huge+x>0.0) { /* raise inexact flag */
|
||||
if(i0>0) i0 += (0x00100000)>>j0;
|
||||
i0 &= (~i); i1=0;
|
||||
}
|
||||
}
|
||||
} else if (j0>51) {
|
||||
if(j0==0x400) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
} else {
|
||||
i = ((u_int32_t)(0xffffffff))>>(j0-20);
|
||||
if((i1&i)==0) return x; /* x is integral */
|
||||
if(huge+x>0.0) { /* raise inexact flag */
|
||||
if(i0>0) {
|
||||
if(j0==20) i0+=1;
|
||||
else {
|
||||
j = i1 + (1<<(52-j0));
|
||||
if(j<i1) i0+=1; /* got a carry */
|
||||
i1 = j;
|
||||
}
|
||||
}
|
||||
i1 &= (~i);
|
||||
}
|
||||
}
|
||||
INSERT_WORDS(x,i0,i1);
|
||||
return x;
|
||||
}
|
||||
|
||||
static double freebsd_rint(double x)
|
||||
{
|
||||
int32_t i0,j0,sx;
|
||||
u_int32_t i,i1;
|
||||
double w,t;
|
||||
EXTRACT_WORDS(i0,i1,x);
|
||||
sx = (i0>>31)&1;
|
||||
j0 = ((i0>>20)&0x7ff)-0x3ff;
|
||||
if(j0<20) {
|
||||
if(j0<0) {
|
||||
if(((i0&0x7fffffff)|i1)==0) return x;
|
||||
i1 |= (i0&0x0fffff);
|
||||
i0 &= 0xfffe0000;
|
||||
i0 |= ((i1|-i1)>>12)&0x80000;
|
||||
SET_HIGH_WORD(x,i0);
|
||||
STRICT_ASSIGN(double,w,TWO52[sx]+x);
|
||||
t = w-TWO52[sx];
|
||||
GET_HIGH_WORD(i0,t);
|
||||
SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31));
|
||||
return t;
|
||||
} else {
|
||||
i = (0x000fffff)>>j0;
|
||||
if(((i0&i)|i1)==0) return x; /* x is integral */
|
||||
i>>=1;
|
||||
if(((i0&i)|i1)!=0) {
|
||||
/*
|
||||
* Some bit is set after the 0.5 bit. To avoid the
|
||||
* possibility of errors from double rounding in
|
||||
* w = TWO52[sx]+x, adjust the 0.25 bit to a lower
|
||||
* guard bit. We do this for all j0<=51. The
|
||||
* adjustment is trickiest for j0==18 and j0==19
|
||||
* since then it spans the word boundary.
|
||||
*/
|
||||
if(j0==19) i1 = 0x40000000; else
|
||||
if(j0==18) i1 = 0x80000000; else
|
||||
i0 = (i0&(~i))|((0x20000)>>j0);
|
||||
}
|
||||
}
|
||||
} else if (j0>51) {
|
||||
if(j0==0x400) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
} else {
|
||||
i = ((u_int32_t)(0xffffffff))>>(j0-20);
|
||||
if((i1&i)==0) return x; /* x is integral */
|
||||
i>>=1;
|
||||
if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20));
|
||||
}
|
||||
INSERT_WORDS(x,i0,i1);
|
||||
STRICT_ASSIGN(double,w,TWO52[sx]+x);
|
||||
return w-TWO52[sx];
|
||||
}
|
||||
|
||||
static int freebsd_isnan(double d)
|
||||
{
|
||||
if (is_little_endian()) {
|
||||
IEEEd2bits_L u;
|
||||
u.d = d;
|
||||
return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
|
||||
}
|
||||
else {
|
||||
IEEEd2bits_B u;
|
||||
u.d = d;
|
||||
return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
|
||||
}
|
||||
}
|
||||
|
||||
static double freebsd_fabs(double x)
|
||||
{
|
||||
u_int32_t high;
|
||||
GET_HIGH_WORD(high,x);
|
||||
SET_HIGH_WORD(x,high&0x7fffffff);
|
||||
return x;
|
||||
}
|
||||
|
||||
static const float huge_f = 1.0e30F;
|
||||
|
||||
static const float
|
||||
TWO23[2]={
|
||||
8.3886080000e+06, /* 0x4b000000 */
|
||||
-8.3886080000e+06, /* 0xcb000000 */
|
||||
};
|
||||
|
||||
static float
|
||||
freebsd_truncf(float x)
|
||||
{
|
||||
int32_t i0,j0;
|
||||
u_int32_t i;
|
||||
GET_FLOAT_WORD(i0,x);
|
||||
j0 = ((i0>>23)&0xff)-0x7f;
|
||||
if(j0<23) {
|
||||
if(j0<0) { /* raise inexact if x != 0 */
|
||||
if(huge_f+x>0.0F) /* |x|<1, so return 0*sign(x) */
|
||||
i0 &= 0x80000000;
|
||||
} else {
|
||||
i = (0x007fffff)>>j0;
|
||||
if((i0&i)==0) return x; /* x is integral */
|
||||
if(huge_f+x>0.0F) /* raise inexact flag */
|
||||
i0 &= (~i);
|
||||
}
|
||||
} else {
|
||||
if(j0==0x80) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
}
|
||||
SET_FLOAT_WORD(x,i0);
|
||||
return x;
|
||||
}
|
||||
|
||||
static float
|
||||
freebsd_rintf(float x)
|
||||
{
|
||||
int32_t i0,j0,sx;
|
||||
float w,t;
|
||||
GET_FLOAT_WORD(i0,x);
|
||||
sx = (i0>>31)&1;
|
||||
j0 = ((i0>>23)&0xff)-0x7f;
|
||||
if(j0<23) {
|
||||
if(j0<0) {
|
||||
if((i0&0x7fffffff)==0) return x;
|
||||
STRICT_ASSIGN(float,w,TWO23[sx]+x);
|
||||
t = w-TWO23[sx];
|
||||
GET_FLOAT_WORD(i0,t);
|
||||
SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
|
||||
return t;
|
||||
}
|
||||
STRICT_ASSIGN(float,w,TWO23[sx]+x);
|
||||
return w-TWO23[sx];
|
||||
}
|
||||
if(j0==0x80) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
}
|
||||
|
||||
static float
|
||||
freebsd_ceilf(float x)
|
||||
{
|
||||
int32_t i0,j0;
|
||||
u_int32_t i;
|
||||
|
||||
GET_FLOAT_WORD(i0,x);
|
||||
j0 = ((i0>>23)&0xff)-0x7f;
|
||||
if(j0<23) {
|
||||
if(j0<0) { /* raise inexact if x != 0 */
|
||||
if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
|
||||
if(i0<0) {i0=0x80000000;}
|
||||
else if(i0!=0) { i0=0x3f800000;}
|
||||
}
|
||||
} else {
|
||||
i = (0x007fffff)>>j0;
|
||||
if((i0&i)==0) return x; /* x is integral */
|
||||
if(huge_f+x>(float)0.0) { /* raise inexact flag */
|
||||
if(i0>0) i0 += (0x00800000)>>j0;
|
||||
i0 &= (~i);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(j0==0x80) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
}
|
||||
SET_FLOAT_WORD(x,i0);
|
||||
return x;
|
||||
}
|
||||
|
||||
static float
|
||||
freebsd_floorf(float x)
|
||||
{
|
||||
int32_t i0,j0;
|
||||
u_int32_t i;
|
||||
GET_FLOAT_WORD(i0,x);
|
||||
j0 = ((i0>>23)&0xff)-0x7f;
|
||||
if(j0<23) {
|
||||
if(j0<0) { /* raise inexact if x != 0 */
|
||||
if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
|
||||
if(i0>=0) {i0=0;}
|
||||
else if((i0&0x7fffffff)!=0)
|
||||
{ i0=0xbf800000;}
|
||||
}
|
||||
} else {
|
||||
i = (0x007fffff)>>j0;
|
||||
if((i0&i)==0) return x; /* x is integral */
|
||||
if(huge_f+x>(float)0.0) { /* raise inexact flag */
|
||||
if(i0<0) i0 += (0x00800000)>>j0;
|
||||
i0 &= (~i);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(j0==0x80) return x+x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
}
|
||||
SET_FLOAT_WORD(x,i0);
|
||||
return x;
|
||||
}
|
||||
|
||||
static float
|
||||
freebsd_fminf(float x, float y)
|
||||
{
|
||||
if (is_little_endian()) {
|
||||
IEEEf2bits_L u[2];
|
||||
|
||||
u[0].f = x;
|
||||
u[1].f = y;
|
||||
|
||||
/* Check for NaNs to avoid raising spurious exceptions. */
|
||||
if (u[0].bits.exp == 255 && u[0].bits.man != 0)
|
||||
return (y);
|
||||
if (u[1].bits.exp == 255 && u[1].bits.man != 0)
|
||||
return (x);
|
||||
|
||||
/* Handle comparisons of signed zeroes. */
|
||||
if (u[0].bits.sign != u[1].bits.sign)
|
||||
return (u[u[1].bits.sign].f);
|
||||
}
|
||||
else {
|
||||
IEEEf2bits_B u[2];
|
||||
|
||||
u[0].f = x;
|
||||
u[1].f = y;
|
||||
|
||||
/* Check for NaNs to avoid raising spurious exceptions. */
|
||||
if (u[0].bits.exp == 255 && u[0].bits.man != 0)
|
||||
return (y);
|
||||
if (u[1].bits.exp == 255 && u[1].bits.man != 0)
|
||||
return (x);
|
||||
|
||||
/* Handle comparisons of signed zeroes. */
|
||||
if (u[0].bits.sign != u[1].bits.sign)
|
||||
return (u[u[1].bits.sign].f);
|
||||
}
|
||||
|
||||
return (x < y ? x : y);
|
||||
}
|
||||
|
||||
static float
|
||||
freebsd_fmaxf(float x, float y)
|
||||
{
|
||||
if (is_little_endian()) {
|
||||
IEEEf2bits_L u[2];
|
||||
|
||||
u[0].f = x;
|
||||
u[1].f = y;
|
||||
|
||||
/* Check for NaNs to avoid raising spurious exceptions. */
|
||||
if (u[0].bits.exp == 255 && u[0].bits.man != 0)
|
||||
return (y);
|
||||
if (u[1].bits.exp == 255 && u[1].bits.man != 0)
|
||||
return (x);
|
||||
|
||||
/* Handle comparisons of signed zeroes. */
|
||||
if (u[0].bits.sign != u[1].bits.sign)
|
||||
return (u[u[0].bits.sign].f);
|
||||
}
|
||||
else {
|
||||
IEEEf2bits_B u[2];
|
||||
|
||||
u[0].f = x;
|
||||
u[1].f = y;
|
||||
|
||||
/* Check for NaNs to avoid raising spurious exceptions. */
|
||||
if (u[0].bits.exp == 255 && u[0].bits.man != 0)
|
||||
return (y);
|
||||
if (u[1].bits.exp == 255 && u[1].bits.man != 0)
|
||||
return (x);
|
||||
|
||||
/* Handle comparisons of signed zeroes. */
|
||||
if (u[0].bits.sign != u[1].bits.sign)
|
||||
return (u[u[0].bits.sign].f);
|
||||
}
|
||||
|
||||
return (x > y ? x : y);
|
||||
}
|
||||
|
||||
double sqrt(double x)
|
||||
{
|
||||
return freebsd_sqrt(x);
|
||||
}
|
||||
|
||||
double floor(double x)
|
||||
{
|
||||
return freebsd_floor(x);
|
||||
}
|
||||
|
||||
double ceil(double x)
|
||||
{
|
||||
return freebsd_ceil(x);
|
||||
}
|
||||
|
||||
double fmin(double x, double y)
|
||||
{
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
double fmax(double x, double y)
|
||||
{
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
double rint(double x)
|
||||
{
|
||||
return freebsd_rint(x);
|
||||
}
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
return freebsd_fabs(x);
|
||||
}
|
||||
|
||||
int isnan(double x)
|
||||
{
|
||||
return freebsd_isnan(x);
|
||||
}
|
||||
|
||||
double trunc(double x)
|
||||
{
|
||||
return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
|
||||
}
|
||||
|
||||
int signbit(double x)
|
||||
{
|
||||
return ((__HI(x) & 0x80000000) >> 31);
|
||||
}
|
||||
|
||||
float
|
||||
truncf(float x)
|
||||
{
|
||||
return freebsd_truncf(x);
|
||||
}
|
||||
|
||||
float
|
||||
rintf(float x)
|
||||
{
|
||||
return freebsd_rintf(x);
|
||||
}
|
||||
|
||||
float
|
||||
ceilf(float x)
|
||||
{
|
||||
return freebsd_ceilf(x);
|
||||
}
|
||||
|
||||
float
|
||||
floorf(float x)
|
||||
{
|
||||
return freebsd_floorf(x);
|
||||
}
|
||||
|
||||
float
|
||||
fminf(float x, float y)
|
||||
{
|
||||
return freebsd_fminf(x, y);
|
||||
}
|
||||
|
||||
float
|
||||
fmaxf(float x, float y)
|
||||
{
|
||||
return freebsd_fmaxf(x, y);
|
||||
}
|
||||
|
||||
56
core/shared/platform/zephyr/bh_platform.c
Executable file
56
core/shared/platform/zephyr/bh_platform.c
Executable file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_memory.h"
|
||||
#include "bh_common.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *bh_strdup(const char *s)
|
||||
{
|
||||
uint32 size;
|
||||
char *s1 = NULL;
|
||||
|
||||
if (s) {
|
||||
size = (uint32)(strlen(s) + 1);
|
||||
if ((s1 = bh_malloc(size)))
|
||||
bh_memcpy_s(s1, size, s, size);
|
||||
}
|
||||
return s1;
|
||||
}
|
||||
|
||||
static int
|
||||
_stdout_hook_iwasm(int c)
|
||||
{
|
||||
printk("%c", (char)c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bh_platform_init()
|
||||
{
|
||||
extern void __stdout_hook_install(int (*hook)(int));
|
||||
/* Enable printf() in Zephyr */
|
||||
__stdout_hook_install(_stdout_hook_iwasm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
bh_mmap(void *hint, unsigned int size, int prot, int flags)
|
||||
{
|
||||
return bh_malloc(size);
|
||||
}
|
||||
|
||||
void
|
||||
bh_munmap(void *addr, uint32 size)
|
||||
{
|
||||
return bh_free(addr);
|
||||
}
|
||||
|
||||
int
|
||||
bh_mprotect(void *addr, uint32 size, int prot)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
157
core/shared/platform/zephyr/bh_platform.h
Normal file
157
core/shared/platform/zephyr/bh_platform.h
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _BH_PLATFORM_H
|
||||
#define _BH_PLATFORM_H
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_types.h"
|
||||
#include "bh_memory.h"
|
||||
#include <autoconf.h>
|
||||
#include <zephyr.h>
|
||||
#include <kernel.h>
|
||||
#include <sys/printk.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#ifndef CONFIG_NET_BUF_USER_DATA_SIZE
|
||||
#define CONFIG_NET_BUF_USER_DATA_SIZE 0
|
||||
#endif
|
||||
#include <net/net_pkt.h>
|
||||
#include <net/net_if.h>
|
||||
#include <net/net_ip.h>
|
||||
#include <net/net_core.h>
|
||||
#include <net/net_context.h>
|
||||
|
||||
#ifndef BH_PLATFORM_ZEPHYR
|
||||
#define BH_PLATFORM_ZEPHYR
|
||||
#endif
|
||||
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
|
||||
|
||||
/* Default thread priority */
|
||||
#define BH_THREAD_DEFAULT_PRIORITY 7
|
||||
|
||||
#define BH_ROUTINE_MODIFIER
|
||||
|
||||
/* Invalid thread tid */
|
||||
#define INVALID_THREAD_ID NULL
|
||||
|
||||
#define BH_WAIT_FOREVER K_FOREVER
|
||||
|
||||
typedef uint64_t uint64;
|
||||
typedef int64_t int64;
|
||||
|
||||
typedef struct k_thread korp_thread;
|
||||
typedef korp_thread *korp_tid;
|
||||
typedef struct k_mutex korp_mutex;
|
||||
typedef struct k_sem korp_sem;
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
#define wa_strdup bh_strdup
|
||||
|
||||
struct bh_thread_wait_node;
|
||||
typedef struct bh_thread_wait_node *bh_thread_wait_list;
|
||||
typedef struct korp_cond {
|
||||
struct k_mutex wait_list_lock;
|
||||
bh_thread_wait_list thread_wait_list;
|
||||
} korp_cond;
|
||||
|
||||
typedef void* (*thread_start_routine_t)(void*);
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
|
||||
#define bh_printf printf
|
||||
|
||||
/* Unit test framework is based on C++, where the declaration of
|
||||
snprintf is different. */
|
||||
#ifndef __cplusplus
|
||||
int snprintf(char *buffer, size_t count, const char *format, ...);
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void*)0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Return the offset of the given field in the given type.
|
||||
*
|
||||
* @param Type the type containing the filed
|
||||
* @param field the field in the type
|
||||
*
|
||||
* @return the offset of field in Type
|
||||
*/
|
||||
#ifndef offsetof
|
||||
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
|
||||
#endif
|
||||
|
||||
#define bh_assert(x) \
|
||||
do { \
|
||||
if (!(x)) { \
|
||||
printk("bh_assert(%s, %d)\n", __func__, __LINE__);\
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
|
||||
unsigned int n);
|
||||
int b_strcat_s(char * s1, size_t s1max, const char * s2);
|
||||
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
|
||||
|
||||
/* math functions */
|
||||
double sqrt(double x);
|
||||
double floor(double x);
|
||||
double ceil(double x);
|
||||
double fmin(double x, double y);
|
||||
double fmax(double x, double y);
|
||||
double rint(double x);
|
||||
double fabs(double x);
|
||||
double trunc(double x);
|
||||
float floorf(float x);
|
||||
float ceilf(float x);
|
||||
float fminf(float x, float y);
|
||||
float fmaxf(float x, float y);
|
||||
float rintf(float x);
|
||||
float truncf(float x);
|
||||
int signbit(double x);
|
||||
int isnan(double x);
|
||||
|
||||
unsigned long long int strtoull(const char *nptr, char **endptr,
|
||||
int base);
|
||||
double strtod(const char *nptr, char **endptr);
|
||||
float strtof(const char *nptr, char **endptr);
|
||||
|
||||
int bh_platform_init();
|
||||
|
||||
/* MMAP mode */
|
||||
enum {
|
||||
MMAP_PROT_NONE = 0,
|
||||
MMAP_PROT_READ = 1,
|
||||
MMAP_PROT_WRITE = 2,
|
||||
MMAP_PROT_EXEC = 4
|
||||
};
|
||||
|
||||
/* MMAP flags */
|
||||
enum {
|
||||
MMAP_MAP_NONE = 0,
|
||||
/* Put the mapping into 0 to 2 G, supported only on x86_64 */
|
||||
MMAP_MAP_32BIT = 1,
|
||||
/* Don't interpret addr as a hint: place the mapping at exactly
|
||||
that address. */
|
||||
MMAP_MAP_FIXED = 2
|
||||
};
|
||||
|
||||
void *bh_mmap(void *hint, unsigned int size, int prot, int flags);
|
||||
void bh_munmap(void *addr, uint32 size);
|
||||
int bh_mprotect(void *addr, uint32 size, int prot);
|
||||
|
||||
#endif
|
||||
57
core/shared/platform/zephyr/bh_platform_log.c
Normal file
57
core/shared/platform/zephyr/bh_platform_log.c
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdio.h>
|
||||
|
||||
struct out_context {
|
||||
int count;
|
||||
};
|
||||
|
||||
typedef int (*out_func_t)(int c, void *ctx);
|
||||
|
||||
extern void *__printk_get_hook(void);
|
||||
static int (*_char_out)(int) = NULL;
|
||||
|
||||
static int char_out(int c, struct out_context *ctx)
|
||||
{
|
||||
ctx->count++;
|
||||
if (_char_out == NULL) {
|
||||
_char_out = __printk_get_hook();
|
||||
}
|
||||
return _char_out(c);
|
||||
}
|
||||
|
||||
static int bh_vprintk(const char *fmt, va_list ap)
|
||||
{
|
||||
struct out_context ctx = { 0 };
|
||||
z_vprintk((out_func_t) char_out, &ctx, fmt, ap);
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
void bh_log_emit(const char *fmt, va_list ap)
|
||||
{
|
||||
bh_vprintk(fmt, ap);
|
||||
}
|
||||
|
||||
int bh_fprintf(FILE *stream, const char *fmt, ...)
|
||||
{
|
||||
(void) stream;
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = bh_vprintk(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bh_fflush(void *stream)
|
||||
{
|
||||
(void) stream;
|
||||
return 0;
|
||||
}
|
||||
|
||||
528
core/shared/platform/zephyr/bh_thread.c
Executable file
528
core/shared/platform/zephyr/bh_thread.c
Executable file
@ -0,0 +1,528 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct bh_thread_wait_node {
|
||||
struct k_sem sem;
|
||||
bh_thread_wait_list next;
|
||||
} bh_thread_wait_node;
|
||||
|
||||
typedef struct bh_thread_data {
|
||||
/* Next thread data */
|
||||
struct bh_thread_data *next;
|
||||
/* Zephyr thread handle */
|
||||
korp_tid tid;
|
||||
/* Jeff thread local root */
|
||||
void *tlr;
|
||||
/* Lock for waiting list */
|
||||
struct k_mutex wait_list_lock;
|
||||
/* Waiting list of other threads who are joining this thread */
|
||||
bh_thread_wait_list thread_wait_list;
|
||||
/* Thread stack size */
|
||||
unsigned stack_size;
|
||||
/* Thread stack */
|
||||
char stack[1];
|
||||
} bh_thread_data;
|
||||
|
||||
typedef struct bh_thread_obj {
|
||||
struct k_thread thread;
|
||||
/* Whether the thread is terminated and this thread object is to
|
||||
be freed in the future. */
|
||||
bool to_be_freed;
|
||||
struct bh_thread_obj *next;
|
||||
} bh_thread_obj;
|
||||
|
||||
static bool is_thread_sys_inited = false;
|
||||
|
||||
/* Thread data of supervisor thread */
|
||||
static bh_thread_data supervisor_thread_data;
|
||||
|
||||
/* Lock for thread data list */
|
||||
static struct k_mutex thread_data_lock;
|
||||
|
||||
/* Thread data list */
|
||||
static bh_thread_data *thread_data_list = NULL;
|
||||
|
||||
/* Lock for thread object list */
|
||||
static struct k_mutex thread_obj_lock;
|
||||
|
||||
/* Thread object list */
|
||||
static bh_thread_obj *thread_obj_list = NULL;
|
||||
|
||||
static void thread_data_list_add(bh_thread_data *thread_data)
|
||||
{
|
||||
k_mutex_lock(&thread_data_lock, K_FOREVER);
|
||||
if (!thread_data_list)
|
||||
thread_data_list = thread_data;
|
||||
else {
|
||||
/* If already in list, just return */
|
||||
bh_thread_data *p = thread_data_list;
|
||||
while (p) {
|
||||
if (p == thread_data) {
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
return;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
/* Set as head of list */
|
||||
thread_data->next = thread_data_list;
|
||||
thread_data_list = thread_data;
|
||||
}
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
}
|
||||
|
||||
static void thread_data_list_remove(bh_thread_data *thread_data)
|
||||
{
|
||||
k_mutex_lock(&thread_data_lock, K_FOREVER);
|
||||
if (thread_data_list) {
|
||||
if (thread_data_list == thread_data)
|
||||
thread_data_list = thread_data_list->next;
|
||||
else {
|
||||
/* Search and remove it from list */
|
||||
bh_thread_data *p = thread_data_list;
|
||||
while (p && p->next != thread_data)
|
||||
p = p->next;
|
||||
if (p && p->next == thread_data)
|
||||
p->next = p->next->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
}
|
||||
|
||||
static bh_thread_data *
|
||||
thread_data_list_lookup(k_tid_t tid)
|
||||
{
|
||||
k_mutex_lock(&thread_data_lock, K_FOREVER);
|
||||
if (thread_data_list) {
|
||||
bh_thread_data *p = thread_data_list;
|
||||
while (p) {
|
||||
if (p->tid == tid) {
|
||||
/* Found */
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
return p;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void thread_obj_list_add(bh_thread_obj *thread_obj)
|
||||
{
|
||||
k_mutex_lock(&thread_obj_lock, K_FOREVER);
|
||||
if (!thread_obj_list)
|
||||
thread_obj_list = thread_obj;
|
||||
else {
|
||||
/* Set as head of list */
|
||||
thread_obj->next = thread_obj_list;
|
||||
thread_obj_list = thread_obj;
|
||||
}
|
||||
k_mutex_unlock(&thread_obj_lock);
|
||||
}
|
||||
|
||||
static void thread_obj_list_reclaim()
|
||||
{
|
||||
bh_thread_obj *p, *p_prev;
|
||||
k_mutex_lock(&thread_obj_lock, K_FOREVER);
|
||||
p_prev = NULL;
|
||||
p = thread_obj_list;
|
||||
while (p) {
|
||||
if (p->to_be_freed) {
|
||||
if (p_prev == NULL) { /* p is the head of list */
|
||||
thread_obj_list = p->next;
|
||||
bh_free(p);
|
||||
p = thread_obj_list;
|
||||
} else { /* p is not the head of list */
|
||||
p_prev->next = p->next;
|
||||
bh_free(p);
|
||||
p = p_prev->next;
|
||||
}
|
||||
} else {
|
||||
p_prev = p;
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&thread_obj_lock);
|
||||
}
|
||||
|
||||
int _vm_thread_sys_init()
|
||||
{
|
||||
if (is_thread_sys_inited)
|
||||
return BHT_OK;
|
||||
|
||||
k_mutex_init(&thread_data_lock);
|
||||
k_mutex_init(&thread_obj_lock);
|
||||
|
||||
/* Initialize supervisor thread data */
|
||||
memset(&supervisor_thread_data, 0, sizeof(supervisor_thread_data));
|
||||
supervisor_thread_data.tid = k_current_get();
|
||||
/* Set as head of thread data list */
|
||||
thread_data_list = &supervisor_thread_data;
|
||||
|
||||
is_thread_sys_inited = true;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void vm_thread_sys_destroy(void)
|
||||
{
|
||||
if (is_thread_sys_inited) {
|
||||
is_thread_sys_inited = false;
|
||||
}
|
||||
}
|
||||
|
||||
static bh_thread_data *
|
||||
thread_data_current()
|
||||
{
|
||||
k_tid_t tid = k_current_get();
|
||||
return thread_data_list_lookup(tid);
|
||||
}
|
||||
|
||||
static void vm_thread_cleanup(void)
|
||||
{
|
||||
bh_thread_data *thread_data = thread_data_current();
|
||||
|
||||
bh_assert(thread_data != NULL);
|
||||
k_mutex_lock(&thread_data->wait_list_lock, K_FOREVER);
|
||||
if (thread_data->thread_wait_list) {
|
||||
/* Signal each joining thread */
|
||||
bh_thread_wait_list head = thread_data->thread_wait_list;
|
||||
while (head) {
|
||||
bh_thread_wait_list next = head->next;
|
||||
k_sem_give(&head->sem);
|
||||
/* head will be freed by joining thread */
|
||||
head = next;
|
||||
}
|
||||
thread_data->thread_wait_list = NULL;
|
||||
}
|
||||
k_mutex_unlock(&thread_data->wait_list_lock);
|
||||
|
||||
thread_data_list_remove(thread_data);
|
||||
/* Set flag to true for the next thread creating to
|
||||
free the thread object */
|
||||
((bh_thread_obj*) thread_data->tid)->to_be_freed = true;
|
||||
bh_free(thread_data);
|
||||
}
|
||||
|
||||
static void vm_thread_wrapper(void *start, void *arg, void *thread_data)
|
||||
{
|
||||
/* Set thread custom data */
|
||||
((bh_thread_data*) thread_data)->tid = k_current_get();
|
||||
thread_data_list_add(thread_data);
|
||||
|
||||
((thread_start_routine_t) start)(arg);
|
||||
vm_thread_cleanup();
|
||||
}
|
||||
|
||||
int _vm_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
|
||||
unsigned int stack_size)
|
||||
{
|
||||
return _vm_thread_create_with_prio(p_tid, start, arg, stack_size,
|
||||
BH_THREAD_DEFAULT_PRIORITY);
|
||||
}
|
||||
|
||||
int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
|
||||
void *arg, unsigned int stack_size, int prio)
|
||||
{
|
||||
korp_tid tid;
|
||||
bh_thread_data *thread_data;
|
||||
unsigned thread_data_size;
|
||||
|
||||
if (!p_tid || !stack_size)
|
||||
return BHT_ERROR;
|
||||
|
||||
/* Free the thread objects of terminated threads */
|
||||
thread_obj_list_reclaim();
|
||||
|
||||
/* Create and initialize thread object */
|
||||
if (!(tid = bh_malloc(sizeof(bh_thread_obj))))
|
||||
return BHT_ERROR;
|
||||
|
||||
memset(tid, 0, sizeof(bh_thread_obj));
|
||||
|
||||
/* Create and initialize thread data */
|
||||
thread_data_size = offsetof(bh_thread_data, stack) + stack_size;
|
||||
if (!(thread_data = bh_malloc(thread_data_size))) {
|
||||
bh_free(tid);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
memset(thread_data, 0, thread_data_size);
|
||||
k_mutex_init(&thread_data->wait_list_lock);
|
||||
thread_data->stack_size = stack_size;
|
||||
thread_data->tid = tid;
|
||||
|
||||
/* Create the thread */
|
||||
if (!((tid = k_thread_create(tid, (k_thread_stack_t *) thread_data->stack,
|
||||
stack_size, vm_thread_wrapper, start, arg, thread_data, prio, 0,
|
||||
K_NO_WAIT)))) {
|
||||
bh_free(tid);
|
||||
bh_free(thread_data);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
bh_assert(tid == thread_data->tid);
|
||||
|
||||
/* Set thread custom data */
|
||||
thread_data_list_add(thread_data);
|
||||
thread_obj_list_add((bh_thread_obj*) tid);
|
||||
*p_tid = tid;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
korp_tid _vm_self_thread()
|
||||
{
|
||||
return (korp_tid) k_current_get();
|
||||
}
|
||||
|
||||
void vm_thread_exit(void * code)
|
||||
{
|
||||
(void) code;
|
||||
korp_tid self = vm_self_thread();
|
||||
vm_thread_cleanup();
|
||||
k_thread_abort((k_tid_t) self);
|
||||
}
|
||||
|
||||
int _vm_thread_cancel(korp_tid thread)
|
||||
{
|
||||
k_thread_abort((k_tid_t) thread);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
|
||||
{
|
||||
(void) value_ptr;
|
||||
bh_thread_data *thread_data;
|
||||
bh_thread_wait_node *node;
|
||||
|
||||
/* Create wait node and append it to wait list */
|
||||
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
|
||||
return BHT_ERROR;
|
||||
|
||||
k_sem_init(&node->sem, 0, 1);
|
||||
node->next = NULL;
|
||||
|
||||
/* Get thread data */
|
||||
thread_data = thread_data_list_lookup(thread);
|
||||
bh_assert(thread_data != NULL);
|
||||
|
||||
k_mutex_lock(&thread_data->wait_list_lock, K_FOREVER);
|
||||
if (!thread_data->thread_wait_list)
|
||||
thread_data->thread_wait_list = node;
|
||||
else {
|
||||
/* Add to end of waiting list */
|
||||
bh_thread_wait_node *p = thread_data->thread_wait_list;
|
||||
while (p->next)
|
||||
p = p->next;
|
||||
p->next = node;
|
||||
}
|
||||
k_mutex_unlock(&thread_data->wait_list_lock);
|
||||
|
||||
/* Wait the sem */
|
||||
k_sem_take(&node->sem, mills);
|
||||
|
||||
/* Wait some time for the thread to be actually terminated */
|
||||
k_sleep(100);
|
||||
|
||||
/* Destroy resource */
|
||||
bh_free(node);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_thread_detach(korp_tid thread)
|
||||
{
|
||||
(void) thread;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void *_vm_tls_get(unsigned idx)
|
||||
{
|
||||
(void) idx;
|
||||
bh_thread_data *thread_data;
|
||||
|
||||
bh_assert(idx == 0);
|
||||
thread_data = thread_data_current();
|
||||
|
||||
return thread_data ? thread_data->tlr : NULL;
|
||||
}
|
||||
|
||||
int _vm_tls_put(unsigned idx, void * tls)
|
||||
{
|
||||
bh_thread_data *thread_data;
|
||||
|
||||
(void) idx;
|
||||
bh_assert(idx == 0);
|
||||
thread_data = thread_data_current();
|
||||
bh_assert(thread_data != NULL);
|
||||
|
||||
thread_data->tlr = tls;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
(void) mutex;
|
||||
k_mutex_init(mutex);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_recursive_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
k_mutex_init(mutex);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_destroy(korp_mutex *mutex)
|
||||
{
|
||||
(void) mutex;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void vm_mutex_lock(korp_mutex *mutex)
|
||||
{
|
||||
k_mutex_lock(mutex, K_FOREVER);
|
||||
}
|
||||
|
||||
int vm_mutex_trylock(korp_mutex *mutex)
|
||||
{
|
||||
return k_mutex_lock(mutex, K_NO_WAIT);
|
||||
}
|
||||
|
||||
void vm_mutex_unlock(korp_mutex *mutex)
|
||||
{
|
||||
k_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
int _vm_sem_init(korp_sem* sem, unsigned int c)
|
||||
{
|
||||
k_sem_init(sem, 0, c);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_destroy(korp_sem *sem)
|
||||
{
|
||||
(void) sem;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_wait(korp_sem *sem)
|
||||
{
|
||||
return k_sem_take(sem, K_FOREVER);
|
||||
}
|
||||
|
||||
int _vm_sem_reltimedwait(korp_sem *sem, int mills)
|
||||
{
|
||||
return k_sem_take(sem, mills);
|
||||
}
|
||||
|
||||
int _vm_sem_post(korp_sem *sem)
|
||||
{
|
||||
k_sem_give(sem);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_init(korp_cond *cond)
|
||||
{
|
||||
k_mutex_init(&cond->wait_list_lock);
|
||||
cond->thread_wait_list = NULL;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_destroy(korp_cond *cond)
|
||||
{
|
||||
(void) cond;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
|
||||
bool timed, int mills)
|
||||
{
|
||||
bh_thread_wait_node *node;
|
||||
|
||||
/* Create wait node and append it to wait list */
|
||||
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
|
||||
return BHT_ERROR;
|
||||
|
||||
k_sem_init(&node->sem, 0, 1);
|
||||
node->next = NULL;
|
||||
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (!cond->thread_wait_list)
|
||||
cond->thread_wait_list = node;
|
||||
else {
|
||||
/* Add to end of wait list */
|
||||
bh_thread_wait_node *p = cond->thread_wait_list;
|
||||
while (p->next)
|
||||
p = p->next;
|
||||
p->next = node;
|
||||
}
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
/* Unlock mutex, wait sem and lock mutex again */
|
||||
k_mutex_unlock(mutex);
|
||||
k_sem_take(&node->sem, timed ? mills : K_FOREVER);
|
||||
k_mutex_lock(mutex, K_FOREVER);
|
||||
|
||||
/* Remove wait node from wait list */
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (cond->thread_wait_list == node)
|
||||
cond->thread_wait_list = node->next;
|
||||
else {
|
||||
/* Remove from the wait list */
|
||||
bh_thread_wait_node *p = cond->thread_wait_list;
|
||||
while (p->next != node)
|
||||
p = p->next;
|
||||
p->next = node->next;
|
||||
}
|
||||
bh_free(node);
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
||||
{
|
||||
return _vm_cond_wait_internal(cond, mutex, false, 0);
|
||||
}
|
||||
|
||||
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
|
||||
{
|
||||
return _vm_cond_wait_internal(cond, mutex, true, mills);
|
||||
}
|
||||
|
||||
int _vm_cond_signal(korp_cond *cond)
|
||||
{
|
||||
/* Signal the head wait node of wait list */
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (cond->thread_wait_list)
|
||||
k_sem_give(&cond->thread_wait_list->sem);
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_broadcast(korp_cond *cond)
|
||||
{
|
||||
/* Signal each wait node of wait list */
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (cond->thread_wait_list) {
|
||||
bh_thread_wait_node *p = cond->thread_wait_list;
|
||||
while (p) {
|
||||
k_sem_give(&p->sem);
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
46
core/shared/platform/zephyr/bh_time.c
Executable file
46
core/shared/platform/zephyr/bh_time.c
Executable file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_time.h"
|
||||
|
||||
/*
|
||||
* This function returns milliseconds per tick.
|
||||
* @return milliseconds per tick.
|
||||
*/
|
||||
uint64 _bh_time_get_tick_millisecond()
|
||||
{
|
||||
return k_uptime_get_32();
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns milliseconds after boot.
|
||||
* @return milliseconds after boot.
|
||||
*/
|
||||
uint64 _bh_time_get_boot_millisecond()
|
||||
{
|
||||
return k_uptime_get_32();
|
||||
}
|
||||
|
||||
uint64 _bh_time_get_millisecond_from_1970()
|
||||
{
|
||||
return k_uptime_get();
|
||||
}
|
||||
|
||||
size_t _bh_time_strftime(char *str, size_t max, const char *format, int64 time)
|
||||
{
|
||||
(void) format;
|
||||
(void) time;
|
||||
uint32 t = k_uptime_get_32();
|
||||
int h, m, s;
|
||||
|
||||
t = t % (24 * 60 * 60);
|
||||
h = t / (60 * 60);
|
||||
t = t % (60 * 60);
|
||||
m = t / 60;
|
||||
s = t % 60;
|
||||
|
||||
return snprintf(str, max, "%02d:%02d:%02d", h, m, s);
|
||||
}
|
||||
|
||||
13
core/shared/platform/zephyr/shared_platform.cmake
Normal file
13
core/shared/platform/zephyr/shared_platform.cmake
Normal file
@ -0,0 +1,13 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${PLATFORM_SHARED_DIR})
|
||||
include_directories(${PLATFORM_SHARED_DIR}/../include)
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
|
||||
|
||||
set (PLATFORM_SHARED_SOURCE ${source_all})
|
||||
|
||||
Reference in New Issue
Block a user