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>
11 #include <zephyr/app_memory/app_memdomain.h>
12 #include <stdio.h>
13 #include <stddef.h>
14 
15 #ifdef __cplusplus
16 extern "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)
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 and picolibc */
35 
36 __syscall int zephyr_fputc(int c, FILE * stream);
37 
38 #ifdef CONFIG_MINIMAL_LIBC
39 /* Minimal libc only */
40 
41 __syscall size_t zephyr_fwrite(const void *ZRESTRICT ptr, size_t size,
42 				size_t nitems, FILE *ZRESTRICT stream);
43 #endif
44 
45 #endif /* CONFIG_NEWLIB_LIBC */
46 
47 /* Handle deprecated malloc arena size configuration values */
48 #ifdef CONFIG_COMMON_LIBC_MALLOC
49 # if defined(CONFIG_MINIMAL_LIBC) && (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE != -2)
50 #  undef CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE
51 #  define CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE
52 #  warning Using deprecated setting CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE
53 # elif defined(CONFIG_PICOLIBC) && (CONFIG_PICOLIBC_HEAP_SIZE != -2)
54 #  undef CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE
55 #  define CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE CONFIG_PICOLIBC_HEAP_SIZE
56 #  warning Using deprecated setting CONFIG_PICOLIBC_HEAP_SIZE
57 # endif
58 #endif
59 
60 #ifdef CONFIG_USERSPACE
61 #ifdef CONFIG_COMMON_LIBC_MALLOC
62 
63 /* When using the common malloc implementation with CONFIG_USERSPACE, the
64  * heap will be in a separate partition when there's an MPU or MMU
65  * available.
66  */
67 #if CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE != 0 && \
68 (defined(CONFIG_MPU) || defined(CONFIG_MMU))
69 #define Z_MALLOC_PARTITION_EXISTS 1
70 #endif
71 
72 #elif defined(CONFIG_NEWLIB_LIBC)
73 /* If we are using newlib, the heap arena is in one of two areas:
74  *  - If we have an MPU that requires power of two alignment, the heap bounds
75  *    must be specified in Kconfig via CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE.
76  *  - Otherwise, the heap arena on most arches starts at a suitably
77  *    aligned base addreess after the `_end` linker symbol, through to the end
78  *    of system RAM.
79  */
80 #if (!defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) || \
81      (defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) && \
82       CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE))
83 #define Z_MALLOC_PARTITION_EXISTS 1
84 #endif
85 
86 #endif /* CONFIG_NEWLIB_LIBC */
87 
88 #ifdef Z_MALLOC_PARTITION_EXISTS
89 /* Memory partition containing the libc malloc arena. Configuration controls
90  * whether this is available, and an arena size may need to be set.
91  */
92 extern struct k_mem_partition z_malloc_partition;
93 #endif
94 
95 #ifdef CONFIG_NEED_LIBC_MEM_PARTITION
96 /* - All newlib globals will be placed into z_libc_partition.
97  * - Minimal C library globals, if any, will be placed into
98  *   z_libc_partition.
99  * - Stack canary globals will be placed into z_libc_partition since
100  *   it is not worth placing in its own partition.
101  * - Some architectures may place the global pointer to the thread local
102  *   storage in z_libc_partition since it is not worth placing in its
103  *   own partition.
104  */
105 #define Z_LIBC_PARTITION_EXISTS 1
106 
107 /* C library globals, except the malloc arena */
108 extern struct k_mem_partition z_libc_partition;
109 #endif
110 #endif /* CONFIG_USERSPACE */
111 
112 #include <syscalls/libc-hooks.h>
113 
114 /* C library memory partitions */
115 #define Z_LIBC_DATA K_APP_DMEM(z_libc_partition)
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* ZEPHYR_INCLUDE_SYS_LIBC_HOOKS_H_ */
122