MOS Source Code
Loading...
Searching...
No Matches
cow.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/mm/cow.h"
4
5#include "mos/mm/mm.h"
6#include "mos/mm/mmstat.h"
9
10#include <mos/interrupt/ipi.h>
11#include <mos/mm/cow.h>
13#include <mos/mm/physical/pmm.h>
15#include <mos/syslog/printk.h>
16#include <mos/tasks/process.h>
18#include <mos/types.h>
19#include <mos_string.h>
20
22static phyframe_t *zero_page(void)
23{
24 if (unlikely(!_zero_page))
25 {
29 }
30
31 return _zero_page;
32}
33
35{
36 MOS_UNUSED(fault_addr);
37
38 if (info->is_present && info->is_write)
39 {
40 vmap_stat_dec(vmap, cow); // the faulting page is a CoW page
41 vmap_stat_inc(vmap, regular);
42 return mm_resolve_cow_fault(vmap, fault_addr, info);
43 }
44
45 MOS_ASSERT(!info->is_present); // we can't have (present && !write)
46
47 if (info->is_write)
48 {
49 // non-present and write, must be a ZoD page
51 vmap_stat_inc(vmap, regular);
53 }
54 else
55 {
56 info->backing_page = zero_page();
57 vmap_stat_inc(vmap, cow);
59 }
60}
61
63{
64 // remove that VM_WRITE flag
65 mm_flag_pages_locked(src_vmap->mmctx, src_vmap->vaddr, src_vmap->npages, src_vmap->vmflags & ~VM_WRITE);
66 src_vmap->stat.cow += src_vmap->stat.regular;
67 src_vmap->stat.regular = 0; // no longer private
68
69 vmap_t *dst_vmap = mm_clone_vmap_locked(src_vmap, target_mmctx);
70
71 if (!src_vmap->on_fault)
73
74 dst_vmap->on_fault = src_vmap->on_fault;
75 dst_vmap->stat = dst_vmap->stat;
76 return dst_vmap;
77}
78
79vmap_t *cow_allocate_zeroed_pages(mm_context_t *mmctx, size_t npages, ptr_t vaddr, valloc_flags allocflags, vm_flags flags)
80{
82 vmap_t *vmap = mm_get_free_vaddr_locked(mmctx, npages, vaddr, allocflags);
84
85 if (IS_ERR(vmap))
86 return vmap;
87
88 vmap->vmflags = flags;
90 return vmap;
91}
#define MOS_ASSERT(cond)
Definition assert.h:14
#define MOS_PAGE_SIZE
Definition autoconf.h:6
static vmfault_result_t cow_zod_fault_handler(vmap_t *vmap, ptr_t fault_addr, pagefault_t *info)
Definition cow.c:34
static phyframe_t * zero_page(void)
Definition cow.c:22
static phyframe_t * _zero_page
Definition cow.c:21
vmap_t * cow_allocate_zeroed_pages(mm_context_t *mmctx, size_t npages, ptr_t vaddr, valloc_flags allocflags, vm_flags flags)
Allocate zero-on-demand pages at a specific address.
Definition cow.c:79
vmap_t * cow_clone_vmap_locked(mm_context_t *target_mmctx, vmap_t *src_vmap)
Copy-on-write a page range.
Definition cow.c:62
__BEGIN_DECLS phyframe_t * mm_get_free_page(void)
Definition mm.c:46
#define phyframe_va(frame)
Definition mm.h:79
vmfault_result_t
Definition mm.h:45
vmfault_result_t mm_resolve_cow_fault(vmap_t *vmap, ptr_t fault_addr, pagefault_t *info)
Helper function to resolve a copy-on-write fault.
Definition mm.c:273
@ VMFAULT_MAP_BACKING_PAGE
the caller should map the backing page into the faulting address
Definition mm.h:48
@ VMFAULT_MAP_BACKING_PAGE_RO
the caller should map the backing page into the faulting address, and mark it non-writable
Definition mm.h:47
valloc_flags
Definition paging.h:20
vmap_t * mm_clone_vmap_locked(vmap_t *src_vmap, mm_context_t *dst_ctx)
Remap a block of virtual memory from one page table to another, i.e. copy the mappings.
Definition paging.c:132
void mm_flag_pages_locked(mm_context_t *mmctx, ptr_t vaddr, size_t npages, vm_flags flags)
Update the flags of a block of virtual memory.
Definition paging.c:171
vmap_t * mm_get_free_vaddr_locked(mm_context_t *mmctx, size_t n_pages, ptr_t base_vaddr, valloc_flags flags)
Gets npages unmapped free pages from a page table.
Definition paging.c:18
#define pmm_ref_one(thing)
Definition pmm.h:146
#define vmap_stat_inc(vmap, type)
Definition mmstat.h:70
#define vmap_stat_dec(vmap, type)
Definition mmstat.h:71
#define MOS_UNUSED(x)
Definition mos_global.h:64
#define unlikely(x)
Definition mos_global.h:40
#define NULL
Definition pb_syshdr.h:46
vm_flags
Definition platform.h:40
@ VM_WRITE
Definition platform.h:42
#define memzero(ptr, size)
Definition rpc_client.c:40
#define spinlock_acquire(lock)
Definition spinlock.h:61
#define spinlock_release(lock)
Definition spinlock.h:62
spinlock_t mm_lock
protects [pgd] and the [mmaps] list (the list itself, not the vmap_t objects)
Definition platform.h:81
bool is_present
Definition mm.h:37
const phyframe_t * backing_page
the frame that contains the data for this page, the on_fault handler should set this
Definition mm.h:41
bool is_write
Definition mm.h:37
size_t cow
pages that are copy-on-write
Definition mmstat.h:67
size_t regular
regular pages with no special flags being set or unset
Definition mmstat.h:65
Definition mm.h:58
ptr_t vaddr
Definition mm.h:62
size_t npages
Definition mm.h:63
vmfault_handler_t on_fault
Definition mm.h:73
vm_flags vmflags
Definition mm.h:64
vmap_stat_t stat
Definition mm.h:72
mm_context_t * mmctx
Definition mm.h:65
unsigned long ptr_t
Definition types.h:25