Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: Copy on write #1392

Merged
merged 54 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
20f67ed
cow: First implementation
franciscozdo May 16, 2023
829ec29
mips: add EXC_TLB{RI,XI} to trap handler
franciscozdo Jun 15, 2023
d86bdcc
gdb: get mips pmap correctly
franciscozdo Jun 15, 2023
affe470
vm: Properly limit protection while cloning vm_map
franciscozdo Jun 15, 2023
a04a72f
tests: add mmap_private test
franciscozdo Jun 15, 2023
15c1c15
Redesign vm_page_fault with amaps
franciscozdo Jun 17, 2023
88e76d1
Merge branch 'master' into uvm-cow
franciscozdo Jun 17, 2023
3e775de
vm: Fix order of operations during COW
franciscozdo Jul 1, 2023
e637abc
Merge branch 'master' into uvm-cow
cahirwpz Jul 6, 2023
d17f6d7
Revert "gdb: get mips pmap correctly"
franciscozdo Jul 7, 2023
0237f71
Revert "tests: add mmap_private test"
franciscozdo Jul 7, 2023
de573c8
tests: add mmap_private test
franciscozdo Jun 15, 2023
5c01aeb
Merge branch 'master' into add-mmap-test
franciscozdo Jul 7, 2023
6c1b4e8
Update retval comments
franciscozdo Jul 7, 2023
2ad61b0
Update include format
franciscozdo Jul 7, 2023
c8b63b9
Fix typo
franciscozdo Jul 7, 2023
7048086
Unify order of fields during init of vm_aref_t
franciscozdo Jul 7, 2023
7bb2b72
fix signal name
franciscozdo Jul 7, 2023
55a1507
Call exit at the end of child branch
franciscozdo Jul 7, 2023
ab617fb
Merge branch 'add-mmap-test' into uvm-cow
franciscozdo Jul 9, 2023
1b5a84f
Make anons ref_cnt atomic again
franciscozdo Jul 12, 2023
b8c2f78
Fix formatting
franciscozdo Jul 12, 2023
279533a
Check if anon's page is allocated
franciscozdo Jul 12, 2023
3f22a15
Make amaps ref_cnt atomics
franciscozdo Jul 12, 2023
198633c
Properly use amap mtx
franciscozdo Jul 12, 2023
dfc09a2
Merge branch 'master' into uvm-cow
cahirwpz Jul 16, 2023
84e7b53
Fix tests after merge
franciscozdo Jul 17, 2023
06f29e7
Apply changes suggested in review
franciscozdo Jul 17, 2023
cd63d04
Remove klog from page fault handler
franciscozdo Jul 17, 2023
46876b8
Merge branch 'master' into uvm-cow
franciscozdo Jul 17, 2023
366155c
Style changes
franciscozdo Jul 18, 2023
6b65a56
Merge branch 'master' into uvm-cow
franciscozdo Jul 23, 2023
bd270f8
Sort includes
franciscozdo Jul 23, 2023
08453d0
Chnage locking during amap needs copy
franciscozdo Jul 23, 2023
3fbf2ef
Update comment about future cases to handle
franciscozdo Jul 23, 2023
a1cfc25
Reorganize COW handling in vm_page_fault
franciscozdo Jul 23, 2023
10c5287
Describe amap's fields
franciscozdo Jul 23, 2023
0893dcc
Remove klog from vm_page_fault
franciscozdo Jul 24, 2023
eddff78
Rename variables
franciscozdo Jul 24, 2023
688a53f
Simplify code by using default values
franciscozdo Jul 24, 2023
b96faa4
Handle non-write case differently during COW
franciscozdo Jul 24, 2023
2323a4b
Revert "Handle non-write case differently during COW"
franciscozdo Jul 25, 2023
bbc79ba
Simplify flow by limiting insert prot earlier
franciscozdo Jul 25, 2023
1e7247c
Change ifs in vm_page_fault
franciscozdo Jul 25, 2023
811e064
Simplify even more
franciscozdo Jul 25, 2023
4fb0e7a
Adjust comments
franciscozdo Jul 25, 2023
8e7cc0e
Visual tweaks to vm_page_fault
franciscozdo Jul 26, 2023
41bbed5
Remove unused definition and fix comment
franciscozdo Jul 26, 2023
36a78cd
Rename vm_amap_needs_copy to vm_amap_copy_if_needed
franciscozdo Jul 26, 2023
3efcb1f
Rearrange arguments of cow_page_fault
franciscozdo Jul 26, 2023
1fa56be
Remove unnecessary includes
franciscozdo Jul 26, 2023
430c619
Revert "Remove unnecessary includes"
franciscozdo Jul 26, 2023
bf29ae2
Remove unncecesary includes from .h files
franciscozdo Jul 26, 2023
ba5c004
Add includes of mimiker and thread headers
franciscozdo Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions include/sys/vm_amap.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,116 @@
#ifndef _SYS_VM_AMAP_H_
#define _SYS_VM_AMAP_H_

#include <sys/mutex.h>
franciscozdo marked this conversation as resolved.
Show resolved Hide resolved
#include <sys/refcnt.h>
#include <sys/types.h>
#include <sys/vm.h>
#include <stddef.h>

typedef struct vm_amap vm_amap_t;
typedef struct vm_anon vm_anon_t;
typedef struct vm_aref vm_aref_t;

