xref: /netbsd-src/sys/arch/usermode/include/thunk.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: thunk.h,v 1.65 2018/06/04 19:53:01 reinoud Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _ARCH_USERMODE_INCLUDE_THUNK_H
30 #define _ARCH_USERMODE_INCLUDE_THUNK_H
31 
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/stat.h>
35 #include <sys/fcntl.h>
36 #include <sys/ucontext.h>
37 #include <sys/signal.h>
38 
39 struct thunk_timeval {
40 	int64_t tv_sec;
41 	int32_t tv_usec;
42 };
43 
44 struct thunk_itimerval {
45 	struct thunk_timeval it_interval;
46 	struct thunk_timeval it_value;
47 };
48 
49 struct thunk_termios {
50 	uint32_t c_iflag;
51 	uint32_t c_oflag;
52 	uint32_t c_cflag;
53 	uint32_t c_lflag;
54 	uint8_t c_cc[20];
55 	int32_t c_ispeed;
56 	int32_t c_ospeed;
57 };
58 
59 #define THUNK_MAP_ANON		0x0001
60 #define THUNK_MAP_FIXED		0x0002
61 #define THUNK_MAP_FILE		0x0004
62 #define THUNK_MAP_SHARED	0x0010
63 #define THUNK_MAP_PRIVATE	0x0020
64 
65 #define THUNK_PROT_NONE		0x00
66 #define THUNK_PROT_READ		0x01
67 #define THUNK_PROT_WRITE	0x02
68 #define THUNK_PROT_EXEC		0x04
69 
70 #define THUNK_MADV_NORMAL	0x01
71 #define THUNK_MADV_RANDOM	0x02
72 #define THUNK_MADV_SEQUENTIAL	0x04
73 #define THUNK_MADV_WILLNEED	0x08
74 #define THUNK_MADV_DONTNEED	0x10
75 #define THUNK_MADV_FREE		0x20
76 
77 
78 struct aiocb;
79 
80 void	thunk_printf_debug(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
81 void	thunk_printf(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
82 
83 int	thunk_syscallemu_init(void *, void *);
84 
85 int	thunk_setitimer(int, const struct thunk_itimerval *, struct thunk_itimerval *);
86 int	thunk_gettimeofday(struct thunk_timeval *, void *);
87 unsigned int thunk_getcounter(void);
88 long	thunk_clock_getres_monotonic(void);
89 int	thunk_usleep(useconds_t);
90 
91 timer_t	thunk_timer_attach(void);
92 int	thunk_timer_start(timer_t, int);
93 int	thunk_timer_getoverrun(timer_t);
94 
95 void	thunk_kill(pid_t pid, int sig);
96 void	thunk_exit(int);
97 void	thunk_abort(void);
98 
99 int	thunk_geterrno(void);
100 void	thunk_seterrno(int err);
101 
102 int	thunk_getcontext(ucontext_t *);
103 int	thunk_setcontext(const ucontext_t *);
104 void	thunk_makecontext(ucontext_t *ucp, void (*func)(void),
105 		int nargs, void *arg1, void *arg2, void *arg3, void *arg4);
106 int	thunk_swapcontext(ucontext_t *, ucontext_t *);
107 
108 int	thunk_tcgetattr(int, struct thunk_termios *);
109 int	thunk_tcsetattr(int, int, const struct thunk_termios *);
110 
111 int	thunk_set_stdin_sigio(int);
112 int	thunk_pollchar(void);
113 int	thunk_getchar(void);
114 void	thunk_putchar(int);
115 
116 int	thunk_execv(const char *, char * const []);
117 
118 int	thunk_open(const char *, int, mode_t);
119 int	thunk_close(int);
120 int	thunk_fstat_getsize(int, off_t *, ssize_t *);
121 ssize_t	thunk_pread(int, void *, size_t, off_t);
122 ssize_t	thunk_pwrite(int, const void *, size_t, off_t);
123 ssize_t	thunk_read(int, void *, size_t);
124 ssize_t	thunk_write(int, const void *, size_t);
125 int	thunk_fsync(int);
126 int	thunk_mkstemp(char *);
127 int	thunk_unlink(const char *);
128 pid_t	thunk_getpid(void);
129 
130 int	thunk_sigaction(int, const struct sigaction *, struct sigaction *);
131 int	thunk_sigaltstack(const stack_t *, stack_t *);
132 void	thunk_signal(int, void (*)(int));
133 int	thunk_sigblock(int);
134 int	thunk_sigunblock(int);
135 int	thunk_sigemptyset(sigset_t *sa_mask);
136 int	thunk_sigfillset(sigset_t *sa_mask);
137 void	thunk_sigaddset(sigset_t *sa_mask, int sig);
138 int	thunk_sigprocmask(int how, const sigset_t * set, sigset_t *oset);
139 int	thunk_atexit(void (*function)(void));
140 pid_t	thunk_fork(void);
141 int	thunk_ioctl(int fd, unsigned long request, void *opaque);
142 
143 int	thunk_aio_read(struct aiocb *);
144 int	thunk_aio_write(struct aiocb *);
145 int	thunk_aio_error(const struct aiocb *);
146 int	thunk_aio_return(struct aiocb *);
147 
148 void *	thunk_malloc(size_t len);
149 void 	thunk_free(void *addr);
150 void *	thunk_sbrk(intptr_t len);
151 void *	thunk_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
152 int	thunk_munmap(void *addr, size_t len);
153 int	thunk_mprotect(void *addr, size_t len, int prot);
154 int	thunk_madvise(void *addr, size_t len, int behav);
155 int	thunk_posix_memalign(void **, size_t, size_t);
156 
157 int	thunk_idle(void);
158 
159 char *	thunk_getenv(const char *);
160 vaddr_t	thunk_get_vm_min_address(void);
161 
162 int	thunk_getcpuinfo(char *, size_t *);
163 
164 int	thunk_getmachine(char *, size_t, char *, size_t);
165 
166 int	thunk_setown(int);
167 
168 int	thunk_open_tap(const char *);
169 int	thunk_pollin_tap(int, int);
170 int	thunk_pollout_tap(int, int);
171 
172 int	thunk_assert_presence(vaddr_t from, size_t size);
173 
174 typedef struct {
175 	unsigned int		sample_rate;
176 	unsigned int		precision;
177 	unsigned int		validbits;
178 	unsigned int		channels;
179 } thunk_audio_config_t;
180 
181 int	thunk_audio_open(const char *);
182 int	thunk_audio_close(int);
183 int	thunk_audio_drain(int);
184 int	thunk_audio_config(int, const thunk_audio_config_t *,
185 			   const thunk_audio_config_t *);
186 int	thunk_audio_pollout(int);
187 int	thunk_audio_pollin(int);
188 ssize_t	thunk_audio_write(int, const void *, size_t);
189 ssize_t	thunk_audio_read(int, void *, size_t);
190 
191 typedef enum {
192 	/* client -> server */
193 	THUNK_RFB_SET_PIXEL_FORMAT = 0,
194 	THUNK_RFB_SET_ENCODINGS = 2,
195 	THUNK_RFB_FRAMEBUFFER_UPDATE_REQUEST = 3,
196 	THUNK_RFB_KEY_EVENT = 4,
197 	THUNK_RFB_POINTER_EVENT = 5,
198 	THUNK_RFB_CLIENT_CUT_TEXT = 6,
199 } thunk_rfb_message_t;
200 
201 typedef struct {
202 	thunk_rfb_message_t	message_type;
203 	union {
204 		struct {
205 			uint8_t		down_flag;
206 			uint32_t	keysym;
207 		} key_event;
208 		struct {
209 			uint8_t		button_mask;
210 			uint16_t	absx;
211 			uint16_t	absy;
212 		} pointer_event;
213 	} data;
214 } thunk_rfb_event_t;
215 
216 
217 typedef struct {
218 	uint8_t			enc;
219 	uint16_t		x, y, w, h;
220 	uint16_t		srcx, srcy;
221 	uint8_t			pixel[4];
222 } thunk_rfb_update_t;
223 #define THUNK_RFB_TYPE_RAW	0
224 #define THUNK_RFB_TYPE_COPYRECT	1
225 #define THUNK_RFB_TYPE_RRE	2		/* rectangle fill */
226 
227 #define THUNK_RFB_QUEUELEN	128
228 
229 typedef struct {
230 	int			sockfd;
231 	int			clientfd;
232 	thunk_rfb_event_t	event;
233 
234 	bool			connected;
235 
236 	uint16_t		width;
237 	uint16_t		height;
238 	uint8_t			depth;
239 	char			name[64];
240 	uint8_t			*framebuf;
241 
242 	bool			schedule_bell;
243 	unsigned int		nupdates;
244 	unsigned int		first_mergable;
245 	thunk_rfb_update_t	update[THUNK_RFB_QUEUELEN];
246 } thunk_rfb_t;
247 
248 int	thunk_rfb_open(thunk_rfb_t *, uint16_t);
249 int	thunk_rfb_poll(thunk_rfb_t *, thunk_rfb_event_t *);
250 void	thunk_rfb_bell(thunk_rfb_t *);
251 void	thunk_rfb_update(thunk_rfb_t *, int, int, int, int);
252 void	thunk_rfb_copyrect(thunk_rfb_t *, int, int, int, int, int, int);
253 void	thunk_rfb_fillrect(thunk_rfb_t *, int, int, int, int, uint8_t *);
254 
255 #endif /* !_ARCH_USERMODE_INCLUDE_THUNK_H */
256