Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 6 | #include "linux/mm.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 7 | #include "linux/sched.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include "linux/slab.h" |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 9 | #include "asm/pgalloc.h" |
| 10 | #include "asm/pgtable.h" |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 11 | #include "as-layout.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "os.h" |
| 13 | #include "skas.h" |
| 14 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 15 | extern int __syscall_stub_start; |
| 16 | |
| 17 | static int init_stub_pte(struct mm_struct *mm, unsigned long proc, |
| 18 | unsigned long kernel) |
| 19 | { |
| 20 | pgd_t *pgd; |
| 21 | pud_t *pud; |
| 22 | pmd_t *pmd; |
| 23 | pte_t *pte; |
| 24 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 25 | pgd = pgd_offset(mm, proc); |
| 26 | pud = pud_alloc(mm, pgd, proc); |
| 27 | if (!pud) |
| 28 | goto out; |
| 29 | |
| 30 | pmd = pmd_alloc(mm, pud, proc); |
| 31 | if (!pmd) |
| 32 | goto out_pmd; |
| 33 | |
| 34 | pte = pte_alloc_map(mm, pmd, proc); |
| 35 | if (!pte) |
| 36 | goto out_pte; |
| 37 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 38 | *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT)); |
Paolo 'Blaisorblade' Giarrusso | 21c935e | 2006-10-11 01:21:32 -0700 | [diff] [blame] | 39 | *pte = pte_mkread(*pte); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 40 | return 0; |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 41 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 42 | out_pte: |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 43 | pmd_free(mm, pmd); |
Roel Kluin | 4d04c70 | 2009-09-21 17:04:11 -0700 | [diff] [blame] | 44 | out_pmd: |
| 45 | pud_free(mm, pud); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 46 | out: |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 47 | return -ENOMEM; |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 50 | int init_new_context(struct task_struct *task, struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Jeff Dike | 6c738ff | 2007-10-16 01:27:06 -0700 | [diff] [blame] | 52 | struct mm_context *from_mm = NULL; |
| 53 | struct mm_context *to_mm = &mm->context; |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 54 | unsigned long stack = 0; |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 55 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 57 | if (skas_needs_stub) { |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 58 | stack = get_zeroed_page(GFP_KERNEL); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 59 | if (stack == 0) |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 60 | goto out; |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 61 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 62 | |
| 63 | to_mm->id.stack = stack; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 64 | if (current->mm != NULL && current->mm != &init_mm) |
Jeff Dike | 6c738ff | 2007-10-16 01:27:06 -0700 | [diff] [blame] | 65 | from_mm = ¤t->mm->context; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 66 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 67 | if (proc_mm) { |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 68 | ret = new_mm(stack); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 69 | if (ret < 0) { |
| 70 | printk(KERN_ERR "init_new_context_skas - " |
| 71 | "new_mm failed, errno = %d\n", ret); |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 72 | goto out_free; |
| 73 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 74 | to_mm->id.u.mm_fd = ret; |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 75 | } |
| 76 | else { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 77 | if (from_mm) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 78 | to_mm->id.u.pid = copy_context_skas0(stack, |
| 79 | from_mm->id.u.pid); |
| 80 | else to_mm->id.u.pid = start_userspace(stack); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 81 | |
| 82 | if (to_mm->id.u.pid < 0) { |
| 83 | ret = to_mm->id.u.pid; |
| 84 | goto out_free; |
| 85 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | ret = init_new_ldt(to_mm, from_mm); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 89 | if (ret < 0) { |
| 90 | printk(KERN_ERR "init_new_context_skas - init_ldt" |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 91 | " failed, errno = %d\n", ret); |
| 92 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Dike | ac2a659 | 2008-02-08 04:22:11 -0800 | [diff] [blame] | 95 | to_mm->stub_pages = NULL; |
| 96 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 97 | return 0; |
| 98 | |
| 99 | out_free: |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 100 | if (to_mm->id.stack != 0) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 101 | free_page(to_mm->id.stack); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 102 | out: |
| 103 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Dike | 3963333 | 2008-02-04 22:31:01 -0800 | [diff] [blame] | 106 | void arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) |
| 107 | { |
| 108 | struct page **pages; |
| 109 | int err, ret; |
| 110 | |
| 111 | if (!skas_needs_stub) |
| 112 | return; |
| 113 | |
| 114 | ret = init_stub_pte(mm, STUB_CODE, |
| 115 | (unsigned long) &__syscall_stub_start); |
| 116 | if (ret) |
| 117 | goto out; |
| 118 | |
| 119 | ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack); |
| 120 | if (ret) |
| 121 | goto out; |
| 122 | |
| 123 | pages = kmalloc(2 * sizeof(struct page *), GFP_KERNEL); |
| 124 | if (pages == NULL) { |
| 125 | printk(KERN_ERR "arch_dup_mmap failed to allocate 2 page " |
| 126 | "pointers\n"); |
| 127 | goto out; |
| 128 | } |
| 129 | |
| 130 | pages[0] = virt_to_page(&__syscall_stub_start); |
| 131 | pages[1] = virt_to_page(mm->context.id.stack); |
Jeff Dike | ac2a659 | 2008-02-08 04:22:11 -0800 | [diff] [blame] | 132 | mm->context.stub_pages = pages; |
Jeff Dike | 3963333 | 2008-02-04 22:31:01 -0800 | [diff] [blame] | 133 | |
| 134 | /* dup_mmap already holds mmap_sem */ |
| 135 | err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START, |
| 136 | VM_READ | VM_MAYREAD | VM_EXEC | |
| 137 | VM_MAYEXEC | VM_DONTCOPY, pages); |
| 138 | if (err) { |
| 139 | printk(KERN_ERR "install_special_mapping returned %d\n", err); |
| 140 | goto out_free; |
| 141 | } |
| 142 | return; |
| 143 | |
| 144 | out_free: |
| 145 | kfree(pages); |
| 146 | out: |
| 147 | force_sigsegv(SIGSEGV, current); |
| 148 | } |
| 149 | |
| 150 | void arch_exit_mmap(struct mm_struct *mm) |
| 151 | { |
| 152 | pte_t *pte; |
| 153 | |
Jeff Dike | ac2a659 | 2008-02-08 04:22:11 -0800 | [diff] [blame] | 154 | if (mm->context.stub_pages != NULL) |
| 155 | kfree(mm->context.stub_pages); |
Jeff Dike | 3963333 | 2008-02-04 22:31:01 -0800 | [diff] [blame] | 156 | pte = virt_to_pte(mm, STUB_CODE); |
| 157 | if (pte != NULL) |
| 158 | pte_clear(mm, STUB_CODE, pte); |
| 159 | |
| 160 | pte = virt_to_pte(mm, STUB_DATA); |
| 161 | if (pte == NULL) |
| 162 | return; |
| 163 | |
| 164 | pte_clear(mm, STUB_DATA, pte); |
| 165 | } |
| 166 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 167 | void destroy_context(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Jeff Dike | 6c738ff | 2007-10-16 01:27:06 -0700 | [diff] [blame] | 169 | struct mm_context *mmu = &mm->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 171 | if (proc_mm) |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 172 | os_close_file(mmu->id.u.mm_fd); |
Jeff Dike | 00a905e | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 173 | else { |
| 174 | /* |
| 175 | * If init_new_context wasn't called, this will be |
| 176 | * zero, resulting in a kill(0), which will result in the |
| 177 | * whole UML suddenly dying. Also, cover negative and |
| 178 | * 1 cases, since they shouldn't happen either. |
| 179 | */ |
| 180 | if (mmu->id.u.pid < 2) { |
| 181 | printk(KERN_ERR "corrupt mm_context - pid = %d\n", |
| 182 | mmu->id.u.pid); |
| 183 | return; |
| 184 | } |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 185 | os_kill_ptraced_process(mmu->id.u.pid, 1); |
Jeff Dike | 00a905e | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 186 | } |
Bodo Stroesser | 8b51304 | 2005-09-03 15:57:49 -0700 | [diff] [blame] | 187 | |
Jeff Dike | 3963333 | 2008-02-04 22:31:01 -0800 | [diff] [blame] | 188 | if (skas_needs_stub) |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 189 | free_page(mmu->id.stack); |
Jeff Dike | 28078e8 | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 190 | |
| 191 | free_ldt(mmu); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 192 | } |