xref: /netbsd/sys/arch/usermode/include/thunk.h (revision b950503f)
1 /* $NetBSD: thunk.h,v 1.67 2019/05/08 13:40:16 isaki 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_gdb_open(void);
131 int	thunk_gdb_accept(int sockfd);
132 int	thunk_kgdb_getc(int fd, char *ch);
133 int	thunk_kgdb_putc(int fd, char ch);
134 
135 int	thunk_sigaction(int, const struct sigaction *, struct sigaction *);
136 int	thunk_sigaltstack(const stack_t *, stack_t *);
137 void	thunk_signal(int, void (*)(int));
138 int	thunk_sigblock(int);
139 int	thunk_sigunblock(int);
140 int	thunk_sigemptyset(sigset_t *sa_mask);
141 int	thunk_sigfillset(sigset_t *sa_mask);
142 void	thunk_sigaddset(sigset_t *sa_mask, int sig);
143 int	thunk_sigprocmask(int how, const sigset_t * set, sigset_t *oset);
144 int	thunk_atexit(void (*function)(void));
145 pid_t	thunk_fork(void);
146 int	thunk_ioctl(int fd, unsigned long request, void *opaque);
147 
148 int	thunk_aio_read(struct aiocb *);
149 int	thunk_aio_write(struct aiocb *);
150 int	thunk_aio_error(const struct aiocb *);
151 int	thunk_aio_return(struct aiocb *);
152 
153 void *	thunk_malloc(size_t len);
154 void 	thunk_free(void *addr);
155 void *	thunk_sbrk(intptr_t len);
156 void *	thunk_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
157 int	thunk_munmap(void *addr, size_t len);
158 int	thunk_mprotect(void *addr, size_t len, int prot);
159 int	thunk_madvise(void *addr, size_t len, int behav);
160 int	thunk_posix_memalign(void **, size_t, size_t);
161 
162 int	thunk_idle(void);
163 
164 char *	thunk_getenv(const char *);
165 vaddr_t	thunk_get_vm_min_address(void);
166 
167 int	thunk_getcpuinfo(char *, size_t *);
168 
169 int	thunk_getmachine(char *, size_t, char *, size_t);
170 
171 int	thunk_setown(int);
172 
173 int	thunk_open_tap(const char *);
174 int	thunk_pollin_tap(int, int);
175 int	thunk_pollout_tap(int, int);
176 
177 int	thunk_assert_presence(vaddr_t from, size_t size);
178 
179 typedef struct {
180 	unsigned int		sample_rate;
181 	unsigned int		precision;
182 	unsigned int		validbits;
183 	unsigned int		channels;
184 } thunk_audio_config_t;
185 
186 int	thunk_audio_open(const char *);
187 int	thunk_audio_close(int);
188 int	thunk_audio_config(int, const thunk_audio_config_t *,
189 			   const thunk_audio_config_t *);
190 int	thunk_audio_pollout(int);
191 int	thunk_audio_pollin(int);
192 ssize_t	thunk_audio_write(int, const void *, size_t);
193 ssize_t	thunk_audio_read(int, void *, size_t);
194 
195 typedef enum {
196 	/* client -> server */
197 	THUNK_RFB_SET_PIXEL_FORMAT = 0,
198 	THUNK_RFB_SET_ENCODINGS = 2,
199 	THUNK_RFB_FRAMEBUFFER_UPDATE_REQUEST = 3,
200 	THUNK_RFB_KEY_EVENT = 4,
201 	THUNK_RFB_POINTER_EVENT = 5,
202 	THUNK_RFB_CLIENT_CUT_TEXT = 6,
203 } thunk_rfb_message_t;
204 
205 typedef struct {
206 	thunk_rfb_message_t	message_type;
207 	union {
208 		struct {
209 			uint8_t		down_flag;
210 			uint32_t	keysym;
211 		} key_event;
212 		struct {
213 			uint8_t		button_mask;
214 			uint16_t	absx;
215 			uint16_t	absy;
216 		} pointer_event;
217 	} data;
218 } thunk_rfb_event_t;
219 
220 
221 typedef struct {
222 	uint8_t			enc;
223 	uint16_t		x, y, w, h;
224 	uint16_t		srcx, srcy;
225 	uint8_t			pixel[4];
226 } thunk_rfb_update_t;
227 #define THUNK_RFB_TYPE_RAW	0
228 #define THUNK_RFB_TYPE_COPYRECT	1
229 #define THUNK_RFB_TYPE_RRE	2		/* rectangle fill */
230 
231 #define THUNK_RFB_QUEUELEN	128
232 
233 typedef struct {
234 	int			sockfd;
235 	int			clientfd;
236 	thunk_rfb_event_t	event;
237 
238 	bool			connected;
239 
240 	uint16_t		width;
241 	uint16_t		height;
242 	uint8_t			depth;
243 	char			name[64];
244 	uint8_t			*framebuf;
245 
246 	bool			schedule_bell;
247 	unsigned int		nupdates;
248 	unsigned int		first_mergable;
249 	thunk_rfb_update_t	update[THUNK_RFB_QUEUELEN];
250 } thunk_rfb_t;
251 
252 int	thunk_rfb_open(thunk_rfb_t *, uint16_t);
253 int	thunk_rfb_poll(thunk_rfb_t *, thunk_rfb_event_t *);
254 void	thunk_rfb_bell(thunk_rfb_t *);
255 void	thunk_rfb_update(thunk_rfb_t *, int, int, int, int);
256 void	thunk_rfb_copyrect(thunk_rfb_t *, int, int, int, int, int, int);
257 void	thunk_rfb_fillrect(thunk_rfb_t *, int, int, int, int, uint8_t *);
258 
259 #endif /* !_ARCH_USERMODE_INCLUDE_THUNK_H */
260