Memory.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 *) DkVirtualMemoryAlloc(pal_control.user_address.start,
  46. UNIT, 0,
  47. PAL_PROT_READ|PAL_PROT_WRITE);
  48. void * mem4 = (void *) DkVirtualMemoryAlloc(pal_control.user_address.end - UNIT,
  49. UNIT, 0,
  50. PAL_PROT_READ|PAL_PROT_WRITE);
  51. if (mem3 && mem4)
  52. pal_printf("Memory Allocation with Address OK\n");
  53. /* total memory */
  54. pal_printf("Total Memory: %llu\n", pal_control.mem_info.mem_total);
  55. unsigned long before = DkMemoryAvailableQuota();
  56. void * mem5 = (void *) DkVirtualMemoryAlloc(NULL, UNIT * 1000, 0,
  57. PAL_PROT_READ|PAL_PROT_WRITE);
  58. if (mem5) {
  59. unsigned long after = before;
  60. for (int i = 0 ; i < 10000 ; i++) {
  61. for (void * ptr = mem5 ; ptr < mem5 + UNIT * 1000 ; ptr += UNIT)
  62. *(volatile int *) ptr = 0;
  63. unsigned long quota = DkMemoryAvailableQuota();
  64. if (quota < after)
  65. after = quota;
  66. }
  67. pal_printf("Memory Qouta Before Allocation: %ld\n", before);
  68. pal_printf("Memory Qouta After Allocation: %ld\n", after);
  69. /* chance are some pages are evicted, so at least 80% accuracy */
  70. if (before >= after + UNIT * 800)
  71. pal_printf("Get Memory Available Quota OK\n");
  72. }
  73. return 0;
  74. }