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 /* async I/O support */
184 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
185 typedef int IOCanRWHandler(void *opaque);
187 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
188 IOReadHandler *fd_read, void *opaque);
189 void qemu_del_fd_read_handler(int fd);
191 /* network redirectors support */
195 typedef struct NetDriverState {
196 int index; /* index number in QEMU */
199 void (*send_packet)(struct NetDriverState *nd,
200 const uint8_t *buf, int size);
201 void (*add_read_packet)(struct NetDriverState *nd,
202 IOCanRWHandler *fd_can_read,
203 IOReadHandler *fd_read, void *opaque);
204 /* tun specific data */
206 /* slirp specific data */
210 extern NetDriverState nd_table[MAX_NICS];
212 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
213 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
214 IOReadHandler *fd_read, void *opaque);
218 typedef struct QEMUClock QEMUClock;
219 typedef struct QEMUTimer QEMUTimer;
220 typedef void QEMUTimerCB(void *opaque);
222 /* The real time clock should be used only for stuff which does not
223 change the virtual machine state, as it is run even if the virtual
224 machine is stopped. The real time clock has a frequency of 1000
226 extern QEMUClock *rt_clock;
228 /* Rge virtual clock is only run during the emulation. It is stopped
229 when the virtual machine is stopped. Virtual timers use a high
230 precision clock, usually cpu cycles (use ticks_per_sec). */
231 extern QEMUClock *vm_clock;
233 int64_t qemu_get_clock(QEMUClock *clock);
235 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
236 void qemu_free_timer(QEMUTimer *ts);
237 void qemu_del_timer(QEMUTimer *ts);
238 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
239 int qemu_timer_pending(QEMUTimer *ts);
241 extern int64_t ticks_per_sec;
242 extern int pit_min_timer_count;
244 void cpu_enable_ticks(void);
245 void cpu_disable_ticks(void);
249 typedef FILE QEMUFile;
251 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
252 void qemu_put_byte(QEMUFile *f, int v);
253 void qemu_put_be16(QEMUFile *f, unsigned int v);
254 void qemu_put_be32(QEMUFile *f, unsigned int v);
255 void qemu_put_be64(QEMUFile *f, uint64_t v);
256 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
257 int qemu_get_byte(QEMUFile *f);
258 unsigned int qemu_get_be16(QEMUFile *f);
259 unsigned int qemu_get_be32(QEMUFile *f);
260 uint64_t qemu_get_be64(QEMUFile *f);
262 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
264 qemu_put_be64(f, *pv);
267 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
269 qemu_put_be32(f, *pv);
272 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
274 qemu_put_be16(f, *pv);
277 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
279 qemu_put_byte(f, *pv);
282 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
284 *pv = qemu_get_be64(f);
287 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
289 *pv = qemu_get_be32(f);
292 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
294 *pv = qemu_get_be16(f);
297 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
299 *pv = qemu_get_byte(f);
302 int64_t qemu_ftell(QEMUFile *f);
303 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
305 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
306 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
308 int qemu_loadvm(const char *filename);
309 int qemu_savevm(const char *filename);
310 int register_savevm(const char *idstr,
313 SaveStateHandler *save_state,
314 LoadStateHandler *load_state,
316 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
317 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
320 typedef struct BlockDriverState BlockDriverState;
322 BlockDriverState *bdrv_new(const char *device_name);
323 void bdrv_delete(BlockDriverState *bs);
324 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot);
325 void bdrv_close(BlockDriverState *bs);
326 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
327 uint8_t *buf, int nb_sectors);
328 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
329 const uint8_t *buf, int nb_sectors);
330 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr);
331 int bdrv_commit(BlockDriverState *bs);
332 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
334 #define BDRV_TYPE_HD 0
335 #define BDRV_TYPE_CDROM 1
336 #define BDRV_TYPE_FLOPPY 2
338 void bdrv_set_geometry_hint(BlockDriverState *bs,
339 int cyls, int heads, int secs);
340 void bdrv_set_type_hint(BlockDriverState *bs, int type);
341 void bdrv_get_geometry_hint(BlockDriverState *bs,
342 int *pcyls, int *pheads, int *psecs);
343 int bdrv_get_type_hint(BlockDriverState *bs);
344 int bdrv_is_removable(BlockDriverState *bs);
345 int bdrv_is_read_only(BlockDriverState *bs);
346 int bdrv_is_inserted(BlockDriverState *bs);
347 int bdrv_is_locked(BlockDriverState *bs);
348 void bdrv_set_locked(BlockDriverState *bs, int locked);
349 void bdrv_set_change_cb(BlockDriverState *bs,
350 void (*change_cb)(void *opaque), void *opaque);
352 void bdrv_info(void);
353 BlockDriverState *bdrv_find(const char *name);
357 extern target_phys_addr_t isa_mem_base;
359 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
360 typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
362 int register_ioport_read(int start, int length, int size,
363 IOPortReadFunc *func, void *opaque);
364 int register_ioport_write(int start, int length, int size,
365 IOPortWriteFunc *func, void *opaque);
366 void isa_unassign_ioport(int start, int length);
370 extern int pci_enabled;
372 extern target_phys_addr_t pci_mem_base;
374 typedef struct PCIDevice PCIDevice;
376 typedef void PCIConfigWriteFunc(PCIDevice *pci_dev,
377 uint32_t address, uint32_t data, int len);
378 typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev,
379 uint32_t address, int len);
380 typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num,
381 uint32_t addr, uint32_t size, int type);
383 #define PCI_ADDRESS_SPACE_MEM 0x00
384 #define PCI_ADDRESS_SPACE_IO 0x01
385 #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0x08
387 typedef struct PCIIORegion {
388 uint32_t addr; /* current PCI mapping address. -1 means not mapped */
391 PCIMapIORegionFunc *map_func;
394 #define PCI_ROM_SLOT 6
395 #define PCI_NUM_REGIONS 7
397 /* PCI config space */
400 /* the following fields are read only */
404 PCIIORegion io_regions[PCI_NUM_REGIONS];
406 /* do not access the following fields */
407 PCIConfigReadFunc *config_read;
408 PCIConfigWriteFunc *config_write;
412 PCIDevice *pci_register_device(const char *name, int instance_size,
413 int bus_num, int devfn,
414 PCIConfigReadFunc *config_read,
415 PCIConfigWriteFunc *config_write);
417 void pci_register_io_region(PCIDevice *pci_dev, int region_num,
418 uint32_t size, int type,
419 PCIMapIORegionFunc *map_func);
421 void pci_set_irq(PCIDevice *pci_dev, int irq_num, int level);
423 uint32_t pci_default_read_config(PCIDevice *d,
424 uint32_t address, int len);
425 void pci_default_write_config(PCIDevice *d,
426 uint32_t address, uint32_t val, int len);
428 extern struct PIIX3State *piix3_state;
430 void i440fx_init(void);
431 void piix3_init(void);
432 void pci_bios_init(void);
435 /* temporary: will be moved in platform specific file */
436 void pci_prep_init(void);
437 void pci_pmac_init(void);
438 void pci_ppc_bios_init(void);
442 #define VGA_RAM_SIZE (4096 * 1024)
444 typedef struct DisplayState {
448 void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
449 void (*dpy_resize)(struct DisplayState *s, int w, int h);
450 void (*dpy_refresh)(struct DisplayState *s);
453 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
455 s->dpy_update(s, x, y, w, h);
458 static inline void dpy_resize(DisplayState *s, int w, int h)
460 s->dpy_resize(s, w, h);
463 int vga_initialize(DisplayState *ds, uint8_t *vga_ram_base,
464 unsigned long vga_ram_offset, int vga_ram_size,
466 void vga_update_display(void);
467 void vga_screen_dump(const char *filename);
470 void sdl_display_init(DisplayState *ds);
475 extern BlockDriverState *bs_table[MAX_DISKS];
477 void isa_ide_init(int iobase, int iobase2, int irq,
478 BlockDriverState *hd0, BlockDriverState *hd1);
479 void pci_ide_init(BlockDriverState **hd_table);
480 void pci_piix3_ide_init(BlockDriverState **hd_table);
490 void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt);
491 void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt);
492 int AUD_write (void *in_buf, int size);
494 void AUD_adjust_estimate (int _leftover);
495 int AUD_get_free (void);
496 int AUD_get_live (void);
497 int AUD_get_buffer_size (void);
498 void AUD_init (void);
501 typedef int (*DMA_transfer_handler) (void *opaque, target_ulong addr, int size);
502 int DMA_get_channel_mode (int nchan);
503 void DMA_hold_DREQ (int nchan);
504 void DMA_release_DREQ (int nchan);
505 void DMA_schedule(int nchan);
507 void DMA_init (void);
508 void DMA_register_channel (int nchan,
509 DMA_transfer_handler transfer_handler, void *opaque);
512 void SB16_run (void);
513 void SB16_init (void);
517 extern BlockDriverState *fd_table[MAX_FD];
519 typedef struct fdctrl_t fdctrl_t;
521 fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped,
523 BlockDriverState **fds);
524 int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
528 void isa_ne2000_init(int base, int irq, NetDriverState *nd);
529 void pci_ne2000_init(NetDriverState *nd);
533 void kbd_put_keycode(int keycode);
535 #define MOUSE_EVENT_LBUTTON 0x01
536 #define MOUSE_EVENT_RBUTTON 0x02
537 #define MOUSE_EVENT_MBUTTON 0x04
538 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
544 typedef struct RTCState RTCState;
546 RTCState *rtc_init(int base, int irq);
547 void rtc_set_memory(RTCState *s, int addr, int val);
548 void rtc_set_date(RTCState *s, const struct tm *tm);
552 typedef struct SerialState SerialState;
554 extern SerialState *serial_console;
556 SerialState *serial_init(int base, int irq, int fd);
557 int serial_can_receive(SerialState *s);
558 void serial_receive_byte(SerialState *s, int ch);
559 void serial_receive_break(SerialState *s);
563 void pic_set_irq(int irq, int level);
565 uint32_t pic_intack_read(CPUState *env);
571 #define PIT_FREQ 1193182
573 typedef struct PITState PITState;
575 PITState *pit_init(int base, int irq);
576 void pit_set_gate(PITState *pit, int channel, int val);
577 int pit_get_gate(PITState *pit, int channel);
578 int pit_get_out(PITState *pit, int channel, int64_t current_time);
581 void pc_init(int ram_size, int vga_ram_size, int boot_device,
582 DisplayState *ds, const char **fd_filename, int snapshot,
583 const char *kernel_filename, const char *kernel_cmdline,
584 const char *initrd_filename);
587 void ppc_init (int ram_size, int vga_ram_size, int boot_device,
588 DisplayState *ds, const char **fd_filename, int snapshot,
589 const char *kernel_filename, const char *kernel_cmdline,
590 const char *initrd_filename);
591 void ppc_prep_init (int ram_size, int vga_ram_size, int boot_device,
592 DisplayState *ds, const char **fd_filename, int snapshot,
593 const char *kernel_filename, const char *kernel_cmdline,
594 const char *initrd_filename);
595 void ppc_chrp_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);
600 ppc_tb_t *cpu_ppc_tb_init (CPUState *env, uint32_t freq);
602 void PREP_debug_write (void *opaque, uint32_t addr, uint32_t val);
604 extern CPUWriteMemoryFunc *PPC_io_write[];
605 extern CPUReadMemoryFunc *PPC_io_read[];
606 extern int prep_enabled;
609 #include "hw/m48t59.h"
611 void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value);
612 uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr);
613 void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value);
614 uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr);
615 void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value);
616 uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr);
617 void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
618 const unsigned char *str, uint32_t max);
619 int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max);
620 void NVRAM_set_crc (m48t59_t *nvram, uint32_t addr,
621 uint32_t start, uint32_t count);
622 int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
623 const unsigned char *arch,
624 uint32_t RAM_size, int boot_device,
625 uint32_t kernel_image, uint32_t kernel_size,
626 uint32_t cmdline, uint32_t cmdline_size,
627 uint32_t initrd_image, uint32_t initrd_size,
628 uint32_t NVRAM_image);
631 void monitor_init(void);
632 void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
633 void term_flush(void);
634 void term_print_help(void);
638 #define DEFAULT_GDBSTUB_PORT 1234
640 int gdbserver_start(int port);