Skip to content

Commit

Permalink
change mmap return MAP_FAILED on error and set errno
Browse files Browse the repository at this point in the history
  • Loading branch information
clemdiep committed Feb 19, 2025
1 parent 2df5f22 commit 3c6403c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
13 changes: 9 additions & 4 deletions so3/devices/mem.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <vfs.h>
#include <common.h>
#include <process.h>
Expand All @@ -19,11 +20,15 @@ static void *mem_mmap(int fd, addr_t virt_addr, uint32_t page_count, off_t offse

/* Detect physical or virtual address overflow */
if (((offset + remaining_size) < offset)
|| ((virt_addr + remaining_size) < virt_addr))
return NULL;
|| ((virt_addr + remaining_size) < virt_addr)) {
set_errno(EINVAL);
return MAP_FAILED;
}

if (!virt_addr)
return NULL;
if (!virt_addr) {
set_errno(EINVAL);
return MAP_FAILED;
}

pcb = current()->pcb;
BUG_ON(pcb == NULL);
Expand Down
16 changes: 12 additions & 4 deletions so3/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,16 +757,18 @@ void *do_mmap(addr_t start, size_t length, int prot, int fd, off_t offset)

gfd = vfs_get_gfd(fd);
if (-1 == gfd) {
printk("%s: could not get global fd.", __func__);
return NULL;
printk("%s: could not get global fd.\n", __func__);
set_errno(EBADF);
return MAP_FAILED;
}

mutex_lock(&vfs_lock);
fops = vfs_get_fops(gfd);
if (!fops) {
printk("%s: could not get the framebuffer fops.", __func__);
printk("%s: could not get device fops.\n", __func__);
mutex_unlock(&vfs_lock);
return NULL;
set_errno(EBADF);
return MAP_FAILED;
}

mutex_unlock(&vfs_lock);
Expand All @@ -777,6 +779,12 @@ void *do_mmap(addr_t start, size_t length, int prot, int fd, off_t offset)
page_count++;
}

if (!fops->mmap) {
printk("%s: device doesn't support mmap.\n", __func__);
set_errno(EACCES);
return MAP_FAILED;
}

/* Call the mmap fops that will do the actual mapping. */
return fops->mmap(fd, start, page_count, offset);
}
Expand Down
3 changes: 3 additions & 0 deletions so3/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
#define DT_LNK 10 /* Symbolic link */
#define DT_SOCK 12 /* Socket device */

/* Return error values */
#define MAP_FAILED ((void *) -1) /* mmap fail */

#ifndef __ASSEMBLY__

#include <types.h>
Expand Down
4 changes: 4 additions & 0 deletions usr/src/fillfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ int main(int argc, char **argv)

/* Map the framebuffer memory to a process virtual address. */
fbp = mmap(NULL, fb_size, 0, 0, fd, 0);
if (fbp == MAP_FAILED) {
printf("Couldn't map framebuffer.\n");
return -1;
}

/* Display lines of different colors. */
for (i = 0; i < vres; i++) {
Expand Down
2 changes: 1 addition & 1 deletion usr/src/stress/lvgl_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ int fb_init(void)

/* Map the framebuffer into process memory. */
fbp = mmap(NULL, fb_size, 0, 0, fd, 0);
if (!fbp) {
if (fbp == MAP_FAILED) {
printf("Couldn't map framebuffer.\n");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion usr/src/widgets/demofb.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int fb_init(void)

/* Map the framebuffer into process memory. */
fbp = mmap(NULL, fb_size, 0, 0, fd, 0);
if (!fbp) {
if (fbp == MAP_FAILED) {
printf("Couldn't map framebuffer.\n");
return -1;
}
Expand Down

0 comments on commit 3c6403c

Please sign in to comment.