Debian Codegraph @

common/memory.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 */
5
6 #define _GNU_SOURCE
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <assert.h>
15
16 #include "cpu.h"
17 #include "vm.h"
18 #include "dynamips.h"
19 #include "memory.h"
20 #include "device.h"
21
22 /* Record a memory access */
23 void memlog_rec_access(cpu_gen_t *cpu,m_uint64_t vaddr,m_uint64_t data,
24 m_uint32_t op_size,m_uint32_t op_type)
25 {
26 memlog_access_t *acc;
27
28 acc = &cpu->memlog_array[cpu->memlog_pos];
29 acc->iaddr = cpu_get_pc(cpu);
30 acc->vaddr = vaddr;
31 acc->data = data;
32 acc->op_size = op_size;
33 acc->op_type = op_type;
34 acc->data_valid = (op_type == MTS_WRITE);
35
36 cpu->memlog_pos = (cpu->memlog_pos + 1) & (MEMLOG_COUNT - 1);
37 }
38
39 /* Show the latest memory accesses */
40 void memlog_dump(cpu_gen_t *cpu)
41 {
42 memlog_access_t *acc;
43 char s_data[64];
44 u_int i,pos;
45
46 for(i=0;i<MEMLOG_COUNT;i++) {
47 pos = cpu->memlog_pos + i;
48 pos &= (MEMLOG_COUNT-1);
49 acc = &cpu->memlog_array[pos];
50
51 if (cpu_get_pc(cpu)) {
52 if (acc->data_valid)
53 snprintf(s_data,sizeof(s_data),"0x%llx",acc->data);
54 else
55 snprintf(s_data,sizeof(s_data),"XXXXXXXX");
56
57 printf("CPU%u: pc=0x%8.8llx, vaddr=0x%8.8llx, "
58 "size=%u, type=%s, data=%s\n",
59 cpu->id,acc->iaddr,acc->vaddr,acc->op_size,
60 (acc->op_type == MTS_READ) ? "read " : "write",
61 s_data);
62 }
63 }
64 }
65
66 /* Update the data obtained by a read access */
67 void memlog_update_read(cpu_gen_t *cpu,m_iptr_t raddr)
68 {
69 memlog_access_t *acc;
70
71 acc = &cpu->memlog_array[(cpu->memlog_pos-1) & (MEMLOG_COUNT-1)];
72
73 if (acc->op_type == MTS_READ)
74 {
75 switch(acc->op_size) {
76 case 1:
77 acc->data = *(m_uint8_t *)raddr;
78 break;
79 case 2:
80 acc->data = vmtoh16(*(m_uint16_t *)raddr);
81 break;
82 case 4:
83 acc->data = vmtoh32(*(m_uint32_t *)raddr);
84 break;
85 case 8:
86 acc->data = vmtoh64(*(m_uint64_t *)raddr);
87 break;
88 }
89
90 acc->data_valid = TRUE;
91 }
92 }
93
94
95 /* === Operations on physical memory ====================================== */
96
97 /* Get host pointer for the physical address */
98 static inline void *physmem_get_hptr(vm_instance_t *vm,m_uint64_t paddr,
99 u_int op_size,u_int op_type,
100 m_uint64_t *data)
101 {
102 struct vdevice *dev;
103 m_uint32_t offset;
104 void *ptr;
105 int cow;
106
107 if (!(dev = dev_lookup(vm,paddr,FALSE)))
108 return NULL;
109
110 if (dev->flags & VDEVICE_FLAG_SPARSE) {
111 ptr = (void *)dev_sparse_get_host_addr(vm,dev,paddr,op_type,&cow);
112 if (!ptr) return NULL;
113
114 return(ptr + (paddr & VM_PAGE_IMASK));
115 }
116
117 if ((dev->host_addr != 0) && !(dev->flags & VDEVICE_FLAG_NO_MTS_MMAP))
118 return((void *)dev->host_addr + (paddr - dev->phys_addr));
119
120 if (op_size == 0)
121 return NULL;
122
123 offset = paddr - dev->phys_addr;
124 return(dev->handler(vm->boot_cpu,dev,offset,op_size,op_type,data));
125 }
126
127 /* Copy a memory block from VM physical RAM to real host */
128 void physmem_copy_from_vm(vm_instance_t *vm,void *real_buffer,
129 m_uint64_t paddr,size_t len)
130 {
131 m_uint64_t dummy;
132 m_uint32_t r;
133 u_char *ptr;
134
135 while(len > 0) {
136 r = m_min(VM_PAGE_SIZE - (paddr & VM_PAGE_IMASK), len);
137 ptr = physmem_get_hptr(vm,paddr,0,MTS_READ,&dummy);
138
139 if (likely(ptr != NULL)) {
140 memcpy(real_buffer,ptr,r);
141 } else {
142 r = m_min(len,4);
143 switch(r) {
144 case 4:
145 *(m_uint32_t *)real_buffer =
146 htovm32(physmem_copy_u32_from_vm(vm,paddr));
147 break;
148 case 2:
149 *(m_uint16_t *)real_buffer =
150 htovm16(physmem_copy_u16_from_vm(vm,paddr));
151 break;
152 case 1:
153 *(m_uint8_t *)real_buffer = physmem_copy_u8_from_vm(vm,paddr);
154 break;
155 }
156 }
157
158 real_buffer += r;
159 paddr += r;
160 len -= r;
161 }
162 }
163
164 /* Copy a memory block to VM physical RAM from real host */
165 void physmem_copy_to_vm(vm_instance_t *vm,void *real_buffer,
166 m_uint64_t paddr,size_t len)
167 {
168 m_uint64_t dummy;
169 m_uint32_t r;
170 u_char *ptr;
171
172 while(len > 0) {
173 r = m_min(VM_PAGE_SIZE - (paddr & VM_PAGE_IMASK), len);
174 ptr = physmem_get_hptr(vm,paddr,0,MTS_WRITE,&dummy);
175
176 if (likely(ptr != NULL)) {
177 memcpy(ptr,real_buffer,r);
178 } else {
179 r = m_min(len,4);
180 switch(r) {
181 case 4:
182 physmem_copy_u32_to_vm(vm,paddr,
183 htovm32(*(m_uint32_t *)real_buffer));
184 break;
185 case 2:
186 physmem_copy_u16_to_vm(vm,paddr,
187 htovm16(*(m_uint16_t *)real_buffer));
188 break;
189 case 1:
190 physmem_copy_u8_to_vm(vm,paddr,*(m_uint8_t *)real_buffer);
191 break;
192 }
193 }
194
195 real_buffer += r;
196 paddr += r;
197 len -= r;
198 }
199 }
200
201 /* Copy a 32-bit word from the VM physical RAM to real host */
202 m_uint32_t physmem_copy_u32_from_vm(vm_instance_t *vm,m_uint64_t paddr)
203 {
204 m_uint64_t tmp = 0;
205 m_uint32_t *ptr;
206
207 if ((ptr = physmem_get_hptr(vm,paddr,4,MTS_READ,&tmp)) != NULL)
208 return(vmtoh32(*ptr));
209
210 return(tmp);
211 }
212
213 /* Copy a 32-bit word to the VM physical RAM from real host */
214 void physmem_copy_u32_to_vm(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t val)
215 {
216 m_uint64_t tmp = val;
217 m_uint32_t *ptr;
218
219 if ((ptr = physmem_get_hptr(vm,paddr,4,MTS_WRITE,&tmp)) != NULL)
220 *ptr = htovm32(val);
221 }
222
223 /* Copy a 16-bit word from the VM physical RAM to real host */
224 m_uint16_t physmem_copy_u16_from_vm(vm_instance_t *vm,m_uint64_t paddr)
225 {
226 m_uint64_t tmp = 0;
227 m_uint16_t *ptr;
228
229 if ((ptr = physmem_get_hptr(vm,paddr,2,MTS_READ,&tmp)) != NULL)
230 return(vmtoh16(*ptr));
231
232 return(tmp);
233 }
234
235 /* Copy a 16-bit word to the VM physical RAM from real host */
236 void physmem_copy_u16_to_vm(vm_instance_t *vm,m_uint64_t paddr,m_uint16_t val)
237 {
238 m_uint64_t tmp = val;
239 m_uint16_t *ptr;
240
241 if ((ptr = physmem_get_hptr(vm,paddr,2,MTS_WRITE,&tmp)) != NULL)
242 *ptr = htovm16(val);
243 }
244
245 /* Copy a byte from the VM physical RAM to real host */
246 m_uint8_t physmem_copy_u8_from_vm(vm_instance_t *vm,m_uint64_t paddr)
247 {
248 m_uint64_t tmp = 0;
249 m_uint8_t *ptr;
250
251 if ((ptr = physmem_get_hptr(vm,paddr,1,MTS_READ,&tmp)) != NULL)
252 return(*ptr);
253
254 return(tmp);
255 }
256
257 /* Copy a 16-bit word to the VM physical RAM from real host */
258 void physmem_copy_u8_to_vm(vm_instance_t *vm,m_uint64_t paddr,m_uint8_t val)
259 {
260 m_uint64_t tmp = val;
261 m_uint8_t *ptr;
262
263 if ((ptr = physmem_get_hptr(vm,paddr,1,MTS_WRITE,&tmp)) != NULL)
264 *ptr = val;
265 }
266
267 /* DMA transfer operation */
268 void physmem_dma_transfer(vm_instance_t *vm,m_uint64_t src,m_uint64_t dst,
269 size_t len)
270 {
271 m_uint64_t dummy;
272 u_char *sptr,*dptr;
273 size_t clen,sl,dl;
274
275 while(len > 0) {
276 sptr = physmem_get_hptr(vm,src,0,MTS_READ,&dummy);
277 dptr = physmem_get_hptr(vm,dst,0,MTS_WRITE,&dummy);
278
279 if (!sptr || !dptr) {
280 vm_log(vm,"DMA","unable to transfer from 0x%llx to 0x%llx\n",src,dst);
281 return;
282 }
283
284 sl = VM_PAGE_SIZE - (src & VM_PAGE_IMASK);
285 dl = VM_PAGE_SIZE - (dst & VM_PAGE_IMASK);
286 clen = m_min(sl,dl);
287 clen = m_min(clen,len);
288
289 memcpy(dptr,sptr,clen);
290
291 src += clen;
292 dst += clen;
293 len -= clen;
294 }
295 }
296
297 /* strlen in VM physical memory */
298 size_t physmem_strlen(vm_instance_t *vm,m_uint64_t paddr)
299 {
300 struct vdevice *vm_ram;
301 size_t len = 0;
302 char *ptr;
303
304 if ((vm_ram = dev_lookup(vm,paddr,TRUE)) != NULL) {
305 ptr = (char *)vm_ram->host_addr + (paddr - vm_ram->phys_addr);
306 len = strlen(ptr);
307 }
308
309 return(len);
310 }
311
312 /* find sequence of bytes in VM cacheable physical memory interval [first,last] */
313 int physmem_cfind(vm_instance_t *vm,m_uint8_t *bytes,size_t len,
314 m_uint64_t first,m_uint64_t last, m_uint64_t *paddr)
315 {
316 struct vdevice *dev;
317 m_uint8_t *buffer;
318 size_t i, buflen;
319 m_uint64_t last_dev_addr;
320
321 if (len <= 0 || first > last || len + 1 > last - first)
322 return(-1); // nothing to find
323
324 buffer = malloc(len);
325 i = 0;
326 buflen = 0;
327 for(dev = vm->dev_list; dev; dev = dev->next) { // each device
328 if (dev == NULL || dev->phys_addr > last)
329 break; // no more devices
330 if (!(dev->flags & VDEVICE_FLAG_CACHING))
331 continue; // not cacheable
332
333 // reset buffer if previous device is not continuous in memory
334 if (first + buflen != dev->phys_addr) {
335 i = 0;
336 buflen = 0;
337 }
338 last_dev_addr = dev->phys_addr + dev->phys_len - 1;
339 if (last_dev_addr > last)
340 last_dev_addr = last;
341
342 // fill buffer
343 for (; buflen < len && first + buflen <= last_dev_addr; ++buflen) {
344 buffer[buflen] = physmem_copy_u8_from_vm(vm, first + buflen);
345 }
346 if (buflen < len)
347 continue; // not enough data
348
349 // test each possible match
350 while (first + len <= last_dev_addr) {
351 if (i >= len)
352 i = 0;
353 if ((i == 0 || memcmp(buffer, bytes+len-i, i) == 0) &&
354 memcmp(buffer+i, bytes, len-i) == 0) {
355 // match found
356 if (paddr)
357 *paddr = first;
358 free(buffer);
359 return(0);
360 }
361
362 buffer[i] = physmem_copy_u8_from_vm(vm, first + len);
363 ++i;
364 ++first;
365 }
366 }
367
368 free(buffer);
369 return(-1); // not found
370 }
371
372 /* Physical memory dump (32-bit words) */
373 void physmem_dump_vm(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t u32_count)
374 {
375 m_uint32_t i;
376
377 for(i=0;i<u32_count;i++) {
378 vm_log(vm,"physmem_dump","0x%8.8llx: 0x%8.8x\n",
379 paddr+(i<<2),physmem_copy_u32_from_vm(vm,paddr+(i<<2)));
380 }
381 }