Memory.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "pal.h"
  2. #include "pal_debug.h"
  3. #include "api.h"
  4. #define UNIT (pal_control.alloc_align)
  5. static volatile int count = 0;
  6. void handler (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  7. {
  8. count++;
  9. pal_printf("Memory Fault %d\n", count);
  10. while (*(unsigned char *) context->rip != 0x90)
  11. context->rip++;
  12. DkExceptionReturn(event);
  13. }
  14. int main (int argc, char ** argv, char ** envp)
  15. {
  16. volatile int c;
  17. DkSetExceptionHandler(handler, PAL_EVENT_MEMFAULT);
  18. void * mem1 = (void *) DkVirtualMemoryAlloc(NULL, UNIT * 4, 0,
  19. PAL_PROT_READ|PAL_PROT_WRITE);
  20. if (mem1)
  21. pal_printf("Memory Allocation OK\n");
  22. void * mem2 = (void *) DkVirtualMemoryAlloc(NULL, UNIT, 0,
  23. PAL_PROT_READ|PAL_PROT_WRITE);
  24. if (mem2) {
  25. c = count;
  26. *(volatile int *) mem2 = 0;
  27. pal_printf("(int *) %p = %d\n", mem2, *(volatile int *) mem2);
  28. if (c == count)
  29. pal_printf("Memory Allocation Protection (RW) OK\n");
  30. DkVirtualMemoryProtect(mem2, UNIT, PAL_PROT_READ);
  31. c = count;
  32. *(volatile int *) mem2 = 0;
  33. __asm__ volatile("nop");
  34. if (c == count - 1)
  35. pal_printf("Memory Protection (R) OK\n");
  36. DkVirtualMemoryFree(mem2, UNIT);
  37. c = count;
  38. *(volatile int *) mem2 = 0;
  39. __asm__ volatile("nop");
  40. if (c == count - 1)
  41. pal_printf("Memory Deallocation OK\n");
  42. }
  43. void * mem3 = (void *) pal_control.user_address.start;
  44. void * mem4 = (void *) pal_control.user_address.end - UNIT;
  45. if (mem3 >= pal_control.executable_range.start &&
  46. mem3 < pal_control.executable_range.end)
  47. mem3 = (void *) (((PAL_NUM) pal_control.executable_range.end + UNIT - 1) & ~(UNIT - 1));
  48. mem3 = (void *) DkVirtualMemoryAlloc(mem3, UNIT, 0,
  49. PAL_PROT_READ|PAL_PROT_WRITE);
  50. mem4 = (void *) DkVirtualMemoryAlloc(mem4, UNIT, 0,
  51. PAL_PROT_READ|PAL_PROT_WRITE);
  52. if (mem3 && mem4)
  53. pal_printf("Memory Allocation with Address OK\n");
  54. /* Testing total memory */
  55. pal_printf("Total Memory: %lu\n", pal_control.mem_info.mem_total);
  56. /* Testing available memory (must be within valid range) */
  57. PAL_NUM avail = DkMemoryAvailableQuota();
  58. if (avail > 0 && avail < pal_control.mem_info.mem_total)
  59. pal_printf("Get Memory Available Quota OK\n");
  60. return 0;
  61. }