irq statistics code (initial patch by Jocelyn Mayer)
[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 #include <sys/stat.h>
39
40 #ifndef O_LARGEFILE
41 #define O_LARGEFILE 0
42 #endif
43 #ifndef O_BINARY
44 #define O_BINARY 0
45 #endif
46
47 #ifdef _WIN32
48 #define lseek64 _lseeki64
49 #endif
50
51 #include "cpu.h"
52
53 #ifndef glue
54 #define xglue(x, y) x ## y
55 #define glue(x, y) xglue(x, y)
56 #define stringify(s)    tostring(s)
57 #define tostring(s)     #s
58 #endif
59
60 #if defined(WORDS_BIGENDIAN)
61 static inline uint32_t be32_to_cpu(uint32_t v)
62 {
63     return v;
64 }
65
66 static inline uint16_t be16_to_cpu(uint16_t v)
67 {
68     return v;
69 }
70
71 static inline uint32_t cpu_to_be32(uint32_t v)
72 {
73     return v;
74 }
75
76 static inline uint16_t cpu_to_be16(uint16_t v)
77 {
78     return v;
79 }
80
81 static inline uint32_t le32_to_cpu(uint32_t v)
82 {
83     return bswap32(v);
84 }
85
86 static inline uint16_t le16_to_cpu(uint16_t v)
87 {
88     return bswap16(v);
89 }
90
91 static inline uint32_t cpu_to_le32(uint32_t v)
92 {
93     return bswap32(v);
94 }
95
96 static inline uint16_t cpu_to_le16(uint16_t v)
97 {
98     return bswap16(v);
99 }
100
101 #else
102
103 static inline uint32_t be32_to_cpu(uint32_t v)
104 {
105     return bswap32(v);
106 }
107
108 static inline uint16_t be16_to_cpu(uint16_t v)
109 {
110     return bswap16(v);
111 }
112
113 static inline uint32_t cpu_to_be32(uint32_t v)
114 {
115     return bswap32(v);
116 }
117
118 static inline uint16_t cpu_to_be16(uint16_t v)
119 {
120     return bswap16(v);
121 }
122
123 static inline uint32_t le32_to_cpu(uint32_t v)
124 {
125     return v;
126 }
127
128 static inline uint16_t le16_to_cpu(uint16_t v)
129 {
130     return v;
131 }
132
133 static inline uint32_t cpu_to_le32(uint32_t v)
134 {
135     return v;
136 }
137
138 static inline uint16_t cpu_to_le16(uint16_t v)
139 {
140     return v;
141 }
142 #endif
143
144
145 /* vl.c */
146 extern int reset_requested;
147
148 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
149
150 void hw_error(const char *fmt, ...);
151
152 int load_image(const char *filename, uint8_t *addr);
153 extern const char *bios_dir;
154
155 void pstrcpy(char *buf, int buf_size, const char *str);
156 char *pstrcat(char *buf, int buf_size, const char *s);
157
158 int serial_open_device(void);
159
160 extern int vm_running;
161
162 typedef void VMStopHandler(void *opaque, int reason);
163
164 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque);
165 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque);
166
167 void vm_start(void);
168 void vm_stop(int reason);
169
170 extern int audio_enabled;
171
172 /* async I/O support */
173
174 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
175 typedef int IOCanRWHandler(void *opaque);
176
177 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, 
178                              IOReadHandler *fd_read, void *opaque);
179 void qemu_del_fd_read_handler(int fd);
180
181 /* network redirectors support */
182
183 #define MAX_NICS 8
184
185 typedef struct NetDriverState {
186     int index; /* index number in QEMU */
187     uint8_t macaddr[6];
188     char ifname[16];
189     void (*send_packet)(struct NetDriverState *nd, 
190                         const uint8_t *buf, int size);
191     void (*add_read_packet)(struct NetDriverState *nd, 
192                             IOCanRWHandler *fd_can_read, 
193                             IOReadHandler *fd_read, void *opaque);
194     /* tun specific data */
195     int fd;
196     /* slirp specific data */
197 } NetDriverState;
198
199 extern int nb_nics;
200 extern NetDriverState nd_table[MAX_NICS];
201
202 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
203 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, 
204                           IOReadHandler *fd_read, void *opaque);
205
206 /* timers */
207
208 typedef struct QEMUClock QEMUClock;
209 typedef struct QEMUTimer QEMUTimer;
210 typedef void QEMUTimerCB(void *opaque);
211
212 /* The real time clock should be used only for stuff which does not
213    change the virtual machine state, as it is run even if the virtual
214    machine is stopped. The real time clock has a frequency of 1000
215    Hz. */
216 extern QEMUClock *rt_clock;
217
218 /* Rge virtual clock is only run during the emulation. It is stopped
219    when the virtual machine is stopped. Virtual timers use a high
220    precision clock, usually cpu cycles (use ticks_per_sec). */
221 extern QEMUClock *vm_clock;
222
223 int64_t qemu_get_clock(QEMUClock *clock);
224
225 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
226 void qemu_free_timer(QEMUTimer *ts);
227 void qemu_del_timer(QEMUTimer *ts);
228 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
229 int qemu_timer_pending(QEMUTimer *ts);
230
231 extern int64_t ticks_per_sec;
232 extern int pit_min_timer_count;
233
234 void cpu_enable_ticks(void);
235 void cpu_disable_ticks(void);
236
237 /* VM Load/Save */
238
239 typedef FILE QEMUFile;
240
241 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
242 void qemu_put_byte(QEMUFile *f, int v);
243 void qemu_put_be16(QEMUFile *f, unsigned int v);
244 void qemu_put_be32(QEMUFile *f, unsigned int v);
245 void qemu_put_be64(QEMUFile *f, uint64_t v);
246 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
247 int qemu_get_byte(QEMUFile *f);
248 unsigned int qemu_get_be16(QEMUFile *f);
249 unsigned int qemu_get_be32(QEMUFile *f);
250 uint64_t qemu_get_be64(QEMUFile *f);
251
252 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
253 {
254     qemu_put_be64(f, *pv);
255 }
256
257 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
258 {
259     qemu_put_be32(f, *pv);
260 }
261
262 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
263 {
264     qemu_put_be16(f, *pv);
265 }
266
267 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
268 {
269     qemu_put_byte(f, *pv);
270 }
271
272 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
273 {
274     *pv = qemu_get_be64(f);
275 }
276
277 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
278 {
279     *pv = qemu_get_be32(f);
280 }
281
282 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
283 {
284     *pv = qemu_get_be16(f);
285 }
286
287 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
288 {
289     *pv = qemu_get_byte(f);
290 }
291
292 int64_t qemu_ftell(QEMUFile *f);
293 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
294
295 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
296 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
297
298 int qemu_loadvm(const char *filename);
299 int qemu_savevm(const char *filename);
300 int register_savevm(const char *idstr, 
301                     int instance_id, 
302                     int version_id,
303                     SaveStateHandler *save_state,
304                     LoadStateHandler *load_state,
305                     void *opaque);
306 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
307 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
308
309 /* block.c */
310 typedef struct BlockDriverState BlockDriverState;
311
312 BlockDriverState *bdrv_new(const char *device_name);
313 void bdrv_delete(BlockDriverState *bs);
314 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot);
315 void bdrv_close(BlockDriverState *bs);
316 int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
317               uint8_t *buf, int nb_sectors);
318 int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
319                const uint8_t *buf, int nb_sectors);
320 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr);
321 int bdrv_commit(BlockDriverState *bs);
322 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
323
324 #define BDRV_TYPE_HD     0
325 #define BDRV_TYPE_CDROM  1
326 #define BDRV_TYPE_FLOPPY 2
327
328 void bdrv_set_geometry_hint(BlockDriverState *bs, 
329                             int cyls, int heads, int secs);
330 void bdrv_set_type_hint(BlockDriverState *bs, int type);
331 void bdrv_get_geometry_hint(BlockDriverState *bs, 
332                             int *pcyls, int *pheads, int *psecs);
333 int bdrv_get_type_hint(BlockDriverState *bs);
334 int bdrv_is_removable(BlockDriverState *bs);
335 int bdrv_is_read_only(BlockDriverState *bs);
336 int bdrv_is_inserted(BlockDriverState *bs);
337 int bdrv_is_locked(BlockDriverState *bs);
338 void bdrv_set_locked(BlockDriverState *bs, int locked);
339 void bdrv_set_change_cb(BlockDriverState *bs, 
340                         void (*change_cb)(void *opaque), void *opaque);
341
342 void bdrv_info(void);
343 BlockDriverState *bdrv_find(const char *name);
344
345 /* ISA bus */
346
347 extern target_phys_addr_t isa_mem_base;
348
349 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
350 typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
351
352 int register_ioport_read(int start, int length, int size, 
353                          IOPortReadFunc *func, void *opaque);
354 int register_ioport_write(int start, int length, int size, 
355                           IOPortWriteFunc *func, void *opaque);
356 void isa_unassign_ioport(int start, int length);
357
358 /* PCI bus */
359
360 extern int pci_enabled;
361
362 extern target_phys_addr_t pci_mem_base;
363
364 typedef struct PCIDevice PCIDevice;
365
366 typedef void PCIConfigWriteFunc(PCIDevice *pci_dev, 
367                                 uint32_t address, uint32_t data, int len);
368 typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev, 
369                                    uint32_t address, int len);
370 typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num, 
371                                 uint32_t addr, uint32_t size, int type);
372
373 #define PCI_ADDRESS_SPACE_MEM           0x00
374 #define PCI_ADDRESS_SPACE_IO            0x01
375 #define PCI_ADDRESS_SPACE_MEM_PREFETCH  0x08
376
377 typedef struct PCIIORegion {
378     uint32_t addr; /* current PCI mapping address. -1 means not mapped */
379     uint32_t size;
380     uint8_t type;
381     PCIMapIORegionFunc *map_func;
382 } PCIIORegion;
383
384 struct PCIDevice {
385     /* PCI config space */
386     uint8_t config[256];
387
388     /* the following fields are read only */
389     int bus_num;
390     int devfn;
391     char name[64];
392     PCIIORegion io_regions[6];
393     
394     /* do not access the following fields */
395     PCIConfigReadFunc *config_read;
396     PCIConfigWriteFunc *config_write;
397     int irq_index;
398 };
399
400 PCIDevice *pci_register_device(const char *name, int instance_size,
401                                int bus_num, int devfn,
402                                PCIConfigReadFunc *config_read, 
403                                PCIConfigWriteFunc *config_write);
404
405 void pci_register_io_region(PCIDevice *pci_dev, int region_num, 
406                             uint32_t size, int type, 
407                             PCIMapIORegionFunc *map_func);
408
409 void pci_set_irq(PCIDevice *pci_dev, int irq_num, int level);
410
411 uint32_t pci_default_read_config(PCIDevice *d, 
412                                  uint32_t address, int len);
413 void pci_default_write_config(PCIDevice *d, 
414                               uint32_t address, uint32_t val, int len);
415
416 void i440fx_init(void);
417 void piix3_init(void);
418 void pci_bios_init(void);
419 void pci_info(void);
420
421 /* vga.c */
422
423 #define VGA_RAM_SIZE (4096 * 1024)
424
425 typedef struct DisplayState {
426     uint8_t *data;
427     int linesize;
428     int depth;
429     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
430     void (*dpy_resize)(struct DisplayState *s, int w, int h);
431     void (*dpy_refresh)(struct DisplayState *s);
432 } DisplayState;
433
434 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
435 {
436     s->dpy_update(s, x, y, w, h);
437 }
438
439 static inline void dpy_resize(DisplayState *s, int w, int h)
440 {
441     s->dpy_resize(s, w, h);
442 }
443
444 int vga_initialize(DisplayState *ds, uint8_t *vga_ram_base, 
445                    unsigned long vga_ram_offset, int vga_ram_size, 
446                    int is_pci);
447 void vga_update_display(void);
448 void vga_screen_dump(const char *filename);
449
450 /* sdl.c */
451 void sdl_display_init(DisplayState *ds);
452
453 /* ide.c */
454 #define MAX_DISKS 4
455
456 extern BlockDriverState *bs_table[MAX_DISKS];
457
458 void isa_ide_init(int iobase, int iobase2, int irq,
459                   BlockDriverState *hd0, BlockDriverState *hd1);
460 void pci_ide_init(BlockDriverState **hd_table);
461
462 /* oss.c */
463 typedef enum {
464   AUD_FMT_U8,
465   AUD_FMT_S8,
466   AUD_FMT_U16,
467   AUD_FMT_S16
468 } audfmt_e;
469
470 void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt);
471 void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt);
472 int AUD_write (void *in_buf, int size);
473 void AUD_run (void);
474 void AUD_adjust_estimate (int _leftover);
475 int AUD_get_free (void);
476 int AUD_get_live (void);
477 int AUD_get_buffer_size (void);
478 void AUD_init (void);
479
480 /* dma.c */
481 typedef int (*DMA_transfer_handler) (void *opaque, target_ulong addr, int size);
482 int DMA_get_channel_mode (int nchan);
483 void DMA_hold_DREQ (int nchan);
484 void DMA_release_DREQ (int nchan);
485 void DMA_schedule(int nchan);
486 void DMA_run (void);
487 void DMA_init (void);
488 void DMA_register_channel (int nchan,
489                            DMA_transfer_handler transfer_handler, void *opaque);
490
491 /* sb16.c */
492 void SB16_run (void);
493 void SB16_init (void);
494  
495 /* fdc.c */
496 #define MAX_FD 2
497 extern BlockDriverState *fd_table[MAX_FD];
498
499 typedef struct fdctrl_t fdctrl_t;
500
501 fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, 
502                        uint32_t io_base,
503                        BlockDriverState **fds);
504 int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
505
506 /* ne2000.c */
507
508 void isa_ne2000_init(int base, int irq, NetDriverState *nd);
509 void pci_ne2000_init(NetDriverState *nd);
510
511 /* pckbd.c */
512
513 void kbd_put_keycode(int keycode);
514
515 #define MOUSE_EVENT_LBUTTON 0x01
516 #define MOUSE_EVENT_RBUTTON 0x02
517 #define MOUSE_EVENT_MBUTTON 0x04
518 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
519
520 void kbd_init(void);
521
522 /* mc146818rtc.c */
523
524 typedef struct RTCState RTCState;
525
526 RTCState *rtc_init(int base, int irq);
527 void rtc_set_memory(RTCState *s, int addr, int val);
528 void rtc_set_date(RTCState *s, const struct tm *tm);
529
530 /* serial.c */
531
532 typedef struct SerialState SerialState;
533
534 extern SerialState *serial_console;
535
536 SerialState *serial_init(int base, int irq, int fd);
537 int serial_can_receive(SerialState *s);
538 void serial_receive_byte(SerialState *s, int ch);
539 void serial_receive_break(SerialState *s);
540
541 /* i8259.c */
542
543 void pic_set_irq(int irq, int level);
544 void pic_init(void);
545 uint32_t pic_intack_read(CPUState *env);
546 void pic_info(void);
547 void irq_info(void);
548
549 /* i8254.c */
550
551 #define PIT_FREQ 1193182
552
553 typedef struct PITState PITState;
554
555 PITState *pit_init(int base, int irq);
556 void pit_set_gate(PITState *pit, int channel, int val);
557 int pit_get_gate(PITState *pit, int channel);
558 int pit_get_out(PITState *pit, int channel, int64_t current_time);
559
560 /* pc.c */
561 void pc_init(int ram_size, int vga_ram_size, int boot_device,
562              DisplayState *ds, const char **fd_filename, int snapshot,
563              const char *kernel_filename, const char *kernel_cmdline,
564              const char *initrd_filename);
565
566 /* ppc.c */
567 void ppc_init (int ram_size, int vga_ram_size, int boot_device,
568                DisplayState *ds, const char **fd_filename, int snapshot,
569                const char *kernel_filename, const char *kernel_cmdline,
570                const char *initrd_filename);
571
572 /* monitor.c */
573 void monitor_init(void);
574 void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
575 void term_flush(void);
576 void term_print_help(void);
577
578 /* gdbstub.c */
579
580 #define DEFAULT_GDBSTUB_PORT 1234
581
582 int gdbserver_start(int port);
583
584 #endif /* VL_H */