From 15227e5955320c96faed1d75a030fb844e82a53f Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Thu, 2 Nov 2023 14:44:55 +0100 Subject: [PATCH] mmap: change interface to be posix compliant JIRA: RTOS-665 --- include/mman.h | 32 +++++++++++++++++++++----------- posix/posix.c | 18 ++++++++++++++++++ posix/posix.h | 3 +++ proc/msg.c | 7 +++++-- syscalls.c | 41 +++++++++++++++++++++++++---------------- vm/map.c | 6 ++++-- 6 files changed, 76 insertions(+), 31 deletions(-) diff --git a/include/mman.h b/include/mman.h index c788b9078..c09d43abd 100644 --- a/include/mman.h +++ b/include/mman.h @@ -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 diff --git a/posix/posix.c b/posix/posix.c index ed2708e0b..1b2701f3a 100644 --- a/posix/posix.c +++ b/posix/posix.c @@ -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); diff --git a/posix/posix.h b/posix/posix.h index 2225156de..75ca48ba5 100644 --- a/posix/posix.h +++ b/posix/posix.h @@ -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); diff --git a/proc/msg.c b/proc/msg.c index 532f06e0f..8ed41e968 100644 --- a/proc/msg.c +++ b/proc/msg.c @@ -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 { @@ -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; @@ -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; diff --git a/syscalls.c b/syscalls.c index c812456fc..2a2a32981 100644 --- a/syscalls.c +++ b/syscalls.c @@ -52,39 +52,48 @@ 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; diff --git a/vm/map.c b/vm/map.c index 821b2574b..bff5a4c6d 100644 --- a/vm/map.c +++ b/vm/map.c @@ -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) @@ -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);