Zephyr Project API  3.3.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/*
16 * Private header for specifying accessory functions to the C library internals
17 * that need to call into the kernel as system calls
18 */
19
20#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC) || defined(CONFIG_PICOLIBC)
21
22/* syscall generation ignores preprocessor, ensure this is defined to ensure
23 * we don't have compile errors
24 */
25__syscall int zephyr_read_stdin(char *buf, int nbytes);
26
27__syscall int zephyr_write_stdout(const void *buf, int nbytes);
28
29#else
30/* Minimal libc */
31
32__syscall int zephyr_fputc(int c, FILE * stream);
33
34__syscall size_t zephyr_fwrite(const void *ZRESTRICT ptr, size_t size,
35 size_t nitems, FILE *ZRESTRICT stream);
36#endif /* CONFIG_NEWLIB_LIBC */
37
38#ifdef CONFIG_USERSPACE
39#if defined(CONFIG_NEWLIB_LIBC)
40/* If we are using newlib, the heap arena is in one of two areas:
41 * - If we have an MPU that requires power of two alignment, the heap bounds
42 * must be specified in Kconfig via CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE.
43 * - Otherwise, the heap arena on most arches starts at a suitably
44 * aligned base addreess after the `_end` linker symbol, through to the end
45 * of system RAM.
46 */
47#if (!defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) || \
48 (defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) && \
49 CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE))
50#define Z_MALLOC_PARTITION_EXISTS 1
51extern struct k_mem_partition z_malloc_partition;
52#endif
53#elif defined(CONFIG_MINIMAL_LIBC)
54#if (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE > 0)
55/* Minimal libc by default has no malloc arena, its size must be set in
56 * Kconfig via CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE
57 */
58#define Z_MALLOC_PARTITION_EXISTS 1
59#endif /* CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE > 0 */
60
61#elif defined(CONFIG_PICOLIBC)
62/*
63 * When using picolibc, we need z_malloc_partition whenever
64 * the heap size is not zero and there is an mpu or an mmu
65 */
66#if CONFIG_PICOLIBC_HEAP_SIZE != 0 && \
67(defined(CONFIG_MPU) || defined(CONFIG_MMU))
68#define Z_MALLOC_PARTITION_EXISTS 1
69#endif
70
71#endif /* CONFIG_PICOLIBC */
72
73#ifdef Z_MALLOC_PARTITION_EXISTS
74/* Memory partition containing the libc malloc arena. Configuration controls
75 * whether this is available, and an arena size may need to be set.
76 */
77extern struct k_mem_partition z_malloc_partition;
78#endif
79
80#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_STACK_CANARIES) || \
81defined(CONFIG_PICOLIBC) || defined(CONFIG_NEED_LIBC_MEM_PARTITION)
82/* - All newlib globals will be placed into z_libc_partition.
83 * - Minimal C library globals, if any, will be placed into
84 * z_libc_partition.
85 * - Stack canary globals will be placed into z_libc_partition since
86 * it is not worth placing in its own partition.
87 * - Some architectures may place the global pointer to the thread local
88 * storage in z_libc_partition since it is not worth placing in its
89 * own partition.
90 */
91#define Z_LIBC_PARTITION_EXISTS 1
92
93/* C library globals, except the malloc arena */
94extern struct k_mem_partition z_libc_partition;
95#endif
96#endif /* CONFIG_USERSPACE */
97
98#include <syscalls/libc-hooks.h>
99
100/* C library memory partitions */
101#define Z_LIBC_DATA K_APP_DMEM(z_libc_partition)
102
103#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.