Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/mman.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <sys/time.h>
  8.  
  9. #define MAP_PERM_1 (PROT_WRITE | PROT_READ | PROT_EXEC)
  10. #define MAP_PERM_2 (PROT_WRITE | PROT_READ)
  11.  
  12. #define MAP_FLAGS (MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE)
  13.  
  14. #define PG_LEN 4096
  15. #define align_pg_32(addr) (addr & 0xFFFFF000)
  16. #define num_pg_in_range(start, end) ((end - start + 1) >> 12)
  17.  
  18. inline void __force_pgtbl_alloc(unsigned int start)
  19. {
  20. volatile int *s = (int *) start;
  21. *s = *s;
  22. }
  23.  
  24. int __map_a_page_at(unsigned int start, int whichperm)
  25. {
  26. int perm = whichperm ? MAP_PERM_1 : MAP_PERM_2;
  27.  
  28. if(MAP_FAILED == mmap((void *)start, PG_LEN, perm, MAP_FLAGS, 0, 0)){
  29. fprintf(stderr,
  30. "mmap failed at 0x%x: %s.n",
  31. start, strerror(errno));
  32. return 0;
  33. }
  34.  
  35. return 1;
  36. }
  37.  
  38. int __mlock_page(unsigned int addr)
  39. {
  40. if (mlock((void *)addr, (size_t)PG_LEN) < 0){
  41. fprintf(stderr,
  42. "mlock failed on page: 0x%x: %s.n",
  43. addr, strerror(errno));
  44. return 0;
  45. }
  46.  
  47. return 1;
  48. }
  49.  
  50. void sigint_handler(int p)
  51. {
  52. struct timeval start = {0 ,0}, end = {0, 0}, diff = {0, 0};
  53. gettimeofday(&start, NULL);
  54. munlockall();
  55. gettimeofday(&end, NULL);
  56. timersub(&end, &start, &diff);
  57.  
  58. printf("Munlock'd entire VM in %u secs %u usecs.n",
  59. diff.tv_sec, diff.tv_usec);
  60.  
  61. exit(0);
  62. }
  63.  
  64. int make_vma_map(unsigned int start, unsigned int end)
  65. {
  66. int num_pg = num_pg_in_range(start, end);
  67.  
  68. if (end < start){
  69. fprintf(stderr,
  70. "Bad range: start: 0x%x end: 0x%x.n",
  71. start, end);
  72. return 0;
  73. }
  74.  
  75. for (; num_pg; num_pg --, start += PG_LEN){
  76. if (__map_a_page_at(start, num_pg % 2) && __mlock_page(start))
  77. __force_pgtbl_alloc(start);
  78. else
  79. return 0;
  80. }
  81.  
  82. return 1;
  83. }
  84.  
  85. void display_banner()
  86. {
  87. printf("-----------------------------------------n");
  88. printf("Virtual memory allocator. Ctrl+C to exit.n");
  89. printf("-----------------------------------------n");
  90. }
  91.  
  92. int main()
  93. {
  94. unsigned int vma_start, vma_end, input = 0;
  95. int start_end = 0; // 0: start; 1: end;
  96.  
  97. display_banner();
  98.  
  99. // Bind SIGINT handler.
  100. signal(SIGINT, sigint_handler);
  101.  
  102. while (1){
  103. if (!start_end)
  104. printf("start:t");
  105. else
  106. printf("end:t");
  107.  
  108. scanf("%i", &input);
  109.  
  110. if (start_end){
  111. vma_end = align_pg_32(input);
  112. make_vma_map(vma_start, vma_end);
  113. }
  114. else{
  115. vma_start = align_pg_32(input);
  116. }
  117. start_end = !start_end;
  118. }
  119.  
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement