mycokernel.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * mycokernel.c
  3. */
  4. #include <linux/module.h>
  5. #include <linux/kernel.h>
  6. #include <linux/sched.h>
  7. #include <linux/mm.h>
  8. #include <linux/init.h>
  9. #include <linux/pid.h>
  10. #include <linux/pid_namespace.h>
  11. #include <linux/moduleparam.h>
  12. #include <asm/atomic.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/tlb.h>
  15. #include <linux/pagemap.h>
  16. MODULE_AUTHOR("Max Riechelmann");
  17. MODULE_DESCRIPTION("TODO:");
  18. MODULE_LICENSE("GPL");
  19. // Passing argc: sudo insmod mycokernel.ko mypid=1234
  20. int mypid1 = 0;
  21. int mypid2 = 0;
  22. int addr1 = 0;
  23. int addr2 = 0;
  24. module_param(mypid1, int, 0);
  25. module_param(mypid2, int, 0);
  26. module_param(addr1, int, 0);
  27. module_param(addr2, int, 0);
  28. void print_task(struct task_struct *task)
  29. {
  30. struct vm_area_struct *vma;
  31. int count = 0;
  32. printk("Task: %d\n", task->pid);
  33. // Print virtual memory area information
  34. printk("This mm_struct has %d vmas.\n", task->mm->map_count);
  35. for (vma = task->mm->mmap ; vma ; vma = vma->vm_next) {
  36. printk("%d Starts at 0x%lx, Ends at 0x%lx\n",
  37. ++count, vma->vm_start, vma->vm_end);
  38. }
  39. printk("Code Segment start = 0x%lx, end = 0x%lx \n"
  40. "Data Segment start = 0x%lx, end = 0x%lx \n"
  41. "Stack Segment start = 0x%lx \n"
  42. "Heap Segment start = 0x%lx, end = 0x%lx \n"
  43. "mmap Segment start = 0x%lx \n"
  44. "number of pagetables %ld \n"
  45. "total pages mapped %lu \n",
  46. task->mm->start_code, task->mm->end_code,
  47. task->mm->start_data, task->mm->end_data,
  48. task->mm->start_stack,
  49. task->mm->start_brk, task->mm->brk,
  50. task->mm->mmap_base,
  51. atomic_long_read(&task->mm->nr_ptes),
  52. task->mm->total_vm);
  53. }
  54. int init_module(void)
  55. {
  56. struct task_struct *task1, *task2;
  57. int res;
  58. struct page *page;
  59. int *my_page_address;
  60. // Find the task by its pid
  61. task1 = pid_task(find_get_pid(mypid1), PIDTYPE_PID);
  62. task2 = pid_task(find_get_pid(mypid2), PIDTYPE_PID);
  63. // Get page from user task
  64. //down_read(task1->mm->mmap_sem);
  65. res = get_user_pages(task1, task1->mm, addr1, 1, 1, 1, &page, NULL);
  66. my_page_address = kmap(page);
  67. if (res == 1) {
  68. printk("Translated 0x%x to 0x%p\n", addr1, my_page_address);
  69. printk("Its value is : %d\n", *my_page_address);
  70. } else {
  71. printk("Could not read page!\n");
  72. }
  73. kunmap(page);
  74. SetPageDirty(page);
  75. page_cache_release(page);
  76. return 0;
  77. // Old2
  78. /*
  79. if (access_ok(VERIFY_WRITE, addr, size ) == 0) {
  80. printk("Access not ok for 0x%lx\n", addr);
  81. }
  82. else
  83. {
  84. printk("Access ok for 0x%lx\n", addr);
  85. }
  86. if (clear_user((void *)addr, 1) > size) {
  87. printk("Could not clear 0x%lx\n", addr);
  88. }
  89. while (vma2->vm_start < task2->mm->mmap_base) {
  90. vma2 = vma2->vm_next;
  91. }
  92. printk("old vm_area task1: %lx task2: %lx\n", (long unsigned int)vma1, (long unsigned int)vma2);
  93. vma_temp = vma1;
  94. //vma1 = vma2;
  95. //vma2 = vma_temp;
  96. printk("new vm_area task1: %lx task2: %lx\n", (long unsigned int)vma1, (long unsigned int)vma2);
  97. // Flush TLB
  98. tlb_migrate_finish(task1->mm);
  99. tlb_migrate_finish(task2->mm);
  100. */
  101. /*
  102. // Old
  103. struct task_struct *task;
  104. struct vm_area_struct *vma;
  105. int count = 0;
  106. // pte = page table entry
  107. struct page *pte;
  108. // Find the task by its pid
  109. task = pid_task(find_get_pid(mypid), PIDTYPE_PID);
  110. printk("Messing with task %d\n", task->pid);
  111. // Print virtual memory area information
  112. printk("This mm_struct has %d vmas.\n", task->mm->map_count);
  113. for (vma = task->mm->mmap ; vma ; vma = vma->vm_next) {
  114. printk("%d Starts at 0x%lx, Ends at 0x%lx\n",
  115. ++count, vma->vm_start, vma->vm_end);
  116. }
  117. printk("Code Segment start = 0x%lx, end = 0x%lx \n"
  118. "Data Segment start = 0x%lx, end = 0x%lx \n"
  119. "Stack Segment start = 0x%lx \n"
  120. "Heap Segment start = 0x%lx, end = 0x%lx \n"
  121. "mmap Segment start = 0x%lx \n"
  122. "number of pagetables %ld \n"
  123. "total pages mapped %lu \n",
  124. task->mm->start_code, task->mm->end_code,
  125. task->mm->start_data, task->mm->end_data,
  126. task->mm->start_stack,
  127. task->mm->start_brk, task->mm->brk,
  128. task->mm->mmap_base,
  129. atomic_long_read(&task->mm->nr_ptes),
  130. task->mm->total_vm);
  131. // Magic
  132. pte = follow_page(task->mm->mmap, task->mm->mmap->vm_start, FOLL_WRITE);
  133. printk("PTE: %d", pte->first_page->pages);
  134. //printk("Pointer to page table: 0x%lu\n", task->mm->pgd->pgd);
  135. //page = (void *)task->mm->pgd->pgd;
  136. //page_table_entry = (long unsigned int)page & (0x000000000000000 << 49);
  137. //page_table_entry = (unsigned long int)page;
  138. //page_table_entry = page_table_entry >> 14;
  139. //printk("The first PTE is: %lu\n", page_table_entry);
  140. //printk("The first PTE is: %lx\n", pgd_val(task->mm->pgd->pgd));
  141. //printk("page_table_entry bits: %lu", sizeof(page_table_entry) * 8);
  142. //printk("pagetable: %lu", page);
  143. //printk("Physical address of mmap segment = 0x%lx\n", (long)virt_to_phys((void *)task->mm->mmap_base));
  144. //printk("mmap ptr = 0x%lx\n", task->mm->mmap_base);
  145. //ptr = (int *)task->mm->mmap_base;
  146. //ptr = 0;
  147. //printk("Physical address of mmap segment = 0x%lx\n", (long)virt_to_phys((void *)task->mm->mmap_base));
  148. //printk("Virtual address of mmap segment = 0x%lx\n", task->mm->mmap_base);
  149. //printk("Physical address of mmap segment = 0x%lx\n", (long)phys_to_virt(task->mm->mmap_base));
  150. return 0;
  151. */
  152. }
  153. void cleanup_module(void)
  154. {
  155. printk(KERN_INFO "Done.\n");
  156. }