Memory.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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);
  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. /* Testing total memory */
  57. pal_printf("Total Memory: %llu\n", pal_control.mem_info.mem_total);
  58. /* Testing available memory (must be within valid range) */
  59. PAL_NUM avail = DkMemoryAvailableQuota();
  60. if (avail > 0 && avail < pal_control.mem_info.mem_total)
  61. pal_printf("Get Memory Available Quota OK\n");
  62. return 0;
  63. }