aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/sysdeps/keyronex/generic/generic.cpp
blob: a3e77fb9e8b92e68cdea2e0d520aac621b001ee9 (plain)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#include <asm/ioctls.h>

#include <bits/ensure.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <frg/logging.hpp>
#include <keyronex/syscall.h>
#include <limits.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/debug.hpp>
#include <stdlib.h>

#define STUB_ONLY                                           \
	{                                                   \
		__ensure(!"STUB_ONLY function was called"); \
		__builtin_unreachable();                    \
	}

namespace mlibc {

void
sys_libc_log(const char *message)
{
	syscall1(kPXSysDebug, (uintptr_t)message, NULL);
}

void
sys_libc_panic()
{
	sys_libc_log("\nMLIBC PANIC\n");
	for (;;)
		;
	STUB_ONLY
}

void
sys_exit(int status)
{
	syscall1(kPXSysExit, status, NULL);
	mlibc::panicLogger() << "sys_exit() returned!" << frg::endlog;
	__builtin_unreachable();
}

#ifndef MLIBC_BUILDING_RTDL
int
sys_tcgetattr(int fd, struct termios *attr)
{
	int ret;

	if (int r = sys_ioctl(fd, TCGETS, attr, &ret) != 0) {
		return r;
	}

	return 0;
}

int
sys_tcsetattr(int fd, int optional_action, const struct termios *attr)
{
	int ret;

	switch (optional_action) {
	case TCSANOW:
		optional_action = TCSETS;
		break;
	case TCSADRAIN:
		optional_action = TCSETSW;
		break;
	case TCSAFLUSH:
		optional_action = TCSETSF;
		break;
	default:
		__ensure(!"Unsupported tcsetattr");
	}

	if (int r = sys_ioctl(fd, optional_action, (void *)attr, &ret) != 0) {
		return r;
	}

	return 0;
}
#endif

int
sys_tcb_set(void *pointer)
{
	return syscall1(kPXSysSetFSBase, (uintptr_t)pointer, NULL);
}

int
sys_ppoll(struct pollfd *fds, int nfds, const struct timespec *timeout,
    const sigset_t *sigmask, int *num_events)
{
	uintptr_t ret = syscall4(kPXSysPPoll, (uintptr_t)fds, (uintptr_t)nfds,
	    (uintptr_t)timeout, (uintptr_t)sigmask, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*num_events = (ssize_t)ret;
	return 0;
}

int
sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events)
{
	struct timespec ts;
	ts.tv_sec = timeout / 1000;
	ts.tv_nsec = (timeout % 1000) * 1000000;
	return sys_ppoll(fds, count, timeout < 0 ? NULL : &ts, NULL,
	    num_events);
}

#ifndef MLIBC_BUILDING_RTDL
int
sys_pselect(int nfds, fd_set *read_set, fd_set *write_set, fd_set *except_set,
    const struct timespec *timeout, const sigset_t *sigmask, int *num_events)
{
	struct pollfd *fds = (struct pollfd *)malloc(
	    nfds * sizeof(struct pollfd));

	for (int i = 0; i < nfds; i++) {
		struct pollfd *fd = &fds[i];
		memset(fd, 0, sizeof(struct pollfd));

		if (read_set && FD_ISSET(i, read_set))
			fd->events |= POLLIN; // TODO: Additional events.
		if (write_set && FD_ISSET(i, write_set))
			fd->events |= POLLOUT; // TODO: Additional events.
		if (except_set && FD_ISSET(i, except_set))
			fd->events |= POLLPRI;

		if (!fd->events) {
			fd->fd = -1;
			continue;
		}

		fd->fd = i;
	}

	int e = sys_ppoll(fds, nfds, timeout, sigmask, num_events);

	if (e != 0) {
		free(fds);
		return e;
	}

	fd_set res_read_set;
	fd_set res_write_set;
	fd_set res_except_set;
	FD_ZERO(&res_read_set);
	FD_ZERO(&res_write_set);
	FD_ZERO(&res_except_set);

	for (int i = 0; i < nfds; i++) {
		struct pollfd *fd = &fds[i];

		if (read_set && FD_ISSET(i, read_set) &&
		    fd->revents & (POLLIN | POLLERR | POLLHUP)) {
			FD_SET(i, &res_read_set);
		}

		if (write_set && FD_ISSET(i, write_set) &&
		    fd->revents & (POLLOUT | POLLERR | POLLHUP)) {
			FD_SET(i, &res_write_set);
		}

		if (except_set && FD_ISSET(i, except_set) &&
		    fd->revents & POLLPRI) {
			FD_SET(i, &res_except_set);
		}
	}

	free(fds);

	if (read_set)
		memcpy(read_set, &res_read_set, sizeof(fd_set));
	if (write_set)
		memcpy(write_set, &res_write_set, sizeof(fd_set));
	if (except_set)
		memcpy(except_set, &res_except_set, sizeof(fd_set));

	return 0;
}
#endif

int
sys_fcntl(int fd, int request, va_list args, int *result)
{
	auto ret = syscall3(kPXSysFCntl, fd, request, va_arg(args, uint64_t),
	    NULL);
	if (int e = sc_error(ret); e)
		return e;
	*result = ret;
	return 0;
}

int
sys_futex_wait(int *pointer, int expected, const struct timespec *time)
{
	auto ret = syscall3(kPXSysFutexWait, (uintptr_t)pointer, expected,
	    (uintptr_t)time, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_futex_wake(int *pointer)
{
	auto ret = syscall1(kPXSysFutexWake, (uintptr_t)pointer, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

#ifndef MLIBC_BUILDING_RTDL
int
sys_ioctl(int fd, unsigned long request, void *arg, int *result)
{
	uintptr_t r = syscall3(kPXSysIOCtl, fd, request, (uintptr_t)arg, NULL);
	if (int e = sc_error(r); e)
		return e;
	*result = r;
	return 0;
}
#endif

int
sys_isatty(int fd)
{
	uintptr_t ret = syscall1(kPXSysIsATTY, fd, NULL);
	if (int e = sc_error(ret); e) {
		return e;
	}
	return 0;
}

#ifndef MLIBC_BUILDING_RTDL
int
sys_getcwd(char *buffer, size_t size)
{
	uintptr_t ret = syscall2(kPXSysGetCWD, (uintptr_t)buffer, size, NULL);
	if (int e = sc_error(ret); e) {
		return e;
	}
	return 0;
}
#endif

int
sys_dup(int fd, int flags, int *newfd)
{
	uintptr_t ret = syscall2(kPXSysDup, fd, flags, NULL);
	if (int e = sc_error(ret); e) {
		return e;
	}
	*newfd = ret;
	return 0;
}

int
sys_dup2(int fd, int flags, int newfd)
{
	uintptr_t ret = syscall3(kPXSysDup3, fd, newfd, flags, NULL);
	if (int e = sc_error(ret); e) {
		return e;
	}
	return 0;
}

int
sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd)
{
	uintptr_t r = syscall4(kPXSysOpenAt, dirfd, (uintptr_t)path,
	    (uintptr_t)flags, (uintptr_t)mode, NULL);
	if (int e = sc_error(r); e)
		return e;
	*fd = (int)r;
	return 0;
}

int
sys_open(const char *path, int flags, mode_t mode, int *fd)
{
	return sys_openat(AT_FDCWD, path, flags, mode, fd);
}

int
sys_open_dir(const char *path, int *handle)
{
	return sys_open(path, O_DIRECTORY, 0, handle);
}

int
sys_read_entries(int fd, void *buffer, size_t max_size, size_t *bytes_read)
{
	uintptr_t r = syscall3(kPXSysReadDir, fd, (uintptr_t)buffer, max_size,
	    NULL);

	if (int e = sc_error(r); e)
		return e;

	*bytes_read = r;
	return 0;
}

int
sys_close(int fd)
{
	return (int)syscall1(kPXSysClose, fd, NULL);
}

int
sys_link(const char *old_path, const char *new_path)
{
	uintptr_t ret = syscall2(kPXSysLink, (uintptr_t)old_path,
	    (uintptr_t)new_path, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_seek(int fd, off_t offset, int whence, off_t *new_offset)
{
	uintptr_t ret = syscall3(kPXSysSeek, fd, offset, whence, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*new_offset = (ssize_t)ret;
	return 0;
}

int
sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read)
{
	uintptr_t ret = syscall3(kPXSysRead, fd, (uintptr_t)buf,
	    (uintptr_t)count, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*bytes_read = (ssize_t)ret;
	return 0;
}

int
sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written)
{
	uintptr_t ret = syscall3(kPXSysWrite, fd, (uintptr_t)buf,
	    (uintptr_t)count, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*bytes_written = (ssize_t)ret;
	return 0;
}

int
sys_readlink(const char *path, void *buffer, size_t max_size, ssize_t *length)
{
	uintptr_t ret = syscall3(kPXSysReadLink, (uintptr_t)path,
	    (uintptr_t)buffer, (uintptr_t)max_size, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*length = (ssize_t)ret;
	return 0;
}

int
sys_pipe(int *fds, int flags)
{
	uintptr_t ret = syscall2(kPXSysPipe, (uintptr_t)fds, flags, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_unlinkat(int fd, const char *path, int flags)
{
	uintptr_t ret = syscall3(kPXSysUnlinkAt, fd, (uintptr_t)path, flags,
	    NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offset,
    void **window)
{
	uintptr_t r = syscall6(kPXSysMmap, (uintptr_t)hint, size, prot, flags,
	    fd, offset, NULL);
	if (int e = sc_error(r); e)
		return e;
	*window = (void *)r;
	return 0;
}

int
sys_vm_unmap(void *pointer, size_t size)
{
	uintptr_t r = syscall2(kPXSysMunmap, (uintptr_t)pointer, size, NULL);
	if (int e = sc_error(r); e)
		return e;
	return 0;
}

int
sys_vm_protect(void *pointer, size_t size, int prot)
{

	mlibc::infoLogger() << "mlibc: sys_vm_protect(" << pointer << ", "
			    << size << ", " << prot << "); stub!\n"
			    << frg::endlog;
	return 0;
}

int
sys_anon_allocate(size_t size, void **pointer)
{
	return sys_vm_map(NULL, size, PROT_EXEC | PROT_READ | PROT_WRITE,
	    MAP_ANONYMOUS, -1, 0, pointer);
}

int
sys_anon_free(void *pointer, size_t size)
{
	return sys_vm_unmap(pointer, size);
}

pid_t
sys_getpid()
{
	return syscall0(kPXSysGetPID, NULL);
}

pid_t
sys_getppid()
{
	return syscall0(kPXSysGetPPID, NULL);
}

uid_t
sys_getuid()
{
	return 0;
}

uid_t
sys_geteuid()
{
	return 0;
}

gid_t
sys_getgid()
{
	return 0;
}

int
sys_getsid(pid_t pid, pid_t *sid)
{
	auto ret = syscall1(kPXSysGetSID, pid, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*sid = (pid_t)(ret);
	return 0;
}

int
sys_setgid(gid_t gid)
{
	(void)gid;
	return 0;
}

int
sys_getpgid(pid_t pid, pid_t *out)
{
	auto ret = syscall1(kPXSysGetPGID, pid, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*out = (pid_t)(ret);
	return 0;
}

int
sys_setpgid(pid_t pid, pid_t pgid)
{
	auto ret = syscall2(kPXSysSetPGID, pid, pgid, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_setsid(pid_t *sid)
{
	auto ret = syscall0(kPXSysSetSID, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*sid = (pid_t)ret;
	return 0;
}

gid_t
sys_getegid()
{
	mlibc::infoLogger() << "mlibc: " << __func__ << " is a stub!\n"
			    << frg::endlog;
	return 0;
}

pid_t
sys_gettid()
{
	return syscall0(kPXSysGetTID, NULL);
}

int
sys_clock_get(int clock, time_t *secs, long *nanos)
{
	auto ret = syscall1(kPXSysClockGet, clock, NULL);
	*secs = ret / 1000000000;
	*nanos = ret % 1000000000;
	return 0;
}

int
sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags,
    struct stat *statbuf)
{
	uintptr_t r;
	enum posix_stat_kind kind;

	switch (fsfdt) {
	case fsfd_target::fd:
		kind = kPXStatKindFD;
		break;

	case fsfd_target::path:
		kind = kPXStatKindCWD;
		break;
	case fsfd_target::fd_path:
		kind = kPXStatKindAt;
		break;

	default:
		__ensure(!"stat: Invalid fsfdt");
		__builtin_unreachable();
	}

	r = syscall5(kPXSysStat, kind, fd, (uintptr_t)path, flags,
	    (uintptr_t)statbuf, NULL);
	if (int e = sc_error(r); e)
		return e;
	return 0;
}

int
sys_statfs(const char *path, struct statfs *buf)
{
	uintptr_t ret = syscall2(kPXSysStatFS, (uintptr_t)path, (uintptr_t)buf,
	    NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_statvfs(const char *path, struct statvfs *buf)
{
	struct statfs sb;
	uintptr_t ret = syscall2(kPXSysStatFS, (uintptr_t)path, (uintptr_t)&sb,
	    NULL);
	if (int e = sc_error(ret); e)
		return e;

	buf->f_bsize = sb.f_bsize;
	buf->f_frsize = sb.f_frsize;
	buf->f_blocks = sb.f_blocks;
	buf->f_bfree = sb.f_bfree;
	buf->f_bavail = sb.f_bavail;

	buf->f_files = sb.f_files;
	buf->f_ffree = sb.f_ffree;
	buf->f_favail = sb.f_ffree;

	buf->f_fsid = sb.f_fsid.__val[1] | (uint64_t)sb.f_fsid.__val[0] << 32;
	buf->f_flag = sb.f_flags;
	buf->f_namemax = sb.f_namelen;

	return 0;
}

int
sys_faccessat(int dirfd, const char *pathname, int mode, int flags)
{
	(void)flags;
	struct stat buf;
	if (int r = sys_stat(dirfd == AT_FDCWD ? fsfd_target::path :
						 fsfd_target::fd_path,
		dirfd, pathname, mode & AT_SYMLINK_FOLLOW, &buf)) {
		return r;
	}
	return 0;
}

int
sys_access(const char *path, int mode)
{
	return sys_faccessat(AT_FDCWD, path, mode, 0);
}

int
sys_fork(pid_t *child)
{
	uintptr_t ret = syscall0(kPXSysFork, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*child = (int)ret;
	return 0;
}

int
sys_execve(const char *path, char *const argv[], char *const envp[])
{
	uintptr_t ret = syscall3(kPXSysExecVE, (uintptr_t)path, (uintptr_t)argv,
	    (uintptr_t)envp, NULL);
	if (int e = sc_error(ret); e)
		return e;
	mlibc::panicLogger() << "execve returned! " << ret << frg::endlog;
	__builtin_unreachable();
}

int
sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru,
    pid_t *ret_pid)
{
	(void)ru;

	uintptr_t ret = syscall3(kPXSysWaitPID, pid, (uintptr_t)status, flags,
	    NULL);
	if (int e = sc_error(ret); e)
		return e;
	*ret_pid = (pid_t)ret;
	return 0;
}

#ifndef MLIBC_BUILDING_RTDL
int
sys_sleep(time_t *sec, long *nanosec)
{
	auto ret = syscall1(kPXSysSleep, *sec * 1000000000 + *nanosec, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_uname(struct utsname *buf)
{
	uintptr_t ret = syscall1(kPXSysUTSName, (uintptr_t)buf, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_gethostname(char *buf, size_t bufsize)
{
	struct utsname uname_buf;
	if (auto e = sys_uname(&uname_buf); e)
		return e;

	auto node_len = strlen(uname_buf.nodename);
	if (node_len >= bufsize)
		return ENAMETOOLONG;

	memcpy(buf, uname_buf.nodename, node_len);
	buf[node_len] = '\0';
	return 0;
}

int
sys_fsync(int)
{
	mlibc::infoLogger() << "mlibc: fsync is a stub" << frg::endlog;
	return 0;
}

int
sys_getentropy(void *buffer, size_t length)
{
	/* todo: improve lmao */
	mlibc::infoLogger() << "mlibc: getentropy is a stub" << frg::endlog;
	memset(buffer, 123, length);
	return 0;
}
#endif

int
sys_mkdir(const char *path, mode_t mode)
{
	return sys_mkdirat(AT_FDCWD, path, mode);
}

int
sys_mkdirat(int dirfd, const char *path, mode_t mode)
{
	uintptr_t ret = syscall3(kPXSysMkDirAt, dirfd, (uintptr_t)path, mode,
	    NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_chdir(const char *path)
{
	uintptr_t ret = syscall1(kPXSysChDir, (uintptr_t)path, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

int
sys_umask(mode_t mode, mode_t *old)
{
	uintptr_t ret = syscall1(kPXSysUMask, mode, NULL);
	if (int e = sc_error(ret); e)
		return e;
	*old = (mode_t)ret;
	return 0;
}

int
sys_rename(const char *old_path, const char *new_path)
{
	return sys_renameat(AT_FDCWD, old_path, AT_FDCWD, new_path);
}

int
sys_renameat(int old_dirfd, const char *old_path, int new_dirfd,
    const char *new_path)
{
	auto ret = syscall4(kPXSysRenameAt, old_dirfd, (uintptr_t)old_path,
	    new_dirfd, (uintptr_t)new_path, NULL);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

} // namespace mlibc