xref: /freebsd/stand/kboot/libkboot/host_syscalls.c (revision 8f7327dceecc225029b17378e4a26ae0b73a0a49)
1*8f7327dcSWarner Losh /*
2*8f7327dcSWarner Losh  * Copyright (c) 2022-2024, Netflix, Inc.
3*8f7327dcSWarner Losh  *
4*8f7327dcSWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5*8f7327dcSWarner Losh  */
6*8f7327dcSWarner Losh 
72e3f4988SWarner Losh #include "host_syscall.h"
82e3f4988SWarner Losh #include "syscall_nr.h"
92e3f4988SWarner Losh #include <stand.h>
102e3f4988SWarner Losh 
112e3f4988SWarner Losh /*
122e3f4988SWarner Losh  * Various trivial wrappers for Linux system calls. Please keep sorted
132e3f4988SWarner Losh  * alphabetically.
142e3f4988SWarner Losh  */
152e3f4988SWarner Losh 
162e3f4988SWarner Losh int
host_close(int fd)172e3f4988SWarner Losh host_close(int fd)
182e3f4988SWarner Losh {
192e3f4988SWarner Losh 	return host_syscall(SYS_close, fd);
202e3f4988SWarner Losh }
212e3f4988SWarner Losh 
222e3f4988SWarner Losh int
host_dup(int fd)232e3f4988SWarner Losh host_dup(int fd)
242e3f4988SWarner Losh {
252e3f4988SWarner Losh 	return host_syscall(SYS_dup, fd);
262e3f4988SWarner Losh }
272e3f4988SWarner Losh 
282e3f4988SWarner Losh int
host_exit(int code)292e3f4988SWarner Losh host_exit(int code)
302e3f4988SWarner Losh {
312e3f4988SWarner Losh 	return host_syscall(SYS_exit, code);
322e3f4988SWarner Losh }
332e3f4988SWarner Losh 
342e3f4988SWarner Losh /* Same system call with different names on different Linux architectures due to history */
352e3f4988SWarner Losh int
host_fstat(int fd,struct host_kstat * sb)362e3f4988SWarner Losh host_fstat(int fd, struct host_kstat *sb)
372e3f4988SWarner Losh {
382e3f4988SWarner Losh #ifdef SYS_newfstat
392e3f4988SWarner Losh 	return host_syscall(SYS_newfstat, fd, (uintptr_t)sb);
402e3f4988SWarner Losh #else
412e3f4988SWarner Losh 	return host_syscall(SYS_fstat, fd, (uintptr_t)sb);
422e3f4988SWarner Losh #endif
432e3f4988SWarner Losh }
442e3f4988SWarner Losh 
452e3f4988SWarner Losh int
host_getdents64(int fd,void * dirp,int count)462e3f4988SWarner Losh host_getdents64(int fd, void *dirp, int count)
472e3f4988SWarner Losh {
482e3f4988SWarner Losh 	return host_syscall(SYS_getdents64, fd, (uintptr_t)dirp, count);
492e3f4988SWarner Losh }
502e3f4988SWarner Losh 
512e3f4988SWarner Losh int
host_getpid(void)522e3f4988SWarner Losh host_getpid(void)
532e3f4988SWarner Losh {
542e3f4988SWarner Losh 	return host_syscall(SYS_getpid);
552e3f4988SWarner Losh }
562e3f4988SWarner Losh 
572e3f4988SWarner Losh int
host_gettimeofday(struct host_timeval * a,void * b)582e3f4988SWarner Losh host_gettimeofday(struct host_timeval *a, void *b)
592e3f4988SWarner Losh {
602e3f4988SWarner Losh 	return host_syscall(SYS_gettimeofday, (uintptr_t)a, (uintptr_t)b);
612e3f4988SWarner Losh }
622e3f4988SWarner Losh 
632e3f4988SWarner Losh int
host_ioctl(int fd,unsigned long request,unsigned long arg)642e3f4988SWarner Losh host_ioctl(int fd, unsigned long request, unsigned long arg)
652e3f4988SWarner Losh {
662e3f4988SWarner Losh 	return host_syscall(SYS_ioctl, fd, request, arg);
672e3f4988SWarner Losh }
682e3f4988SWarner Losh 
692e3f4988SWarner Losh ssize_t
host_llseek(int fd,int32_t offset_high,int32_t offset_lo,uint64_t * result,int whence)702e3f4988SWarner Losh host_llseek(int fd, int32_t offset_high, int32_t offset_lo, uint64_t *result, int whence)
712e3f4988SWarner Losh {
722e3f4988SWarner Losh #ifdef SYS_llseek
732e3f4988SWarner Losh 	return host_syscall(SYS_llseek, fd, offset_high, offset_lo, (uintptr_t)result, whence);
742e3f4988SWarner Losh #else
752e3f4988SWarner Losh 	int64_t rv = host_syscall(SYS_lseek, fd,
762e3f4988SWarner Losh 	    (int64_t)((uint64_t)offset_high << 32 | (uint32_t)offset_lo), whence);
772e3f4988SWarner Losh 	if (rv > 0)
782e3f4988SWarner Losh 		*result = (uint64_t)rv;
792e3f4988SWarner Losh 	return (rv);
802e3f4988SWarner Losh #endif
812e3f4988SWarner Losh }
822e3f4988SWarner Losh 
832e3f4988SWarner Losh int
host_kexec_load(unsigned long entry,unsigned long nsegs,struct host_kexec_segment * segs,unsigned long flags)842e3f4988SWarner Losh host_kexec_load(unsigned long entry, unsigned long nsegs, struct host_kexec_segment *segs, unsigned long flags)
852e3f4988SWarner Losh {
862e3f4988SWarner Losh 	return host_syscall(SYS_kexec_load, entry, nsegs, segs, flags);
872e3f4988SWarner Losh }
882e3f4988SWarner Losh 
892e3f4988SWarner Losh int
host_mkdir(const char * path,host_mode_t mode)902e3f4988SWarner Losh host_mkdir(const char *path, host_mode_t mode)
912e3f4988SWarner Losh {
922e3f4988SWarner Losh 	return host_syscall(SYS_mkdirat, HOST_AT_FDCWD, (uintptr_t)path, mode);
932e3f4988SWarner Losh }
942e3f4988SWarner Losh 
952e3f4988SWarner Losh void *
host_mmap(void * addr,size_t len,int prot,int flags,int fd,off_t off)962e3f4988SWarner Losh host_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
972e3f4988SWarner Losh {
982e3f4988SWarner Losh 	return (void *)host_syscall(SYS_mmap, (uintptr_t)addr, len, prot, flags, fd, off);
992e3f4988SWarner Losh }
1002e3f4988SWarner Losh 
1012e3f4988SWarner Losh int
host_mount(const char * src,const char * target,const char * type,unsigned long flags,void * data)1022e3f4988SWarner Losh host_mount(const char *src, const char *target, const char *type, unsigned long flags,
1032e3f4988SWarner Losh     void *data)
1042e3f4988SWarner Losh {
1052e3f4988SWarner Losh 	return host_syscall(SYS_mount, src, target, type, flags, data);
1062e3f4988SWarner Losh }
1072e3f4988SWarner Losh 
1082e3f4988SWarner Losh int
host_munmap(void * addr,size_t len)1092e3f4988SWarner Losh host_munmap(void *addr, size_t len)
1102e3f4988SWarner Losh {
1112e3f4988SWarner Losh 	return host_syscall(SYS_munmap, (uintptr_t)addr, len);
1122e3f4988SWarner Losh }
1132e3f4988SWarner Losh 
1142e3f4988SWarner Losh int
host_open(const char * path,int flags,int mode)1152e3f4988SWarner Losh host_open(const char *path, int flags, int mode)
1162e3f4988SWarner Losh {
1172e3f4988SWarner Losh 	return host_syscall(SYS_openat, HOST_AT_FDCWD, (uintptr_t)path, flags, mode);
1182e3f4988SWarner Losh 	/* XXX original overrode errors */
1192e3f4988SWarner Losh }
1202e3f4988SWarner Losh 
1212e3f4988SWarner Losh ssize_t
host_read(int fd,void * buf,size_t nbyte)1222e3f4988SWarner Losh host_read(int fd, void *buf, size_t nbyte)
1232e3f4988SWarner Losh {
1242e3f4988SWarner Losh 	return host_syscall(SYS_read, fd, (uintptr_t)buf, nbyte);
1252e3f4988SWarner Losh 	/* XXX original overrode errors */
1262e3f4988SWarner Losh }
1272e3f4988SWarner Losh 
1282e3f4988SWarner Losh int
host_reboot(int magic1,int magic2,int cmd,uintptr_t arg)1292e3f4988SWarner Losh host_reboot(int magic1, int magic2, int cmd, uintptr_t arg)
1302e3f4988SWarner Losh {
1312e3f4988SWarner Losh 	return host_syscall(SYS_reboot, magic1, magic2, cmd, arg);
1322e3f4988SWarner Losh }
1332e3f4988SWarner Losh 
1342e3f4988SWarner Losh int
host_select(int nfds,long * readfds,long * writefds,long * exceptfds,struct host_timeval * timeout)1352e3f4988SWarner Losh host_select(int nfds, long *readfds, long *writefds, long *exceptfds,
1362e3f4988SWarner Losh     struct host_timeval *timeout)
1372e3f4988SWarner Losh {
1382e3f4988SWarner Losh 	struct timespec ts = { .tv_sec = timeout->tv_sec, .tv_nsec = timeout->tv_usec * 1000 };
1392e3f4988SWarner Losh 
1402e3f4988SWarner Losh 	/*
1412e3f4988SWarner Losh 	 * Note, final arg is a sigset_argpack since most arch can only have 6
1422e3f4988SWarner Losh 	 * syscall args. Since we're not masking signals, though, we can just
1432e3f4988SWarner Losh 	 * pass a NULL.
1442e3f4988SWarner Losh 	 */
1452e3f4988SWarner Losh 	return host_syscall(SYS_pselect6, nfds, (uintptr_t)readfds, (uintptr_t)writefds,
1462e3f4988SWarner Losh 	    (uintptr_t)exceptfds, (uintptr_t)&ts, (uintptr_t)NULL);
1472e3f4988SWarner Losh }
1482e3f4988SWarner Losh 
1492e3f4988SWarner Losh int
host_stat(const char * path,struct host_kstat * sb)1502e3f4988SWarner Losh host_stat(const char *path, struct host_kstat *sb)
1512e3f4988SWarner Losh {
1522e3f4988SWarner Losh 	return host_syscall(SYS_newfstatat, HOST_AT_FDCWD, (uintptr_t)path, (uintptr_t)sb, 0);
1532e3f4988SWarner Losh }
1542e3f4988SWarner Losh 
1552e3f4988SWarner Losh int
host_symlink(const char * path1,const char * path2)1562e3f4988SWarner Losh host_symlink(const char *path1, const char *path2)
1572e3f4988SWarner Losh {
1582e3f4988SWarner Losh 	return host_syscall(SYS_symlinkat, HOST_AT_FDCWD, path1, path2);
1592e3f4988SWarner Losh }
1602e3f4988SWarner Losh 
1612e3f4988SWarner Losh int
host_uname(struct old_utsname * uts)1622e3f4988SWarner Losh host_uname(struct old_utsname *uts)
1632e3f4988SWarner Losh {
1642e3f4988SWarner Losh 	return host_syscall(SYS_uname, (uintptr_t)uts);
1652e3f4988SWarner Losh }
1662e3f4988SWarner Losh 
1672e3f4988SWarner Losh ssize_t
host_write(int fd,const void * buf,size_t nbyte)1682e3f4988SWarner Losh host_write(int fd, const void *buf, size_t nbyte)
1692e3f4988SWarner Losh {
1702e3f4988SWarner Losh 	return host_syscall(SYS_write, fd, (uintptr_t)buf, nbyte);
1712e3f4988SWarner Losh }
172