xref: /openbmc/linux/arch/um/include/shared/os.h (revision 0b9ba613)
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>
14*0b9ba613SJason A. Donenfeld /* This is to get size_t */
15*0b9ba613SJason A. Donenfeld #ifndef __UM_HOST__
16*0b9ba613SJason A. Donenfeld #include <linux/types.h>
17*0b9ba613SJason A. Donenfeld #else
18*0b9ba613SJason A. Donenfeld #include <sys/types.h>
19*0b9ba613SJason 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);
1668569c914SAl Viro extern void os_close_file(int fd);
1678569c914SAl Viro extern int os_rcv_fd(int fd, int *helper_pid_out);
1688569c914SAl Viro extern int os_connect_socket(const char *name);
1698569c914SAl Viro extern int os_file_type(char *file);
1708569c914SAl Viro extern int os_file_mode(const char *file, struct openflags *mode_out);
1718569c914SAl Viro extern int os_lock_file(int fd, int excl);
1728569c914SAl Viro extern void os_flush_stdout(void);
173005a59ecSAl Viro extern unsigned os_major(unsigned long long dev);
174005a59ecSAl Viro extern unsigned os_minor(unsigned long long dev);
175005a59ecSAl Viro extern unsigned long long os_makedev(unsigned major, unsigned minor);
17650109b5aSAnton Ivanov extern int os_falloc_punch(int fd, unsigned long long offset, int count);
177d2a0a616SFrédéric Danis extern int os_falloc_zeroes(int fd, unsigned long long offset, int count);
1785d38f324SErel Geron extern int os_eventfd(unsigned int initval, int flags);
1795d38f324SErel Geron extern int os_sendmsg_fds(int fd, const void *buf, unsigned int len,
1805d38f324SErel Geron 			  const int *fds, unsigned int fds_num);
18188ce6424SJohannes Berg int os_poll(unsigned int n, const int *fds);
1828569c914SAl Viro 
1838569c914SAl Viro /* start_up.c */
1848569c914SAl Viro extern void os_early_checks(void);
1858569c914SAl Viro extern void os_check_bugs(void);
1868569c914SAl Viro extern void check_host_supports_tls(int *supports_tls, int *tls_min);
187d8fb32f4SAnton Ivanov extern void get_host_cpu_features(
188d8fb32f4SAnton Ivanov 	void (*flags_helper_func)(char *line),
189d8fb32f4SAnton Ivanov 	void (*cache_helper_func)(char *line));
1908569c914SAl Viro 
1918569c914SAl Viro /* mem.c */
1928569c914SAl Viro extern int create_mem_file(unsigned long long len);
1938569c914SAl Viro 
1948569c914SAl Viro /* process.c */
1958569c914SAl Viro extern unsigned long os_process_pc(int pid);
1968569c914SAl Viro extern int os_process_parent(int pid);
1972eb5f31bSAnton Ivanov extern void os_alarm_process(int pid);
1988569c914SAl Viro extern void os_stop_process(int pid);
1998569c914SAl Viro extern void os_kill_process(int pid, int reap_child);
2008569c914SAl Viro extern void os_kill_ptraced_process(int pid, int reap_child);
2018569c914SAl Viro 
2028569c914SAl Viro extern int os_getpid(void);
2038569c914SAl Viro extern int os_getpgrp(void);
2048569c914SAl Viro 
2058569c914SAl Viro extern void init_new_thread_signals(void);
2068569c914SAl Viro 
2078569c914SAl Viro extern int os_map_memory(void *virt, int fd, unsigned long long off,
2088569c914SAl Viro 			 unsigned long len, int r, int w, int x);
2098569c914SAl Viro extern int os_protect_memory(void *addr, unsigned long len,
2108569c914SAl Viro 			     int r, int w, int x);
2118569c914SAl Viro extern int os_unmap_memory(void *addr, int len);
2128569c914SAl Viro extern int os_drop_memory(void *addr, int length);
2138569c914SAl Viro extern int can_drop_memory(void);
214f75b1b1bSRichard Weinberger extern int os_mincore(void *addr, unsigned long len);
2158569c914SAl Viro 
2168569c914SAl Viro /* execvp.c */
2178569c914SAl Viro extern int execvp_noalloc(char *buf, const char *file, char *const argv[]);
2188569c914SAl Viro /* helper.c */
2198569c914SAl Viro extern int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv);
2208569c914SAl Viro extern int run_helper_thread(int (*proc)(void *), void *arg,
2218569c914SAl Viro 			     unsigned int flags, unsigned long *stack_out);
2228569c914SAl Viro extern int helper_wait(int pid);
2238569c914SAl Viro 
2248569c914SAl Viro 
2258569c914SAl Viro /* umid.c */
2268569c914SAl Viro extern int umid_file_name(char *name, char *buf, int len);
2278569c914SAl Viro extern int set_umid(char *name);
2288569c914SAl Viro extern char *get_umid(void);
2298569c914SAl Viro 
2308569c914SAl Viro /* signal.c */
2312eb5f31bSAnton Ivanov extern void timer_set_signal_handler(void);
2328569c914SAl Viro extern void set_sigstack(void *sig_stack, int size);
23300361683SAl Viro extern void set_handler(int sig);
234a374b7cbSJohannes Berg extern void send_sigio_to_self(void);
2358569c914SAl Viro extern int change_sig(int signal, int on);
2368569c914SAl Viro extern void block_signals(void);
2378569c914SAl Viro extern void unblock_signals(void);
238bbe33504SJohannes Berg extern int um_set_signals(int enable);
239bbe33504SJohannes Berg extern int um_set_signals_trace(int enable);
240f72c22e4SRichard Weinberger extern int os_is_signal_stack(void);
2412eb5f31bSAnton Ivanov extern void deliver_alarm(void);
24292dcd3d3SJohannes Berg extern void register_pm_wake_signal(void);
243d6b399a0SJohannes Berg extern void block_signals_hard(void);
244d6b399a0SJohannes Berg extern void unblock_signals_hard(void);
245d6b399a0SJohannes Berg extern void mark_sigio_pending(void);
2468569c914SAl Viro 
2478569c914SAl Viro /* util.c */
2488569c914SAl Viro extern void stack_protections(unsigned long address);
2498569c914SAl Viro extern int raw(int fd);
2508569c914SAl Viro extern void setup_machinename(char *machine_out);
2518569c914SAl Viro extern void setup_hostinfo(char *buf, int len);
252*0b9ba613SJason A. Donenfeld extern ssize_t os_getrandom(void *buf, size_t len, unsigned int flags);
2538569c914SAl Viro extern void os_dump_core(void) __attribute__ ((noreturn));
254d634f194SRichard Weinberger extern void um_early_printk(const char *s, unsigned int n);
25591d44ff8SRichard Weinberger extern void os_fix_helper_signals(void);
256f7887ee1SMasami Hiramatsu extern void os_info(const char *fmt, ...)
257f7887ee1SMasami Hiramatsu 	__attribute__ ((format (printf, 1, 2)));
258721ccae8SMasami Hiramatsu extern void os_warn(const char *fmt, ...)
259721ccae8SMasami Hiramatsu 	__attribute__ ((format (printf, 1, 2)));
2608569c914SAl Viro 
2618569c914SAl Viro /* time.c */
26249da38a3SJohannes Berg extern void os_idle_sleep(void);
26356fc1870SJohannes Berg extern int os_timer_create(void);
264c7c6f3b9SJohannes Berg extern int os_timer_set_interval(unsigned long long nsecs);
265c7c6f3b9SJohannes Berg extern int os_timer_one_shot(unsigned long long nsecs);
26656fc1870SJohannes Berg extern void os_timer_disable(void);
2672eb5f31bSAnton Ivanov extern long long os_persistent_clock_emulation(void);
2688569c914SAl Viro extern long long os_nsecs(void);
2698569c914SAl Viro 
2708569c914SAl Viro /* skas/mem.c */
2718569c914SAl Viro extern long run_syscall_stub(struct mm_id * mm_idp,
2728569c914SAl Viro 			     int syscall, unsigned long *args, long expected,
2738569c914SAl Viro 			     void **addr, int done);
2748569c914SAl Viro extern long syscall_stub_data(struct mm_id * mm_idp,
2758569c914SAl Viro 			      unsigned long *data, int data_count,
2768569c914SAl Viro 			      void **addr, void **stub_addr);
2778569c914SAl Viro extern int map(struct mm_id * mm_idp, unsigned long virt,
2788569c914SAl Viro 	       unsigned long len, int prot, int phys_fd,
2798569c914SAl Viro 	       unsigned long long offset, int done, void **data);
2808569c914SAl Viro extern int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
2818569c914SAl Viro 		 int done, void **data);
2828569c914SAl Viro extern int protect(struct mm_id * mm_idp, unsigned long addr,
2838569c914SAl Viro 		   unsigned long len, unsigned int prot, int done, void **data);
2848569c914SAl Viro 
2858569c914SAl Viro /* skas/process.c */
2868569c914SAl Viro extern int is_skas_winch(int pid, int fd, void *data);
2878569c914SAl Viro extern int start_userspace(unsigned long stub_stack);
2888569c914SAl Viro extern int copy_context_skas0(unsigned long stack, int pid);
2896f602afdSThomas Meyer extern void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs);
2908569c914SAl Viro extern void new_thread(void *stack, jmp_buf *buf, void (*handler)(void));
2918569c914SAl Viro extern void switch_threads(jmp_buf *me, jmp_buf *you);
2928569c914SAl Viro extern int start_idle_thread(void *stack, jmp_buf *switch_buf);
2938569c914SAl Viro extern void initial_thread_cb_skas(void (*proc)(void *),
2948569c914SAl Viro 				 void *arg);
2958569c914SAl Viro extern void halt_skas(void);
2968569c914SAl Viro extern void reboot_skas(void);
2978569c914SAl Viro 
2988569c914SAl Viro /* irq.c */
299ff6a1798SAnton Ivanov extern int os_waiting_for_events_epoll(void);
300ff6a1798SAnton Ivanov extern void *os_epoll_get_data_pointer(int index);
301ff6a1798SAnton Ivanov extern int os_epoll_triggered(int index, int events);
3022fccfcc0SJohannes Berg extern int os_event_mask(enum um_irq_type irq_type);
303ff6a1798SAnton Ivanov extern int os_setup_epoll(void);
304ff6a1798SAnton Ivanov extern int os_add_epoll_fd(int events, int fd, void *data);
305ff6a1798SAnton Ivanov extern int os_mod_epoll_fd(int events, int fd, void *data);
306ff6a1798SAnton Ivanov extern int os_del_epoll_fd(int fd);
3078569c914SAl Viro extern void os_set_ioignore(void);
308ff6a1798SAnton Ivanov extern void os_close_epoll_fd(void);
309a374b7cbSJohannes Berg extern void um_irqs_suspend(void);
310a374b7cbSJohannes Berg extern void um_irqs_resume(void);
3118569c914SAl Viro 
3128569c914SAl Viro /* sigio.c */
3138569c914SAl Viro extern int add_sigio_fd(int fd);
3148569c914SAl Viro extern int ignore_sigio_fd(int fd);
3152fccfcc0SJohannes Berg extern void maybe_sigio_broken(int fd);
3162fccfcc0SJohannes Berg extern void sigio_broken(int fd);
317cae20ba0SJohannes Berg /*
318cae20ba0SJohannes Berg  * unlocked versions for IRQ controller code.
319cae20ba0SJohannes Berg  *
320cae20ba0SJohannes Berg  * This is safe because it's used at suspend/resume and nothing
321cae20ba0SJohannes Berg  * else is running.
322cae20ba0SJohannes Berg  */
323cae20ba0SJohannes Berg extern int __add_sigio_fd(int fd);
324cae20ba0SJohannes Berg extern int __ignore_sigio_fd(int fd);
3258569c914SAl Viro 
326dd93938aSKyle Huey /* prctl.c */
32717a6e1b8SKyle Huey extern int os_arch_prctl(int pid, int option, unsigned long *arg2);
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