Statistics
| Branch: | Revision:

root / vl.h @ c20709aa

History | View | Annotate | Download (12.9 kB)

1
/*
2
 * QEMU System Emulator header
3
 * 
4
 * Copyright (c) 2003 Fabrice Bellard
5
 * 
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:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
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
22
 * THE SOFTWARE.
23
 */
24
#ifndef VL_H
25
#define VL_H
26

    
27
/* we put basic includes here to avoid repeating them in device drivers */
28
#include <stdlib.h>
29
#include <stdio.h>
30
#include <stdarg.h>
31
#include <string.h>
32
#include <inttypes.h>
33
#include <time.h>
34
#include <ctype.h>
35
#include <errno.h>
36
#include <unistd.h>
37
#include <fcntl.h>
38

    
39
#ifndef O_LARGEFILE
40
#define O_LARGEFILE 0
41
#endif
42
#ifndef O_BINARY
43
#define O_BINARY 0
44
#endif
45

    
46
#ifdef _WIN32
47
#define lseek64 lseek
48
#endif
49

    
50
#include "cpu.h"
51

    
52
#ifndef glue
53
#define xglue(x, y) x ## y
54
#define glue(x, y) xglue(x, y)
55
#define stringify(s)        tostring(s)
56
#define tostring(s)        #s
57
#endif
58

    
59
#if defined(WORDS_BIGENDIAN)
60
static inline uint32_t be32_to_cpu(uint32_t v)
61
{
62
    return v;
63
}
64

    
65
static inline uint16_t be16_to_cpu(uint16_t v)
66
{
67
    return v;
68
}
69

    
70
static inline uint32_t le32_to_cpu(uint32_t v)
71
{
72
    return bswap32(v);
73
}
74

    
75
static inline uint16_t le16_to_cpu(uint16_t v)
76
{
77
    return bswap16(v);
78
}
79

    
80
#else
81
static inline uint32_t be32_to_cpu(uint32_t v)
82
{
83
    return bswap32(v);
84
}
85

    
86
static inline uint16_t be16_to_cpu(uint16_t v)
87
{
88
    return bswap16(v);
89
}
90

    
91
static inline uint32_t le32_to_cpu(uint32_t v)
92
{
93
    return v;
94
}
95

    
96
static inline uint16_t le16_to_cpu(uint16_t v)
97
{
98
    return v;
99
}
100
#endif
101

    
102

    
103
/* vl.c */
104
extern int reset_requested;
105

    
106
typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
107
typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
108

    
109
int register_ioport_read(int start, int length, int size, 
110
                         IOPortReadFunc *func, void *opaque);
111
int register_ioport_write(int start, int length, int size, 
112
                          IOPortWriteFunc *func, void *opaque);
113
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
114

    
115
void hw_error(const char *fmt, ...);
116

    
117
int load_image(const char *filename, uint8_t *addr);
118
extern const char *bios_dir;
119

    
120
void pstrcpy(char *buf, int buf_size, const char *str);
121
char *pstrcat(char *buf, int buf_size, const char *s);
122

    
123
int serial_open_device(void);
124

    
125
extern int vm_running;
126

    
127
typedef void VMStopHandler(void *opaque, int reason);
128

    
129
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque);
130
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque);
131

    
132
void vm_start(void);
133
void vm_stop(int reason);
134

    
135
/* async I/O support */
136

    
137
typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
138
typedef int IOCanRWHandler(void *opaque);
139

    
140
int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, 
141
                             IOReadHandler *fd_read, void *opaque);
142
void qemu_del_fd_read_handler(int fd);
143

    
144
/* network redirectors support */
145

    
146
#define MAX_NICS 8
147

    
148
typedef struct NetDriverState {
149
    int index; /* index number in QEMU */
150
    uint8_t macaddr[6];
151
    char ifname[16];
152
    void (*send_packet)(struct NetDriverState *nd, 
153
                        const uint8_t *buf, int size);
154
    void (*add_read_packet)(struct NetDriverState *nd, 
155
                            IOCanRWHandler *fd_can_read, 
156
                            IOReadHandler *fd_read, void *opaque);
157
    /* tun specific data */
158
    int fd;
159
    /* slirp specific data */
160
} NetDriverState;
161

    
162
extern int nb_nics;
163
extern NetDriverState nd_table[MAX_NICS];
164

    
165
void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
166
void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, 
167
                          IOReadHandler *fd_read, void *opaque);
168

    
169
/* timers */
170

    
171
typedef struct QEMUClock QEMUClock;
172
typedef struct QEMUTimer QEMUTimer;
173
typedef void QEMUTimerCB(void *opaque);
174

    
175
/* The real time clock should be used only for stuff which does not
176
   change the virtual machine state, as it is run even if the virtual
177
   machine is stopped. The real time clock has a frequency or 1000
178
   Hz. */
