Skip to content

Commit

Permalink
mm: relay mmap() to external server
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Nov 29, 2024
1 parent 87c5641 commit 1f7adbf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/include/kernel/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ uintptr_t mmio(Thread *, uintptr_t, off_t, int);
uintptr_t pcontig(Thread *, uintptr_t, off_t, int);
uintptr_t vtop(Thread *, uintptr_t);

void *mmap(Thread *, void *, size_t, int, int, int, off_t);
void *mmap(Thread *, uint64_t, void *, size_t, int, int, int, off_t);
int munmap(Thread *, void *, size_t);
int msync(Thread *, void *, size_t, int);
int msync(Thread *, uint64_t, void *, size_t, int);
63 changes: 63 additions & 0 deletions src/memory/mmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* lux - a lightweight unix-like operating system
* Omar Elghoul, 2024
*
* Core Microkernel
*/

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <kernel/memory.h>
#include <kernel/file.h>
#include <kernel/sched.h>
#include <kernel/servers.h>

/* mmap(): creates a memory mapping for a file descriptor
* params: t - calling thread
* params: id - syscall ID
* params: addr - process-suggested address
* params: len - length of the mapping
* params: prot - protection flags
* params: flags - mapping flags
* params: fd - file descriptor
* params: off - offset into file descriptor
* returns: pointer to the memory mapping, negative error code on fail
*/

void *mmap(Thread *t, uint64_t id, void *addr, size_t len, int prot, int flags,
int fd, off_t off) {
if(fd < 0 || fd > MAX_IO_DESCRIPTORS) return (void *) -EBADF;
MmapCommand *command = calloc(1, sizeof(MmapCommand));
if(!command) return (void *) -ENOMEM;

Process *p = getProcess(t->pid);
if(!p) return (void *) -ESRCH;

IODescriptor *io = &p->io[fd];
if(io->valid || !io->data) return (void *) -EBADF;
if(io->type != IO_FILE) return (void *) -ENODEV;

FileDescriptor *f = (FileDescriptor *) io->data;

command->header.header.command = COMMAND_MMAP;
command->header.header.length = sizeof(MmapCommand);
command->header.id = id;
command->uid = p->user;
command->gid = p->group;
command->position = f->position;
command->openFlags = io->flags;
command->id = f->id;
strcpy(command->device, f->device);
strcpy(command->path, f->abspath);

command->len = len;
command->prot = prot;
command->flags = flags;
command->off = off;

int status = requestServer(t, command);
free(command);
return (void *) (intptr_t) status;
}

0 comments on commit 1f7adbf

Please sign in to comment.