Skip to content

Commit

Permalink
mmap: change interface to be posix compliant
Browse files Browse the repository at this point in the history
JIRA: RTOS-665
  • Loading branch information
badochov committed Nov 2, 2023
1 parent 4d32462 commit 4855a91
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 31 deletions.
32 changes: 21 additions & 11 deletions include/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,27 @@
#define _PHOENIX_MMAN_H_


enum { MAP_NONE = 0x0, MAP_NEEDSCOPY = 0x1, MAP_UNCACHED = 0x2, MAP_DEVICE = 0x4, MAP_NOINHERIT = 0x8,
MAP_SHARED = 0x0, MAP_PRIVATE = 0x0, MAP_FIXED = 0x0, MAP_ANONYMOUS = 0x0 };


enum { PROT_NONE = 0x0, PROT_READ = 0x1, PROT_WRITE = 0x2, PROT_EXEC = 0x4, PROT_USER = 0x8 };


/* Predefined oids */
#define OID_NULL NULL
#define OID_PHYSMEM (void *)-1
#define OID_CONTIGUOUS (void *)-2
#define MAP_NONE 0x0
#define MAP_NEEDSCOPY 0x1
#define MAP_UNCACHED 0x2
#define MAP_DEVICE 0x4
#define MAP_NOINHERIT 0x8
#define MAP_PHYSMEM 0x10
#define MAP_CONTIGUOUS 0x20
#define MAP_ANONYMOUS 0x40
#define MAP_SHARED 0x0
#define MAP_PRIVATE 0x0
#define MAP_FIXED 0x0


#define PROT_NONE 0x0
#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define PROT_EXEC 0x4
#define PROT_USER 0x8


#define MAP_FAILED (void *)-1


#endif
18 changes: 18 additions & 0 deletions posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,24 @@ ssize_t posix_write(int fildes, void *buf, size_t nbyte)
}


int posix_get_oid(int fildes, oid_t *oid)
{
open_file_t *f;
int err;

err = posix_getOpenFile(fildes, &f);
if (err < 0) {
return err;
}

hal_memcpy(oid, &f->oid, sizeof(oid_t));

posix_fileDeref(f);

return EOK;
}


int posix_dup(int fildes)
{
TRACE("dup(%d)", fildes);
Expand Down
3 changes: 3 additions & 0 deletions posix/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ extern ssize_t posix_read(int fildes, void *buf, size_t nbyte);
extern ssize_t posix_write(int fildes, void *buf, size_t nbyte);


extern int posix_get_oid(int fildes, oid_t *oid);


extern int posix_dup(int fildes);


Expand Down
7 changes: 5 additions & 2 deletions proc/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#define CEIL(x) (((x) + SIZE_PAGE - 1) & ~(SIZE_PAGE - 1))


/* clang-format off */
enum { msg_rejected = -1, msg_waiting = 0, msg_received, msg_responded };
/* clang-format on */



struct {
Expand Down Expand Up @@ -115,7 +118,7 @@ static void *msg_map(int dir, kmsg_t *kmsg, void *data, size_t size, process_t *
return NULL;
}

vaddr = vm_mmap(msg_common.kmap, NULL, NULL, SIZE_PAGE, PROT_READ | PROT_WRITE, OID_PHYSMEM, bpa, flags);
vaddr = vm_mmap(msg_common.kmap, NULL, NULL, SIZE_PAGE, PROT_READ | PROT_WRITE, (void *)-1, bpa, flags);
ml->bvaddr = vaddr;
if (vaddr == NULL) {
return NULL;
Expand Down Expand Up @@ -159,7 +162,7 @@ static void *msg_map(int dir, kmsg_t *kmsg, void *data, size_t size, process_t *
nep = nbp;
}

vaddr = vm_mmap(msg_common.kmap, NULL, NULL, SIZE_PAGE, PROT_READ | PROT_WRITE, OID_PHYSMEM, epa, flags);
vaddr = vm_mmap(msg_common.kmap, NULL, NULL, SIZE_PAGE, PROT_READ | PROT_WRITE, (void *)-1, epa, flags);
ml->evaddr = vaddr;
if (vaddr == NULL) {
return NULL;
Expand Down
42 changes: 26 additions & 16 deletions syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,49 @@ void *syscalls_mmap(void *ustack)
{
void *vaddr;
size_t size;
int prot, flags;
oid_t *oid;
int prot, flags, fildes;
off_t offs;
vm_object_t *o;
oid_t oid;

GETFROMSTACK(ustack, void *, vaddr, 0);
GETFROMSTACK(ustack, size_t, size, 1);
GETFROMSTACK(ustack, int, prot, 2);
GETFROMSTACK(ustack, int, flags, 3);
GETFROMSTACK(ustack, oid_t *, oid, 4);
GETFROMSTACK(ustack, int, fildes, 4);
GETFROMSTACK(ustack, off_t, offs, 5);

if (oid == OID_NULL) {
o = NULL;
}
else if (oid == OID_PHYSMEM) {
o = (void *)-1;
}
else if (oid == OID_CONTIGUOUS) {
o = vm_objectContiguous(size);
if (o == NULL) {
return (void *)-1;
if (fildes == -1) {
if ((flags & MAP_PHYSMEM) != 0) {
o = (void *)-1;
}
else if ((flags & MAP_CONTIGUOUS) != 0) {
o = vm_objectContiguous(size);
if (o == NULL) {
return MAP_FAILED;
}
}
else if ((flags & MAP_ANONYMOUS) != 0){
o = NULL;
}
else {
return MAP_FAILED;
}
}
else if (vm_objectGet(&o, *oid) != EOK) {
return NULL;
else {
if (posix_get_oid(fildes, &oid) != EOK) {
return MAP_FAILED;
}
if (vm_objectGet(&o, oid) != EOK) {
return MAP_FAILED;
}
}

vaddr = vm_mmap(proc_current()->process->mapp, vaddr, NULL, size, PROT_USER | prot, o, (o == NULL) ? -1 : offs, flags);
vm_objectPut(o);

if (vaddr == NULL) {
return (void *)-1;
return MAP_FAILED;
}

return vaddr;
Expand Down
6 changes: 4 additions & 2 deletions vm/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,9 @@ void *_vm_mmap(vm_map_t *map, void *vaddr, page_t *p, size_t size, u8 prot, vm_o
thread_t *current;
map_entry_t *e;

if (!size || (size & (SIZE_PAGE - 1)))
if ((size & (SIZE_PAGE - 1)) != 0) {
return NULL;
}

/* NULL page indicates that proc sybsystem is ready */
if (p == NULL && (current = proc_current()) != NULL)
Expand Down Expand Up @@ -508,8 +509,9 @@ void *_vm_mmap(vm_map_t *map, void *vaddr, page_t *p, size_t size, u8 prot, vm_o

void *vm_mmap(vm_map_t *map, void *vaddr, page_t *p, size_t size, u8 prot, vm_object_t *o, off_t offs, u8 flags)
{
if (map == NULL)
if (map == NULL) {
map = map_common.kmap;
}

proc_lockSet(&map->lock);
vaddr = _vm_mmap(map, vaddr, p, size, prot, o, offs, flags);
Expand Down

0 comments on commit 4855a91

Please sign in to comment.