/* Anon structure
*
* Marks for fields locks:
* (a) atomic
* (!) read-only access, do not modify!
*/
struct vm_anon {
refcnt_t ref_cnt; /* (a) number of references */
vm_page_t *page; /* (!) anon page */
};

struct vm_aref {
size_t offset; /* offset in slots */
vm_amap_t *amap; /* underlying amap */
};

/*
* Allocate new amap with specified number of slots.
/** Allocate new amap.
*
* Always returns new amap with ref_cnt equal to 1.
* The size of new amap is determined using `slots`.
*
* @returns New amap with ref_cnt = 1.
*/
vm_amap_t *vm_amap_alloc(size_t slots);

/*
/** Clone the amap.
*
* Create new amap with contents matching old amap. Starting from offset
* specified by aref and copying specified number of slots.
*
* Always returns new amap with ref_cnt equal to 1.
* @returns New amap with ref_cnt = 1.
cahirwpz marked this conversation as resolved.
Show resolved Hide resolved
*/
vm_amap_t *vm_amap_clone(vm_aref_t aref, size_t slots);
franciscozdo marked this conversation as resolved.
Show resolved Hide resolved

/* Bump the ref counter to record that amap is used by next one entry. */
/** Copy amap when it is needed.
*
* Amap is actually copied is referenced by more then one vm_map_entries.
franciscozdo marked this conversation as resolved.
Show resolved Hide resolved
* Otherwise nothing is done and old aref is returned.
*
* @returns Aref ready to use
*/
vm_aref_t vm_amap_needs_copy(vm_aref_t aref, size_t slots);
franciscozdo marked this conversation as resolved.
Show resolved Hide resolved

/** Bump the ref counter to record that amap is used by next one entry. */
void vm_amap_hold(vm_amap_t *amap);

/* Drop ref counter and possibly free amap if it drops to 0. */
/** Drop ref counter and possibly free amap if it drops to 0. */
void vm_amap_drop(vm_amap_t *amap);

vm_page_t *vm_amap_find_page(vm_aref_t aref, size_t offset);
int vm_amap_add_page(vm_aref_t aref, vm_page_t *frame, size_t offset);
/** Remove multiple pages from amap
*
* @param offset Start from this offset in amap.
* @param n_slots Remove that many pages (anons).
*/
void vm_amap_remove_pages(vm_aref_t aref, size_t offset, size_t n_slots);

/* Functions accessing amap's internal data */
/** Get amap reference count */
int vm_amap_ref(vm_amap_t *amap);

/** Get amap max size (in slots) */
size_t vm_amap_slots(vm_amap_t *amap);

/** Alloc new anon.
*
* @retval != NULL Anon with zeroed page and ref_cnt = 1.
* @retval NULL If anon's page can't be allocate.
*/
vm_anon_t *vm_anon_alloc(void);

/** Copy anon.
*
* NOTE: Don't decrease ref_cnt of src.
*
* @retval != NULL Anon with ref_cnt = 1.
* @retval NULL If anon's page can't be allocate.
*/
vm_anon_t *vm_anon_copy(vm_anon_t *src);

/** Find anon in amap.
*
* @returns Found anon or NULL.
*/
vm_anon_t *vm_amap_find_anon(vm_aref_t aref, size_t offset);

/** Insert anon into amap
*
* If anon was already present at given offset it will be replaced and returned
* from this function.
*
* @retval NULL Anon was inserted
* @retval != NULL Anon that was previously at that offset.
* */
vm_anon_t *vm_amap_insert_anon(vm_aref_t aref, vm_anon_t *anon, size_t offset);

/** Replace anon in amap with new one. */
void vm_amap_replace_anon(vm_aref_t aref, vm_anon_t *anon, size_t offset);

/** Bump the ref counter to record that anon is used by one more amap. */
void vm_anon_hold(vm_anon_t *anon);

/** Drop ref counter and possibly free anon if it drops to 0. */
void vm_anon_drop(vm_anon_t *anon);

#endif /* !_SYS_VM_AMAP_H_ */
6 changes: 4 additions & 2 deletions include/sys/vm_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ typedef struct vm_map vm_map_t;
typedef struct vm_map_entry vm_map_entry_t;

typedef enum {
VM_ENT_SHARED = 1, /* shared memory */
VM_ENT_PRIVATE = 2, /* private memory (default) */
VM_ENT_SHARED = 1, /* shared memory */
VM_ENT_PRIVATE = 2, /* private memory (default) */
VM_ENT_COW = 4, /* copy on write */
VM_ENT_NEEDSCOPY = 8, /* amap needs copy */
} vm_entry_flags_t;

/*! \brief Called during kernel initialization. */
Expand Down
3 changes: 0 additions & 3 deletions sys/gen/pmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,6 @@ int pmap_fault_handler(ctx_t *ctx, vaddr_t vaddr, vm_prot_t access) {
if (!(error = pmap_emulate_bits(pmap, vaddr, access)))
return 0;

if (error == EACCES)
goto fault;

franciscozdo marked this conversation as resolved.
Show resolved Hide resolved
vm_map_t *vmap = vm_map_user();

if (!(error = vm_page_fault(vmap, vaddr, access)))
Expand Down
Loading