File: mtest.c

package info (click to toggle)
hwtools 0.3-4
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 992 kB
  • ctags: 905
  • sloc: ansic: 6,699; tcl: 2,140; asm: 683; makefile: 271; sh: 227; csh: 42
file content (668 lines) | stat: -rw-r--r-- 14,166 bytes parent folder | download
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/* mtest.c - MemTest-86 */

/* Copyright 1996,  Chris Brady
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without fee
 * is granted provided that the above copyright notice appears in all copies.
 * It is provided "as is" without express or implied warranty.
 */

#include <linux/tty.h>
#include <linux/sched.h>
#include <asm/io.h>
#include "mtest.h"

#ifndef UNIXTEST
unsigned short memsz;
#else
unsigned memsz;
#endif

void cprint(int y,int x,char *s);
void hprint(int y,int x,unsigned long val);
void dprint(int y,int x,unsigned long val,int len);
void check(long p1,long p2);
void error(long* adr,long good,long bad);
void do_spin(void);
void init();
void inter();

extern long idt_descr;
extern long trap_regs[];

extern __inline__ void cache_off()
{
        __asm__("push %eax\n\t"
		"movl %cr0,%eax\n\t"
                "orl $0x60000000,%eax\n\t"
                "movl %eax,%cr0\n\t"
		".byte 0x0f,0x09\n\t"	/* Invalidate and flush cache */
		"pop  %eax\n\t");
}
extern __inline__ void cache_on()
{
        __asm__("push %eax\n\t"
		"movl %cr0,%eax\n\t"
                "andl $0x9fffffff,%eax\n\t"
                "movl %eax,%cr0\n\t"
		"pop  %eax\n\t");
}
extern __inline__ void reboot()
{
        __asm__("movl %cr0,%eax\n\t"
       		"andl  $0x00000011,%eax\n\t"
       		"orl   $0x60000000,%eax\n\t"
       		"movl  %eax,%cr0\n\t"
       		"movl  %eax,%cr3\n\t"
		"cli\n\t"
		".byte 0x0f,0x09\n\t"	/* Invalidate and flush cache */
       		"andb  $0x10,%al\n\t"
		"movl  %eax,%cr0\n\t"
		"ljmp  $0xffff,$0x0000\n\t");
}

struct mmap {
	volatile long *start;
	volatile long *end;
};

/*
 * Define common variables for normal and relocated test locations
 */
struct vars {
	int firsttime;
	int pass;
	long *eadr;
	long exor;
	int msg_line;
	int ecount;
	int msegs;
	int cache_flag;
	int ref_flag;
	int scroll_start;
};

char spin[] = {'/', '-', '\\', '|'};

char *codes[] = {
	"  Divide",
	"   Debug",
	"     NMI",
	"  Brkpnt",
	"Overflow",
	"   Bound",
	"  Inv_Op",
	" No_Math",
	"Double_Fault"
	"Seg_Over",
	" Inv_TSS",
	"  Seg_NP",
	"Stack_Fault",
	"Gen_Prot",
	"Page_Fault",
	"   Resvd",
	"     FPE",
	"Alignment"
	" Mch_Chk"
};


#ifdef UNIXTEST
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#undef START_ADR
#define START_ADR start_adr
#undef SCREEN_ADR
#define SCREEN_ADR screen_adr
#define MEMSZ 4096 /* test area size Kbytes */
char start_adr[MEMSZ*1024];
unsigned long screen_adr;
void do_test(void);
int main ()
{
  int fd;
  memsz = (unsigned)start_adr/1024 + MEMSZ - 1024;
  fd = open("/dev/mem",O_RDWR);
  screen_adr = (unsigned)mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0xb8000);
  close(fd);
  do_test();
}
#endif


int i = 0;
unsigned long p1 = 0, p2 = 0, p0 = 0;
volatile long *p = 0;
volatile long *pd = 0;
volatile char *pp = 0;
unsigned long m_lim = 0;
char buf[18];
int segs = 0;
int s = 0;
struct vars *v = 0;
struct mmap map[10];