179
extern QEMUClock *rt_clock;
180

    
181
/* Rge virtual clock is only run during the emulation. It is stopped
182
   when the virtual machine is stopped. Virtual timers use a high
183
   precision clock, usually cpu cycles (use ticks_per_sec). */
184
extern QEMUClock *vm_clock;
185

    
186
int64_t qemu_get_clock(QEMUClock *clock);
187

    
188
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
189
void qemu_free_timer(QEMUTimer *ts);
190
void qemu_del_timer(QEMUTimer *ts);
191
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
192
int qemu_timer_pending(QEMUTimer *ts);
193

    
194
extern int64_t ticks_per_sec;
195
extern int pit_min_timer_count;
196

    
197
void cpu_enable_ticks(void);
198
void cpu_disable_ticks(void);
199

    
200
/* VM Load/Save */
201

    
202
typedef FILE QEMUFile;
203

    
204
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
205
void qemu_put_byte(QEMUFile *f, int v);
206
void qemu_put_be16(QEMUFile *f, unsigned int v);
207
void qemu_put_be32(QEMUFile *f, unsigned int v);
208
void qemu_put_be64(QEMUFile *f, uint64_t v);
209
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
210
int qemu_get_byte(QEMUFile *f);
211
unsigned int qemu_get_be16(QEMUFile *f);
212
unsigned int qemu_get_be32(QEMUFile *f);
213
uint64_t qemu_get_be64(QEMUFile *f);
214

    
215
static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
216
{
217
    qemu_put_be64(f, *pv);
218
}
219

    
220
static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
221
{
222
    qemu_put_be32(f, *pv);
223
}
224

    
225
static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
226
{
227
    qemu_put_be16(f, *pv);
228
}
229

    
230
static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
231
{
232
    qemu_put_byte(f, *pv);
233
}
234

    
235
static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
236
{
237
    *pv = qemu_get_be64(f);
238
}
239

    
240
static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
241
{
242
    *pv = qemu_get_be32(f);
243
}
244

    
245
static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
246
{
247
    *pv = qemu_get_be16(f);
248
}
249

    
250
static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
251
{
252
    *pv = qemu_get_byte(f);
253
}
254

    
255
int64_t qemu_ftell(QEMUFile *f);
256
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
257

    
258
typedef void SaveStateHandler(QEMUFile *f, void *opaque);
259
typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
260

    
261
int qemu_loadvm(const char *filename);
262
int qemu_savevm(const char *filename);
263
int register_savevm(const char *idstr, 
264
                    int instance_id, 
265
                    int version_id,
266
                    SaveStateHandler *save_state,
267
                    LoadStateHandler *load_state,
268
                    void *opaque);
269
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
270
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
271

    
272
/* block.c */
273
typedef struct BlockDriverState BlockDriverState;
274

    
275
BlockDriverState *bdrv_new(const char *device_name);
276
void bdrv_delete(BlockDriverState *bs);
277
int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot);
278
void bdrv_close(BlockDriverState *bs);
279
int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
280
              uint8_t *buf, int nb_sectors);
281
int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
282
               const uint8_t *buf, int nb_sectors);
283
void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr);
284
int bdrv_commit(BlockDriverState *bs);
285
void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
286

    
287
#define BDRV_TYPE_HD     0
288
#define BDRV_TYPE_CDROM  1
289
#define BDRV_TYPE_FLOPPY 2
290

    
291
void bdrv_set_geometry_hint(BlockDriverState *bs, 
292
                            int cyls, int heads, int secs);
293
void bdrv_set_type_hint(BlockDriverState *bs, int type);
294
void bdrv_get_geometry_hint(BlockDriverState *bs, 
295
                            int *pcyls, int *pheads, int *psecs);
296
int bdrv_get_type_hint(BlockDriverState *bs);
297
int bdrv_is_removable(BlockDriverState *bs);
298
int bdrv_is_read_only(BlockDriverState *bs);
299
int bdrv_is_inserted(BlockDriverState *bs);
300
int bdrv_is_locked(BlockDriverState *bs);
301
void bdrv_set_locked(BlockDriverState *bs, int locked);
302
void bdrv_set_change_cb(BlockDriverState *bs, 
303
                        void (*change_cb)(void *opaque), void *opaque);
304

    
305
void bdrv_info(void);
306
BlockDriverState *bdrv_find(const char *name);
307

    
308
/* vga.c */
309

    
310
#define VGA_RAM_SIZE (4096 * 1024)
311

    
312
typedef struct DisplayState {
313
    uint8_t *data;
314
    int linesize;
315
    int depth;
316
    void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
317
    void (*dpy_resize)(struct DisplayState *s, int w, int h);
318
    void (*dpy_refresh)(struct DisplayState *s);
319
} DisplayState;
320

    
321
static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
322
{
323
    s->dpy_update(s, x, y, w, h);
324
}
325

    
326
static inline void dpy_resize(DisplayState *s, int w, int h)
327
{
328
    s->dpy_resize(s, w, h);
329
}
330

    
331
int vga_initialize(DisplayState *ds, uint8_t *vga_ram_base, 
332
                   unsigned long vga_ram_offset, int vga_ram_size);
