18569c914SAl Viro /* 28569c914SAl Viro * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 38569c914SAl Viro * Licensed under the GPL 48569c914SAl Viro */ 58569c914SAl Viro 68569c914SAl Viro #ifndef __OS_H__ 78569c914SAl Viro #define __OS_H__ 88569c914SAl Viro 98569c914SAl Viro #include <stdarg.h> 108569c914SAl Viro #include "irq_user.h" 118569c914SAl Viro #include "longjmp.h" 128569c914SAl Viro #include "mm_id.h" 138569c914SAl Viro #include "sysdep/tls.h" 148569c914SAl Viro 158569c914SAl Viro #define CATCH_EINTR(expr) while ((errno = 0, ((expr) < 0)) && (errno == EINTR)) 168569c914SAl Viro 178569c914SAl Viro #define OS_TYPE_FILE 1 188569c914SAl Viro #define OS_TYPE_DIR 2 198569c914SAl Viro #define OS_TYPE_SYMLINK 3 208569c914SAl Viro #define OS_TYPE_CHARDEV 4 218569c914SAl Viro #define OS_TYPE_BLOCKDEV 5 228569c914SAl Viro #define OS_TYPE_FIFO 6 238569c914SAl Viro #define OS_TYPE_SOCK 7 248569c914SAl Viro 258569c914SAl Viro /* os_access() flags */ 268569c914SAl Viro #define OS_ACC_F_OK 0 /* Test for existence. */ 278569c914SAl Viro #define OS_ACC_X_OK 1 /* Test for execute permission. */ 288569c914SAl Viro #define OS_ACC_W_OK 2 /* Test for write permission. */ 298569c914SAl Viro #define OS_ACC_R_OK 4 /* Test for read permission. */ 308569c914SAl Viro #define OS_ACC_RW_OK (OS_ACC_W_OK | OS_ACC_R_OK) /* Test for RW permission */ 318569c914SAl Viro 320ce451acSRichard Weinberger #ifdef CONFIG_64BIT 330ce451acSRichard Weinberger #define OS_LIB_PATH "/usr/lib64/" 340ce451acSRichard Weinberger #else 350ce451acSRichard Weinberger #define OS_LIB_PATH "/usr/lib/" 360ce451acSRichard Weinberger #endif 370ce451acSRichard Weinberger 388569c914SAl Viro /* 398569c914SAl Viro * types taken from stat_file() in hostfs_user.c 408569c914SAl Viro * (if they are wrong here, they are wrong there...). 418569c914SAl Viro */ 428569c914SAl Viro struct uml_stat { 438569c914SAl Viro int ust_dev; /* device */ 448569c914SAl Viro unsigned long long ust_ino; /* inode */ 458569c914SAl Viro int ust_mode; /* protection */ 468569c914SAl Viro int ust_nlink; /* number of hard links */ 478569c914SAl Viro int ust_uid; /* user ID of owner */ 488569c914SAl Viro int ust_gid; /* group ID of owner */ 498569c914SAl Viro unsigned long long ust_size; /* total size, in bytes */ 508569c914SAl Viro int ust_blksize; /* blocksize for filesystem I/O */ 518569c914SAl Viro unsigned long long ust_blocks; /* number of blocks allocated */ 528569c914SAl Viro unsigned long ust_atime; /* time of last access */ 538569c914SAl Viro unsigned long ust_mtime; /* time of last modification */ 548569c914SAl Viro unsigned long ust_ctime; /* time of last change */ 558569c914SAl Viro }; 568569c914SAl Viro 578569c914SAl Viro struct openflags { 588569c914SAl Viro unsigned int r : 1; 598569c914SAl Viro unsigned int w : 1; 608569c914SAl Viro unsigned int s : 1; /* O_SYNC */ 618569c914SAl Viro unsigned int c : 1; /* O_CREAT */ 628569c914SAl Viro unsigned int t : 1; /* O_TRUNC */ 638569c914SAl Viro unsigned int a : 1; /* O_APPEND */ 648569c914SAl Viro unsigned int e : 1; /* O_EXCL */ 658569c914SAl Viro unsigned int cl : 1; /* FD_CLOEXEC */ 668569c914SAl Viro }; 678569c914SAl Viro 688569c914SAl Viro #define OPENFLAGS() ((struct openflags) { .r = 0, .w = 0, .s = 0, .c = 0, \ 698569c914SAl Viro .t = 0, .a = 0, .e = 0, .cl = 0 }) 708569c914SAl Viro 718569c914SAl Viro static inline struct openflags of_read(struct openflags flags) 728569c914SAl Viro { 738569c914SAl Viro flags.r = 1; 748569c914SAl Viro return flags; 758569c914SAl Viro } 768569c914SAl Viro 778569c914SAl Viro static inline struct openflags of_write(struct openflags flags) 788569c914SAl Viro { 798569c914SAl Viro flags.w = 1; 808569c914SAl Viro return flags; 818569c914SAl Viro } 828569c914SAl Viro 838569c914SAl Viro static inline struct openflags of_rdwr(struct openflags flags) 848569c914SAl Viro { 858569c914SAl Viro return of_read(of_write(flags)); 868569c914SAl Viro } 878569c914SAl Viro 888569c914SAl Viro static inline struct openflags of_set_rw(struct openflags flags, int r, int w) 898569c914SAl Viro { 908569c914SAl Viro flags.r = r; 918569c914SAl Viro flags.w = w; 928569c914SAl Viro return flags; 938569c914SAl Viro } 948569c914SAl Viro 958569c914SAl Viro static inline struct openflags of_sync(struct openflags flags) 968569c914SAl Viro { 978569c914SAl Viro flags.s = 1; 988569c914SAl Viro return flags; 998569c914SAl Viro } 1008569c914SAl Viro 1018569c914SAl Viro static inline struct openflags of_create(struct openflags flags) 1028569c914SAl Viro { 1038569c914SAl Viro flags.c = 1; 1048569c914SAl Viro return flags; 1058569c914SAl Viro } 1068569c914SAl Viro 1078569c914SAl Viro static inline struct openflags of_trunc(struct openflags flags) 1088569c914SAl Viro { 1098569c914SAl Viro flags.t = 1; 1108569c914SAl Viro return flags; 1118569c914SAl Viro } 1128569c914SAl Viro 1138569c914SAl Viro static inline struct openflags of_append(struct openflags flags) 1148569c914SAl Viro { 1158569c914SAl Viro flags.a = 1; 1168569c914SAl Viro return flags; 1178569c914SAl Viro } 1188569c914SAl Viro 1198569c914SAl Viro static inline struct openflags of_excl(struct openflags flags) 1208569c914SAl Viro { 1218569c914SAl Viro flags.e = 1; 1228569c914SAl Viro return flags; 1238569c914SAl Viro } 1248569c914SAl Viro 1258569c914SAl Viro static inline struct openflags of_cloexec(struct openflags flags) 1268569c914SAl Viro { 1278569c914SAl Viro flags.cl = 1; 1288569c914SAl Viro return flags; 1298569c914SAl Viro } 1308569c914SAl Viro 1318569c914SAl Viro /* file.c */ 1328569c914SAl Viro extern int os_stat_file(const char *file_name, struct uml_stat *buf); 1338569c914SAl Viro extern int os_stat_fd(const int fd, struct uml_stat *buf); 1348569c914SAl Viro extern int os_access(const char *file, int mode); 1358569c914SAl Viro extern int os_set_exec_close(int fd); 1368569c914SAl Viro extern int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg); 1378569c914SAl Viro extern int os_get_ifname(int fd, char *namebuf); 1388569c914SAl Viro extern int os_set_slip(int fd); 1398569c914SAl Viro extern int os_mode_fd(int fd, int mode); 1408569c914SAl Viro 1418569c914SAl Viro extern int os_seek_file(int fd, unsigned long long offset); 1428569c914SAl Viro extern int os_open_file(const char *file, struct openflags flags, int mode); 1438569c914SAl Viro extern int os_read_file(int fd, void *buf, int len); 1448569c914SAl Viro extern int os_write_file(int fd, const void *buf, int count); 1458569c914SAl Viro extern int os_file_size(const char *file, unsigned long long *size_out); 1468569c914SAl Viro extern int os_file_modtime(const char *file, unsigned long *modtime); 1478569c914SAl Viro extern int os_pipe(int *fd, int stream, int close_on_exec); 1488569c914SAl Viro extern int os_set_fd_async(int fd); 1498569c914SAl Viro extern int os_clear_fd_async(int fd); 1508569c914SAl Viro extern int os_set_fd_block(int fd, int blocking); 1518569c914SAl Viro extern int os_accept_connection(int fd); 1528569c914SAl Viro extern int os_create_unix_socket(const char *file, int len, int close_on_exec); 1538569c914SAl Viro extern int os_shutdown_socket(int fd, int r, int w); 1548569c914SAl Viro extern void os_close_file(int fd); 1558569c914SAl Viro extern int os_rcv_fd(int fd, int *helper_pid_out); 1568569c914SAl Viro extern int create_unix_socket(char *file, int len, int close_on_exec); 1578569c914SAl Viro extern int os_connect_socket(const char *name); 1588569c914SAl Viro extern int os_file_type(char *file); 1598569c914SAl Viro extern int os_file_mode(const char *file, struct openflags *mode_out); 1608569c914SAl Viro extern int os_lock_file(int fd, int excl); 1618569c914SAl Viro extern void os_flush_stdout(void); 1628569c914SAl Viro extern int os_stat_filesystem(char *path, long *bsize_out, 1638569c914SAl Viro long long *blocks_out, long long *bfree_out, 1648569c914SAl Viro long long *bavail_out, long long *files_out, 1658569c914SAl Viro long long *ffree_out, void *fsid_out, 1668569c914SAl Viro int fsid_size, long *namelen_out, 1678569c914SAl Viro long *spare_out); 1688569c914SAl Viro extern int os_change_dir(char *dir); 1698569c914SAl Viro extern int os_fchange_dir(int fd); 170005a59ecSAl Viro extern unsigned os_major(unsigned long long dev); 171005a59ecSAl Viro extern unsigned os_minor(unsigned long long dev); 172005a59ecSAl Viro extern unsigned long long os_makedev(unsigned major, unsigned minor); 1738569c914SAl Viro 1748569c914SAl Viro /* start_up.c */ 1758569c914SAl Viro extern void os_early_checks(void); 1768569c914SAl Viro extern void can_do_skas(void); 1778569c914SAl Viro extern void os_check_bugs(void); 1788569c914SAl Viro extern void check_host_supports_tls(int *supports_tls, int *tls_min); 1798569c914SAl Viro 1808569c914SAl Viro /* mem.c */ 1818569c914SAl Viro extern int create_mem_file(unsigned long long len); 1828569c914SAl Viro 1838569c914SAl Viro /* process.c */ 1848569c914SAl Viro extern unsigned long os_process_pc(int pid); 1858569c914SAl Viro extern int os_process_parent(int pid); 1868569c914SAl Viro extern void os_stop_process(int pid); 1878569c914SAl Viro extern void os_kill_process(int pid, int reap_child); 1888569c914SAl Viro extern void os_kill_ptraced_process(int pid, int reap_child); 1898569c914SAl Viro extern long os_ptrace_ldt(long pid, long addr, long data); 1908569c914SAl Viro 1918569c914SAl Viro extern int os_getpid(void); 1928569c914SAl Viro extern int os_getpgrp(void); 1938569c914SAl Viro 1948569c914SAl Viro extern void init_new_thread_signals(void); 1958569c914SAl Viro extern int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr); 1968569c914SAl Viro 1978569c914SAl Viro extern int os_map_memory(void *virt, int fd, unsigned long long off, 1988569c914SAl Viro unsigned long len, int r, int w, int x); 1998569c914SAl Viro extern int os_protect_memory(void *addr, unsigned long len, 2008569c914SAl Viro int r, int w, int x); 2018569c914SAl Viro extern int os_unmap_memory(void *addr, int len); 2028569c914SAl Viro extern int os_drop_memory(void *addr, int length); 2038569c914SAl Viro extern int can_drop_memory(void); 2048569c914SAl Viro extern void os_flush_stdout(void); 2058569c914SAl Viro 2068569c914SAl Viro /* execvp.c */ 2078569c914SAl Viro extern int execvp_noalloc(char *buf, const char *file, char *const argv[]); 2088569c914SAl Viro /* helper.c */ 2098569c914SAl Viro extern int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv); 2108569c914SAl Viro extern int run_helper_thread(int (*proc)(void *), void *arg, 2118569c914SAl Viro unsigned int flags, unsigned long *stack_out); 2128569c914SAl Viro extern int helper_wait(int pid); 2138569c914SAl Viro 2148569c914SAl Viro 2158569c914SAl Viro /* tls.c */ 2168569c914SAl Viro extern int os_set_thread_area(user_desc_t *info, int pid); 2178569c914SAl Viro extern int os_get_thread_area(user_desc_t *info, int pid); 2188569c914SAl Viro 2198569c914SAl Viro /* umid.c */ 2208569c914SAl Viro extern int umid_file_name(char *name, char *buf, int len); 2218569c914SAl Viro extern int set_umid(char *name); 2228569c914SAl Viro extern char *get_umid(void); 2238569c914SAl Viro 2248569c914SAl Viro /* signal.c */ 2258569c914SAl Viro extern void timer_init(void); 2268569c914SAl Viro extern void set_sigstack(void *sig_stack, int size); 2278569c914SAl Viro extern void remove_sigstack(void); 228*00361683SAl Viro extern void set_handler(int sig); 2298569c914SAl Viro extern int change_sig(int signal, int on); 2308569c914SAl Viro extern void block_signals(void); 2318569c914SAl Viro extern void unblock_signals(void); 2328569c914SAl Viro extern int get_signals(void); 2338569c914SAl Viro extern int set_signals(int enable); 2348569c914SAl Viro 2358569c914SAl Viro /* util.c */ 2368569c914SAl Viro extern void stack_protections(unsigned long address); 2378569c914SAl Viro extern int raw(int fd); 2388569c914SAl Viro extern void setup_machinename(char *machine_out); 2398569c914SAl Viro extern void setup_hostinfo(char *buf, int len); 2408569c914SAl Viro extern void os_dump_core(void) __attribute__ ((noreturn)); 241d634f194SRichard Weinberger extern void um_early_printk(const char *s, unsigned int n); 2428569c914SAl Viro 2438569c914SAl Viro /* time.c */ 2448569c914SAl Viro extern void idle_sleep(unsigned long long nsecs); 2458569c914SAl Viro extern int set_interval(void); 2468569c914SAl Viro extern int timer_one_shot(int ticks); 2478569c914SAl Viro extern long long disable_timer(void); 2488569c914SAl Viro extern void uml_idle_timer(void); 2498569c914SAl Viro extern long long os_nsecs(void); 2508569c914SAl Viro 2518569c914SAl Viro /* skas/mem.c */ 2528569c914SAl Viro extern long run_syscall_stub(struct mm_id * mm_idp, 2538569c914SAl Viro int syscall, unsigned long *args, long expected, 2548569c914SAl Viro void **addr, int done); 2558569c914SAl Viro extern long syscall_stub_data(struct mm_id * mm_idp, 2568569c914SAl Viro unsigned long *data, int data_count, 2578569c914SAl Viro void **addr, void **stub_addr); 2588569c914SAl Viro extern int map(struct mm_id * mm_idp, unsigned long virt, 2598569c914SAl Viro unsigned long len, int prot, int phys_fd, 2608569c914SAl Viro unsigned long long offset, int done, void **data); 2618569c914SAl Viro extern int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len, 2628569c914SAl Viro int done, void **data); 2638569c914SAl Viro extern int protect(struct mm_id * mm_idp, unsigned long addr, 2648569c914SAl Viro unsigned long len, unsigned int prot, int done, void **data); 2658569c914SAl Viro 2668569c914SAl Viro /* skas/process.c */ 2678569c914SAl Viro extern int is_skas_winch(int pid, int fd, void *data); 2688569c914SAl Viro extern int start_userspace(unsigned long stub_stack); 2698569c914SAl Viro extern int copy_context_skas0(unsigned long stack, int pid); 2708569c914SAl Viro extern void userspace(struct uml_pt_regs *regs); 2718569c914SAl Viro extern int map_stub_pages(int fd, unsigned long code, unsigned long data, 2728569c914SAl Viro unsigned long stack); 2738569c914SAl Viro extern void new_thread(void *stack, jmp_buf *buf, void (*handler)(void)); 2748569c914SAl Viro extern void switch_threads(jmp_buf *me, jmp_buf *you); 2758569c914SAl Viro extern int start_idle_thread(void *stack, jmp_buf *switch_buf); 2768569c914SAl Viro extern void initial_thread_cb_skas(void (*proc)(void *), 2778569c914SAl Viro void *arg); 2788569c914SAl Viro extern void halt_skas(void); 2798569c914SAl Viro extern void reboot_skas(void); 2808569c914SAl Viro 2818569c914SAl Viro /* irq.c */ 2828569c914SAl Viro extern int os_waiting_for_events(struct irq_fd *active_fds); 2838569c914SAl Viro extern int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds); 2848569c914SAl Viro extern void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg, 2858569c914SAl Viro struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2); 2868569c914SAl Viro extern void os_free_irq_later(struct irq_fd *active_fds, 2878569c914SAl Viro int irq, void *dev_id); 2888569c914SAl Viro extern int os_get_pollfd(int i); 2898569c914SAl Viro extern void os_set_pollfd(int i, int fd); 2908569c914SAl Viro extern void os_set_ioignore(void); 2918569c914SAl Viro 2928569c914SAl Viro /* sigio.c */ 2938569c914SAl Viro extern int add_sigio_fd(int fd); 2948569c914SAl Viro extern int ignore_sigio_fd(int fd); 2958569c914SAl Viro extern void maybe_sigio_broken(int fd, int read); 2968569c914SAl Viro extern void sigio_broken(int fd, int read); 2978569c914SAl Viro 2988569c914SAl Viro /* sys-x86_64/prctl.c */ 2998569c914SAl Viro extern int os_arch_prctl(int pid, int code, unsigned long *addr); 3008569c914SAl Viro 3018569c914SAl Viro /* tty.c */ 3028569c914SAl Viro extern int get_pty(void); 3038569c914SAl Viro 3048569c914SAl Viro /* sys-$ARCH/task_size.c */ 3058569c914SAl Viro extern unsigned long os_get_top_address(void); 3068569c914SAl Viro 3078569c914SAl Viro #endif 308