asmlinkage void do_test(void)
{
	/*
	 * Since all phases of the test have the same entry point we use
	 * the address of a static variable (i) to know if the test code has
	 * been relocated.
	 */
	if (&i < (int *)RELOBASE) {
		/* Not relocated */
		/* Set stack and idt */
		__asm__ __volatile__ ("mov %0,%%esp" : : "a" (TESTADR));
		__asm__ __volatile__ ("lidt %0" : : "m" (idt_descr));

		/* Set pointer to common variable area */
		v = (struct vars *)(TESTADR+TSTSIZE-0x400);

		/* If first time, initialize test */
		if (v->firsttime == 0) {
			init();
			v->firsttime = 1;
		}

		/* Update display of memory segments being tested */
		cprint(1, 0, "          ");
		for (i=0; i<v->msegs; i++) {
			dprint(2+i, 9, (long)map[i].start/1024, 5);
			cprint(2+i, 14, "k - ");
			dprint(2+i, 18, (long)map[i].end/1024, 7);
			cprint(2+i, 25, "k");
		}
		segs = v->msegs;
	} else {
		/* Relocated */
		/* Set stack and idt */
		__asm__ __volatile__ ("mov %0,%%esp" : : "a" (TESTADR+RELOBASE));
		__asm__ __volatile__ ("lidt %0" : : "m" (idt_descr));

		/* Set pointer to common variable area */
		v = (struct vars *)(RELOBASE+TESTADR+TSTSIZE-0x400);

		/*
		 * If this is the first time relocated, find the lowest
		 * address that can be tested without causing an exception.
		 * The trap handler (inter) setups up the test addresses
		 * for us when an exeption occurs.
		 */
		if (map[0].end == 0) {
			for (p=(long *)TESTADR-0x100; p;) {
				*(--p) = 0;
				*p = -1;
			}
			map[0].start = (volatile long *)0;
			map[0].end = (volatile long *)SKIP_START;
		}

		/* Update display of memory segments being tested */
		segs = 1;
		cprint(1, 0, "Relocated");
		dprint(2, 9, (long)map[0].start, 5);
		cprint(2, 14, "  - ");
		dprint(2, 18, (long)map[0].end/1024, 7);
		for (i=1; i<v->msegs; i++) {
			cprint(2+i, 10, "                 ");
		}
	}

	/*
	 * Use a 4 bit wide walking ones pattern and it's complement.
	 * This will check out 4 bit wide chips.  This should be
	 * changed if chips more than 4 bits wide become available.
	 */
	p0 = 8;
	for (i=0; i<5; i++, p0=p0>>1) {
		p1 = p0 | (p0<<4) | (p0<<8) | (p0<<12) | (p0<<16) |
			(p0<<20) | (p0<<24) | (p0<<28);
		p2 = ~p1;
		check(p1,p2);
	
		/*
		 * Switch patterns
		 */
		p2 = p1;
		p1 = ~p2;
		check(p1,p2);
	}

	/*
	 * End of a test phase so relocate the test
	 * Only if there is more than 1 meg of memory
	 */
	if (&i < (int *)RELOBASE && map[v->msegs-1].end > (long *)0x110000) {
		/* Copy test code to high memory */
		p = (long *)TESTADR;
		pd = (long *)(RELOBASE+TESTADR);
		for (i=0; i<(TSTSIZE)/4; i++) {
			*pd = *p;
			p++;
			pd++;
		}
		/* Jump to relocated code */
		p = (long *)(RELOBASE+TESTADR+MAINSZ);
		goto *p;
	} else {
		/*
		 * Since the code is relocated to high memory, this is
		 * the end of a pass.  Adjust cache and refresh settings
		 * for the next pass.
		 */
#ifdef CACHE
		/*
		 * Alternate cache on and off for each pass
		 */
		if (v->cache_flag == 0) {
			cache_off();
			v->cache_flag++;
			cprint(2, 73, "OFF");
		} else {
			cache_on();
			v->cache_flag = 0;
			cprint(2, 73, " ON");
#endif
#ifdef REFRESH
			/*
			 * For every 2 passes alternate with short and long
			 * refresh rates
			 */
			if (v->ref_flag == 0) {
				v->ref_flag++;
				/* set refresh to 150ms */
				outb(0x74, 0x43);
				outb(0xb4, 0x41);
				outb(0x00, 0x41);
				cprint(2, 43, "Extended (150ms)");
			} else {
				v->ref_flag = 0;
				/* set refresh to 15ms */
				outb(0x74, 0x43);
				outb(0x12, 0x41);
				outb(0x00, 0x41);
				cprint(2, 43, "  Normal (15ms) ");
			}
#endif
#ifdef CACHE
		}
#endif
		dprint(0, 71, ++(v->pass), 5);

		/* If relocated, move back to low memory */
		if (&i > (int *)RELOBASE) {
			/* Copy test code to low memory */
			p = (long *)(RELOBASE+TESTADR);
			pd = (long *)TESTADR;
			for (i=0; i<(TSTSIZE)/4; i++) {
				*pd = *p;
				p++;
				pd++;
			}
			/* Jump to test start */
			p = (long *)TESTADR;
			goto *p;
		} else {
			do_test();
		}
	}
}

/*
 * Initialize test, setup screen and find out how much memory there is.
 */
