1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
7 #include <linux/module.h>
8 #include <linux/sched/signal.h>
10 #include <asm/tlbflush.h>
11 #include <as-layout.h>
15 #include <kern_util.h>
17 struct host_vm_change {
19 enum { NONE, MMAP, MUNMAP, MPROTECT } type;
46 #define INIT_HVC(mm, force, userspace) \
47 ((struct host_vm_change) \
48 { .ops = { { .type = NONE } }, \
51 .userspace = userspace, \
55 static void report_enomem(void)
57 printk(KERN_ERR "UML ran out of memory on the host side! "
58 "This can happen due to a memory limitation or "
59 "vm.max_map_count has been reached.\n");
62 static int do_ops(struct host_vm_change *hvc, int end,
65 struct host_vm_op *op;
68 for (i = 0; i < end && !ret; i++) {
73 ret = map(&hvc->mm->context.id, op->u.mmap.addr,
74 op->u.mmap.len, op->u.mmap.prot,
76 op->u.mmap.offset, finished,
79 map_memory(op->u.mmap.addr, op->u.mmap.offset,
80 op->u.mmap.len, 1, 1, 1);
84 ret = unmap(&hvc->mm->context.id,
86 op->u.munmap.len, finished,
89 ret = os_unmap_memory(
90 (void *) op->u.munmap.addr,
96 ret = protect(&hvc->mm->context.id,
100 finished, &hvc->data);
102 ret = os_protect_memory(
103 (void *) op->u.mprotect.addr,
108 printk(KERN_ERR "Unknown op type %d in do_ops\n",
121 static int add_mmap(unsigned long virt, unsigned long phys, unsigned long len,
122 unsigned int prot, struct host_vm_change *hvc)
125 struct host_vm_op *last;
126 int fd = -1, ret = 0;
128 if (virt + len > STUB_START && virt < STUB_END)
132 fd = phys_mapping(phys, &offset);
135 if (hvc->index != 0) {
136 last = &hvc->ops[hvc->index - 1];
137 if ((last->type == MMAP) &&
138 (last->u.mmap.addr + last->u.mmap.len == virt) &&
139 (last->u.mmap.prot == prot) && (last->u.mmap.fd == fd) &&
140 (last->u.mmap.offset + last->u.mmap.len == offset)) {
141 last->u.mmap.len += len;
146 if (hvc->index == ARRAY_SIZE(hvc->ops)) {
147 ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
151 hvc->ops[hvc->index++] = ((struct host_vm_op)
153 .u = { .mmap = { .addr = virt,
162 static int add_munmap(unsigned long addr, unsigned long len,
163 struct host_vm_change *hvc)
165 struct host_vm_op *last;
168 if (addr + len > STUB_START && addr < STUB_END)
171 if (hvc->index != 0) {
172 last = &hvc->ops[hvc->index - 1];
173 if ((last->type == MUNMAP) &&
174 (last->u.munmap.addr + last->u.mmap.len == addr)) {
175 last->u.munmap.len += len;
180 if (hvc->index == ARRAY_SIZE(hvc->ops)) {
181 ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
185 hvc->ops[hvc->index++] = ((struct host_vm_op)
187 .u = { .munmap = { .addr = addr,
192 static int add_mprotect(unsigned long addr, unsigned long len,
193 unsigned int prot, struct host_vm_change *hvc)
195 struct host_vm_op *last;
198 if (addr + len > STUB_START && addr < STUB_END)
201 if (hvc->index != 0) {
202 last = &hvc->ops[hvc->index - 1];
203 if ((last->type == MPROTECT) &&
204 (last->u.mprotect.addr + last->u.mprotect.len == addr) &&
205 (last->u.mprotect.prot == prot)) {
206 last->u.mprotect.len += len;
211 if (hvc->index == ARRAY_SIZE(hvc->ops)) {
212 ret = do_ops(hvc, ARRAY_SIZE(hvc->ops), 0);
216 hvc->ops[hvc->index++] = ((struct host_vm_op)
218 .u = { .mprotect = { .addr = addr,
224 #define ADD_ROUND(n, inc) (((n) + (inc)) & ~((inc) - 1))
226 static inline int update_pte_range(pmd_t *pmd, unsigned long addr,
228 struct host_vm_change *hvc)
231 int r, w, x, prot, ret = 0;
233 pte = pte_offset_kernel(pmd, addr);
235 if ((addr >= STUB_START) && (addr < STUB_END))
241 if (!pte_young(*pte)) {
244 } else if (!pte_dirty(*pte))
247 prot = ((r ? UM_PROT_READ : 0) | (w ? UM_PROT_WRITE : 0) |
248 (x ? UM_PROT_EXEC : 0));
249 if (hvc->force || pte_newpage(*pte)) {
250 if (pte_present(*pte)) {
251 if (pte_newpage(*pte))
252 ret = add_mmap(addr, pte_val(*pte) & PAGE_MASK,
253 PAGE_SIZE, prot, hvc);
255 ret = add_munmap(addr, PAGE_SIZE, hvc);
256 } else if (pte_newprot(*pte))
257 ret = add_mprotect(addr, PAGE_SIZE, prot, hvc);
258 *pte = pte_mkuptodate(*pte);
259 } while (pte++, addr += PAGE_SIZE, ((addr < end) && !ret));
263 static inline int update_pmd_range(pud_t *pud, unsigned long addr,
265 struct host_vm_change *hvc)
271 pmd = pmd_offset(pud, addr);
273 next = pmd_addr_end(addr, end);
274 if (!pmd_present(*pmd)) {
275 if (hvc->force || pmd_newpage(*pmd)) {
276 ret = add_munmap(addr, next - addr, hvc);
277 pmd_mkuptodate(*pmd);
280 else ret = update_pte_range(pmd, addr, next, hvc);
281 } while (pmd++, addr = next, ((addr < end) && !ret));
285 static inline int update_pud_range(p4d_t *p4d, unsigned long addr,
287 struct host_vm_change *hvc)
293 pud = pud_offset(p4d, addr);
295 next = pud_addr_end(addr, end);
296 if (!pud_present(*pud)) {
297 if (hvc->force || pud_newpage(*pud)) {
298 ret = add_munmap(addr, next - addr, hvc);
299 pud_mkuptodate(*pud);
302 else ret = update_pmd_range(pud, addr, next, hvc);
303 } while (pud++, addr = next, ((addr < end) && !ret));
307 static inline int update_p4d_range(pgd_t *pgd, unsigned long addr,
309 struct host_vm_change *hvc)
315 p4d = p4d_offset(pgd, addr);
317 next = p4d_addr_end(addr, end);
318 if (!p4d_present(*p4d)) {
319 if (hvc->force || p4d_newpage(*p4d)) {
320 ret = add_munmap(addr, next - addr, hvc);
321 p4d_mkuptodate(*p4d);
324 ret = update_pud_range(p4d, addr, next, hvc);
325 } while (p4d++, addr = next, ((addr < end) && !ret));
329 void fix_range_common(struct mm_struct *mm, unsigned long start_addr,
330 unsigned long end_addr, int force)
333 struct host_vm_change hvc;
334 unsigned long addr = start_addr, next;
335 int ret = 0, userspace = 1;
337 hvc = INIT_HVC(mm, force, userspace);
338 pgd = pgd_offset(mm, addr);
340 next = pgd_addr_end(addr, end_addr);
341 if (!pgd_present(*pgd)) {
342 if (force || pgd_newpage(*pgd)) {
343 ret = add_munmap(addr, next - addr, &hvc);
344 pgd_mkuptodate(*pgd);
347 ret = update_p4d_range(pgd, addr, next, &hvc);
348 } while (pgd++, addr = next, ((addr < end_addr) && !ret));
351 ret = do_ops(&hvc, hvc.index, 1);
353 /* This is not an else because ret is modified above */
355 struct mm_id *mm_idp = ¤t->mm->context.id;
357 printk(KERN_ERR "fix_range_common: failed, killing current "
358 "process: %d\n", task_tgid_vnr(current));
363 static int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
365 struct mm_struct *mm;
371 unsigned long addr, last;
372 int updated = 0, err = 0, force = 0, userspace = 0;
373 struct host_vm_change hvc;
376 hvc = INIT_HVC(mm, force, userspace);
377 for (addr = start; addr < end;) {
378 pgd = pgd_offset(mm, addr);
379 if (!pgd_present(*pgd)) {
380 last = ADD_ROUND(addr, PGDIR_SIZE);
383 if (pgd_newpage(*pgd)) {
385 err = add_munmap(addr, last - addr, &hvc);
387 panic("munmap failed, errno = %d\n",
394 p4d = p4d_offset(pgd, addr);
395 if (!p4d_present(*p4d)) {
396 last = ADD_ROUND(addr, P4D_SIZE);
399 if (p4d_newpage(*p4d)) {
401 err = add_munmap(addr, last - addr, &hvc);
403 panic("munmap failed, errno = %d\n",
410 pud = pud_offset(p4d, addr);
411 if (!pud_present(*pud)) {
412 last = ADD_ROUND(addr, PUD_SIZE);
415 if (pud_newpage(*pud)) {
417 err = add_munmap(addr, last - addr, &hvc);
419 panic("munmap failed, errno = %d\n",
426 pmd = pmd_offset(pud, addr);
427 if (!pmd_present(*pmd)) {
428 last = ADD_ROUND(addr, PMD_SIZE);
431 if (pmd_newpage(*pmd)) {
433 err = add_munmap(addr, last - addr, &hvc);
435 panic("munmap failed, errno = %d\n",
442 pte = pte_offset_kernel(pmd, addr);
443 if (!pte_present(*pte) || pte_newpage(*pte)) {
445 err = add_munmap(addr, PAGE_SIZE, &hvc);
447 panic("munmap failed, errno = %d\n",
449 if (pte_present(*pte))
450 err = add_mmap(addr, pte_val(*pte) & PAGE_MASK,
453 else if (pte_newprot(*pte)) {
455 err = add_mprotect(addr, PAGE_SIZE, 0, &hvc);
460 err = do_ops(&hvc, hvc.index, 1);
463 panic("flush_tlb_kernel failed, errno = %d\n", err);
467 void flush_tlb_page(struct vm_area_struct *vma, unsigned long address)
474 struct mm_struct *mm = vma->vm_mm;
476 int r, w, x, prot, err = 0;
479 address &= PAGE_MASK;
481 if (address >= STUB_START && address < STUB_END)
484 pgd = pgd_offset(mm, address);
485 if (!pgd_present(*pgd))
488 p4d = p4d_offset(pgd, address);
489 if (!p4d_present(*p4d))
492 pud = pud_offset(p4d, address);
493 if (!pud_present(*pud))
496 pmd = pmd_offset(pud, address);
497 if (!pmd_present(*pmd))
500 pte = pte_offset_kernel(pmd, address);
505 if (!pte_young(*pte)) {
508 } else if (!pte_dirty(*pte)) {
512 mm_id = &mm->context.id;
513 prot = ((r ? UM_PROT_READ : 0) | (w ? UM_PROT_WRITE : 0) |
514 (x ? UM_PROT_EXEC : 0));
515 if (pte_newpage(*pte)) {
516 if (pte_present(*pte)) {
517 unsigned long long offset;
520 fd = phys_mapping(pte_val(*pte) & PAGE_MASK, &offset);
521 err = map(mm_id, address, PAGE_SIZE, prot, fd, offset,
524 else err = unmap(mm_id, address, PAGE_SIZE, 1, &flush);
526 else if (pte_newprot(*pte))
527 err = protect(mm_id, address, PAGE_SIZE, prot, 1, &flush);
536 *pte = pte_mkuptodate(*pte);
541 printk(KERN_ERR "Failed to flush page for address 0x%lx\n", address);
545 void flush_tlb_all(void)
548 * Don't bother flushing if this address space is about to be
551 if (atomic_read(¤t->mm->mm_users) == 0)
554 flush_tlb_mm(current->mm);
557 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
559 flush_tlb_kernel_range_common(start, end);
562 void flush_tlb_kernel_vm(void)
564 flush_tlb_kernel_range_common(start_vm, end_vm);
567 void __flush_tlb_one(unsigned long addr)
569 flush_tlb_kernel_range_common(addr, addr + PAGE_SIZE);
572 static void fix_range(struct mm_struct *mm, unsigned long start_addr,
573 unsigned long end_addr, int force)
576 * Don't bother flushing if this address space is about to be
579 if (atomic_read(&mm->mm_users) == 0)
582 fix_range_common(mm, start_addr, end_addr, force);
585 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
588 if (vma->vm_mm == NULL)
589 flush_tlb_kernel_range_common(start, end);
590 else fix_range(vma->vm_mm, start, end, 0);
592 EXPORT_SYMBOL(flush_tlb_range);
594 void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
597 fix_range(mm, start, end, 0);
600 void flush_tlb_mm(struct mm_struct *mm)
602 struct vm_area_struct *vma = mm->mmap;
604 while (vma != NULL) {
605 fix_range(mm, vma->vm_start, vma->vm_end, 0);
610 void force_flush_all(void)
612 struct mm_struct *mm = current->mm;
613 struct vm_area_struct *vma = mm->mmap;
615 while (vma != NULL) {
616 fix_range(mm, vma->vm_start, vma->vm_end, 1);