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
)
146 extern int reset_requested
;
148 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
);
150 void hw_error(const char *fmt
, ...);
152 int load_image(const char *filename
, uint8_t *addr
);
153 extern const char *bios_dir
;
155 void pstrcpy(char *buf
, int buf_size
, const char *str
);
156 char *pstrcat(char *buf
, int buf_size
, const char *s
);
158 int serial_open_device(void);
160 extern int vm_running
;
162 typedef void VMStopHandler(void *opaque
, int reason
);
164 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
);
165 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
);
168 void vm_stop(int reason
);
170 extern int audio_enabled
;
172 extern int bios_size
;
175 /* XXX: make it dynamic */
176 #if defined (TARGET_PPC)
177 #define BIOS_SIZE (512 * 1024)
182 /* keyboard/mouse support */
184 #define MOUSE_EVENT_LBUTTON 0x01
185 #define MOUSE_EVENT_RBUTTON 0x02
186 #define MOUSE_EVENT_MBUTTON 0x04
188 typedef void QEMUPutKBDEvent(void *opaque
, int keycode
);
189 typedef void QEMUPutMouseEvent(void *opaque
, int dx
, int dy
, int dz
, int buttons_state
);
191 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
);
192 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
);
194 void kbd_put_keycode(int keycode
);
195 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
);
197 /* async I/O support */
199 typedef void IOReadHandler(void *opaque
, const uint8_t *buf
, int size
);
200 typedef int IOCanRWHandler(void *opaque
);
202 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
203 IOReadHandler
*fd_read
, void *opaque
);
204 void qemu_del_fd_read_handler(int fd
);
206 /* network redirectors support */
210 typedef struct NetDriverState
{
211 int index
; /* index number in QEMU */
214 void (*send_packet
)(struct NetDriverState
*nd
,
215 const uint8_t *buf
, int size
);
216 void (*add_read_packet
)(struct NetDriverState
*nd
,
217 IOCanRWHandler
*fd_can_read
,
218 IOReadHandler
*fd_read
, void *opaque
);
219 /* tun specific data */
221 /* slirp specific data */
225 extern NetDriverState nd_table
[MAX_NICS
];
227 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
);
228 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
229 IOReadHandler
*fd_read
, void *opaque
);
233 typedef struct QEMUClock QEMUClock
;
234 typedef struct QEMUTimer QEMUTimer
;
235 typedef void QEMUTimerCB(void *opaque
);
237 /* The real time clock should be used only for stuff which does not
238 change the virtual machine state, as it is run even if the virtual
239 machine is stopped. The real time clock has a frequency of 1000
241 extern QEMUClock
*rt_clock
;
243 /* Rge virtual clock is only run during the emulation. It is stopped
244 when the virtual machine is stopped. Virtual timers use a high
245 precision clock, usually cpu cycles (use ticks_per_sec). */
246 extern QEMUClock
*vm_clock
;
248 int64_t qemu_get_clock(QEMUClock
*clock
);
250 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
);
251 void qemu_free_timer(QEMUTimer
*ts
);
252 void qemu_del_timer(QEMUTimer
*ts
);
253 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
);
254 int qemu_timer_pending(QEMUTimer
*ts
);
256 extern int64_t ticks_per_sec
;
257 extern int pit_min_timer_count
;
259 void cpu_enable_ticks(void);
260 void cpu_disable_ticks(void);
264 typedef FILE QEMUFile
;
266 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
);
267 void qemu_put_byte(QEMUFile
*f
, int v
);
268 void qemu_put_be16(QEMUFile
*f
, unsigned int v
);
269 void qemu_put_be32(QEMUFile
*f
, unsigned int v
);
270 void qemu_put_be64(QEMUFile
*f
, uint64_t v
);
271 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
);
272 int qemu_get_byte(QEMUFile
*f
);
273 unsigned int qemu_get_be16(QEMUFile
*f
);
274 unsigned int qemu_get_be32(QEMUFile
*f
);
275 uint64_t qemu_get_be64(QEMUFile
*f
);
277 static inline void qemu_put_be64s(QEMUFile
*f
, const uint64_t *pv
)
279 qemu_put_be64(f
, *pv
);
282 static inline void qemu_put_be32s(QEMUFile
*f
, const uint32_t *pv
)
284 qemu_put_be32(f
, *pv
);
287 static inline void qemu_put_be16s(QEMUFile
*f
, const uint16_t *pv
)
289 qemu_put_be16(f
, *pv
);
292 static inline void qemu_put_8s(QEMUFile
*f
, const uint8_t *pv
)
294 qemu_put_byte(f
, *pv
);
297 static inline void qemu_get_be64s(QEMUFile
*f
, uint64_t *pv
)
299 *pv
= qemu_get_be64(f
);
302 static inline void qemu_get_be32s(QEMUFile
*f
, uint32_t *pv
)
304 *pv
= qemu_get_be32(f
);
307 static inline void qemu_get_be16s(QEMUFile
*f
, uint16_t *pv
)
309 *pv
= qemu_get_be16(f
);
312 static inline void qemu_get_8s(QEMUFile
*f
, uint8_t *pv
)
314 *pv
= qemu_get_byte(f
);
317 int64_t qemu_ftell(QEMUFile
*f
);
318 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
);
320 typedef void SaveStateHandler(QEMUFile
*f
, void *opaque
);
321 typedef int LoadStateHandler(QEMUFile
*f
, void *opaque
, int version_id
);
323 int qemu_loadvm(const char *filename
);
324 int qemu_savevm(const char *filename
);
325 int register_savevm(const char *idstr
,
328 SaveStateHandler
*save_state
,
329 LoadStateHandler
*load_state
,
331 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
);
332 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
);
335 typedef struct BlockDriverState BlockDriverState
;
337 BlockDriverState
*bdrv_new(const char *device_name
);
338 void bdrv_delete(BlockDriverState
*bs
);
339 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int snapshot
);
340 void bdrv_close(BlockDriverState
*bs
);
341 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
342 uint8_t *buf
, int nb_sectors
);
343 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
344 const uint8_t *buf
, int nb_sectors
);
345 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
);
346 int bdrv_commit(BlockDriverState
*bs
);
347 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
);
349 #define BDRV_TYPE_HD 0
350 #define BDRV_TYPE_CDROM 1
351 #define BDRV_TYPE_FLOPPY 2
353 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
354 int cyls
, int heads
, int secs
);
355 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
);
356 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
357 int *pcyls
, int *pheads
, int *psecs
);
358 int bdrv_get_type_hint(BlockDriverState
*bs
);
359 int bdrv_is_removable(BlockDriverState
*bs
);
360 int bdrv_is_read_only(BlockDriverState
*bs
);
361 int bdrv_is_inserted(BlockDriverState
*bs
);
362 int bdrv_is_locked(BlockDriverState
*bs
);
363 void bdrv_set_locked(BlockDriverState
*bs
, int locked
);
364 void bdrv_set_change_cb(BlockDriverState
*bs
,
365 void (*change_cb
)(void *opaque
), void *opaque
);
367 void bdrv_info(void);
368 BlockDriverState
*bdrv_find(const char *name
);
372 extern target_phys_addr_t isa_mem_base
;
374 typedef void (IOPortWriteFunc
)(void *opaque
, uint32_t address
, uint32_t data
);
375 typedef uint32_t (IOPortReadFunc
)(void *opaque
, uint32_t address
);
377 int register_ioport_read(int start
, int length
, int size
,
378 IOPortReadFunc
*func
, void *opaque
);
379 int register_ioport_write(int start
, int length
, int size
,
380 IOPortWriteFunc
*func
, void *opaque
);
381 void isa_unassign_ioport(int start
, int length
);
385 extern int pci_enabled
;
387 extern target_phys_addr_t pci_mem_base
;
389 typedef struct PCIDevice PCIDevice
;
391 typedef void PCIConfigWriteFunc(PCIDevice
*pci_dev
,
392 uint32_t address
, uint32_t data
, int len
);
393 typedef uint32_t PCIConfigReadFunc(PCIDevice
*pci_dev
,
394 uint32_t address
, int len
);
395 typedef void PCIMapIORegionFunc(PCIDevice
*pci_dev
, int region_num
,
396 uint32_t addr
, uint32_t size
, int type
);
398 #define PCI_ADDRESS_SPACE_MEM 0x00
399 #define PCI_ADDRESS_SPACE_IO 0x01
400 #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0x08
402 typedef struct PCIIORegion
{
403 uint32_t addr
; /* current PCI mapping address. -1 means not mapped */
406 PCIMapIORegionFunc
*map_func
;
409 #define PCI_ROM_SLOT 6
410 #define PCI_NUM_REGIONS 7
412 /* PCI config space */
415 /* the following fields are read only */
419 PCIIORegion io_regions
[PCI_NUM_REGIONS
];
421 /* do not access the following fields */
422 PCIConfigReadFunc
*config_read
;
423 PCIConfigWriteFunc
*config_write
;
427 PCIDevice
*pci_register_device(const char *name
, int instance_size
,
428 int bus_num
, int devfn
,
429 PCIConfigReadFunc
*config_read
,
430 PCIConfigWriteFunc
*config_write
);
432 void pci_register_io_region(PCIDevice
*pci_dev
, int region_num
,
433 uint32_t size
, int type
,
434 PCIMapIORegionFunc
*map_func
);
436 void pci_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
);
438 uint32_t pci_default_read_config(PCIDevice
*d
,
439 uint32_t address
, int len
);
440 void pci_default_write_config(PCIDevice
*d
,
441 uint32_t address
, uint32_t val
, int len
);
443 extern struct PIIX3State
*piix3_state
;
445 void i440fx_init(void);
446 void piix3_init(void);
447 void pci_bios_init(void);
450 /* temporary: will be moved in platform specific file */
451 void pci_prep_init(void);
452 void pci_pmac_init(void);
453 void pci_ppc_bios_init(void);
457 #define VGA_RAM_SIZE (4096 * 1024)
459 typedef struct DisplayState
{
463 void (*dpy_update
)(struct DisplayState
*s
, int x
, int y
, int w
, int h
);
464 void (*dpy_resize
)(struct DisplayState
*s
, int w
, int h
);
465 void (*dpy_refresh
)(struct DisplayState
*s
);
468 static inline void dpy_update(DisplayState
*s
, int x
, int y
, int w
, int h
)
470 s
->dpy_update(s
, x
, y
, w
, h
);
473 static inline void dpy_resize(DisplayState
*s
, int w
, int h
)
475 s
->dpy_resize(s
, w
, h
);
478 int vga_initialize(DisplayState
*ds
, uint8_t *vga_ram_base
,
479 unsigned long vga_ram_offset
, int vga_ram_size
,
481 void vga_update_display(void);
482 void vga_screen_dump(const char *filename
);
485 void sdl_display_init(DisplayState
*ds
);
490 extern BlockDriverState
*bs_table
[MAX_DISKS
];
492 void isa_ide_init(int iobase
, int iobase2
, int irq
,
493 BlockDriverState
*hd0
, BlockDriverState
*hd1
);
494 void pci_ide_init(BlockDriverState
**hd_table
);
495 void pci_piix3_ide_init(BlockDriverState
**hd_table
);
505 void AUD_open (int rfreq
, int rnchannels
, audfmt_e rfmt
);
506 void AUD_reset (int rfreq
, int rnchannels
, audfmt_e rfmt
);
507 int AUD_write (void *in_buf
, int size
);
509 void AUD_adjust_estimate (int _leftover
);
510 int AUD_get_free (void);
511 int AUD_get_live (void);
512 int AUD_get_buffer_size (void);
513 void AUD_init (void);
516 typedef int (*DMA_transfer_handler
) (void *opaque
, target_ulong addr
, int size
);
517 int DMA_get_channel_mode (int nchan
);
518 void DMA_hold_DREQ (int nchan
);
519 void DMA_release_DREQ (int nchan
);
520 void DMA_schedule(int nchan
);
522 void DMA_init (void);
523 void DMA_register_channel (int nchan
,
524 DMA_transfer_handler transfer_handler
, void *opaque
);
527 void SB16_run (void);
528 void SB16_init (void);
532 extern BlockDriverState
*fd_table
[MAX_FD
];
534 typedef struct fdctrl_t fdctrl_t
;
536 fdctrl_t
*fdctrl_init (int irq_lvl
, int dma_chann
, int mem_mapped
,
538 BlockDriverState
**fds
);
539 int fdctrl_get_drive_type(fdctrl_t
*fdctrl
, int drive_num
);
543 void isa_ne2000_init(int base
, int irq
, NetDriverState
*nd
);
544 void pci_ne2000_init(NetDriverState
*nd
);
552 typedef struct RTCState RTCState
;
554 RTCState
*rtc_init(int base
, int irq
);
555 void rtc_set_memory(RTCState
*s
, int addr
, int val
);
556 void rtc_set_date(RTCState
*s
, const struct tm
*tm
);
560 typedef struct SerialState SerialState
;
562 extern SerialState
*serial_console
;
564 SerialState
*serial_init(int base
, int irq
, int fd
);
565 int serial_can_receive(SerialState
*s
);
566 void serial_receive_byte(SerialState
*s
, int ch
);
567 void serial_receive_break(SerialState
*s
);
571 void pic_set_irq(int irq
, int level
);
573 uint32_t pic_intack_read(CPUState
*env
);
579 #define PIT_FREQ 1193182
581 typedef struct PITState PITState
;
583 PITState
*pit_init(int base
, int irq
);
584 void pit_set_gate(PITState
*pit
, int channel
, int val
);
585 int pit_get_gate(PITState
*pit
, int channel
);
586 int pit_get_out(PITState
*pit
, int channel
, int64_t current_time
);
589 void pc_init(int ram_size
, int vga_ram_size
, int boot_device
,
590 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
591 const char *kernel_filename
, const char *kernel_cmdline
,
592 const char *initrd_filename
);
595 void ppc_init (int ram_size
, int vga_ram_size
, int boot_device
,
596 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
597 const char *kernel_filename
, const char *kernel_cmdline
,
598 const char *initrd_filename
);
599 void ppc_prep_init (int ram_size
, int vga_ram_size
, int boot_device
,
600 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
601 const char *kernel_filename
, const char *kernel_cmdline
,
602 const char *initrd_filename
);
603 void ppc_chrp_init(int ram_size
, int vga_ram_size
, int boot_device
,
604 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
605 const char *kernel_filename
, const char *kernel_cmdline
,
606 const char *initrd_filename
);
608 ppc_tb_t
*cpu_ppc_tb_init (CPUState
*env
, uint32_t freq
);
610 void PREP_debug_write (void *opaque
, uint32_t addr
, uint32_t val
);
612 extern CPUWriteMemoryFunc
*PPC_io_write
[];
613 extern CPUReadMemoryFunc
*PPC_io_read
[];
614 extern int prep_enabled
;
617 #include "hw/m48t59.h"
619 void NVRAM_set_byte (m48t59_t
*nvram
, uint32_t addr
, uint8_t value
);
620 uint8_t NVRAM_get_byte (m48t59_t
*nvram
, uint32_t addr
);
621 void NVRAM_set_word (m48t59_t
*nvram
, uint32_t addr
, uint16_t value
);
622 uint16_t NVRAM_get_word (m48t59_t
*nvram
, uint32_t addr
);
623 void NVRAM_set_lword (m48t59_t
*nvram
, uint32_t addr
, uint32_t value
);
624 uint32_t NVRAM_get_lword (m48t59_t
*nvram
, uint32_t addr
);
625 void NVRAM_set_string (m48t59_t
*nvram
, uint32_t addr
,
626 const unsigned char *str
, uint32_t max
);
627 int NVRAM_get_string (m48t59_t
*nvram
, uint8_t *dst
, uint16_t addr
, int max
);
628 void NVRAM_set_crc (m48t59_t
*nvram
, uint32_t addr
,
629 uint32_t start
, uint32_t count
);
630 int PPC_NVRAM_set_params (m48t59_t
*nvram
, uint16_t NVRAM_size
,
631 const unsigned char *arch
,
632 uint32_t RAM_size
, int boot_device
,
633 uint32_t kernel_image
, uint32_t kernel_size
,
634 uint32_t cmdline
, uint32_t cmdline_size
,
635 uint32_t initrd_image
, uint32_t initrd_size
,
636 uint32_t NVRAM_image
);
640 #define MAX_ADB_DEVICES 16
642 typedef struct ADBDevice ADBDevice
;
644 typedef void ADBDeviceReceivePacket(ADBDevice
*d
, const uint8_t *buf
, int len
);
647 struct ADBBusState
*bus
;
650 ADBDeviceReceivePacket
*receive_packet
;
654 typedef struct ADBBusState
{
655 ADBDevice devices
[MAX_ADB_DEVICES
];
659 void adb_receive_packet(ADBBusState
*s
, const uint8_t *buf
, int len
);
660 void adb_send_packet(ADBBusState
*s
, const uint8_t *buf
, int len
);
662 ADBDevice
*adb_register_device(ADBBusState
*s
, int devaddr
,
663 ADBDeviceReceivePacket
*receive_packet
,
665 void adb_kbd_init(ADBBusState
*bus
);
666 void adb_mouse_init(ADBBusState
*bus
);
670 extern ADBBusState adb_bus
;
674 void monitor_init(void);
675 void term_printf(const char *fmt
, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
676 void term_flush(void);
677 void term_print_help(void);
681 #define DEFAULT_GDBSTUB_PORT 1234
683 int gdbserver_start(int port
);