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