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 void __stdout_hook_install(int (*hook)(int));
48 
49 #ifdef CONFIG_USERSPACE
50 #ifdef CONFIG_COMMON_LIBC_MALLOC
51 
52 /* When using the common malloc implementation with CONFIG_USERSPACE, the
53  * heap will be in a separate partition when there's an MPU or MMU
54  * available.
55  */
56 #if CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE != 0 && \
57 (defined(CONFIG_MPU) || defined(CONFIG_MMU))
58 #define Z_MALLOC_PARTITION_EXISTS 1
59 #endif
60 
61 #elif defined(CONFIG_NEWLIB_LIBC)
62 /* If we are using newlib, the heap arena is in one of two areas:
63  *  - If we have an MPU that requires power of two alignment, the heap bounds
64  *    must be specified in Kconfig via CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE.
65  *  - Otherwise, the heap arena on most arches starts at a suitably
66  *    aligned base address after the `_end` linker symbol, through to the end
67  *    of system RAM.
68  */
69 #if (!defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) || \
70      (defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) && \
71       CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE))
72 #define Z_MALLOC_PARTITION_EXISTS 1
73 #endif
74 
75 #endif /* CONFIG_NEWLIB_LIBC */
76 
77 #ifdef Z_MALLOC_PARTITION_EXISTS
78 /* Memory partition containing the libc malloc arena. Configuration controls
79  * whether this is available, and an arena size may need to be set.
80  */
81 extern struct k_mem_partition z_malloc_partition;
82 #endif
83 
84 #ifdef CONFIG_NEED_LIBC_MEM_PARTITION
85 /* - All newlib globals will be placed into z_libc_partition.
86  * - Minimal C library globals, if any, will be placed into
87  *   z_libc_partition.
88  * - Stack canary globals will be placed into z_libc_partition since
89  *   it is not worth placing in its own partition.
90  * - Some architectures may place the global pointer to the thread local
91  *   storage in z_libc_partition since it is not worth placing in its
92  *   own partition.
93  */
94 #define Z_LIBC_PARTITION_EXISTS 1
95 
96 /* C library globals, except the malloc arena */
97 extern struct k_mem_partition z_libc_partition;
98 #endif
99 #endif /* CONFIG_USERSPACE */
100 
101 #include <zephyr/syscalls/libc-hooks.h>
102 
103 /* C library memory partitions */
104 #define Z_LIBC_DATA K_APP_DMEM(z_libc_partition)
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* ZEPHYR_INCLUDE_SYS_LIBC_HOOKS_H_ */
111