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