xref: /freebsd/stand/kboot/libkboot/host_syscalls.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1*2e3f4988SWarner Losh #include "host_syscall.h"
2*2e3f4988SWarner Losh #include "syscall_nr.h"
3*2e3f4988SWarner Losh #include <stand.h>
4*2e3f4988SWarner Losh 
5*2e3f4988SWarner Losh /*
6*2e3f4988SWarner Losh  * Various trivial wrappers for Linux system calls. Please keep sorted
7*2e3f4988SWarner Losh  * alphabetically.
8*2e3f4988SWarner Losh  */
9*2e3f4988SWarner Losh 
10*2e3f4988SWarner Losh int
11*2e3f4988SWarner Losh host_close(int fd)
12*2e3f4988SWarner Losh {
13*2e3f4988SWarner Losh 	return host_syscall(SYS_close, fd);
14*2e3f4988SWarner Losh }
15*2e3f4988SWarner Losh 
16*2e3f4988SWarner Losh int
17*2e3f4988SWarner Losh host_dup(int fd)
18*2e3f4988SWarner Losh {
19*2e3f4988SWarner Losh 	return host_syscall(SYS_dup, fd);
20*2e3f4988SWarner Losh }
21*2e3f4988SWarner Losh 
22*2e3f4988SWarner Losh int
23*2e3f4988SWarner Losh host_exit(int code)
24*2e3f4988SWarner Losh {
25*2e3f4988SWarner Losh 	return host_syscall(SYS_exit, code);
26*2e3f4988SWarner Losh }
27*2e3f4988SWarner Losh 
28*2e3f4988SWarner Losh /* Same system call with different names on different Linux architectures due to history */
29*2e3f4988SWarner Losh int
30*2e3f4988SWarner Losh host_fstat(int fd, struct host_kstat *sb)
31*2e3f4988SWarner Losh {
32*2e3f4988SWarner Losh #ifdef SYS_newfstat
33*2e3f4988SWarner Losh 	return host_syscall(SYS_newfstat, fd, (uintptr_t)sb);
34*2e3f4988SWarner Losh #else
35*2e3f4988SWarner Losh 	return host_syscall(SYS_fstat, fd, (uintptr_t)sb);
36*2e3f4988SWarner Losh #endif
37*2e3f4988SWarner Losh }
38*2e3f4988SWarner Losh 
39*2e3f4988SWarner Losh int
40*2e3f4988SWarner Losh host_getdents64(int fd, void *dirp, int count)
41*2e3f4988SWarner Losh {
42*2e3f4988SWarner Losh 	return host_syscall(SYS_getdents64, fd, (uintptr_t)dirp, count);
43*2e3f4988SWarner Losh }
44*2e3f4988SWarner Losh 
45*2e3f4988SWarner Losh int
46*2e3f4988SWarner Losh host_getpid(void)
47*2e3f4988SWarner Losh {
48*2e3f4988SWarner Losh 	return host_syscall(SYS_getpid);
49*2e3f4988SWarner Losh }
50*2e3f4988SWarner Losh 
51*2e3f4988SWarner Losh int
52*2e3f4988SWarner Losh host_gettimeofday(struct host_timeval *a, void *b)
53*2e3f4988SWarner Losh {
54*2e3f4988SWarner Losh 	return host_syscall(SYS_gettimeofday, (uintptr_t)a, (uintptr_t)b);
55*2e3f4988SWarner Losh }
56*2e3f4988SWarner Losh 
57*2e3f4988SWarner Losh int
58*2e3f4988SWarner Losh host_ioctl(int fd, unsigned long request, unsigned long arg)
59*2e3f4988SWarner Losh {
60*2e3f4988SWarner Losh 	return host_syscall(SYS_ioctl, fd, request, arg);
61*2e3f4988SWarner Losh }
62*2e3f4988SWarner Losh 
63*2e3f4988SWarner Losh ssize_t
64*2e3f4988SWarner Losh host_llseek(int fd, int32_t offset_high, int32_t offset_lo, uint64_t *result, int whence)
65*2e3f4988SWarner Losh {
66*2e3f4988SWarner Losh #ifdef SYS_llseek
67*2e3f4988SWarner Losh 	return host_syscall(SYS_llseek, fd, offset_high, offset_lo, (uintptr_t)result, whence);
68*2e3f4988SWarner Losh #else
69*2e3f4988SWarner Losh 	int64_t rv = host_syscall(SYS_lseek, fd,
70*2e3f4988SWarner Losh 	    (int64_t)((uint64_t)offset_high << 32 | (uint32_t)offset_lo), whence);
71*2e3f4988SWarner Losh 	if (rv > 0)
72*2e3f4988SWarner Losh 		*result = (uint64_t)rv;
73*2e3f4988SWarner Losh 	return (rv);
74*2e3f4988SWarner Losh #endif
75*2e3f4988SWarner Losh }
76*2e3f4988SWarner Losh 
77*2e3f4988SWarner Losh int
78*2e3f4988SWarner Losh host_kexec_load(unsigned long entry, unsigned long nsegs, struct host_kexec_segment *segs, unsigned long flags)
79*2e3f4988SWarner Losh {
80*2e3f4988SWarner Losh 	return host_syscall(SYS_kexec_load, entry, nsegs, segs, flags);
81*2e3f4988SWarner Losh }
82*2e3f4988SWarner Losh 
83*2e3f4988SWarner Losh int
84*2e3f4988SWarner Losh host_mkdir(const char *path, host_mode_t mode)
85*2e3f4988SWarner Losh {
86*2e3f4988SWarner Losh 	return host_syscall(SYS_mkdirat, HOST_AT_FDCWD, (uintptr_t)path, mode);
87*2e3f4988SWarner Losh }
88*2e3f4988SWarner Losh 
89*2e3f4988SWarner Losh void *
90*2e3f4988SWarner Losh host_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
91*2e3f4988SWarner Losh {
92*2e3f4988SWarner Losh 	return (void *)host_syscall(SYS_mmap, (uintptr_t)addr, len, prot, flags, fd, off);
93*2e3f4988SWarner Losh }
94*2e3f4988SWarner Losh 
95*2e3f4988SWarner Losh int
96*2e3f4988SWarner Losh host_mount(const char *src, const char *target, const char *type, unsigned long flags,
97*2e3f4988SWarner Losh     void *data)
98*2e3f4988SWarner Losh {
99*2e3f4988SWarner Losh 	return host_syscall(SYS_mount, src, target, type, flags, data);
100*2e3f4988SWarner Losh }
101*2e3f4988SWarner Losh 
102*2e3f4988SWarner Losh int
103*2e3f4988SWarner Losh host_munmap(void *addr, size_t len)
104*2e3f4988SWarner Losh {
105*2e3f4988SWarner Losh 	return host_syscall(SYS_munmap, (uintptr_t)addr, len);
106*2e3f4988SWarner Losh }
107*2e3f4988SWarner Losh 
108*2e3f4988SWarner Losh int
109*2e3f4988SWarner Losh host_open(const char *path, int flags, int mode)
110*2e3f4988SWarner Losh {
111*2e3f4988SWarner Losh 	return host_syscall(SYS_openat, HOST_AT_FDCWD, (uintptr_t)path, flags, mode);
112*2e3f4988SWarner Losh 	/* XXX original overrode errors */
113*2e3f4988SWarner Losh }
114*2e3f4988SWarner Losh 
115*2e3f4988SWarner Losh ssize_t
116*2e3f4988SWarner Losh host_read(int fd, void *buf, size_t nbyte)
117*2e3f4988SWarner Losh {
118*2e3f4988SWarner Losh 	return host_syscall(SYS_read, fd, (uintptr_t)buf, nbyte);
119*2e3f4988SWarner Losh 	/* XXX original overrode errors */
120*2e3f4988SWarner Losh }
121*2e3f4988SWarner Losh 
122*2e3f4988SWarner Losh int
123*2e3f4988SWarner Losh host_reboot(int magic1, int magic2, int cmd, uintptr_t arg)
124*2e3f4988SWarner Losh {
125*2e3f4988SWarner Losh 	return host_syscall(SYS_reboot, magic1, magic2, cmd, arg);
126*2e3f4988SWarner Losh }
127*2e3f4988SWarner Losh 
128*2e3f4988SWarner Losh int
129*2e3f4988SWarner Losh host_select(int nfds, long *readfds, long *writefds, long *exceptfds,
130*2e3f4988SWarner Losh     struct host_timeval *timeout)
131*2e3f4988SWarner Losh {
132*2e3f4988SWarner Losh 	struct timespec ts = { .tv_sec = timeout->tv_sec, .tv_nsec = timeout->tv_usec * 1000 };
133*2e3f4988SWarner Losh 
134*2e3f4988SWarner Losh 	/*
135*2e3f4988SWarner Losh 	 * Note, final arg is a sigset_argpack since most arch can only have 6
136*2e3f4988SWarner Losh 	 * syscall args. Since we're not masking signals, though, we can just
137*2e3f4988SWarner Losh 	 * pass a NULL.
138*2e3f4988SWarner Losh 	 */
139*2e3f4988SWarner Losh 	return host_syscall(SYS_pselect6, nfds, (uintptr_t)readfds, (uintptr_t)writefds,
140*2e3f4988SWarner Losh 	    (uintptr_t)exceptfds, (uintptr_t)&ts, (uintptr_t)NULL);
141*2e3f4988SWarner Losh }
142*2e3f4988SWarner Losh 
143*2e3f4988SWarner Losh int
144*2e3f4988SWarner Losh host_stat(const char *path, struct host_kstat *sb)
145*2e3f4988SWarner Losh {
146*2e3f4988SWarner Losh 	return host_syscall(SYS_newfstatat, HOST_AT_FDCWD, (uintptr_t)path, (uintptr_t)sb, 0);
147*2e3f4988SWarner Losh }
148*2e3f4988SWarner Losh 
149*2e3f4988SWarner Losh int
150*2e3f4988SWarner Losh host_symlink(const char *path1, const char *path2)
151*2e3f4988SWarner Losh {
152*2e3f4988SWarner Losh 	return host_syscall(SYS_symlinkat, HOST_AT_FDCWD, path1, path2);
153*2e3f4988SWarner Losh }
154*2e3f4988SWarner Losh 
155*2e3f4988SWarner Losh int
156*2e3f4988SWarner Losh host_uname(struct old_utsname *uts)
157*2e3f4988SWarner Losh {
158*2e3f4988SWarner Losh 	return host_syscall(SYS_uname, (uintptr_t)uts);
159*2e3f4988SWarner Losh }
160*2e3f4988SWarner Losh 
161*2e3f4988SWarner Losh ssize_t
162*2e3f4988SWarner Losh host_write(int fd, const void *buf, size_t nbyte)
163*2e3f4988SWarner Losh {
164*2e3f4988SWarner Losh 	return host_syscall(SYS_write, fd, (uintptr_t)buf, nbyte);
165*2e3f4988SWarner Losh }
166