-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathsyscalls.c
395 lines (316 loc) · 9.08 KB
/
syscalls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (C) 2019 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_esp_common
* @{
*
* @file
* @brief Implementation of required system calls
*
* @author Gunar Schorcht <[email protected]>
*
* @}
*/
#include <string.h>
#include <stdio_ext.h>
#include <sys/unistd.h>
#include "irq_arch.h"
#include "mutex.h"
#include "rmutex.h"
#include "timex.h"
#include "esp_attr.h"
#include "syscalls.h"
#ifdef MODULE_ESP_IDF_HEAP
#include "esp_heap_caps.h"
#else
#include "malloc.h"
#endif
#define ENABLE_DEBUG (0)
#include "debug.h"
#ifndef MODULE_PTHREAD
#define PTHREAD_CANCEL_DISABLE 1
/*
* This is a dummy function to avoid undefined references when linking
* against newlib and module pthread is not used.
*/
int pthread_setcancelstate(int state, int *oldstate)
{
if (oldstate) {
*oldstate = PTHREAD_CANCEL_DISABLE;
}
return 0;
}
#endif /* MODULE_PTHREAD */
/*
* TODO: When the lock functions in this section are enabled, an application
* crashes when an ISR calls a `newlib` function that uses `_lock_acquire`
* or `_log_acquire_recursive` to be thread-safe, for example, `puts` in
* `tests/isr_yield_higher`. The reason is that the implementation of these
* functions uses `mutex` and `rmutex` that do not work in the interrupt
* context. Therefore, the lock functions are disabled for the moment, and
* instead `newlib`'s dummy lock functions are used which do not guarantee
* thread safety.
*/
/**
* @name Locking functions
*
* Following functions implement the lock mechanism for newlib.
*/
/**
* _malloc_rmtx is defined as static variable to avoid recursive calls of
* malloc when _malloc_r tries to lock __malloc_lock_object the first
* time. All other mutexes that are used for the lock mechanism are allocated
* dynamically.
*/
static rmutex_t _malloc_rmtx = RMUTEX_INIT;
/**
* To properly handle the static rmutex _malloc_rmtx, we have to know
* the address of newlib's static variable __malloc_lock_object.
*/
static _lock_t *__malloc_static_object = NULL;
void IRAM_ATTR _lock_init(_lock_t *lock)
{
assert(lock != NULL);
mutex_t* mtx = malloc(sizeof(mutex_t));
if (mtx) {
memset(mtx, 0, sizeof(mutex_t));
*lock = (_lock_t)mtx;
}
}
void IRAM_ATTR _lock_init_recursive(_lock_t *lock)
{
assert(lock != NULL);
/**
* Since we don't have direct access to newlib's static variable
* __malloc_lock_object, we have to rely on the fact that function
* _lock_aqcuire_recursive, and thus function _lock_init_recursive
* is called for the first time with newlib's static variable
* __malloc_lock_object as parameter. This is ensured by calling
* malloc in the function syscalls_init.
*/
if (__malloc_static_object == NULL) {
*lock = (_lock_t)&_malloc_rmtx;
__malloc_static_object = lock;
return;
}
/* _malloc_rmtx is static and has not to be allocated */
if (lock == __malloc_static_object) {
return;
}
rmutex_t* rmtx = malloc(sizeof(rmutex_t));
if (rmtx) {
memset(rmtx, 0, sizeof(rmutex_t));
*lock = (_lock_t)rmtx;
}
}
void IRAM_ATTR _lock_close(_lock_t *lock)
{
assert(lock != NULL);
assert(lock != __malloc_static_object);
free((void*)*lock);
*lock = 0;
}
void IRAM_ATTR _lock_close_recursive(_lock_t *lock)
{
assert(lock != NULL);
assert(lock != __malloc_static_object);
free((void*)*lock);
*lock = 0;
}
void IRAM_ATTR _lock_acquire(_lock_t *lock)
{
assert(lock != NULL);
/* if the lock data structure is still not allocated, initialize it first */
if (*lock == 0) {
_lock_init(lock);
}
/* if scheduler is not running, we have not to lock the mutex */
if (sched_active_thread == NULL) {
return;
}
assert(!irq_is_in());
mutex_lock((mutex_t*)*lock);
}
void IRAM_ATTR _lock_acquire_recursive(_lock_t *lock)
{
assert(lock != NULL);
/* if the lock data structure is still not allocated, initialize it first */
if (*lock == 0) {
_lock_init_recursive(lock);
}
/* if scheduler is not running, we have not to lock the rmutex */
if (sched_active_thread == NULL) {
return;
}
assert(!irq_is_in());
rmutex_lock((rmutex_t*)*lock);
}
int IRAM_ATTR _lock_try_acquire(_lock_t *lock)
{
assert(lock != NULL);
/* if the lock data structure is still not allocated, initialize it first */
if (*lock == 0) {
_lock_init(lock);
}
/* if scheduler is not running, we have not to lock the mutex */
if (sched_active_thread == NULL) {
return 0;
}
if (irq_is_in()) {
return 0;
}
return mutex_trylock((mutex_t*)*lock);
}
int IRAM_ATTR _lock_try_acquire_recursive(_lock_t *lock)
{
assert(lock != NULL);
/* if the lock data structure is still not allocated, initialize it first */
if (*lock == 0) {
_lock_init_recursive(lock);
}
/* if scheduler is not running, we have not to lock the rmutex */
if (sched_active_thread == NULL) {
return 0;
}
if (irq_is_in()) {
return 0;
}
return rmutex_trylock((rmutex_t*)*lock);
}
void IRAM_ATTR _lock_release(_lock_t *lock)
{
assert(lock != NULL && *lock != 0);
/* if scheduler is not running, we have not to unlock the mutex */
if (sched_active_thread == NULL) {
return;
}
mutex_unlock((mutex_t*)*lock);
}
void IRAM_ATTR _lock_release_recursive(_lock_t *lock)
{
assert(lock != NULL && *lock != 0);
/* if scheduler is not running, we have not to unlock the rmutex */
if (sched_active_thread == NULL) {
return;
}
rmutex_unlock((rmutex_t*)*lock);
}
/**
* @name Memory allocation functions
*/
#ifdef MODULE_ESP_IDF_HEAP
#define heap_caps_malloc_default(s) heap_caps_malloc(s, MALLOC_CAP_DEFAULT)
#define heap_caps_realloc_default(p,s) heap_caps_realloc(p, s, MALLOC_CAP_DEFAULT)
void* IRAM_ATTR __wrap__malloc_r(struct _reent *r, size_t size)
{
return heap_caps_malloc_default( size );
}
void IRAM_ATTR __wrap__free_r(struct _reent *r, void *ptr)
{
heap_caps_free( ptr );
}
void* IRAM_ATTR __wrap__realloc_r(struct _reent *r, void* ptr, size_t size)
{
return heap_caps_realloc_default( ptr, size );
}
void* IRAM_ATTR __wrap__calloc_r(struct _reent *r, size_t count, size_t size)
{
void *result = heap_caps_malloc_default(count * size);
if (result) {
bzero(result, count * size);
}
return result;
}
#else /* MODULE_ESP_IDF_HEAP */
/* for compatibility with ESP-IDF heap functions */
void* _heap_caps_malloc(size_t size, uint32_t caps, const char *file, size_t line)
{
(void)caps;
return malloc(size);
}
void* _heap_caps_calloc(size_t n, size_t size, uint32_t caps, const char *file, size_t line)
{
(void)caps;
return calloc(n, size);
}
void* _heap_caps_realloc(void *ptr, size_t size, uint32_t caps, const char *file, size_t line)
{
return realloc(ptr, size);
}
void *_heap_caps_zalloc(size_t size, uint32_t caps, const char *file, size_t line)
{
void *ptr = malloc(size);
if (ptr) {
memset(ptr, 0, size);
}
return ptr;
}
void _heap_caps_free(void *ptr, const char *file, size_t line)
{
(void)file;
(void)line;
free(ptr);
}
void heap_caps_init(void)
{
}
extern uint8_t _eheap; /* end of heap (defined in ld script) */
extern uint8_t _sheap; /* start of heap (defined in ld script) */
unsigned int IRAM_ATTR get_free_heap_size(void)
{
struct mallinfo minfo = mallinfo();
return &_eheap - &_sheap - minfo.uordblks;
}
void heap_stats(void)
{
ets_printf("heap: %u (used %u, free %u) [bytes]\n",
&_eheap - &_sheap, &_eheap - &_sheap - get_free_heap_size(),
get_free_heap_size());
}
/* alias for compatibility with espressif/wifi_libs */
uint32_t esp_get_free_heap_size( void ) __attribute__((alias("get_free_heap_size")));
#endif /* MODULE_ESP_IDF_HEAP */
/**
* @name Other system functions
*/
struct _reent* __getreent(void) {
return _GLOBAL_REENT;
}
static struct _reent s_reent;
void syscalls_init(void)
{
extern void syscalls_init_arch(void);
syscalls_init_arch();
_GLOBAL_REENT = &s_reent;
environ = malloc(sizeof(char*));
environ[0] = NULL;
/* initialization of newlib, includes the ctors initialization */
extern void __libc_init_array(void);
__libc_init_array();
/* initialization of gloabal reent data structure */
_REENT_SMALL_CHECK_INIT(_GLOBAL_REENT);
/*
* disable the locking for stdout/stderr to avoid rmutex based locking
* when puts/printf are called from an ISR
*/
__fsetlocking(_GLOBAL_REENT->_stdout, FSETLOCKING_BYCALLER);
__fsetlocking(_GLOBAL_REENT->_stderr, FSETLOCKING_BYCALLER);
}
__attribute__((weak)) void
_system_prevent_memset_lto(void *const s, int c, const size_t n)
{
(void)s;
(void)c;
(void)n;
}
void *system_secure_memset(void *s, int c, size_t n)
{
memset(s, c, n);
_system_prevent_memset_lto(s, c, n);
return s;
}