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

Mmap #16

Open
wants to merge 8 commits into
base: mmap
Choose a base branch
from
Open

Mmap #16

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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ UPROGS=\
$U/_bcachetest\
$U/_alloctest\
$U/_bigfile\
$U/_mmaptest\

fs.img: mkfs/mkfs README user/xargstest.sh $(UPROGS)
mkfs/mkfs fs.img README user/xargstest.sh $(UPROGS)
Expand Down
2 changes: 2 additions & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void ramdiskintr(void);
void ramdiskrw(struct buf*);

// kalloc.c
void in_ref(void *);
void* kalloc(void);
void kfree(void *);
void kinit();
Expand Down Expand Up @@ -154,6 +155,7 @@ void uartputc(int);
int uartgetc(void);

// vm.c
pte_t* walk(pagetable_t, uint64, int);
void kvminit(void);
void kvminithart(void);
uint64 kvmpa(uint64);
Expand Down
41 changes: 31 additions & 10 deletions kernel/kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "riscv.h"
#include "defs.h"

#define REF_INDEX(pa) ((char*) pa - end) >> 12

void freerange(void *pa_start, void *pa_end);

extern char end[]; // first address after kernel.
Expand All @@ -21,22 +23,36 @@ struct run {
struct {
struct spinlock lock;
struct run *freelist;
char *kref;
} kmem;

void in_ref(void *pa){
kmem.kref[REF_INDEX(pa)]++;
}


void
kinit()
{
initlock(&kmem.lock, "kmem");
freerange(end, (void*)PHYSTOP);
kmem.kref = end;
uint64 rc_offset = (((((PHYSTOP - (uint64)end) >> 12) + 1) * sizeof(uint) >> 12) + 1) << 12;
freerange(end + rc_offset, (void*)PHYSTOP);
}

void
freerange(void *pa_start, void *pa_end)
{
char *p;
p = (char*)PGROUNDUP((uint64)pa_start);
for(; p + PGSIZE <= (char*)pa_end; p += PGSIZE)
kfree(p);
for(; p + PGSIZE <= (char*)pa_end; p += PGSIZE){
memset(p, 1, PGSIZE);
struct run *r = (struct run*)p;
acquire(&kmem.lock);
r->next = kmem.freelist;
kmem.freelist = r;
release(&kmem.lock);
}
}

// Free the page of physical memory pointed at by v,
Expand All @@ -52,14 +68,17 @@ kfree(void *pa)
panic("kfree");

// Fill with junk to catch dangling refs.
memset(pa, 1, PGSIZE);
char ref = --kmem.kref[REF_INDEX(pa)];
if(ref == 0){
memset(pa, 1, PGSIZE);

r = (struct run*)pa;
r = (struct run*)pa;

acquire(&kmem.lock);
r->next = kmem.freelist;
kmem.freelist = r;
release(&kmem.lock);
acquire(&kmem.lock);
r->next = kmem.freelist;
kmem.freelist = r;
release(&kmem.lock);
}
}

// Allocate one 4096-byte page of physical memory.
Expand All @@ -72,8 +91,10 @@ kalloc(void)

acquire(&kmem.lock);
r = kmem.freelist;
if(r)
if(r){
kmem.kref[REF_INDEX(r)] = 1;
kmem.freelist = r->next;
}
release(&kmem.lock);

if(r)
Expand Down
6 changes: 6 additions & 0 deletions kernel/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
#define FSSIZE 2000 // size of file system in blocks
#define MAXPATH 128 // maximum file path name
#define NDISK 2

#define NMAP 5 //maximum number of mapped files per process
#define PROT_READ (1L << 1)
#define PROT_WRITE (1L << 2)
#define MAP_PRIVATE 0
#define MAP_SHARED 1
31 changes: 30 additions & 1 deletion kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ allocproc(void)
p->context.ra = (uint64)forkret;
p->context.sp = p->kstack + PGSIZE;

memset(&p->vma_list, 0, sizeof p->vma_list);



return p;
}

Expand Down Expand Up @@ -272,9 +276,26 @@ fork(void)
np->tf->a0 = 0;

// increment reference counts on open file descriptors.
for(i = 0; i < NOFILE; i++)
for(i = 0; i < NOFILE; i++){
if(p->ofile[i])
np->ofile[i] = filedup(p->ofile[i]);
}

