syscalls.c 7.3 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
#include <reent.h>
#include <sys/errno.h>
#include <sys/time.h>
12 13
#include <stdio.h>

14 15
#include <rtthread.h>

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

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

24 25 26 27
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

28 29
/* Reentrant versions of system calls.  */

30 31 32 33 34 35 36 37
#ifndef _REENT_ONLY
int *
__errno ()
{
  return _rt_errno();
}
#endif

38 39 40
int
_close_r(struct _reent *ptr, int fd)
{
41
#ifndef RT_USING_DFS
B
BernardXiong 已提交
42
    return 0;
43
#else
B
BernardXiong 已提交
44
    return close(fd);
45
#endif
46 47 48 49 50
}

int
_execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
{
B
BernardXiong 已提交
51 52 53
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
54 55 56 57 58
}

int
_fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
{
B
BernardXiong 已提交
59 60 61
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
62 63 64 65 66
}

int
_fork_r(struct _reent *ptr)
{
B
BernardXiong 已提交
67 68 69
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
70 71 72 73 74
}

int
_fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
{
B
BernardXiong 已提交
75 76 77
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
78 79 80 81 82
}

int
_getpid_r(struct _reent *ptr)
{
B
BernardXiong 已提交
83
    return 0;
84 85 86 87 88
}

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

B
BernardXiong 已提交
91 92 93
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
94 95 96 97 98
}

int
_kill_r(struct _reent *ptr, int pid, int sig)
{
B
BernardXiong 已提交
99 100 101
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
102 103 104 105 106
}

int
_link_r(struct _reent *ptr, const char *old, const char *new)
{
B
BernardXiong 已提交
107 108 109
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
110 111 112 113 114
}

_off_t
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
115
#ifndef RT_USING_DFS
B
BernardXiong 已提交
116
    return 0;
117
#else
B
BernardXiong 已提交
118
    _off_t rc;
119

B
BernardXiong 已提交
120 121
    rc = lseek(fd, pos, whence);
    return rc;
122
#endif
123 124 125 126 127
}

int
_mkdir_r(struct _reent *ptr, const char *name, int mode)
{
128
#ifndef RT_USING_DFS
B
BernardXiong 已提交
129
    return 0;
130
#else
B
BernardXiong 已提交
131
    int rc;
132

B
BernardXiong 已提交
133 134
    rc = mkdir(name, mode);
    return rc;
135
#endif
136 137 138 139 140
}

int
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
141
#ifndef RT_USING_DFS
B
BernardXiong 已提交
142
    return 0;
143
#else
B
BernardXiong 已提交
144
    int rc;
145

B
BernardXiong 已提交
146 147
    rc = open(file, flags, mode);
    return rc;
148
#endif
149 150
}

151
_ssize_t
152 153
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
154
#ifndef RT_USING_DFS
B
BernardXiong 已提交
155
    return 0;
156
#else
B
BernardXiong 已提交
157
    _ssize_t rc;
158

B
BernardXiong 已提交
159 160
    rc = read(fd, buf, nbytes);
    return rc;
161
#endif
162 163 164 165 166
}

int
_rename_r(struct _reent *ptr, const char *old, const char *new)
{
167
#ifndef RT_USING_DFS
B
BernardXiong 已提交
168
    return 0;
169
#else
B
BernardXiong 已提交
170
    int rc;
171

B
BernardXiong 已提交
172 173
    rc = rename(old, new);
    return rc;
174
#endif
175 176 177 178 179
}

void *
_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
{
B
BernardXiong 已提交
180 181
    /* no use this routine to get memory */
    return RT_NULL;
182 183 184 185 186
}

int
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
187
#ifndef RT_USING_DFS
B
BernardXiong 已提交
188
    return 0;
189
#else
B
BernardXiong 已提交
190
    int rc;
191

B
BernardXiong 已提交
192 193
    rc = stat(file, pstat);
    return rc;
194
#endif
195 196 197 198 199
}

_CLOCK_T_
_times_r(struct _reent *ptr, struct tms *ptms)
{
B
BernardXiong 已提交
200 201 202
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
203 204 205 206 207
}

int
_unlink_r(struct _reent *ptr, const char *file)
{
208
#ifndef RT_USING_DFS
B
BernardXiong 已提交
209
    return 0;
210
#else
B
BernardXiong 已提交
211
    int rc;
212

B
BernardXiong 已提交
213 214
    rc = unlink(file);
    return rc;
215
#endif
216 217 218 219 220
}

int
_wait_r(struct _reent *ptr, int *status)
{
B
BernardXiong 已提交
221 222 223
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
224 225
}

