blob: f06a5da71a8fe2123f27423c0e0cecd46cb6cb57 [file] [log] [blame]
Jeff Dike329c6e42006-07-14 00:24:03 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#ifndef __OS_H__
7#define __OS_H__
8
Jeff Dikeba180fd2007-10-16 01:27:00 -07009#include <stdarg.h>
Jeff Dike63ae2a92006-03-27 01:14:30 -080010#include "irq_user.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include "longjmp.h"
12#include "mm_id.h"
Paolo 'Blaisorblade' Giarrussodd77aec2006-03-31 02:30:23 -080013#include "sysdep/tls.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Jeff Dike24fa6c02007-05-06 14:51:09 -070015#define CATCH_EINTR(expr) while ((errno = 0, ((expr) < 0)) && (errno == EINTR))
16
Jeff Dike329c6e42006-07-14 00:24:03 -070017#define OS_TYPE_FILE 1
18#define OS_TYPE_DIR 2
19#define OS_TYPE_SYMLINK 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define OS_TYPE_CHARDEV 4
21#define OS_TYPE_BLOCKDEV 5
22#define OS_TYPE_FIFO 6
23#define OS_TYPE_SOCK 7
24
25/* os_access() flags */
26#define OS_ACC_F_OK 0 /* Test for existence. */
27#define OS_ACC_X_OK 1 /* Test for execute permission. */
28#define OS_ACC_W_OK 2 /* Test for write permission. */
29#define OS_ACC_R_OK 4 /* Test for read permission. */
30#define OS_ACC_RW_OK (OS_ACC_W_OK | OS_ACC_R_OK) /* Test for RW permission */
31
Richard Weinberger0ce451a2011-05-24 17:13:00 -070032#ifdef CONFIG_64BIT
33#define OS_LIB_PATH "/usr/lib64/"
34#else
35#define OS_LIB_PATH "/usr/lib/"
36#endif
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * types taken from stat_file() in hostfs_user.c
40 * (if they are wrong here, they are wrong there...).
41 */
42struct uml_stat {
43 int ust_dev; /* device */
44 unsigned long long ust_ino; /* inode */
45 int ust_mode; /* protection */
46 int ust_nlink; /* number of hard links */
47 int ust_uid; /* user ID of owner */
48 int ust_gid; /* group ID of owner */
49 unsigned long long ust_size; /* total size, in bytes */
50 int ust_blksize; /* blocksize for filesystem I/O */
51 unsigned long long ust_blocks; /* number of blocks allocated */
52 unsigned long ust_atime; /* time of last access */
53 unsigned long ust_mtime; /* time of last modification */
54 unsigned long ust_ctime; /* time of last change */
55};
56
57struct openflags {
58 unsigned int r : 1;
59 unsigned int w : 1;
60 unsigned int s : 1; /* O_SYNC */
61 unsigned int c : 1; /* O_CREAT */
62 unsigned int t : 1; /* O_TRUNC */
63 unsigned int a : 1; /* O_APPEND */
64 unsigned int e : 1; /* O_EXCL */
65 unsigned int cl : 1; /* FD_CLOEXEC */
66};
67
68#define OPENFLAGS() ((struct openflags) { .r = 0, .w = 0, .s = 0, .c = 0, \
Jeff Dike329c6e42006-07-14 00:24:03 -070069 .t = 0, .a = 0, .e = 0, .cl = 0 })
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static inline struct openflags of_read(struct openflags flags)
72{
Jeff Dike329c6e42006-07-14 00:24:03 -070073 flags.r = 1;
74 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static inline struct openflags of_write(struct openflags flags)
78{
Jeff Dike329c6e42006-07-14 00:24:03 -070079 flags.w = 1;
80 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static inline struct openflags of_rdwr(struct openflags flags)
84{
Jeff Dike329c6e42006-07-14 00:24:03 -070085 return of_read(of_write(flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static inline struct openflags of_set_rw(struct openflags flags, int r, int w)
89{
90 flags.r = r;
91 flags.w = w;
Jeff Dike329c6e42006-07-14 00:24:03 -070092 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static inline struct openflags of_sync(struct openflags flags)
Jeff Dike329c6e42006-07-14 00:24:03 -070096{
97 flags.s = 1;
98 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static inline struct openflags of_create(struct openflags flags)
Jeff Dike329c6e42006-07-14 00:24:03 -0700102{
103 flags.c = 1;
104 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Jeff Dike329c6e42006-07-14 00:24:03 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static inline struct openflags of_trunc(struct openflags flags)
Jeff Dike329c6e42006-07-14 00:24:03 -0700108{
109 flags.t = 1;
110 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Jeff Dike329c6e42006-07-14 00:24:03 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static inline struct openflags of_append(struct openflags flags)
Jeff Dike329c6e42006-07-14 00:24:03 -0700114{
115 flags.a = 1;
116 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
Jeff Dike329c6e42006-07-14 00:24:03 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline struct openflags of_excl(struct openflags flags)
Jeff Dike329c6e42006-07-14 00:24:03 -0700120{
121 flags.e = 1;
122 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static inline struct openflags of_cloexec(struct openflags flags)
Jeff Dike329c6e42006-07-14 00:24:03 -0700126{
127 flags.cl = 1;
128 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
Jeff Dike329c6e42006-07-14 00:24:03 -0700130
Jeff Dikecf9165a2006-03-27 01:14:36 -0800131/* file.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132extern int os_stat_file(const char *file_name, struct uml_stat *buf);
133extern int os_stat_fd(const int fd, struct uml_stat *buf);
134extern int os_access(const char *file, int mode);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700135extern int os_set_exec_close(int fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136extern int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137extern int os_get_ifname(int fd, char *namebuf);
138extern int os_set_slip(int fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139extern int os_mode_fd(int fd, int mode);
140
Jeff Dikeba180fd2007-10-16 01:27:00 -0700141extern int os_seek_file(int fd, unsigned long long offset);
WANG Congc9a30722008-02-04 22:30:35 -0800142extern int os_open_file(const char *file, struct openflags flags, int mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143extern int os_read_file(int fd, void *buf, int len);
144extern int os_write_file(int fd, const void *buf, int count);
WANG Congc9a30722008-02-04 22:30:35 -0800145extern int os_file_size(const char *file, unsigned long long *size_out);
146extern int os_file_modtime(const char *file, unsigned long *modtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147extern int os_pipe(int *fd, int stream, int close_on_exec);
Jeff Dikebf8fde72008-02-04 22:31:04 -0800148extern int os_set_fd_async(int fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149extern int os_clear_fd_async(int fd);
150extern int os_set_fd_block(int fd, int blocking);
151extern int os_accept_connection(int fd);
WANG Congc9a30722008-02-04 22:30:35 -0800152extern int os_create_unix_socket(const char *file, int len, int close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153extern int os_shutdown_socket(int fd, int r, int w);
154extern void os_close_file(int fd);
155extern int os_rcv_fd(int fd, int *helper_pid_out);
156extern int create_unix_socket(char *file, int len, int close_on_exec);
WANG Congc9a30722008-02-04 22:30:35 -0800157extern int os_connect_socket(const char *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158extern int os_file_type(char *file);
WANG Congc9a30722008-02-04 22:30:35 -0800159extern int os_file_mode(const char *file, struct openflags *mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160extern int os_lock_file(int fd, int excl);
Jeff Dikecf9165a2006-03-27 01:14:36 -0800161extern void os_flush_stdout(void);
162extern int os_stat_filesystem(char *path, long *bsize_out,
163 long long *blocks_out, long long *bfree_out,
164 long long *bavail_out, long long *files_out,
165 long long *ffree_out, void *fsid_out,
166 int fsid_size, long *namelen_out,
167 long *spare_out);
168extern int os_change_dir(char *dir);
169extern int os_fchange_dir(int fd);
Al Viro005a59e2009-04-21 01:27:08 -0400170extern unsigned os_major(unsigned long long dev);
171extern unsigned os_minor(unsigned long long dev);
172extern unsigned long long os_makedev(unsigned major, unsigned minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700174/* start_up.c */
175extern void os_early_checks(void);
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800176extern void can_do_skas(void);
Jeff Dike8e367062006-03-27 01:14:32 -0800177extern void os_check_bugs(void);
Paolo 'Blaisorblade' Giarrusso3feb8852006-03-31 02:30:25 -0800178extern void check_host_supports_tls(int *supports_tls, int *tls_min);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700179
Jeff Dike0f80bc82005-09-16 19:27:50 -0700180/* mem.c */
Jeff Dikeae173812005-11-07 00:58:57 -0800181extern int create_mem_file(unsigned long long len);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700182
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700183/* process.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184extern unsigned long os_process_pc(int pid);
185extern int os_process_parent(int pid);
186extern void os_stop_process(int pid);
187extern void os_kill_process(int pid, int reap_child);
188extern void os_kill_ptraced_process(int pid, int reap_child);
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800189extern long os_ptrace_ldt(long pid, long addr, long data);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191extern int os_getpid(void);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700192extern int os_getpgrp(void);
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800193
Jeff Dikee64bd132006-07-10 04:45:07 -0700194extern void init_new_thread_signals(void);
Jeff Dikefab95c52007-10-16 01:27:05 -0700195extern int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197extern int os_map_memory(void *virt, int fd, unsigned long long off,
198 unsigned long len, int r, int w, int x);
Jeff Dike329c6e42006-07-14 00:24:03 -0700199extern int os_protect_memory(void *addr, unsigned long len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int r, int w, int x);
201extern int os_unmap_memory(void *addr, int len);
Jeff Dike02dea082006-03-31 02:30:08 -0800202extern int os_drop_memory(void *addr, int length);
203extern int can_drop_memory(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204extern void os_flush_stdout(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Gennady Sharapovbb578422005-11-07 00:58:50 -0800206/* uaccess.c */
207extern unsigned long __do_user_copy(void *to, const void *from, int n,
Jeff Dikefab95c52007-10-16 01:27:05 -0700208 void **fault_addr, jmp_buf **fault_catcher,
Gennady Sharapovbb578422005-11-07 00:58:50 -0800209 void (*op)(void *to, const void *from,
210 int n), int *faulted_out);
211
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800212/* execvp.c */
213extern int execvp_noalloc(char *buf, const char *file, char *const argv[]);
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800214/* helper.c */
Jeff Dikec4399012007-07-15 23:38:56 -0700215extern int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv);
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800216extern int run_helper_thread(int (*proc)(void *), void *arg,
Jeff Dikec4399012007-07-15 23:38:56 -0700217 unsigned int flags, unsigned long *stack_out);
Jeff Dike1aa351a2008-02-04 22:31:10 -0800218extern int helper_wait(int pid);
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800219
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800220
221/* tls.c */
Paolo 'Blaisorblade' Giarrussodd77aec2006-03-31 02:30:23 -0800222extern int os_set_thread_area(user_desc_t *info, int pid);
223extern int os_get_thread_area(user_desc_t *info, int pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Paolo 'Blaisorblade' Giarrussodd77aec2006-03-31 02:30:23 -0800225/* umid.c */
Jeff Dike2264c4752006-01-06 00:18:59 -0800226extern int umid_file_name(char *name, char *buf, int len);
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800227extern int set_umid(char *name);
228extern char *get_umid(void);
Jeff Dike2264c4752006-01-06 00:18:59 -0800229
Gennady Sharapov0805d892006-01-08 01:01:29 -0800230/* signal.c */
Jeff Dike78a26e22007-10-16 01:27:23 -0700231extern void timer_init(void);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800232extern void set_sigstack(void *sig_stack, int size);
233extern void remove_sigstack(void);
234extern void set_handler(int sig, void (*handler)(int), int flags, ...);
235extern int change_sig(int signal, int on);
236extern void block_signals(void);
237extern void unblock_signals(void);
238extern int get_signals(void);
239extern int set_signals(int enable);
240
Gennady Sharapov4fef0c12006-01-18 17:42:41 -0800241/* util.c */
242extern void stack_protections(unsigned long address);
Gennady Sharapov4fef0c12006-01-18 17:42:41 -0800243extern int raw(int fd);
244extern void setup_machinename(char *machine_out);
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -0700245extern void setup_hostinfo(char *buf, int len);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800246extern void os_dump_core(void) __attribute__ ((noreturn));
Gennady Sharapov4fef0c12006-01-18 17:42:41 -0800247
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800248/* time.c */
Jeff Dikeb160fb62007-10-16 01:27:26 -0700249extern void idle_sleep(unsigned long long nsecs);
Jeff Dikea2f018b2007-10-16 01:27:22 -0700250extern int set_interval(void);
Jeff Diked2753a6d2007-10-16 01:27:25 -0700251extern int timer_one_shot(int ticks);
Jeff Dike5f734612007-10-16 01:27:27 -0700252extern long long disable_timer(void);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800253extern void uml_idle_timer(void);
Jeff Dike5f734612007-10-16 01:27:27 -0700254extern long long os_nsecs(void);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800255
Gennady Sharapovf45d9fc2006-01-18 17:42:45 -0800256/* skas/mem.c */
257extern long run_syscall_stub(struct mm_id * mm_idp,
258 int syscall, unsigned long *args, long expected,
259 void **addr, int done);
260extern long syscall_stub_data(struct mm_id * mm_idp,
261 unsigned long *data, int data_count,
262 void **addr, void **stub_addr);
263extern int map(struct mm_id * mm_idp, unsigned long virt,
Jeff Dike16dd07b2007-05-06 14:51:48 -0700264 unsigned long len, int prot, int phys_fd,
Gennady Sharapovf45d9fc2006-01-18 17:42:45 -0800265 unsigned long long offset, int done, void **data);
Jeff Dike64f60842007-05-06 14:51:45 -0700266extern int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
Gennady Sharapovf45d9fc2006-01-18 17:42:45 -0800267 int done, void **data);
268extern int protect(struct mm_id * mm_idp, unsigned long addr,
Jeff Dike16dd07b2007-05-06 14:51:48 -0700269 unsigned long len, unsigned int prot, int done, void **data);
Gennady Sharapovf45d9fc2006-01-18 17:42:45 -0800270
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800271/* skas/process.c */
272extern int is_skas_winch(int pid, int fd, void *data);
273extern int start_userspace(unsigned long stub_stack);
274extern int copy_context_skas0(unsigned long stack, int pid);
Jeff Dike77bf4402007-10-16 01:26:58 -0700275extern void userspace(struct uml_pt_regs *regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800276extern int map_stub_pages(int fd, unsigned long code, unsigned long data,
277 unsigned long stack);
Jeff Dike3c917352006-09-27 01:50:40 -0700278extern void new_thread(void *stack, jmp_buf *buf, void (*handler)(void));
279extern void switch_threads(jmp_buf *me, jmp_buf *you);
280extern int start_idle_thread(void *stack, jmp_buf *switch_buf);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800281extern void initial_thread_cb_skas(void (*proc)(void *),
282 void *arg);
283extern void halt_skas(void);
284extern void reboot_skas(void);
285
Jeff Dike63ae2a92006-03-27 01:14:30 -0800286/* irq.c */
287extern int os_waiting_for_events(struct irq_fd *active_fds);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800288extern int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds);
289extern void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
290 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2);
291extern void os_free_irq_later(struct irq_fd *active_fds,
292 int irq, void *dev_id);
293extern int os_get_pollfd(int i);
294extern void os_set_pollfd(int i, int fd);
295extern void os_set_ioignore(void);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800296
Jeff Dikef206aab2006-03-27 01:14:33 -0800297/* sigio.c */
Jeff Dike19bdf042006-09-25 23:33:04 -0700298extern int add_sigio_fd(int fd);
Jeff Dikef206aab2006-03-27 01:14:33 -0800299extern int ignore_sigio_fd(int fd);
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700300extern void maybe_sigio_broken(int fd, int read);
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700301extern void sigio_broken(int fd, int read);
Jeff Dikef206aab2006-03-27 01:14:33 -0800302
Jeff Dike24fa6c02007-05-06 14:51:09 -0700303/* sys-x86_64/prctl.c */
Jeff Dikef3555592007-02-10 01:44:29 -0800304extern int os_arch_prctl(int pid, int code, unsigned long *addr);
305
Jeff Dike24fa6c02007-05-06 14:51:09 -0700306/* tty.c */
Jeff Dike536788f2008-02-08 04:22:07 -0800307extern int get_pty(void);
308
309/* sys-$ARCH/task_size.c */
Tom Spink40fb16a2008-06-05 22:46:12 -0700310extern unsigned long os_get_top_address(void);
Jeff Dike24fa6c02007-05-06 14:51:09 -0700311
Jeff Dike2264c4752006-01-06 00:18:59 -0800312#endif