333
void vga_update_display(void);
334
void vga_screen_dump(const char *filename);
335

    
336
/* sdl.c */
337
void sdl_display_init(DisplayState *ds);
338

    
339
/* ide.c */
340
#define MAX_DISKS 4
341

    
342
extern BlockDriverState *bs_table[MAX_DISKS];
343

    
344
void ide_init(int iobase, int iobase2, int irq,
345
              BlockDriverState *hd0, BlockDriverState *hd1);
346

    
347
/* oss.c */
348
typedef enum {
349
  AUD_FMT_U8,
350
  AUD_FMT_S8,
351
  AUD_FMT_U16,
352
  AUD_FMT_S16
353
} audfmt_e;
354

    
355
void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt);
356
void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt);
357
int AUD_write (void *in_buf, int size);
358
void AUD_run (void);
359
void AUD_adjust_estimate (int _leftover);
360
int AUD_get_free (void);
361
int AUD_get_live (void);
362
int AUD_get_buffer_size (void);
363
void AUD_init (void);
364

    
365
/* dma.c */
366
typedef int (*DMA_transfer_handler) (void *opaque, target_ulong addr, int size);
367
int DMA_get_channel_mode (int nchan);
368
void DMA_hold_DREQ (int nchan);
369
void DMA_release_DREQ (int nchan);
370
void DMA_schedule(int nchan);
371
void DMA_run (void);
372
void DMA_init (void);
373
void DMA_register_channel (int nchan,
374
                           DMA_transfer_handler transfer_handler, void *opaque);
375

    
376
/* sb16.c */
377
void SB16_run (void);
378
void SB16_init (void);
379
 
380
/* fdc.c */
381
#define MAX_FD 2
382
extern BlockDriverState *fd_table[MAX_FD];
383

    
384
typedef struct fdctrl_t fdctrl_t;
385

    
386
fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, 
387
                       uint32_t io_base,
388
                       BlockDriverState **fds);
389
int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
390

    
391
/* ne2000.c */
392

    
393
void ne2000_init(int base, int irq, NetDriverState *nd);
394

    
395
/* pckbd.c */
396

    
397
void kbd_put_keycode(int keycode);
398

    
399
#define MOUSE_EVENT_LBUTTON 0x01
400
#define MOUSE_EVENT_RBUTTON 0x02
401
#define MOUSE_EVENT_MBUTTON 0x04
402
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
403

    
404
void kbd_init(void);
405

    
406
/* mc146818rtc.c */
407

    
408
typedef struct RTCState RTCState;
409

    
410
RTCState *rtc_init(int base, int irq);
411
void rtc_set_memory(RTCState *s, int addr, int val);
412
void rtc_set_date(RTCState *s, const struct tm *tm);
413

    
414
/* serial.c */
415

    
416
typedef struct SerialState SerialState;
417

    
418
extern SerialState *serial_console;
419

    
420
SerialState *serial_init(int base, int irq, int fd);
421
int serial_can_receive(SerialState *s);
422
void serial_receive_byte(SerialState *s, int ch);
423
void serial_receive_break(SerialState *s);
424

    
425
/* i8259.c */
426

    
427
void pic_set_irq(int irq, int level);
428
void pic_init(void);
429
uint32_t pic_intack_read(CPUState *env);
430
void pic_info(void);
431

    
432
/* i8254.c */
433

    
434
#define PIT_FREQ 1193182
435

    
436
typedef struct PITChannelState {
437
    int count; /* can be 65536 */
438
    uint16_t latched_count;
439
    uint8_t rw_state;
440
    uint8_t mode;
441
    uint8_t bcd; /* not supported */
442
    uint8_t gate; /* timer start */
443
    int64_t count_load_time;
444
    /* irq handling */
445
    int64_t next_transition_time;
446
    QEMUTimer *irq_timer;
447
    int irq;
448
} PITChannelState;
449

    
450
extern PITChannelState pit_channels[3];
451

    
452
void pit_init(int base, int irq);
453
void pit_set_gate(PITChannelState *s, int val);
454
int pit_get_out(PITChannelState *s, int64_t current_time);
455
int pit_get_out_edges(PITChannelState *s);
456

    
457
/* pc.c */
458
void pc_init(int ram_size, int vga_ram_size, int boot_device,
459
             DisplayState *ds, const char **fd_filename, int snapshot,
460
             const char *kernel_filename, const char *kernel_cmdline,
461
             const char *initrd_filename);
462

    
463
/* monitor.c */
464
void monitor_init(void);
465
void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
466
void term_flush(void);
467
void term_print_help(void);
468

    
469
/* gdbstub.c */
470

    
471
#define DEFAULT_GDBSTUB_PORT 1234
472

    
473
int gdbserver_start(int port);
474

    
475
#endif /* VL_H */