more endianness macros
[qemu] / vl.h
1 /*
2  * QEMU System Emulator header
3  * 
4  * Copyright (c) 2003 Fabrice Bellard
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #ifndef VL_H
25 #define VL_H
26
27 /* we put basic includes here to avoid repeating them in device drivers */
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <inttypes.h>
33 #include <time.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #ifndef O_LARGEFILE
40 #define O_LARGEFILE 0
41 #endif
42 #ifndef O_BINARY
43 #define O_BINARY 0
44 #endif
45
46 #ifdef _WIN32
47 #define lseek64 _lseeki64
48 #endif
49
50 #include "cpu.h"
51
52 #ifndef glue
53 #define xglue(x, y) x ## y
54 #define glue(x, y) xglue(x, y)
55 #define stringify(s)    tostring(s)
56 #define tostring(s)     #s
57 #endif
58
59 #if defined(WORDS_BIGENDIAN)
60 static inline uint32_t be32_to_cpu(uint32_t v)
61 {
62     return v;
63 }
64
65 static inline uint16_t be16_to_cpu(uint16_t v)
66 {
67     return v;
68 }
69
70 static inline uint32_t cpu_to_be32(uint32_t v)
71 {
72     return v;
73 }
74
75 static inline uint16_t cpu_to_be16(uint16_t v)
76 {
77     return v;
78 }
79
80 static inline uint32_t le32_to_cpu(uint32_t v)
81 {
82     return bswap32(v);
83 }
84
85 static inline uint16_t le16_to_cpu(uint16_t v)
86 {
87     return bswap16(v);
88 }
89
90 static inline uint32_t cpu_to_le32(uint32_t v)
91 {
92     return bswap32(v);
93 }
94
95 static inline uint16_t cpu_to_le16(uint16_t v)
96 {
97     return bswap16(v);
98 }
99
100 #else
101
102 static inline uint32_t be32_to_cpu(uint32_t v)
103 {
104     return bswap32(v);
105 }
106
107 static inline uint16_t be16_to_cpu(uint16_t v)
108 {
109     return bswap16(v);
110 }
111
112 static inline uint32_t cpu_to_be32(uint32_t v)
113 {
114     return bswap32(v);
115 }
116
117 static inline uint16_t cpu_to_be16(uint16_t v)
118 {
119     return bswap16(v);
120 }
121
122 static inline uint32_t le32_to_cpu(uint32_t v)
123 {
124     return v;
125 }
126
127 static inline uint16_t le16_to_cpu(uint16_t v)
128 {
129     return v;
130 }
131
132 static inline uint32_t cpu_to_le32(uint32_t v)
133 {
134     return v;
135 }
136
137 static inline uint16_t cpu_to_le16(uint16_t v)
138 {
139     return v;
140 }
141 #endif
142
143
144 /* vl.c */
145 extern int reset_requested;
146
147 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
148 typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
149
150 int register_ioport_read(int start, int length, int size, 
151                          IOPortReadFunc *func, void *opaque);
152 int register_ioport_write(int start, int length, int size, 
153                           IOPortWriteFunc *func, void *opaque);
154 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
155
156 void hw_error(const char *fmt, ...);
157
158 int load_image(const char *filename, uint8_t *addr);
159 extern const char *bios_dir;
160
161 void pstrcpy(char *buf, int buf_size, const char *str);
162 char *pstrcat(char *buf, int buf_size, const char *s);
163
164 int serial_open_device(void);
165
166 extern int vm_running;
167
168 typedef void VMStopHandler(void *opaque, int reason);
169
170 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque);
171 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque);
172
173 void vm_start(void);
174 void vm_stop(int reason);
175
176 /* async I/O support */
177
178 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
179 typedef int IOCanRWHandler(void *opaque);
180
181 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, 
182                              IOReadHandler *fd_read, void *opaque);
183 void qemu_del_fd_read_handler(int fd);
184
185 /* network redirectors support */
186
187 #define MAX_NICS 8
188
189 typedef struct NetDriverState {
190     int index; /* index number in QEMU */
191     uint8_t macaddr[6];
192     char ifname[16];
193     void (*send_packet)(struct NetDriverState *nd, 
194                         const uint8_t *buf, int size);
195     void (*add_read_packet)(struct NetDriverState *nd, 
196                             IOCanRWHandler *fd_can_read, 
197                             IOReadHandler *fd_read, void *opaque);
198     /* tun specific data */
199     int fd;
200     /* slirp specific data */
201 } NetDriverState;
202
203 extern int nb_nics;
204 extern NetDriverState nd_table[MAX_NICS];
205
206 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
207 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, 
208                           IOReadHandler *fd_read, void *opaque);
209
210 /* timers */
211
212 typedef struct QEMUClock QEMUClock;
213 typedef struct QEMUTimer QEMUTimer;
214 typedef void QEMUTimerCB(void *opaque);
215
216 /* The real time clock should be used only for stuff which does not
217    change the virtual machine state, as it is run even if the virtual
218    machine is stopped. The real time clock has a frequency or 1000
219    Hz. */
220 extern QEMUClock *rt_clock;
221
222 /* Rge virtual clock is only run during the emulation. It is stopped
223    when the virtual machine is stopped. Virtual timers use a high
224    precision clock, usually cpu cycles (use ticks_per_sec). */
225 extern QEMUClock *vm_clock;
226
227 int64_t qemu_get_clock(QEMUClock *clock);
228
229 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
230 void qemu_free_timer(QEMUTimer *ts);
231 void qemu_del_timer(QEMUTimer *ts);
232 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
233 int qemu_timer_pending(QEMUTimer *ts);
234
235 extern int64_t ticks_per_sec;
236 extern int pit_min_timer_count;
237
238 void cpu_enable_ticks(void);
239 void cpu_disable_ticks(void);
240
241 /* VM Load/Save */
242
243 typedef FILE QEMUFile;
244
245 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
246 void qemu_put_byte(QEMUFile *f, int v);
247 void qemu_put_be16(QEMUFile *f, unsigned int v);
248 void qemu_put_be32(QEMUFile *f, unsigned int v);
249 void qemu_put_be64(QEMUFile *f, uint64_t v);
250 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
251 int qemu_get_byte(QEMUFile *f);
252 unsigned int qemu_get_be16(QEMUFile *f);
253 unsigned int qemu_get_be32(QEMUFile *f);
254 uint64_t qemu_get_be64(QEMUFile *f);
255
256 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
257 {
258     qemu_put_be64(f, *pv);
259 }
260
261 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
262 {
263     qemu_put_be32(f, *pv);
264 }
265
266 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
267 {
268     qemu_put_be16(f, *pv);
269 }
270
271 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
272 {
273     qemu_put_byte(f, *pv);
274 }
275
276 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
277 {
278     *pv = qemu_get_be64(f);
279 }
280
281 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
282 {
283     *pv = qemu_get_be32(f);
284 }
285
286 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
287 {
288     *pv = qemu_get_be16(f);
289 }
290
291 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
292 {
293     *pv = qemu_get_byte(f);
294 }
295
296 int64_t qemu_ftell(QEMUFile *f);
297 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
298
299 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
300 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
301
302 int qemu_loadvm(const char *filename);
303 int qemu_savevm(const char *filename);
304 int register_savevm(const char *idstr, 
305                     int instance_id, 
306                     int version_id,
307                     SaveStateHandler *save_state,
308                     LoadStateHandler *load_state,
309                     void *opaque);
310 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
311 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
312
313 /* block.c */
314 typedef struct BlockDriverState BlockDriverState;
315
316 BlockDriverState *bdrv_new(const char *device_name);
317 void bdrv_delete(BlockDriverState *bs);
318 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot);
319 void bdrv_close(BlockDriverState *bs);
320 int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
321               uint8_t *buf, int nb_sectors);
322 int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
323                const uint8_t *buf, int nb_sectors);
324 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr);
325 int bdrv_commit(BlockDriverState *bs);
326 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
327
328 #define BDRV_TYPE_HD     0
329 #define BDRV_TYPE_CDROM  1
330 #define BDRV_TYPE_FLOPPY 2
331
332 void bdrv_set_geometry_hint(BlockDriverState *bs, 
333                             int cyls, int heads, int secs);
334 void bdrv_set_type_hint(BlockDriverState *bs, int type);
335 void bdrv_get_geometry_hint(BlockDriverState *bs, 
336                             int *pcyls, int *pheads, int *psecs);
337 int bdrv_get_type_hint(BlockDriverState *bs);
338 int bdrv_is_removable(BlockDriverState *bs);
339 int bdrv_is_read_only(BlockDriverState *bs);
340 int bdrv_is_inserted(BlockDriverState *bs);
341 int bdrv_is_locked(BlockDriverState *bs);
342 void bdrv_set_locked(BlockDriverState *bs, int locked);
343 void bdrv_set_change_cb(BlockDriverState *bs, 
344                         void (*change_cb)(void *opaque), void *opaque);
345
346 void bdrv_info(void);
347 BlockDriverState *bdrv_find(const char *name);
348
349 /* vga.c */
350
351 #define VGA_RAM_SIZE (4096 * 1024)
352
353 typedef struct DisplayState {
354     uint8_t *data;
355     int linesize;
356     int depth;
357     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
358     void (*dpy_resize)(struct DisplayState *s, int w, int h);
359     void (*dpy_refresh)(struct DisplayState *s);
360 } DisplayState;
361
362 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
363 {
364     s->dpy_update(s, x, y, w, h);
365 }
366
367 static inline void dpy_resize(DisplayState *s, int w, int h)
368 {
369     s->dpy_resize(s, w, h);
370 }
371
372 int vga_initialize(DisplayState *ds, uint8_t *vga_ram_base, 
373                    unsigned long vga_ram_offset, int vga_ram_size);
374 void vga_update_display(void);
375 void vga_screen_dump(const char *filename);
376
377 /* sdl.c */
378 void sdl_display_init(DisplayState *ds);
379
380 /* ide.c */
381 #define MAX_DISKS 4
382
383 extern BlockDriverState *bs_table[MAX_DISKS];
384
385 void ide_init(int iobase, int iobase2, int irq,
386               BlockDriverState *hd0, BlockDriverState *hd1);
387
388 /* oss.c */
389 typedef enum {
390   AUD_FMT_U8,
391   AUD_FMT_S8,
392   AUD_FMT_U16,
393   AUD_FMT_S16
394 } audfmt_e;
395
396 void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt);
397 void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt);
398 int AUD_write (void *in_buf, int size);
399 void AUD_run (void);
400 void AUD_adjust_estimate (int _leftover);
401 int AUD_get_free (void);
402 int AUD_get_live (void);
403 int AUD_get_buffer_size (void);
404 void AUD_init (void);
405
406 /* dma.c */
407 typedef int (*DMA_transfer_handler) (void *opaque, target_ulong addr, int size);
408 int DMA_get_channel_mode (int nchan);
409 void DMA_hold_DREQ (int nchan);
410 void DMA_release_DREQ (int nchan);
411 void DMA_schedule(int nchan);
412 void DMA_run (void);
413 void DMA_init (void);
414 void DMA_register_channel (int nchan,
415                            DMA_transfer_handler transfer_handler, void *opaque);
416
417 /* sb16.c */
418 void SB16_run (void);
419 void SB16_init (void);
420  
421 /* fdc.c */
422 #define MAX_FD 2
423 extern BlockDriverState *fd_table[MAX_FD];
424
425 typedef struct fdctrl_t fdctrl_t;
426
427 fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, 
428                        uint32_t io_base,
429                        BlockDriverState **fds);
430 int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
431
432 /* ne2000.c */
433
434 void ne2000_init(int base, int irq, NetDriverState *nd);
435
436 /* pckbd.c */
437
438 void kbd_put_keycode(int keycode);
439
440 #define MOUSE_EVENT_LBUTTON 0x01
441 #define MOUSE_EVENT_RBUTTON 0x02
442 #define MOUSE_EVENT_MBUTTON 0x04
443 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
444
445 void kbd_init(void);
446
447 /* mc146818rtc.c */
448
449 typedef struct RTCState RTCState;
450
451 RTCState *rtc_init(int base, int irq);
452 void rtc_set_memory(RTCState *s, int addr, int val);
453 void rtc_set_date(RTCState *s, const struct tm *tm);
454
455 /* serial.c */
456
457 typedef struct SerialState SerialState;
458
459 extern SerialState *serial_console;
460
461 SerialState *serial_init(int base, int irq, int fd);
462 int serial_can_receive(SerialState *s);
463 void serial_receive_byte(SerialState *s, int ch);
464 void serial_receive_break(SerialState *s);
465
466 /* i8259.c */
467
468 void pic_set_irq(int irq, int level);
469 void pic_init(void);
470 uint32_t pic_intack_read(CPUState *env);
471 void pic_info(void);
472
473 /* i8254.c */
474
475 #define PIT_FREQ 1193182
476
477 typedef struct PITChannelState {
478     int count; /* can be 65536 */
479     uint16_t latched_count;
480     uint8_t rw_state;
481     uint8_t mode;
482     uint8_t bcd; /* not supported */
483     uint8_t gate; /* timer start */
484     int64_t count_load_time;
485     /* irq handling */
486     int64_t next_transition_time;
487     QEMUTimer *irq_timer;
488     int irq;
489 } PITChannelState;
490
491 extern PITChannelState pit_channels[3];
492
493 void pit_init(int base, int irq);
494 void pit_set_gate(PITChannelState *s, int val);
495 int pit_get_out(PITChannelState *s, int64_t current_time);
496 int pit_get_out_edges(PITChannelState *s);
497
498 /* pc.c */
499 void pc_init(int ram_size, int vga_ram_size, int boot_device,
500              DisplayState *ds, const char **fd_filename, int snapshot,
501              const char *kernel_filename, const char *kernel_cmdline,
502              const char *initrd_filename);
503
504 /* monitor.c */
505 void monitor_init(void);
506 void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
507 void term_flush(void);
508 void term_print_help(void);
509
510 /* gdbstub.c */
511
512 #define DEFAULT_GDBSTUB_PORT 1234
513
514 int gdbserver_start(int port);
515
516 #endif /* VL_H */