1f2f4bf5aSAlex Dewar /* SPDX-License-Identifier: GPL-2.0 */
28569c914SAl Viro /*
32eb5f31bSAnton Ivanov * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
42eb5f31bSAnton Ivanov * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
58569c914SAl Viro * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
68569c914SAl Viro */
78569c914SAl Viro
88569c914SAl Viro #ifndef __OS_H__
98569c914SAl Viro #define __OS_H__
108569c914SAl Viro
1137185b33SAl Viro #include <irq_user.h>
1237185b33SAl Viro #include <longjmp.h>
1337185b33SAl Viro #include <mm_id.h>
140b9ba613SJason A. Donenfeld /* This is to get size_t */
150b9ba613SJason A. Donenfeld #ifndef __UM_HOST__
160b9ba613SJason A. Donenfeld #include <linux/types.h>
170b9ba613SJason A. Donenfeld #else
180b9ba613SJason A. Donenfeld #include <sys/types.h>
190b9ba613SJason A. Donenfeld #endif
208569c914SAl Viro
218569c914SAl Viro #define CATCH_EINTR(expr) while ((errno = 0, ((expr) < 0)) && (errno == EINTR))
228569c914SAl Viro
238569c914SAl Viro #define OS_TYPE_FILE 1
248569c914SAl Viro #define OS_TYPE_DIR 2
258569c914SAl Viro #define OS_TYPE_SYMLINK 3
268569c914SAl Viro #define OS_TYPE_CHARDEV 4
278569c914SAl Viro #define OS_TYPE_BLOCKDEV 5
288569c914SAl Viro #define OS_TYPE_FIFO 6
298569c914SAl Viro #define OS_TYPE_SOCK 7
308569c914SAl Viro
318569c914SAl Viro /* os_access() flags */
328569c914SAl Viro #define OS_ACC_F_OK 0 /* Test for existence. */
338569c914SAl Viro #define OS_ACC_X_OK 1 /* Test for execute permission. */
348569c914SAl Viro #define OS_ACC_W_OK 2 /* Test for write permission. */
358569c914SAl Viro #define OS_ACC_R_OK 4 /* Test for read permission. */
368569c914SAl Viro #define OS_ACC_RW_OK (OS_ACC_W_OK | OS_ACC_R_OK) /* Test for RW permission */
378569c914SAl Viro
380ce451acSRichard Weinberger #ifdef CONFIG_64BIT
390ce451acSRichard Weinberger #define OS_LIB_PATH "/usr/lib64/"
400ce451acSRichard Weinberger #else
410ce451acSRichard Weinberger #define OS_LIB_PATH "/usr/lib/"
420ce451acSRichard Weinberger #endif
430ce451acSRichard Weinberger
445d38f324SErel Geron #define OS_SENDMSG_MAX_FDS 8
455d38f324SErel Geron
468569c914SAl Viro /*
478569c914SAl Viro * types taken from stat_file() in hostfs_user.c
488569c914SAl Viro * (if they are wrong here, they are wrong there...).
498569c914SAl Viro */
508569c914SAl Viro struct uml_stat {
518569c914SAl Viro int ust_dev; /* device */
528569c914SAl Viro unsigned long long ust_ino; /* inode */
538569c914SAl Viro int ust_mode; /* protection */
548569c914SAl Viro int ust_nlink; /* number of hard links */
558569c914SAl Viro int ust_uid; /* user ID of owner */
568569c914SAl Viro int ust_gid; /* group ID of owner */
578569c914SAl Viro unsigned long long ust_size; /* total size, in bytes */
588569c914SAl Viro int ust_blksize; /* blocksize for filesystem I/O */
598569c914SAl Viro unsigned long long ust_blocks; /* number of blocks allocated */
608569c914SAl Viro unsigned long ust_atime; /* time of last access */
618569c914SAl Viro unsigned long ust_mtime; /* time of last modification */
628569c914SAl Viro unsigned long ust_ctime; /* time of last change */
638569c914SAl Viro };
648569c914SAl Viro
658569c914SAl Viro struct openflags {
668569c914SAl Viro unsigned int r : 1;
678569c914SAl Viro unsigned int w : 1;
688569c914SAl Viro unsigned int s : 1; /* O_SYNC */
698569c914SAl Viro unsigned int c : 1; /* O_CREAT */
708569c914SAl Viro unsigned int t : 1; /* O_TRUNC */
718569c914SAl Viro unsigned int a : 1; /* O_APPEND */
728569c914SAl Viro unsigned int e : 1; /* O_EXCL */
738569c914SAl Viro unsigned int cl : 1; /* FD_CLOEXEC */
748569c914SAl Viro };
758569c914SAl Viro
768569c914SAl Viro #define OPENFLAGS() ((struct openflags) { .r = 0, .w = 0, .s = 0, .c = 0, \
778569c914SAl Viro .t = 0, .a = 0, .e = 0, .cl = 0 })
788569c914SAl Viro
of_read(struct openflags flags)798569c914SAl Viro static inline struct openflags of_read(struct openflags flags)
808569c914SAl Viro {
818569c914SAl Viro flags.r = 1;
828569c914SAl Viro return flags;
838569c914SAl Viro }
848569c914SAl Viro
of_write(struct openflags flags)858569c914SAl Viro static inline struct openflags of_write(struct openflags flags)
868569c914SAl Viro {
878569c914SAl Viro flags.w = 1;
888569c914SAl Viro return flags;
898569c914SAl Viro }
908569c914SAl Viro
of_rdwr(struct openflags flags)918569c914SAl Viro static inline struct openflags of_rdwr(struct openflags flags)
928569c914SAl Viro {
938569c914SAl Viro return of_read(of_write(flags));
948569c914SAl Viro }
958569c914SAl Viro
of_set_rw(struct openflags flags,int r,int w)968569c914SAl Viro static inline struct openflags of_set_rw(struct openflags flags, int r, int w)
978569c914SAl Viro {
988569c914SAl Viro flags.r = r;
998569c914SAl Viro flags.w = w;
1008569c914SAl Viro return flags;
1018569c914SAl Viro }
1028569c914SAl Viro
of_sync(struct openflags flags)1038569c914SAl Viro static inline struct openflags of_sync(struct openflags flags)
1048569c914SAl Viro {
1058569c914SAl Viro flags.s = 1;
1068569c914SAl Viro return flags;
1078569c914SAl Viro }
1088569c914SAl Viro
of_create(struct openflags flags)1098569c914SAl Viro static inline struct openflags of_create(struct openflags flags)
1108569c914SAl Viro {
1118569c914SAl Viro flags.c = 1;
1128569c914SAl Viro return flags;
1138569c914SAl Viro }
1148569c914SAl Viro
of_trunc(struct openflags flags)1158569c914SAl Viro static inline struct openflags of_trunc(struct openflags flags)
1168569c914SAl Viro {
1178569c914SAl Viro flags.t = 1;
1188569c914SAl Viro return flags;
1198569c914SAl Viro }
1208569c914SAl Viro
of_append(struct openflags flags)1218569c914SAl Viro static inline struct openflags of_append(struct openflags flags)
1228569c914SAl Viro {
1238569c914SAl Viro flags.a = 1;
1248569c914SAl Viro return flags;
1258569c914SAl Viro }
1268569c914SAl Viro
of_excl(struct openflags flags)1278569c914SAl Viro static inline struct openflags of_excl(struct openflags flags)
1288569c914SAl Viro {
1298569c914SAl Viro flags.e = 1;
1308569c914SAl Viro return flags;
1318569c914SAl Viro }
1328569c914SAl Viro
of_cloexec(struct openflags flags)1338569c914SAl Viro static inline struct openflags of_cloexec(struct openflags flags)
1348569c914SAl Viro {
1358569c914SAl Viro flags.cl = 1;
1368569c914SAl Viro return flags;
1378569c914SAl Viro }
1388569c914SAl Viro
1398569c914SAl Viro /* file.c */
1408569c914SAl Viro extern int os_stat_file(const char *file_name, struct uml_stat *buf);
1418569c914SAl Viro extern int os_stat_fd(const int fd, struct uml_stat *buf);
1428569c914SAl Viro extern int os_access(const char *file, int mode);
1438569c914SAl Viro extern int os_set_exec_close(int fd);
1448569c914SAl Viro extern int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg);
1458569c914SAl Viro extern int os_get_ifname(int fd, char *namebuf);
1468569c914SAl Viro extern int os_set_slip(int fd);
1478569c914SAl Viro extern int os_mode_fd(int fd, int mode);
1480565103dSAnton Ivanov extern int os_fsync_file(int fd);
1498569c914SAl Viro
1508569c914SAl Viro extern int os_seek_file(int fd, unsigned long long offset);
1518569c914SAl Viro extern int os_open_file(const char *file, struct openflags flags, int mode);
1528569c914SAl Viro extern int os_read_file(int fd, void *buf, int len);
1538569c914SAl Viro extern int os_write_file(int fd, const void *buf, int count);
154805f11a0SRichard Weinberger extern int os_sync_file(int fd);
1558569c914SAl Viro extern int os_file_size(const char *file, unsigned long long *size_out);
1568c6157b6SAnton Ivanov extern int os_pread_file(int fd, void *buf, int len, unsigned long long offset);
1578c6157b6SAnton Ivanov extern int os_pwrite_file(int fd, const void *buf, int count, unsigned long long offset);
158853bc0abSArnd Bergmann extern int os_file_modtime(const char *file, long long *modtime);
1598569c914SAl Viro extern int os_pipe(int *fd, int stream, int close_on_exec);
1608569c914SAl Viro extern int os_set_fd_async(int fd);
1618569c914SAl Viro extern int os_clear_fd_async(int fd);
1628569c914SAl Viro extern int os_set_fd_block(int fd, int blocking);
1638569c914SAl Viro extern int os_accept_connection(int fd);
1648569c914SAl Viro extern int os_create_unix_socket(const char *file, int len, int close_on_exec);
1658569c914SAl Viro extern int os_shutdown_socket(int fd, int r, int w);
166b2f9b77cSBenjamin Berg extern int os_dup_file(int fd);
1678569c914SAl Viro extern void os_close_file(int fd);
1685cde6096SJohannes Berg ssize_t os_rcv_fd_msg(int fd, int *fds, unsigned int n_fds,
1695cde6096SJohannes Berg void *data, size_t data_len);
1708569c914SAl Viro extern int os_connect_socket(const char *name);
1718569c914SAl Viro extern int os_file_type(char *file);
1728569c914SAl Viro extern int os_file_mode(const char *file, struct openflags *mode_out);
1738569c914SAl Viro extern int os_lock_file(int fd, int excl);
1748569c914SAl Viro extern void os_flush_stdout(void);
175005a59ecSAl Viro extern unsigned os_major(unsigned long long dev);
176005a59ecSAl Viro extern unsigned os_minor(unsigned long long dev);
177005a59ecSAl Viro extern unsigned long long os_makedev(unsigned major, unsigned minor);
17850109b5aSAnton Ivanov extern int os_falloc_punch(int fd, unsigned long long offset, int count);
179d2a0a616SFrédéric Danis extern int os_falloc_zeroes(int fd, unsigned long long offset, int count);
1805d38f324SErel Geron extern int os_eventfd(unsigned int initval, int flags);
1815d38f324SErel Geron extern int os_sendmsg_fds(int fd, const void *buf, unsigned int len,
1825d38f324SErel Geron const int *fds, unsigned int fds_num);
18388ce6424SJohannes Berg int os_poll(unsigned int n, const int *fds);
184e20f9b3cSJohannes Berg void *os_mmap_rw_shared(int fd, size_t size);
185e20f9b3cSJohannes Berg void *os_mremap_rw_shared(void *old_addr, size_t old_size, size_t new_size);
1868569c914SAl Viro
1878569c914SAl Viro /* start_up.c */
1888569c914SAl Viro extern void os_early_checks(void);
1898569c914SAl Viro extern void os_check_bugs(void);
1908569c914SAl Viro extern void check_host_supports_tls(int *supports_tls, int *tls_min);
191d8fb32f4SAnton Ivanov extern void get_host_cpu_features(
192d8fb32f4SAnton Ivanov void (*flags_helper_func)(char *line),
193d8fb32f4SAnton Ivanov void (*cache_helper_func)(char *line));
1948569c914SAl Viro
1958569c914SAl Viro /* mem.c */
1968569c914SAl Viro extern int create_mem_file(unsigned long long len);
1978569c914SAl Viro
1983c83170dSBenjamin Berg /* tlb.c */
1993c83170dSBenjamin Berg extern void report_enomem(void);
2003c83170dSBenjamin Berg
2018569c914SAl Viro /* process.c */
2028569c914SAl Viro extern unsigned long os_process_pc(int pid);
2038569c914SAl Viro extern int os_process_parent(int pid);
2042eb5f31bSAnton Ivanov extern void os_alarm_process(int pid);
2058569c914SAl Viro extern void os_stop_process(int pid);
2068569c914SAl Viro extern void os_kill_process(int pid, int reap_child);
2078569c914SAl Viro extern void os_kill_ptraced_process(int pid, int reap_child);
2088569c914SAl Viro
2098569c914SAl Viro extern int os_getpid(void);
2108569c914SAl Viro extern int os_getpgrp(void);
2118569c914SAl Viro
2128569c914SAl Viro extern void init_new_thread_signals(void);
2138569c914SAl Viro
2148569c914SAl Viro extern int os_map_memory(void *virt, int fd, unsigned long long off,
2158569c914SAl Viro unsigned long len, int r, int w, int x);
2168569c914SAl Viro extern int os_protect_memory(void *addr, unsigned long len,
2178569c914SAl Viro int r, int w, int x);
2188569c914SAl Viro extern int os_unmap_memory(void *addr, int len);
2198569c914SAl Viro extern int os_drop_memory(void *addr, int length);
2208569c914SAl Viro extern int can_drop_memory(void);
221f75b1b1bSRichard Weinberger extern int os_mincore(void *addr, unsigned long len);
2228569c914SAl Viro
2238569c914SAl Viro /* execvp.c */
2248569c914SAl Viro extern int execvp_noalloc(char *buf, const char *file, char *const argv[]);
2258569c914SAl Viro /* helper.c */
2268569c914SAl Viro extern int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv);
2278569c914SAl Viro extern int run_helper_thread(int (*proc)(void *), void *arg,
2288569c914SAl Viro unsigned int flags, unsigned long *stack_out);
2298569c914SAl Viro extern int helper_wait(int pid);
2308569c914SAl Viro
2318569c914SAl Viro
2328569c914SAl Viro /* umid.c */
2338569c914SAl Viro extern int umid_file_name(char *name, char *buf, int len);
2348569c914SAl Viro extern int set_umid(char *name);
2358569c914SAl Viro extern char *get_umid(void);
2368569c914SAl Viro
2378569c914SAl Viro /* signal.c */
2382eb5f31bSAnton Ivanov extern void timer_set_signal_handler(void);
2398569c914SAl Viro extern void set_sigstack(void *sig_stack, int size);
24000361683SAl Viro extern void set_handler(int sig);
241a374b7cbSJohannes Berg extern void send_sigio_to_self(void);
2428569c914SAl Viro extern int change_sig(int signal, int on);
2438569c914SAl Viro extern void block_signals(void);
2448569c914SAl Viro extern void unblock_signals(void);
245bbe33504SJohannes Berg extern int um_set_signals(int enable);
246bbe33504SJohannes Berg extern int um_set_signals_trace(int enable);
247f72c22e4SRichard Weinberger extern int os_is_signal_stack(void);
2482eb5f31bSAnton Ivanov extern void deliver_alarm(void);
24992dcd3d3SJohannes Berg extern void register_pm_wake_signal(void);
250d6b399a0SJohannes Berg extern void block_signals_hard(void);
251d6b399a0SJohannes Berg extern void unblock_signals_hard(void);
252d6b399a0SJohannes Berg extern void mark_sigio_pending(void);
2538569c914SAl Viro
2548569c914SAl Viro /* util.c */
2558569c914SAl Viro extern void stack_protections(unsigned long address);
2568569c914SAl Viro extern int raw(int fd);
2578569c914SAl Viro extern void setup_machinename(char *machine_out);
2588569c914SAl Viro extern void setup_hostinfo(char *buf, int len);
2590b9ba613SJason A. Donenfeld extern ssize_t os_getrandom(void *buf, size_t len, unsigned int flags);
2608569c914SAl Viro extern void os_dump_core(void) __attribute__ ((noreturn));
261d634f194SRichard Weinberger extern void um_early_printk(const char *s, unsigned int n);
26291d44ff8SRichard Weinberger extern void os_fix_helper_signals(void);
263f7887ee1SMasami Hiramatsu extern void os_info(const char *fmt, ...)
264f7887ee1SMasami Hiramatsu __attribute__ ((format (printf, 1, 2)));
265721ccae8SMasami Hiramatsu extern void os_warn(const char *fmt, ...)
266721ccae8SMasami Hiramatsu __attribute__ ((format (printf, 1, 2)));
2678569c914SAl Viro
2688569c914SAl Viro /* time.c */
26949da38a3SJohannes Berg extern void os_idle_sleep(void);
27056fc1870SJohannes Berg extern int os_timer_create(void);
271c7c6f3b9SJohannes Berg extern int os_timer_set_interval(unsigned long long nsecs);
272c7c6f3b9SJohannes Berg extern int os_timer_one_shot(unsigned long long nsecs);
27356fc1870SJohannes Berg extern void os_timer_disable(void);
2742eb5f31bSAnton Ivanov extern long long os_persistent_clock_emulation(void);
2758569c914SAl Viro extern long long os_nsecs(void);
2768569c914SAl Viro
2778569c914SAl Viro /* skas/mem.c */
27876ed9158SBenjamin Berg int syscall_stub_flush(struct mm_id *mm_idp);
27976ed9158SBenjamin Berg struct stub_syscall *syscall_stub_alloc(struct mm_id *mm_idp);
2803c83170dSBenjamin Berg void syscall_stub_dump_error(struct mm_id *mm_idp);
28176ed9158SBenjamin Berg
282*573a446fSBenjamin Berg int map(struct mm_id *mm_idp, unsigned long virt,
2838569c914SAl Viro unsigned long len, int prot, int phys_fd,
28476ed9158SBenjamin Berg unsigned long long offset);
285*573a446fSBenjamin Berg int unmap(struct mm_id *mm_idp, unsigned long addr, unsigned long len);
286*573a446fSBenjamin Berg int protect(struct mm_id *mm_idp, unsigned long addr,
28776ed9158SBenjamin Berg unsigned long len, unsigned int prot);
2888569c914SAl Viro
2898569c914SAl Viro /* skas/process.c */
2908569c914SAl Viro extern int is_skas_winch(int pid, int fd, void *data);
2918569c914SAl Viro extern int start_userspace(unsigned long stub_stack);
2926f602afdSThomas Meyer extern void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs);
2938569c914SAl Viro extern void new_thread(void *stack, jmp_buf *buf, void (*handler)(void));
2948569c914SAl Viro extern void switch_threads(jmp_buf *me, jmp_buf *you);
2958569c914SAl Viro extern int start_idle_thread(void *stack, jmp_buf *switch_buf);
2968569c914SAl Viro extern void initial_thread_cb_skas(void (*proc)(void *),
2978569c914SAl Viro void *arg);
2988569c914SAl Viro extern void halt_skas(void);
2998569c914SAl Viro extern void reboot_skas(void);
3008569c914SAl Viro
3018569c914SAl Viro /* irq.c */
302ff6a1798SAnton Ivanov extern int os_waiting_for_events_epoll(void);
303ff6a1798SAnton Ivanov extern void *os_epoll_get_data_pointer(int index);
304ff6a1798SAnton Ivanov extern int os_epoll_triggered(int index, int events);
3052fccfcc0SJohannes Berg extern int os_event_mask(enum um_irq_type irq_type);
306ff6a1798SAnton Ivanov extern int os_setup_epoll(void);
307ff6a1798SAnton Ivanov extern int os_add_epoll_fd(int events, int fd, void *data);
308ff6a1798SAnton Ivanov extern int os_mod_epoll_fd(int events, int fd, void *data);
309ff6a1798SAnton Ivanov extern int os_del_epoll_fd(int fd);
3108569c914SAl Viro extern void os_set_ioignore(void);
311ff6a1798SAnton Ivanov extern void os_close_epoll_fd(void);
312a374b7cbSJohannes Berg extern void um_irqs_suspend(void);
313a374b7cbSJohannes Berg extern void um_irqs_resume(void);
3148569c914SAl Viro
3158569c914SAl Viro /* sigio.c */
3168569c914SAl Viro extern int add_sigio_fd(int fd);
3178569c914SAl Viro extern int ignore_sigio_fd(int fd);
3182fccfcc0SJohannes Berg extern void maybe_sigio_broken(int fd);
3192fccfcc0SJohannes Berg extern void sigio_broken(int fd);
320cae20ba0SJohannes Berg /*
321cae20ba0SJohannes Berg * unlocked versions for IRQ controller code.
322cae20ba0SJohannes Berg *
323cae20ba0SJohannes Berg * This is safe because it's used at suspend/resume and nothing
324cae20ba0SJohannes Berg * else is running.
325cae20ba0SJohannes Berg */
326cae20ba0SJohannes Berg extern int __add_sigio_fd(int fd);
327cae20ba0SJohannes Berg extern int __ignore_sigio_fd(int fd);
3288569c914SAl Viro
3298569c914SAl Viro /* tty.c */
3308569c914SAl Viro extern int get_pty(void);
3318569c914SAl Viro
3328569c914SAl Viro /* sys-$ARCH/task_size.c */
3338569c914SAl Viro extern unsigned long os_get_top_address(void);
3348569c914SAl Viro
33589520d99SRichard Weinberger long syscall(long number, ...);
33689520d99SRichard Weinberger
3370dafcbe1SJohannes Berg /* irqflags tracing */
3380dafcbe1SJohannes Berg extern void block_signals_trace(void);
3390dafcbe1SJohannes Berg extern void unblock_signals_trace(void);
3400dafcbe1SJohannes Berg extern void um_trace_signals_on(void);
3410dafcbe1SJohannes Berg extern void um_trace_signals_off(void);
3420dafcbe1SJohannes Berg
34311385539SJohannes Berg /* time-travel */
34411385539SJohannes Berg extern void deliver_time_travel_irqs(void);
34511385539SJohannes Berg
3468569c914SAl Viro #endif
347