File: dev_bswap.c

package info (click to toggle)
dynamips 0.2.7-0.2.8RC2-5.1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 4,184 kB
  • sloc: ansic: 70,972; makefile: 1,639; perl: 20
file content (114 lines) | stat: -rw-r--r-- 2,639 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Cisco router simulation platform.
 * Copyright (c) 2007 Christophe Fillot (cf@utc.fr)
 *
 * Byte-swapping device.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "cpu.h"
#include "vm.h"
#include "dynamips.h"
#include "memory.h"
#include "device.h"

struct bswap_data {
   /* VM object info */
   vm_obj_t vm_obj;
   
   /* VM instance */
   vm_instance_t *vm;

   /* Byte-swap device */
   struct vdevice dev;

   /* Physical address base for rewrite */
   m_uint64_t phys_base;
};

/*
 * Byte swapped access.
 */
static void *dev_bswap_access(cpu_gen_t *cpu,struct vdevice *dev,
                              m_uint32_t offset,u_int op_size,u_int op_type,
                              m_uint64_t *data)
{
   struct bswap_data *d = dev->priv_data;
   m_uint64_t paddr;

   paddr = d->phys_base + offset;

   switch(op_size) {
      case 1:
         if (op_type == MTS_READ)
            *data = physmem_copy_u8_from_vm(d->vm,paddr ^ 0x03);
         else
            physmem_copy_u8_to_vm(d->vm,paddr ^ 0x03,*data);
         break;

      case 2:
         if (op_type == MTS_READ)
            *data = swap16(physmem_copy_u16_from_vm(d->vm,paddr ^ 0x02));
         else
            physmem_copy_u16_to_vm(d->vm,paddr ^ 0x02,swap16(*data));
         break;

      case 4:
         if (op_type == MTS_READ)
            *data = swap32(physmem_copy_u32_from_vm(d->vm,paddr));
         else
            physmem_copy_u32_to_vm(d->vm,paddr,swap32(*data));
         break;
   }

   return NULL;
}

/* Shutdown an byte-swap device */
void dev_bswap_shutdown(vm_instance_t *vm,struct bswap_data *d)
{
   if (d != NULL) {
      /* Remove the alias, the byte-swapped and the main device */
      dev_remove(vm,&d->dev);

      /* Free the structure itself */
      free(d);
   }
}

/* Initialized a byte-swap device */
int dev_bswap_init(vm_instance_t *vm,char *name,
                   m_uint64_t paddr,m_uint32_t len,
                   m_uint64_t remap_addr)
{
   struct bswap_data *d;

   if (!(d = malloc(sizeof(*d)))) {
      fprintf(stderr,"BSWAP: unable to create device.\n");
      return(-1);
   }

   vm_object_init(&d->vm_obj);
   d->vm = vm;
   d->phys_base = remap_addr;
   d->vm_obj.name = name;
   d->vm_obj.data = d;
   d->vm_obj.shutdown = (vm_shutdown_t)dev_bswap_shutdown;

   dev_init(&d->dev);
   d->dev.name      = name;
   d->dev.phys_addr = paddr;
   d->dev.phys_len  = len;
   d->dev.handler   = dev_bswap_access;
   d->dev.priv_data = d;

   /* Map this device to the VM */
   vm_bind_device(vm,&d->dev);
   vm_object_add(vm,&d->vm_obj);
   return(0);
}