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

UVM: basic anon implementation #1076

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 42 additions & 0 deletions include/sys/vm_anon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef _SYS_UVM_ANON_H_
#define _SYS_UVM_ANON_H_

#ifndef _KERNEL
#error "Do not use this header file outside kernel code!"
#endif

#include <sys/mutex.h>
#include <sys/vm.h>

typedef struct vm_anon vm_anon_t;

/*
* Field markings and the corresponding locks:
* (@) vm_anon:an_lock
*/

struct vm_anon {
mtx_t an_lock;
int an_ref; /* (@) Reference counter */
vm_page_t *an_page; /* (@) Pointer to holded page */
};

/* Allocate new anon. */
vm_anon_t *vm_anon_alloc(void);
/* Acquire anon->an_lock. */
void vm_anon_lock(vm_anon_t *anon);
/* Release anon->an_lock. */
void vm_anon_unlock(vm_anon_t *anon);

/* Increase anon's reference counter.
*
* Must be called with anon:an_lock held. */
void vm_anon_hold(vm_anon_t *anon);

/* Decrease anon's reference counter and destroy anon if it reached 0.
*
* Must be called with anon:an_lock held.
* Releases anon:an_lock. */
void vm_anon_drop(vm_anon_t *anon);

#endif /* !_SYS_UVM_ANON_H_ */
1 change: 1 addition & 0 deletions sys/kern/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ SOURCES = \
uart_tty.c \
uio.c \
ustack.c \
uvm_anon.c \
vfs.c \
vfs_name.c \
vfs_readdir.c \
Expand Down
42 changes: 42 additions & 0 deletions sys/kern/vm_anon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#define KL_LOG KL_VM
#include <sys/mimiker.h>
#include <sys/klog.h>
#include <sys/pool.h>
#include <sys/vm.h>
#include <sys/vm_physmem.h>
#include <sys/vm_anon.h>

static POOL_DEFINE(P_ANON, "vm_anon", sizeof(vm_anon_t));

vm_anon_t *vm_anon_alloc(void) {
vm_anon_t *anon = pool_alloc(P_ANON, M_ZERO);
mtx_init(&anon->an_lock, 0);
return anon;
}

void vm_anon_lock(vm_anon_t *anon) {
mtx_lock(&anon->an_lock);
}

void vm_anon_unlock(vm_anon_t *anon) {
mtx_unlock(&anon->an_lock);
}

void vm_anon_hold(vm_anon_t *anon) {
assert(mtx_owned(&anon->an_lock));
anon->an_ref++;
}

static void anon_free(vm_anon_t *anon) {
/* TODO(fz): free anon->an_page */
pool_free(P_ANON, anon);
}

void vm_anon_drop(vm_anon_t *anon) {
assert(mtx_owned(&anon->an_lock));
anon->an_ref--;

vm_anon_unlock(anon);
if (anon->an_ref == 0)
anon_free(anon);
}
1 change: 1 addition & 0 deletions sys/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SOURCES = \
turnstile_propagate_many.c \
uiomove.c \
utest.c \
uvm.c \
vm_map.c \
devclass.c \
vfs.c \
Expand Down
25 changes: 25 additions & 0 deletions sys/tests/vm_anon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <sys/klog.h>
#include <sys/ktest.h>
#include <sys/vm_anon.h>

static int test_anon_simple(void) {

vm_anon_t *anon = vm_anon_alloc();
assert(anon != NULL);

vm_anon_lock(anon);

vm_anon_hold(anon);
vm_anon_hold(anon);
assert(anon->an_ref == 2);

vm_anon_drop(anon);
assert(anon->an_ref == 1);

vm_anon_lock(anon);
vm_anon_drop(anon);

return KTEST_SUCCESS;
}

KTEST_ADD(vm_anon_simple, test_anon_simple, 0);