um: Ensure that a stub page cannot get unmapped
[deliverable/linux.git] / arch / um / kernel / tlb.c
1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6 #include <linux/mm.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <asm/pgtable.h>
10 #include <asm/tlbflush.h>
11 #include <as-layout.h>
12 #include <mem_user.h>
13 #include <os.h>
14 #include <skas.h>
15
16 struct host_vm_change {
17 struct host_vm_op {
18 enum { NONE, MMAP, MUNMAP, MPROTECT } type;
19 union {
20 struct {
21 unsigned long addr;
22 unsigned long len;
23 unsigned int prot;
24 int fd;
25 __u64 offset;
26 } mmap;
27 struct {
28 unsigned long addr;
29 unsigned long len;
30 } munmap;
31 struct {
32 unsigned long addr;
33 unsigned long len;
34 unsigned int prot;
35 } mprotect;
36 } u;
37 } ops[1];
38 int index;
39 struct mm_id *id;
40 void *data;
41 int force;
42 };
43
44 #define INIT_HVC(mm, force) \
45 ((struct host_vm_change) \
46 { .ops = { { .type = NONE } }, \
47 .id = &mm->context.id, \
48 .data = NULL, \
49 .index = 0, \
50 .force = force })
51
52 static int do_ops(struct host_vm_change *hvc, int end,
53 int finished)
54 {
55 struct host_vm_op *op;
56 int i, ret = 0;
57
58 for (i = 0; i < end && !ret; i++) {
59 op = &hvc->ops[i];
60 switch (op->type) {
61 case MMAP:
62 ret = map(hvc->id, op->u.mmap.addr, op->u.mmap.len,
63 op->u.mmap.prot, op->u.mmap.fd,
64 op->u.mmap.offset, finished, &hvc->data);
65 break;
66 case MUNMAP:
67 ret = unmap(hvc->id, op->u.munmap.addr,
68 op->u.munmap.len, finished, &hvc->data);
69 break;
70 case MPROTECT:
71 ret = protect(hvc->id, op->u.mprotect.addr,
72 op->u.mprotect.len, op->u.mprotect.prot,
73 finished, &hvc->data);
74 break;
75 default:
76 printk(KERN_ERR "Unknown op type %d in do_ops\n",
77 op->type);
78 BUG();
79 break;
80 }
81 }
82
83 return ret;
84 }
85
86 static int add_mmap(unsigned long virt, unsigned long phys, unsigned long len,
87 unsigned int prot, struct host_vm_change *hvc)
88 {
89 __u64 offset;
90 struct host_vm_op *last;
91 int fd, ret = 0;
92
93 fd = phys_mapping(phys, &offset);
94 if (hvc->index != 0) {
95 last = &hvc->ops[hvc->index - 1];
96 if ((last->type == MMAP) &&
97 (last->u.mmap.addr + last->u.mmap.len == virt) &&
98 (last->u.mmap.prot == prot) && (last->u.mmap.fd == fd) &&
99 (last->u.mmap.offset + last->u.mmap.len == offset)) {
100 last->u.mmap.len += len;
101 return 0;
102 }
103 }
104
105 if (hvc->index == ARRAY_SIZE(hvc->ops)) {
106 ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
107 hvc->index = 0;
108 }
109
110 hvc->ops[hvc->index++] = ((struct host_vm_op)
111 { .type = MMAP,
112 .u = { .mmap = { .addr = virt,
113 .len = len,
114 .prot = prot,
115 .fd = fd,
116 .offset = offset }
117 } });
118 return ret;
119 }
120
121 static int add_munmap(unsigned long addr, unsigned long len,
122 struct host_vm_change *hvc)
123 {
124 struct host_vm_op *last;
125 int ret = 0;
126
127 if ((addr >= STUB_START) && (addr < STUB_END))
128 return -EINVAL;
129
130 if (hvc->index != 0) {
131 last = &hvc->ops[hvc->index - 1];
132 if ((last->type == MUNMAP) &&
133 (last->u.munmap.addr + last->u.mmap.len == addr)) {
134 last->u.munmap.len += len;
135 return 0;
136 }
137 }
138
139 if (hvc->index == ARRAY_SIZE(hvc->ops)) {
140 ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
141 hvc->index = 0;
142 }
143
144 hvc->ops[hvc->index++] = ((struct host_vm_op)
145 { .type = MUNMAP,
146 .u = { .munmap = { .addr = addr,
147 .len = len } } });
148 return ret;
149 }
150
151 static int add_mprotect(unsigned long addr, unsigned long len,
152 unsigned int prot, struct host_vm_change *hvc)
153 {
154 struct host_vm_op *last;
155 int ret = 0;
156
157 if (hvc->index != 0) {
158 last = &hvc->ops[hvc->index - 1];
159 if ((last->type == MPROTECT) &&
160 (last->u.mprotect.addr + last->u.mprotect.len == addr) &&
161 (last->u.mprotect.prot == prot)) {
162 last->u.mprotect.len += len;
163 return 0;
164 }
165 }
166
167 if (hvc->index == ARRAY_SIZE(hvc->ops)) {
168 ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
169 hvc->index = 0;
170 }
171
172 hvc->ops[hvc->index++] = ((struct host_vm_op)
173 { .type = MPROTECT,
174 .u = { .mprotect = { .addr = addr,
175 .len = len,
176 .prot = prot } } });
177 return ret;
178 }
179
180 #define ADD_ROUND(n, inc) (((n) + (inc)) & ~((inc) - 1))
181
182 static inline int update_pte_range(pmd_t *pmd, unsigned long addr,
183 unsigned long end,
184 struct host_vm_change *hvc)
185 {
186 pte_t *pte;
187 int r, w, x, prot, ret = 0;
188
189 pte = pte_offset_kernel(pmd, addr);
190 do {
191 if ((addr >= STUB_START) && (addr < STUB_END))
192 continue;
193
194 r = pte_read(*pte);
195 w = pte_write(*pte);
196 x = pte_exec(*pte);
197 if (!pte_young(*pte)) {
198 r = 0;
199 w = 0;
200 } else if (!pte_dirty(*pte))
201 w = 0;
202
203 prot = ((r ? UM_PROT_READ : 0) | (w ? UM_PROT_WRITE : 0) |
204 (x ? UM_PROT_EXEC : 0));
205 if (hvc->force || pte_newpage(*pte)) {
206 if (pte_present(*pte))
207 ret = add_mmap(addr, pte_val(*pte) & PAGE_MASK,
208 PAGE_SIZE, prot, hvc);
209 else
210 ret = add_munmap(addr, PAGE_SIZE, hvc);
211 } else if (pte_newprot(*pte))
212 ret = add_mprotect(addr, PAGE_SIZE, prot, hvc);
213 *pte = pte_mkuptodate(*pte);
214 } while (pte++, addr += PAGE_SIZE, ((addr < end) && !ret));
215 return ret;
216 }
217
218 static inline int update_pmd_range(pud_t *pud, unsigned long addr,
219 unsigned long end,
220 struct host_vm_change *hvc)
221 {
222 pmd_t *pmd;
223 unsigned long next;
224 int ret = 0;
225
226 pmd = pmd_offset(pud, addr);
227 do {
228 next = pmd_addr_end(addr, end);
229 if (!pmd_present(*pmd)) {
230 if (hvc->force || pmd_newpage(*pmd)) {
231 ret = add_munmap(addr, next - addr, hvc);
232 pmd_mkuptodate(*pmd);
233 }
234 }
235 else ret = update_pte_range(pmd, addr, next, hvc);
236 } while (pmd++, addr = next, ((addr < end) && !ret));
237 return ret;
238 }
239
240 static inline int update_pud_range(pgd_t *pgd, unsigned long addr,
241 unsigned long end,
242 struct host_vm_change *hvc)
243 {
244 pud_t *pud;
245 unsigned long next;
246 int ret = 0;
247
248 pud = pud_offset(pgd, addr);
249 do {
250 next = pud_addr_end(addr, end);
251 if (!pud_present(*pud)) {
252 if (hvc->force || pud_newpage(*pud)) {
253 ret = add_munmap(addr, next - addr, hvc);
254 pud_mkuptodate(*pud);
255 }
256 }
257 else ret = update_pmd_range(pud, addr, next, hvc);
258 } while (pud++, addr = next, ((addr < end) && !ret));
259 return ret;
260 }
261
262 void fix_range_common(struct mm_struct *mm, unsigned long start_addr,
263 unsigned long end_addr, int force)
264 {
265 pgd_t *pgd;
266 struct host_vm_change hvc;
267 unsigned long addr = start_addr, next;
268 int ret = 0;
269
270 hvc = INIT_HVC(mm, force);
271 pgd = pgd_offset(mm, addr);
272 do {
273 next = pgd_addr_end(addr, end_addr);
274 if (!pgd_present(*pgd)) {
275 if (force || pgd_newpage(*pgd)) {
276 ret = add_munmap(addr, next - addr, &hvc);
277 pgd_mkuptodate(*pgd);
278 }
279 }
280 else ret = update_pud_range(pgd, addr, next, &hvc);
281 } while (pgd++, addr = next, ((addr < end_addr) && !ret));
282
283 if (!ret)
284 ret = do_ops(&hvc, hvc.index, 1);
285
286 /* This is not an else because ret is modified above */
287 if (ret) {
288 printk(KERN_ERR "fix_range_common: failed, killing current "
289 "process\n");
290 force_sig(SIGKILL, current);
291 }
292 }
293
294 static int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
295 {
296 struct mm_struct *mm;
297 pgd_t *pgd;
298 pud_t *pud;
299 pmd_t *pmd;
300 pte_t *pte;
301 unsigned long addr, last;
302 int updated = 0, err;
303
304 mm = &init_mm;
305 for (addr = start; addr < end;) {
306 pgd = pgd_offset(mm, addr);
307 if (!pgd_present(*pgd)) {
308 last = ADD_ROUND(addr, PGDIR_SIZE);
309 if (last > end)
310 last = end;
311 if (pgd_newpage(*pgd)) {
312 updated = 1;
313 err = os_unmap_memory((void *) addr,
314 last - addr);
315 if (err < 0)
316 panic("munmap failed, errno = %d\n",
317 -err);
318 }
319 addr = last;
320 continue;
321 }
322
323 pud = pud_offset(pgd, addr);
324 if (!pud_present(*pud)) {
325 last = ADD_ROUND(addr, PUD_SIZE);
326 if (last > end)
327 last = end;
328 if (pud_newpage(*pud)) {
329 updated = 1;
330 err = os_unmap_memory((void *) addr,
331 last - addr);
332 if (err < 0)
333 panic("munmap failed, errno = %d\n",
334 -err);
335 }
336 addr = last;
337 continue;
338 }
339
340 pmd = pmd_offset(pud, addr);
341 if (!pmd_present(*pmd)) {
342 last = ADD_ROUND(addr, PMD_SIZE);
343 if (last > end)
344 last = end;
345 if (pmd_newpage(*pmd)) {
346 updated = 1;
347 err = os_unmap_memory((void *) addr,
348 last - addr);
349 if (err < 0)
350 panic("munmap failed, errno = %d\n",
351 -err);
352 }
353 addr = last;
354 continue;
355 }
356
357 pte = pte_offset_kernel(pmd, addr);
358 if (!pte_present(*pte) || pte_newpage(*pte)) {
359 updated = 1;
360 err = os_unmap_memory((void *) addr,
361 PAGE_SIZE);
362 if (err < 0)
363 panic("munmap failed, errno = %d\n",
364 -err);
365 if (pte_present(*pte))
366 map_memory(addr,
367 pte_val(*pte) & PAGE_MASK,
368 PAGE_SIZE, 1, 1, 1);
369 }
370 else if (pte_newprot(*pte)) {
371 updated = 1;
372 os_protect_memory((void *) addr, PAGE_SIZE, 1, 1, 1);
373 }
374 addr += PAGE_SIZE;
375 }
376 return updated;
377 }
378
379 void flush_tlb_page(struct vm_area_struct *vma, unsigned long address)
380 {
381 pgd_t *pgd;
382 pud_t *pud;
383 pmd_t *pmd;
384 pte_t *pte;
385 struct mm_struct *mm = vma->vm_mm;
386 void *flush = NULL;
387 int r, w, x, prot, err = 0;
388 struct mm_id *mm_id;
389
390 address &= PAGE_MASK;
391 pgd = pgd_offset(mm, address);
392 if (!pgd_present(*pgd))
393 goto kill;
394
395 pud = pud_offset(pgd, address);
396 if (!pud_present(*pud))
397 goto kill;
398
399 pmd = pmd_offset(pud, address);
400 if (!pmd_present(*pmd))
401 goto kill;
402
403 pte = pte_offset_kernel(pmd, address);
404
405 r = pte_read(*pte);
406 w = pte_write(*pte);
407 x = pte_exec(*pte);
408 if (!pte_young(*pte)) {
409 r = 0;
410 w = 0;
411 } else if (!pte_dirty(*pte)) {
412 w = 0;
413 }
414
415 mm_id = &mm->context.id;
416 prot = ((r ? UM_PROT_READ : 0) | (w ? UM_PROT_WRITE : 0) |
417 (x ? UM_PROT_EXEC : 0));
418 if (pte_newpage(*pte)) {
419 if (pte_present(*pte)) {
420 unsigned long long offset;
421 int fd;
422
423 fd = phys_mapping(pte_val(*pte) & PAGE_MASK, &offset);
424 err = map(mm_id, address, PAGE_SIZE, prot, fd, offset,
425 1, &flush);
426 }
427 else err = unmap(mm_id, address, PAGE_SIZE, 1, &flush);
428 }
429 else if (pte_newprot(*pte))
430 err = protect(mm_id, address, PAGE_SIZE, prot, 1, &flush);
431
432 if (err)
433 goto kill;
434
435 *pte = pte_mkuptodate(*pte);
436
437 return;
438
439 kill:
440 printk(KERN_ERR "Failed to flush page for address 0x%lx\n", address);
441 force_sig(SIGKILL, current);
442 }
443
444 pgd_t *pgd_offset_proc(struct mm_struct *mm, unsigned long address)
445 {
446 return pgd_offset(mm, address);
447 }
448
449 pud_t *pud_offset_proc(pgd_t *pgd, unsigned long address)
450 {
451 return pud_offset(pgd, address);
452 }
453
454 pmd_t *pmd_offset_proc(pud_t *pud, unsigned long address)
455 {
456 return pmd_offset(pud, address);
457 }
458
459 pte_t *pte_offset_proc(pmd_t *pmd, unsigned long address)
460 {
461 return pte_offset_kernel(pmd, address);
462 }
463
464 pte_t *addr_pte(struct task_struct *task, unsigned long addr)
465 {
466 pgd_t *pgd = pgd_offset(task->mm, addr);
467 pud_t *pud = pud_offset(pgd, addr);
468 pmd_t *pmd = pmd_offset(pud, addr);
469
470 return pte_offset_map(pmd, addr);
471 }
472
473 void flush_tlb_all(void)
474 {
475 flush_tlb_mm(current->mm);
476 }
477
478 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
479 {
480 flush_tlb_kernel_range_common(start, end);
481 }
482
483 void flush_tlb_kernel_vm(void)
484 {
485 flush_tlb_kernel_range_common(start_vm, end_vm);
486 }
487
488 void __flush_tlb_one(unsigned long addr)
489 {
490 flush_tlb_kernel_range_common(addr, addr + PAGE_SIZE);
491 }
492
493 static void fix_range(struct mm_struct *mm, unsigned long start_addr,
494 unsigned long end_addr, int force)
495 {
496 fix_range_common(mm, start_addr, end_addr, force);
497 }
498
499 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
500 unsigned long end)
501 {
502 if (vma->vm_mm == NULL)
503 flush_tlb_kernel_range_common(start, end);
504 else fix_range(vma->vm_mm, start, end, 0);
505 }
506 EXPORT_SYMBOL(flush_tlb_range);
507
508 void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
509 unsigned long end)
510 {
511 /*
512 * Don't bother flushing if this address space is about to be
513 * destroyed.
514 */
515 if (atomic_read(&mm->mm_users) == 0)
516 return;
517
518 fix_range(mm, start, end, 0);
519 }
520
521 void flush_tlb_mm(struct mm_struct *mm)
522 {
523 struct vm_area_struct *vma = mm->mmap;
524
525 while (vma != NULL) {
526 fix_range(mm, vma->vm_start, vma->vm_end, 0);
527 vma = vma->vm_next;
528 }
529 }
530
531 void force_flush_all(void)
532 {
533 struct mm_struct *mm = current->mm;
534 struct vm_area_struct *vma = mm->mmap;
535
536 while (vma != NULL) {
537 fix_range(mm, vma->vm_start, vma->vm_end, 1);
538 vma = vma->vm_next;
539 }
540 }
This page took 0.122967 seconds and 6 git commands to generate.