/[dynamips]/upstream/dynamips-0.2.7-RC1/dev_ram.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.7-RC1/dev_ram.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Sat Oct 6 16:23:47 2007 UTC (12 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 3048 byte(s)
dynamips-0.2.7-RC1

1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot. All rights reserved.
4 *
5 * RAM emulation.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <errno.h>
13 #include <unistd.h>
14
15 #include "cpu.h"
16 #include "vm.h"
17 #include "dynamips.h"
18 #include "memory.h"
19 #include "device.h"
20
21 /* RAM private data */
22 struct ram_data {
23 vm_obj_t vm_obj;
24 struct vdevice *dev;
25 char *filename;
26 int delete_file;
27 };
28
29 /* Shutdown a RAM device */
30 void dev_ram_shutdown(vm_instance_t *vm,struct ram_data *d)
31 {
32 if (d != NULL) {
33 /* Remove the device */
34 dev_remove(vm,d->dev);
35 free(d->dev);
36
37 /* Remove filename used to virtualize RAM */
38 if (d->filename && d->delete_file) {
39 unlink(d->filename);
40 free(d->filename);
41 }
42
43 /* Free the structure itself */
44 free(d);
45 }
46 }
47
48 /* Initialize a RAM zone */
49 int dev_ram_init(vm_instance_t *vm,char *name,int use_mmap,int delete_file,
50 char *alternate_name,int sparse,
51 m_uint64_t paddr,m_uint32_t len)
52 {
53 struct ram_data *d;
54
55 /* allocate the private data structure */
56 if (!(d = malloc(sizeof(*d)))) {
57 fprintf(stderr,"RAM: unable to create device.\n");
58 return(-1);
59 }
60
61 memset(d,0,sizeof(*d));
62 d->delete_file = delete_file;
63
64 vm_object_init(&d->vm_obj);
65 d->vm_obj.name = name;
66 d->vm_obj.data = d;
67 d->vm_obj.shutdown = (vm_shutdown_t)dev_ram_shutdown;
68
69 if (use_mmap) {
70 if (!alternate_name)
71 d->filename = vm_build_filename(vm,name);
72 else
73 d->filename = strdup(alternate_name);
74
75 if (!d->filename) {
76 fprintf(stderr,"RAM: unable to create filename.\n");
77 goto err_filename;
78 }
79 }
80
81 if (!(d->dev = dev_create_ram(vm,name,sparse,d->filename,paddr,len))) {
82 fprintf(stderr,"RAM: unable to create device.\n");
83 goto err_dev_create;
84 }
85
86 vm_object_add(vm,&d->vm_obj);
87 return(0);
88
89 err_dev_create:
90 free(d->filename);
91 err_filename:
92 free(d);
93 return(-1);
94 }
95
96 /* Initialize a ghosted RAM zone */
97 int dev_ram_ghost_init(vm_instance_t *vm,char *name,int sparse,char *filename,
98 m_uint64_t paddr,m_uint32_t len)
99 {
100 struct ram_data *d;
101
102 /* allocate the private data structure */
103 if (!filename || !(d = malloc(sizeof(*d)))) {
104 fprintf(stderr,"RAM_ghost: unable to create device.\n");
105 return(-1);
106 }
107
108 memset(d,0,sizeof(*d));
109 d->delete_file = FALSE;
110
111 if (!(d->filename = strdup(filename)))
112 goto err_filename;
113
114 vm_object_init(&d->vm_obj);
115 d->vm_obj.name = name;
116 d->vm_obj.data = d;
117 d->vm_obj.shutdown = (vm_shutdown_t)dev_ram_shutdown;
118
119 if (!(d->dev = dev_create_ghost_ram(vm,name,sparse,d->filename,
120 paddr,len)))
121 {
122 fprintf(stderr,"RAM_ghost: unable to create device.\n");
123 goto err_dev_create;
124 }
125
126 vm_object_add(vm,&d->vm_obj);
127 return(0);
128
129 err_dev_create:
130 free(d->filename);
131 err_filename:
132 free(d);
133 return(-1);
134 }

  ViewVC Help
Powered by ViewVC 1.1.26