Skip to content

Commit 6ee6332

Browse files
committed
mm: implemented mmio()
1 parent 9cd40d2 commit 6ee6332

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/memory/mmio.c

+25
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,30 @@
2929
*/
3030

3131
uintptr_t mmio(Thread *t, uintptr_t addr, off_t count, int flags) {
32+
off_t offset = addr & ~(PAGE_SIZE-1);
33+
size_t pageCount = (count + PAGE_SIZE - 1) / PAGE_SIZE;
34+
if(addr & ~(PAGE_SIZE-1)) pageCount++;
3235

36+
if(flags & MMIO_ENABLE) {
37+
// creating a memory mapping
38+
int pageFlags = PLATFORM_PAGE_PRESENT | PLATFORM_PAGE_USER;
39+
if(flags & MMIO_W) pageFlags |= PLATFORM_PAGE_WRITE;
40+
if(flags & MMIO_X) pageFlags |= PLATFORM_PAGE_EXEC;
41+
42+
uintptr_t virt = vmmAllocate(USER_MMIO_BASE, USER_LIMIT_ADDRESS, pageCount, VMM_USER);
43+
if(!virt) return 0;
44+
45+
for(int i = 0; i < pageCount; i++)
46+
platformMapPage(virt + (i*PAGE_SIZE), addr + (i*PAGE_SIZE), pageFlags);
47+
48+
return virt | offset;
49+
} else {
50+
// deleting a memory mapping
51+
if(addr < USER_MMIO_BASE) return addr;
52+
53+
for(int i = 0; i < pageCount; i++)
54+
platformMapPage(addr + (i * PAGE_SIZE), 0, 0);
55+
56+
return 0;
57+
}
3358
}

0 commit comments

Comments
 (0)