2015-06-15 06:51:06 +00:00
|
|
|
/* newlib_syscalls.c - newlib syscalls for ESP8266
|
|
|
|
*
|
|
|
|
* Part of esp-open-rtos
|
|
|
|
* Copyright (C) 2105 Superhouse Automation Pty Ltd
|
|
|
|
* BSD Licensed as described in the file LICENSE
|
|
|
|
*/
|
|
|
|
#include <sys/reent.h>
|
|
|
|
#include <sys/types.h>
|
2015-09-03 01:14:44 +00:00
|
|
|
#include <sys/errno.h>
|
2015-06-15 06:51:06 +00:00
|
|
|
#include <espressif/sdk_private.h>
|
2015-07-29 00:34:09 +00:00
|
|
|
#include <common_macros.h>
|
2016-02-16 11:00:29 +00:00
|
|
|
#include <xtensa_ops.h>
|
2015-10-06 04:51:57 +00:00
|
|
|
#include <esp/uart.h>
|
2015-06-15 06:51:06 +00:00
|
|
|
#include <stdlib.h>
|
2016-12-19 15:55:26 +00:00
|
|
|
#include <stdout_redirect.h>
|
2017-04-13 03:52:05 +00:00
|
|
|
#include <sys/time.h>
|
2017-10-18 17:33:32 +00:00
|
|
|
#include <lwip/sockets.h>
|
2017-12-08 13:45:57 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <semphr.h>
|
|
|
|
#include <esp/hwrand.h>
|
2017-10-18 17:33:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The file descriptor index space is allocated in blocks. The first block of 3
|
|
|
|
* is for newlib I/O the stdin stdout and stderr. The next block of
|
|
|
|
* MEMP_NUM_NETCONN is allocated for lwip sockets, and the remainer to file
|
|
|
|
* system descriptors. The newlib default FD_SETSIZE is 64.
|
|
|
|
*/
|
|
|
|
#if LWIP_SOCKET_OFFSET < 3
|
|
|
|
#error Expecting a LWIP_SOCKET_OFFSET >= 3, to allow room for the standard I/O descriptors.
|
|
|
|
#endif
|
|
|
|
#define FILE_DESCRIPTOR_OFFSET (LWIP_SOCKET_OFFSET + MEMP_NUM_NETCONN)
|
|
|
|
#if FILE_DESCRIPTOR_OFFSET > FD_SETSIZE
|
|
|
|
#error Too many lwip sockets for the FD_SETSIZE.
|
|
|
|
#endif
|
2015-06-15 06:51:06 +00:00
|
|
|
|
2018-03-01 04:18:39 +00:00
|
|
|
void *_sbrk_r (struct _reent *r, ptrdiff_t incr)
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
2018-03-01 04:18:39 +00:00
|
|
|
r->_errno = ENOMEM;
|
|
|
|
return (caddr_t)-1;
|
|
|
|
}
|
2016-02-16 11:00:29 +00:00
|
|
|
|
2018-03-01 04:18:39 +00:00
|
|
|
/* If there is a restriction on the dram usage then skip this chunk if in dram,
|
|
|
|
* and if there is a restriction on the iram usage then skip this chunk if in
|
|
|
|
* iram */
|
|
|
|
IRAM int _malloc_region_masked(void *r, unsigned int mask)
|
|
|
|
{
|
|
|
|
if ( ((mask & 1) && (uint32_t)r < 0x40000000) ||
|
|
|
|
((mask & 2) && (uint32_t)r >= 0x40100000) ) {
|
|
|
|
return 1;
|
2016-02-16 11:00:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 04:18:39 +00:00
|
|
|
return 0;
|
2015-06-15 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 04:18:39 +00:00
|
|
|
uint32_t set_malloc_regions(uint32_t mask)
|
|
|
|
{
|
|
|
|
uint32_t malloc_mask = _REENT->malloc_region_mask;
|
|
|
|
_REENT->malloc_region_mask = mask;
|
|
|
|
return malloc_mask;
|
2017-12-16 02:42:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 04:51:57 +00:00
|
|
|
/* syscall implementation for stdio write to UART */
|
2017-12-08 13:45:57 +00:00
|
|
|
__attribute__((weak)) ssize_t _write_stdout_r(struct _reent *r, int fd, const void *ptr, size_t len )
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
2015-10-06 04:51:57 +00:00
|
|
|
for(int i = 0; i < len; i++) {
|
2015-10-06 12:24:40 +00:00
|
|
|
/* Auto convert CR to CRLF, ignore other LFs (compatible with Espressif SDK behaviour) */
|
2017-12-08 13:45:57 +00:00
|
|
|
if(((char *)ptr)[i] == '\r')
|
2015-10-06 12:24:40 +00:00
|
|
|
continue;
|
2017-12-08 13:45:57 +00:00
|
|
|
if(((char *)ptr)[i] == '\n')
|
2015-10-06 12:24:40 +00:00
|
|
|
uart_putc(0, '\r');
|
2017-12-08 13:45:57 +00:00
|
|
|
uart_putc(0, ((char *)ptr)[i]);
|
2015-10-06 04:51:57 +00:00
|
|
|
}
|
2015-06-15 06:51:06 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-12-19 15:55:26 +00:00
|
|
|
static _WriteFunction *current_stdout_write_r = &_write_stdout_r;
|
|
|
|
|
|
|
|
void set_write_stdout(_WriteFunction *f)
|
|
|
|
{
|
|
|
|
if (f != NULL) {
|
|
|
|
current_stdout_write_r = f;
|
|
|
|
} else {
|
|
|
|
current_stdout_write_r = &_write_stdout_r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_WriteFunction *get_write_stdout()
|
|
|
|
{
|
|
|
|
return current_stdout_write_r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* default implementation, replace in a filesystem */
|
2017-12-08 13:45:57 +00:00
|
|
|
__attribute__((weak)) ssize_t _write_filesystem_r(struct _reent *r, int fd, const void *ptr, size_t len)
|
2016-12-19 15:55:26 +00:00
|
|
|
{
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:45:57 +00:00
|
|
|
__attribute__((weak)) ssize_t _write_r(struct _reent *r, int fd, const void *ptr, size_t len)
|
2016-12-19 15:55:26 +00:00
|
|
|
{
|
2017-10-18 17:33:32 +00:00
|
|
|
if (fd >= FILE_DESCRIPTOR_OFFSET) {
|
2016-12-19 15:55:26 +00:00
|
|
|
return _write_filesystem_r(r, fd, ptr, len);
|
|
|
|
}
|
2017-10-18 17:33:32 +00:00
|
|
|
if (fd >= LWIP_SOCKET_OFFSET) {
|
|
|
|
return lwip_write(fd, ptr, len);
|
|
|
|
}
|
|
|
|
if (fd == r->_stdout->_file) {
|
2017-10-23 21:55:11 +00:00
|
|
|
return current_stdout_write_r(r, fd, ptr, len);
|
2017-10-18 17:33:32 +00:00
|
|
|
}
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
2016-12-19 15:55:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 04:51:57 +00:00
|
|
|
/* syscall implementation for stdio read from UART */
|
2017-12-08 13:45:57 +00:00
|
|
|
__attribute__((weak)) ssize_t _read_stdin_r(struct _reent *r, int fd, void *ptr, size_t len)
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
2015-10-06 04:51:57 +00:00
|
|
|
int ch, i;
|
|
|
|
uart_rxfifo_wait(0, 1);
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
ch = uart_getc_nowait(0);
|
|
|
|
if (ch < 0) break;
|
2017-12-08 13:45:57 +00:00
|
|
|
((char *)ptr)[i] = ch;
|
2015-09-03 01:14:44 +00:00
|
|
|
}
|
2015-10-06 04:51:57 +00:00
|
|
|
return i;
|
2015-06-15 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 15:55:26 +00:00
|
|
|
/* default implementation, replace in a filesystem */
|
2017-12-08 13:45:57 +00:00
|
|
|
__attribute__((weak)) ssize_t _read_filesystem_r( struct _reent *r, int fd, void *ptr, size_t len )
|
2016-12-19 15:55:26 +00:00
|
|
|
{
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:45:57 +00:00
|
|
|
__attribute__((weak)) ssize_t _read_r( struct _reent *r, int fd, void *ptr, size_t len )
|
2016-11-01 15:14:34 +00:00
|
|
|
{
|
2017-10-18 17:33:32 +00:00
|
|
|
if (fd >= FILE_DESCRIPTOR_OFFSET) {
|
2016-12-19 15:55:26 +00:00
|
|
|
return _read_filesystem_r(r, fd, ptr, len);
|
2016-11-01 15:14:34 +00:00
|
|
|
}
|
2017-10-18 17:33:32 +00:00
|
|
|
if (fd >= LWIP_SOCKET_OFFSET) {
|
|
|
|
return lwip_read(fd, ptr, len);
|
|
|
|
}
|
|
|
|
if (fd == r->_stdin->_file) {
|
2017-10-23 21:55:11 +00:00
|
|
|
return _read_stdin_r(r, fd, ptr, len);
|
2017-10-18 17:33:32 +00:00
|
|
|
}
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* default implementation, replace in a filesystem */
|
|
|
|
__attribute__((weak)) int _close_filesystem_r(struct _reent *r, int fd)
|
|
|
|
{
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((weak)) int _close_r(struct _reent *r, int fd)
|
|
|
|
{
|
|
|
|
if (fd >= FILE_DESCRIPTOR_OFFSET) {
|
|
|
|
return _close_filesystem_r(r, fd);
|
|
|
|
}
|
|
|
|
if (fd >= LWIP_SOCKET_OFFSET) {
|
|
|
|
return lwip_close(fd);
|
|
|
|
}
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
2016-11-01 15:14:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 01:15:05 +00:00
|
|
|
/* Stub syscall implementations follow, to allow compiling newlib functions that
|
|
|
|
pull these in via various codepaths
|
|
|
|
*/
|
2016-06-30 14:38:05 +00:00
|
|
|
__attribute__((weak, alias("syscall_returns_enosys")))
|
|
|
|
int _open_r(struct _reent *r, const char *pathname, int flags, int mode);
|
|
|
|
|
|
|
|
__attribute__((weak, alias("syscall_returns_enosys")))
|
|
|
|
int _unlink_r(struct _reent *r, const char *path);
|
|
|
|
|
2016-07-14 13:13:03 +00:00
|
|
|
__attribute__((weak, alias("syscall_returns_enosys")))
|
2017-12-08 13:45:57 +00:00
|
|
|
int _fstat_r(struct _reent *r, int fd, struct stat *buf);
|
2016-06-30 14:38:05 +00:00
|
|
|
|
2016-07-14 13:13:03 +00:00
|
|
|
__attribute__((weak, alias("syscall_returns_enosys")))
|
2017-12-08 13:45:57 +00:00
|
|
|
int _stat_r(struct _reent *r, const char *pathname, struct stat *buf);
|
2016-07-14 13:13:03 +00:00
|
|
|
|
|
|
|
__attribute__((weak, alias("syscall_returns_enosys")))
|
2016-06-30 14:38:05 +00:00
|
|
|
off_t _lseek_r(struct _reent *r, int fd, off_t offset, int whence);
|
2015-06-15 06:51:06 +00:00
|
|
|
|
2017-04-13 03:52:05 +00:00
|
|
|
__attribute__((weak, alias("_gettimeofday_r")))
|
2018-02-16 13:43:18 +00:00
|
|
|
int _gettimeofday_r (struct _reent *ptr, struct timeval *ptimeval, void *ptimezone) {
|
|
|
|
ptimeval->tv_sec = 0;
|
|
|
|
ptimeval->tv_usec = 0;
|
2017-04-13 03:52:05 +00:00
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:15:05 +00:00
|
|
|
/* Generic stub for any newlib syscall that fails with errno ENOSYS
|
|
|
|
("Function not implemented") and a return value equivalent to
|
|
|
|
(int)-1. */
|
|
|
|
static int syscall_returns_enosys(struct _reent *r)
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
2015-09-03 01:15:05 +00:00
|
|
|
r->_errno=ENOSYS;
|
2015-06-15 06:51:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-12-08 13:45:57 +00:00
|
|
|
|
|
|
|
int getentropy(void *ptr, size_t n)
|
|
|
|
{
|
|
|
|
hwrand_fill(ptr, n);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _arc4random_getentropy_fail(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void _exit(int status)
|
|
|
|
{
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Newlib lock implementation. Some newlib locks are statically allocated, but
|
|
|
|
* can not be statically initialized so are set to NULL and initialized at
|
|
|
|
* startup. The malloc lock is used before it can be initialized so there are
|
|
|
|
* runtime checks on the functions that use it early.
|
|
|
|
*/
|
|
|
|
static int locks_initialized = 0;
|
|
|
|
|
|
|
|
extern _lock_t __arc4random_mutex;
|
|
|
|
extern _lock_t __at_quick_exit_mutex;
|
|
|
|
//extern _lock_t __dd_hash_mutex;
|
|
|
|
extern _lock_t __tz_mutex;
|
|
|
|
|
|
|
|
extern _lock_t __atexit_recursive_mutex;
|
|
|
|
extern _lock_t __env_recursive_mutex;
|
|
|
|
extern _lock_t __malloc_recursive_mutex;
|
|
|
|
extern _lock_t __sfp_recursive_mutex;
|
|
|
|
extern _lock_t __sinit_recursive_mutex;
|
|
|
|
|
|
|
|
void init_newlib_locks()
|
|
|
|
{
|
2017-12-27 02:45:11 +00:00
|
|
|
#if 0
|
2018-02-23 11:56:19 +00:00
|
|
|
/* Used a separate mutex for each lock.
|
2017-12-27 02:45:11 +00:00
|
|
|
* Each mutex uses about 96 bytes which adds up. */
|
2018-02-23 11:56:19 +00:00
|
|
|
_lock_init(&__arc4random_mutex);
|
2017-12-08 13:45:57 +00:00
|
|
|
_lock_init(&__at_quick_exit_mutex);
|
|
|
|
//_lock_init(&__dd_hash_mutex);
|
|
|
|
_lock_init(&__tz_mutex);
|
|
|
|
|
|
|
|
_lock_init_recursive(&__atexit_recursive_mutex);
|
|
|
|
_lock_init_recursive(&__env_recursive_mutex);
|
|
|
|
_lock_init_recursive(&__malloc_recursive_mutex);
|
|
|
|
_lock_init_recursive(&__sfp_recursive_mutex);
|
|
|
|
_lock_init_recursive(&__sinit_recursive_mutex);
|
2017-12-27 02:45:11 +00:00
|
|
|
#else
|
2018-02-23 11:56:19 +00:00
|
|
|
/* Reuse one mutex and one recursive mutex for this set, reducing memory
|
|
|
|
* usage. Newlib will still allocate other locks dynamically and some of
|
|
|
|
* those need to be separate such as the file lock where a thread might
|
|
|
|
* block with them held. */
|
|
|
|
_lock_init(&__arc4random_mutex);
|
2017-12-27 02:45:11 +00:00
|
|
|
__at_quick_exit_mutex = __arc4random_mutex;
|
|
|
|
//__dd_hash_mutex = __arc4random_mutex;
|
|
|
|
__tz_mutex = __arc4random_mutex;
|
|
|
|
|
2018-02-23 11:56:19 +00:00
|
|
|
_lock_init_recursive(&__atexit_recursive_mutex);
|
|
|
|
__env_recursive_mutex = __atexit_recursive_mutex;
|
|
|
|
__malloc_recursive_mutex = __atexit_recursive_mutex;
|
|
|
|
__sfp_recursive_mutex = __atexit_recursive_mutex;
|
|
|
|
__sinit_recursive_mutex = __atexit_recursive_mutex;
|
2017-12-27 02:45:11 +00:00
|
|
|
#endif
|
2017-12-08 13:45:57 +00:00
|
|
|
|
|
|
|
locks_initialized = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_init(_lock_t *lock) {
|
|
|
|
*lock = (_lock_t)xSemaphoreCreateMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_init_recursive(_lock_t *lock) {
|
|
|
|
*lock = (_lock_t)xSemaphoreCreateRecursiveMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_close(_lock_t *lock) {
|
|
|
|
vSemaphoreDelete((QueueHandle_t)*lock);
|
|
|
|
*lock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_close_recursive(_lock_t *lock) {
|
|
|
|
vSemaphoreDelete((QueueHandle_t)*lock);
|
|
|
|
*lock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_acquire(_lock_t *lock) {
|
|
|
|
xSemaphoreTake((QueueHandle_t)*lock, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_acquire_recursive(_lock_t *lock) {
|
|
|
|
if (locks_initialized) {
|
2018-03-02 22:47:42 +00:00
|
|
|
if (sdk_NMIIrqIsOn) {
|
|
|
|
uart_putc(0, ':');
|
|
|
|
return;
|
|
|
|
}
|
2017-12-08 13:45:57 +00:00
|
|
|
xSemaphoreTakeRecursive((QueueHandle_t)*lock, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int _lock_try_acquire(_lock_t *lock) {
|
|
|
|
return xSemaphoreTake((QueueHandle_t)*lock, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int _lock_try_acquire_recursive(_lock_t *lock) {
|
|
|
|
return xSemaphoreTakeRecursive((QueueHandle_t)*lock, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_release(_lock_t *lock) {
|
|
|
|
xSemaphoreGive((QueueHandle_t)*lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock_release_recursive(_lock_t *lock) {
|
|
|
|
if (locks_initialized) {
|
2018-03-02 22:47:42 +00:00
|
|
|
if (sdk_NMIIrqIsOn) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-08 13:45:57 +00:00
|
|
|
xSemaphoreGiveRecursive((QueueHandle_t)*lock);
|
|
|
|
}
|
|
|
|
}
|