syscalls.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */
9 10 11 12 13
#include <reent.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <rtthread.h>

B
Bernard Xiong 已提交
14 15 16 17
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif

18
#ifdef RT_USING_PTHREADS
B
Bernard Xiong 已提交
19 20 21
#include <pthread.h>
#endif

22 23 24 25
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

26 27 28 29 30
/* Reentrant versions of system calls.  */

int
_close_r(struct _reent *ptr, int fd)
{
31
#ifndef RT_USING_DFS
B
BernardXiong 已提交
32
    return 0;
33
#else
B
BernardXiong 已提交
34
    return close(fd);
35
#endif
36 37 38 39 40
}

int
_execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
{
B
BernardXiong 已提交
41 42 43
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
44 45 46 47 48
}

int
_fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
{
B
BernardXiong 已提交
49 50 51
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
52 53 54 55 56
}

int
_fork_r(struct _reent *ptr)
{
B
BernardXiong 已提交
57 58 59
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
60 61 62 63 64
}

int
_fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
{
B
BernardXiong 已提交
65 66 67
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
68 69 70 71 72
}

int
_getpid_r(struct _reent *ptr)
{
B
BernardXiong 已提交
73
    return 0;
74 75 76 77 78
}

int
_isatty_r(struct _reent *ptr, int fd)
{
B
BernardXiong 已提交
79
    if (fd >=0 && fd < 3) return 1;
80

B
BernardXiong 已提交
81 82 83
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
84 85 86 87 88
}

int
_kill_r(struct _reent *ptr, int pid, int sig)
{
B
BernardXiong 已提交
89 90 91
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
92 93 94 95 96
}

int
_link_r(struct _reent *ptr, const char *old, const char *new)
{
B
BernardXiong 已提交
97 98 99
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
100 101 102 103 104
}

_off_t
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
105
#ifndef RT_USING_DFS
B
BernardXiong 已提交
106
    return 0;
107
#else
B
BernardXiong 已提交
108
    _off_t rc;
109

B
BernardXiong 已提交
110 111
    rc = lseek(fd, pos, whence);
    return rc;
112
#endif
113 114 115 116 117
}

int
_mkdir_r(struct _reent *ptr, const char *name, int mode)
{
118
#ifndef RT_USING_DFS
B
BernardXiong 已提交
119
    return 0;
120
#else
B
BernardXiong 已提交
121
    int rc;
122

B
BernardXiong 已提交
123 124
    rc = mkdir(name, mode);
    return rc;
125
#endif
126 127 128 129 130
}

int
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
131
#ifndef RT_USING_DFS
B
BernardXiong 已提交
132
    return 0;
133
#else
B
BernardXiong 已提交
134
    int rc;
135

B
BernardXiong 已提交
136 137
    rc = open(file, flags, mode);
    return rc;
138
#endif
139 140
}

141
_ssize_t
142 143
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
144
#ifndef RT_USING_DFS
B
BernardXiong 已提交
145
    return 0;
146
#else
B
BernardXiong 已提交
147
    _ssize_t rc;
148

B
BernardXiong 已提交
149 150
    rc = read(fd, buf, nbytes);
    return rc;
151
#endif
152 153 154 155 156
}

int
_rename_r(struct _reent *ptr, const char *old, const char *new)
{
157
#ifndef RT_USING_DFS
B
BernardXiong 已提交
158
    return 0;
159
#else
B
BernardXiong 已提交
160
    int rc;
161

B
BernardXiong 已提交
162 163
    rc = rename(old, new);
    return rc;
164
#endif
165 166 167 168 169
}

void *
_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
{
B
BernardXiong 已提交
170 171
    /* no use this routine to get memory */
    return RT_NULL;
172 173 174 175 176
}

int
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
177
#ifndef RT_USING_DFS
B
BernardXiong 已提交
178
    return 0;
179
#else
B
BernardXiong 已提交
180
    int rc;
181

B
BernardXiong 已提交
182 183
    rc = stat(file, pstat);
    return rc;
184
#endif
185 186 187 188 189
}

_CLOCK_T_
_times_r(struct _reent *ptr, struct tms *ptms)
{
B
BernardXiong 已提交
190 191 192
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
193 194 195 196 197
}

int
_unlink_r(struct _reent *ptr, const char *file)
{
198
#ifndef RT_USING_DFS
B
BernardXiong 已提交
199
    return 0;
200
#else
B
BernardXiong 已提交
201
    int rc;
202

B
BernardXiong 已提交
203 204
    rc = unlink(file);
    return rc;
205
#endif
206 207 208 209 210
}

int
_wait_r(struct _reent *ptr, int *status)
{
B
BernardXiong 已提交
211 212 213
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
214 215
}

216
#ifdef RT_USING_DEVICE
217 218 219
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
B
bernard 已提交
220
#ifndef RT_USING_DFS
B
BernardXiong 已提交
221 222 223
    if (fd == 0)
    {
        rt_device_t console;
224

B
BernardXiong 已提交
225 226 227
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
228

B
bernard 已提交
229 230
    return 0;

231
#else
B
BernardXiong 已提交
232
    _ssize_t rc;
B
bernard 已提交
233

B
BernardXiong 已提交
234 235
    rc = write(fd, buf, nbytes);
    return rc;
236
#endif
237
}
238
#endif
239

B
BernardXiong 已提交
240
#ifdef RT_USING_PTHREADS
241

