Skip to content

Commit 019ab2a

Browse files
committed
mm: implemented vtop()
1 parent 693c356 commit 019ab2a

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/include/kernel/memory.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@ uintptr_t vmmSetFlags(uintptr_t, size_t, int);
7070

7171
void *sbrk(Thread *, intptr_t);
7272

73-
uintptr_t mmio(Thread *t, uintptr_t, off_t, int);
74-
uintptr_t pcontig(Thread *t, uintptr_t, off_t, int);
73+
uintptr_t mmio(Thread *, uintptr_t, off_t, int);
74+
uintptr_t pcontig(Thread *, uintptr_t, off_t, int);
75+
uintptr_t vtop(Thread *, uintptr_t);

src/memory/mmio.c

+24
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,28 @@ uintptr_t mmio(Thread *t, uintptr_t addr, off_t count, int flags) {
6565
//KDEBUG("unmapped %d pages at virtual address 0x%X for tid %d\n", pageCount, addr, t->tid);
6666
return 0;
6767
}
68+
}
69+
70+
/* vtop(): returns the physical address associated with a virtual address
71+
* params: t - calling thread
72+
* params: virt - virtual address
73+
* returns: physical address on success, zero on error
74+
*/
75+
76+
uintptr_t vtop(Thread *t, uintptr_t virt) {
77+
Process *p = getProcess(t->pid);
78+
if(!p) return 0;
79+
80+
// only root can do this
81+
if(p->user) return 0;
82+
83+
off_t offset = virt & (PAGE_SIZE-1);
84+
uintptr_t phys = 0;
85+
int flags = vmmPageStatus(virt, &phys);
86+
if(flags & PLATFORM_PAGE_ERROR) return 0;
87+
88+
// disallow using this function to gain access to kernel physical memory
89+
if(!(flags & PLATFORM_PAGE_USER)) return 0;
90+
91+
return phys | offset;
6892
}

0 commit comments

Comments
 (0)