Skip to content

Commit

Permalink
mm: handle mmap() for device files
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Nov 30, 2024
1 parent 7016112 commit d322b92
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/memory/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <kernel/logger.h>
#include <kernel/memory.h>
#include <kernel/file.h>
#include <kernel/sched.h>
#include <kernel/servers.h>
#include <platform/platform.h>

/* mmap(): creates a memory mapping for a file descriptor
* params: t - calling thread
Expand Down Expand Up @@ -60,4 +62,52 @@ void *mmap(Thread *t, uint64_t id, void *addr, size_t len, int prot, int flags,
int status = requestServer(t, command);
free(command);
return (void *) (intptr_t) status;
}

/* mmapHandle(): handler for mmap() response message from a driver
* params: msg - response message
* params: req - relevant system call request
* returns: nothing
*/

void mmapHandle(MmapCommand *msg, SyscallRequest *req) {
struct MmapSyscallParams *p = (struct MmapSyscallParams *) req->params[0];

if(msg->responseType) {
/* memory-mapped device file */
size_t pageCount = (msg->len+PAGE_SIZE-1) / PAGE_SIZE;
int pageFlags = PLATFORM_PAGE_PRESENT | PLATFORM_PAGE_USER;
if(msg->prot & PROT_WRITE) pageFlags |= PLATFORM_PAGE_WRITE;
if(msg->prot & PROT_EXEC) pageFlags |= PLATFORM_PAGE_EXEC;

// allocate one extra page for the mmap() header so we can unmap later
// the difference between this and malloc() is that this must always be
// page-aligned to comply with POSIX
uintptr_t base = vmmAllocate(USER_MMIO_BASE, USER_LIMIT_ADDRESS, pageCount+1, VMM_USER | VMM_WRITE);
if(!base) {
req->ret = -ENOMEM;
return;
}

MmapHeader *header = (MmapHeader *) base;
header->device = true;
header->fd = p->fd;
header->flags = p->flags;
header->length = msg->len;
header->offset = msg->off;
header->prot = msg->prot;
header->pid = req->thread->pid;
header->tid = req->thread->tid;

base += PAGE_SIZE; // skip over to the next page

for(size_t i = 0; i < pageCount; i++)
platformMapPage(base + (i*PAGE_SIZE), msg->mmio + (i*PAGE_SIZE), pageFlags);

req->ret = base;
} else {
/* memory-mapped regular file */
KWARN("TODO: implement memory-mapped regular files\n");
req->ret = -ENOSYS;
}
}

0 comments on commit d322b92

Please sign in to comment.