-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnewlib_syscalls.c
More file actions
196 lines (156 loc) · 3.8 KB
/
newlib_syscalls.c
File metadata and controls
196 lines (156 loc) · 3.8 KB
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
// SPDX-License-Identifier: ZPL-2.1
// SPDX-FileCopyrightText: Copyright fincs, devkitPro
#include <calico/types.h>
#include <calico/system/mutex.h>
#include <calico/system/condvar.h>
#include <calico/system/thread.h>
#include <errno.h>
#include <malloc.h>
#include <sys/iosupport.h>
#if defined(__NDS__)
#include "../nds/transfer.h"
MK_INLINE time_t _getUnixTime(void)
{
return s_transferRegion->unix_time;
}
#else
MK_INLINE time_t _getUnixTime(void)
{
return 0;
}
#endif
struct __pthread_t {
Thread base;
};
// Dummy symbol referenced by crt0 so that this object file is pulled in by the linker
const u32 __newlib_syscalls = 0xdeadbeef;
struct _reent* __SYSCALL(getreent)(void)
{
return (struct _reent*)threadGetSelf()->impure;
}
int* __errno(void)
{
static int s_defaultErrno;
struct _reent* r = __SYSCALL(getreent)();
return r ? &r->_errno : &s_defaultErrno;
}
int __SYSCALL(gettod_r)(struct _reent* ptr, struct timeval* tp, struct timezone* tz)
{
if (tp) {
tp->tv_sec = _getUnixTime();
tp->tv_usec = 0;
}
if (tz) {
tz->tz_minuteswest = 0;
tz->tz_dsttime = 0;
}
return 0;
}
MK_CODE32 int __SYSCALL(nanosleep)(const struct timespec* req, struct timespec* rem)
{
threadSleep((unsigned)req->tv_sec*1000000U + (unsigned)req->tv_nsec/1000U);
return 0;
}
void __SYSCALL(lock_init)(_LOCK_T* lock)
{
*lock = (_LOCK_T){0};
}
void __SYSCALL(lock_acquire)(_LOCK_T* lock)
{
mutexLock((Mutex*)lock);
}
void __SYSCALL(lock_release)(_LOCK_T* lock)
{
mutexUnlock((Mutex*)lock);
}
void __SYSCALL(lock_init_recursive)(_LOCK_RECURSIVE_T* lock)
{
*lock = (_LOCK_RECURSIVE_T){0};
}
void __SYSCALL(lock_acquire_recursive)(_LOCK_RECURSIVE_T* lock)
{
rmutexLock((RMutex*)lock);
}
void __SYSCALL(lock_release_recursive)(_LOCK_RECURSIVE_T* lock)
{
rmutexUnlock((RMutex*)lock);
}
int __SYSCALL(cond_signal)(_COND_T* cond)
{
condvarSignal((CondVar*)cond);
return 0;
}
int __SYSCALL(cond_broadcast)(_COND_T* cond)
{
condvarBroadcast((CondVar*)cond);
return 0;
}
int __SYSCALL(cond_wait)(_COND_T* cond, _LOCK_T* lock, uint64_t timeout_ns)
{
if (timeout_ns != UINT64_MAX) {
return ETIMEDOUT;
}
condvarWait((CondVar*)cond, (Mutex*)lock);
return 0;
}
int __SYSCALL(cond_wait_recursive)(_COND_T* cond, _LOCK_RECURSIVE_T* lock, uint64_t timeout_ns)
{
if (timeout_ns != UINT64_MAX) {
return ETIMEDOUT;
}
RMutex* r = (RMutex*)lock;
u32 counter_backup = r->counter;
r->counter = 0;
condvarWait((CondVar*)cond, &r->mutex);
r->counter = counter_backup;
return 0;
}
static size_t extraTls = 0;
void threadSetPthreadExtraTls(const size_t size)
{
// make sure it's 8-byte aligned
extraTls = (size + 7) &~ 7;
}
int __SYSCALL(thread_create)(struct __pthread_t** thread, void* (*func)(void*), void* arg, void* stack_addr, size_t stack_size)
{
if (((uptr)stack_addr & 7) || (stack_size & 7)) {
return EINVAL;
}
if (!stack_size) {
stack_size = 8*1024;
}
size_t struct_sz = (sizeof(struct __pthread_t) + 7) &~ 7;
size_t needed_sz = struct_sz + threadGetLocalStorageSize() + extraTls;
if (!stack_addr) {
needed_sz += stack_size;
}
*thread = _malloc_r(__SYSCALL(getreent)(), needed_sz); // malloc align is 2*sizeof(void*); which is already 8
if (!*thread) {
return ENOMEM;
}
void* stack_top;
if (stack_addr) {
stack_top = (u8*)stack_addr + stack_size;
} else {
stack_top = (u8*)*thread + needed_sz;
}
Thread* t = &(*thread)->base;
threadPrepare(t, (ThreadFunc)func, arg, stack_top, THREAD_MIN_PRIO);
threadAttachLocalStorage(t, (u8*)*thread + struct_sz);
threadStart(t);
return 0;
}
void* __SYSCALL(thread_join)(struct __pthread_t* thread)
{
void* rc = (void*)threadJoin(&thread->base);
_free_r(__SYSCALL(getreent)(), thread);
return rc;
}
void __SYSCALL(thread_exit)(void* value)
{
threadExit((int)value);
}
struct __pthread_t* __SYSCALL(thread_self)(void)
{
return (struct __pthread_t*)threadGetSelf();
}