xref: /freebsd-13-stable/stand/kboot/host_syscall.h (revision 17da660ad5b3b9cd90e164dd4dbb9beaa7203054)
1 /*-
2  * Copyright (C) 2014 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef _HOST_SYSCALL_H
27 #define _HOST_SYSCALL_H
28 
29 #include <stand.h>
30 
31 long host_syscall(int number, ...);
32 
33 /*
34  * Sizes taken from musl's include/alltypes.h.in and expanded for LP64 hosts
35  */
36 typedef uint64_t host_dev_t;
37 typedef uint64_t host_ino_t;
38 typedef unsigned int host_mode_t;
39 typedef unsigned int host_uid_t;
40 typedef unsigned int host_gid_t;
41 typedef int64_t host_off_t;
42 typedef long host_blksize_t;
43 typedef int64_t host_blkcnt_t;
44 
45 #include "stat_arch.h"
46 
47 /*
48  * stat flags
49  * These are arch independent and match the values in nolib and uapi headers
50  * with HOST_ prepended.
51  */
52 #define	HOST_S_IFMT	0170000
53 #define	HOST_S_IFIFO	0010000
54 #define	HOST_S_IFCHR	0020000
55 #define	HOST_S_IFDIR	0040000
56 #define	HOST_S_IFBLK	0060000
57 #define	HOST_S_IFREG	0100000
58 #define	HOST_S_IFLNK	0120000
59 #define	HOST_S_IFSOCK	0140000
60 
61 #define	HOST_S_ISBLK(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFBLK)
62 #define	HOST_S_ISCHR(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFCHR)
63 #define	HOST_S_ISDIR(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFDIR)
64 #define	HOST_S_ISFIFO(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFIFO)
65 #define	HOST_S_ISLNK(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFLNK)
66 #define	HOST_S_ISREG(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFREG)
67 #define	HOST_S_ISSOCK(mode)	(((mode) & HOST_S_IFMT) == HOST_S_IFSOCK)
68 
69 /*
70  * Constants for open, fcntl, etc
71  *
72  * Note: Some of these are arch dependent on Linux, but are the same for
73  * powerpc, x86, arm*, and riscv. We should be futureproof, though, since these
74  * are the 'generic' values and only older architectures (no longer supported by
75  * FreeBSD) vary.
76  *
77  * These are from tools/include/uapi/asm-generic/fcntl.h and use the octal
78  * notation. Beware, hex is used in other places creating potential confsion.
79  */
80 #define HOST_O_RDONLY		    0
81 #define HOST_O_WRONLY		    1
82 #define HOST_O_RDWR		    2
83 #define HOST_O_CREAT		00100
84 #define HOST_O_EXCL		00200
85 #define HOST_O_NOCTTY		00400
86 #define HOST_O_TRUNC		01000
87 #define HOST_O_APPEND		02000
88 #define HOST_O_NONBLOCK		04000
89 
90 #define HOST_AT_FDCWD		-100            /* Relative to current directory */
91 
92 /*
93  * Data types
94  */
95 struct old_utsname {
96 	char sysname[65];
97 	char nodename[65];
98 	char release[65];
99 	char version[65];
100 	char machine[65];
101 };
102 
103 struct host_timeval {
104 	time_t tv_sec;
105 	long tv_usec;
106 };
107 
108 /*
109  * Must match Linux's values see linux/tools/include/uapi/asm-generic/mman-common.h
110  * and linux/tools/include/linux/mman.h
111  *
112  * And pre-pend HOST_ here.
113  */
114 #define HOST_PROT_READ	0x1
115 #define HOST_PROT_WRITE	0x2
116 #define HOST_PROT_EXEC	0x4
117 
118 #define HOST_MAP_SHARED		0x01
119 #define	HOST_MAP_PRIVATE	0x02
120 #define HOST_MAP_FIXED		0x10
121 #define HOST_MAP_ANONYMOUS	0x20
122 
123 /* Mount flags from uapi */
124 #define MS_RELATIME (1 << 21)
125 
126 #define HOST_REBOOT_MAGIC1	0xfee1dead
127 #define HOST_REBOOT_MAGIC2	672274793
128 #define HOST_REBOOT_CMD_KEXEC	0x45584543
129 
130 /*
131  * Values from linux/tools/include/uapi/linux/kexec.h
132  */
133 
134 /*
135  * Values match ELF architecture types.
136  */
137 #define HOST_KEXEC_ARCH_X86_64  (62 << 16)
138 #define HOST_KEXEC_ARCH_PPC64   (21 << 16)
139 #define HOST_KEXEC_ARCH_ARM     (40 << 16)
140 #define HOST_KEXEC_ARCH_AARCH64 (183 << 16)
141 #define HOST_KEXEC_ARCH_RISCV   (243 << 16)
142 
143 /* Arbitrary cap on segments */
144 #define HOST_KEXEC_SEGMENT_MAX 16
145 
146 struct host_kexec_segment {
147 	void *buf;
148 	int bufsz;
149 	void *mem;
150 	int memsz;
151 };
152 
153 struct host_dirent64 {
154 	uint64_t	d_ino;		/* 64-bit inode number */
155 	int64_t		d_off;		/* 64-bit offset to next structure */
156 	unsigned short	d_reclen;	/* Size of this dirent */
157 	unsigned char	d_type;		/* File type */
158 	char		d_name[];	/* Filename (null-terminated) */
159 };
160 
161 /* d_type values */
162 #define HOST_DT_UNKNOWN		 0
163 #define HOST_DT_FIFO		 1
164 #define HOST_DT_CHR		 2
165 #define HOST_DT_DIR		 4
166 #define HOST_DT_BLK		 6
167 #define HOST_DT_REG		 8
168 #define HOST_DT_LNK		10
169 #define HOST_DT_SOCK		12
170 #define HOST_DT_WHT		14
171 
172 /*
173  * System Calls
174  */
175 int host_close(int fd);
176 int host_dup(int fd);
177 int host_exit(int code);
178 int host_fstat(int fd, struct host_kstat *sb);
179 int host_getdents64(int fd, void *dirp, int count);
180 int host_getpid(void);
181 int host_gettimeofday(struct host_timeval *a, void *b);
182 int host_ioctl(int fd, unsigned long request, unsigned long arg);
183 int host_kexec_load(unsigned long entry, unsigned long nsegs, struct host_kexec_segment *segs, unsigned long flags);
184 ssize_t host_llseek(int fd, int32_t offset_high, int32_t offset_lo, uint64_t *result, int whence);
185 int host_mkdir(const char *, host_mode_t);
186 void *host_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off);
187 int host_mount(const char *src, const char *target, const char *type,
188     unsigned long flags, void *data);
189 int host_munmap(void *addr, size_t len);
190 int host_open(const char *path, int flags, int mode);
191 ssize_t host_read(int fd, void *buf, size_t nbyte);
192 int host_reboot(int, int, int, uintptr_t);
193 int host_select(int nfds, long *readfds, long *writefds, long *exceptfds,
194     struct host_timeval *timeout);
195 int host_stat(const char *path, struct host_kstat *sb);
196 int host_symlink(const char *path1, const char *path2);
197 int host_uname(struct old_utsname *);
198 ssize_t host_write(int fd, const void *buf, size_t nbyte);
199 
200 /*
201  * Wrappers / one-liners
202  */
203 #define host_getmem(size) \
204 	host_mmap(0, size, HOST_PROT_READ | HOST_PROT_WRITE, \
205 	    HOST_MAP_PRIVATE | HOST_MAP_ANONYMOUS, -1, 0);
206 
207 #endif
208