nommu UML
Subsystems (7)
v4 cover nommu UML
This is a series of patches of nommu arch addition to UML. It would be nice to ask comments/opinions on this.
There are still several limitations/issues which we already found; here is the list of those issues.
- memory mapped by loadable modules are not distinguished from userspace memory.
-- Hajime
v4:
- add arch/um/nommu, arch/x86/um/nommu to contain !MMU specific codes
- remove zpoline patch
- drop binfmt_elf_fdpic patch
- reduce ifndef CONFIG_MMU if possible
- split to elf header cleanup patch [01/13]
- fix kernel test robot warnings [06/13]
- fix coding styles [07/13]
- move task_top_of_stack definition [05/13]
v3:
- https://lore.kernel.org/linux-um/[email protected]/
- add seccomp-based syscall hook in addition to zpoline [06/13]
- remove RFC, add a line to MAINTAINERS file
- fix kernel test robot warnings [02/13,08/13,10/13]
- add base-commit tag to cover letter
- pull the latest uml/next
- clean up SIGSEGV handling [10/13]
- detect fsgsbase availability with elf aux vector [08/13]
- simplify vdso code with macros [09/13]
RFC v2:
- https://lore.kernel.org/linux-um/[email protected]/
- base branch is now uml/linux.git instead of torvalds/linux.git.
- reorganize the patch series to clean up
- fixed various coding styles issues
- clean up exec code path [07/13]
- fixed the crash/SIGSEGV case on userspace programs [10/13]
- add seccomp filter to limit syscall caller address [06/13]
- detect fsgsbase availability with sigsetjmp/siglongjmp [08/13]
- removes unrelated changes
- removes unneeded ifndef CONFIG_MMU
- convert UML_CONFIG_MMU to CONFIG_MMU as using uml/linux.git
- proposed a patch of maple-tree issue (resolving a limitation in RFC v1)
https://lore.kernel.org/linux-mm/[email protected]/
RFC:
- https://lore.kernel.org/linux-um/[email protected]/
Hajime Tazaki (13):
x86/um: clean up elf specific definitions
x86/um: nommu: elf loader for fdpic
um: decouple MMU specific code from the common part
um: nommu: memory handling
x86/um: nommu: syscall handling
um: nommu: seccomp syscalls hook
x86/um: nommu: process/thread handling
um: nommu: configure fs register on host syscall invocation x86/um/vdso: nommu: vdso memory update
x86/um: nommu: signal handling
um: change machine name for uname output
um: nommu: add documentation of nommu UML
um: nommu: plug nommu code into build system
Documentation/virt/uml/nommu-uml.rst | 177 ++++++++++++++++++++++ MAINTAINERS | 1 + arch/um/Kconfig | 14 +- arch/um/Makefile | 10 ++ arch/um/configs/x86_64_nommu_defconfig | 64 ++++++++ arch/um/include/asm/Kbuild | 1 + arch/um/include/asm/futex.h | 4 + arch/um/include/asm/mmu.h | 8 + arch/um/include/asm/mmu_context.h | 2 + arch/um/include/asm/ptrace-generic.h | 6 + arch/um/include/asm/uaccess.h | 7 +- arch/um/include/shared/kern_util.h | 8 + arch/um/include/shared/mem.h | 8 + arch/um/include/shared/os.h | 16 ++ arch/um/kernel/Makefile | 5 +- arch/um/kernel/mem-pgtable.c | 175 ++++++++++++++++++++++ arch/um/kernel/mem.c | 152 +------------------ arch/um/kernel/process.c | 25 ++++ arch/um/kernel/skas/process.c | 27 ---- arch/um/kernel/um_arch.c | 3 + arch/um/nommu/Makefile | 3 + arch/um/nommu/os-Linux/Makefile | 7 + arch/um/nommu/os-Linux/signal.c | 15 ++ arch/um/nommu/trap.c | 188 ++++++++++++++++++++++++ arch/um/os-Linux/Makefile | 8 +- arch/um/os-Linux/internal.h | 5 + arch/um/os-Linux/mem.c | 4 + arch/um/os-Linux/process.c | 149 ++++++++++++++++++- arch/um/os-Linux/seccomp.c | 87 +++++++++++ arch/um/os-Linux/signal.c | 12 ++ arch/um/os-Linux/skas/process.c | 132 ----------------- arch/um/os-Linux/start_up.c | 20 +++ arch/um/os-Linux/util.c | 3 +- arch/x86/um/Makefile | 7 +- arch/x86/um/asm/elf.h | 8 +- arch/x86/um/asm/module.h | 24 --- arch/x86/um/nommu/Makefile | 8 + arch/x86/um/nommu/do_syscall_64.c | 100 +++++++++++++ arch/x86/um/nommu/entry_64.S | 111 ++++++++++++++ arch/x86/um/nommu/os-Linux/Makefile | 6 + arch/x86/um/nommu/os-Linux/mcontext.c | 26 ++++ arch/x86/um/nommu/process.c | 35 +++++ arch/x86/um/nommu/signal.c | 43 ++++++ arch/x86/um/nommu/syscalls.h | 16 ++ arch/x86/um/nommu/syscalls_64.c | 115 +++++++++++++++ arch/x86/um/shared/sysdep/mcontext.h | 4 + arch/x86/um/shared/sysdep/signal.h | 22 +++ arch/x86/um/shared/sysdep/syscalls_64.h | 7 + arch/x86/um/signal.c | 17 ++- arch/x86/um/vdso/vma.c | 17 ++- fs/Kconfig.binfmt | 2 +- 51 files changed, 1564 insertions(+), 350 deletions(-) create mode 100644 Documentation/virt/uml/nommu-uml.rst create mode 100644 arch/um/configs/x86_64_nommu_defconfig create mode 100644 arch/um/kernel/mem-pgtable.c create mode 100644 arch/um/nommu/Makefile create mode 100644 arch/um/nommu/os-Linux/Makefile create mode 100644 arch/um/nommu/os-Linux/signal.c create mode 100644 arch/um/nommu/trap.c create mode 100644 arch/um/os-Linux/seccomp.c delete mode 100644 arch/x86/um/asm/module.h create mode 100644 arch/x86/um/nommu/Makefile create mode 100644 arch/x86/um/nommu/do_syscall_64.c create mode 100644 arch/x86/um/nommu/entry_64.S create mode 100644 arch/x86/um/nommu/os-Linux/Makefile create mode 100644 arch/x86/um/nommu/os-Linux/mcontext.c create mode 100644 arch/x86/um/nommu/process.c create mode 100644 arch/x86/um/nommu/signal.c create mode 100644 arch/x86/um/nommu/syscalls.h create mode 100644 arch/x86/um/nommu/syscalls_64.c create mode 100644 arch/x86/um/shared/sysdep/signal.h
base-commit: bed2cc482600296fe04edbc38005ba2851449c10
Signature · 1 line
2.43.0
PATCH v4 1/13 x86/um: clean up elf specific definitions
The file arch/x86/um/asm/module.h is equivalent to the definition of asm-generic. Thus this commit cleans up to use it.
--- arch/um/include/asm/Kbuild | 1 + arch/x86/um/asm/module.h | 24 ------------------------ 2 files changed, 1 insertion(+), 24 deletions(-) delete mode 100644 arch/x86/um/asm/module.h
arch/um/include/asm/Kbuild +1 -0
@@ -13,6 +13,7 @@ generic-y += irq_work.h generic-y += kdebug.h generic-y += mcs_spinlock.h generic-y += mmiowb.h +generic-y += module.h generic-y += module.lds.h generic-y += param.h generic-y += parport.h
arch/x86/um/asm/module.h deleted +0 -24
@@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __UM_MODULE_H -#define __UM_MODULE_H - -/* UML is simple */ -struct mod_arch_specific -{ -}; - -#ifdef CONFIG_X86_32 - -#define Elf_Shdr Elf32_Shdr -#define Elf_Sym Elf32_Sym -#define Elf_Ehdr Elf32_Ehdr - -#else - -#define Elf_Shdr Elf64_Shdr -#define Elf_Sym Elf64_Sym -#define Elf_Ehdr Elf64_Ehdr - -#endif - -#endif
Signature · 1 line
2.43.0
PATCH v4 2/13 x86/um: nommu: elf loader for fdpic
In reply to Hajime TazakiAs UML supports CONFIG_MMU=n case, it has to use an alternate ELF loader, FDPIC ELF loader. In this commit, we added necessary definitions in the arch, as UML has not been used so far. It also updates Kconfig file to use BINFMT_ELF_FDPIC under !MMU environment.
--- arch/um/include/asm/mmu.h | 5 +++++ arch/um/include/asm/ptrace-generic.h | 6 ++++++ arch/x86/um/asm/elf.h | 8 ++++++-- fs/Kconfig.binfmt | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-)
arch/um/include/asm/mmu.h +5 -0
@@ -14,6 +14,11 @@ typedef struct mm_context {/* Address range in need of a TLB sync */ unsigned long sync_tlb_range_from;unsigned long sync_tlb_range_to; + +#ifdef CONFIG_BINFMT_ELF_FDPIC +unsigned long exec_fdpic_loadmap; +unsigned long interp_fdpic_loadmap; +#endif } mm_context_t;#endif
arch/um/include/asm/ptrace-generic.h +6 -0
@@ -29,6 +29,12 @@ struct pt_regs {#define PTRACE_OLDSETOPTIONS 21 +#ifdef CONFIG_BINFMT_ELF_FDPIC +#define PTRACE_GETFDPIC 31 +#define PTRACE_GETFDPIC_EXEC 0 +#define PTRACE_GETFDPIC_INTERP 1 +#endif +struct task_struct ; extern long subarch_ptrace(struct task_struct *child, long request,
arch/x86/um/asm/elf.h +6 -2
@@ -9,6 +9,7 @@#include <skas.h> #define CORE_DUMP_USE_REGSET +#define ELF_FDPIC_CORE_EFLAGS 0#ifdef CONFIG_X86_32
@@ -190,8 +191,11 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,extern unsigned long um_vdso_addr;#define AT_SYSINFO_EHDR 33 -#define ARCH_DLINFO NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr) - +#define ARCH_DLINFO \ +do { \ + NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr); \ + NEW_AUX_ENT(AT_MINSIGSTKSZ, 0); \ +} while (0)#endif typedef unsigned long elf_greg_t ;
fs/Kconfig.binfmt +1 -1
@@ -58,7 +58,7 @@ config ARCH_USE_GNU_PROPERTY config BINFMT_ELF_FDPIC bool "Kernel support for FDPIC ELF binaries" default y if !BINFMT_ELF - depends on ARM || ((M68K || RISCV || SUPERH || XTENSA) && !MMU) + depends on ARM || ((M68K || RISCV || SUPERH || UML || XTENSA) && !MMU) select ELFCORE help ELF FDPIC binaries are based on ELF, but allow the individual load
Signature · 1 line
2.43.0
PATCH v4 3/13 um: decouple MMU specific code from the common part
In reply to Hajime TazakiThis splits the memory, process related code with common and MMU specific parts in order to avoid ifdefs in .c file and duplication between MMU and !MMU.
--- arch/um/include/shared/mem.h | 8 ++ arch/um/kernel/Makefile | 5 +- arch/um/kernel/mem-pgtable.c | 175 ++++++++++++++++++++++++++++++++ arch/um/kernel/mem.c | 149 +-------------------------- arch/um/kernel/process.c | 25 +++++ arch/um/kernel/skas/process.c | 27 ----- arch/um/os-Linux/Makefile | 3 +- arch/um/os-Linux/internal.h | 5 + arch/um/os-Linux/process.c | 133 ++++++++++++++++++++++++ arch/um/os-Linux/skas/process.c | 132 ------------------------ 10 files changed, 352 insertions(+), 310 deletions(-) create mode 100644 arch/um/kernel/mem-pgtable.c
arch/um/include/shared/mem.h +8 -0
@@ -19,4 +19,12 @@ static inline void *uml_to_virt(unsigned long phys) return((void *) uml_physmem + phys); } +#ifdef CONFIG_MMU +extern void um_fixrange_init (void ); +#else +static inline void um_fixrange_init (void ) +{ +} +#endif +#endif
arch/um/kernel/Makefile +3 -2
@@ -16,9 +16,10 @@ extra-y := vmlinux.lds obj-y = config.o exec.o exitcode.o irq.o ksyms.o mem.o \ physmem.o process.o ptrace.o reboot.o sigio.o \ - signal.o sysrq.o time.o tlb.o trap.o \ - um_arch.o umid.o maccess.o kmsg_dump.o capflags.o skas/ + signal.o sysrq.o time.o \ + um_arch.o umid.o maccess.o kmsg_dump.o capflags.o obj-y += load_file.o +obj-$(CONFIG_MMU) += mem-pgtable.o tlb.o trap.o skas/ obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o obj-$(CONFIG_GPROF) += gprof_syms.o
arch/um/kernel/mem-pgtable.c new file +175 -0
175 lines not on this page · raw message
arch/um/kernel/mem.c +1 -148
177 lines not on this page · raw message
arch/um/kernel/process.c +25 -0
@@ -25,6 +25,7 @@#include <linux/tick.h> #include <linux/threads.h> #include <linux/resume_user_mode.h> +#include <linux/start_kernel.h> #include <asm/current.h> #include <asm/mmu_context.h> #include <asm/switch_to.h>
@@ -46,6 +47,8 @@struct task_struct *cpu_tasks[NR_CPUS ];EXPORT_SYMBOL (cpu_tasks); +static char cpu0_irqstack[THREAD_SIZE ]__aligned(THREAD_SIZE) ; +void free_stack (unsigned long stack,int order) {free_pages (stack, order);
@@ -295,3 +298,25 @@ unsigned long __get_wchan(struct task_struct *p)return 0 ; } + + +static int __init start_kernel_proc (void *unused) +{ +block_signals_trace (); + +start_kernel (); +return 0 ; +} + +int __init start_uml (void ) +{ +stack_protections ((unsigned long ) &cpu0_irqstack); +set_sigstack (cpu0_irqstack,THREAD_SIZE ); + +init_new_thread_signals (); + + init_task.thread.request.thread.proc = start_kernel_proc; + init_task.thread.request.thread.arg =NULL ; +return start_idle_thread (task_stack_page (&init_task), + &init_task.thread.switch_buf); +}
arch/um/kernel/skas/process.c +0 -27
@@ -16,33 +16,6 @@#include <skas.h> #include <kern_util.h> -extern void start_kernel (void ); - -static int __init start_kernel_proc (void *unused) -{ -block_signals_trace (); - -start_kernel (); -return 0 ; -} - -extern int userspace_pid[]; - -static char cpu0_irqstack[THREAD_SIZE ]__aligned(THREAD_SIZE) ; - -int __init start_uml (void ) -{ -stack_protections ((unsigned long ) &cpu0_irqstack); -set_sigstack (cpu0_irqstack,THREAD_SIZE ); - -init_new_thread_signals (); - - init_task.thread.request.thread.proc = start_kernel_proc; - init_task.thread.request.thread.arg =NULL ; -return start_idle_thread (task_stack_page (&init_task), - &init_task.thread.switch_buf); -} -unsigned long current_stub_stack (void ) {if (current->mm ==NULL )
arch/um/os-Linux/Makefile +2 -1
@@ -8,7 +8,8 @@ KCOV_INSTRUMENT := n obj-y = execvp.o file.o helper.o irq.o main.o mem.o process.o \ registers.o sigio.o signal.o start_up.o time.o tty.o \ - umid.o user_syms.o util.o drivers/ skas/ + umid.o user_syms.o util.o drivers/ +obj-$(CONFIG_MMU) += skas/ CFLAGS_signal.o += -Wframe-larger-than=4096
arch/um/os-Linux/internal.h +5 -0
@@ -2,6 +2,11 @@#ifndef __UM_OS_LINUX_INTERNAL_H#define __UM_OS_LINUX_INTERNAL_H +/* +* process.c +*/ +extern int userspace_pid[]; +/* * elf_aux.c */
arch/um/os-Linux/process.c +133 -0
151 lines not on this page · raw message
arch/um/os-Linux/skas/process.c +0 -132
168 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v4 4/13 um: nommu: memory handling
In reply to Hajime TazakiThis commit adds memory operations on UML under !MMU environment.
Some part of the original UML code relying on CONFIG_MMU are excluded from compilation when !CONFIG_MMU. Additionally, generic functions such as uaccess, futex, memcpy/strnlen/strncpy can be used as user- and kernel-space share the address space in !CONFIG_MMU mode.
--- arch/um/Makefile | 4 ++++ arch/um/include/asm/futex.h | 4 ++++ arch/um/include/asm/mmu.h | 3 +++ arch/um/include/asm/mmu_context.h | 2 ++ arch/um/include/asm/uaccess.h | 7 ++++--- arch/um/kernel/mem.c | 3 ++- arch/um/os-Linux/mem.c | 4 ++++ arch/um/os-Linux/process.c | 4 ++-- 8 files changed, 25 insertions(+), 6 deletions(-)
arch/um/Makefile +4 -0
@@ -46,6 +46,10 @@ ARCH_INCLUDE := -I$(srctree)/$(SHARED_HEADERS) ARCH_INCLUDE += -I$(srctree)/$(HOST_DIR)/um/shared KBUILD_CPPFLAGS += -I$(srctree)/$(HOST_DIR)/um +ifneq ($(CONFIG_MMU),y) +core-y += $(ARCH_DIR)/nommu/ +endif + # -Dvmap=kernel_vmap prevents anything from referencing the libpcap.o symbol so # named - it's a common symbol in libpcap, so we get a binary which crashes. #
arch/um/include/asm/futex.h +4 -0
@@ -7,8 +7,12 @@#include <asm/errno.h> +#ifdef CONFIG_MMU int arch_futex_atomic_op_inuser (int op,u32 oparg,int *oval,u32 __user *uaddr);int futex_atomic_cmpxchg_inatomic (u32 *uval,u32 __user *uaddr,u32 oldval,u32 newval); +#else +#include <asm-generic/futex.h> +#endif #endif
arch/um/include/asm/mmu.h +3 -0
@@ -15,10 +15,13 @@ typedef struct mm_context {unsigned long sync_tlb_range_from;unsigned long sync_tlb_range_to; +#ifndef CONFIG_MMU +unsigned long end_brk;#ifdef CONFIG_BINFMT_ELF_FDPIC unsigned long exec_fdpic_loadmap;unsigned long interp_fdpic_loadmap;#endif +#endif /* !CONFIG_MMU */ } mm_context_t;#endif
arch/um/include/asm/mmu_context.h +2 -0
@@ -36,11 +36,13 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, } } +#ifdef CONFIG_MMU #define init_new_context init_new_contextextern int init_new_context (struct task_struct *task,struct mm_struct *mm);#define destroy_context destroy_contextextern void destroy_context (struct mm_struct *mm); +#endif #include <asm-generic/mmu_context.h>
arch/um/include/asm/uaccess.h +4 -3
@@ -22,6 +22,7 @@#define __addr_range_nowrap (addr, size) \ ((unsigned long) (addr) <= ((unsigned long) (addr) + (size))) +#ifdef CONFIG_MMU extern unsigned long raw_copy_from_user (void *to,const void __user *from,unsigned long n);extern unsigned long raw_copy_to_user (void __user *to,const void *from,unsigned long n);extern unsigned long __clear_user (void __user *mem,unsigned long len);
@@ -33,9 +34,6 @@ static inline int __access_ok(const void __user *ptr, unsigned long size);#define INLINE_COPY_FROM_USER #define INLINE_COPY_TO_USER - -#include <asm-generic/uaccess.h> -static inline int __access_ok (const void __user *ptr,unsigned long size) {unsigned long addr = (unsigned long )ptr;
@@ -43,6 +41,9 @@ static inline int __access_ok(const void __user *ptr, unsigned long size) (__under_task_size (addr, size) || __access_ok_vsyscall(addr, size)); } +#endif + +#include <asm-generic/uaccess.h> /* no pagefaults for kernel addresses in um */ #define __get_kernel_nofault (dst, src, type, err_label) \
arch/um/kernel/mem.c +2 -1
@@ -63,7 +63,8 @@ void __init mem_init(void)* to be turned on. */ brk_end = (unsigned long )UML_ROUND_UP (sbrk (0 )); -map_memory (brk_end,__pa (brk_end), uml_reserved - brk_end,1 ,1 ,0 ); +map_memory (brk_end,__pa (brk_end), uml_reserved - brk_end,1 ,1 , + !IS_ENABLED (CONFIG_MMU ));memblock_free ((void *)brk_end, uml_reserved - brk_end); uml_reserved = brk_end;
arch/um/os-Linux/mem.c +4 -0
@@ -213,6 +213,10 @@ int __init create_mem_file(unsigned long long len) {int err, fd; +/* NOMMU kernel uses -1 as a fd for further use (e.g., mmap) */ +if (!IS_ENABLED (CONFIG_MMU )) +return -1 ; + fd =create_tmp_file (len); err =os_set_exec_close (fd);
arch/um/os-Linux/process.c +2 -2
@@ -70,8 +70,8 @@ int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, prot = (r ?PROT_READ :0 ) | (w ?PROT_WRITE :0 ) | (x ?PROT_EXEC :0 ); - loc =mmap64 ((void *) virt, len, prot,MAP_SHARED |MAP_FIXED , - fd, off); + loc =mmap64 ((void *) virt, len, prot,MAP_SHARED |MAP_FIXED | + (!IS_ENABLED (CONFIG_MMU ) ?MAP_ANONYMOUS :0 ), fd, off);if (loc ==MAP_FAILED )return -errno;return 0 ;
Signature · 1 line
2.43.0
PATCH v4 5/13 x86/um: nommu: syscall handling
In reply to Hajime TazakiThis commit introduces an entry point of syscall interface for !MMU mode. It uses an entry function, __kernel_vsyscall, a kernel-wide global symbol accessible from any locations.
Although it isn't in the scope of this commit, it can be also exposed via vdso image which is directly accessible from userspace. A standard library (i.e., libc) can utilize this entry point to implement syscall wrapper; we can also use this by hooking syscall for unmodified userspace applications/libraries, which will be implemented in the subsequent commit.
This only supports 64-bit mode of x86 architecture.
--- arch/x86/um/Makefile | 4 ++ arch/x86/um/nommu/Makefile | 8 +++ arch/x86/um/nommu/do_syscall_64.c | 37 +++++++++++ arch/x86/um/nommu/entry_64.S | 87 +++++++++++++++++++++++++ arch/x86/um/nommu/syscalls.h | 16 +++++ arch/x86/um/shared/sysdep/syscalls_64.h | 6 ++ 6 files changed, 158 insertions(+) create mode 100644 arch/x86/um/nommu/Makefile create mode 100644 arch/x86/um/nommu/do_syscall_64.c create mode 100644 arch/x86/um/nommu/entry_64.S create mode 100644 arch/x86/um/nommu/syscalls.h
arch/x86/um/Makefile +4 -0
@@ -32,6 +32,10 @@ obj-y += syscalls_64.o vdso/ subarch-y = ../lib/csum-partial_64.o ../lib/memcpy_64.o \ ../lib/memmove_64.o ../lib/memset_64.o +ifneq ($(CONFIG_MMU),y) +obj-y += nommu/ +endif + endif subarch-$(CONFIG_MODULES) += ../kernel/module.o
arch/x86/um/nommu/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +ifeq ($(CONFIG_X86_32),y) + BITS := 32 +else + BITS := 64 +endif + +obj-y = do_syscall_$(BITS).o entry_$(BITS).o
arch/x86/um/nommu/do_syscall_64.c new file +37 -0
@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/kernel.h> +#include <linux/ptrace.h> +#include <kern_util.h> +#include <sysdep/syscalls.h> +#include <os.h> + +__visible void do_syscall_64(struct pt_regs *regs) +{ +int syscall; + + syscall =PT_SYSCALL_NR (regs->regs.gp); +UPT_SYSCALL_NR (®s->regs) = syscall; + +pr_debug ("syscall(%d) (current=%lx) (fn=%lx)\n" , + syscall, (unsigned long )current, + (unsigned long )sys_call_table[syscall]); + +if (likely (syscall < NR_syscalls)) { +PT_REGS_SET_SYSCALL_RETURN (regs, +EXECUTE_SYSCALL (syscall, regs)); + } + +pr_debug ("syscall(%d) --> %lx\n" , syscall, + regs->regs.gp[HOST_AX ]); + +PT_REGS_SYSCALL_RET (regs) = regs->regs.gp[HOST_AX ]; + +/* execve succeeded */ +if (syscall == __NR_execve && regs->regs.gp[HOST_AX ] ==0 ) +userspace (¤t->thread.regs.regs); + +/* force do_signal() --> is_syscall() */ +set_thread_flag (TIF_SIGPENDING ); +interrupt_end (); +}
arch/x86/um/nommu/entry_64.S new file +87 -0
87 lines not on this page · raw message
arch/x86/um/nommu/syscalls.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __UM_NOMMU_SYSCALLS_H +#define __UM_NOMMU_SYSCALLS_H + + +#define task_top_of_stack (task) \ +({ \ + unsigned long __ptr = (unsigned long)task->stack; \ + __ptr += THREAD_SIZE; \ + __ptr; \ +}) + +extern long current_top_of_stack; +extern long current_ptregs; + +#endif
arch/x86/um/shared/sysdep/syscalls_64.h +6 -0
@@ -25,4 +25,10 @@ extern syscall_handler_t *sys_call_table[];extern syscall_handler_t sys_modify_ldt;extern syscall_handler_t sys_arch_prctl; +#ifndef CONFIG_MMU +extern void do_syscall_64 (struct pt_regs *regs); +extern long __kernel_vsyscall (int64_t a0,int64_t a1,int64_t a2,int64_t a3, +int64_t a4,int64_t a5,int64_t a6); +#endif +#endif
Signature · 1 line
2.43.0
PATCH v4 6/13 um: nommu: seccomp syscalls hook
In reply to Hajime TazakiThis commit adds syscall hook with seccomp.
Using seccomp raises SIGSYS to UML process, which is captured in the (UML) kernel, then call the syscall entry point, __kernel_vsyscall, to hook the original syscall instructions.
The SIGSYS signal is raised upon the execution from uml_reserved and high_physmem, which locates userspace memory.
--- arch/um/include/shared/kern_util.h | 8 +++ arch/um/include/shared/os.h | 10 +++ arch/um/kernel/um_arch.c | 3 + arch/um/nommu/Makefile | 3 + arch/um/nommu/os-Linux/Makefile | 7 +++ arch/um/nommu/os-Linux/signal.c | 15 +++++ arch/um/os-Linux/Makefile | 5 ++ arch/um/os-Linux/seccomp.c | 87 +++++++++++++++++++++++++++ arch/um/os-Linux/signal.c | 6 ++ arch/x86/um/nommu/Makefile | 2 +- arch/x86/um/nommu/os-Linux/Makefile | 6 ++ arch/x86/um/nommu/os-Linux/mcontext.c | 15 +++++ arch/x86/um/shared/sysdep/mcontext.h | 3 + 13 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 arch/um/nommu/Makefile create mode 100644 arch/um/nommu/os-Linux/Makefile create mode 100644 arch/um/nommu/os-Linux/signal.c create mode 100644 arch/um/os-Linux/seccomp.c create mode 100644 arch/x86/um/nommu/os-Linux/Makefile create mode 100644 arch/x86/um/nommu/os-Linux/mcontext.c
arch/um/include/shared/kern_util.h +8 -0
@@ -67,4 +67,12 @@ void um_idle_sleep(void);void kasan_map_memory (void *start,size_t len); +#ifdef CONFIG_MMU +static inline void arch_sigsys_handler (int sig,struct siginfo *si,void *mc) +{ +} +#else +extern void arch_sigsys_handler (int sig,struct siginfo *si,void *mc); +#endif +#endif
arch/um/include/shared/os.h +10 -0
@@ -334,4 +334,14 @@ extern void um_trace_signals_off(void);/* time-travel */ extern void deliver_time_travel_irqs (void ); +/* seccomp.c */ +#ifdef CONFIG_MMU +static inline int os_setup_seccomp (void ) +{ +return 0 ; +} +#else +extern int os_setup_seccomp (void ); +#endif +#endif
arch/um/kernel/um_arch.c +3 -0
@@ -432,6 +432,9 @@ void __init setup_arch(char **cmdline_p)add_bootloader_randomness (rng_seed,sizeof (rng_seed));memzero_explicit (rng_seed,sizeof (rng_seed)); } + +/* install seccomp filter */ +os_setup_seccomp (); }void __init arch_cpu_finalize_init (void )
arch/um/nommu/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y := os-Linux/
arch/um/nommu/os-Linux/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y := signal.o +USER_OBJS := $(obj-y) + +include $(srctree)/arch/um/scripts/Makefile.rules +USER_CFLAGS+=-I$(srctree)/arch/um/os-Linux
arch/um/nommu/os-Linux/signal.c new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <signal.h> +#include <kern_util.h> +#include <os.h> +#include <sysdep/mcontext.h> +#include <sys/ucontext.h> + +void arch_sigsys_handler (int sig,struct siginfo *si,void *ptr) +{ +mcontext_t *mc = (mcontext_t *) ptr; + +/* hook syscall via SIGSYS */ +mc_set_sigsys_hook (mc); +}
arch/um/os-Linux/Makefile +5 -0
@@ -21,4 +21,9 @@ USER_OBJS := $(user-objs-y) elf_aux.o execvp.o file.o helper.o irq.o \ main.o mem.o process.o registers.o sigio.o signal.o start_up.o time.o \ tty.o umid.o util.o +ifneq ($(CONFIG_MMU),y) +obj-y += seccomp.o +USER_OBJS += seccomp.o +endif + include $(srctree)/arch/um/scripts/Makefile.rules
arch/um/os-Linux/seccomp.c new file +87 -0
87 lines not on this page · raw message
arch/um/os-Linux/signal.c +6 -0
@@ -172,12 +172,18 @@ void register_pm_wake_signal(void)set_handler (SIGUSR1 ); } +static void sigsys_handler (int sig,struct siginfo *unused_si,mcontext_t *mc) +{ +arch_sigsys_handler (sig, unused_si, mc); +} +static void (*handlers[_NSIG])(int sig,struct siginfo *si,mcontext_t *mc) = { [SIGSEGV ] = sig_handler, [SIGBUS ] = sig_handler, [SIGILL ] = sig_handler, [SIGFPE ] = sig_handler, [SIGTRAP ] = sig_handler, + [SIGSYS ] = sigsys_handler, [SIGIO ] = sig_handler, [SIGWINCH ] = sig_handler,
arch/x86/um/nommu/Makefile +1 -1
@@ -5,4 +5,4 @@ else BITS := 64 endif -obj-y = do_syscall_$(BITS).o entry_$(BITS).o +obj-y = do_syscall_$(BITS).o entry_$(BITS).o os-Linux/
arch/x86/um/nommu/os-Linux/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y = mcontext.o +USER_OBJS := mcontext.o + +include $(srctree)/arch/um/scripts/Makefile.rules
arch/x86/um/nommu/os-Linux/mcontext.c new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <sys/ucontext.h> +#define __FRAME_OFFSETS +#include <asm/ptrace.h> +#include <sysdep/ptrace.h> +#include <sysdep/mcontext.h> +#include <sysdep/syscalls.h> + +void mc_set_sigsys_hook (mcontext_t *mc) +{ + mc->gregs[REG_RSP ] -=sizeof (unsigned long ); + *((unsigned long *) (mc->gregs[REG_RSP ])) = mc->gregs[REG_RIP ]; + mc->gregs[REG_RCX ] = mc->gregs[REG_RIP ]; + mc->gregs[REG_RIP ] = (unsigned long ) __kernel_vsyscall; +}
arch/x86/um/shared/sysdep/mcontext.h +3 -0
@@ -7,6 +7,9 @@#define __SYS_SIGCONTEXT_X86_Hextern void get_regs_from_mc (struct uml_pt_regs *,mcontext_t *); +#ifndef CONFIG_MMU +extern void mc_set_sigsys_hook (mcontext_t *mc); +#endif #ifdef __i386__
Signature · 1 line
2.43.0
PATCH v4 7/13 x86/um: nommu: process/thread handling
In reply to Hajime TazakiSince ptrace facility isn't used under !MMU of UML, there is different code path to invoke processes/threads; on an entry to the syscall interface, the stack pointer should be manipulated to handle vfork(2) return address, no external process is used, and need to properly configure some of registers (fs segment register for TLS, etc) on every context switch, etc.
Signals aren't delivered in non-ptrace syscall entry/leave so, we also need to handle pending signal by ourselves.
--- arch/um/os-Linux/process.c | 6 ++++ arch/x86/um/Makefile | 3 +- arch/x86/um/nommu/Makefile | 2 +- arch/x86/um/nommu/do_syscall_64.c | 26 +++++++++++++++ arch/x86/um/nommu/entry_64.S | 24 ++++++++++++++ arch/x86/um/nommu/process.c | 35 ++++++++++++++++++++ arch/x86/um/nommu/syscalls_64.c | 44 +++++++++++++++++++++++++ arch/x86/um/shared/sysdep/syscalls_64.h | 1 + 8 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 arch/x86/um/nommu/process.c create mode 100644 arch/x86/um/nommu/syscalls_64.c
arch/um/os-Linux/process.c +6 -0
@@ -28,6 +28,9 @@ int unscheduled_userspace_iterations;void os_alarm_process (int pid) { +if (pid <=0 ) +return ; +kill (pid,SIGALRM ); }
@@ -45,6 +48,9 @@ void os_kill_process(int pid, int reap_child)void os_kill_ptraced_process (int pid,int reap_child) { +if (pid <=0 ) +return ; +kill (pid,SIGKILL );ptrace (PTRACE_KILL , pid);ptrace (PTRACE_CONT , pid);
arch/x86/um/Makefile +2 -1
@@ -27,7 +27,8 @@ subarch-y += ../kernel/sys_ia32.o else -obj-y += syscalls_64.o vdso/ +obj-y += vdso/ +obj-$(CONFIG_MMU) += syscalls_64.o subarch-y = ../lib/csum-partial_64.o ../lib/memcpy_64.o \ ../lib/memmove_64.o ../lib/memset_64.o
arch/x86/um/nommu/Makefile +1 -1
@@ -5,4 +5,4 @@ else BITS := 64 endif -obj-y = do_syscall_$(BITS).o entry_$(BITS).o os-Linux/ +obj-y = do_syscall_$(BITS).o entry_$(BITS).o process.o syscalls_$(BITS).o os-Linux/
arch/x86/um/nommu/do_syscall_64.c +26 -0
@@ -6,9 +6,28 @@#include <sysdep/syscalls.h> #include <os.h> +/* +* save/restore the return address stored in the stack, as the child overwrites +* the contents after returning to userspace (i.e., by push %rdx). +* +* see the detail in fork_handler(). +*/ +static void vfork_save_stack (void *stack) +{ +memcpy (stack, + (void *)current->thread.regs.regs.gp[HOST_SP ],8 ); +} + +static void vfork_restore_stack (void *stack_copy) +{ +memcpy ((void *)current->thread.regs.regs.gp[HOST_SP ], + stack_copy,8 ); +} + __visible void do_syscall_64(struct pt_regs *regs) {int syscall; +unsigned long stack_copy; syscall =PT_SYSCALL_NR (regs->regs.gp);UPT_SYSCALL_NR (®s->regs) = syscall;
@@ -17,6 +36,9 @@ __visible void do_syscall_64(struct pt_regs *regs) syscall, (unsigned long )current, (unsigned long)sys_call_table[syscall]); +if (syscall == __NR_vfork) +vfork_save_stack (&stack_copy); +if (likely (syscall < NR_syscalls)) {PT_REGS_SET_SYSCALL_RETURN (regs,EXECUTE_SYSCALL (syscall, regs));
@@ -31,6 +53,10 @@ __visible void do_syscall_64(struct pt_regs *regs)if (syscall == __NR_execve && regs->regs.gp[HOST_AX ] ==0 )userspace (¤t->thread.regs.regs); +/* only parents of vfork restores the contents of stack */ +if (syscall == __NR_vfork && regs->regs.gp[HOST_AX ] >0 ) +vfork_restore_stack (&stack_copy); +/* force do_signal() --> is_syscall() */ set_thread_flag (TIF_SIGPENDING );interrupt_end ();
arch/x86/um/nommu/entry_64.S +24 -0
@@ -85,3 +85,27 @@ ENTRY(__kernel_vsyscall) ret END(__kernel_vsyscall) + +// void userspace(struct uml_pt_regs *regs) +ENTRY(userspace) + /* fixup stack for vfork syscall*/ + call arch_fixup_stack + + /* align the stack for x86_64 ABI */ + and $-0x10, %rsp + /* Handle any immediate reschedules or signals */ + call interrupt_end + + movq current_ptregs, %rsp + + POP_REGS + + addq $8, %rsp /* skip orig_ax */ + popq %r11 /* pt_regs->ip */ + addq $8, %rsp /* skip cs */ + addq $8, %rsp /* skip flags */ + popq %rsp + + jmp *%r11 + +END(userspace)
arch/x86/um/nommu/process.c new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <asm/unistd.h> +#include <sysdep/syscalls.h> +#include <os.h> + +void arch_fixup_stack (struct uml_pt_regs *regs) +{ +/* +* child of vfork(2) comes here. +* clone(2) also enters here but doesn't need to advance the %rsp. +* +* This fork can only come from libc's vfork, which +* does this: +* pop %%rdx; +* mov $58,%eax +* syscall ; // hook => __kernel_vsyscall +* push %%rdx; +* %rcx stores the return address which is stored +* at pt_regs[HOST_IP] at the moment. As child returns +* via userspace() with a jmp instruction (while parent +* does via ret instruction in __kernel_vsyscall), we +* need to pop (advance) the pushed address by "call" +* though, so this is what this next line does. +* +* As a result of vfork return in child, stack contents +* is overwritten by child (by pushq in vfork), which +* makes the parent puzzled after child returns. +* +* thus the contents should be restored before vfork/parent +* returns. this is done in do_syscall_64(). +*/ +if (regs->gp[HOST_ORIG_AX ] == __NR_vfork) + regs->gp[REGS_SP_INDEX ] +=8 ; +}
arch/x86/um/nommu/syscalls_64.c new file +44 -0
44 lines not on this page · raw message
arch/x86/um/shared/sysdep/syscalls_64.h +1 -0
@@ -29,6 +29,7 @@ extern syscall_handler_t sys_arch_prctl;extern void do_syscall_64 (struct pt_regs *regs);extern long __kernel_vsyscall (int64_t a0,int64_t a1,int64_t a2,int64_t a3,int64_t a4,int64_t a5,int64_t a6); +extern void arch_fixup_stack (struct uml_pt_regs *regs);#endif #endif
Signature · 1 line
2.43.0
PATCH v4 8/13 um: nommu: configure fs register on host syscall invocation
In reply to Hajime TazakiAs userspace on UML/!MMU also need to configure %fs register when it is running to correctly access thread structure, host syscalls implemented in os-Linux drivers may be puzzled when they are called. Thus it has to configure %fs register via arch_prctl(SET_FS) on every host syscalls.
--- arch/um/include/shared/os.h | 6 +++ arch/um/os-Linux/process.c | 6 +++ arch/um/os-Linux/start_up.c | 20 +++++++++ arch/x86/um/nommu/do_syscall_64.c | 37 ++++++++++++++++ arch/x86/um/nommu/syscalls_64.c | 71 +++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+)
arch/um/include/shared/os.h +6 -0
@@ -190,6 +190,7 @@ extern void check_host_supports_tls(int *supports_tls, int *tls_min);extern void get_host_cpu_features (void (*flags_helper_func)(char *line),void (*cache_helper_func)(char *line)); +extern int host_has_fsgsbase;/* mem.c */ extern int create_mem_file (unsigned long long len);
@@ -214,6 +215,11 @@ extern int os_unmap_memory(void *addr, int len);extern int os_drop_memory (void *addr,int length);extern int can_drop_memory (void );extern int os_mincore (void *addr,unsigned long len); +extern int os_arch_prctl (int pid,int option,unsigned long *arg); +#ifndef CONFIG_MMU +extern long long host_fs; +#endif +void os_set_pdeathsig (void );
arch/um/os-Linux/process.c +6 -0
@@ -16,6 +16,7 @@#include <sys/prctl.h> #include <sys/wait.h> #include <asm/unistd.h> +#include <sys/syscall.h> /* For SYS_xxx definitions */ #include <linux/threads.h> #include <init.h> #include <longjmp.h>
@@ -206,6 +207,11 @@ int os_mincore(void *addr, unsigned long len)return ret; } +int os_arch_prctl (int pid,int option,unsigned long *arg2) +{ +return syscall (SYS_arch_prctl, option, arg2); +} +void init_new_thread_signals (void ) {set_handler (SIGSEGV );
arch/um/os-Linux/start_up.c +20 -0
@@ -19,6 +19,8 @@#include <sys/resource.h> #include <asm/ldt.h> #include <asm/unistd.h> +#include <sys/auxv.h> +#include <asm/hwcap2.h> #include <init.h> #include <os.h> #include <kern_util.h>
@@ -28,6 +30,8 @@#include <skas.h> #include "internal.h" +int host_has_fsgsbase; +static void ptrace_child (void ) {int ret;
@@ -278,6 +282,19 @@ void __init get_host_cpu_features( } } +static void __init check_fsgsbase (void ) +{ +unsigned long auxv =getauxval (AT_HWCAP2 ); + +os_info ("Checking FSGSBASE instructions..." ); +if (auxv &HWCAP2_FSGSBASE ) { + host_has_fsgsbase =1 ; +os_info ("OK\n" ); + }else { + host_has_fsgsbase =0 ; +os_info ("disabled\n" ); + } +}void __init os_early_checks (void ) {
@@ -293,6 +310,9 @@ void __init os_early_checks(void)*/ check_tmpexec (); +/* probe fsgsbase instruction */ +check_fsgsbase (); + pid =start_ptraced_child ();if (init_pid_registers (pid))fatal ("Failed to initialize default registers" );
arch/x86/um/nommu/do_syscall_64.c +37 -0
@@ -2,6 +2,8 @@#include <linux/kernel.h> #include <linux/ptrace.h> +#include <asm/fsgsbase.h> +#include <asm/prctl.h> #include <kern_util.h> #include <sysdep/syscalls.h> #include <os.h>
@@ -24,6 +26,32 @@ static void vfork_restore_stack(void *stack_copy) stack_copy, 8); } +static int os_x86_arch_prctl (int pid,int option,unsigned long *arg2) +{ +if (!host_has_fsgsbase) +return os_arch_prctl (pid, option, arg2); + +switch (option) { +case ARCH_SET_FS : +wrfsbase (*arg2); +break ; +case ARCH_SET_GS : +wrgsbase (*arg2); +break ; +case ARCH_GET_FS : + *arg2 =rdfsbase (); +break ; +case ARCH_GET_GS : + *arg2 =rdgsbase (); +break ; +default : +pr_warn ("%s: unsupported option: 0x%x" , __func__, option); +break ; + } + +return 0 ; +} + __visible void do_syscall_64(struct pt_regs *regs) {int syscall;
@@ -39,6 +67,9 @@ __visible void do_syscall_64(struct pt_regs *regs)if (syscall == __NR_vfork)vfork_save_stack (&stack_copy); +/* set fs register to the original host one */ +os_x86_arch_prctl (0 ,ARCH_SET_FS , (void *)host_fs); +if (likely (syscall < NR_syscalls)) {PT_REGS_SET_SYSCALL_RETURN (regs,EXECUTE_SYSCALL (syscall, regs));
@@ -60,4 +91,10 @@ __visible void do_syscall_64(struct pt_regs *regs)/* force do_signal() --> is_syscall() */ set_thread_flag (TIF_SIGPENDING );interrupt_end (); + +/* restore back fs register to userspace configured one */ +os_x86_arch_prctl (0 ,ARCH_SET_FS , + (void *)(current->thread.regs.regs.gp[FS_BASE + /sizeof (unsigned long )])); + }
arch/x86/um/nommu/syscalls_64.c +71 -0
82 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v4 9/13 x86/um/vdso: nommu: vdso memory update
In reply to Hajime TazakiOn !MMU mode, the address of vdso is accessible from userspace. This commit implements the entry point by pointing a block of page address.
This commit also add memory permission configuration of vdso page to be executable.
--- arch/x86/um/vdso/vma.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
arch/x86/um/vdso/vma.c +15 -2
@@ -9,6 +9,7 @@#include <asm/page.h> #include <asm/elf.h> #include <linux/init.h> +#include <os.h> static unsigned int __read_mostly vdso_enabled =1 ;unsigned long um_vdso_addr;
@@ -24,8 +25,6 @@ static int __init init_vdso(void)BUG_ON (vdso_end - vdso_start >PAGE_SIZE ); - um_vdso_addr = task_size -PAGE_SIZE ; - vdsop =kmalloc (sizeof (struct page *),GFP_KERNEL );if (!vdsop) goto oom;
@@ -40,6 +39,18 @@ static int __init init_vdso(void)copy_page (page_address (um_vdso), vdso_start); *vdsop = um_vdso; +#ifdef CONFIG_MMU + um_vdso_addr = task_size -PAGE_SIZE ; +#else +/* this is fine with NOMMU as everything is accessible */ + um_vdso_addr = (unsigned long )page_address (um_vdso); +os_protect_memory ((void *)um_vdso_addr, vdso_end - vdso_start,1 ,0 ,1 ); +#endif + +pr_info ("vdso_start=%lx um_vdso_addr=%lx pg_um_vdso=%lx" , + (unsigned long )vdso_start, um_vdso_addr, + (unsigned long )page_address (um_vdso)); +return 0 ; oom:
@@ -50,6 +61,7 @@ static int __init init_vdso(void) }subsys_initcall (init_vdso); +#ifdef CONFIG_MMU int arch_setup_additional_pages (struct linux_binprm *bprm,int uses_interp) {struct vm_area_struct *vma;
@@ -74,3 +86,4 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) return IS_ERR(vma) ? PTR_ERR(vma) : 0; } +#endif
Signature · 1 line
2.43.0
PATCH v4 10/13 x86/um: nommu: signal handling
In reply to Hajime TazakiThis commit updates the behavior of signal handling under !MMU environment. 1) the stack preparation for the signal handlers and
2) restoration of stack after rt_sigreturn(2) syscall. Those are needed as the stack usage on vfork(2) syscall is different.
It also adds the follow up routine for SIGSEGV as a signal delivery runs in the same stack frame while we have to avoid endless SIGSEGV.
--- arch/um/nommu/Makefile | 2 +- arch/um/nommu/trap.c | 188 ++++++++++++++++++++++++++ arch/um/os-Linux/signal.c | 6 + arch/x86/um/nommu/Makefile | 2 +- arch/x86/um/nommu/os-Linux/mcontext.c | 11 ++ arch/x86/um/nommu/signal.c | 43 ++++++ arch/x86/um/shared/sysdep/mcontext.h | 1 + arch/x86/um/shared/sysdep/signal.h | 22 +++ arch/x86/um/signal.c | 17 ++- 9 files changed, 289 insertions(+), 3 deletions(-) create mode 100644 arch/um/nommu/trap.c create mode 100644 arch/x86/um/nommu/signal.c create mode 100644 arch/x86/um/shared/sysdep/signal.h
arch/um/nommu/Makefile +1 -1
@@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y := os-Linux/ +obj-y := trap.o os-Linux/
arch/um/nommu/trap.c new file +188 -0
188 lines not on this page · raw message
arch/um/os-Linux/signal.c +6 -0
@@ -50,6 +50,12 @@ static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) (*sig_info[sig])(sig, si, &r); errno = save_errno; + +#ifndef CONFIG_MMU +/* !MMU: force handle signals after rt_sigreturn() */ +if (r.is_user && sig ==SIGSEGV ) +mc_set_regs_ip_relay (mc); +#endif }/*
arch/x86/um/nommu/Makefile +1 -1
@@ -5,4 +5,4 @@ else BITS := 64 endif -obj-y = do_syscall_$(BITS).o entry_$(BITS).o process.o syscalls_$(BITS).o os-Linux/ +obj-y = do_syscall_$(BITS).o entry_$(BITS).o process.o signal.o syscalls_$(BITS).o os-Linux/
arch/x86/um/nommu/os-Linux/mcontext.c +11 -0
@@ -6,6 +6,17 @@#include <sysdep/mcontext.h> #include <sysdep/syscalls.h> +static void userspace_sigreturn (void ) +{ + __asm__volatile ("movq $15, %rax" ); + __asm__volatile ("call *%0" : :"r" (__kernel_vsyscall) :); +} + +void mc_set_regs_ip_relay (mcontext_t *mc) +{ + mc->gregs[REG_RIP ] = (unsigned long ) userspace_sigreturn; +} +void mc_set_sigsys_hook (mcontext_t *mc) { mc->gregs[REG_RSP ] -=sizeof (unsigned long );
arch/x86/um/nommu/signal.c new file +43 -0
43 lines not on this page · raw message
arch/x86/um/shared/sysdep/mcontext.h +1 -0
@@ -9,6 +9,7 @@extern void get_regs_from_mc (struct uml_pt_regs *,mcontext_t *);#ifndef CONFIG_MMU extern void mc_set_sigsys_hook (mcontext_t *mc); +extern void mc_set_regs_ip_relay (mcontext_t *mc);#endif #ifdef __i386__
arch/x86/um/shared/sysdep/signal.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __SYSDEP_X86_64_SIGNAL_H__ +#define __SYSDEP_X86_64_SIGNAL_H__ + +#ifdef CONFIG_MMU +static inline int arch_setup_signal_stack_si (struct rt_sigframe **frame, +struct ksignal *ksig) +{ +return 0 ; +} +static inline struct rt_sigframe *arch_setup_rt_sigreturn (struct rt_sigframe *frame) +{ +return frame; +} +#else +extern int arch_setup_signal_stack_si (struct rt_sigframe **frame, +struct ksignal *ksig); +extern struct rt_sigframe *arch_setup_rt_sigreturn (struct rt_sigframe *frame); +#endif + +#endif
arch/x86/um/signal.c +16 -1
@@ -19,6 +19,8 @@#include <linux/regset.h> #include <asm/sigframe.h> +#include <sysdep/signal.h> + #ifdef CONFIG_X86_32 struct _xstate_64 { struct _fpstate_64 fpstate;
@@ -370,6 +372,13 @@ int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig, frame = (struct rt_sigframe __user *)round_down (stack_top -sizeof (struct rt_sigframe ),16 ); +#ifndef CONFIG_MMU +/* +* the sig_frame on !MMU needs be aligned for SSE as +* the frame is used as-is. +*/ + math_size =round_down (math_size,16 ); +#endif /* Add required space for math frame */ frame = (struct rt_sigframe __user *)((unsigned long )frame - math_size);
@@ -417,6 +426,8 @@ int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,/* could use a vstub here */ return err; +/* fixup rt_sigframe for nommu */ + err |=arch_setup_signal_stack_si (&frame, ksig);if (err)return err;
@@ -442,9 +453,13 @@ SYSCALL_DEFINE0(rt_sigreturn)unsigned long sp =PT_REGS_SP (¤t->thread.regs);struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(sp -sizeof (long )); -struct ucontext __user *uc = &frame->uc; +struct ucontext __user *uc;sigset_t set; +/* fixup rt_sigframe for nommu */ + frame =arch_setup_rt_sigreturn (frame); + uc = &frame->uc; +if (copy_from_user (&set, &uc->uc_sigmask,sizeof (set))) goto segfault;
Signature · 1 line
2.43.0
Re: [PATCH v4 10/13] x86/um: nommu: signal handling
Quoted 8 lines · Hajime Tazaki
This commit updates the behavior of signal handling under !MMU environment. 1) the stack preparation for the signal handlers and
2) restoration of stack afterrt_sigreturn(2)syscall. Those are needed as the stack usage on vfork(2) syscall is different.It also adds the follow up routine for SIGSEGV as a signal delivery runs in the same stack frame while we have to avoid endless SIGSEGV.
(snip)
Quoted 20 lines
arch/x86/um/nommu/signal.c new file +14
-0@@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/syscalls.h> +#include <linux/kernel.h> +#include <asm/sigframe.h> + +#include <sysdep/signal.h> + +int arch_setup_signal_stack_si (struct rt_sigframe __user **frame, +struct ksignal *ksig) +{ +int err =0 ; + +/*
arch/x86/um/nommu/signal.c line 24
Quoted 10 lines
arch/x86/um/nommu/signal.c new file +10
-0+* we need to push handler address at top of stack, as +* __kernel_vsyscall, called after this returns with ret with +* stack contents, thus push the handler here. +*/ + *frame = (struct rt_sigframe __user *) ((unsigned long ) *frame - +sizeof (unsigned long )); + err |=__put_user ((unsigned long )ksig->ka.sa.sa_handler, + (unsigned long *)*frame); + +return err;
this has a serious issue to handle signals.
I also found that manipulation of stack pointer makes the code cryptic..
I will clean up those parts in the next version.
-- Hajime
PATCH v4 11/13 um: change machine name for uname output
In reply to Hajime TazakiThis commit tries to display MMU/!MMU mode from the output of uname(2) so that users can distinguish which mode of UML is running right now.
--- arch/um/Makefile | 6 ++++++ arch/um/os-Linux/util.c | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-)
arch/um/Makefile +6 -0
@@ -155,6 +155,12 @@ export CFLAGS_vmlinux := $(LINK-y) $(LINK_WRAPS) $(LD_FLAGS_CMDLINE) $(CC_FLAGS_ CLEAN_FILES += linux x.i gmon.out MRPROPER_FILES += $(HOST_DIR)/include/generated +ifeq ($(CONFIG_MMU),y) +UTS_MACHINE := "um" +else +UTS_MACHINE := "um\(nommu\)" +endif + archclean: @find . \( -name '*.bb' -o -name '*.bbg' -o -name '*.da' \ -o -name '*.gcov' \) -type f -print | xargs rm -f
arch/um/os-Linux/util.c +2 -1
@@ -65,7 +65,8 @@ void setup_machinename(char *machine_out) }# endif #endif -strcpy (machine_out, host.machine); +strcat (machine_out,"/" ); +strcat (machine_out, host.machine); }void setup_hostinfo (char *buf,int len)
Signature · 1 line
2.43.0
PATCH v4 12/13 um: nommu: add documentation of nommu UML
In reply to Hajime TazakiThis commit adds an initial documentation for !MMU mode of UML.
--- Documentation/virt/uml/nommu-uml.rst | 177 +++++++++++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 178 insertions(+) create mode 100644 Documentation/virt/uml/nommu-uml.rst
Documentation/virt/uml/nommu-uml.rst new file +177 -0
@@ -0,0 +1,177 @@ +.. SPDX-License-Identifier: GPL-2.0 + +UML has been built with CONFIG_MMU since day 0. The patchset +introduces the nommu mode on UML in a different angle from what Linux +Kernel Library tried. + +.. contents:: :local: + +What is it for ? +================ + +- Alleviate syscall hook overhead implemented with ptrace(2) +- To exercises nommu code over UML (and over KUnit) +- Less dependency to host facilities + + +How it works ? +============== + +To illustrate how this feature works, the below shows how syscalls are +called under nommu/UML environment. + +- boot kernel, install seccomp filter if ``syscall`` instructions are + called from userspace memory based on the address of instruction + pointer +- (userspace starts) +- calls ``vfork``/``execve`` syscalls +- ``SIGSYS`` signal raised, handler calls syscall entry point ``__kernel_vsyscall`` +- call handler function in ``sys_call_table[]`` and follow how UML syscall + works. +- return to userspace + + +What are the differences from MMU-full UML ? +============================================ + +The current nommu implementation adds 3 different functions which +MMU-full UML doesn't have: + +- kernel address space can directly be accessible from userspace + - so, ``uaccess()`` always returns 1 + - generic implementation of memcpy/strcpy/futex is also used +- alternate syscall entrypoint without ptrace +- alternate syscall hook + - hook syscall by seccomp filter + +With those modifications, it allows us to use unmodified userspace +binaries with nommu UML. + + +History +======= + +This feature was originally introduced by Ricardo Koller at Open +Source Summit NA 2020, then integrated with the syscall translation +functionality with the clean up to the original code. + +Building and run +================ + +:: + + make ARCH=um x86_64_nommu_defconfig + make ARCH=um + +will build UML with ``CONFIG_MMU=n`` applied. + +Kunit tests can run with the following command:: + + ./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_MMU=n + +To run a typical Linux distribution, we need nommu-aware userspace. +We can use a stock version of Alpine Linux with nommu-built version of +busybox and musl-libc. + + +Preparing root filesystem +========================= + +nommu UML requires to use a specific standard library which is aware +of nommu kernel. We have tested custom-build musl-libc and busybox, +both of which have built-in support for nommu kernels. + +There are no available Linux distributions for nommu under x86_64 +architecture, so we need to prepare our own image for the root +filesystem. We use Alpine Linux as a base distribution and replace +busybox and musl-libc on top of that. The following are the step to +prepare the filesystem for the quick start:: + + container_id=$(docker create ghcr.io/thehajime/alpine:3.20.3-um-nommu) + docker start $container_id + docker wait $container_id + docker export $container_id > alpine.tar + docker rm $container_id + + mnt=$(mktemp -d) + dd if=/dev/zero of=alpine.ext4 bs=1 count=0 seek=1G + sudo chmod og+wr "alpine.ext4" + yes 2>/dev/null | mkfs.ext4 "alpine.ext4" || true + sudo mount "alpine.ext4" $mnt + sudo tar -xf alpine.tar -C $mnt + sudo umount $mnt + +This will create a file image, ``alpine.ext4``, which contains busybox +and musl with nommu build on the Alpine Linux root filesystem. The +file can be specified to the argument ``ubd0=`` to the UML command line:: + + ./vmlinux ubd0=./alpine.ext4 rw mem=1024m loglevel=8 init=/sbin/init + +We plan to upstream apk packages for busybox and musl so that we can +follow the proper procedure to set up the root filesystem. + + +Quick start with docker +======================= + +There is a docker image that you can quickly start with a simple step:: + + docker run -it -v /dev/shm:/dev/shm --rm ghcr.io/thehajime/alpine:3.20.3-um-nommu + +This will launch a UML instance with an pre-configured root filesystem. + +Benchmark +========= + +The below shows an example of performance measurement conducted with +lmbench and (self-crafted) getpid benchmark (with v6.12-rc2 uml/next +tree). + +.. csv-table:: lmbench (usec) + :header: ,native,um,um-nommu(s) + + select-10 ,0.5544,29.7143,2.8920 + select-100 ,2.3992,27.7262,3.7794 + select-1000 ,20.4708,42.0885,12.6920 + syscall ,0.1734,26.2471,2.6070 + read ,0.3433,29.8828,2.6923 + write ,0.2866,25.9753,2.6925 + stat ,1.9195,40.1164,3.1813 + open/close ,3.8657,63.4730,6.2049 + fork+sh ,1161.1111,5216.5000,462.3077 + fork+execve ,536.5263,2117.0000,131.0633 + +.. csv-table:: do_getpid bench (nsec) + :header: ,native,um,um-nommu(s) + + getpid, 172 , 26807 , 2614 + +Limitations +=========== + +generic nommu limitations +------------------------- +Since this port is a kernel of nommu architecture so, the +implementation inherits the characteristics of other nommu kernels +(riscv, arm, etc), described below. + +- vfork(2) should be used instead of fork(2) +- ELF loader only loads PIE (position independent executable) binaries +- processes share the address space among others +- mmap(2) offers a subset of functionalities (e.g., unsupported + MMAP_FIXED) + +Thus, we have limited options to userspace programs. We have tested +Alpine Linux with musl-libc, which has a support nommu kernel. + +supported architecture +---------------------- +The current implementation of nommu UML only works on x86_64 SUBARCH. +We have not tested with 32-bit environment. + + +Further readings about NOMMU UML +================================ + +- NOMMU UML (original code by Ricardo Koller) + - https://static.sched.com/hosted_files/ossna2020/ec/kollerr_linux_um_nommu.pdf
MAINTAINERS +1 -0
@@ -24186,6 +24186,7 @@ USER-MODE LINUX (UML) M: Richard Weinberger <[email protected]> M: Anton Ivanov <[email protected]> M: Johannes Berg <[email protected]> +M: Hajime Tazaki <[email protected]> L: [email protected] S: Maintained W: http://user-mode-linux.sourceforge.net
Signature · 1 line
2.43.0
PATCH v4 13/13 um: nommu: plug nommu code into build system
In reply to Hajime TazakiAdd nommu kernel for um build. defconfig is also provided.
--- arch/um/Kconfig | 14 +++++- arch/um/configs/x86_64_nommu_defconfig | 64 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 arch/um/configs/x86_64_nommu_defconfig
arch/um/Kconfig +12 -2
@@ -30,14 +30,17 @@ config UML select ARCH_SUPPORTS_LTO_CLANG_THIN select TRACE_IRQFLAGS_SUPPORT select TTY # Needed for line.c - select HAVE_ARCH_VMAP_STACK + select HAVE_ARCH_VMAP_STACK if MMU select HAVE_RUST select ARCH_HAS_UBSAN select HAVE_ARCH_TRACEHOOK select THREAD_INFO_IN_TASK + select UACCESS_MEMCPY if !MMU + select GENERIC_STRNLEN_USER if !MMU + select GENERIC_STRNCPY_FROM_USER if !MMU config MMU - bool + bool "MMU-based Paged Memory Management Support" if 64BIT default y config UML_DMA_EMULATION
@@ -190,8 +193,15 @@ config MAGIC_SYSRQ The keys are documented in <file:Documentation/admin-guide/sysrq.rst>. Don't say Y unless you really know what this hack does. +config ARCH_FORCE_MAX_ORDER + int "Order of maximal physically contiguous allocations" if EXPERT + default "10" if MMU + default "16" if !MMU + config KERNEL_STACK_ORDER int "Kernel stack size order" + default 3 if !MMU + range 3 10 if !MMU default 2 if 64BIT range 2 10 if 64BIT default 1 if !64BIT
arch/um/configs/x86_64_nommu_defconfig new file +64 -0
64 lines not on this page · raw message
Signature · 1 line
2.43.0