From 24de8f53ef292afc7e0e3908292f111db12e08dd Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Sun, 15 May 2022 14:20:48 +0200
Subject: kernel/mem: remove virt_cpy2kmalloc

---
 src/kernel/mem/virt.h | 12 ------------
 src/kernel/syscalls.c |  7 ++++---
 2 files changed, 4 insertions(+), 15 deletions(-)

(limited to 'src')

diff --git a/src/kernel/mem/virt.h b/src/kernel/mem/virt.h
index b858bdd..7d95b3b 100644
--- a/src/kernel/mem/virt.h
+++ b/src/kernel/mem/virt.h
@@ -39,15 +39,3 @@ static inline bool virt_cpy_from(struct pagedir *src_pages, // virtual -> physic
 		void *dest, const void __user *src, size_t length) {
 	return virt_cpy(NULL, (userptr_t)dest, src_pages, src, length);
 }
-
-/** Copies a chunk of virtual memory to a newly kmalloc'd buffer. */
-static inline void *virt_cpy2kmalloc(struct pagedir *src_pages,
-		const void __user *src, size_t length) {
-	void *buf = kmalloc(length);
-	if (virt_cpy_from(src_pages, buf, src, length)) {
-		return buf;
-	} else {
-		kfree(buf);
-		return NULL;
-	}
-}
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index 21b9cf2..be1a6c2 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -87,8 +87,9 @@ handle_t _syscall_open(const char __user *path, int len) {
 	if (process_find_free_handle(process_current, 0) < 0)
 		SYSCALL_RETURN(-1);
 
-	path_buf = virt_cpy2kmalloc(process_current->pages, path, len);
+	path_buf = kmalloc(len);
 	if (!path_buf) goto fail;
+	if (!virt_cpy_from(process_current->pages, path_buf, path, len)) goto fail;
 
 	len = path_simplify(path_buf, path_buf, len);
 	if (len < 0) goto fail;
@@ -127,9 +128,9 @@ int _syscall_mount(handle_t hid, const char __user *path, int len) {
 	if (PATH_MAX < len)
 		SYSCALL_RETURN(-1);
 
-	// copy the path to the kernel to simplify it
-	path_buf = virt_cpy2kmalloc(process_current->pages, path, len);
+	path_buf = kmalloc(len);
 	if (!path_buf) goto fail;
+	if (!virt_cpy_from(process_current->pages, path_buf, path, len)) goto fail;
 
 	len = path_simplify(path_buf, path_buf, len);
 	if (len < 0) goto fail;
-- 
cgit v1.2.3