feat: brk and sbrk (mmap based)
authorMinep <zelong56@gmail.com>
Fri, 6 Jan 2023 00:47:54 +0000 (00:47 +0000)
committerMinep <zelong56@gmail.com>
Fri, 6 Jan 2023 00:47:54 +0000 (00:47 +0000)
feat: environ access
refactor: status code user space port

12 files changed:
lunaix-os/includes/lunaix/mm/mmap.h
lunaix-os/kernel/loader/exec.c
lunaix-os/kernel/mm/dmm.c
lunaix-os/kernel/mm/mmap.c
lunaix-os/usr/api/unistd.c
lunaix-os/usr/includes/errno.h
lunaix-os/usr/includes/string.h
lunaix-os/usr/includes/sys/ioctl.h
lunaix-os/usr/includes/unistd.h
lunaix-os/usr/libc/string.c
lunaix-os/usr/uinit.c
lunaix-os/usr/uwrap.S

index ff1e443c9c8d5ab7178a2912bc12cbcfc9e56bff..f85302e339b8d14056b9a1b3661608c6ff933ace 100644 (file)
@@ -17,6 +17,11 @@ struct mmap_param
     u32_t type;           // region type
 };
 
     u32_t type;           // region type
 };
 
+int
+mem_adjust_inplace(vm_regions_t* regions,
+                   struct mm_region* region,
+                   ptr_t newend);
+
 int
 mem_map(void** addr_out,
         struct mm_region** created,
 int
 mem_map(void** addr_out,
         struct mm_region** created,
index 0105753a614492e74baa58b12b75d4904b9ac598..331cacca5283df528a73b79b2e1d0dca65275061 100644 (file)
@@ -7,6 +7,7 @@
 #include <lunaix/spike.h>
 #include <lunaix/status.h>
 #include <lunaix/syscall.h>
 #include <lunaix/spike.h>
 #include <lunaix/status.h>
 #include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
 
 #include <klibc/string.h>
 
 
 #include <klibc/string.h>
 
@@ -50,7 +51,7 @@ __exec_remap_heap(struct ld_param* param, struct proc_mm* pvms)
                                     .flags = MAP_ANON | MAP_PRIVATE,
                                     .type = REGION_TYPE_HEAP,
                                     .proct = PROT_READ | PROT_WRITE,
                                     .flags = MAP_ANON | MAP_PRIVATE,
                                     .type = REGION_TYPE_HEAP,
                                     .proct = PROT_READ | PROT_WRITE,
-                                    .mlen = DEFAULT_HEAP_PAGES * PG_SIZE };
+                                    .mlen = PG_SIZE };
     int status = 0;
     struct mm_region* heap;
     if ((status = mem_map(NULL, &heap, param->info.end, NULL, &map_param))) {
     int status = 0;
     struct mm_region* heap;
     if ((status = mem_map(NULL, &heap, param->info.end, NULL, &map_param))) {
@@ -192,5 +193,5 @@ __DEFINE_LXSYSCALL3(int,
     // return so execve 'will not return' from the perspective of it's invoker
 
 done:
     // return so execve 'will not return' from the perspective of it's invoker
 
 done:
-    return errno;
+    return DO_STATUS(errno);
 }
\ No newline at end of file
 }
\ No newline at end of file
index 188bb0635eebf9a5771d979c55d3e04f642a74e0..cc75bd9be9b4d5f32b782b28733fbf7be54fbd31 100644 (file)
@@ -1,18 +1,30 @@
-#include <lunaix/mm/page.h>
-#include <lunaix/mm/vmm.h>
+#include <lunaix/mm/mmap.h>
+#include <lunaix/process.h>
 #include <lunaix/status.h>
 
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
 #include <lunaix/status.h>
 
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
 
 
-__DEFINE_LXSYSCALL1(int, sbrk, size_t, size)
+__DEFINE_LXSYSCALL1(void*, sbrk, ssize_t, incr)
 {
 {
-    // TODO mem_remap to expand heap region
-    return 0;
+    struct proc_mm* pvms = &__current->mm;
+    struct mm_region* heap = pvms->heap;
+
+    assert(heap);
+    int err = mem_adjust_inplace(&pvms->regions, heap, heap->end + incr);
+    if (err) {
+        return (void*)DO_STATUS(err);
+    }
+    return (void*)heap->end;
 }
 
 }
 
-__DEFINE_LXSYSCALL1(void*, brk, void*, addr)
+__DEFINE_LXSYSCALL1(int, brk, void*, addr)
 {
 {
-    // TODO mem_remap to expand heap region
-    return 0;
+    struct proc_mm* pvms = &__current->mm;
+    struct mm_region* heap = pvms->heap;
+
+    assert(heap);
+    int err = mem_adjust_inplace(&pvms->regions, heap, (ptr_t)addr);
+    return DO_STATUS(err);
 }
\ No newline at end of file
 }
\ No newline at end of file
index 3a8677dd6cdd92b60c8cc803839f1945090e45e3..f700fd533179d9528ff84191e6f14402d0f5eca2 100644 (file)
@@ -11,9 +11,8 @@
 #define BS_SIZE (KERNEL_MM_BASE - UMMAP_START)
 
 int
 #define BS_SIZE (KERNEL_MM_BASE - UMMAP_START)
 
 int
-mem_has_overlap(vm_regions_t* regions, ptr_t start, size_t len)
+mem_has_overlap(vm_regions_t* regions, ptr_t start, ptr_t end)
 {
 {
-    ptr_t end = start + end - 1;
     struct mm_region *pos, *n;
     llist_for_each(pos, n, regions, head)
     {
     struct mm_region *pos, *n;
     llist_for_each(pos, n, regions, head)
     {
@@ -33,6 +32,29 @@ mem_has_overlap(vm_regions_t* regions, ptr_t start, size_t len)
     return 0;
 }
 
     return 0;
 }
 
+int
+mem_adjust_inplace(vm_regions_t* regions,
+                   struct mm_region* region,
+                   ptr_t newend)
+{
+    ssize_t len = newend - region->start;
+    if (len == 0) {
+        return 0;
+    }
+
+    if (len < 0) {
+        return EINVAL;
+    }
+
+    if (mem_has_overlap(regions, region->start, newend)) {
+        return ENOMEM;
+    }
+
+    region->end = newend;
+
+    return 0;
+}
+
 int
 mem_map(void** addr_out,
         struct mm_region** created,
 int
 mem_map(void** addr_out,
         struct mm_region** created,
@@ -48,7 +70,7 @@ mem_map(void** addr_out,
     vm_regions_t* vm_regions = &param->pvms->regions;
 
     if ((param->flags & MAP_FIXED_NOREPLACE)) {
     vm_regions_t* vm_regions = &param->pvms->regions;
 
     if ((param->flags & MAP_FIXED_NOREPLACE)) {
-        if (mem_has_overlap(vm_regions, found_loc, param->mlen)) {
+        if (mem_has_overlap(vm_regions, found_loc, param->mlen + found_loc)) {
             return EEXIST;
         }
         goto found;
             return EEXIST;
         }
         goto found;
index e8a4a0a0b0780aaf07c1956b3218ced4e38c5dab..088c33582b78da9602279980b25847d3a2c780eb 100644 (file)
@@ -3,9 +3,9 @@
 
 __LXSYSCALL(pid_t, fork)
 
 
 __LXSYSCALL(pid_t, fork)
 
-__LXSYSCALL1(int, sbrk, void*, addr)
+__LXSYSCALL1(int, brk, void*, addr)
 
 
-__LXSYSCALL1(void*, brk, unsigned long, size)
+__LXSYSCALL1(void*, sbrk, ssize_t, size)
 
 __LXSYSCALL(pid_t, getpid)
 
 
 __LXSYSCALL(pid_t, getpid)
 
index 5b9b51bbf24e9028efbefb0b8b3e7d4a461fe31e..f3c5347f87666e9e6c55f3ccf5e34bc6b8fc617e 100644 (file)
@@ -1,6 +1,31 @@
 #ifndef __LUNAIX_SYS_ERRNO_H
 #define __LUNAIX_SYS_ERRNO_H
 
 #ifndef __LUNAIX_SYS_ERRNO_H
 #define __LUNAIX_SYS_ERRNO_H
 
+#define EINVAL -(6)
+#define EINTR -(7)
+#define EMFILE -8
+#define ENOENT -9
+#define ENAMETOOLONG -10
+#define ENOTDIR -11
+#define EEXIST -12
+#define EBADF -13
+#define ENOTSUP -14
+#define EIO -15
+#define ELOOP -16
+#define ENOTEMPTY -17
+#define EROFS -18
+#define EISDIR -19
+#define EBUSY -20
+#define EXDEV -21
+#define ENODEV -22
+#define ERANGE -23
+#define ENOMEM -(3)
+#define ENOTDEV -24
+#define EOVERFLOW -25
+#define ENOTBLK -26
+#define ENOEXEC -27
+#define E2BIG -28
+
 int
 geterrno();
 
 int
 geterrno();
 
index 4c45ce290d8f1c3a720170070bed2f9a084a505f..2b5799a15fc100ac85b50ce3768d40fc906b6201 100644 (file)
@@ -15,4 +15,7 @@ strncpy(char* dest, const char* src, size_t n);
 const char*
 strchr(const char* str, int character);
 
 const char*
 strchr(const char* str, int character);
 
+char*
+strcpy(char* dest, const char* src);
+
 #endif /* __LUNAIX_STRING_H */
 #endif /* __LUNAIX_STRING_H */
index 827cd44d112de32b4c35ebf4be772162aa079bc2..58c681a9720b42bf4cde63b9b6d0d60fd65d05dd 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef __LUNAIX_SYS_IOCTL_H
 #define __LUNAIX_SYS_IOCTL_H
 
 #ifndef __LUNAIX_SYS_IOCTL_H
 #define __LUNAIX_SYS_IOCTL_H
 
+#include <sys/ioctl_defs.h>
+
 int
 ioctl(int fd, int req, ...);
 
 int
 ioctl(int fd, int req, ...);
 
index 90740a05df5ae5af0d8623e66ba210967b3c6a53..8420ffac3954cc93443eaf37f67e03e4510149c1 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <sys/types.h>
 
 
 #include <sys/types.h>
 
+extern const char** environ;
+
 pid_t
 fork();
 
 pid_t
 fork();
 
@@ -19,10 +21,10 @@ pid_t
 setpgid(pid_t pid, pid_t pgid);
 
 int
 setpgid(pid_t pid, pid_t pgid);
 
 int
-sbrk(void* addr);
+brk(void* addr);
 
 void*
 
 void*
-brk(size_t size);
+sbrk(ssize_t size);
 
 void
 _exit(int status);
 
 void
 _exit(int status);
index 2e34dc7460cfa65a2a22d2c606c08a6649a43fd3..280cbbe3c8b18fea2440d3010293a1aa3f3acfd6 100644 (file)
@@ -41,4 +41,17 @@ strncpy(char* dest, const char* src, size_t n)
     while (i <= n)
         dest[i++] = 0;
     return dest;
     while (i <= n)
         dest[i++] = 0;
     return dest;
-}
\ No newline at end of file
+}
+
+char*
+strcpy(char* dest, const char* src)
+{
+    char c;
+    unsigned int i = 0;
+    while ((c = src[i])) {
+        dest[i] = c;
+        i++;
+    }
+    dest[i] = '\0';
+    return dest;
+}
index ab383c2478c53b44a4bfebfe0b36908726ca6006..03527431ae08e2885ad52f163544faeaac09f6ea 100644 (file)
@@ -6,5 +6,8 @@ usr_pre_init(struct usr_exec_param* param)
 {
     // TODO some inits before executing user program
 
 {
     // TODO some inits before executing user program
 
+    extern ptr_t environ;
+    environ = (ptr_t)param->envp;
+
     return 0;
 }
\ No newline at end of file
     return 0;
 }
\ No newline at end of file
index 1f077228da8d889b3829a6c00b875686b98860a5..5708463653a63d0564a37ba16f3d3899dabc68b9 100644 (file)
@@ -1,6 +1,11 @@
 #define __ASM__
 #include <lunaix/syscall.h>
 
 #define __ASM__
 #include <lunaix/syscall.h>
 
+.section .data
+    .global environ
+    environ:
+        .long 0
+
 .section .text
     .global _u_start
     _u_start:
 .section .text
     .global _u_start
     _u_start: