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 2 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
35 changes: 35 additions & 0 deletions include/sys/uvm_anon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#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 uvm_anon uvm_anon_t;

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

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

/* Allocate new anon */
uvm_anon_t *uvm_anon_alloc(void);
/* Acquire anon->an_lock */
void uvm_anon_lock(uvm_anon_t *anon);
/* Release anon->an_lock */
void uvm_anon_unlock(uvm_anon_t *anon);
/* Increase anon's reference counter */
void uvm_anon_hold(uvm_anon_t *anon);
/* Decrease anon's reference counter and destroy anon if it reached 0 */
void uvm_anon_drop(uvm_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/uvm_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/uvm_anon.h>

static POOL_DEFINE(P_ANON, "uvm_anon", sizeof(uvm_anon_t));

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

void uvm_anon_lock(uvm_anon_t *anon) {
mtx_lock(&anon->an_lock);
}

void uvm_anon_unlock(uvm_anon_t *anon) {
mtx_unlock(&anon->an_lock);
}

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

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

void uvm_anon_drop(uvm_anon_t *anon) {
assert(mtx_owned(&anon->an_lock));
anon->an_ref--;

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
24 changes: 24 additions & 0 deletions sys/tests/uvm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <sys/klog.h>
#include <sys/ktest.h>
#include <sys/uvm_anon.h>

static int test_anon_simple(void) {

uvm_anon_t *anon = uvm_anon_alloc();
assert(anon != NULL);

uvm_anon_lock(anon);

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

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

uvm_anon_drop(anon);

return KTEST_SUCCESS;
}

KTEST_ADD(uvm_anon_simple, test_anon_simple, 0);