123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- * mycokernel.c
- */
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/mm.h>
- #include <linux/init.h>
- #include <linux/pid.h>
- #include <linux/pid_namespace.h>
- #include <linux/moduleparam.h>
- #include <asm/atomic.h>
- #include <asm/tlbflush.h>
- #include <asm/tlb.h>
- #include <linux/pagemap.h>
- MODULE_AUTHOR("Max Riechelmann");
- MODULE_DESCRIPTION("TODO:");
- MODULE_LICENSE("GPL");
- // Passing argc: sudo insmod mycokernel.ko mypid=1234
- int mypid1 = 0;
- int mypid2 = 0;
- int addr1 = 0;
- int addr2 = 0;
- module_param(mypid1, int, 0);
- module_param(mypid2, int, 0);
- module_param(addr1, int, 0);
- module_param(addr2, int, 0);
- void print_task(struct task_struct *task)
- {
- struct vm_area_struct *vma;
- int count = 0;
- printk("Task: %d\n", task->pid);
- // Print virtual memory area information
- printk("This mm_struct has %d vmas.\n", task->mm->map_count);
- for (vma = task->mm->mmap ; vma ; vma = vma->vm_next) {
- printk("%d Starts at 0x%lx, Ends at 0x%lx\n",
- ++count, vma->vm_start, vma->vm_end);
- }
- printk("Code Segment start = 0x%lx, end = 0x%lx \n"
- "Data Segment start = 0x%lx, end = 0x%lx \n"
- "Stack Segment start = 0x%lx \n"
- "Heap Segment start = 0x%lx, end = 0x%lx \n"
- "mmap Segment start = 0x%lx \n"
- "number of pagetables %ld \n"
- "total pages mapped %lu \n",
- task->mm->start_code, task->mm->end_code,
- task->mm->start_data, task->mm->end_data,
- task->mm->start_stack,
- task->mm->start_brk, task->mm->brk,
- task->mm->mmap_base,
- atomic_long_read(&task->mm->nr_ptes),
- task->mm->total_vm);
- }
- int init_module(void)
- {
- struct task_struct *task1, *task2;
- int res;
- struct page *page;
- int *my_page_address;
-
- // Find the task by its pid
- task1 = pid_task(find_get_pid(mypid1), PIDTYPE_PID);
- task2 = pid_task(find_get_pid(mypid2), PIDTYPE_PID);
- // Get page from user task
- //down_read(task1->mm->mmap_sem);
- res = get_user_pages(task1, task1->mm, addr1, 1, 1, 1, &page, NULL);
- my_page_address = kmap(page);
- if (res == 1) {
- printk("Translated 0x%x to 0x%p\n", addr1, my_page_address);
- printk("Its value is : %d\n", *my_page_address);
- } else {
- printk("Could not read page!\n");
- }
- kunmap(page);
- SetPageDirty(page);
- page_cache_release(page);
- return 0;
- // Old2
-
- /*
- if (access_ok(VERIFY_WRITE, addr, size ) == 0) {
- printk("Access not ok for 0x%lx\n", addr);
- }
- else
- {
- printk("Access ok for 0x%lx\n", addr);
- }
- if (clear_user((void *)addr, 1) > size) {
- printk("Could not clear 0x%lx\n", addr);
- }
- while (vma2->vm_start < task2->mm->mmap_base) {
- vma2 = vma2->vm_next;
- }
- printk("old vm_area task1: %lx task2: %lx\n", (long unsigned int)vma1, (long unsigned int)vma2);
- vma_temp = vma1;
- //vma1 = vma2;
- //vma2 = vma_temp;
- printk("new vm_area task1: %lx task2: %lx\n", (long unsigned int)vma1, (long unsigned int)vma2);
- // Flush TLB
- tlb_migrate_finish(task1->mm);
- tlb_migrate_finish(task2->mm);
- */
- /*
- // Old
- struct task_struct *task;
- struct vm_area_struct *vma;
- int count = 0;
- // pte = page table entry
- struct page *pte;
- // Find the task by its pid
- task = pid_task(find_get_pid(mypid), PIDTYPE_PID);
- printk("Messing with task %d\n", task->pid);
- // Print virtual memory area information
- printk("This mm_struct has %d vmas.\n", task->mm->map_count);
- for (vma = task->mm->mmap ; vma ; vma = vma->vm_next) {
- printk("%d Starts at 0x%lx, Ends at 0x%lx\n",
- ++count, vma->vm_start, vma->vm_end);
- }
- printk("Code Segment start = 0x%lx, end = 0x%lx \n"
- "Data Segment start = 0x%lx, end = 0x%lx \n"
- "Stack Segment start = 0x%lx \n"
- "Heap Segment start = 0x%lx, end = 0x%lx \n"
- "mmap Segment start = 0x%lx \n"
- "number of pagetables %ld \n"
- "total pages mapped %lu \n",
- task->mm->start_code, task->mm->end_code,
- task->mm->start_data, task->mm->end_data,
- task->mm->start_stack,
- task->mm->start_brk, task->mm->brk,
- task->mm->mmap_base,
- atomic_long_read(&task->mm->nr_ptes),
- task->mm->total_vm);
-
- // Magic
- pte = follow_page(task->mm->mmap, task->mm->mmap->vm_start, FOLL_WRITE);
- printk("PTE: %d", pte->first_page->pages);
-
- //printk("Pointer to page table: 0x%lu\n", task->mm->pgd->pgd);
- //page = (void *)task->mm->pgd->pgd;
- //page_table_entry = (long unsigned int)page & (0x000000000000000 << 49);
- //page_table_entry = (unsigned long int)page;
- //page_table_entry = page_table_entry >> 14;
- //printk("The first PTE is: %lu\n", page_table_entry);
- //printk("The first PTE is: %lx\n", pgd_val(task->mm->pgd->pgd));
- //printk("page_table_entry bits: %lu", sizeof(page_table_entry) * 8);
- //printk("pagetable: %lu", page);
- //printk("Physical address of mmap segment = 0x%lx\n", (long)virt_to_phys((void *)task->mm->mmap_base));
- //printk("mmap ptr = 0x%lx\n", task->mm->mmap_base);
- //ptr = (int *)task->mm->mmap_base;
- //ptr = 0;
- //printk("Physical address of mmap segment = 0x%lx\n", (long)virt_to_phys((void *)task->mm->mmap_base));
- //printk("Virtual address of mmap segment = 0x%lx\n", task->mm->mmap_base);
- //printk("Physical address of mmap segment = 0x%lx\n", (long)phys_to_virt(task->mm->mmap_base));
- return 0;
- */
- }
- void cleanup_module(void)
- {
- printk(KERN_INFO "Done.\n");
- }
|