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
;
174 /* XXX: make it dynamic */
175 #if defined (TARGET_PPC)
176 #define BIOS_SIZE (512 * 1024)
181 /* async I/O support */
183 typedef void IOReadHandler(void *opaque
, const uint8_t *buf
, int size
);
184 typedef int IOCanRWHandler(void *opaque
);
186 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
187 IOReadHandler
*fd_read
, void *opaque
);
188 void qemu_del_fd_read_handler(int fd
);
190 /* network redirectors support */
194 typedef struct NetDriverState
{
195 int index
; /* index number in QEMU */
198 void (*send_packet
)(struct NetDriverState
*nd
,
199 const uint8_t *buf
, int size
);
200 void (*add_read_packet
)(struct NetDriverState
*nd
,
201 IOCanRWHandler
*fd_can_read
,
202 IOReadHandler
*fd_read
, void *opaque
);
203 /* tun specific data */
205 /* slirp specific data */
209 extern NetDriverState nd_table
[MAX_NICS
];
211 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
);
212 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
213 IOReadHandler
*fd_read
, void *opaque
);
217 typedef struct QEMUClock QEMUClock
;
218 typedef struct QEMUTimer QEMUTimer
;
219 typedef void QEMUTimerCB(void *opaque
);
221 /* The real time clock should be used only for stuff which does not
222 change the virtual machine state, as it is run even if the virtual
223 machine is stopped. The real time clock has a frequency of 1000
225 extern QEMUClock
*rt_clock
;
227 /* Rge virtual clock is only run during the emulation. It is stopped
228 when the virtual machine is stopped. Virtual timers use a high
229 precision clock, usually cpu cycles (use ticks_per_sec). */
230 extern QEMUClock
*vm_clock
;
232 int64_t qemu_get_clock(QEMUClock
*clock
);
234 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
);
235 void qemu_free_timer(QEMUTimer
*ts
);
236 void qemu_del_timer(QEMUTimer
*ts
);
237 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
);
238 int qemu_timer_pending(QEMUTimer
*ts
);
240 extern int64_t ticks_per_sec
;
241 extern int pit_min_timer_count
;
243 void cpu_enable_ticks(void);
244 void cpu_disable_ticks(void);
248 typedef FILE QEMUFile
;
250 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
);
251 void qemu_put_byte(QEMUFile
*f
, int v
);
252 void qemu_put_be16(QEMUFile
*f
, unsigned int v
);
253 void qemu_put_be32(QEMUFile
*f
, unsigned int v
);
254 void qemu_put_be64(QEMUFile
*f
, uint64_t v
);
255 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
);
256 int qemu_get_byte(QEMUFile
*f
);
257 unsigned int qemu_get_be16(QEMUFile
*f
);
258 unsigned int qemu_get_be32(QEMUFile
*f
);
259 uint64_t qemu_get_be64(QEMUFile
*f
);
261 static inline void qemu_put_be64s(QEMUFile
*f
, const uint64_t *pv
)
263 qemu_put_be64(f
, *pv
);
266 static inline void qemu_put_be32s(QEMUFile
*f
, const uint32_t *pv
)
268 qemu_put_be32(f
, *pv
);
271 static inline void qemu_put_be16s(QEMUFile
*f
, const uint16_t *pv
)
273 qemu_put_be16(f
, *pv
);
276 static inline void qemu_put_8s(QEMUFile
*f
, const uint8_t *pv
)
278 qemu_put_byte(f
, *pv
);
281 static inline void qemu_get_be64s(QEMUFile
*f
, uint64_t *pv
)
283 *pv
= qemu_get_be64(f
);
286 static inline void qemu_get_be32s(QEMUFile
*f
, uint32_t *pv
)
288 *pv
= qemu_get_be32(f
);
291 static inline void qemu_get_be16s(QEMUFile
*f
, uint16_t *pv
)
293 *pv
= qemu_get_be16(f
);
296 static inline void qemu_get_8s(QEMUFile
*f
, uint8_t *pv
)
298 *pv
= qemu_get_byte(f
);
301 int64_t qemu_ftell(QEMUFile
*f
);
302 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
);
304 typedef void SaveStateHandler(QEMUFile
*f
, void *opaque
);
305 typedef int LoadStateHandler(QEMUFile
*f
, void *opaque
, int version_id
);
307 int qemu_loadvm(const char *filename
);
308 int qemu_savevm(const char *filename
);
309 int register_savevm(const char *idstr
,
312 SaveStateHandler
*save_state
,
313 LoadStateHandler
*load_state
,
315 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
);
316 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
);
319 typedef struct BlockDriverState BlockDriverState
;
321 BlockDriverState
*bdrv_new(const char *device_name
);
322 void bdrv_delete(BlockDriverState
*bs
);
323 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int snapshot
);
324 void bdrv_close(BlockDriverState
*bs
);
325 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
326 uint8_t *buf
, int nb_sectors
);
327 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
328 const uint8_t *buf
, int nb_sectors
);
329 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
);
330 int bdrv_commit(BlockDriverState
*bs
);
331 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
);
333 #define BDRV_TYPE_HD 0
334 #define BDRV_TYPE_CDROM 1
335 #define BDRV_TYPE_FLOPPY 2
337 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
338 int cyls
, int heads
, int secs
);
339 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
);
340 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
341 int *pcyls
, int *pheads
, int *psecs
);
342 int bdrv_get_type_hint(BlockDriverState
*bs
);
343 int bdrv_is_removable(BlockDriverState
*bs
);
344 int bdrv_is_read_only(BlockDriverState
*bs
);
345 int bdrv_is_inserted(BlockDriverState
*bs
);
346 int bdrv_is_locked(BlockDriverState
*bs
);
347 void bdrv_set_locked(BlockDriverState
*bs
, int locked
);
348 void bdrv_set_change_cb(BlockDriverState
*bs
,
349 void (*change_cb
)(void *opaque
), void *opaque
);
351 void bdrv_info(void);
352 BlockDriverState
*bdrv_find(const char *name
);
356 extern target_phys_addr_t isa_mem_base
;
358 typedef void (IOPortWriteFunc
)(void *opaque
, uint32_t address
, uint32_t data
);
359 typedef uint32_t (IOPortReadFunc
)(void *opaque
, uint32_t address
);
361 int register_ioport_read(int start
, int length
, int size
,
362 IOPortReadFunc
*func
, void *opaque
);
363 int register_ioport_write(int start
, int length
, int size
,
364 IOPortWriteFunc
*func
, void *opaque
);
365 void isa_unassign_ioport(int start
, int length
);
369 extern int pci_enabled
;
371 extern target_phys_addr_t pci_mem_base
;
373 typedef struct PCIDevice PCIDevice
;
375 typedef void PCIConfigWriteFunc(PCIDevice
*pci_dev
,
376 uint32_t address
, uint32_t data
, int len
);
377 typedef uint32_t PCIConfigReadFunc(PCIDevice
*pci_dev
,
378 uint32_t address
, int len
);
379 typedef void PCIMapIORegionFunc(PCIDevice
*pci_dev
, int region_num
,
380 uint32_t addr
, uint32_t size
, int type
);
382 #define PCI_ADDRESS_SPACE_MEM 0x00
383 #define PCI_ADDRESS_SPACE_IO 0x01
384 #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0x08
386 typedef struct PCIIORegion
{
387 uint32_t addr
; /* current PCI mapping address. -1 means not mapped */
390 PCIMapIORegionFunc
*map_func
;
394 /* PCI config space */
397 /* the following fields are read only */
401 PCIIORegion io_regions
[6];
403 /* do not access the following fields */
404 PCIConfigReadFunc
*config_read
;
405 PCIConfigWriteFunc
*config_write
;
409 PCIDevice
*pci_register_device(const char *name
, int instance_size
,
410 int bus_num
, int devfn
,
411 PCIConfigReadFunc
*config_read
,
412 PCIConfigWriteFunc
*config_write
);
414 void pci_register_io_region(PCIDevice
*pci_dev
, int region_num
,
415 uint32_t size
, int type
,
416 PCIMapIORegionFunc
*map_func
);
418 void pci_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
);
420 uint32_t pci_default_read_config(PCIDevice
*d
,
421 uint32_t address
, int len
);
422 void pci_default_write_config(PCIDevice
*d
,
423 uint32_t address
, uint32_t val
, int len
);
425 extern struct PIIX3State
*piix3_state
;
427 void i440fx_init(void);
428 void piix3_init(void);
429 void pci_bios_init(void);
432 /* temporary: will be moved in platform specific file */
433 void pci_prep_init(void);
434 void pci_pmac_init(void);
435 void pci_ppc_bios_init(void);
439 #define VGA_RAM_SIZE (4096 * 1024)
441 typedef struct DisplayState
{
445 void (*dpy_update
)(struct DisplayState
*s
, int x
, int y
, int w
, int h
);
446 void (*dpy_resize
)(struct DisplayState
*s
, int w
, int h
);
447 void (*dpy_refresh
)(struct DisplayState
*s
);
450 static inline void dpy_update(DisplayState
*s
, int x
, int y
, int w
, int h
)
452 s
->dpy_update(s
, x
, y
, w
, h
);
455 static inline void dpy_resize(DisplayState
*s
, int w
, int h
)
457 s
->dpy_resize(s
, w
, h
);
460 int vga_initialize(DisplayState
*ds
, uint8_t *vga_ram_base
,
461 unsigned long vga_ram_offset
, int vga_ram_size
,
463 void vga_update_display(void);
464 void vga_screen_dump(const char *filename
);
467 void sdl_display_init(DisplayState
*ds
);
472 extern BlockDriverState
*bs_table
[MAX_DISKS
];
474 void isa_ide_init(int iobase
, int iobase2
, int irq
,
475 BlockDriverState
*hd0
, BlockDriverState
*hd1
);
476 void pci_ide_init(BlockDriverState
**hd_table
);
477 void pci_piix3_ide_init(BlockDriverState
**hd_table
);
487 void AUD_open (int rfreq
, int rnchannels
, audfmt_e rfmt
);
488 void AUD_reset (int rfreq
, int rnchannels
, audfmt_e rfmt
);
489 int AUD_write (void *in_buf
, int size
);
491 void AUD_adjust_estimate (int _leftover
);
492 int AUD_get_free (void);
493 int AUD_get_live (void);
494 int AUD_get_buffer_size (void);
495 void AUD_init (void);
498 typedef int (*DMA_transfer_handler
) (void *opaque
, target_ulong addr
, int size
);
499 int DMA_get_channel_mode (int nchan
);
500 void DMA_hold_DREQ (int nchan
);
501 void DMA_release_DREQ (int nchan
);
502 void DMA_schedule(int nchan
);
504 void DMA_init (void);
505 void DMA_register_channel (int nchan
,
506 DMA_transfer_handler transfer_handler
, void *opaque
);
509 void SB16_run (void);
510 void SB16_init (void);
514 extern BlockDriverState
*fd_table
[MAX_FD
];
516 typedef struct fdctrl_t fdctrl_t
;
518 fdctrl_t
*fdctrl_init (int irq_lvl
, int dma_chann
, int mem_mapped
,
520 BlockDriverState
**fds
);
521 int fdctrl_get_drive_type(fdctrl_t
*fdctrl
, int drive_num
);
525 void isa_ne2000_init(int base
, int irq
, NetDriverState
*nd
);
526 void pci_ne2000_init(NetDriverState
*nd
);
530 void kbd_put_keycode(int keycode
);
532 #define MOUSE_EVENT_LBUTTON 0x01
533 #define MOUSE_EVENT_RBUTTON 0x02
534 #define MOUSE_EVENT_MBUTTON 0x04
535 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
);
541 typedef struct RTCState RTCState
;
543 RTCState
*rtc_init(int base
, int irq
);
544 void rtc_set_memory(RTCState
*s
, int addr
, int val
);
545 void rtc_set_date(RTCState
*s
, const struct tm
*tm
);
549 typedef struct SerialState SerialState
;
551 extern SerialState
*serial_console
;
553 SerialState
*serial_init(int base
, int irq
, int fd
);
554 int serial_can_receive(SerialState
*s
);
555 void serial_receive_byte(SerialState
*s
, int ch
);
556 void serial_receive_break(SerialState
*s
);
560 void pic_set_irq(int irq
, int level
);
562 uint32_t pic_intack_read(CPUState
*env
);
568 #define PIT_FREQ 1193182
570 typedef struct PITState PITState
;
572 PITState
*pit_init(int base
, int irq
);
573 void pit_set_gate(PITState
*pit
, int channel
, int val
);
574 int pit_get_gate(PITState
*pit
, int channel
);
575 int pit_get_out(PITState
*pit
, int channel
, int64_t current_time
);
578 void pc_init(int ram_size
, int vga_ram_size
, int boot_device
,
579 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
580 const char *kernel_filename
, const char *kernel_cmdline
,
581 const char *initrd_filename
);
584 void ppc_init (int ram_size
, int vga_ram_size
, int boot_device
,
585 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
586 const char *kernel_filename
, const char *kernel_cmdline
,
587 const char *initrd_filename
);
588 void ppc_prep_init (int ram_size
, int vga_ram_size
, int boot_device
,
589 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
590 const char *kernel_filename
, const char *kernel_cmdline
,
591 const char *initrd_filename
);
592 void ppc_chrp_init(int ram_size
, int vga_ram_size
, int boot_device
,
593 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
594 const char *kernel_filename
, const char *kernel_cmdline
,
595 const char *initrd_filename
);
596 ppc_tb_t
*cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
);
597 void PREP_debug_write (void *opaque
, uint32_t addr
, uint32_t val
);
599 extern CPUWriteMemoryFunc
*PPC_io_write
[];
600 extern CPUReadMemoryFunc
*PPC_io_read
[];
601 extern int prep_enabled
;
604 #include "hw/m48t59.h"
606 void NVRAM_set_byte (m48t59_t
*nvram
, uint32_t addr
, uint8_t value
);
607 uint8_t NVRAM_get_byte (m48t59_t
*nvram
, uint32_t addr
);
608 void NVRAM_set_word (m48t59_t
*nvram
, uint32_t addr
, uint16_t value
);
609 uint16_t NVRAM_get_word (m48t59_t
*nvram
, uint32_t addr
);
610 void NVRAM_set_lword (m48t59_t
*nvram
, uint32_t addr
, uint32_t value
);
611 uint32_t NVRAM_get_lword (m48t59_t
*nvram
, uint32_t addr
);
612 void NVRAM_set_string (m48t59_t
*nvram
, uint32_t addr
,
613 const unsigned char *str
, uint32_t max
);
614 int NVRAM_get_string (m48t59_t
*nvram
, uint8_t *dst
, uint16_t addr
, int max
);
615 void NVRAM_set_crc (m48t59_t
*nvram
, uint32_t addr
,
616 uint32_t start
, uint32_t count
);
617 int PPC_NVRAM_set_params (m48t59_t
*nvram
, uint16_t NVRAM_size
,
618 const unsigned char *arch
,
619 uint32_t RAM_size
, int boot_device
,
620 uint32_t kernel_image
, uint32_t kernel_size
,
621 uint32_t cmdline
, uint32_t cmdline_size
,
622 uint32_t initrd_image
, uint32_t initrd_size
,
623 uint32_t NVRAM_image
);
626 void monitor_init(void);
627 void term_printf(const char *fmt
, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
628 void term_flush(void);
629 void term_print_help(void);
633 #define DEFAULT_GDBSTUB_PORT 1234
635 int gdbserver_start(int port
);