vm-crash3.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #if defined(__mips__)
  5. #define INSN_TYPE unsigned int
  6. #define INVAL_INSN 0x0000003f
  7. #else
  8. #error "Please fix this"
  9. #endif
  10. void write_to_code(void);
  11. void read_data(void);
  12. void write_data(void);
  13. void
  14. write_to_code(void)
  15. {
  16. extern int func_150();
  17. /* picking a code page that should no longer be in the TLB */
  18. INSN_TYPE *x = (INSN_TYPE *) func_150;
  19. *x = INVAL_INSN;
  20. printf("IF THIS PRINTS THE TEST FAILED\n");
  21. }
  22. extern void call_all(void);
  23. #define PAGE_SIZE (4096)
  24. #define PAGES (128)
  25. #define SIZE (PAGE_SIZE * PAGES / sizeof(int))
  26. #define NUM_REFS (2)
  27. unsigned int array[SIZE];
  28. void
  29. write_data(void)
  30. {
  31. unsigned int refs = 0;
  32. unsigned int i = 0;
  33. for (refs = 0; refs < NUM_REFS-1; refs++) {
  34. for (i=0; i<SIZE; i+= PAGE_SIZE) {
  35. array[i] = i;
  36. }
  37. }
  38. }
  39. void
  40. read_data(void)
  41. {
  42. unsigned int i = 0;
  43. for (i=0; i<SIZE; i+= PAGE_SIZE) {
  44. if (array[i] != i) {
  45. printf("FAILED array[%d] = %u != %d\n", i, array[i], i);
  46. exit(1);
  47. }
  48. }
  49. }
  50. int
  51. main()
  52. {
  53. unsigned int i = 0;
  54. for (i=0; i<5; i++) {
  55. call_all();
  56. write_data();
  57. call_all();
  58. read_data();
  59. }
  60. /* This won't hopefully return becasue it crashes */
  61. write_to_code();
  62. exit(0);
  63. }