mmap syscall declaration

This commit is contained in:
Mathieu Maret 2023-03-24 23:43:09 +01:00 committed by Mathieu Maret
parent 1e3be650f2
commit 8af3ba0762
11 changed files with 87 additions and 2 deletions

View File

@ -28,6 +28,7 @@
#include "time.h" #include "time.h"
#include "types.h" #include "types.h"
#include "vga.h" #include "vga.h"
#include "zero.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit))) #define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))

View File

@ -5,6 +5,7 @@
#include "stdarg.h" #include "stdarg.h"
#include "thread.h" #include "thread.h"
#include "types.h" #include "types.h"
#include "uaccess.h"
#include "uaddrspace.h" #include "uaddrspace.h"
int syscallExecute(int syscallId, const struct cpu_state *userCtx) int syscallExecute(int syscallId, const struct cpu_state *userCtx)
@ -55,6 +56,24 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx)
threadChangeCurrentContext(NULL); threadChangeCurrentContext(NULL);
break; break;
} }
case SYSCALL_ID_MMAP: {
struct uAddrSpace *as;
uaddr_t uaddr;
size_t size;
uint32_t rights;
uint32_t flags;
uaddr_t userPath;
char path[256];
as = processGetAddrSpace(getCurrentThread()->process);
ret = syscallGet5args(userCtx, (unsigned int *)&uaddr, (unsigned int *)&size, (unsigned int *)&rights,
(unsigned int *)&flags, (unsigned int *)&userPath);
strzcpyFromUser((vaddr_t *)path, (uaddr_t *)userPath, sizeof(path));
printf("Trying mmap for device %s\n", path);
(void)as;
break;
}
default: default:
printf("Unknon syscall id %d\n", syscallId); printf("Unknon syscall id %d\n", syscallId);
ret = -ENOENT; ret = -ENOENT;

View File

@ -9,6 +9,7 @@
#define SYSCALL_ID_READ 4 #define SYSCALL_ID_READ 4
#define SYSCALL_ID_TEST 5 #define SYSCALL_ID_TEST 5
#define SYSCALL_ID_BRK 6 #define SYSCALL_ID_BRK 6
#define SYSCALL_ID_MMAP 7
#ifdef __KERNEL__ #ifdef __KERNEL__
int syscallExecute(int syscallId, const struct cpu_state *user_ctx); int syscallExecute(int syscallId, const struct cpu_state *user_ctx);

View File

@ -1,10 +1,12 @@
#include "uaccess.h"
#include "assert.h" #include "assert.h"
#include "errno.h" #include "errno.h"
#include "minmax.h"
#include "mmuContext.h" #include "mmuContext.h"
#include "paging.h" #include "paging.h"
#include "process.h" #include "process.h"
#include "thread.h" #include "thread.h"
#include "uaccess.h" #include "types.h"
static int bindtoUserContext() static int bindtoUserContext()
{ {
@ -52,3 +54,37 @@ int memcpyFromUser(vaddr_t to, uaddr_t from, size_t size)
return memcpyUserMemNoCheck(to, from, size); return memcpyUserMemNoCheck(to, from, size);
} }
static int strzcpyFromUserNoCheck(char *to, char *from, size_t size)
{
int ret;
ret = bindtoUserContext();
if (ret != 0)
return ret;
for (unsigned int i = 0; i < size; i++) {
to[i] = from[i];
if (from[i] == '\0')
break;
}
if (size > 0)
to[size - 1] = '\0';
ret = unbindUserContext();
if (ret != 0)
return ret;
return size;
}
int strzcpyFromUser(vaddr_t *to, uaddr_t *from, size_t size)
{
if ((uint)from < PAGING_BASE_USER_ADDRESS)
return -EPERM;
if ((uint)from > PAGING_TOP_USER_ADDRESS - size)
return -EPERM;
return strzcpyFromUserNoCheck((char *)to, (char *)from, size);
}

View File

@ -3,3 +3,4 @@
#include "stdarg.h" #include "stdarg.h"
int memcpyFromUser(vaddr_t to, uaddr_t from, size_t size); int memcpyFromUser(vaddr_t to, uaddr_t from, size_t size);
int strzcpyFromUser(vaddr_t *to, uaddr_t *from, size_t size);

View File

@ -2,7 +2,6 @@ label: dos
label-id: 0x9ec19bcc label-id: 0x9ec19bcc
device: disk.img device: disk.img
unit: sectors unit: sectors
sector-size: 512
disk.img1 : start= 2048, size= 32768, type=6, bootable disk.img1 : start= 2048, size= 32768, type=6, bootable
disk.img2 : start= 34816, size= 30720, type=83 disk.img2 : start= 34816, size= 30720, type=83

9
drivers/zero.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "types.h"
#include <stddef.h>
#include <stdint.h>
int zeroSetup();
int zeroMmap(uaddr_t *uaddr, size_t size, uint32_t rights, uint32_t flags);

View File

@ -9,6 +9,7 @@
#define SYSCALL_ID_READ 4 #define SYSCALL_ID_READ 4
#define SYSCALL_ID_TEST 5 #define SYSCALL_ID_TEST 5
#define SYSCALL_ID_BRK 6 #define SYSCALL_ID_BRK 6
#define SYSCALL_ID_MMAP 7
#ifdef __KERNEL__ #ifdef __KERNEL__
int syscallExecute(int syscallId, const struct cpu_state *user_ctx); int syscallExecute(int syscallId, const struct cpu_state *user_ctx);

View File

@ -725,3 +725,7 @@ void *realloc(void *ptr, size_t size)
return new_ptr; return new_ptr;
} }
void *mmap(void *addr, size_t len, int prot, int flags, char *path){
return (void *)syscall5(SYSCALL_ID_MMAP, (unsigned int)addr, len, prot, flags, (unsigned int)path);
}

View File

@ -36,6 +36,7 @@ int putc(const int c);
int vsnprintf(char *str, size_t size, const char *format, va_list ap) __attribute__ ((__format__ (printf, 3, 0))); int vsnprintf(char *str, size_t size, const char *format, va_list ap) __attribute__ ((__format__ (printf, 3, 0)));
int vprintf(const char *format, va_list ap) __attribute__ ((__format__ (printf, 1, 0))); int vprintf(const char *format, va_list ap) __attribute__ ((__format__ (printf, 1, 0)));
int printf(const char *format, ...) __attribute__ ((__format__ (printf, 1, 2))); int printf(const char *format, ...) __attribute__ ((__format__ (printf, 1, 2)));
void *mmap(void *addr, size_t len, int prot, int flags, char *path);
int asprintf(char **strp, const char *fmt, ...) __attribute__ ((__format__ (printf, 2, 3))); int asprintf(char **strp, const char *fmt, ...) __attribute__ ((__format__ (printf, 2, 3)));
int vasprintf(char **strp, const char *fmt, va_list ap) __attribute__ ((__format__ (printf, 2, 0))); int vasprintf(char **strp, const char *fmt, va_list ap) __attribute__ ((__format__ (printf, 2, 0)));

View File

@ -55,6 +55,15 @@ int func_alloc()
return 0; return 0;
} }
int func_mmap()
{
char *path ="/dev/zero";
mmap(0, 4096, 0, 0, path);
return 0;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
(void)argc; (void)argc;
@ -85,6 +94,10 @@ int main(int argc, char *argv[])
func_tiny(); func_tiny();
continue; continue;
} }
if (strcmp(buf, "mmap") == 0) {
func_mmap();
continue;
}
} }
return 0; return 0;
} }