1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef _ROM_LIBC_STUBS_H_
15 #define _ROM_LIBC_STUBS_H_
16 
17 #include <sys/lock.h>
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <reent.h>
23 #include <errno.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /*
30 ESP32 ROM code contains implementations of some of C library functions.
31 Whenever a function in ROM needs to use a syscall, it calls a pointer to the corresponding syscall
32 implementation defined in the following struct.
33 
34 The table itself, by default, is not allocated in RAM. A global pointer syscall_table_ptr is used to
35 set the address
36 
37 So, before using any of the C library functions (except for pure functions and memcpy/memset functions),
38 application must allocate syscall table structure for each CPU being used, and populate it with pointers
39 to actual implementations of corresponding syscalls.
40 */
41 
42 struct syscall_stub_table
43 {
44     struct _reent* (*__getreent)(void);
45     void* (*_malloc_r)(struct _reent *r, size_t);
46     void (*_free_r)(struct _reent *r, void*);
47     void* (*_realloc_r)(struct _reent *r, void*, size_t);
48     void* (*_calloc_r)(struct _reent *r, size_t, size_t);
49     void (*_abort)(void);
50     int (*_system_r)(struct _reent *r, const char*);
51     int (*_rename_r)(struct _reent *r, const char*, const char*);
52     clock_t (*_times_r)(struct _reent *r, struct tms *);
53     int (*_gettimeofday_r) (struct _reent *r, struct timeval *, void *);
54     void (*_raise_r)(struct _reent *r);
55     int (*_unlink_r)(struct _reent *r, const char*);
56     int (*_link_r)(struct _reent *r, const char*, const char*);
57     int (*_stat_r)(struct _reent *r, const char*, struct stat *);
58     int (*_fstat_r)(struct _reent *r, int, struct stat *);
59     void* (*_sbrk_r)(struct _reent *r, ptrdiff_t);
60     int (*_getpid_r)(struct _reent *r);
61     int (*_kill_r)(struct _reent *r, int, int);
62     void (*_exit_r)(struct _reent *r, int);
63     int (*_close_r)(struct _reent *r, int);
64     int (*_open_r)(struct _reent *r, const char *, int, int);
65     int (*_write_r)(struct _reent *r, int, const void *, int);
66     int (*_lseek_r)(struct _reent *r, int, int, int);
67     int (*_read_r)(struct _reent *r, int, void *, int);
68 #ifdef _RETARGETABLE_LOCKING
69     void (*_retarget_lock_init)(_LOCK_T *lock);
70     void (*_retarget_lock_init_recursive)(_LOCK_T *lock);
71     void (*_retarget_lock_close)(_LOCK_T lock);
72     void (*_retarget_lock_close_recursive)(_LOCK_T lock);
73     void (*_retarget_lock_acquire)(_LOCK_T lock);
74     void (*_retarget_lock_acquire_recursive)(_LOCK_T lock);
75     int (*_retarget_lock_try_acquire)(_LOCK_T lock);
76     int (*_retarget_lock_try_acquire_recursive)(_LOCK_T lock);
77     void (*_retarget_lock_release)(_LOCK_T lock);
78     void (*_retarget_lock_release_recursive)(_LOCK_T lock);
79 #else
80     void (*_lock_init)(_lock_t *lock);
81     void (*_lock_init_recursive)(_lock_t *lock);
82     void (*_lock_close)(_lock_t *lock);
83     void (*_lock_close_recursive)(_lock_t *lock);
84     void (*_lock_acquire)(_lock_t *lock);
85     void (*_lock_acquire_recursive)(_lock_t *lock);
86     int (*_lock_try_acquire)(_lock_t *lock);
87     int (*_lock_try_acquire_recursive)(_lock_t *lock);
88     void (*_lock_release)(_lock_t *lock);
89     void (*_lock_release_recursive)(_lock_t *lock);
90 #endif
91     int (*_printf_float)(struct _reent *data, void *pdata, FILE * fp, int (*pfunc) (struct _reent *, FILE *, const char *, size_t len), va_list * ap);
92     int (*_scanf_float) (struct _reent *rptr, void *pdata, FILE *fp, va_list *ap);
93     void (*__assert_func) (const char *file, int line, const char * func, const char *failedexpr) __attribute__((noreturn));
94     void (*__sinit) (struct _reent *r);
95     void (*_cleanup_r) (struct _reent* r);
96 };
97 
98 extern struct syscall_stub_table *syscall_table_ptr;
99 
100 #ifdef __cplusplus
101 } // extern "C"
102 #endif
103 
104 #endif /* _ROM_LIBC_STUBS_H_ */
105