226
#ifdef RT_USING_DEVICE
227 228 229
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
B
bernard 已提交
230
#ifndef RT_USING_DFS
231
    if (fileno(stdout) == fd)
B
BernardXiong 已提交
232 233
    {
        rt_device_t console;
234

B
BernardXiong 已提交
235 236 237
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
238

B
bernard 已提交
239 240
    return 0;

241
#else
B
BernardXiong 已提交
242
    _ssize_t rc;
B
bernard 已提交
243

B
BernardXiong 已提交
244 245
    rc = write(fd, buf, nbytes);
    return rc;
246
#endif
247
}
248
#endif
249

B
BernardXiong 已提交
250
#ifdef RT_USING_PTHREADS
251

B
BernardXiong 已提交
252 253 254 255 256 257 258
#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;
259

B
BernardXiong 已提交
260 261 262 263 264 265 266
    if (clock_gettime(CLOCK_REALTIME, &tp) == 0)
    {
        if (__tp != RT_NULL)
        {
            __tp->tv_sec  = tp.tv_sec;
            __tp->tv_usec = tp.tv_nsec / 1000UL;
        }
267

B
BernardXiong 已提交
268 269 270 271 272 273 274
        return tp.tv_sec;
    }

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

B
BernardXiong 已提交
276 277 278 279 280
#else

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

B
BernardXiong 已提交
282 283 284
#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)
285 286

struct timeval _timevalue = {0};
287 288
#ifdef RT_USING_DEVICE
static void libc_system_time_init(void)
289
{
B
BernardXiong 已提交
290 291 292
    time_t time;
    rt_tick_t tick;
    rt_device_t device;
293

B
BernardXiong 已提交
294 295 296 297 298 299 300
    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);
    }
301

B
BernardXiong 已提交
302 303
    /* get tick */
    tick = rt_tick_get();
304

B
BernardXiong 已提交
305 306
    _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
    _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
307
}
308
#endif
309 310 311

int libc_get_time(struct timespec *time)
{
B
BernardXiong 已提交
312 313
    rt_tick_t tick;
    static rt_bool_t inited = 0;
314

B
BernardXiong 已提交
315
    RT_ASSERT(time != RT_NULL);
316

B
BernardXiong 已提交
317 318 319 320 321 322
    /* initialize system time */
    if (inited == 0)
    {
        libc_system_time_init();
        inited = 1;
    }
323

B
BernardXiong 已提交
324 325
    /* get tick */
    tick = rt_tick_get();
326

B
BernardXiong 已提交
327 328
    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;
329

B
BernardXiong 已提交
330
    return 0;
331 332 333 334 335
}

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

B
BernardXiong 已提交
338 339 340 341 342 343 344
    if (libc_get_time(&tp) == 0)
    {
        if (__tp != RT_NULL)
        {
            __tp->tv_sec  = tp.tv_sec;
            __tp->tv_usec = tp.tv_nsec / 1000UL;
        }
345

B
BernardXiong 已提交
346 347
        return tp.tv_sec;
    }
348

B
BernardXiong 已提交
349 350 351
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
352 353 354 355 356 357 358
}
#endif

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

B
BernardXiong 已提交
361 362 363 364 365
    result = (void*)rt_malloc (size);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
366

B
BernardXiong 已提交
367
    return result;
368 369 370 371 372
}

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

B
BernardXiong 已提交
375 376 377 378 379
    result = (void*)rt_realloc (old, newlen);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
380

B
BernardXiong 已提交
381
    return result;
382 383 384 385
}

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

B
BernardXiong 已提交
388 389 390 391 392
    result = (void*)rt_calloc (size, len);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
393

B
BernardXiong 已提交
394
    return result;
395 396
}

397
void
398 399
_free_r (struct _reent *ptr, void *addr)
{
B
BernardXiong 已提交
400
    rt_free (addr);
401 402 403
}

void
404
exit (int status)
405
{
406
#ifdef RT_USING_MODULE
407
    if (dlmodule_self())
B
BernardXiong 已提交
408
    {
409
        dlmodule_exit(status);
B
BernardXiong 已提交
410
    }
411
#endif
412

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

B
BernardXiong 已提交
416
    while (1);
417
}
418

419
void
420 421 422 423 424
_system(const char *s)
{
    /* not support this call */
    return;
}
425 426 427

void __libc_init_array(void)
{
B
BernardXiong 已提交
428
    /* we not use __libc init_aray to initialize C++ objects */
429
}
430 431 432

void abort(void)
{
433 434 435 436 437 438 439 440 441 442
    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 已提交
443
    while (1);
444
}