B
BernardXiong 已提交
242 243 244 245 246 247 248
#include <clock_time.h>
/* POSIX timer provides clock_gettime function */
#include <time.h>
int
_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
{
    struct timespec tp;
249

B
BernardXiong 已提交
250 251 252 253 254 255 256
    if (clock_gettime(CLOCK_REALTIME, &tp) == 0)
    {
        if (__tp != RT_NULL)
        {
            __tp->tv_sec  = tp.tv_sec;
            __tp->tv_usec = tp.tv_nsec / 1000UL;
        }
257

B
BernardXiong 已提交
258 259 260 261 262 263 264
        return tp.tv_sec;
    }

    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
}
265

B
BernardXiong 已提交
266 267 268 269 270
#else

#define MILLISECOND_PER_SECOND  1000UL
#define MICROSECOND_PER_SECOND  1000000UL
#define NANOSECOND_PER_SECOND   1000000000UL
271

B
BernardXiong 已提交
272 273 274
#define MILLISECOND_PER_TICK    (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
#define MICROSECOND_PER_TICK    (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
#define NANOSECOND_PER_TICK     (NANOSECOND_PER_SECOND  / RT_TICK_PER_SECOND)
275 276

struct timeval _timevalue = {0};
277 278
#ifdef RT_USING_DEVICE
static void libc_system_time_init(void)
279
{
B
BernardXiong 已提交
280 281 282
    time_t time;
    rt_tick_t tick;
    rt_device_t device;
283

B
BernardXiong 已提交
284 285 286 287 288 289 290
    time = 0;
    device = rt_device_find("rtc");
    if (device != RT_NULL)
    {
        /* get realtime seconds */
        rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
    }
291

B
BernardXiong 已提交
292 293
    /* get tick */
    tick = rt_tick_get();
294

B
BernardXiong 已提交
295 296
    _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
    _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
297
}
298
#endif
299 300 301

int libc_get_time(struct timespec *time)
{
B
BernardXiong 已提交
302 303
    rt_tick_t tick;
    static rt_bool_t inited = 0;
304

B
BernardXiong 已提交
305
    RT_ASSERT(time != RT_NULL);
306

B
BernardXiong 已提交
307 308 309 310 311 312
    /* initialize system time */
    if (inited == 0)
    {
        libc_system_time_init();
        inited = 1;
    }
313

B
BernardXiong 已提交
314 315
    /* get tick */
    tick = rt_tick_get();
316

B
BernardXiong 已提交
317 318
    time->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
    time->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
319

B
BernardXiong 已提交
320
    return 0;
321 322 323 324 325
}

int
_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
{
B
BernardXiong 已提交
326
    struct timespec tp;
327

B
BernardXiong 已提交
328 329 330 331 332 333 334
    if (libc_get_time(&tp) == 0)
    {
        if (__tp != RT_NULL)
        {
            __tp->tv_sec  = tp.tv_sec;
            __tp->tv_usec = tp.tv_nsec / 1000UL;
        }
335

B
BernardXiong 已提交
336 337
        return tp.tv_sec;
    }
338

B
BernardXiong 已提交
339 340 341
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
342 343 344 345 346 347 348
}
#endif

/* Memory routine */
void *
_malloc_r (struct _reent *ptr, size_t size)
{
B
BernardXiong 已提交
349
    void* result;
350

B
BernardXiong 已提交
351 352 353 354 355
    result = (void*)rt_malloc (size);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
356

B
BernardXiong 已提交
357
    return result;
358 359 360 361 362
}

void *
_realloc_r (struct _reent *ptr, void *old, size_t newlen)
{
B
BernardXiong 已提交
363
    void* result;
364

B
BernardXiong 已提交
365 366 367 368 369
    result = (void*)rt_realloc (old, newlen);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
370

B
BernardXiong 已提交
371
    return result;
372 373 374 375
}

void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
{
B
BernardXiong 已提交
376
    void* result;
377

B
BernardXiong 已提交
378 379 380 381 382
    result = (void*)rt_calloc (size, len);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
383

B
BernardXiong 已提交
384
    return result;
385 386
}

387
void
388 389
_free_r (struct _reent *ptr, void *addr)
{
B
BernardXiong 已提交
390
    rt_free (addr);
391 392 393
}

void
394
exit (int status)
395
{
396
#ifdef RT_USING_MODULE
397
    if (dlmodule_self())
B
BernardXiong 已提交
398
    {
399
        dlmodule_exit(status);
B
BernardXiong 已提交
400
    }
401
#endif
402

B
BernardXiong 已提交
403 404
    rt_kprintf("thread:%s exit with %d\n", rt_thread_self()->name, status);
    RT_ASSERT(0);
405

B
BernardXiong 已提交
406
    while (1);
407
}
408

409
void
410 411 412 413 414
_system(const char *s)
{
    /* not support this call */
    return;
}
415 416 417

void __libc_init_array(void)
{
B
BernardXiong 已提交
418
    /* we not use __libc init_aray to initialize C++ objects */
419
}
420 421 422

void abort(void)
{
423 424 425 426 427 428 429 430 431 432
    if (rt_thread_self())
    {
        rt_thread_t self = rt_thread_self();

        rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
        rt_thread_suspend(self);

        rt_schedule();
    }

B
BernardXiong 已提交
433
    while (1);
434
}