2 * QEMU System Emulator header
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 /* we put basic includes here to avoid repeating them in device drivers */
48 #define lseek64 _lseeki64
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
60 #if defined(WORDS_BIGENDIAN)
61 static inline uint32_t be32_to_cpu(uint32_t v)
66 static inline uint16_t be16_to_cpu(uint16_t v)
71 static inline uint32_t cpu_to_be32(uint32_t v)
76 static inline uint16_t cpu_to_be16(uint16_t v)
81 static inline uint32_t le32_to_cpu(uint32_t v)
86 static inline uint16_t le16_to_cpu(uint16_t v)
91 static inline uint32_t cpu_to_le32(uint32_t v)
96 static inline uint16_t cpu_to_le16(uint16_t v)
103 static inline uint32_t be32_to_cpu(uint32_t v)
108 static inline uint16_t be16_to_cpu(uint16_t v)
113 static inline uint32_t cpu_to_be32(uint32_t v)
118 static inline uint16_t cpu_to_be16(uint16_t v)
123 static inline uint32_t le32_to_cpu(uint32_t v)
128 static inline uint16_t le16_to_cpu(uint16_t v)
133 static inline uint32_t cpu_to_le32(uint32_t v)
138 static inline uint16_t cpu_to_le16(uint16_t v)
144 static inline void cpu_to_le16w(uint16_t *p, uint16_t v)
149 static inline void cpu_to_le32w(uint32_t *p, uint32_t v)
154 static inline uint16_t le16_to_cpup(const uint16_t *p)
156 return le16_to_cpu(*p);
159 static inline uint32_t le32_to_cpup(const uint32_t *p)
161 return le32_to_cpu(*p);
164 /* unaligned versions (optimized for frequent unaligned accesses)*/
166 #if defined(__i386__) || defined(__powerpc__)
168 #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
169 #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
170 #define le16_to_cpupu(p) le16_to_cpup(p)
171 #define le32_to_cpupu(p) le32_to_cpup(p)
175 static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
177 uint8_t *p1 = (uint8_t *)p;
183 static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
185 uint8_t *p1 = (uint8_t *)p;
193 static inline uint16_t le16_to_cpupu(const uint16_t *p)
195 const uint8_t *p1 = (const uint8_t *)p;
196 return p1[0] | (p1[1] << 8);
199 static inline uint32_t le32_to_cpupu(const uint32_t *p)
201 const uint8_t *p1 = (const uint8_t *)p;
202 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
208 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
210 void hw_error(const char *fmt, ...);
212 int get_image_size(const char *filename);
213 int load_image(const char *filename, uint8_t *addr);
214 extern const char *bios_dir;
216 void pstrcpy(char *buf, int buf_size, const char *str);
217 char *pstrcat(char *buf, int buf_size, const char *s);
219 int serial_open_device(void);
221 extern int vm_running;
223 typedef void VMStopHandler(void *opaque, int reason);
225 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque);
226 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque);
229 void vm_stop(int reason);
231 typedef void QEMUResetHandler(void *opaque);
233 void qemu_register_reset(QEMUResetHandler *func, void *opaque);
234 void qemu_system_reset_request(void);
235 void qemu_system_shutdown_request(void);
237 extern int audio_enabled;
239 extern int bios_size;
241 extern int cirrus_vga_enabled;
243 /* XXX: make it dynamic */
244 #if defined (TARGET_PPC)
245 #define BIOS_SIZE (512 * 1024)
247 #define BIOS_SIZE ((256 + 64) * 1024)
250 /* keyboard/mouse support */
252 #define MOUSE_EVENT_LBUTTON 0x01
253 #define MOUSE_EVENT_RBUTTON 0x02
254 #define MOUSE_EVENT_MBUTTON 0x04
256 typedef void QEMUPutKBDEvent(void *opaque, int keycode);
257 typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int buttons_state);
259 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque);
260 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque);
262 void kbd_put_keycode(int keycode);
263 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
265 /* async I/O support */
267 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
268 typedef int IOCanRWHandler(void *opaque);
270 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
271 IOReadHandler *fd_read, void *opaque);
272 void qemu_del_fd_read_handler(int fd);
274 /* network redirectors support */
278 typedef struct NetDriverState {
279 int index; /* index number in QEMU */
282 void (*send_packet)(struct NetDriverState *nd,
283 const uint8_t *buf, int size);
284 void (*add_read_packet)(struct NetDriverState *nd,
285 IOCanRWHandler *fd_can_read,
286 IOReadHandler *fd_read, void *opaque);
287 /* tun specific data */
289 /* slirp specific data */
293 extern NetDriverState nd_table[MAX_NICS];
295 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
296 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
297 IOReadHandler *fd_read, void *opaque);
301 typedef struct QEMUClock QEMUClock;
302 typedef struct QEMUTimer QEMUTimer;
303 typedef void QEMUTimerCB(void *opaque);
305 /* The real time clock should be used only for stuff which does not
306 change the virtual machine state, as it is run even if the virtual
307 machine is stopped. The real time clock has a frequency of 1000
309 extern QEMUClock *rt_clock;
311 /* Rge virtual clock is only run during the emulation. It is stopped
312 when the virtual machine is stopped. Virtual timers use a high
313 precision clock, usually cpu cycles (use ticks_per_sec). */
314 extern QEMUClock *vm_clock;
316 int64_t qemu_get_clock(QEMUClock *clock);
318 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
319 void qemu_free_timer(QEMUTimer *ts);
320 void qemu_del_timer(QEMUTimer *ts);
321 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
322 int qemu_timer_pending(QEMUTimer *ts);
324 extern int64_t ticks_per_sec;
325 extern int pit_min_timer_count;
327 void cpu_enable_ticks(void);
328 void cpu_disable_ticks(void);
332 typedef FILE QEMUFile;
334 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
335 void qemu_put_byte(QEMUFile *f, int v);
336 void qemu_put_be16(QEMUFile *f, unsigned int v);
337 void qemu_put_be32(QEMUFile *f, unsigned int v);
338 void qemu_put_be64(QEMUFile *f, uint64_t v);
339 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
340 int qemu_get_byte(QEMUFile *f);
341 unsigned int qemu_get_be16(QEMUFile *f);
342 unsigned int qemu_get_be32(QEMUFile *f);
343 uint64_t qemu_get_be64(QEMUFile *f);
345 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
347 qemu_put_be64(f, *pv);
350 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
352 qemu_put_be32(f, *pv);
355 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
357 qemu_put_be16(f, *pv);
360 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
362 qemu_put_byte(f, *pv);
365 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
367 *pv = qemu_get_be64(f);
370 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
372 *pv = qemu_get_be32(f);
375 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
377 *pv = qemu_get_be16(f);
380 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
382 *pv = qemu_get_byte(f);
385 int64_t qemu_ftell(QEMUFile *f);
386 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
388 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
389 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
391 int qemu_loadvm(const char *filename);
392 int qemu_savevm(const char *filename);
393 int register_savevm(const char *idstr,
396 SaveStateHandler *save_state,
397 LoadStateHandler *load_state,
399 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
400 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
403 typedef struct BlockDriverState BlockDriverState;
405 BlockDriverState *bdrv_new(const char *device_name);
406 void bdrv_delete(BlockDriverState *bs);
407 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot);
408 void bdrv_close(BlockDriverState *bs);
409 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
410 uint8_t *buf, int nb_sectors);
411 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
412 const uint8_t *buf, int nb_sectors);
413 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr);
414 int bdrv_commit(BlockDriverState *bs);
415 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
417 #define BDRV_TYPE_HD 0
418 #define BDRV_TYPE_CDROM 1
419 #define BDRV_TYPE_FLOPPY 2
421 void bdrv_set_geometry_hint(BlockDriverState *bs,
422 int cyls, int heads, int secs);
423 void bdrv_set_type_hint(BlockDriverState *bs, int type);
424 void bdrv_get_geometry_hint(BlockDriverState *bs,
425 int *pcyls, int *pheads, int *psecs);
426 int bdrv_get_type_hint(BlockDriverState *bs);
427 int bdrv_is_removable(BlockDriverState *bs);
428 int bdrv_is_read_only(BlockDriverState *bs);
429 int bdrv_is_inserted(BlockDriverState *bs);
430 int bdrv_is_locked(BlockDriverState *bs);
431 void bdrv_set_locked(BlockDriverState *bs, int locked);
432 void bdrv_set_change_cb(BlockDriverState *bs,
433 void (*change_cb)(void *opaque), void *opaque);
435 void bdrv_info(void);
436 BlockDriverState *bdrv_find(const char *name);
440 extern target_phys_addr_t isa_mem_base;
442 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
443 typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
445 int register_ioport_read(int start, int length, int size,
446 IOPortReadFunc *func, void *opaque);
447 int register_ioport_write(int start, int length, int size,
448 IOPortWriteFunc *func, void *opaque);
449 void isa_unassign_ioport(int start, int length);
453 extern int pci_enabled;
455 extern target_phys_addr_t pci_mem_base;
457 typedef struct PCIDevice PCIDevice;
459 typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
460 uint32_t address, uint32_t data, int len);
461 typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev,
462 uint32_t address, int len);
463 typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num,
464 uint32_t addr, uint32_t size, int type);
466 #define PCI_ADDRESS_SPACE_MEM 0x00
467 #define PCI_ADDRESS_SPACE_IO 0x01
468 #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0x08
470 typedef struct PCIIORegion {
471 uint32_t addr; /* current PCI mapping address. -1 means not mapped */
474 PCIMapIORegionFunc *map_func;
477 #define PCI_ROM_SLOT 6
478 #define PCI_NUM_REGIONS 7
480 /* PCI config space */
483 /* the following fields are read only */
487 PCIIORegion io_regions[PCI_NUM_REGIONS];
489 /* do not access the following fields */
490 PCIConfigReadFunc *config_read;
491 PCIConfigWriteFunc *config_write;
495 PCIDevice *pci_register_device(const char *name, int instance_size,
496 int bus_num, int devfn,
497 PCIConfigReadFunc *config_read,
498 PCIConfigWriteFunc *config_write);
500 void pci_register_io_region(PCIDevice *pci_dev, int region_num,
501 uint32_t size, int type,
502 PCIMapIORegionFunc *map_func);
504 void pci_set_irq(PCIDevice *pci_dev, int irq_num, int level);
506 uint32_t pci_default_read_config(PCIDevice *d,
507 uint32_t address, int len);
508 void pci_default_write_config(PCIDevice *d,
509 uint32_t address, uint32_t val, int len);
511 extern struct PIIX3State *piix3_state;
513 void i440fx_init(void);
514 void piix3_init(void);
515 void pci_bios_init(void);
518 /* temporary: will be moved in platform specific file */
519 void pci_prep_init(void);
520 void pci_pmac_init(void);
521 void pci_ppc_bios_init(void);
525 #define VGA_RAM_SIZE (4096 * 1024)
527 typedef struct DisplayState {
531 void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
532 void (*dpy_resize)(struct DisplayState *s, int w, int h);
533 void (*dpy_refresh)(struct DisplayState *s);
536 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
538 s->dpy_update(s, x, y, w, h);
541 static inline void dpy_resize(DisplayState *s, int w, int h)
543 s->dpy_resize(s, w, h);
546 int vga_initialize(DisplayState *ds, uint8_t *vga_ram_base,
547 unsigned long vga_ram_offset, int vga_ram_size,
549 void vga_update_display(void);
550 void vga_invalidate_display(void);
551 void vga_screen_dump(const char *filename);
554 void pci_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
555 unsigned long vga_ram_offset, int vga_ram_size);
557 void isa_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
558 unsigned long vga_ram_offset, int vga_ram_size);
561 void sdl_display_init(DisplayState *ds);
566 extern BlockDriverState *bs_table[MAX_DISKS];
568 void isa_ide_init(int iobase, int iobase2, int irq,
569 BlockDriverState *hd0, BlockDriverState *hd1);
570 void pci_ide_init(BlockDriverState **hd_table);
571 void pci_piix3_ide_init(BlockDriverState **hd_table);
581 void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt);
582 void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt);
583 int AUD_write (void *in_buf, int size);
585 void AUD_adjust_estimate (int _leftover);
586 int AUD_get_free (void);
587 int AUD_get_live (void);
588 int AUD_get_buffer_size (void);
589 void AUD_init (void);
592 typedef int (*DMA_transfer_handler) (void *opaque, target_ulong addr, int size);
593 int DMA_get_channel_mode (int nchan);
594 void DMA_hold_DREQ (int nchan);
595 void DMA_release_DREQ (int nchan);
596 void DMA_schedule(int nchan);
598 void DMA_init (void);
599 void DMA_register_channel (int nchan,
600 DMA_transfer_handler transfer_handler, void *opaque);
603 void SB16_run (void);
604 void SB16_init (void);
608 extern BlockDriverState *fd_table[MAX_FD];
610 typedef struct fdctrl_t fdctrl_t;
612 fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped,
614 BlockDriverState **fds);
615 int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
619 void isa_ne2000_init(int base, int irq, NetDriverState *nd);
620 void pci_ne2000_init(NetDriverState *nd);
628 typedef struct RTCState RTCState;
630 RTCState *rtc_init(int base, int irq);
631 void rtc_set_memory(RTCState *s, int addr, int val);
632 void rtc_set_date(RTCState *s, const struct tm *tm);
636 typedef struct SerialState SerialState;
638 extern SerialState *serial_console;
640 SerialState *serial_init(int base, int irq, int fd);
641 int serial_can_receive(SerialState *s);
642 void serial_receive_byte(SerialState *s, int ch);
643 void serial_receive_break(SerialState *s);
647 void pic_set_irq(int irq, int level);
649 uint32_t pic_intack_read(CPUState *env);
655 #define PIT_FREQ 1193182
657 typedef struct PITState PITState;
659 PITState *pit_init(int base, int irq);
660 void pit_set_gate(PITState *pit, int channel, int val);
661 int pit_get_gate(PITState *pit, int channel);
662 int pit_get_out(PITState *pit, int channel, int64_t current_time);
665 void pc_init(int ram_size, int vga_ram_size, int boot_device,
666 DisplayState *ds, const char **fd_filename, int snapshot,
667 const char *kernel_filename, const char *kernel_cmdline,
668 const char *initrd_filename);
671 void ppc_init (int ram_size, int vga_ram_size, int boot_device,
672 DisplayState *ds, const char **fd_filename, int snapshot,
673 const char *kernel_filename, const char *kernel_cmdline,
674 const char *initrd_filename);
675 void ppc_prep_init (int ram_size, int vga_ram_size, int boot_device,
676 DisplayState *ds, const char **fd_filename, int snapshot,
677 const char *kernel_filename, const char *kernel_cmdline,
678 const char *initrd_filename);
679 void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
680 DisplayState *ds, const char **fd_filename, int snapshot,
681 const char *kernel_filename, const char *kernel_cmdline,
682 const char *initrd_filename);
684 ppc_tb_t *cpu_ppc_tb_init (CPUState *env, uint32_t freq);
686 void PREP_debug_write (void *opaque, uint32_t addr, uint32_t val);
688 extern CPUWriteMemoryFunc *PPC_io_write[];
689 extern CPUReadMemoryFunc *PPC_io_read[];
690 extern int prep_enabled;
693 #include "hw/m48t59.h"
695 void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value);
696 uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr);
697 void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value);
698 uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr);
699 void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value);
700 uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr);
701 void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
702 const unsigned char *str, uint32_t max);
703 int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max);
704 void NVRAM_set_crc (m48t59_t *nvram, uint32_t addr,
705 uint32_t start, uint32_t count);
706 int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
707 const unsigned char *arch,
708 uint32_t RAM_size, int boot_device,
709 uint32_t kernel_image, uint32_t kernel_size,
710 uint32_t cmdline, uint32_t cmdline_size,
711 uint32_t initrd_image, uint32_t initrd_size,
712 uint32_t NVRAM_image);
716 #define MAX_ADB_DEVICES 16
718 typedef struct ADBDevice ADBDevice;
720 typedef void ADBDeviceReceivePacket(ADBDevice *d, const uint8_t *buf, int len);
723 struct ADBBusState *bus;
726 ADBDeviceReceivePacket *receive_packet;
730 typedef struct ADBBusState {
731 ADBDevice devices[MAX_ADB_DEVICES];
735 void adb_receive_packet(ADBBusState *s, const uint8_t *buf, int len);
736 void adb_send_packet(ADBBusState *s, const uint8_t *buf, int len);
738 ADBDevice *adb_register_device(ADBBusState *s, int devaddr,
739 ADBDeviceReceivePacket *receive_packet,
741 void adb_kbd_init(ADBBusState *bus);
742 void adb_mouse_init(ADBBusState *bus);
746 extern ADBBusState adb_bus;
750 void monitor_init(void);
751 void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
752 void term_flush(void);
753 void term_print_help(void);
757 #define DEFAULT_GDBSTUB_PORT 1234
759 int gdbserver_start(int port);