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
55 #define ftruncate64 ftruncate
56 #define mkstemp64 mkstemp
57 #define MAP_ANONYMOUS MAP_ANON
61 #define xglue(x, y) x ## y
62 #define glue(x, y) xglue(x, y)
63 #define stringify(s) tostring(s)
64 #define tostring(s) #s
67 #if defined(WORDS_BIGENDIAN)
68 static inline uint32_t be32_to_cpu(uint32_t v
)
73 static inline uint16_t be16_to_cpu(uint16_t v
)
78 static inline uint32_t cpu_to_be32(uint32_t v
)
83 static inline uint16_t cpu_to_be16(uint16_t v
)
88 static inline uint32_t le32_to_cpu(uint32_t v
)
93 static inline uint16_t le16_to_cpu(uint16_t v
)
98 static inline uint32_t cpu_to_le32(uint32_t v
)
103 static inline uint16_t cpu_to_le16(uint16_t v
)
110 static inline uint32_t be32_to_cpu(uint32_t v
)
115 static inline uint16_t be16_to_cpu(uint16_t v
)
120 static inline uint32_t cpu_to_be32(uint32_t v
)
125 static inline uint16_t cpu_to_be16(uint16_t v
)
130 static inline uint32_t le32_to_cpu(uint32_t v
)
135 static inline uint16_t le16_to_cpu(uint16_t v
)
140 static inline uint32_t cpu_to_le32(uint32_t v
)
145 static inline uint16_t cpu_to_le16(uint16_t v
)
153 extern int reset_requested
;
155 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
);
157 void hw_error(const char *fmt
, ...);
159 int load_image(const char *filename
, uint8_t *addr
);
160 extern const char *bios_dir
;
162 void pstrcpy(char *buf
, int buf_size
, const char *str
);
163 char *pstrcat(char *buf
, int buf_size
, const char *s
);
165 int serial_open_device(void);
167 extern int vm_running
;
169 typedef void VMStopHandler(void *opaque
, int reason
);
171 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
);
172 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
);
175 void vm_stop(int reason
);
177 extern int audio_enabled
;
179 /* async I/O support */
181 typedef void IOReadHandler(void *opaque
, const uint8_t *buf
, int size
);
182 typedef int IOCanRWHandler(void *opaque
);
184 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
185 IOReadHandler
*fd_read
, void *opaque
);
186 void qemu_del_fd_read_handler(int fd
);
188 /* network redirectors support */
192 typedef struct NetDriverState
{
193 int index
; /* index number in QEMU */
196 void (*send_packet
)(struct NetDriverState
*nd
,
197 const uint8_t *buf
, int size
);
198 void (*add_read_packet
)(struct NetDriverState
*nd
,
199 IOCanRWHandler
*fd_can_read
,
200 IOReadHandler
*fd_read
, void *opaque
);
201 /* tun specific data */
203 /* slirp specific data */
207 extern NetDriverState nd_table
[MAX_NICS
];
209 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
);
210 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
211 IOReadHandler
*fd_read
, void *opaque
);
215 typedef struct QEMUClock QEMUClock
;
216 typedef struct QEMUTimer QEMUTimer
;
217 typedef void QEMUTimerCB(void *opaque
);
219 /* The real time clock should be used only for stuff which does not
220 change the virtual machine state, as it is run even if the virtual
221 machine is stopped. The real time clock has a frequency of 1000
223 extern QEMUClock
*rt_clock
;
225 /* Rge virtual clock is only run during the emulation. It is stopped
226 when the virtual machine is stopped. Virtual timers use a high
227 precision clock, usually cpu cycles (use ticks_per_sec). */
228 extern QEMUClock
*vm_clock
;
230 int64_t qemu_get_clock(QEMUClock
*clock
);
232 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
);
233 void qemu_free_timer(QEMUTimer
*ts
);
234 void qemu_del_timer(QEMUTimer
*ts
);
235 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
);
236 int qemu_timer_pending(QEMUTimer
*ts
);
238 extern int64_t ticks_per_sec
;
239 extern int pit_min_timer_count
;
241 void cpu_enable_ticks(void);
242 void cpu_disable_ticks(void);
246 typedef FILE QEMUFile
;
248 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
);
249 void qemu_put_byte(QEMUFile
*f
, int v
);
250 void qemu_put_be16(QEMUFile
*f
, unsigned int v
);
251 void qemu_put_be32(QEMUFile
*f
, unsigned int v
);
252 void qemu_put_be64(QEMUFile
*f
, uint64_t v
);
253 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
);
254 int qemu_get_byte(QEMUFile
*f
);
255 unsigned int qemu_get_be16(QEMUFile
*f
);
256 unsigned int qemu_get_be32(QEMUFile
*f
);
257 uint64_t qemu_get_be64(QEMUFile
*f
);
259 static inline void qemu_put_be64s(QEMUFile
*f
, const uint64_t *pv
)
261 qemu_put_be64(f
, *pv
);
264 static inline void qemu_put_be32s(QEMUFile
*f
, const uint32_t *pv
)
266 qemu_put_be32(f
, *pv
);
269 static inline void qemu_put_be16s(QEMUFile
*f
, const uint16_t *pv
)
271 qemu_put_be16(f
, *pv
);
274 static inline void qemu_put_8s(QEMUFile
*f
, const uint8_t *pv
)
276 qemu_put_byte(f
, *pv
);
279 static inline void qemu_get_be64s(QEMUFile
*f
, uint64_t *pv
)
281 *pv
= qemu_get_be64(f
);
284 static inline void qemu_get_be32s(QEMUFile
*f
, uint32_t *pv
)
286 *pv
= qemu_get_be32(f
);
289 static inline void qemu_get_be16s(QEMUFile
*f
, uint16_t *pv
)
291 *pv
= qemu_get_be16(f
);
294 static inline void qemu_get_8s(QEMUFile
*f
, uint8_t *pv
)
296 *pv
= qemu_get_byte(f
);
299 int64_t qemu_ftell(QEMUFile
*f
);
300 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
);
302 typedef void SaveStateHandler(QEMUFile
*f
, void *opaque
);
303 typedef int LoadStateHandler(QEMUFile
*f
, void *opaque
, int version_id
);
305 int qemu_loadvm(const char *filename
);
306 int qemu_savevm(const char *filename
);
307 int register_savevm(const char *idstr
,
310 SaveStateHandler
*save_state
,
311 LoadStateHandler
*load_state
,
313 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
);
314 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
);
317 typedef struct BlockDriverState BlockDriverState
;
319 BlockDriverState
*bdrv_new(const char *device_name
);
320 void bdrv_delete(BlockDriverState
*bs
);
321 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int snapshot
);
322 void bdrv_close(BlockDriverState
*bs
);
323 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
324 uint8_t *buf
, int nb_sectors
);
325 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
326 const uint8_t *buf
, int nb_sectors
);
327 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
);
328 int bdrv_commit(BlockDriverState
*bs
);
329 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
);
331 #define BDRV_TYPE_HD 0
332 #define BDRV_TYPE_CDROM 1
333 #define BDRV_TYPE_FLOPPY 2
335 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
336 int cyls
, int heads
, int secs
);
337 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
);
338 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
339 int *pcyls
, int *pheads
, int *psecs
);
340 int bdrv_get_type_hint(BlockDriverState
*bs
);
341 int bdrv_is_removable(BlockDriverState
*bs
);
342 int bdrv_is_read_only(BlockDriverState
*bs
);
343 int bdrv_is_inserted(BlockDriverState
*bs
);
344 int bdrv_is_locked(BlockDriverState
*bs
);
345 void bdrv_set_locked(BlockDriverState
*bs
, int locked
);
346 void bdrv_set_change_cb(BlockDriverState
*bs
,
347 void (*change_cb
)(void *opaque
), void *opaque
);
349 void bdrv_info(void);
350 BlockDriverState
*bdrv_find(const char *name
);
354 extern target_phys_addr_t isa_mem_base
;
356 typedef void (IOPortWriteFunc
)(void *opaque
, uint32_t address
, uint32_t data
);
357 typedef uint32_t (IOPortReadFunc
)(void *opaque
, uint32_t address
);
359 int register_ioport_read(int start
, int length
, int size
,
360 IOPortReadFunc
*func
, void *opaque
);
361 int register_ioport_write(int start
, int length
, int size
,
362 IOPortWriteFunc
*func
, void *opaque
);
363 void isa_unassign_ioport(int start
, int length
);
367 extern int pci_enabled
;
369 extern target_phys_addr_t pci_mem_base
;
371 typedef struct PCIDevice PCIDevice
;
373 typedef void PCIConfigWriteFunc(PCIDevice
*pci_dev
,
374 uint32_t address
, uint32_t data
, int len
);
375 typedef uint32_t PCIConfigReadFunc(PCIDevice
*pci_dev
,
376 uint32_t address
, int len
);
377 typedef void PCIMapIORegionFunc(PCIDevice
*pci_dev
, int region_num
,
378 uint32_t addr
, uint32_t size
, int type
);
380 #define PCI_ADDRESS_SPACE_MEM 0x00
381 #define PCI_ADDRESS_SPACE_IO 0x01
382 #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0x08
384 typedef struct PCIIORegion
{
388 PCIMapIORegionFunc
*map_func
;
392 /* PCI config space */
395 /* the following fields are read only */
399 PCIIORegion io_regions
[6];
401 /* do not access the following fields */
402 PCIConfigReadFunc
*config_read
;
403 PCIConfigWriteFunc
*config_write
;
406 PCIDevice
*pci_register_device(const char *name
, int instance_size
,
407 int bus_num
, int devfn
,
408 PCIConfigReadFunc
*config_read
,
409 PCIConfigWriteFunc
*config_write
);
411 void pci_register_io_region(PCIDevice
*pci_dev
, int region_num
,
412 uint32_t size
, int type
,
413 PCIMapIORegionFunc
*map_func
);
415 void i440fx_init(void);
416 void piix3_init(void);
417 void pci_bios_init(void);
421 #define VGA_RAM_SIZE (4096 * 1024)
423 typedef struct DisplayState
{
427 void (*dpy_update
)(struct DisplayState
*s
, int x
, int y
, int w
, int h
);
428 void (*dpy_resize
)(struct DisplayState
*s
, int w
, int h
);
429 void (*dpy_refresh
)(struct DisplayState
*s
);
432 static inline void dpy_update(DisplayState
*s
, int x
, int y
, int w
, int h
)
434 s
->dpy_update(s
, x
, y
, w
, h
);
437 static inline void dpy_resize(DisplayState
*s
, int w
, int h
)
439 s
->dpy_resize(s
, w
, h
);
442 int vga_initialize(DisplayState
*ds
, uint8_t *vga_ram_base
,
443 unsigned long vga_ram_offset
, int vga_ram_size
);
444 void vga_update_display(void);
445 void vga_screen_dump(const char *filename
);
448 void sdl_display_init(DisplayState
*ds
);
453 extern BlockDriverState
*bs_table
[MAX_DISKS
];
455 void isa_ide_init(int iobase
, int iobase2
, int irq
,
456 BlockDriverState
*hd0
, BlockDriverState
*hd1
);
457 void pci_ide_init(BlockDriverState
**hd_table
);
467 void AUD_open (int rfreq
, int rnchannels
, audfmt_e rfmt
);
468 void AUD_reset (int rfreq
, int rnchannels
, audfmt_e rfmt
);
469 int AUD_write (void *in_buf
, int size
);
471 void AUD_adjust_estimate (int _leftover
);
472 int AUD_get_free (void);
473 int AUD_get_live (void);
474 int AUD_get_buffer_size (void);
475 void AUD_init (void);
478 typedef int (*DMA_transfer_handler
) (void *opaque
, target_ulong addr
, int size
);
479 int DMA_get_channel_mode (int nchan
);
480 void DMA_hold_DREQ (int nchan
);
481 void DMA_release_DREQ (int nchan
);
482 void DMA_schedule(int nchan
);
484 void DMA_init (void);
485 void DMA_register_channel (int nchan
,
486 DMA_transfer_handler transfer_handler
, void *opaque
);
489 void SB16_run (void);
490 void SB16_init (void);
494 extern BlockDriverState
*fd_table
[MAX_FD
];
496 typedef struct fdctrl_t fdctrl_t
;
498 fdctrl_t
*fdctrl_init (int irq_lvl
, int dma_chann
, int mem_mapped
,
500 BlockDriverState
**fds
);
501 int fdctrl_get_drive_type(fdctrl_t
*fdctrl
, int drive_num
);
505 void isa_ne2000_init(int base
, int irq
, NetDriverState
*nd
);
506 void pci_ne2000_init(NetDriverState
*nd
);
510 void kbd_put_keycode(int keycode
);
512 #define MOUSE_EVENT_LBUTTON 0x01
513 #define MOUSE_EVENT_RBUTTON 0x02
514 #define MOUSE_EVENT_MBUTTON 0x04
515 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
);
521 typedef struct RTCState RTCState
;
523 RTCState
*rtc_init(int base
, int irq
);
524 void rtc_set_memory(RTCState
*s
, int addr
, int val
);
525 void rtc_set_date(RTCState
*s
, const struct tm
*tm
);
529 typedef struct SerialState SerialState
;
531 extern SerialState
*serial_console
;
533 SerialState
*serial_init(int base
, int irq
, int fd
);
534 int serial_can_receive(SerialState
*s
);
535 void serial_receive_byte(SerialState
*s
, int ch
);
536 void serial_receive_break(SerialState
*s
);
540 void pic_set_irq(int irq
, int level
);
542 uint32_t pic_intack_read(CPUState
*env
);
547 #define PIT_FREQ 1193182
549 typedef struct PITState PITState
;
551 PITState
*pit_init(int base
, int irq
);
552 void pit_set_gate(PITState
*pit
, int channel
, int val
);
553 int pit_get_gate(PITState
*pit
, int channel
);
554 int pit_get_out(PITState
*pit
, int channel
, int64_t current_time
);
557 void pc_init(int ram_size
, int vga_ram_size
, int boot_device
,
558 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
559 const char *kernel_filename
, const char *kernel_cmdline
,
560 const char *initrd_filename
);
563 void ppc_init (int ram_size
, int vga_ram_size
, int boot_device
,
564 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
565 const char *kernel_filename
, const char *kernel_cmdline
,
566 const char *initrd_filename
);
569 void monitor_init(void);
570 void term_printf(const char *fmt
, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
571 void term_flush(void);
572 void term_print_help(void);
576 #define DEFAULT_GDBSTUB_PORT 1234
578 int gdbserver_start(int port
);