2015-05-18 22:37:29 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Intel Corporation.
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-05-18 22:37:29 -04:00
|
|
|
*/
|
|
|
|
|
2017-02-10 21:06:55 -08:00
|
|
|
#include <arch/cpu.h>
|
2015-05-18 08:57:17 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
2017-06-17 11:30:47 -04:00
|
|
|
#include <linker/linker-defs.h>
|
2016-02-22 19:47:06 -08:00
|
|
|
#include <misc/util.h>
|
2018-05-09 16:36:44 -07:00
|
|
|
#include <kernel_internal.h>
|
2018-07-19 11:09:33 -07:00
|
|
|
#include <misc/errno_private.h>
|
2018-03-26 17:07:33 -07:00
|
|
|
#include <misc/libc-hooks.h>
|
|
|
|
#include <syscall_handler.h>
|
2016-02-22 19:47:06 -08:00
|
|
|
|
|
|
|
#define USED_RAM_END_ADDR POINTER_TO_UINT(&_end)
|
2017-02-10 16:30:21 -06:00
|
|
|
|
2018-05-09 16:36:44 -07:00
|
|
|
#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
|
|
|
|
/* Compiler will throw an error if the provided value isn't a power of two */
|
|
|
|
static unsigned char __kernel __aligned(CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE)
|
|
|
|
heap_base[CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE];
|
|
|
|
#define MAX_HEAP_SIZE CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
|
|
|
|
#else
|
|
|
|
|
2016-05-06 10:59:10 -07:00
|
|
|
#if CONFIG_X86
|
2016-02-22 19:47:06 -08:00
|
|
|
#define USED_RAM_SIZE (USED_RAM_END_ADDR - CONFIG_PHYS_RAM_ADDR)
|
|
|
|
#define MAX_HEAP_SIZE ((KB(CONFIG_RAM_SIZE)) - USED_RAM_SIZE)
|
2016-06-07 13:53:13 -07:00
|
|
|
#elif CONFIG_NIOS2
|
|
|
|
#include <layout.h>
|
|
|
|
#define USED_RAM_SIZE (USED_RAM_END_ADDR - _RAM_ADDR)
|
|
|
|
#define MAX_HEAP_SIZE (_RAM_SIZE - USED_RAM_SIZE)
|
2017-01-11 00:24:31 +01:00
|
|
|
#elif CONFIG_RISCV32
|
|
|
|
#include <soc.h>
|
|
|
|
#define USED_RAM_SIZE (USED_RAM_END_ADDR - RISCV_RAM_BASE)
|
|
|
|
#define MAX_HEAP_SIZE (RISCV_RAM_SIZE - USED_RAM_SIZE)
|
2017-02-10 21:06:55 -08:00
|
|
|
#elif CONFIG_ARM
|
|
|
|
#include <soc.h>
|
|
|
|
#define USED_RAM_SIZE (USED_RAM_END_ADDR - CONFIG_SRAM_BASE_ADDRESS)
|
|
|
|
#define MAX_HEAP_SIZE ((KB(CONFIG_SRAM_SIZE)) - USED_RAM_SIZE)
|
2017-02-07 14:27:34 +01:00
|
|
|
#elif CONFIG_XTENSA
|
2017-08-10 16:23:03 +08:00
|
|
|
extern void *_heap_sentry;
|
|
|
|
#define MAX_HEAP_SIZE (POINTER_TO_UINT(&_heap_sentry) - USED_RAM_END_ADDR)
|
2016-05-06 10:59:10 -07:00
|
|
|
#else
|
|
|
|
#define USED_RAM_SIZE (USED_RAM_END_ADDR - CONFIG_SRAM_BASE_ADDRESS)
|
|
|
|
#define MAX_HEAP_SIZE ((KB(CONFIG_SRAM_SIZE)) - USED_RAM_SIZE)
|
2016-02-22 19:47:06 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static unsigned char *heap_base = UINT_TO_POINTER(USED_RAM_END_ADDR);
|
2018-05-09 16:36:44 -07:00
|
|
|
#endif /* CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE */
|
|
|
|
|
2016-02-22 19:47:06 -08:00
|
|
|
static unsigned int heap_sz;
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
static int _stdout_hook_default(int c)
|
|
|
|
{
|
|
|
|
(void)(c); /* Prevent warning about unused argument */
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
return EOF;
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int (*_stdout_hook)(int) = _stdout_hook_default;
|
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
void __stdout_hook_install(int (*hook)(int))
|
|
|
|
{
|
|
|
|
_stdout_hook = hook;
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
static unsigned char _stdin_hook_default(void)
|
|
|
|
{
|
|
|
|
return 0;
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char (*_stdin_hook)(void) = _stdin_hook_default;
|
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
void __stdin_hook_install(unsigned char (*hook)(void))
|
|
|
|
{
|
|
|
|
_stdin_hook = hook;
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
|
|
|
|
2018-03-26 17:07:33 -07:00
|
|
|
int _impl__zephyr_read(char *buf, int nbytes)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2015-05-18 08:57:17 -04:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nbytes; i++) {
|
|
|
|
*(buf + i) = _stdin_hook();
|
|
|
|
if ((*(buf + i) == '\n') || (*(buf + i) == '\r')) {
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 11:24:35 -04:00
|
|
|
return i;
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
|
|
|
|
2018-03-26 17:07:33 -07:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
Z_SYSCALL_HANDLER(_zephyr_read, buf, nbytes)
|
|
|
|
{
|
|
|
|
Z_OOPS(Z_SYSCALL_MEMORY_WRITE(buf, nbytes));
|
|
|
|
return _impl__zephyr_read((char *)buf, nbytes);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-19 10:36:45 +03:00
|
|
|
int _impl__zephyr_write(const void *buffer, int nbytes)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2018-10-19 10:36:45 +03:00
|
|
|
const char *buf = buffer;
|
2015-05-18 08:57:17 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nbytes; i++) {
|
|
|
|
if (*(buf + i) == '\n') {
|
2015-06-04 11:24:35 -04:00
|
|
|
_stdout_hook('\r');
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
2015-06-04 11:24:35 -04:00
|
|
|
_stdout_hook(*(buf + i));
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
2015-06-04 11:24:35 -04:00
|
|
|
return nbytes;
|
2015-05-18 08:57:17 -04:00
|
|
|
}
|
2018-03-26 17:07:33 -07:00
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
Z_SYSCALL_HANDLER(_zephyr_write, buf, nbytes)
|
|
|
|
{
|
|
|
|
Z_OOPS(Z_SYSCALL_MEMORY_READ(buf, nbytes));
|
2018-10-19 10:36:45 +03:00
|
|
|
return _impl__zephyr_write((const void *)buf, nbytes);
|
2018-03-26 17:07:33 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_POSIX_FS
|
|
|
|
int _read(int fd, char *buf, int nbytes)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(fd);
|
|
|
|
|
|
|
|
return _zephyr_read(buf, nbytes);
|
|
|
|
}
|
|
|
|
FUNC_ALIAS(_read, read, int);
|
|
|
|
|
2018-10-19 10:36:45 +03:00
|
|
|
int _write(int fd, const void *buf, int nbytes)
|
2018-03-26 17:07:33 -07:00
|
|
|
{
|
|
|
|
ARG_UNUSED(fd);
|
|
|
|
|
|
|
|
return _zephyr_write(buf, nbytes);
|
|
|
|
}
|
2016-06-07 14:15:23 -05:00
|
|
|
FUNC_ALIAS(_write, write, int);
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2018-05-03 17:17:22 +05:30
|
|
|
int _open(const char *name, int mode)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
FUNC_ALIAS(_open, open, int);
|
|
|
|
|
|
|
|
int _close(int file)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
FUNC_ALIAS(_close, close, int);
|
|
|
|
|
|
|
|
int _lseek(int file, int ptr, int dir)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
FUNC_ALIAS(_lseek, lseek, int);
|
|
|
|
#else
|
2018-10-19 10:36:45 +03:00
|
|
|
extern ssize_t write(int file, const char *buffer, size_t count);
|
2018-05-03 17:17:22 +05:30
|
|
|
#define _write write
|
|
|
|
#endif
|
|
|
|
|
2016-06-07 14:15:23 -05:00
|
|
|
int _isatty(int file)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2015-05-18 08:57:17 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-07 14:15:23 -05:00
|
|
|
FUNC_ALIAS(_isatty, isatty, int);
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2016-06-07 14:15:23 -05:00
|
|
|
int _kill(int i, int j)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2015-05-18 08:57:17 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-07 14:15:23 -05:00
|
|
|
FUNC_ALIAS(_kill, kill, int);
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2016-06-07 14:15:23 -05:00
|
|
|
int _getpid(void)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2015-05-18 08:57:17 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-07 14:15:23 -05:00
|
|
|
FUNC_ALIAS(_getpid, getpid, int);
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2016-06-07 14:15:23 -05:00
|
|
|
int _fstat(int file, struct stat *st)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2015-05-18 08:57:17 -04:00
|
|
|
st->st_mode = S_IFCHR;
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-07 14:15:23 -05:00
|
|
|
FUNC_ALIAS(_fstat, fstat, int);
|
2015-05-18 08:57:17 -04:00
|
|
|
|
2016-01-15 19:29:21 -02:00
|
|
|
void _exit(int status)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2018-05-05 23:51:48 +03:00
|
|
|
_write(1, "exit\n", 5);
|
2015-05-18 08:57:17 -04:00
|
|
|
while (1) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 14:15:23 -05:00
|
|
|
void *_sbrk(int count)
|
2015-06-04 11:24:35 -04:00
|
|
|
{
|
2016-02-22 19:47:06 -08:00
|
|
|
void *ptr = heap_base + heap_sz;
|
2015-06-04 11:24:35 -04:00
|
|
|
|
2016-02-22 19:47:06 -08:00
|
|
|
if ((heap_sz + count) < MAX_HEAP_SIZE) {
|
2015-05-18 08:57:17 -04:00
|
|
|
heap_sz += count;
|
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
return (void *)-1;
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 14:15:23 -05:00
|
|
|
FUNC_ALIAS(_sbrk, sbrk, void *);
|
2018-05-09 15:59:32 -07:00
|
|
|
|
2018-05-09 16:36:44 -07:00
|
|
|
void z_newlib_get_heap_bounds(void **base, size_t *size)
|
2018-05-09 15:59:32 -07:00
|
|
|
{
|
2018-05-09 16:36:44 -07:00
|
|
|
*base = heap_base;
|
|
|
|
*size = MAX_HEAP_SIZE;
|
2018-05-09 15:59:32 -07:00
|
|
|
}
|
2018-07-19 11:09:33 -07:00
|
|
|
|
|
|
|
int *__errno(void)
|
|
|
|
{
|
|
|
|
return z_errno();
|
|
|
|
}
|