os161-1.99
 All Data Structures
vm-crash3.c
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #if defined(__mips__)
00006 #define INSN_TYPE unsigned int
00007 #define INVAL_INSN  0x0000003f
00008 #else
00009 #error "Please fix this"
00010 #endif
00011 
00012 void write_to_code(void);
00013 void read_data(void);
00014 void write_data(void);
00015 
00016 void
00017 write_to_code(void)
00018 {
00019   extern int func_150();
00020   /* picking a code page that should no longer be in the TLB */
00021         INSN_TYPE *x = (INSN_TYPE *) func_150;
00022         *x = INVAL_INSN;
00023 
00024         printf("IF THIS PRINTS THE TEST FAILED\n");
00025 }
00026 
00027 extern void     call_all(void);
00028 
00029 #define PAGE_SIZE (4096)
00030 #define PAGES     (128)
00031 #define SIZE      (PAGE_SIZE * PAGES / sizeof(int))
00032 #define NUM_REFS  (2)
00033 
00034 unsigned int array[SIZE];
00035 
00036 void
00037 write_data(void)
00038 {
00039         unsigned int refs = 0;
00040         unsigned int i = 0;
00041 
00042         for (refs = 0; refs < NUM_REFS-1; refs++) {
00043                 for (i=0; i<SIZE; i+= PAGE_SIZE) {
00044                         array[i] = i;
00045                 }
00046         }
00047 }
00048 
00049 void
00050 read_data(void)
00051 {
00052         unsigned int i = 0;
00053 
00054         for (i=0; i<SIZE; i+= PAGE_SIZE) {
00055                 if (array[i] != i) {
00056                         printf("FAILED array[%d] = %u != %d\n", i, array[i], i);
00057                         exit(1);
00058                 }
00059         }
00060 }
00061 
00062 
00063 int
00064 main()
00065 {
00066         unsigned int i = 0;
00067 
00068         for (i=0; i<5; i++) {
00069           call_all();
00070           write_data();
00071           call_all();
00072           read_data();
00073         }
00074 
00075         /* This won't hopefully return becasue it crashes */
00076         write_to_code();
00077         exit(0);
00078 }
 All Data Structures