void init()
{
	for(i=0, pp=(char *)(SCREEN_ADR+1); i<18; i++, pp+=2) {
		*pp = 0x017;
	}
	cprint(0, 0, " Memtest-86 v1.3  ");
	cprint(2, 0, "Testing: ");

	/*
	 * Since all address bits are not decoded, the search for memory
	 * must be limited.  The max address is found by checking for
	 * memory wrap from 1mb to 4gb.
	 */
	map[0].start = (long *)0x1234569;
	p1 = (long)&map[0].start;
	m_lim = 0xffffffff; 
	for (p2 = 0x100000; p2; p2 <<= 1) {  
		p = (long *)(p1 + p2);
		if (*p == 0x1234569) {
			m_lim = --p2;
			break;
		}
	}

	cprint(0, 34, "Max_Mem:");
	dprint(0, 43, m_lim/1024, 7);
	cprint(0, 50, "k");
	
	/*
	 * Find all segments of RAM
	 * We test for DRAM by reading a location and then writing  the
	 * complement. If any bits change then we assume that it is DRAM.
	 * To save time we only do this check every 1024 bytes.
	 */
	p = (long *)(START_ADR + 0x400);
	i = 0;
	map[i].start = p;
	dprint(2, 9, (long)p/1024, 5);
	cprint(2, 14, "k - ");

	/*
	 * Limit search for memory to m_lim and make sure we don't 
	 * overflow the 32 bit size of p.
	 */
	while ((long)p < m_lim && (long)p > START_ADR) {
		/*
		 * Skip over reserved memory
		 */
		if ((long)p < SKIP_END && (long)p >= SKIP_START) {
			map[i].end = (long *)SKIP_START;
			dprint(2+i, 18, (long)p/1024, 7);
			cprint(2+i, 25, "k");
			p = (long *)SKIP_END;
			i++;
			map[i].start = 0;
			goto fstart;
		}
		p1 = *p;
		*p = ~p1;
		if (*p == p1) {
			/*
			 * ROM or nothing at this address, record end addrs
			 */
			map[i].end = p;
			dprint(2 + i, 18, (long)p/1024, 7);
			cprint(2 + i, 25, "k");
			i++;
			map[i].start = 0;
fstart:
			while ((long)p < m_lim && (long)p > START_ADR) {
				/*
				 * Skip over video memory
				 */
				if ((long)p < SKIP_END &&
					(long)p >= SKIP_START) {
					p = (long *)SKIP_END;
				}
				p1 = *p;
				*p =  ~p1;
				if (*p != p1) {
					/*
					 * More RAM, record start addrs
					 */
					map[i].start = p;
					dprint(2 + i, 9, (long)p/1024, 5);
					cprint(2 + i, 14, "k - ");
					break;
				}
				p += 0x400;
			}
		}
		p += 0x400;
	}

	/*
	 * If there is ram right up to the memory limit this will record
	 * the last address.
	 */
	if (map[i].start) {
		map[i].end = (long *)m_lim;
		dprint(2 + i, 18, (long)p/1024, 7);
		cprint(2 + i, 25, "k");
		i++;
		map[i].start = 0;
	}

	v->msegs = i;
	v->msg_line = v->msegs + 2;
	v->scroll_start = v->msg_line * 160;
	cprint(1, 34, "Pattern:");
	cprint(2, 34, "Refresh:");
	cprint(2, 43, " Default");
	cprint(0, 63, "  Pass:");
	dprint(0, 71, v->pass, 5);
	cprint(1, 63, "Errors:");
	dprint(1, 71, 0, 5);
	cprint(2, 63, " Cache:");
	cprint(2, 73, " ON");
}

/*
 * Scroll the error message area of the screen
 * Starts at line 3 + v->msegs and ends at line 25
 */
void scroll() {
	int i, j;
	char *s;
        for (i=4+v->msegs; i<=25; i++) {
		s = (char *)(SCREEN_ADR + (i * 160));
                for (j=0; j<160; j++, s++) {
			*(s-160) = *s;
		}
	}
}

/*
 * Test all of memory using a "moving inversions" algorithm using the
 * pattern in p1 and it's complement in p2.
 */
