Debian Codegraph @

stable/vm.h

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * Virtual Machines.
6 */
7
8 #ifndef __VM_H__
9 #define __VM_H__
10
11 #include <pthread.h>
12
13 #include "dynamips.h"
14 #include "memory.h"
15 #include "cpu.h"
16 #include "dev_vtty.h"
17 #include "cisco_eeprom.h"
18 #include "cisco_card.h"
19 #include "rommon_var.h"
20
21 #define VM_PAGE_SHIFT 12
22 #define VM_PAGE_SIZE (1 << VM_PAGE_SHIFT)
23 #define VM_PAGE_IMASK (VM_PAGE_SIZE - 1)
24 #define VM_PAGE_MASK (~(VM_PAGE_IMASK))
25
26 /* Number of pages in chunk area */
27 #define VM_CHUNK_AREA_SIZE 256
28
29 /* VM memory chunk */
30 typedef struct vm_chunk vm_chunk_t;
31 struct vm_chunk {
32 void *area;
33 u_int page_alloc,page_total;
34 vm_chunk_t *next;
35 };
36
37 /* VM ghost pool entry */
38 typedef struct vm_ghost_image vm_ghost_image_t;
39 struct vm_ghost_image {
40 char *filename;
41 u_int ref_count;
42 int fd;
43 off_t file_size;
44 u_char *area_ptr;
45 vm_ghost_image_t *next;
46 };
47
48 /* Maximum number of devices per VM */
49 #define VM_DEVICE_MAX (1 << 6)
50
51 /* Size of the PCI bus pool */
52 #define VM_PCI_POOL_SIZE 32
53
54 /* VM instance status */
55 enum {
56 VM_STATUS_HALTED = 0, /* VM is halted and no HW resources are used */
57 VM_STATUS_SHUTDOWN, /* Shutdown procedure engaged */
58 VM_STATUS_RUNNING, /* VM is running */
59 VM_STATUS_SUSPENDED, /* VM is suspended */
60 };
61
62 /* Ghost RAM status */
63 enum {
64 VM_GHOST_RAM_NONE = 0,
65 VM_GHOST_RAM_GENERATE,
66 VM_GHOST_RAM_USE,
67 };
68
69 /* Timer IRQ check interval */
70 #define VM_TIMER_IRQ_CHECK_ITV 1000
71
72 /* Max slots per VM */
73 #define VM_MAX_SLOTS 16
74
75 /* forward declarations */
76 typedef struct vm_obj vm_obj_t;
77
78 /* Shutdown function prototype for an object */
79 typedef void *(*vm_shutdown_t)(vm_instance_t *vm,void *data);
80
81 /* VM object, used to keep track of devices and various things */
82 struct vm_obj {
83 char *name;
84 void *data;
85 struct vm_obj *next,**pprev;
86 vm_shutdown_t shutdown;
87 };
88
89 /* VM instance */
90 struct vm_instance {
91 char *name;
92 vm_platform_t *platform; /* Platform specific helpers */
93 int status; /* Instance status */
94 int instance_id; /* Instance Identifier */
95 char *lock_file; /* Lock file */
96 char *log_file; /* Log filename */
97 int log_file_enabled; /* Logging enabled */
98 u_int ram_size,rom_size; /* RAM and ROM size in Mb */
99 u_int ram_res_size; /* RAM reserved space size */
100 u_int iomem_size; /* IOMEM size in Mb */
101 u_int nvram_size; /* NVRAM size in Kb */
102 u_int pcmcia_disk_size[2]; /* PCMCIA disk0 and disk1 sizes (in Mb) */
103 u_int conf_reg,conf_reg_setup; /* Config register */
104 u_int clock_divisor; /* Clock Divisor (see cp0.c) */
105 u_int ram_mmap; /* Memory-mapped RAM ? */
106 u_int restart_ios; /* Restart IOS on reload ? */
107 u_int elf_machine_id; /* ELF machine identifier */
108 u_int exec_area_size; /* Size of execution area for CPU */
109 m_uint32_t ios_entry_point; /* IOS entry point */
110 char *ios_image; /* IOS image filename */
111 char *ios_startup_config; /* IOS configuration file for startup-config */
112 char *ios_private_config; /* IOS configuration file for private-config */
113 char *rom_filename; /* ROM filename */
114 char *sym_filename; /* Symbol filename */
115 FILE *lock_fd,*log_fd; /* Lock/Log file descriptors */
116 int debug_level; /* Debugging Level */
117 int jit_use; /* CPUs use JIT */
118 int sparse_mem; /* Use sparse virtual memory */
119 u_int nm_iomem_size; /* IO mem size to be passed to Smart Init */
120
121 /* ROMMON variables */
122 struct rommon_var_list rommon_vars;
123
124 /* Memory chunks */
125 vm_chunk_t *chunks;
126
127 /* Basic hardware: system CPU, PCI busses and PCI I/O space */
128 cpu_group_t *cpu_group;
129 cpu_gen_t *boot_cpu;
130 struct pci_bus *pci_bus[2];
131 struct pci_bus *pci_bus_pool[VM_PCI_POOL_SIZE];
132 struct pci_io_data *pci_io_space;
133
134 /* Memory mapped devices */
135 struct vdevice *dev_list;
136 struct vdevice *dev_array[VM_DEVICE_MAX];
137
138 /* IRQ routing */
139 void (*set_irq)(vm_instance_t *vm,u_int irq);
140 void (*clear_irq)(vm_instance_t *vm,u_int irq);
141
142 /* Slots for PA/NM/... */
143 u_int nr_slots;
144 u_int slots_type;
145 struct cisco_card *slots[VM_MAX_SLOTS];
146 struct cisco_card_driver **slots_drivers;
147 struct pci_bus *slots_pci_bus[VM_MAX_SLOTS];
148
149 /* Filename for ghosted RAM */
150 char *ghost_ram_filename;
151
152 /* Ghost RAM image handling */
153 int ghost_status;
154
155 /* Timer IRQ interval check */
156 u_int timer_irq_check_itv;
157
158 /* "idling" pointer counter */
159 m_uint64_t idle_pc;
160
161 /* JIT block direct jumps */
162 int exec_blk_direct_jump;
163
164 /* IRQ idling preemption */
165 u_int irq_idle_preempt[256];
166
167 /* Console and AUX port VTTY type and parameters */
168 int vtty_con_type,vtty_aux_type;
169 int vtty_con_tcp_port,vtty_aux_tcp_port;
170 vtty_serial_option_t vtty_con_serial_option,vtty_aux_serial_option;
171
172 /* Virtual TTY for Console and AUX ports */
173 vtty_t *vtty_con,*vtty_aux;
174
175 /* Space reserved in NVRAM by ROM monitor */
176 u_int nvram_rom_space;
177
178 /* Chassis cookie (for c2600 and maybe other routers) */
179 m_uint16_t chassis_cookie[64];
180
181 /* Specific hardware data */
182 void *hw_data;
183
184 /* VM objects */
185 struct vm_obj *vm_object_list;
186 };
187
188 /* VM Platform definition */
189 struct vm_platform {
190 char *name;
191 char *log_name;
192 char *cli_name;
193 int (*create_instance)(vm_instance_t *vm);
194 int (*delete_instance)(vm_instance_t *vm);
195 int (*init_instance)(vm_instance_t *vm);
196 int (*stop_instance)(vm_instance_t *vm);
197 int (*oir_start)(vm_instance_t *vm,u_int slot_id,u_int subslot_id);
198 int (*oir_stop)(vm_instance_t *vm,u_int slot_id,u_int subslot_id);
199 int (*nvram_extract_config)(vm_instance_t *vm,u_char **startup_config,size_t *startup_len,u_char **private_config,size_t *private_len);
200 int (*nvram_push_config)(vm_instance_t *vm,u_char *startup_config,size_t startup_len,u_char *private_config,size_t private_len);
201 u_int (*get_mac_addr_msb)(void);
202 void (*save_config)(vm_instance_t *vm,FILE *fd);
203 int (*cli_parse_options)(vm_instance_t *vm,int option);
204 void (*cli_show_options)(vm_instance_t *vm);
205 void (*show_spec_drivers)(void);
206 };
207
208 /* VM platform list item */
209 struct vm_platform_list {
210 struct vm_platform_list *next;
211 struct vm_platform *platform;
212 };
213
214 extern int vm_file_naming_type;
215
216 /* Set an IRQ for a VM */
217 static inline void vm_set_irq(vm_instance_t *vm,u_int irq)
218 {
219 if (vm->set_irq != NULL)
220 vm->set_irq(vm,irq);
221 }
222
223 /* Clear an IRQ for a VM */
224 static inline void vm_clear_irq(vm_instance_t *vm,u_int irq)
225 {
226 if (vm->clear_irq != NULL)
227 vm->clear_irq(vm,irq);
228 }
229
230 /* Initialize a VM object */
231 void vm_object_init(vm_obj_t *obj);
232
233 /* Add a VM object to an instance */
234 void vm_object_add(vm_instance_t *vm,vm_obj_t *obj);
235
236 /* Remove a VM object from an instance */
237 void vm_object_remove(vm_instance_t *vm,vm_obj_t *obj);
238
239 /* Find an object given its name */
240 vm_obj_t *vm_object_find(vm_instance_t *vm,char *name);
241
242 /* Check that a mandatory object is present */
243 int vm_object_check(vm_instance_t *vm,char *name);
244
245 /* Dump the object list of an instance */
246 void vm_object_dump(vm_instance_t *vm);
247
248 /* Get VM type */
249 char *vm_get_type(vm_instance_t *vm);
250
251 /* Get MAC address MSB */
252 u_int vm_get_mac_addr_msb(vm_instance_t *vm);
253
254 /* Generate a filename for use by the instance */
255 char *vm_build_filename(vm_instance_t *vm,char *name);
256
257 /* Get the amount of host virtual memory used by a VM */
258 size_t vm_get_vspace_size(vm_instance_t *vm);
259
260 /* Check that an instance lock file doesn't already exist */
261 int vm_get_lock(vm_instance_t *vm);
262
263 /* Erase lock file */
264 void vm_release_lock(vm_instance_t *vm,int erase);
265
266 /* Log a message */
267 void vm_flog(vm_instance_t *vm,char *module,char *format,va_list ap);
268
269 /* Log a message */
270 void vm_log(vm_instance_t *vm,char *module,char *format,...);
271
272 /* Close the log file */
273 int vm_close_log(vm_instance_t *vm);
274
275 /* Create the log file */
276 int vm_create_log(vm_instance_t *vm);
277
278 /* Reopen the log file */
279 int vm_reopen_log(vm_instance_t *vm);
280
281 /* Error message */
282 void vm_error(vm_instance_t *vm,char *format,...);
283
284 /* Shutdown hardware resources used by a VM */
285 int vm_hardware_shutdown(vm_instance_t *vm);
286
287 /* Free resources used by a VM */
288 void vm_free(vm_instance_t *vm);
289
290 /* Get an instance given a name */
291 vm_instance_t *vm_acquire(char *name);
292
293 /* Release a VM (decrement reference count) */
294 int vm_release(vm_instance_t *vm);
295
296 /* Initialize RAM */
297 int vm_ram_init(vm_instance_t *vm,m_uint64_t paddr);
298
299 /* Initialize VTTY */
300 int vm_init_vtty(vm_instance_t *vm);
301
302 /* Delete VTTY */
303 void vm_delete_vtty(vm_instance_t *vm);
304
305 /* Bind a device to a virtual machine */
306 int vm_bind_device(vm_instance_t *vm,struct vdevice *dev);
307
308 /* Unbind a device from a virtual machine */
309 int vm_unbind_device(vm_instance_t *vm,struct vdevice *dev);
310
311 /* Map a device at the specified physical address */
312 int vm_map_device(vm_instance_t *vm,struct vdevice *dev,m_uint64_t base_addr);
313
314 /* Set an IRQ for a VM */
315 void vm_set_irq(vm_instance_t *vm,u_int irq);
316
317 /* Clear an IRQ for a VM */
318 void vm_clear_irq(vm_instance_t *vm,u_int irq);
319
320 /* Suspend a VM instance */
321 int vm_suspend(vm_instance_t *vm);
322
323 /* Resume a VM instance */
324 int vm_resume(vm_instance_t *vm);
325
326 /* Stop an instance */
327 int vm_stop(vm_instance_t *vm);
328
329 /* Monitor an instance periodically */
330 void vm_monitor(vm_instance_t *vm);
331
332 /* Allocate an host page */
333 void *vm_alloc_host_page(vm_instance_t *vm);
334
335 /* Free an host page */
336 void vm_free_host_page(vm_instance_t *vm,void *ptr);
337
338 /* Get a ghost image */
339 int vm_ghost_image_get(char *filename,u_char **ptr,int *fd);
340
341 /* Release a ghost image */
342 int vm_ghost_image_release(int fd);
343
344 /* Open a VM file and map it in memory */
345 int vm_mmap_open_file(vm_instance_t *vm,char *name,
346 u_char **ptr,off_t *fsize);
347
348 /* Open/Create a VM file and map it in memory */
349 int vm_mmap_create_file(vm_instance_t *vm,char *name,size_t len,u_char **ptr);
350
351 /* Close a memory mapped file */
352 int vm_mmap_close_file(int fd,u_char *ptr,size_t len);
353
354 /* Save the Cisco IOS configuration from NVRAM */
355 int vm_ios_save_config(vm_instance_t *vm);
356
357 /* Set Cisco IOS image to use */
358 int vm_ios_set_image(vm_instance_t *vm,char *ios_image);
359
360 /* Unset a Cisco IOS configuration file */
361 void vm_ios_unset_config(vm_instance_t *vm);
362
363 /* Set Cisco IOS configuration files to use (NULL to keep existing data) */
364 int vm_ios_set_config(vm_instance_t *vm,const char *startup_filename,const char *private_filename);
365
366 /* Extract IOS configuration from NVRAM and write it to a file */
367 int vm_nvram_extract_config(vm_instance_t *vm,char *filename);
368
369 /* Read IOS configuraton from the files and push it to NVRAM (NULL to keep existing data) */
370 int vm_nvram_push_config(vm_instance_t *vm,const char *startup_filename,const char *private_filename);
371
372 /* Save general VM configuration into the specified file */
373 void vm_save_config(vm_instance_t *vm,FILE *fd);
374
375 /* Find a platform */
376 vm_platform_t *vm_platform_find(char *name);
377
378 /* Find a platform given its CLI name */
379 vm_platform_t *vm_platform_find_cli_name(char *name);
380
381 /* Register a platform */
382 int vm_platform_register(vm_platform_t *platform);
383
384 /* Create an instance of the specified type */
385 vm_instance_t *vm_create_instance(char *name,int instance_id,char *type);
386
387 /* Delete a VM instance */
388 int vm_delete_instance(char *name);
389
390 /* Rename a VM instance */
391 int vm_rename_instance(vm_instance_t *vm, char *name);
392
393 /* Initialize a VM instance */
394 int vm_init_instance(vm_instance_t *vm);
395
396 /* Stop a VM instance */
397 int vm_stop_instance(vm_instance_t *vm);
398
399 /* Delete all VM instances */
400 int vm_delete_all_instances(void);
401
402 /* Save all VM configs */
403 int vm_save_config_all(FILE *fd);
404
405 /* OIR to start a slot/subslot */
406 int vm_oir_start(vm_instance_t *vm,u_int slot,u_int subslot);
407
408 /* OIR to stop a slot/subslot */
409 int vm_oir_stop(vm_instance_t *vm,u_int slot,u_int subslot);
410
411 #endif