Zephyr Project API  3.4.0
A Scalable Open Source RTOS
libc-hooks.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_LIBC_HOOKS_H_
8#define ZEPHYR_INCLUDE_SYS_LIBC_HOOKS_H_
9
10#include <zephyr/toolchain.h>
12#include <stdio.h>
13#include <stddef.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19/*
20 * Private header for specifying accessory functions to the C library internals
21 * that need to call into the kernel as system calls
22 */
23
24#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC) || defined(CONFIG_PICOLIBC)
25
26/* syscall generation ignores preprocessor, ensure this is defined to ensure
27 * we don't have compile errors
28 */
29__syscall int zephyr_read_stdin(char *buf, int nbytes);
30
31__syscall int zephyr_write_stdout(const void *buf, int nbytes);
32
33#else
34/* Minimal libc */
35
36__syscall int zephyr_fputc(int c, FILE * stream);
37
38__syscall size_t zephyr_fwrite(const void *ZRESTRICT ptr, size_t size,
39 size_t nitems, FILE *ZRESTRICT stream);
40#endif /* CONFIG_NEWLIB_LIBC */
41
42/* Handle deprecated malloc arena size configuration values */
43#ifdef CONFIG_COMMON_LIBC_MALLOC
44# if defined(CONFIG_MINIMAL_LIBC) && (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE != -2)
45# undef CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE
46# define CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE
47# warning Using deprecated setting CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE
48# elif defined(CONFIG_PICOLIBC) && (CONFIG_PICOLIBC_HEAP_SIZE != -2)
49# undef CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE
50# define CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE CONFIG_PICOLIBC_HEAP_SIZE
51# warning Using deprecated setting CONFIG_PICOLIBC_HEAP_SIZE
52# endif
53#endif
54
55#ifdef CONFIG_USERSPACE
56#ifdef CONFIG_COMMON_LIBC_MALLOC
57
58/* When using the common malloc implementation with CONFIG_USERSPACE, the
59 * heap will be in a separate partition when there's an MPU or MMU
60 * available.
61 */
62#if CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE != 0 && \
63(defined(CONFIG_MPU) || defined(CONFIG_MMU))
64#define Z_MALLOC_PARTITION_EXISTS 1
65#endif
66
67#elif defined(CONFIG_NEWLIB_LIBC)
68/* If we are using newlib, the heap arena is in one of two areas:
69 * - If we have an MPU that requires power of two alignment, the heap bounds
70 * must be specified in Kconfig via CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE.
71 * - Otherwise, the heap arena on most arches starts at a suitably
72 * aligned base addreess after the `_end` linker symbol, through to the end
73 * of system RAM.
74 */
75#if (!defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) || \
76 (defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) && \
77 CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE))
78#define Z_MALLOC_PARTITION_EXISTS 1
79#endif
80
81#endif /* CONFIG_NEWLIB_LIBC */
82
83#ifdef Z_MALLOC_PARTITION_EXISTS
84/* Memory partition containing the libc malloc arena. Configuration controls
85 * whether this is available, and an arena size may need to be set.
86 */
87extern struct k_mem_partition z_malloc_partition;
88#endif
89
90#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_STACK_CANARIES) || \
91defined(CONFIG_PICOLIBC) || defined(CONFIG_NEED_LIBC_MEM_PARTITION)
92/* - All newlib globals will be placed into z_libc_partition.
93 * - Minimal C library globals, if any, will be placed into
94 * z_libc_partition.
95 * - Stack canary globals will be placed into z_libc_partition since
96 * it is not worth placing in its own partition.
97 * - Some architectures may place the global pointer to the thread local
98 * storage in z_libc_partition since it is not worth placing in its
99 * own partition.
100 */
101#define Z_LIBC_PARTITION_EXISTS 1
102
103/* C library globals, except the malloc arena */
104extern struct k_mem_partition z_libc_partition;
105#endif
106#endif /* CONFIG_USERSPACE */
107
108#include <syscalls/libc-hooks.h>
109
110/* C library memory partitions */
111#define Z_LIBC_DATA K_APP_DMEM(z_libc_partition)
112
113#ifdef __cplusplus
114}
115#endif
116
117#endif /* ZEPHYR_INCLUDE_SYS_LIBC_HOOKS_H_ */
#define ZRESTRICT
Definition: common.h:31
size_t zephyr_fwrite(const void *ZRESTRICT ptr, size_t size, size_t nitems, FILE *ZRESTRICT stream)
int zephyr_fputc(int c, FILE *stream)
char c
Definition: printk.c:112
void * ptr
Definition: printk.c:120
int FILE
Definition: stdio.h:22
Memory Partition.
Definition: mem_domain.h:55
Macros to abstract toolchain specific capabilities.