for(i = 0; i < NMAP; i++){
if(p->vma_list[i].valid){
np->vma_list[i].f = p->vma_list[i].f;
filedup(p->vma_list[i].f);
np->vma_list[i].valid = 1;
np->vma_list[i].va = p->vma_list[i].va;
np->vma_list[i].length = p->vma_list[i].length;
np->vma_list[i].unmap_length = p->vma_list[i].unmap_length;
np->vma_list[i].prot = p->vma_list[i].prot;
np->vma_list[i].offset = p->vma_list[i].offset;
np->vma_list[i].flag = p->vma_list[i].flag;
} else{
np->vma_list[i].valid = 0;
}
}
np->cwd = idup(p->cwd);

safestrcpy(np->name, p->name, sizeof(p->name));
Expand Down Expand Up @@ -334,6 +355,14 @@ exit(int status)
}
}

for(int i=0; i < NMAP; i++){
if(p->vma_list[i].valid){
struct file *f = p->vma_list[i].f;
fileclose(f);
p->vma_list[i].valid = 0;
}
}

begin_op(ROOTDEV);
iput(p->cwd);
end_op(ROOTDEV);
Expand Down
14 changes: 14 additions & 0 deletions kernel/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ struct trapframe {
/* 280 */ uint64 t6;
};

struct vma
{
int valid;
uint64 va;
int length;
int unmap_length;
struct file *f;
int prot;
int offset;
int flag;
};


enum procstate { UNUSED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };

// Per-process state
Expand All @@ -103,4 +116,5 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
struct vma vma_list[NMAP];
};
2 changes: 2 additions & 0 deletions kernel/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ sfence_vma()
#define PTE_W (1L << 2)
#define PTE_X (1L << 3)
#define PTE_U (1L << 4) // 1 -> user can access
#define PTE_D (1L << 7)
#define PTE_M (1L << 8)

// shift a physical address to the right place for a PTE.
#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
Expand Down
4 changes: 4 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ extern uint64 sys_wait(void);
extern uint64 sys_write(void);
extern uint64 sys_uptime(void);
extern uint64 sys_ntas(void);
extern uint64 sys_mmap(void);
extern uint64 sys_munmap(void);

static uint64 (*syscalls[])(void) = {
[SYS_fork] sys_fork,
Expand All @@ -129,6 +131,8 @@ static uint64 (*syscalls[])(void) = {
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_ntas] sys_ntas,
[SYS_mmap] sys_mmap,
[SYS_munmap] sys_munmap,
};

void
Expand Down
2 changes: 2 additions & 0 deletions kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@

// System calls for labs
#define SYS_ntas 22
#define SYS_mmap 23
#define SYS_munmap 24
40 changes: 39 additions & 1 deletion kernel/trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "riscv.h"
#include "spinlock.h"
#include "proc.h"
#include "sleeplock.h"
#include "fs.h"
#include "file.h"
#include "defs.h"

struct spinlock tickslock;
Expand Down Expand Up @@ -70,7 +73,41 @@ usertrap(void)
syscall();
} else if((which_dev = devintr()) != 0){
// ok
} else {
} else if(r_scause() == 13 || r_scause() == 15){
uint64 fault_addr = r_stval();
uint64 vpage_addr = PGROUNDDOWN(fault_addr);
char *mem;
int prot;
for(struct vma *vma = p->vma_list; vma < p->vma_list + NMAP; vma++){
if(vma->valid != 0 && vma->va <= fault_addr && vma->va + vma->length > fault_addr){
if((mem = (char*) walkaddr(p->pagetable, vpage_addr)) == 0){
mem = kalloc();
prot = vma->prot;
}

if(mem == 0){
p->killed = 1;
printf("usertrap(): alloc memory failed\n");
exit(-1);
}
memset(mem, 0, PGSIZE);

if(mappages(p->pagetable, vpage_addr, PGSIZE, (uint64)mem, PTE_U|prot) != 0){
kfree(mem);
p->killed = 1;
printf("usertrap(): cannot mapping\n");
exit(-1);
}

struct file *f = vma->f;
f->off = vpage_addr - vma->va;
fileread(f, vpage_addr, PGSIZE);
goto TRAP_END;
}
}
printf("usertrap(): page fault\n");
p->killed = 1;
} else{
printf("usertrap(): unexpected scause %p (%s) pid=%d\n", r_scause(), scause_desc(r_scause()), p->pid);
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
p->killed = 1;
Expand All @@ -79,6 +116,7 @@ usertrap(void)
if(p->killed)
exit(-1);

TRAP_END:
// give up the CPU if this is a timer interrupt.
if(which_dev == 2)
yield();
Expand Down
Loading