1 /* $NetBSD: thunk.h,v 1.61 2012/03/03 21:15:16 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_exit(int); 96 void thunk_abort(void); 97 98 int thunk_geterrno(void); 99 void thunk_seterrno(int err); 100 101 int thunk_getcontext(ucontext_t *); 102 int thunk_setcontext(const ucontext_t *); 103 void thunk_makecontext(ucontext_t *ucp, void (*func)(void), 104 int nargs, void *arg1, void *arg2, void *arg3, void *arg4); 105 int thunk_swapcontext(ucontext_t *, ucontext_t *); 106 107 int thunk_tcgetattr(int, struct thunk_termios *); 108 int thunk_tcsetattr(int, int, const struct thunk_termios *); 109 110 int thunk_set_stdin_sigio(int); 111 int thunk_pollchar(void); 112 int thunk_getchar(void); 113 void thunk_putchar(int); 114 115 int thunk_execv(const char *, char * const []); 116 117 int thunk_open(const char *, int, mode_t); 118 int thunk_close(int); 119 int thunk_fstat_getsize(int, off_t *, ssize_t *); 120 ssize_t thunk_pread(int, void *, size_t, off_t); 121 ssize_t thunk_pwrite(int, const void *, size_t, off_t); 122 ssize_t thunk_read(int, void *, size_t); 123 ssize_t thunk_write(int, const void *, size_t); 124 int thunk_fsync(int); 125 int thunk_mkstemp(char *); 126 int thunk_unlink(const char *); 127 pid_t thunk_getpid(void); 128 129 int thunk_sigaction(int, const struct sigaction *, struct sigaction *); 130 int thunk_sigaltstack(const stack_t *, stack_t *); 131 void thunk_signal(int, void (*)(int)); 132 int thunk_sigblock(int); 133 int thunk_sigunblock(int); 134 int thunk_sigemptyset(sigset_t *sa_mask); 135 int thunk_sigfillset(sigset_t *sa_mask); 136 void thunk_sigaddset(sigset_t *sa_mask, int sig); 137 int thunk_sigprocmask(int how, const sigset_t * set, sigset_t *oset); 138 int thunk_atexit(void (*function)(void)); 139 140 int thunk_aio_read(struct aiocb *); 141 int thunk_aio_write(struct aiocb *); 142 int thunk_aio_error(const struct aiocb *); 143 int thunk_aio_return(struct aiocb *); 144 145 void * thunk_malloc(size_t len); 146 void thunk_free(void *addr); 147 void * thunk_sbrk(intptr_t len); 148 void * thunk_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); 149 int thunk_munmap(void *addr, size_t len); 150 int thunk_mprotect(void *addr, size_t len, int prot); 151 int thunk_madvise(void *addr, size_t len, int behav); 152 int thunk_posix_memalign(void **, size_t, size_t); 153 154 int thunk_idle(void); 155 156 char * thunk_getenv(const char *); 157 vaddr_t thunk_get_vm_min_address(void); 158 159 int thunk_getcpuinfo(char *, int *); 160 161 int thunk_getmachine(char *, size_t, char *, size_t); 162 163 int thunk_setown(int); 164 165 int thunk_open_tap(const char *); 166 int thunk_pollin_tap(int, int); 167 int thunk_pollout_tap(int, int); 168 169 typedef struct { 170 unsigned int sample_rate; 171 unsigned int precision; 172 unsigned int validbits; 173 unsigned int channels; 174 } thunk_audio_config_t; 175 176 int thunk_audio_open(const char *); 177 int thunk_audio_close(int); 178 int thunk_audio_drain(int); 179 int thunk_audio_config(int, const thunk_audio_config_t *, 180 const thunk_audio_config_t *); 181 int thunk_audio_pollout(int); 182 int thunk_audio_pollin(int); 183 ssize_t thunk_audio_write(int, const void *, size_t); 184 ssize_t thunk_audio_read(int, void *, size_t); 185 186 typedef enum { 187 /* client -> server */ 188 THUNK_RFB_SET_PIXEL_FORMAT = 0, 189 THUNK_RFB_SET_ENCODINGS = 2, 190 THUNK_RFB_FRAMEBUFFER_UPDATE_REQUEST = 3, 191 THUNK_RFB_KEY_EVENT = 4, 192 THUNK_RFB_POINTER_EVENT = 5, 193 THUNK_RFB_CLIENT_CUT_TEXT = 6, 194 } thunk_rfb_message_t; 195 196 typedef struct { 197 thunk_rfb_message_t message_type; 198 union { 199 struct { 200 uint8_t down_flag; 201 uint32_t keysym; 202 } key_event; 203 struct { 204 uint8_t button_mask; 205 uint16_t absx; 206 uint16_t absy; 207 } pointer_event; 208 } data; 209 } thunk_rfb_event_t; 210 211 212 typedef struct { 213 uint8_t enc; 214 uint16_t x, y, w, h; 215 uint16_t srcx, srcy; 216 uint8_t pixel[4]; 217 } thunk_rfb_update_t; 218 #define THUNK_RFB_TYPE_RAW 0 219 #define THUNK_RFB_TYPE_COPYRECT 1 220 #define THUNK_RFB_TYPE_RRE 2 /* rectangle fill */ 221 222 #define THUNK_RFB_QUEUELEN 128 223 224 typedef struct { 225 int sockfd; 226 int clientfd; 227 thunk_rfb_event_t event; 228 229 bool connected; 230 231 uint16_t width; 232 uint16_t height; 233 uint8_t depth; 234 char name[64]; 235 uint8_t *framebuf; 236 237 bool schedule_bell; 238 unsigned int nupdates; 239 unsigned int first_mergable; 240 thunk_rfb_update_t update[THUNK_RFB_QUEUELEN]; 241 } thunk_rfb_t; 242 243 int thunk_rfb_open(thunk_rfb_t *, uint16_t); 244 int thunk_rfb_poll(thunk_rfb_t *, thunk_rfb_event_t *); 245 void thunk_rfb_bell(thunk_rfb_t *); 246 void thunk_rfb_update(thunk_rfb_t *, int, int, int, int); 247 void thunk_rfb_copyrect(thunk_rfb_t *, int, int, int, int, int, int); 248 void thunk_rfb_fillrect(thunk_rfb_t *, int, int, int, int, uint8_t *); 249 250 #endif /* !_ARCH_USERMODE_INCLUDE_THUNK_H */ 251