1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
5  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6  */
7 
8 #ifndef __OS_H__
9 #define __OS_H__
10 
11 #include <irq_user.h>
12 #include <longjmp.h>
13 #include <mm_id.h>
14 /* This is to get size_t */
15 #ifndef __UM_HOST__
16 #include <linux/types.h>
17 #else
18 #include <sys/types.h>
19 #endif
20 
21 #define CATCH_EINTR(expr) while ((errno = 0, ((expr) < 0)) && (errno == EINTR))
22 
23 #define OS_TYPE_FILE 1
24 #define OS_TYPE_DIR 2
25 #define OS_TYPE_SYMLINK 3
26 #define OS_TYPE_CHARDEV 4
27 #define OS_TYPE_BLOCKDEV 5
28 #define OS_TYPE_FIFO 6
29 #define OS_TYPE_SOCK 7
30 
31 /* os_access() flags */
32 #define OS_ACC_F_OK    0       /* Test for existence.  */
33 #define OS_ACC_X_OK    1       /* Test for execute permission.  */
34 #define OS_ACC_W_OK    2       /* Test for write permission.  */
35 #define OS_ACC_R_OK    4       /* Test for read permission.  */
36 #define OS_ACC_RW_OK   (OS_ACC_W_OK | OS_ACC_R_OK) /* Test for RW permission */
37 
38 #ifdef CONFIG_64BIT
39 #define OS_LIB_PATH	"/usr/lib64/"
40 #else
41 #define OS_LIB_PATH	"/usr/lib/"
42 #endif
43 
44 #define OS_SENDMSG_MAX_FDS 8
45 
46 /*
47  * types taken from stat_file() in hostfs_user.c
48  * (if they are wrong here, they are wrong there...).
49  */
50 struct uml_stat {
51 	int                ust_dev;        /* device */
52 	unsigned long long ust_ino;        /* inode */
53 	int                ust_mode;       /* protection */
54 	int                ust_nlink;      /* number of hard links */
55 	int                ust_uid;        /* user ID of owner */
56 	int                ust_gid;        /* group ID of owner */
57 	unsigned long long ust_size;       /* total size, in bytes */
58 	int                ust_blksize;    /* blocksize for filesystem I/O */
59 	unsigned long long ust_blocks;     /* number of blocks allocated */
60 	unsigned long      ust_atime;      /* time of last access */
61 	unsigned long      ust_mtime;      /* time of last modification */
62 	unsigned long      ust_ctime;      /* time of last change */
63 };
64 
65 struct openflags {
66 	unsigned int r : 1;
67 	unsigned int w : 1;
68 	unsigned int s : 1;	/* O_SYNC */
69 	unsigned int c : 1;	/* O_CREAT */
70 	unsigned int t : 1;	/* O_TRUNC */
71 	unsigned int a : 1;	/* O_APPEND */
72 	unsigned int e : 1;	/* O_EXCL */
73 	unsigned int cl : 1;    /* FD_CLOEXEC */
74 };
75 
76 #define OPENFLAGS() ((struct openflags) { .r = 0, .w = 0, .s = 0, .c = 0, \
77 					  .t = 0, .a = 0, .e = 0, .cl = 0 })
78 
of_read(struct openflags flags)79 static inline struct openflags of_read(struct openflags flags)
80 {
81 	flags.r = 1;
82 	return flags;
83 }
84 
of_write(struct openflags flags)85 static inline struct openflags of_write(struct openflags flags)
86 {
87 	flags.w = 1;
88 	return flags;
89 }
90 
of_rdwr(struct openflags flags)91 static inline struct openflags of_rdwr(struct openflags flags)
92 {
93 	return of_read(of_write(flags));
94 }
95 
of_set_rw(struct openflags flags,int r,int w)96 static inline struct openflags of_set_rw(struct openflags flags, int r, int w)
97 {
98 	flags.r = r;
99 	flags.w = w;
100 	return flags;
101 }
102 
of_sync(struct openflags flags)103 static inline struct openflags of_sync(struct openflags flags)
104 {
105 	flags.s = 1;
106 	return flags;
107 }
108 
of_create(struct openflags flags)109 static inline struct openflags of_create(struct openflags flags)
110 {
111 	flags.c = 1;
112 	return flags;
113 }
114 
of_trunc(struct openflags flags)115 static inline struct openflags of_trunc(struct openflags flags)
116 {
117 	flags.t = 1;
118 	return flags;
119 }
120 
of_append(struct openflags flags)121 static inline struct openflags of_append(struct openflags flags)
122 {
123 	flags.a = 1;
124 	return flags;
125 }
126 
of_excl(struct openflags flags)127 static inline struct openflags of_excl(struct openflags flags)
128 {
129 	flags.e = 1;
130 	return flags;
131 }
132 
of_cloexec(struct openflags flags)133 static inline struct openflags of_cloexec(struct openflags flags)
134 {
135 	flags.cl = 1;
136 	return flags;
137 }
138 
139 /* file.c */
140 extern int os_stat_file(const char *file_name, struct uml_stat *buf);
141 extern int os_stat_fd(const int fd, struct uml_stat *buf);
142 extern int os_access(const char *file, int mode);
143 extern int os_set_exec_close(int fd);
144 extern int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg);
145 extern int os_get_ifname(int fd, char *namebuf);
146 extern int os_set_slip(int fd);
147 extern int os_mode_fd(int fd, int mode);
148 extern int os_fsync_file(int fd);
149 
150 extern int os_seek_file(int fd, unsigned long long offset);
151 extern int os_open_file(const char *file, struct openflags flags, int mode);
152 extern int os_read_file(int fd, void *buf, int len);
153 extern int os_write_file(int fd, const void *buf, int count);
154 extern int os_sync_file(int fd);
155 extern int os_file_size(const char *file, unsigned long long *size_out);
156 extern int os_pread_file(int fd, void *buf, int len, unsigned long long offset);
157 extern int os_pwrite_file(int fd, const void *buf, int count, unsigned long long offset);
158 extern int os_file_modtime(const char *file, long long *modtime);
159 extern int os_pipe(int *fd, int stream, int close_on_exec);
160 extern int os_set_fd_async(int fd);
161 extern int os_clear_fd_async(int fd);
162 extern int os_set_fd_block(int fd, int blocking);
163 extern int os_accept_connection(int fd);
164 extern int os_create_unix_socket(const char *file, int len, int close_on_exec);
165 extern int os_shutdown_socket(int fd, int r, int w);
166 extern void os_close_file(int fd);
167 extern int os_rcv_fd(int fd, int *helper_pid_out);
168 extern int os_connect_socket(const char *name);
169 extern int os_file_type(char *file);
170 extern int os_file_mode(const char *file, struct openflags *mode_out);
171 extern int os_lock_file(int fd, int excl);
172 extern void os_flush_stdout(void);
173 extern unsigned os_major(unsigned long long dev);
174 extern unsigned os_minor(unsigned long long dev);
175 extern unsigned long long os_makedev(unsigned major, unsigned minor);
176 extern int os_falloc_punch(int fd, unsigned long long offset, int count);
177 extern int os_falloc_zeroes(int fd, unsigned long long offset, int count);
178 extern int os_eventfd(unsigned int initval, int flags);
179 extern int os_sendmsg_fds(int fd, const void *buf, unsigned int len,
180 			  const int *fds, unsigned int fds_num);
181 int os_poll(unsigned int n, const int *fds);
182 
183 /* start_up.c */
184 extern void os_early_checks(void);
185 extern void os_check_bugs(void);
186 extern void check_host_supports_tls(int *supports_tls, int *tls_min);
187 extern void get_host_cpu_features(
188 	void (*flags_helper_func)(char *line),
189 	void (*cache_helper_func)(char *line));
190 
191 /* mem.c */
192 extern int create_mem_file(unsigned long long len);
193 
194 /* process.c */
195 extern unsigned long os_process_pc(int pid);
196 extern int os_process_parent(int pid);
197 extern void os_alarm_process(int pid);
198 extern void os_stop_process(int pid);
199 extern void os_kill_process(int pid, int reap_child);
200 extern void os_kill_ptraced_process(int pid, int reap_child);
201 
202 extern int os_getpid(void);
203 extern int os_getpgrp(void);
204 
205 extern void init_new_thread_signals(void);
206 
207 extern int os_map_memory(void *virt, int fd, unsigned long long off,
208 			 unsigned long len, int r, int w, int x);
209 extern int os_protect_memory(void *addr, unsigned long len,
210 			     int r, int w, int x);
211 extern int os_unmap_memory(void *addr, int len);
212 extern int os_drop_memory(void *addr, int length);
213 extern int can_drop_memory(void);
214 extern int os_mincore(void *addr, unsigned long len);
215 
216 /* execvp.c */
217 extern int execvp_noalloc(char *buf, const char *file, char *const argv[]);
218 /* helper.c */
219 extern int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv);
220 extern int run_helper_thread(int (*proc)(void *), void *arg,
221 			     unsigned int flags, unsigned long *stack_out);
222 extern int helper_wait(int pid);
223 
224 
225 /* umid.c */
226 extern int umid_file_name(char *name, char *buf, int len);
227 extern int set_umid(char *name);
228 extern char *get_umid(void);
229 
230 /* signal.c */
231 extern void timer_set_signal_handler(void);
232 extern void set_sigstack(void *sig_stack, int size);
233 extern void set_handler(int sig);
234 extern void send_sigio_to_self(void);
235 extern int change_sig(int signal, int on);
236 extern void block_signals(void);
237 extern void unblock_signals(void);
238 extern int um_set_signals(int enable);
239 extern int um_set_signals_trace(int enable);
240 extern int os_is_signal_stack(void);
241 extern void deliver_alarm(void);
242 extern void register_pm_wake_signal(void);
243 extern void block_signals_hard(void);
244 extern void unblock_signals_hard(void);
245 extern void mark_sigio_pending(void);
246 
247 /* util.c */
248 extern void stack_protections(unsigned long address);
249 extern int raw(int fd);
250 extern void setup_machinename(char *machine_out);
251 extern void setup_hostinfo(char *buf, int len);
252 extern ssize_t os_getrandom(void *buf, size_t len, unsigned int flags);
253 extern void os_dump_core(void) __attribute__ ((noreturn));
254 extern void um_early_printk(const char *s, unsigned int n);
255 extern void os_fix_helper_signals(void);
256 extern void os_info(const char *fmt, ...)
257 	__attribute__ ((format (printf, 1, 2)));
258 extern void os_warn(const char *fmt, ...)
259 	__attribute__ ((format (printf, 1, 2)));
260 
261 /* time.c */
262 extern void os_idle_sleep(void);
263 extern int os_timer_create(void);
264 extern int os_timer_set_interval(unsigned long long nsecs);
265 extern int os_timer_one_shot(unsigned long long nsecs);
266 extern void os_timer_disable(void);
267 extern long long os_persistent_clock_emulation(void);
268 extern long long os_nsecs(void);
269 
270 /* skas/mem.c */
271 extern long run_syscall_stub(struct mm_id * mm_idp,
272 			     int syscall, unsigned long *args, long expected,
273 			     void **addr, int done);
274 extern long syscall_stub_data(struct mm_id * mm_idp,
275 			      unsigned long *data, int data_count,
276 			      void **addr, void **stub_addr);
277 extern int map(struct mm_id * mm_idp, unsigned long virt,
278 	       unsigned long len, int prot, int phys_fd,
279 	       unsigned long long offset, int done, void **data);
280 extern int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
281 		 int done, void **data);
282 extern int protect(struct mm_id * mm_idp, unsigned long addr,
283 		   unsigned long len, unsigned int prot, int done, void **data);
284 
285 /* skas/process.c */
286 extern int is_skas_winch(int pid, int fd, void *data);
287 extern int start_userspace(unsigned long stub_stack);
288 extern int copy_context_skas0(unsigned long stack, int pid);
289 extern void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs);
290 extern void new_thread(void *stack, jmp_buf *buf, void (*handler)(void));
291 extern void switch_threads(jmp_buf *me, jmp_buf *you);
292 extern int start_idle_thread(void *stack, jmp_buf *switch_buf);
293 extern void initial_thread_cb_skas(void (*proc)(void *),
294 				 void *arg);
295 extern void halt_skas(void);
296 extern void reboot_skas(void);
297 
298 /* irq.c */
299 extern int os_waiting_for_events_epoll(void);
300 extern void *os_epoll_get_data_pointer(int index);
301 extern int os_epoll_triggered(int index, int events);
302 extern int os_event_mask(enum um_irq_type irq_type);
303 extern int os_setup_epoll(void);
304 extern int os_add_epoll_fd(int events, int fd, void *data);
305 extern int os_mod_epoll_fd(int events, int fd, void *data);
306 extern int os_del_epoll_fd(int fd);
307 extern void os_set_ioignore(void);
308 extern void os_close_epoll_fd(void);
309 extern void um_irqs_suspend(void);
310 extern void um_irqs_resume(void);
311 
312 /* sigio.c */
313 extern int add_sigio_fd(int fd);
314 extern int ignore_sigio_fd(int fd);
315 extern void maybe_sigio_broken(int fd);
316 extern void sigio_broken(int fd);
317 /*
318  * unlocked versions for IRQ controller code.
319  *
320  * This is safe because it's used at suspend/resume and nothing
321  * else is running.
322  */
323 extern int __add_sigio_fd(int fd);
324 extern int __ignore_sigio_fd(int fd);
325 
326 /* prctl.c */
327 extern int os_arch_prctl(int pid, int option, unsigned long *arg2);
328 
329 /* tty.c */
330 extern int get_pty(void);
331 
332 /* sys-$ARCH/task_size.c */
333 extern unsigned long os_get_top_address(void);
334 
335 long syscall(long number, ...);
336 
337 /* irqflags tracing */
338 extern void block_signals_trace(void);
339 extern void unblock_signals_trace(void);
340 extern void um_trace_signals_on(void);
341 extern void um_trace_signals_off(void);
342 
343 /* time-travel */
344 extern void deliver_time_travel_irqs(void);
345 
346 #endif
347