Memory.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. #include "api.h"
  6. #define UNIT pal_control.alloc_align
  7. static volatile int count = 0;
  8. void handler (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  9. {
  10. count++;
  11. pal_printf("Memory Fault %d\n", count);
  12. while (*(unsigned char *) context->rip != 0x90)
  13. context->rip++;
  14. DkExceptionReturn(event);
  15. }
  16. int main (int argc, char ** argv, char ** envp)
  17. {
  18. volatile int c;
  19. DkSetExceptionHandler(handler, PAL_EVENT_MEMFAULT, 0);
  20. void * mem1 = (void *) DkVirtualMemoryAlloc(NULL, UNIT * 4, 0,
  21. PAL_PROT_READ|PAL_PROT_WRITE);
  22. if (mem1)
  23. pal_printf("Memory Allocation OK\n");
  24. void * mem2 = (void *) DkVirtualMemoryAlloc(NULL, UNIT, 0,
  25. PAL_PROT_READ|PAL_PROT_WRITE);
  26. if (mem2) {
  27. c = count;
  28. *(volatile int *) mem2 = 0;
  29. pal_printf("(int *) %p = %d\n", mem2, *(volatile int *) mem2);
  30. if (c == count)
  31. pal_printf("Memory Allocation Protection (RW) OK\n");
  32. DkVirtualMemoryProtect(mem2, UNIT, PAL_PROT_READ);
  33. c = count;
  34. *(volatile int *) mem2 = 0;
  35. asm volatile("nop");
  36. if (c == count - 1)
  37. pal_printf("Memory Protection (R) OK\n");
  38. DkVirtualMemoryFree(mem2, UNIT);
  39. c = count;
  40. *(volatile int *) mem2 = 0;
  41. asm volatile("nop");
  42. if (c == count - 1)
  43. pal_printf("Memory Deallocation OK\n");
  44. }
  45. void * mem3 = (void *) pal_control.user_address.start;
  46. void * mem4 = (void *) pal_control.user_address.end - UNIT;
  47. if (mem3 >= pal_control.executable_range.start &&
  48. mem3 < pal_control.executable_range.end)
  49. mem3 = (void *) (((PAL_NUM) pal_control.executable_range.end + UNIT - 1) & ~(UNIT - 1));
  50. mem3 = (void *) DkVirtualMemoryAlloc(mem3, UNIT, 0,
  51. PAL_PROT_READ|PAL_PROT_WRITE);
  52. mem4 = (void *) DkVirtualMemoryAlloc(mem4, UNIT, 0,
  53. PAL_PROT_READ|PAL_PROT_WRITE);
  54. if (mem3 && mem4)
  55. pal_printf("Memory Allocation with Address OK\n");
  56. /* total memory */
  57. pal_printf("Total Memory: %llu\n", pal_control.mem_info.mem_total);
  58. unsigned long before = DkMemoryAvailableQuota();
  59. void * mem5 = (void *) DkVirtualMemoryAlloc(NULL, UNIT * 1000, 0,
  60. PAL_PROT_READ|PAL_PROT_WRITE);
  61. if (mem5) {
  62. unsigned long after = before;
  63. for (int i = 0 ; i < 10000 ; i++) {
  64. for (void * ptr = mem5 ; ptr < mem5 + UNIT * 1000 ; ptr += UNIT)
  65. *(volatile int *) ptr = 0;
  66. unsigned long quota = DkMemoryAvailableQuota();
  67. if (quota < after)
  68. after = quota;
  69. }
  70. pal_printf("Memory Qouta Before Allocation: %ld\n", before);
  71. pal_printf("Memory Qouta After Allocation: %ld\n", after);
  72. /* chance are some pages are evicted, so at least 80% accuracy */
  73. if (before >= after + UNIT * 800)
  74. pal_printf("Get Memory Available Quota OK\n");
  75. }
  76. return 0;
  77. }