linux-um archives
 help / color / mirror / Atom feed
From: Benjamin Berg <benjamin@sipsolutions.net>
To: linux-um@lists.infradead.org
Cc: Benjamin Berg <benjamin.berg@intel.com>
Subject: [PATCH v9 06/10] um: Calculate stub data address relative to stub code
Date: Thu, 19 Sep 2024 14:45:07 +0200	[thread overview]
Message-ID: <20240919124511.282088-7-benjamin@sipsolutions.net> (raw)
In-Reply-To: <20240919124511.282088-1-benjamin@sipsolutions.net>

From: Benjamin Berg <benjamin.berg@intel.com>

Instead of using the current stack pointer, we can also use the current
instruction to calculate where the stub data is. With this the stub data
only needs to be aligned to a full page boundary.

Changing this has the advantage that we do not have a hole in the memory
space above the stub data (which would need to be explicitly cleared).

Another motivation to do this is that with the planned addition of a
SECCOMP based userspace the stack pointer may not be fully trustworthy.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
---
 arch/um/kernel/um_arch.c            |  6 ++----
 arch/x86/um/shared/sysdep/stub_32.h | 10 +++++++---
 arch/x86/um/shared/sysdep/stub_64.h |  8 +++++---
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c
index 8e594cda6d77..44589cbd4174 100644
--- a/arch/um/kernel/um_arch.c
+++ b/arch/um/kernel/um_arch.c
@@ -325,10 +325,8 @@ int __init linux_main(int argc, char **argv)
 		add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
 
 	host_task_size = os_get_top_address();
-	/* reserve a few pages for the stubs (taking care of data alignment) */
-	/* align the data portion */
-	BUILD_BUG_ON(!is_power_of_2(STUB_DATA_PAGES));
-	stub_start = (host_task_size - 1) & ~(STUB_DATA_PAGES * PAGE_SIZE - 1);
+	/* reserve a few pages for the stubs */
+	stub_start = host_task_size - STUB_DATA_PAGES * PAGE_SIZE;
 	/* another page for the code portion */
 	stub_start -= PAGE_SIZE;
 	host_task_size = stub_start;
diff --git a/arch/x86/um/shared/sysdep/stub_32.h b/arch/x86/um/shared/sysdep/stub_32.h
index 0b44a86dd346..631a18d0ff44 100644
--- a/arch/x86/um/shared/sysdep/stub_32.h
+++ b/arch/x86/um/shared/sysdep/stub_32.h
@@ -112,10 +112,14 @@ static __always_inline void *get_stub_data(void)
 	unsigned long ret;
 
 	asm volatile (
-		"movl %%esp,%0 ;"
-		"andl %1,%0"
+		"call _here_%=;"
+		"_here_%=:"
+		"popl %0;"
+		"andl %1, %0 ;"
+		"addl %2, %0 ;"
 		: "=a" (ret)
-		: "g" (~(STUB_DATA_PAGES * UM_KERN_PAGE_SIZE - 1)));
+		: "g" (~(UM_KERN_PAGE_SIZE - 1)),
+		  "g" (UM_KERN_PAGE_SIZE));
 
 	return (void *)ret;
 }
diff --git a/arch/x86/um/shared/sysdep/stub_64.h b/arch/x86/um/shared/sysdep/stub_64.h
index 8e4ff39dcade..17153dfd780a 100644
--- a/arch/x86/um/shared/sysdep/stub_64.h
+++ b/arch/x86/um/shared/sysdep/stub_64.h
@@ -117,10 +117,12 @@ static __always_inline void *get_stub_data(void)
 	unsigned long ret;
 
 	asm volatile (
-		"movq %%rsp,%0 ;"
-		"andq %1,%0"
+		"lea 0(%%rip), %0;"
+		"andq %1, %0 ;"
+		"addq %2, %0 ;"
 		: "=a" (ret)
-		: "g" (~(STUB_DATA_PAGES * UM_KERN_PAGE_SIZE - 1)));
+		: "g" (~(UM_KERN_PAGE_SIZE - 1)),
+		  "g" (UM_KERN_PAGE_SIZE));
 
 	return (void *)ret;
 }
-- 
2.46.0



  parent reply	other threads:[~2024-09-19 12:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-19 12:45 [PATCH v9 00/10] Increased address space for 64 bit Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 01/10] um: Add generic stub_syscall1 function Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 02/10] um: use execveat to create userspace MMs Benjamin Berg
2024-10-16  2:34   ` Nathan Chancellor
2024-10-16  8:19     ` Berg, Benjamin
2024-10-17  7:17   ` David Gow
2024-10-17  8:18     ` Johannes Berg
2024-10-17 12:05       ` Benjamin Berg
2024-10-17 23:17         ` David Gow
2024-10-17  9:01     ` Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 03/10] um: Set parent death signal for userspace process Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 04/10] um: Set parent death signal for winch thread/process Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 05/10] um: Add compile time assert that stub fits on a page Benjamin Berg
2024-09-19 12:45 ` Benjamin Berg [this message]
2024-09-19 12:45 ` [PATCH v9 07/10] um: Limit TASK_SIZE to the addressable range Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 08/10] um: Discover host_task_size from envp Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 09/10] um: clear all memory in new userspace processes Benjamin Berg
2024-09-19 12:45 ` [PATCH v9 10/10] um: Switch to 4 level page tables on 64 bit Benjamin Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240919124511.282088-7-benjamin@sipsolutions.net \
    --to=benjamin@sipsolutions.net \
    --cc=benjamin.berg@intel.com \
    --cc=linux-um@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox