/
mmap.c
330 lines (267 loc) · 6.73 KB
/
mmap.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*****************************************************************************
*
* File: mmap.c
*
* $RCSfile: mmap.c,v $
*
* Copyright (C) 1999 D-TACQ Solutions Ltd
* not to be used without owner's permission
*
* Description:
*
* $Id: mmap.c,v 1.4 2001/10/07 10:01:42 pgm Exp $
* $Log: mmap.c,v $
* Revision 1.4 2001/10/07 10:01:42 pgm
* *** empty log message ***
*
* Revision 1.3 2001/03/23 19:44:46 pgm
* map only opt, debugs added
*
* Revision 1.2 2000/10/05 21:29:55 pgm
* mmap can read,write,fill
*
* Revision 1.1 2000/10/05 20:56:38 pgm
* *** empty log message ***
*
* Revision 1.1 1999/10/22 16:26:49 pgm
* first entry to cvs
*
*
\*****************************************************************************/
#undef __KERNEL__
#include "local.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <popt.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
#define FNAME "/dev/acq32/acq32.1.raw"
#define HELP \
"mmap $Revision: 1.4 $\n" \
"usage: mmap opts\n" \
" -f device name\n" \
" -r read [default]\n" \
" -w write\n" \
" -n nop (just block, holding the mapping)\n" \
" -o offset\n" \
" -l length\n" \
"maps device [ram] space and either\n" \
" reads from ram to stdout\n" \
"-or-writes to ram from stdin\n" \
" -b [-v value] = block fill [default 0xdeadbeef]\n" \
" -T [list of test regs] registers walking bit test\n" \
""
int acq200_debug;
int doRegsTest(volatile unsigned* regs, unsigned* offsets, int nregs)
{
int ir;
int irr;
int pass = 0;
int fail = 0;
dbg(2, "testing ..");
for (ir = 0; ir != nregs; ++ir){
regs[offsets[ir]] = 0;
}
for (ir = 0; ir != nregs; ++ir){
int cursor;
for (cursor = 0x1; cursor; cursor <<= 1){
unsigned wanted, got;
dbg(3, "regs[%d] := %08x", offsets[ir], cursor);
regs[offsets[ir]] = cursor;
for (irr = 0; irr != nregs; ++irr){
got = regs[offsets[irr]];
dbg(3, "regs[%d] = %08x", irr, got);
if (irr == ir){
wanted = cursor;
}else{
wanted = 0;
}
if (wanted != got){
err("regs[%d] wanted 0x%08x got 0x%08x",
offsets[irr], wanted, got);
++fail;
}else{
++pass;
}
}
dbg(3, "\n");
}
regs[offsets[ir]] = cursor;
}
dbg(2, "complete %d fail %d pass", fail, pass);
return fail != 0;
}
int G_fail;
int G_pass;
int please_quit;
int alarm_set;
int regsTest(void* pmem, const char* argv[])
{
/* argv is a list of register offsets */
int ii;
int nargs;
unsigned* offsets;
int iter = 0;
for (nargs = 0; argv[nargs] != NULL; ++nargs){
;
}
info("pmem %p nargs:%d argv[0] \"%s\"", pmem, nargs, argv[0]);
offsets = calloc(nargs, sizeof(unsigned));
for (ii = 0; ii != nargs; ++ii){
offsets[ii] = strtoul(argv[ii], 0, 0);
offsets[ii] /= sizeof(unsigned);
dbg(2, "offsets[%d] (%s) is %d", ii, argv[ii], offsets[ii]);
}
while(!please_quit){
if (doRegsTest((unsigned*)pmem, offsets, nargs)){
G_fail++;
}else{
G_pass++;
}
if ((++iter&0x01ff) == 0 || alarm_set||please_quit){
info("FAIL:%10d PASS:%10d %s",
G_fail, G_pass,
G_fail==0? "*** NO ERRORS ***": ":-( :-( :-(");
alarm_set = 0;
}
}
return G_fail != 0;
}
static void alarm_handler(int signum) {
alarm_set = 1;
}
static void quit_handler(int signum){
alarm_handler(signum);
please_quit = 1;
}
static void install_handlers(void) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = alarm_handler;
struct sigaction saq;
memset(&saq, 0, sizeof(saq));
sa.sa_handler = quit_handler;
if (sigaction(SIGALRM, &sa, NULL)) perror ("sigaction");
if (sigaction(SIGINT, &saq, NULL)) perror ("sigaction");
}
int main( int argc, const char* argv[] )
{
/* WORKTODO ... args handling */
int fd;
void* region;
char* fname = FNAME;
unsigned offset = 0;
unsigned length = 0x100000;
int rc;
unsigned fill_value = 0xdeadbeef;
enum MODE { M_READ, M_WRITE, M_FILL, M_TEST, M_NOP } mode = M_READ;
struct poptOption opt_table[] = {
{ "device", 'f', POPT_ARG_STRING, &fname, 0 },
{ "help", 'h', POPT_ARG_NONE, 0, 'h' },
{ "read", 'r', POPT_ARG_NONE, 0, 'r' },
{ "write", 'w', POPT_ARG_NONE, 0, 'w' },
{ "nop", 'n', POPT_ARG_NONE, 0, 'n' },
{ "fill", 'b', POPT_ARG_NONE, 0, 'f' },
{ "offset", 'o', POPT_ARG_INT, &offset, 'o' },
{ "length", 'l', POPT_ARG_INT, &length, 'l' },
{ "value", 'v', POPT_ARG_INT, &fill_value, 0 },
{ "regstest", 'T', POPT_ARG_NONE, 0, 'T' },
{ "verbose", 'V', POPT_ARG_INT, &acq200_debug, 0 },
{ }
};
poptContext opt_context;
opt_context = poptGetContext( argv[0], argc, argv, opt_table, 0 );
int open_mode = O_RDONLY;
int mmap_mode = PROT_READ;
while ( (rc = poptGetNextOpt( opt_context )) > 0 ){
switch( rc ){
case 'h':
fprintf( stderr, HELP );
return 1;
case 'r':
mode = M_READ;
break;
case 'w':
open_mode = O_RDWR;
mmap_mode = PROT_READ|PROT_WRITE;
mode = M_WRITE;
break;
case 'n':
mode = M_NOP;
break;
case 'f':
open_mode = O_RDWR;
mmap_mode = PROT_READ|PROT_WRITE;
mode = M_FILL;
break;
case 'T':
mode = M_TEST;
break;
}
} // processes all other opts via arg pointers
if ( (fd = open( fname, open_mode)) < 0 ){
fprintf( stderr, "mmap: failed to open device \"%s\" - ", fname );
perror( "" );
return 1;
}
region = mmap( NULL, length, mmap_mode, MAP_SHARED, fd, 0 );
if ( region == (caddr_t)-1 ){
perror( "mmap" );
return 1;
}
switch( mode ){
default:
case M_READ:
// spew to stdout in one big blurt
write( 1, (char*)region+offset, length );
break;
case M_FILL: {
unsigned* praw = (unsigned*)&((char*)region)[offset];
int iwrite;
int imax = length/sizeof(unsigned);
for ( iwrite = 0; iwrite != imax; ++iwrite ){
praw[iwrite] = fill_value;
}
break;
}
case M_WRITE:{
// write to memory - read input until first of EOF or length
// it's slow char at a time stuff but who cares?
int iwrite;
int cc;
char* praw = &((char*)region)[offset];
for ( iwrite=0; (cc = getchar()) != -1 && iwrite != length; ++iwrite ){
praw[iwrite] = cc;
}
break;
}
case M_NOP :
fprintf( stderr,
"mmap: blocking. Hit any key to continue, q to quit\n" );
while ( getchar() != 'q' ){
char aline[128];
FILE* fp = fopen( "/proc/self/maps", "r" );
assert( fp );
while( fgets( aline, sizeof(aline), fp ) != NULL ) {
fputs(aline, stderr);
}
fclose( fp );
}
break;
case M_TEST:
install_handlers();
// alarm(2);
return regsTest(region, poptGetArgs(opt_context));
break;
}
return 0;
}