Skip to content

Commit

Permalink
Add pgaccess syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
cw00h committed Jan 10, 2023
1 parent 2daef57 commit ee4d06b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
int pgaccess(void *base, int len, void *mask);


// swtch.S
void swtch(struct context*, struct context*);
Expand Down
26 changes: 26 additions & 0 deletions kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct proc *initproc;
int nextpid = 1;
struct spinlock pid_lock;

extern pte_t *walk(pagetable_t pagetable, uint64 va, int alloc);
extern void forkret(void);
static void freeproc(struct proc *p);

Expand Down Expand Up @@ -675,3 +676,28 @@ procdump(void)
printf("\n");
}
}


int pgaccess(void *base, int len, void *mask) {
struct proc *p = myproc();
unsigned int temp;
char *_base = base;
pte_t *pte;

if(len > 32) return -1;

for(int i = 0; i < len; i++) {
if((pte = walk(p->pagetable, (uint64)(_base + PGSIZE * i), 0))) {
if(*pte & PTE_A) {
*pte &= ~PTE_A;
temp |= (1 << i);
}
}
else return -1;
}

if(copyout(p->pagetable, (uint64)mask, (char *)&temp, sizeof(temp)) < 0) {
printf("copyout error\n");
}
return 0;
}
1 change: 1 addition & 0 deletions kernel/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ sfence_vma()
#define PTE_W (1L << 2)
#define PTE_X (1L << 3)
#define PTE_U (1L << 4) // 1 -> user can access
#define PTE_A (1L << 6)

// shift a physical address to the right place for a PTE.
#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
Expand Down
17 changes: 10 additions & 7 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ sys_sleep(void)
}


#ifdef LAB_PGTBL
int
sys_pgaccess(void)
{
// lab pgtbl: your code here.
return 0;
int sys_pgaccess(void) {
uint64 base, mask;
int len;

if(argaddr(0, &base) < 0) return -1;
if(argint(1, &len) < 0) return -1;
if(argaddr(2, &mask) < 0) return -1;

return pgaccess((void *)base, len, (void *)mask);
}
#endif

uint64
sys_kill(void)
Expand All @@ -107,3 +109,4 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}

2 changes: 2 additions & 0 deletions user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ struct stat;
struct rtcdate;
struct sysinfo;

#define LAB_PGTBL 1

// system calls
int fork(void);
int exit(int) __attribute__((noreturn));
Expand Down

0 comments on commit ee4d06b

Please sign in to comment.