summaryrefslogtreecommitdiff
path: root/src/libnml/buffer/physmem.cc
blob: 577af2f4b128ab524d714343d456243ad99c22e6 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/********************************************************************
* Description: physmem.cc
*   Provides the member functions for the PHYSMEM_HANDLE class. This
*   is a C++ interface for portable access to physical memory.
*
*   Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: LGPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>		/* memcpy(), memset() */
#include <stdio.h>		// sprintf()
#include <stdlib.h>		/* malloc() */

#ifdef __cplusplus
}
#endif
#include "physmem.hh"		/* class PHYSMEM_HANDLE */
#include "rcs_print.hh"
PHYSMEM_HANDLE::PHYSMEM_HANDLE()
{
    size = 0;
    offset = 0;
    temp_buf = NULL;
    local_address = (LOCAL_ADDRESS_TYPE) NULL;
    physical_address = 0;
    using_bit3 = 0;
    isvalid = 1;
    total_bytes_moved = 0;
    enable_byte_counting = 0;

}

PHYSMEM_HANDLE::PHYSMEM_HANDLE(unsigned long _physical_address,
    long _address_code, long _size)
{
    temp_buf = NULL;
    physical_address = _physical_address;
    size = _size;
    address_code = _address_code;
    local_address = (LOCAL_ADDRESS_TYPE) NULL;
    isvalid = 1;
    offset = 0;
    using_bit3 = 0;

    if (0 == physical_address) {
	local_address = (LOCAL_ADDRESS_TYPE) NULL;
	return;
    }
    local_address = (LOCAL_ADDRESS_TYPE) physical_address;
}

/* Destructor. */
PHYSMEM_HANDLE::~PHYSMEM_HANDLE()
{
}

/* Use an ordinary pointer to access memory. */
void
  PHYSMEM_HANDLE::set_to_ptr(void *_ptr, long _size)
{
    local_address = (LOCAL_ADDRESS_TYPE) _ptr;
    size = _size;
    offset = 0;
}

static int physmem_read_local_address_is_null_error_print_count = 0;

/***********************************************************
* Read _read_size bytes from physical memory to store at _to.
* Returns: 0 for success or -1 for failure.
**********************************************************/
int PHYSMEM_HANDLE::read(void *_to, long _read_size)
{
    if (NULL == _to) {
	rcs_print_error("PHYSMEM_HANDLE::read _to = NULL.\n");
	return (-1);
    }
    /* Check sizes. */
    if (_read_size + offset > size || offset < 0) {
	rcs_print_error
	    ("PHYSMEM_HANDLE: Can't read %ld bytes at offset %ld from buffer of size %ld.\n",
	    _read_size, offset, size);
	return (-1);
    }

    if (enable_byte_counting) {
	total_bytes_moved += _read_size;
    }

    /* If local_address has been initialized use it as an ordinary pointer. */
    if (NULL != local_address) {
	char *from;
	from = ((char *) local_address) + offset;
	if (_read_size == 2) {
	    short *sfrom = (short *) from;
	    short sval;
	    sval = *sfrom;
	    short *sto = (short *) _to;
	    *sto = sval;
	} else {
	    memcpy(_to, from, _read_size);
	}
	return (0);
    }

    /* include platform specific ways of accessing phsical memory here. */
    if (!(physmem_read_local_address_is_null_error_print_count % 100000)) {
	rcs_print_error
	    ("PHYSMEM_HANDLE: Cannot read from physical memory when local address is NULL.\n");
	rcs_print_error("(This error has occured %d times.)\n",
	    physmem_read_local_address_is_null_error_print_count + 1);
    }
    physmem_read_local_address_is_null_error_print_count++;
    return (-1);
}

static int physmem_write_local_address_is_null_error_print_count = 0;

/***********************************************************
* Write _write_size bytes from memory at _from to physical memory.
* Returns: 0 for success or -1 for failure.
**********************************************************/
int PHYSMEM_HANDLE::write(void *_from, long _write_size)
{
    if (NULL == _from) {
	rcs_print_error("PHYSMEM_HANDLE:write _from = NULL\n");
	return -1;
    }

    /* Check sizes. */
    if (_write_size + offset > size || offset < 0) {
	rcs_print_error
	    ("PHYSMEM_HANDLE: Can't write %ld bytes at offset %ld from buffer of size %ld.\n",
	    _write_size, offset, size);
	return (-1);
    }
    if (enable_byte_counting) {
	total_bytes_moved += _write_size;
    }
    /* If local_address has been initialized use it as an ordinary pointer. */
    if (NULL != local_address) {
	char *to;
	to = ((char *) local_address) + offset;
	if (_write_size == 2) {
	    short *sto = (short *) to;
	    short sval = *(short *) _from;
	    *sto = sval;
	} else {
	    memcpy(to, _from, _write_size);
	}
	return (0);
    }

    /* include platform specific ways of accessing phsical memory here. */
    if (!(physmem_write_local_address_is_null_error_print_count % 100000)) {
	rcs_print_error
	    ("PHYSMEM_HANDLE: Cannot write to physical memory when local address is NULL.\n");
	rcs_print_error("(This error has occured %d times.)\n",
	    physmem_write_local_address_is_null_error_print_count + 1);
    }
    physmem_write_local_address_is_null_error_print_count++;
    return (-1);
}

void PHYSMEM_HANDLE::memsetf(long _memset_offset, char _byte,
    long _memset_size)
{
    /* Check sizes. */
    if (_memset_size + _memset_offset > size) {
	return;
    }

    /* If local_address has been initialized use it as an ordinary pointer. */
    if (NULL != local_address) {
	char *temp_addr;
	temp_addr = ((char *) local_address) + _memset_offset;
	memset(temp_addr, _byte, _memset_size);
	return;
    } else {
	/* Since local address is not initialized use temp_buf and write to
	   access the physical memory in an platform specific way. */
	if (NULL == temp_buf) {
	    temp_buf = (char *) malloc(size);
	}
	if (NULL != temp_buf) {
	    if (_memset_size + _memset_offset <= size) {
		memset(temp_buf, _byte, _memset_size);
		unsigned long old_offset;
		old_offset = offset;
		offset = _memset_offset;
		write(temp_buf, _memset_size);
		offset = old_offset;
	    } else {
		memset(temp_buf, _byte, size - _memset_offset);
		unsigned long old_offset;
		old_offset = offset;
		offset = _memset_offset;
		write(temp_buf, size - offset);
		offset = old_offset;
	    }
	}
    }
}

int PHYSMEM_HANDLE::clear_memory()
{
    /* If local_address has been initialized use it as an ordinary pointer. */
    if (NULL != local_address) {
	memset(local_address, 0, size);
	return (0);
    } else {
	/* Since local address is not initialized use temp_buf and write to
	   access the physical memory in an platform specific way. */
	if (NULL == temp_buf) {
	    temp_buf = (char *) malloc(size);
	}
	if (NULL == temp_buf) {
	    return (-1);
	}
	memset(temp_buf, 0, size);
	unsigned long old_offset;
	old_offset = offset;
	offset = 0;
	if (-1 == write(temp_buf, size)) {
	    offset = old_offset;
	    return (-1);
	}
	offset = old_offset;
    }
    return (0);
}

int PHYSMEM_HANDLE::valid()
{
    return isvalid;
}