Skip to content

Commit e701954

Browse files
committed
sysdeps/zinnia: Implement more sysdeps
1 parent f085e2f commit e701954

4 files changed

Lines changed: 242 additions & 128 deletions

File tree

sysdeps/zinnia/generic/posix.cpp

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stdlib.h>
1313
#include <string.h>
1414
#include <sys/ioctl.h>
15+
#include <sys/resource.h>
1516
#include <sys/stat.h>
1617
#include <zinnia/syscall.hpp>
1718

@@ -47,6 +48,13 @@ int Sysdeps<ClockGetres>::operator()(int clock, time_t *secs, long *nanos) {
4748
return 0;
4849
}
4950

51+
int Sysdeps<ClockSet>::operator()(int clock, time_t secs, long nanos) {
52+
struct timespec ts;
53+
ts.tv_sec = secs;
54+
ts.tv_nsec = nanos;
55+
return zinnia_syscall(SYSCALL_CLOCK_SET, clock, (size_t)&ts).error;
56+
}
57+
5058
int Sysdeps<Flock>::operator()(int fd, int options) {
5159
auto r = zinnia_syscall(SYSCALL_FLOCK, fd, options);
5260
return r.error;
@@ -165,6 +173,14 @@ int Sysdeps<Ftruncate>::operator()(int fd, size_t size) {
165173
return zinnia_syscall(SYSCALL_FTRUNCATE, fd, size).error;
166174
}
167175

176+
int Sysdeps<Truncate>::operator()(const char *path, off_t length) {
177+
return zinnia_syscall(SYSCALL_TRUNCATE, (size_t)path, length).error;
178+
}
179+
180+
int Sysdeps<Fadvise>::operator()(int fd, off_t offset, off_t length, int advice) {
181+
return zinnia_syscall(SYSCALL_FADVISE, fd, offset, length, advice).error;
182+
}
183+
168184
int Sysdeps<Fallocate>::operator()(int fd, off_t offset, size_t size) {
169185
return zinnia_syscall(SYSCALL_FALLOCATE, fd, offset, size).error;
170186
}
@@ -351,6 +367,10 @@ int Sysdeps<Execve>::operator()(const char *path, char *const argv[], char *cons
351367
return zinnia_syscall(SYSCALL_EXECVE, (size_t)path, (size_t)argv, (size_t)envp).error;
352368
}
353369

370+
int Sysdeps<Fexecve>::operator()(int fd, char *const argv[], char *const envp[]) {
371+
return zinnia_syscall(SYSCALL_FEXECVE, fd, (size_t)argv, (size_t)envp).error;
372+
}
373+
354374
int Sysdeps<Pselect>::operator()(
355375
int num_fds,
356376
fd_set *read_set,
@@ -588,6 +608,27 @@ int Sysdeps<Tcsetattr>::operator()(int fd, int optional_action, const struct ter
588608
return sysdep<Ioctl>(fd, req, (void *)attr, &ret);
589609
}
590610

611+
int Sysdeps<Tcsendbreak>::operator()(int fd, int) {
612+
// Like Linux, we ignore the duration and always send a break of the default length.
613+
int ret;
614+
return sysdep<Ioctl>(fd, TCSBRK, (void *)0, &ret);
615+
}
616+
617+
int Sysdeps<Tcdrain>::operator()(int fd) {
618+
int ret;
619+
return sysdep<Ioctl>(fd, TCSBRK, (void *)1, &ret);
620+
}
621+
622+
int Sysdeps<Tcflow>::operator()(int fd, int action) {
623+
int ret;
624+
return sysdep<Ioctl>(fd, TCXONC, (void *)(uintptr_t)action, &ret);
625+
}
626+
627+
int Sysdeps<Tcflush>::operator()(int fd, int queue) {
628+
int ret;
629+
return sysdep<Ioctl>(fd, TCFLSH, (void *)(uintptr_t)queue, &ret);
630+
}
631+
591632
int Sysdeps<Tcgetwinsize>::operator()(int fd, struct winsize *winsz) {
592633
int result;
593634
return sysdep<Ioctl>(fd, TIOCGWINSZ, winsz, &result);
@@ -621,7 +662,7 @@ int Sysdeps<Poll>::operator()(struct pollfd *fds, nfds_t count, int timeout, int
621662
ts.tv_sec = timeout / 1000;
622663
ts.tv_nsec = (timeout % 1000) * 1000000;
623664

624-
return sysdep<Ppoll>(fds, count, timeout != -1 ? &ts : NULL, NULL, num_events);
665+
return sysdep<Ppoll>(fds, count, timeout != -1 ? &ts : nullptr, nullptr, num_events);
625666
}
626667

627668
int Sysdeps<Ioctl>::operator()(int fd, unsigned long request, void *arg, int *result) {
@@ -669,6 +710,27 @@ int Sysdeps<Kill>::operator()(pid_t pid, int signal) {
669710
return zinnia_syscall(SYSCALL_KILL, pid, signal).error;
670711
}
671712

713+
int Sysdeps<Sigqueue>::operator()(pid_t pid, int sig, const union sigval val) {
714+
return zinnia_syscall(SYSCALL_SIGQUEUE, pid, sig, (size_t)val.sival_ptr).error;
715+
}
716+
717+
int Sysdeps<Nice>::operator()(int inc, int *new_nice) {
718+
auto r = zinnia_syscall(SYSCALL_GETPRIORITY, PRIO_PROCESS, 0);
719+
if (r.error)
720+
return r.error;
721+
722+
int target = (int)r.value + inc;
723+
if (int e = zinnia_syscall(SYSCALL_SETPRIORITY, PRIO_PROCESS, 0, target).error)
724+
return e;
725+
726+
auto updated = zinnia_syscall(SYSCALL_GETPRIORITY, PRIO_PROCESS, 0);
727+
if (updated.error)
728+
return updated.error;
729+
730+
*new_nice = (int)updated.value;
731+
return 0;
732+
}
733+
672734
int Sysdeps<GetHostname>::operator()(char *buffer, size_t bufsize) {
673735
struct utsname name;
674736
int i = sysdep<Uname>(&name);
@@ -764,6 +826,18 @@ int Sysdeps<TimerSettime>::operator()(
764826
return zinnia_syscall(SYSCALL_TIMER_SET, (size_t)t, flags, (size_t)val, (size_t)old).error;
765827
}
766828

829+
int Sysdeps<TimerGettime>::operator()(timer_t t, struct itimerspec *val) {
830+
return zinnia_syscall(SYSCALL_TIMER_GET, (size_t)t, (size_t)val).error;
831+
}
832+
833+
int Sysdeps<TimerGetoverrun>::operator()(timer_t t, int *out) {
834+
auto r = zinnia_syscall(SYSCALL_TIMER_GETOVERRUN, (size_t)t);
835+
if (r.error)
836+
return r.error;
837+
*out = (int)r.value;
838+
return 0;
839+
}
840+
767841
int Sysdeps<TimerDelete>::operator()(timer_t t) {
768842
return zinnia_syscall(SYSCALL_TIMER_DELETE, (size_t)t).error;
769843
}
@@ -799,7 +873,7 @@ int Sysdeps<SetRegid>::operator()(gid_t rgid, gid_t egid) {
799873

800874
int Sysdeps<Unlockpt>::operator()(int fd) {
801875
int unlock = 0;
802-
if (int e = sysdep<Ioctl>(fd, TIOCSPTLCK, &unlock, NULL); e)
876+
if (int e = sysdep<Ioctl>(fd, TIOCSPTLCK, &unlock, nullptr); e)
803877
return e;
804878
return 0;
805879
}
@@ -820,7 +894,7 @@ int Sysdeps<Waitid>::operator()(idtype_t idtype, id_t id, siginfo_t *info, int o
820894

821895
int Sysdeps<Ptsname>::operator()(int fd, char *buffer, size_t length) {
822896
int index;
823-
if (int e = sysdep<Ioctl>(fd, TIOCGPTN, &index, NULL); e)
897+
if (int e = sysdep<Ioctl>(fd, TIOCGPTN, &index, nullptr); e)
824898
return e;
825899
if ((size_t)snprintf(buffer, length, "/dev/pts/%d", index) >= length) {
826900
return ERANGE;
@@ -838,7 +912,7 @@ int Sysdeps<IfIndextoname>::operator()(unsigned int index, char *name) {
838912
struct ifreq ifr;
839913
ifr.ifr_ifindex = index;
840914

841-
int ret = sysdep<Ioctl>(fd, SIOCGIFNAME, &ifr, NULL);
915+
int ret = sysdep<Ioctl>(fd, SIOCGIFNAME, &ifr, nullptr);
842916
close(fd);
843917

844918
if (ret) {
@@ -862,7 +936,7 @@ int Sysdeps<IfNametoindex>::operator()(const char *name, unsigned int *ret) {
862936
struct ifreq ifr;
863937
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
864938

865-
r = sysdep<Ioctl>(fd, SIOCGIFINDEX, &ifr, NULL);
939+
r = sysdep<Ioctl>(fd, SIOCGIFINDEX, &ifr, nullptr);
866940
close(fd);
867941

868942
if (r) {
@@ -913,7 +987,7 @@ int Sysdeps<Sysconf>::operator()(int num, long *ret) {
913987
}
914988

915989
int Sysdeps<Pause>::operator()() {
916-
return sysdep<Ppoll>(NULL, 0, NULL, NULL, NULL);
990+
return sysdep<Ppoll>(nullptr, 0, nullptr, nullptr, nullptr);
917991
}
918992

919993
} // namespace mlibc

sysdeps/zinnia/generic/thread.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
#include <mlibc/arch-defs.hpp>
44
#include <mlibc/tcb.hpp>
55
#include <sys/mman.h>
6+
#include <sys/resource.h>
67
#include <zinnia/syscall.hpp>
78

9+
extern "C" uintptr_t *__dlapi_entrystack();
10+
811
extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) {
912
if (mlibc::sysdep<TcbSet>(tcb))
1013
__ensure(!"failed to set tcb for new thread");
@@ -78,4 +81,22 @@ int Sysdeps<PrepareStack>::operator()(
7881
return 0;
7982
}
8083

84+
int Sysdeps<GetCurrentStackInfo>::operator()(void **stack_base, size_t *stack_size) {
85+
auto top = reinterpret_cast<uintptr_t>(__dlapi_entrystack());
86+
if (!top)
87+
return ESRCH;
88+
89+
size_t size = DEFAULT_STACK;
90+
struct rlimit rl;
91+
if (!sysdep<GetRlimit>(RLIMIT_STACK, &rl) && rl.rlim_cur && rl.rlim_cur != RLIM_INFINITY)
92+
size = rl.rlim_cur;
93+
94+
// Round up to the top of the mapping and report its lowest byte.
95+
top = (top + page_size - 1) & ~(page_size - 1);
96+
97+
*stack_base = reinterpret_cast<void *>(top - size);
98+
*stack_size = size;
99+
return 0;
100+
}
101+
81102
} // namespace mlibc

sysdeps/zinnia/include/mlibc/sysdeps.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct ZinniaSysdepTags :
2626
ThreadExit,
2727
ClockGet,
2828
ClockGetres,
29+
ClockSet,
2930
Flock,
3031
OpenDir,
3132
ReadEntries,
@@ -43,6 +44,8 @@ struct ZinniaSysdepTags :
4344
Readlinkat,
4445
Rmdir,
4546
Ftruncate,
47+
Truncate,
48+
Fadvise,
4649
Fallocate,
4750
Unlinkat,
4851
Socket,
@@ -79,6 +82,7 @@ struct ZinniaSysdepTags :
7982
Waitpid,
8083
Fork,
8184
Execve,
85+
Fexecve,
8286
Pselect,
8387
GetRusage,
8488
GetRlimit,
@@ -111,6 +115,10 @@ struct ZinniaSysdepTags :
111115
SetSid,
112116
Tcgetattr,
113117
Tcsetattr,
118+
Tcsendbreak,
119+
Tcdrain,
120+
Tcflow,
121+
Tcflush,
114122
Tcgetwinsize,
115123
Tcsetwinsize,
116124
Pipe,
@@ -121,6 +129,8 @@ struct ZinniaSysdepTags :
121129
Sigaction,
122130
Sigtimedwait,
123131
Kill,
132+
Sigqueue,
133+
Nice,
124134
GetHostname,
125135
SetHostname,
126136
Mkfifoat,
@@ -138,6 +148,8 @@ struct ZinniaSysdepTags :
138148
SetItimer,
139149
TimerCreate,
140150
TimerSettime,
151+
TimerGettime,
152+
TimerGetoverrun,
141153
TimerDelete,
142154
Uname,
143155
SetResuid,
@@ -155,6 +167,7 @@ struct ZinniaSysdepTags :
155167
IfNametoindex,
156168
Clone,
157169
PrepareStack,
170+
GetCurrentStackInfo,
158171
Reboot,
159172
Readv,
160173
Writev,

0 commit comments

Comments
 (0)