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;
129 fd
= phys_mapping(phys
, &offset
);
132 if (hvc
->index
!= 0) {
133 last
= &hvc
->ops
[hvc
->index
- 1];
134 if ((last
->type
== MMAP
) &&
135 (last
->u
.mmap
.addr
+ last
->u
.mmap
.len
== virt
) &&
136 (last
->u
.mmap
.prot
== prot
) && (last
->u
.mmap
.fd
== fd
) &&
137 (last
->u
.mmap
.offset
+ last
->u
.mmap
.len
== offset
)) {
138 last
->u
.mmap
.len
+= len
;
143 if (hvc
->index
== ARRAY_SIZE(hvc
->ops
)) {
144 ret
= do_ops(hvc
, ARRAY_SIZE(hvc
->ops
), 0);
148 hvc
->ops
[hvc
->index
++] = ((struct host_vm_op
)
150 .u
= { .mmap
= { .addr
= virt
,
159 static int add_munmap(unsigned long addr
, unsigned long len
,
160 struct host_vm_change
*hvc
)
162 struct host_vm_op
*last
;
165 if ((addr
>= STUB_START
) && (addr
< STUB_END
))
168 if (hvc
->index
!= 0) {
169 last
= &hvc
->ops
[hvc
->index
- 1];
170 if ((last
->type
== MUNMAP
) &&
171 (last
->u
.munmap
.addr
+ last
->u
.mmap
.len
== addr
)) {
172 last
->u
.munmap
.len
+= len
;
177 if (hvc
->index
== ARRAY_SIZE(hvc
->ops
)) {
178 ret
= do_ops(hvc
, ARRAY_SIZE(hvc
->ops
), 0);
182 hvc
->ops
[hvc
->index
++] = ((struct host_vm_op
)
184 .u
= { .munmap
= { .addr
= addr
,
189 static int add_mprotect(unsigned long addr
, unsigned long len
,
190 unsigned int prot
, struct host_vm_change
*hvc
)
192 struct host_vm_op
*last
;
195 if (hvc
->index
!= 0) {
196 last
= &hvc
->ops
[hvc
->index
- 1];
197 if ((last
->type
== MPROTECT
) &&
198 (last
->u
.mprotect
.addr
+ last
->u
.mprotect
.len
== addr
) &&
199 (last
->u
.mprotect
.prot
== prot
)) {
200 last
->u
.mprotect
.len
+= len
;
205 if (hvc
->index
== ARRAY_SIZE(hvc
->ops
)) {
206 ret
= do_ops(hvc
, ARRAY_SIZE(hvc
->ops
), 0);
210 hvc
->ops
[hvc
->index
++] = ((struct host_vm_op
)
212 .u
= { .mprotect
= { .addr
= addr
,
218 #define ADD_ROUND(n, inc) (((n) + (inc)) & ~((inc) - 1))
220 static inline int update_pte_range(pmd_t
*pmd
, unsigned long addr
,
222 struct host_vm_change
*hvc
)
225 int r
, w
, x
, prot
, ret
= 0;
227 pte
= pte_offset_kernel(pmd
, addr
);
229 if ((addr
>= STUB_START
) && (addr
< STUB_END
))
235 if (!pte_young(*pte
)) {
238 } else if (!pte_dirty(*pte
))
241 prot
= ((r
? UM_PROT_READ
: 0) | (w
? UM_PROT_WRITE
: 0) |
242 (x
? UM_PROT_EXEC
: 0));
243 if (hvc
->force
|| pte_newpage(*pte
)) {
244 if (pte_present(*pte
)) {
245 if (pte_newpage(*pte
))
246 ret
= add_mmap(addr
, pte_val(*pte
) & PAGE_MASK
,
247 PAGE_SIZE
, prot
, hvc
);
249 ret
= add_munmap(addr
, PAGE_SIZE
, hvc
);
250 } else if (pte_newprot(*pte
))
251 ret
= add_mprotect(addr
, PAGE_SIZE
, prot
, hvc
);
252 *pte
= pte_mkuptodate(*pte
);
253 } while (pte
++, addr
+= PAGE_SIZE
, ((addr
< end
) && !ret
));
257 static inline int update_pmd_range(pud_t
*pud
, unsigned long addr
,
259 struct host_vm_change
*hvc
)
265 pmd
= pmd_offset(pud
, addr
);
267 next
= pmd_addr_end(addr
, end
);
268 if (!pmd_present(*pmd
)) {
269 if (hvc
->force
|| pmd_newpage(*pmd
)) {
270 ret
= add_munmap(addr
, next
- addr
, hvc
);
271 pmd_mkuptodate(*pmd
);
274 else ret
= update_pte_range(pmd
, addr
, next
, hvc
);
275 } while (pmd
++, addr
= next
, ((addr
< end
) && !ret
));
279 static inline int update_pud_range(p4d_t
*p4d
, unsigned long addr
,
281 struct host_vm_change
*hvc
)
287 pud
= pud_offset(p4d
, addr
);
289 next
= pud_addr_end(addr
, end
);
290 if (!pud_present(*pud
)) {
291 if (hvc
->force
|| pud_newpage(*pud
)) {
292 ret
= add_munmap(addr
, next
- addr
, hvc
);
293 pud_mkuptodate(*pud
);
296 else ret
= update_pmd_range(pud
, addr
, next
, hvc
);
297 } while (pud
++, addr
= next
, ((addr
< end
) && !ret
));
301 static inline int update_p4d_range(pgd_t
*pgd
, unsigned long addr
,
303 struct host_vm_change
*hvc
)
309 p4d
= p4d_offset(pgd
, addr
);
311 next
= p4d_addr_end(addr
, end
);
312 if (!p4d_present(*p4d
)) {
313 if (hvc
->force
|| p4d_newpage(*p4d
)) {
314 ret
= add_munmap(addr
, next
- addr
, hvc
);
315 p4d_mkuptodate(*p4d
);
318 ret
= update_pud_range(p4d
, addr
, next
, hvc
);
319 } while (p4d
++, addr
= next
, ((addr
< end
) && !ret
));
323 void fix_range_common(struct mm_struct
*mm
, unsigned long start_addr
,
324 unsigned long end_addr
, int force
)
327 struct host_vm_change hvc
;
328 unsigned long addr
= start_addr
, next
;
329 int ret
= 0, userspace
= 1;
331 hvc
= INIT_HVC(mm
, force
, userspace
);
332 pgd
= pgd_offset(mm
, addr
);
334 next
= pgd_addr_end(addr
, end_addr
);
335 if (!pgd_present(*pgd
)) {
336 if (force
|| pgd_newpage(*pgd
)) {
337 ret
= add_munmap(addr
, next
- addr
, &hvc
);
338 pgd_mkuptodate(*pgd
);
341 ret
= update_p4d_range(pgd
, addr
, next
, &hvc
);
342 } while (pgd
++, addr
= next
, ((addr
< end_addr
) && !ret
));
345 ret
= do_ops(&hvc
, hvc
.index
, 1);
347 /* This is not an else because ret is modified above */
349 printk(KERN_ERR
"fix_range_common: failed, killing current "
350 "process: %d\n", task_tgid_vnr(current
));
351 /* We are under mmap_lock, release it such that current can terminate */
352 mmap_write_unlock(current
->mm
);
354 do_signal(¤t
->thread
.regs
);
358 static int flush_tlb_kernel_range_common(unsigned long start
, unsigned long end
)
360 struct mm_struct
*mm
;
366 unsigned long addr
, last
;
367 int updated
= 0, err
= 0, force
= 0, userspace
= 0;
368 struct host_vm_change hvc
;
371 hvc
= INIT_HVC(mm
, force
, userspace
);
372 for (addr
= start
; addr
< end
;) {
373 pgd
= pgd_offset(mm
, addr
);
374 if (!pgd_present(*pgd
)) {
375 last
= ADD_ROUND(addr
, PGDIR_SIZE
);
378 if (pgd_newpage(*pgd
)) {
380 err
= add_munmap(addr
, last
- addr
, &hvc
);
382 panic("munmap failed, errno = %d\n",
389 p4d
= p4d_offset(pgd
, addr
);
390 if (!p4d_present(*p4d
)) {
391 last
= ADD_ROUND(addr
, P4D_SIZE
);
394 if (p4d_newpage(*p4d
)) {
396 err
= add_munmap(addr
, last
- addr
, &hvc
);
398 panic("munmap failed, errno = %d\n",
405 pud
= pud_offset(p4d
, addr
);
406 if (!pud_present(*pud
)) {
407 last
= ADD_ROUND(addr
, PUD_SIZE
);
410 if (pud_newpage(*pud
)) {
412 err
= add_munmap(addr
, last
- addr
, &hvc
);
414 panic("munmap failed, errno = %d\n",
421 pmd
= pmd_offset(pud
, addr
);
422 if (!pmd_present(*pmd
)) {
423 last
= ADD_ROUND(addr
, PMD_SIZE
);
426 if (pmd_newpage(*pmd
)) {
428 err
= add_munmap(addr
, last
- addr
, &hvc
);
430 panic("munmap failed, errno = %d\n",
437 pte
= pte_offset_kernel(pmd
, addr
);
438 if (!pte_present(*pte
) || pte_newpage(*pte
)) {
440 err
= add_munmap(addr
, PAGE_SIZE
, &hvc
);
442 panic("munmap failed, errno = %d\n",
444 if (pte_present(*pte
))
445 err
= add_mmap(addr
, pte_val(*pte
) & PAGE_MASK
,
448 else if (pte_newprot(*pte
)) {
450 err
= add_mprotect(addr
, PAGE_SIZE
, 0, &hvc
);
455 err
= do_ops(&hvc
, hvc
.index
, 1);
458 panic("flush_tlb_kernel failed, errno = %d\n", err
);
462 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long address
)
469 struct mm_struct
*mm
= vma
->vm_mm
;
471 int r
, w
, x
, prot
, err
= 0;
474 address
&= PAGE_MASK
;
475 pgd
= pgd_offset(mm
, address
);
476 if (!pgd_present(*pgd
))
479 p4d
= p4d_offset(pgd
, address
);
480 if (!p4d_present(*p4d
))
483 pud
= pud_offset(p4d
, address
);
484 if (!pud_present(*pud
))
487 pmd
= pmd_offset(pud
, address
);
488 if (!pmd_present(*pmd
))
491 pte
= pte_offset_kernel(pmd
, address
);
496 if (!pte_young(*pte
)) {
499 } else if (!pte_dirty(*pte
)) {
503 mm_id
= &mm
->context
.id
;
504 prot
= ((r
? UM_PROT_READ
: 0) | (w
? UM_PROT_WRITE
: 0) |
505 (x
? UM_PROT_EXEC
: 0));
506 if (pte_newpage(*pte
)) {
507 if (pte_present(*pte
)) {
508 unsigned long long offset
;
511 fd
= phys_mapping(pte_val(*pte
) & PAGE_MASK
, &offset
);
512 err
= map(mm_id
, address
, PAGE_SIZE
, prot
, fd
, offset
,
515 else err
= unmap(mm_id
, address
, PAGE_SIZE
, 1, &flush
);
517 else if (pte_newprot(*pte
))
518 err
= protect(mm_id
, address
, PAGE_SIZE
, prot
, 1, &flush
);
527 *pte
= pte_mkuptodate(*pte
);
532 printk(KERN_ERR
"Failed to flush page for address 0x%lx\n", address
);
536 void flush_tlb_all(void)
539 * Don't bother flushing if this address space is about to be
542 if (atomic_read(¤t
->mm
->mm_users
) == 0)
545 flush_tlb_mm(current
->mm
);
548 void flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
550 flush_tlb_kernel_range_common(start
, end
);
553 void flush_tlb_kernel_vm(void)
555 flush_tlb_kernel_range_common(start_vm
, end_vm
);
558 void __flush_tlb_one(unsigned long addr
)
560 flush_tlb_kernel_range_common(addr
, addr
+ PAGE_SIZE
);
563 static void fix_range(struct mm_struct
*mm
, unsigned long start_addr
,
564 unsigned long end_addr
, int force
)
567 * Don't bother flushing if this address space is about to be
570 if (atomic_read(&mm
->mm_users
) == 0)
573 fix_range_common(mm
, start_addr
, end_addr
, force
);
576 void flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
579 if (vma
->vm_mm
== NULL
)
580 flush_tlb_kernel_range_common(start
, end
);
581 else fix_range(vma
->vm_mm
, start
, end
, 0);
583 EXPORT_SYMBOL(flush_tlb_range
);
585 void flush_tlb_mm_range(struct mm_struct
*mm
, unsigned long start
,
588 fix_range(mm
, start
, end
, 0);
591 void flush_tlb_mm(struct mm_struct
*mm
)
593 struct vm_area_struct
*vma
= mm
->mmap
;
595 while (vma
!= NULL
) {
596 fix_range(mm
, vma
->vm_start
, vma
->vm_end
, 0);
601 void force_flush_all(void)
603 struct mm_struct
*mm
= current
->mm
;
604 struct vm_area_struct
*vma
= mm
->mmap
;
606 while (vma
!= NULL
) {
607 fix_range(mm
, vma
->vm_start
, vma
->vm_end
, 1);
612 struct page_change_data
{
613 unsigned int set_mask
, clear_mask
;
616 static int change_page_range(pte_t
*ptep
, unsigned long addr
, void *data
)
618 struct page_change_data
*cdata
= data
;
619 pte_t pte
= READ_ONCE(*ptep
);
621 pte_clear_bits(pte
, cdata
->clear_mask
);
622 pte_set_bits(pte
, cdata
->set_mask
);
628 static int change_memory(unsigned long start
, unsigned long pages
,
629 unsigned int set_mask
, unsigned int clear_mask
)
631 unsigned long size
= pages
* PAGE_SIZE
;
632 struct page_change_data data
;
635 data
.set_mask
= set_mask
;
636 data
.clear_mask
= clear_mask
;
638 ret
= apply_to_page_range(&init_mm
, start
, size
, change_page_range
,
641 flush_tlb_kernel_range(start
, start
+ size
);
646 int set_memory_ro(unsigned long addr
, int numpages
)
648 return change_memory(addr
, numpages
, 0, _PAGE_RW
);
651 int set_memory_rw(unsigned long addr
, int numpages
)
653 return change_memory(addr
, numpages
, _PAGE_RW
, 0);
656 int set_memory_nx(unsigned long addr
, int numpages
)
661 int set_memory_x(unsigned long addr
, int numpages
)