void check(long p1, long p2)
{
	register int i, j;
	volatile register long *p;
	volatile long *start,*end;
	long bad;

        hprint(1, 43, p1);

	/*
	 * Initialize memory with the initial pattern.
	 */
	for (j=0; j<segs; j++) {
		start = map[j].start;
		end = map[j].end;
		for (p = start; p < end; p++) {
			*p = p1;
		}
	}
	do_spin();

	/*
	 * Do moving inversions test. Check for initial pattern and then
	 * write the complement for each memory location. Test from bottom
	 * up and then from the top down.
	 */
	for (i=0; i<3; i++) {
		for (j=0; j<segs; j++) {
			start = map[j].start;
			end = map[j].end;
			for (p = start; p < end; p++) {
				if ((bad=*p) != p1) {
					error((long*)p, p1, bad);
				}
				*p = p2;
			}
		}
		do_spin();
		for (j=segs-1; j>=0; j--) {
			start = map[j].start;
			end = map[j].end;
			p = --end;
			do {
				if ((bad=*p) != p2) {
					error((long*)p, p2, bad);
				}
				*p = p1;
			} while (p-- > start);
		}
		do_spin();
	}
}

/*
 * Display data error message. Don't display repeat duplicate errors.
 */
void error(long *adr, long good, long bad)
{
	long xor;

	xor = good ^ bad;

	/* Don't display duplicate errors */
	if (adr == v->eadr && xor == v->exor) {
		dprint(1, 71, ++(v->ecount), 5);
		return;
	}

        /*
         * Advance line for error message
         * If at the bottom of the screen, scroll
         */
        if (v->msg_line < 25) {
                v->msg_line++;
        } else {
                scroll();
        }

	cprint(v->msg_line, 0, "Error - Addrs:");
	hprint(v->msg_line, 14, (long)adr);
	cprint(v->msg_line, 24, "Good:");
	hprint(v->msg_line, 29, good);
	cprint(v->msg_line, 39, "Bad:");
	hprint(v->msg_line, 43, bad);
	cprint(v->msg_line, 53, "Xor:");
	hprint(v->msg_line, 57, xor);
	v->eadr = adr;
	v->exor = xor;
	dprint(1, 71, ++(v->ecount), 5);
}

/*
 * Print characters on screen
 */
void cprint(int y, int x, char *text)
{
	register int i;
	char *dptr;

	dptr = (char *)(SCREEN_ADR + (160*y) + (2*x));
	for (i=0; i < text[i]; i++) {
		*dptr = text[i];
		dptr += 2;
	}
}

/*
 * Print a decimal number on screen
 */
void dprint(int y, int x, unsigned long val, int len)
{
        unsigned long j, k;
        int i, flag=0;

        for(i=0, j=1; i<len-1; i++) {
                j *= 10;
        }
        for (i=0; j>0; j/=10) {
                k = val/j;
                if (k > 9) {
                        j *= 100;
                        continue;
                }
                if (flag || k || j == 1) {
                        buf[i++] = k + '0';
			flag++;
                } else {
			buf[i++] = ' ';
		}
                val -= k * j;
        }
        buf[i] = 0;
	cprint(y,x,buf);
}

/*
 * Print a hex number on screen
 */
void hprint(int y,int x, unsigned long val)
{
	unsigned long j;
	int i, idx, flag = 0;

        for (i=0, idx=0; i<8; i++) {
                j = val >> (28 - (4 * i));
		j &= 0xf;
		if (j < 10) {
			if (flag || j || i == 7) {
		                buf[idx++] = j + '0';
				flag++;
			} else {
				buf[idx++] = ' ';
			}
		} else {
			buf[idx++] = j + 'a' - 10;
			flag++;
		}
        }
        buf[idx] = 0;
	cprint(y,x,buf);
}
	
/*
 * Display a spinning pattern to show progress
 */
void do_spin(void)
{
	char *dptr = (char *)(SCREEN_ADR+0x26);
	unsigned char c;

	*dptr = spin[s]&0x7f;
	dptr++;
	*dptr = 0xf;
	if (++s > 3) {
		s = 0;
	}

	/*
	 * If the "DEL" key is pressed, bail out.
	 */
        c = __inbc(0x60);
	c &= 0x7f;
	if (c == 0x53) {
		*((unsigned short *)0x472) = 0x1234;
		reboot();
	}
}

void inter()
{
	
	if (map[0].start == 0) {
		map[0].start = (volatile long *)++p;
		map[0].end = (volatile long *)SKIP_START;
		do_test();
	}
	cprint(19, 0, "Unexpected Interrupt - Halting");
	cprint(20, 0, "    Type: ");
	cprint(21, 0, "      PC: ");
	cprint(22, 0, "   Eflag: ");
	cprint(23, 0, "      CS: ");
	cprint(24, 0, "Err Code: ");
	if (trap_regs[0] <= 18) {
		cprint(20, 10, codes[trap_regs[0]-1]);
	} else {
		hprint(20, 10, trap_regs[0]);
	}
	hprint(21, 10, trap_regs[3]);
	hprint(22, 10, trap_regs[1]);
	hprint(23, 10, trap_regs[2]);
	hprint(24, 10, trap_regs[4]);
	while(1);
}